1/*
2 * Copyright (C) 2016 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 */
16package com.android.internal.telephony;
17
18import android.test.suitebuilder.annotation.SmallTest;
19import com.android.internal.telephony.VisualVoicemailSmsParser.WrappedMessageData;
20import junit.framework.TestCase;
21
22public class VisualVoicemailSmsParserTest extends TestCase {
23
24    @SmallTest
25    public void testParsing() {
26        WrappedMessageData result = VisualVoicemailSmsParser.parse("//VVM",
27                "//VVM:STATUS:st=R;rc=0;srv=1;dn=1;ipt=1;spt=0;u=eg@example.com;pw=1");
28
29        assertEquals("STATUS", result.prefix);
30        assertEquals("R", result.fields.getString("st"));
31        assertEquals("0", result.fields.getString("rc"));
32        assertEquals("1", result.fields.getString("srv"));
33        assertEquals("1", result.fields.getString("dn"));
34        assertEquals("1", result.fields.getString("ipt"));
35        assertEquals("0", result.fields.getString("spt"));
36        assertEquals("eg@example.com", result.fields.getString("u"));
37        assertEquals("1", result.fields.getString("pw"));
38    }
39
40    @SmallTest
41    public void testParsing_TrailingSemiColon() {
42        WrappedMessageData result = VisualVoicemailSmsParser.parse("//VVM",
43                "//VVM:STATUS:st=R;rc=0;srv=1;dn=1;ipt=1;spt=0;u=eg@example.com;pw=1;");
44
45        assertEquals("STATUS", result.prefix);
46        assertEquals("R", result.fields.getString("st"));
47        assertEquals("0", result.fields.getString("rc"));
48        assertEquals("1", result.fields.getString("srv"));
49        assertEquals("1", result.fields.getString("dn"));
50        assertEquals("1", result.fields.getString("ipt"));
51        assertEquals("0", result.fields.getString("spt"));
52        assertEquals("eg@example.com", result.fields.getString("u"));
53        assertEquals("1", result.fields.getString("pw"));
54    }
55
56    @SmallTest
57    public void testParsing_EmptyPrefix() {
58        WrappedMessageData result = VisualVoicemailSmsParser.parse("//VVM",
59                "//VVM::st=R;rc=0;srv=1;dn=1;ipt=1;spt=0;u=eg@example.com;pw=1");
60
61        assertEquals("", result.prefix);
62        assertEquals("R", result.fields.getString("st"));
63        assertEquals("0", result.fields.getString("rc"));
64        assertEquals("1", result.fields.getString("srv"));
65        assertEquals("1", result.fields.getString("dn"));
66        assertEquals("1", result.fields.getString("ipt"));
67        assertEquals("0", result.fields.getString("spt"));
68        assertEquals("eg@example.com", result.fields.getString("u"));
69        assertEquals("1", result.fields.getString("pw"));
70    }
71
72    @SmallTest
73    public void testParsing_EmptyField() {
74        WrappedMessageData result = VisualVoicemailSmsParser.parse("//VVM",
75                "//VVM:STATUS:");
76        assertTrue(result.fields.isEmpty());
77    }
78
79    @SmallTest
80    public void testParsingFail_NotVvm() {
81        WrappedMessageData result = VisualVoicemailSmsParser.parse("//VVM",
82                "helloworld");
83
84        assertNull(result);
85    }
86
87    @SmallTest
88    public void testParsingFail_PrefixMismatch() {
89        WrappedMessageData result = VisualVoicemailSmsParser.parse("//VVM",
90                "//ANDROIDVVM:STATUS:st=R;rc=0;srv=1;dn=1;ipt=1;spt=0;u=eg@example.com;pw=1");
91
92        assertNull(result);
93    }
94
95    @SmallTest
96    public void testParsingFail_MissingFirstColon() {
97        WrappedMessageData result = VisualVoicemailSmsParser.parse("//VVM",
98                "//VVMSTATUS:st=R;rc=0;srv=1;dn=1;ipt=1;spt=0;u=eg@example.com;pw=1");
99
100        assertNull(result);
101    }
102
103    @SmallTest
104    public void testParsingFail_MissingSecondColon() {
105        WrappedMessageData result = VisualVoicemailSmsParser.parse("//VVM",
106                "//VVM:STATUSst=R;rc=0;srv=1;dn=1;ipt=1;spt=0;u=eg@example.com;pw=1");
107
108        assertNull(result);
109    }
110
111    @SmallTest
112    public void testParsingFail_MessageEndAfterClientPrefix() {
113        WrappedMessageData result = VisualVoicemailSmsParser.parse("//VVM",
114                "//VVM:");
115
116        assertNull(result);
117    }
118
119    @SmallTest
120    public void testParsingFail_MessageEndAfterPrefix() {
121        WrappedMessageData result = VisualVoicemailSmsParser.parse("//VVM",
122                "//VVM:STATUS");
123
124        assertNull(result);
125    }
126
127    @SmallTest
128    public void testParsingFail_InvalidKeyValuePair() {
129        WrappedMessageData result = VisualVoicemailSmsParser.parse("//VVM",
130                "//VVM:STATUS:key");
131        assertNull(result);
132    }
133
134    @SmallTest
135    public void testParsingFail_InvalidMissingKey() {
136        WrappedMessageData result = VisualVoicemailSmsParser.parse("//VVM",
137                "//VVM:STATUS:=value");
138        assertNull(result);
139    }
140
141    @SmallTest
142    public void testParsingFail_MissingValue() {
143        WrappedMessageData result = VisualVoicemailSmsParser.parse("//VVM",
144                "//VVM:STATUS:key=");
145        assertEquals("STATUS", result.prefix);
146        assertEquals("", result.fields.getString("key"));
147    }
148
149    @SmallTest
150    public void testAlternativeParsing_Mboxupdate() {
151        WrappedMessageData result = VisualVoicemailSmsParser.parseAlternativeFormat(
152            "MBOXUPDATE?m=1;server=example.com;port=143;name=foo@example.com;pw=bar");
153
154        assertEquals("MBOXUPDATE", result.prefix);
155        assertEquals("1", result.fields.getString("m"));
156        assertEquals("example.com", result.fields.getString("server"));
157        assertEquals("143", result.fields.getString("port"));
158        assertEquals("foo@example.com", result.fields.getString("name"));
159        assertEquals("bar", result.fields.getString("pw"));
160    }
161
162    @SmallTest
163    public void testAlternativeParsing_Unrecognized() {
164        WrappedMessageData result = VisualVoicemailSmsParser.parseAlternativeFormat(
165            "UNRECOGNIZED?cmd=STATUS");
166
167        assertEquals("UNRECOGNIZED", result.prefix);
168        assertEquals("STATUS", result.fields.getString("cmd"));
169    }
170
171    @SmallTest
172    public void testAlternativeParsingFail_MissingSeparator() {
173        WrappedMessageData result = VisualVoicemailSmsParser.parseAlternativeFormat(
174            "I send SMS in weird formats");
175
176        assertNull(result);
177    }
178
179    @SmallTest
180    public void testAlternativeParsingFail_NotWhitelistedEvent() {
181        WrappedMessageData result = VisualVoicemailSmsParser.parseAlternativeFormat(
182            "AreYouStillThere?");
183
184        assertNull(result);
185    }
186}
187