• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 
17 package com.android.cellbroadcastreceiver;
18 
19 import android.content.Intent;
20 import android.os.PersistableBundle;
21 import android.provider.Telephony;
22 import android.telephony.CarrierConfigManager;
23 import android.telephony.CellBroadcastMessage;
24 import android.telephony.SmsCbCmasInfo;
25 import android.telephony.SmsCbEtwsInfo;
26 import android.telephony.SmsCbLocation;
27 import android.telephony.SmsCbMessage;
28 import android.util.Log;
29 
30 import com.android.internal.telephony.gsm.SmsCbConstants;
31 
32 import org.junit.After;
33 import org.junit.Before;
34 
35 import java.util.ArrayList;
36 
37 import static com.android.cellbroadcastreceiver.CellBroadcastAlertAudio.ALERT_AUDIO_TONE_TYPE;
38 import static com.android.cellbroadcastreceiver.CellBroadcastAlertService.SHOW_NEW_ALERT_ACTION;
39 import static org.junit.Assert.assertArrayEquals;
40 import static org.mockito.Matchers.anyInt;
41 import static org.mockito.Mockito.doReturn;
42 
43 public class CellBroadcastAlertServiceTest extends
44         CellBroadcastServiceTestCase<CellBroadcastAlertService> {
45 
CellBroadcastAlertServiceTest()46     public CellBroadcastAlertServiceTest() {
47         super(CellBroadcastAlertService.class);
48     }
49 
createMessage(int serialNumber)50     static SmsCbMessage createMessage(int serialNumber) {
51         return new SmsCbMessage(1, 2, serialNumber, new SmsCbLocation(),
52                 SmsCbConstants.MESSAGE_ID_CMAS_ALERT_PRESIDENTIAL_LEVEL, "language", "body",
53                 SmsCbMessage.MESSAGE_PRIORITY_EMERGENCY, null, new SmsCbCmasInfo(1, 2, 3, 4, 5, 6));
54     }
55 
56     @Before
setUp()57     public void setUp() throws Exception {
58         super.setUp();
59     }
60 
61     @After
tearDown()62     public void tearDown() throws Exception {
63         super.tearDown();
64     }
65 
compareEtwsWarningInfo(SmsCbEtwsInfo info1, SmsCbEtwsInfo info2)66     private static void compareEtwsWarningInfo(SmsCbEtwsInfo info1, SmsCbEtwsInfo info2) {
67         if (info1 == info2) return;
68         assertEquals(info1.toString(), info2.toString());
69         assertArrayEquals(info1.getPrimaryNotificationSignature(),
70                 info2.getPrimaryNotificationSignature());
71         assertEquals(info1.isPrimary(), info2.isPrimary());
72     }
73 
compareCmasWarningInfo(SmsCbCmasInfo info1, SmsCbCmasInfo info2)74     private static void compareCmasWarningInfo(SmsCbCmasInfo info1, SmsCbCmasInfo info2) {
75         if (info1 == info2) return;
76         assertEquals(info1.getCategory(), info2.getCategory());
77         assertEquals(info1.getCertainty(), info2.getCertainty());
78         assertEquals(info1.getMessageClass(), info2.getMessageClass());
79         assertEquals(info1.getResponseType(), info2.getResponseType());
80         assertEquals(info1.getSeverity(), info2.getSeverity());
81         assertEquals(info1.getUrgency(), info2.getUrgency());
82     }
83 
compareCellBroadCastMessage(CellBroadcastMessage cbm1, CellBroadcastMessage cbm2)84     private static void compareCellBroadCastMessage(CellBroadcastMessage cbm1,
85                                                     CellBroadcastMessage cbm2) {
86         if (cbm1 == cbm2) return;
87         assertEquals(cbm1.getCmasMessageClass(), cbm2.getCmasMessageClass());
88         compareCmasWarningInfo(cbm1.getCmasWarningInfo(), cbm2.getCmasWarningInfo());
89         compareEtwsWarningInfo(cbm1.getEtwsWarningInfo(), cbm2.getEtwsWarningInfo());
90         assertEquals(cbm1.getLanguageCode(), cbm2.getLanguageCode());
91         assertEquals(cbm1.getMessageBody(), cbm2.getMessageBody());
92         assertEquals(cbm1.getSerialNumber(), cbm2.getSerialNumber());
93         assertEquals(cbm1.getServiceCategory(), cbm2.getServiceCategory());
94         assertEquals(cbm1.getSubId(), cbm2.getSubId());
95         assertEquals(cbm1.getSerialNumber(), cbm2.getSerialNumber());
96     }
97 
sendMessage(int serialNumber)98     private void sendMessage(int serialNumber) {
99         Intent intent = new Intent(mContext, CellBroadcastAlertService.class);
100         intent.setAction(Telephony.Sms.Intents.SMS_EMERGENCY_CB_RECEIVED_ACTION);
101 
102         SmsCbMessage m = createMessage(serialNumber);
103         sendMessage(m, intent);
104     }
105 
sendMessage(SmsCbMessage m, Intent intent)106     private void sendMessage(SmsCbMessage m, Intent intent) {
107         intent.putExtra("message", m);
108         startService(intent);
109     }
110 
111     // Test handleCellBroadcastIntent method
testHandleCellBroadcastIntent()112     public void testHandleCellBroadcastIntent() throws Exception {
113         sendMessage(987654321);
114         waitForMs(200);
115 
116         assertEquals(SHOW_NEW_ALERT_ACTION, mServiceIntentToVerify.getAction());
117 
118         CellBroadcastMessage cbmTest =
119                 (CellBroadcastMessage) mServiceIntentToVerify.getExtras().get("message");
120         CellBroadcastMessage cbm = new CellBroadcastMessage(createMessage(987654321));
121 
122         compareCellBroadCastMessage(cbm, cbmTest);
123     }
124 
125     // Test showNewAlert method
testShowNewAlert()126     public void testShowNewAlert() throws Exception {
127         Intent intent = new Intent(mContext, CellBroadcastAlertService.class);
128         intent.setAction(SHOW_NEW_ALERT_ACTION);
129         SmsCbMessage message = createMessage(34788612);
130         intent.putExtra("message", new CellBroadcastMessage(message));
131         startService(intent);
132         waitForMs(200);
133 
134         // verify audio service intent
135         assertEquals(CellBroadcastAlertAudio.ACTION_START_ALERT_AUDIO,
136                 mServiceIntentToVerify.getAction());
137         assertEquals(CellBroadcastAlertAudio.ToneType.CMAS_DEFAULT,
138                 mServiceIntentToVerify.getSerializableExtra(ALERT_AUDIO_TONE_TYPE));
139         assertEquals(message.getMessageBody(),
140                 mServiceIntentToVerify.getStringExtra(
141                         CellBroadcastAlertAudio.ALERT_AUDIO_MESSAGE_BODY));
142 
143         // verify alert dialog activity intent
144         ArrayList<CellBroadcastMessage> newMessageList = mActivityIntentToVerify
145                 .getParcelableArrayListExtra(CellBroadcastMessage.SMS_CB_MESSAGE_EXTRA);
146         assertEquals(1, newMessageList.size());
147         assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK,
148                 (mActivityIntentToVerify.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK));
149         compareCellBroadCastMessage(new CellBroadcastMessage(message), newMessageList.get(0));
150     }
151 
152     // Test if we ignore the duplicate message
testDuplicateMessage()153     public void testDuplicateMessage() throws Exception {
154         sendMessage(4321);
155         waitForMs(200);
156 
157         assertEquals(SHOW_NEW_ALERT_ACTION, mServiceIntentToVerify.getAction());
158 
159         CellBroadcastMessage cbmTest =
160                 (CellBroadcastMessage) mServiceIntentToVerify.getExtras().get("message");
161         CellBroadcastMessage cbm = new CellBroadcastMessage(createMessage(4321));
162 
163         compareCellBroadCastMessage(cbm, cbmTest);
164 
165         mServiceIntentToVerify = null;
166 
167         Intent intent = new Intent(mContext, CellBroadcastAlertService.class);
168         intent.setAction(Telephony.Sms.Intents.SMS_EMERGENCY_CB_RECEIVED_ACTION);
169 
170         SmsCbMessage m = createMessage(4321);
171         intent.putExtra("message", m);
172 
173         startService(intent);
174         waitForMs(200);
175 
176         // If the duplicate detection is working, the service should not pop-up the dialog and
177         // play the alert tones.
178         assertNull(mServiceIntentToVerify);
179     }
180 
181     // Test if we allow non-duplicate message
testNonDuplicateMessage()182     public void testNonDuplicateMessage() throws Exception {
183         sendMessage(187286123);
184 
185         mServiceIntentToVerify = null;
186 
187         Intent intent = new Intent(mContext, CellBroadcastAlertService.class);
188         intent.setAction(Telephony.Sms.Intents.SMS_EMERGENCY_CB_RECEIVED_ACTION);
189 
190         SmsCbMessage m = createMessage(129487394);
191         intent.putExtra("message", m);
192 
193         startService(intent);
194         waitForMs(200);
195 
196         assertEquals(SHOW_NEW_ALERT_ACTION, mServiceIntentToVerify.getAction());
197         assertEquals(CellBroadcastAlertService.class.getName(),
198                 intent.getComponent().getClassName());
199 
200         CellBroadcastMessage cbmTest =
201                 (CellBroadcastMessage) mServiceIntentToVerify.getExtras().get("message");
202         CellBroadcastMessage cbm = new CellBroadcastMessage(m);
203 
204         compareCellBroadCastMessage(cbm, cbmTest);
205     }
206 
207     // Test when we reach the maximum messages, the oldest one should be evicted.
testMaximumMessages()208     public void testMaximumMessages() throws Exception {
209         for (int i = 0; i < 1024 + 1; i++) {
210             sendMessage(i);
211             waitForMs(50);
212         }
213 
214         sendMessage(0);
215         waitForMs(40);
216         // Check if the oldest one has been already evicted.
217         CellBroadcastMessage cbmTest =
218                 (CellBroadcastMessage) mServiceIntentToVerify.getExtras().get("message");
219         CellBroadcastMessage cbm = new CellBroadcastMessage(createMessage(0));
220 
221         compareCellBroadCastMessage(cbm, cbmTest);
222         mActivityIntentToVerify = null;
223     }
224 
testExpiration()225     public void testExpiration() throws Exception {
226         PersistableBundle b = new PersistableBundle();
227         b.putLong(CarrierConfigManager.KEY_MESSAGE_EXPIRATION_TIME_LONG, 1000);
228         doReturn(b).when(mMockedCarrierConfigManager).getConfigForSubId(anyInt());
229 
230         sendMessage(91924);
231         waitForMs(100);
232 
233         CellBroadcastMessage cbmTest =
234                 (CellBroadcastMessage) mServiceIntentToVerify.getExtras().get("message");
235         assertEquals(91924, cbmTest.getSerialNumber());
236         mServiceIntentToVerify = null;
237 
238         // Wait until it expires.
239         waitForMs(1500);
240         sendMessage(91924);
241         waitForMs(100);
242 
243         // Since the previous one has already expired, this one should not be treated as a duplicate
244         cbmTest = (CellBroadcastMessage) mServiceIntentToVerify.getExtras().get("message");
245         assertEquals(91924, cbmTest.getSerialNumber());
246 
247         waitForMs(500);
248         mServiceIntentToVerify = null;
249         // This one should be treated as a duplicate since it's not expired yet.
250         sendMessage(91924);
251         assertNull(mServiceIntentToVerify);
252 
253     }
254 }