1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.bluetooth.mapclient;
18 
19 import static org.mockito.Mockito.*;
20 
21 import androidx.test.filters.MediumTest;
22 import androidx.test.runner.AndroidJUnit4;
23 
24 import org.junit.Assert;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 
28 @MediumTest
29 @RunWith(AndroidJUnit4.class)
30 public class BmessageTest {
31     private static final String TAG = BmessageTest.class.getSimpleName();
32     private static final String SIMPLE_MMS_MESSAGE =
33             "BEGIN:BMSG\r\nVERSION:1.0\r\nSTATUS:READ\r\nTYPE:MMS\r\nFOLDER:null\r\nBEGIN:BENV\r\n"
34             + "BEGIN:VCARD\r\nVERSION:2.1\r\nN:null;;;;\r\nTEL:555-5555\r\nEND:VCARD\r\n"
35             + "BEGIN:BBODY\r\nLENGTH:39\r\nBEGIN:MSG\r\nThis is a new msg\r\nEND:MSG\r\n"
36             + "END:BBODY\r\nEND:BENV\r\nEND:BMSG\r\n";
37 
38     private static final String NO_END_MESSAGE =
39             "BEGIN:BMSG\r\nVERSION:1.0\r\nSTATUS:READ\r\nTYPE:MMS\r\nFOLDER:null\r\nBEGIN:BENV\r\n"
40             + "BEGIN:VCARD\r\nVERSION:2.1\r\nN:null;;;;\r\nTEL:555-5555\r\nEND:VCARD\r\n"
41             + "BEGIN:BBODY\r\nLENGTH:39\r\nBEGIN:MSG\r\nThis is a new msg\r\n";
42 
43     private static final String WRONG_LENGTH_MESSAGE =
44             "BEGIN:BMSG\r\nVERSION:1.0\r\nSTATUS:READ\r\nTYPE:MMS\r\nFOLDER:null\r\nBEGIN:BENV\r\n"
45             + "BEGIN:VCARD\r\nVERSION:2.1\r\nN:null;;;;\r\nTEL:555-5555\r\nEND:VCARD\r\n"
46             + "BEGIN:BBODY\r\nLENGTH:200\r\nBEGIN:MSG\r\nThis is a new msg\r\nEND:MSG\r\n"
47             + "END:BBODY\r\nEND:BENV\r\nEND:BMSG\r\n";
48 
49     private static final String NO_BODY_MESSAGE =
50             "BEGIN:BMSG\r\nVERSION:1.0\r\nSTATUS:READ\r\nTYPE:MMS\r\nFOLDER:null\r\nBEGIN:BENV\r\n"
51             + "BEGIN:VCARD\r\nVERSION:2.1\r\nN:null;;;;\r\nTEL:555-5555\r\nEND:VCARD\r\n"
52             + "BEGIN:BBODY\r\nLENGTH:\r\n";
53 
54     private static final String NEGATIVE_LENGTH_MESSAGE =
55             "BEGIN:BMSG\r\nVERSION:1.0\r\nSTATUS:READ\r\nTYPE:MMS\r\nFOLDER:null\r\nBEGIN:BENV\r\n"
56             + "BEGIN:VCARD\r\nVERSION:2.1\r\nN:null;;;;\r\nTEL:555-5555\r\nEND:VCARD\r\n"
57             + "BEGIN:BBODY\r\nLENGTH:-1\r\nBEGIN:MSG\r\nThis is a new msg\r\nEND:MSG\r\n"
58             + "END:BBODY\r\nEND:BENV\r\nEND:BMSG\r\n";
59 
60     @Test
testNormalMessages()61     public void testNormalMessages() {
62         Bmessage message = BmessageParser.createBmessage(SIMPLE_MMS_MESSAGE);
63         Assert.assertNotNull(message);
64     }
65 
66     @Test
testParseWrongLengthMessage()67     public void testParseWrongLengthMessage() {
68         Bmessage message = BmessageParser.createBmessage(WRONG_LENGTH_MESSAGE);
69         Assert.assertNull(message);
70     }
71 
72     @Test
testParseNoEndMessage()73     public void testParseNoEndMessage() {
74         Bmessage message = BmessageParser.createBmessage(NO_END_MESSAGE);
75         Assert.assertNull(message);
76     }
77 
78     @Test
testParseReallyLongMessage()79     public void testParseReallyLongMessage() {
80         String testMessage = new String(new char[68048]).replace('\0', 'A');
81         Bmessage message = BmessageParser.createBmessage(testMessage);
82         Assert.assertNull(message);
83     }
84 
85     @Test
testNoBodyMessage()86     public void testNoBodyMessage() {
87         Bmessage message = BmessageParser.createBmessage(NO_BODY_MESSAGE);
88         Assert.assertNull(message);
89     }
90 
91     @Test
testNegativeLengthMessage()92     public void testNegativeLengthMessage() {
93         Bmessage message = BmessageParser.createBmessage(NEGATIVE_LENGTH_MESSAGE);
94         Assert.assertNull(message);
95     }
96 }
97