aboutsummaryrefslogtreecommitdiff
path: root/tests/telephonytests/src/com/android/internal/telephony/cdma/CdmaSmsCbTest.java
blob: 3cbc501d95c70d6f4cd963fa22a4218df00b559b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.telephony.cdma;

import android.hardware.radio.V1_0.CdmaSmsMessage;
import android.telephony.Rlog;
import android.telephony.SmsCbCmasInfo;
import android.telephony.SmsCbMessage;
import android.telephony.cdma.CdmaSmsCbProgramData;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import androidx.test.filters.FlakyTest;

import com.android.internal.telephony.GsmAlphabet;
import com.android.internal.telephony.cdma.sms.BearerData;
import com.android.internal.telephony.cdma.sms.CdmaSmsAddress;
import com.android.internal.telephony.cdma.sms.SmsEnvelope;
import com.android.internal.telephony.cdma.sms.UserData;
import com.android.internal.util.BitwiseOutputStream;

import org.junit.Ignore;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * Test cases for basic SmsCbMessage operation for CDMA.
 */
public class CdmaSmsCbTest extends AndroidTestCase {

    /* Copy of private subparameter identifier constants from BearerData class. */
    private static final byte SUBPARAM_MESSAGE_IDENTIFIER   = (byte) 0x00;
    private static final byte SUBPARAM_USER_DATA            = (byte) 0x01;
    private static final byte SUBPARAM_PRIORITY_INDICATOR   = (byte) 0x08;
    private static final byte SUBPARAM_LANGUAGE_INDICATOR   = (byte) 0x0D;
    private static final byte SUBPARAM_SERVICE_CATEGORY_PROGRAM_DATA    = 0x12;

    /**
     * Initialize a Parcel for an incoming CDMA cell broadcast. The caller will write the
     * bearer data and then convert it to an SmsMessage.
     * @param serviceCategory the CDMA service category
     * @return the initialized Parcel
     */
    private static CdmaSmsMessage createBroadcastParcel(int serviceCategory) {
        CdmaSmsMessage msg = new CdmaSmsMessage();

        msg.teleserviceId = SmsEnvelope.TELESERVICE_NOT_SET;
        msg.isServicePresent = true;
        msg.serviceCategory = serviceCategory;

        // dummy address (RIL may generate a different dummy address for broadcasts)
        msg.address.digitMode = CdmaSmsAddress.DIGIT_MODE_4BIT_DTMF;
        msg.address.numberMode = CdmaSmsAddress.NUMBER_MODE_NOT_DATA_NETWORK;
        msg.address.numberType = CdmaSmsAddress.TON_UNKNOWN;
        msg.address.numberPlan = CdmaSmsAddress.NUMBERING_PLAN_ISDN_TELEPHONY;
        msg.subAddress.subaddressType = 0;
        msg.subAddress.odd = false;
        return msg;
    }

    /**
     * Initialize a BitwiseOutputStream with the CDMA bearer data subparameters except for
     * user data. The caller will append the user data and add it to the parcel.
     * @param messageId the 16-bit message identifier
     * @param priority message priority
     * @param language message language code
     * @return the initialized BitwiseOutputStream
     */
    private static BitwiseOutputStream createBearerDataStream(int messageId, int priority,
            int language) throws BitwiseOutputStream.AccessException {
        BitwiseOutputStream bos = new BitwiseOutputStream(10);
        bos.write(8, SUBPARAM_MESSAGE_IDENTIFIER);
        bos.write(8, 3);    // length: 3 bytes
        bos.write(4, BearerData.MESSAGE_TYPE_DELIVER);
        bos.write(8, ((messageId >>> 8) & 0xff));
        bos.write(8, (messageId & 0xff));
        bos.write(1, 0);    // no User Data Header
        bos.write(3, 0);    // reserved

        if (priority != -1) {
            bos.write(8, SUBPARAM_PRIORITY_INDICATOR);
            bos.write(8, 1);    // length: 1 byte
            bos.write(2, (priority & 0x03));
            bos.write(6, 0);    // reserved
        }

        if (language != -1) {
            bos.write(8, SUBPARAM_LANGUAGE_INDICATOR);
            bos.write(8, 1);    // length: 1 byte
            bos.write(8, (language & 0xff));
        }

        return bos;
    }

    /**
     * Write the bearer data array to the parcel, then return a new SmsMessage from the parcel.
     * @param msg CdmaSmsMessage containing the CDMA SMS headers
     * @param bearerData the bearer data byte array to append to the parcel
     * @return the new SmsMessage created from the parcel
     */
    private static SmsMessage createMessageFromParcel(CdmaSmsMessage msg, byte[] bearerData) {
        for (byte b : bearerData) {
            msg.bearerData.add(b);
        }
        SmsMessage message = SmsMessageConverter.newCdmaSmsMessageFromRil(msg);
        return message;
    }

    /**
     * Create a parcel for an incoming CMAS broadcast, then return a new SmsMessage created
     * from the parcel.
     * @param serviceCategory the CDMA service category
     * @param messageId the 16-bit message identifier
     * @param priority message priority
     * @param language message language code
     * @param body message body
     * @param cmasCategory CMAS category (or -1 to skip adding CMAS type 1 elements record)
     * @param responseType CMAS response type
     * @param severity CMAS severity
     * @param urgency CMAS urgency
     * @param certainty CMAS certainty
     * @return the newly created SmsMessage object
     */
    private static SmsMessage createCmasSmsMessage(int serviceCategory, int messageId, int priority,
            int language, int encoding, String body, int cmasCategory, int responseType,
            int severity, int urgency, int certainty) throws Exception {
        BitwiseOutputStream cmasBos = new BitwiseOutputStream(10);
        cmasBos.write(8, 0);    // CMAE protocol version 0

        if (body != null) {
            cmasBos.write(8, 0);        // Type 0 elements (alert text)
            encodeBody(encoding, body, true, cmasBos);
        }

        if (cmasCategory != SmsCbCmasInfo.CMAS_CATEGORY_UNKNOWN) {
            cmasBos.write(8, 1);    // Type 1 elements
            cmasBos.write(8, 4);    // length: 4 bytes
            cmasBos.write(8, (cmasCategory & 0xff));
            cmasBos.write(8, (responseType & 0xff));
            cmasBos.write(4, (severity & 0x0f));
            cmasBos.write(4, (urgency & 0x0f));
            cmasBos.write(4, (certainty & 0x0f));
            cmasBos.write(4, 0);    // pad to octet boundary
        }

        byte[] cmasUserData = cmasBos.toByteArray();

        CdmaSmsMessage msg = createBroadcastParcel(serviceCategory);
        BitwiseOutputStream bos = createBearerDataStream(messageId, priority, language);

        bos.write(8, SUBPARAM_USER_DATA);
        bos.write(8, cmasUserData.length + 2);  // add 2 bytes for msg_encoding and num_fields
        bos.write(5, UserData.ENCODING_OCTET);
        bos.write(8, cmasUserData.length);
        bos.writeByteArray(cmasUserData.length * 8, cmasUserData);
        bos.write(3, 0);    // pad to byte boundary

        return createMessageFromParcel(msg, bos.toByteArray());
    }

    /**
     * Create a parcel for an incoming CDMA cell broadcast, then return a new SmsMessage created
     * from the parcel.
     * @param serviceCategory the CDMA service category
     * @param messageId the 16-bit message identifier
     * @param priority message priority
     * @param language message language code
     * @param encoding user data encoding method
     * @param body the message body
     * @return the newly created SmsMessage object
     */
    private static SmsMessage createBroadcastSmsMessage(int serviceCategory, int messageId,
            int priority, int language, int encoding, String body) throws Exception {
        CdmaSmsMessage msg = createBroadcastParcel(serviceCategory);
        BitwiseOutputStream bos = createBearerDataStream(messageId, priority, language);

        bos.write(8, SUBPARAM_USER_DATA);
        encodeBody(encoding, body, false, bos);

        return createMessageFromParcel(msg, bos.toByteArray());
    }

    /**
     * Append the message length, encoding, and body to the BearerData output stream.
     * This is used for writing the User Data subparameter for non-CMAS broadcasts and for
     * writing the alert text for CMAS broadcasts.
     * @param encoding one of the CDMA UserData encoding values
     * @param body the message body
     * @param isCmasRecord true if this is a CMAS type 0 elements record; false for user data
     * @param bos the BitwiseOutputStream to write to
     * @throws Exception on any encoding error
     */
    private static void encodeBody(int encoding, String body, boolean isCmasRecord,
            BitwiseOutputStream bos) throws Exception {
        if (encoding == UserData.ENCODING_7BIT_ASCII || encoding == UserData.ENCODING_IA5) {
            int charCount = body.length();
            int recordBits = (charCount * 7) + 5;       // add 5 bits for char set field
            int recordOctets = (recordBits + 7) / 8;    // round up to octet boundary
            int padBits = (recordOctets * 8) - recordBits;

            if (!isCmasRecord) {
                recordOctets++;                         // add 8 bits for num_fields
            }

            bos.write(8, recordOctets);
            bos.write(5, (encoding & 0x1f));

            if (!isCmasRecord) {
                bos.write(8, charCount);
            }

            for (int i = 0; i < charCount; i++) {
                bos.write(7, body.charAt(i));
            }

            bos.write(padBits, 0);      // pad to octet boundary
        } else if (encoding == UserData.ENCODING_GSM_7BIT_ALPHABET
                || encoding == UserData.ENCODING_GSM_DCS) {
            // convert to 7-bit packed encoding with septet count in index 0 of byte array
            byte[] encodedBody = GsmAlphabet.stringToGsm7BitPacked(body);

            int charCount = encodedBody[0];             // septet count
            int recordBits = (charCount * 7) + 5;       // add 5 bits for char set field
            int recordOctets = (recordBits + 7) / 8;    // round up to octet boundary
            int padBits = (recordOctets * 8) - recordBits;

            if (!isCmasRecord) {
                recordOctets++;                         // add 8 bits for num_fields
                if (encoding == UserData.ENCODING_GSM_DCS) {
                    recordOctets++;                     // add 8 bits for DCS (message type)
                }
            }

            bos.write(8, recordOctets);
            bos.write(5, (encoding & 0x1f));

            if (!isCmasRecord && encoding == UserData.ENCODING_GSM_DCS) {
                bos.write(8, 0);        // GSM DCS: 7 bit default alphabet, no msg class
            }

            if (!isCmasRecord) {
                bos.write(8, charCount);
            }
            byte[] bodySeptets = Arrays.copyOfRange(encodedBody, 1, encodedBody.length);
            bos.writeByteArray(charCount * 7, bodySeptets);
            bos.write(padBits, 0);      // pad to octet boundary
        } else if (encoding == UserData.ENCODING_IS91_EXTENDED_PROTOCOL) {
            // 6 bit packed encoding with 0x20 offset (ASCII 0x20 - 0x60)
            int charCount = body.length();
            int recordBits = (charCount * 6) + 21;      // add 21 bits for header fields
            int recordOctets = (recordBits + 7) / 8;    // round up to octet boundary
            int padBits = (recordOctets * 8) - recordBits;

            bos.write(8, recordOctets);

            bos.write(5, (encoding & 0x1f));
            bos.write(8, UserData.IS91_MSG_TYPE_SHORT_MESSAGE);
            bos.write(8, charCount);

            for (int i = 0; i < charCount; i++) {
                bos.write(6, ((int) body.charAt(i) - 0x20));
            }

            bos.write(padBits, 0);      // pad to octet boundary
        } else {
            byte[] encodedBody;
            switch (encoding) {
                case UserData.ENCODING_UNICODE_16:
                    encodedBody = body.getBytes("UTF-16BE");
                    break;

                case UserData.ENCODING_SHIFT_JIS:
                    encodedBody = body.getBytes("Shift_JIS");
                    break;

                case UserData.ENCODING_KOREAN:
                    encodedBody = body.getBytes("KSC5601");
                    break;

                case UserData.ENCODING_LATIN_HEBREW:
                    encodedBody = body.getBytes("ISO-8859-8");
                    break;

                case UserData.ENCODING_LATIN:
                default:
                    encodedBody = body.getBytes("ISO-8859-1");
                    break;
            }
            int charCount = body.length();              // use actual char count for num fields
            int recordOctets = encodedBody.length + 1;  // add 1 byte for encoding and pad bits
            if (!isCmasRecord) {
                recordOctets++;                         // add 8 bits for num_fields
            }
            bos.write(8, recordOctets);
            bos.write(5, (encoding & 0x1f));
            if (!isCmasRecord) {
                bos.write(8, charCount);
            }
            bos.writeByteArray(encodedBody.length * 8, encodedBody);
            bos.write(3, 0);            // pad to octet boundary
        }
    }

    private static final String TEST_TEXT = "This is a test CDMA cell broadcast message..."
            + "678901234567890123456789012345678901234567890";

    private static final String PRES_ALERT =
            "THE PRESIDENT HAS ISSUED AN EMERGENCY ALERT. CHECK LOCAL MEDIA FOR MORE DETAILS";

    private static final String EXTREME_ALERT = "FLASH FLOOD WARNING FOR SOUTH COCONINO COUNTY"
            + " - NORTH CENTRAL ARIZONA UNTIL 415 PM MST";

    private static final String SEVERE_ALERT = "SEVERE WEATHER WARNING FOR SOMERSET COUNTY"
            + " - NEW JERSEY UNTIL 415 PM MST";

    private static final String AMBER_ALERT =
            "AMBER ALERT:Mountain View,CA VEH'07 Blue Honda Civic CA LIC 5ABC123";

    private static final String MONTHLY_TEST_ALERT = "This is a test of the emergency alert system."
            + " This is only a test. 89012345678901234567890";

    private static final String IS91_TEXT = "IS91 SHORT MSG";   // max length 14 chars

    /**
     * Verify that the SmsCbMessage has the correct values for CDMA.
     * @param cbMessage the message to test
     */
    private static void verifyCbValues(SmsCbMessage cbMessage) {
        assertEquals(SmsCbMessage.MESSAGE_FORMAT_3GPP2, cbMessage.getMessageFormat());
        assertEquals(SmsCbMessage.GEOGRAPHICAL_SCOPE_PLMN_WIDE, cbMessage.getGeographicalScope());
        assertEquals(false, cbMessage.isEtwsMessage()); // ETWS on CDMA not currently supported
    }

    private static void doTestNonEmergencyBroadcast(int encoding) throws Exception {
        SmsMessage msg = createBroadcastSmsMessage(123, 456, BearerData.PRIORITY_NORMAL,
                BearerData.LANGUAGE_ENGLISH, encoding, TEST_TEXT);

        SmsCbMessage cbMessage = msg.parseBroadcastSms("", 0, 1);
        verifyCbValues(cbMessage);
        assertEquals(123, cbMessage.getServiceCategory());
        assertEquals(456, cbMessage.getSerialNumber());
        assertEquals(SmsCbMessage.MESSAGE_PRIORITY_NORMAL, cbMessage.getMessagePriority());
        assertEquals("en", cbMessage.getLanguageCode());
        assertEquals(TEST_TEXT, cbMessage.getMessageBody());
        assertEquals(false, cbMessage.isEmergencyMessage());
        assertEquals(false, cbMessage.isCmasMessage());
    }

    @Test @SmallTest
    public void testNonEmergencyBroadcast7bitAscii() throws Exception {
        doTestNonEmergencyBroadcast(UserData.ENCODING_7BIT_ASCII);
    }

    @Test @SmallTest
    public void testNonEmergencyBroadcast7bitGsm() throws Exception {
        doTestNonEmergencyBroadcast(UserData.ENCODING_GSM_7BIT_ALPHABET);
    }

    @Test @SmallTest
    public void testNonEmergencyBroadcast16bitUnicode() throws Exception {
        doTestNonEmergencyBroadcast(UserData.ENCODING_UNICODE_16);
    }

    @Test @SmallTest
    public void testNonEmergencyBroadcastIs91Extended() throws Exception {
        // IS-91 doesn't support language or priority subparameters, max 14 chars text
        SmsMessage msg = createBroadcastSmsMessage(987, 654, -1, -1,
                UserData.ENCODING_IS91_EXTENDED_PROTOCOL, IS91_TEXT);

        SmsCbMessage cbMessage = msg.parseBroadcastSms("", 0, 1);
        verifyCbValues(cbMessage);
        assertEquals(987, cbMessage.getServiceCategory());
        assertEquals(654, cbMessage.getSerialNumber());
        assertEquals(SmsCbMessage.MESSAGE_PRIORITY_NORMAL, cbMessage.getMessagePriority());
        assertEquals(null, cbMessage.getLanguageCode());
        assertEquals(IS91_TEXT, cbMessage.getMessageBody());
        assertEquals(false, cbMessage.isEmergencyMessage());
        assertEquals(false, cbMessage.isCmasMessage());
    }

    private static void doTestCmasBroadcast(int serviceCategory, int messageClass, String body)
            throws Exception {
        SmsMessage msg = createCmasSmsMessage(
                serviceCategory, 1234, BearerData.PRIORITY_EMERGENCY, BearerData.LANGUAGE_ENGLISH,
                UserData.ENCODING_7BIT_ASCII, body, -1, -1, -1, -1, -1);

        SmsCbMessage cbMessage = msg.parseBroadcastSms("", 0, 1);
        verifyCbValues(cbMessage);
        assertEquals(serviceCategory, cbMessage.getServiceCategory());
        assertEquals(1234, cbMessage.getSerialNumber());
        assertEquals(SmsCbMessage.MESSAGE_PRIORITY_EMERGENCY, cbMessage.getMessagePriority());
        assertEquals("en", cbMessage.getLanguageCode());
        assertEquals(body, cbMessage.getMessageBody());
        assertEquals(true, cbMessage.isEmergencyMessage());
        assertEquals(true, cbMessage.isCmasMessage());
        SmsCbCmasInfo cmasInfo = cbMessage.getCmasWarningInfo();
        assertEquals(messageClass, cmasInfo.getMessageClass());
        assertEquals(SmsCbCmasInfo.CMAS_CATEGORY_UNKNOWN, cmasInfo.getCategory());
        assertEquals(SmsCbCmasInfo.CMAS_RESPONSE_TYPE_UNKNOWN, cmasInfo.getResponseType());
        assertEquals(SmsCbCmasInfo.CMAS_SEVERITY_UNKNOWN, cmasInfo.getSeverity());
        assertEquals(SmsCbCmasInfo.CMAS_URGENCY_UNKNOWN, cmasInfo.getUrgency());
        assertEquals(SmsCbCmasInfo.CMAS_CERTAINTY_UNKNOWN, cmasInfo.getCertainty());
    }

    @Test @SmallTest
    public void testCmasPresidentialAlert() throws Exception {
        doTestCmasBroadcast(SmsEnvelope.SERVICE_CATEGORY_CMAS_PRESIDENTIAL_LEVEL_ALERT,
                SmsCbCmasInfo.CMAS_CLASS_PRESIDENTIAL_LEVEL_ALERT, PRES_ALERT);
    }

    @Test @SmallTest
    public void testCmasExtremeAlert() throws Exception {
        doTestCmasBroadcast(SmsEnvelope.SERVICE_CATEGORY_CMAS_EXTREME_THREAT,
                SmsCbCmasInfo.CMAS_CLASS_EXTREME_THREAT, EXTREME_ALERT);
    }

    @Test @SmallTest
    public void testCmasSevereAlert() throws Exception {
        doTestCmasBroadcast(SmsEnvelope.SERVICE_CATEGORY_CMAS_SEVERE_THREAT,
                SmsCbCmasInfo.CMAS_CLASS_SEVERE_THREAT, SEVERE_ALERT);
    }

    @Test @SmallTest
    public void testCmasAmberAlert() throws Exception {
        doTestCmasBroadcast(SmsEnvelope.SERVICE_CATEGORY_CMAS_CHILD_ABDUCTION_EMERGENCY,
                SmsCbCmasInfo.CMAS_CLASS_CHILD_ABDUCTION_EMERGENCY, AMBER_ALERT);
    }

    @Test @SmallTest
    public void testCmasTestMessage() throws Exception {
        doTestCmasBroadcast(SmsEnvelope.SERVICE_CATEGORY_CMAS_TEST_MESSAGE,
                SmsCbCmasInfo.CMAS_CLASS_REQUIRED_MONTHLY_TEST, MONTHLY_TEST_ALERT);
    }

    @Test @SmallTest
    public void testCmasExtremeAlertType1Elements() throws Exception {
        SmsMessage msg = createCmasSmsMessage(SmsEnvelope.SERVICE_CATEGORY_CMAS_EXTREME_THREAT,
                5678, BearerData.PRIORITY_EMERGENCY, BearerData.LANGUAGE_ENGLISH,
                UserData.ENCODING_7BIT_ASCII, EXTREME_ALERT, SmsCbCmasInfo.CMAS_CATEGORY_ENV,
                SmsCbCmasInfo.CMAS_RESPONSE_TYPE_MONITOR, SmsCbCmasInfo.CMAS_SEVERITY_SEVERE,
                SmsCbCmasInfo.CMAS_URGENCY_EXPECTED, SmsCbCmasInfo.CMAS_CERTAINTY_LIKELY);

        SmsCbMessage cbMessage = msg.parseBroadcastSms("", 0, 1);
        verifyCbValues(cbMessage);
        assertEquals(SmsEnvelope.SERVICE_CATEGORY_CMAS_EXTREME_THREAT,
                cbMessage.getServiceCategory());
        assertEquals(5678, cbMessage.getSerialNumber());
        assertEquals(SmsCbMessage.MESSAGE_PRIORITY_EMERGENCY, cbMessage.getMessagePriority());
        assertEquals("en", cbMessage.getLanguageCode());
        assertEquals(EXTREME_ALERT, cbMessage.getMessageBody());
        assertEquals(true, cbMessage.isEmergencyMessage());
        assertEquals(true, cbMessage.isCmasMessage());
        SmsCbCmasInfo cmasInfo = cbMessage.getCmasWarningInfo();
        assertEquals(SmsCbCmasInfo.CMAS_CLASS_EXTREME_THREAT, cmasInfo.getMessageClass());
        assertEquals(SmsCbCmasInfo.CMAS_CATEGORY_ENV, cmasInfo.getCategory());
        assertEquals(SmsCbCmasInfo.CMAS_RESPONSE_TYPE_MONITOR, cmasInfo.getResponseType());
        assertEquals(SmsCbCmasInfo.CMAS_SEVERITY_SEVERE, cmasInfo.getSeverity());
        assertEquals(SmsCbCmasInfo.CMAS_URGENCY_EXPECTED, cmasInfo.getUrgency());
        assertEquals(SmsCbCmasInfo.CMAS_CERTAINTY_LIKELY, cmasInfo.getCertainty());
    }

    // VZW requirement is to discard message with unsupported charset. Verify that we return null
    // for this unsupported character set.
    @FlakyTest
    @Ignore
    @Test
    @SmallTest
    public void testCmasUnsupportedCharSet() throws Exception {
        SmsMessage msg = createCmasSmsMessage(SmsEnvelope.SERVICE_CATEGORY_CMAS_EXTREME_THREAT,
                12345, BearerData.PRIORITY_EMERGENCY, BearerData.LANGUAGE_ENGLISH,
                0x1F, EXTREME_ALERT, -1, -1, -1, -1, -1);

        SmsCbMessage cbMessage = msg.parseBroadcastSms("", 0, 1);
        assertNull("expected null for unsupported charset", cbMessage);
    }

    // VZW requirement is to discard message with unsupported charset. Verify that we return null
    // for this unsupported character set.
    @Test @SmallTest
    public void testCmasUnsupportedCharSet2() throws Exception {
        SmsMessage msg = createCmasSmsMessage(SmsEnvelope.SERVICE_CATEGORY_CMAS_EXTREME_THREAT,
                67890, BearerData.PRIORITY_EMERGENCY, BearerData.LANGUAGE_ENGLISH,
                UserData.ENCODING_KOREAN, EXTREME_ALERT, -1, -1, -1, -1, -1);

        SmsCbMessage cbMessage = msg.parseBroadcastSms("", 0, 1);
        assertNull("expected null for unsupported charset", cbMessage);
    }

    // VZW requirement is to discard message without record type 0. The framework will decode it
    // and the app will discard it.
    @Test @SmallTest
    public void testCmasNoRecordType0() throws Exception {
        SmsMessage msg = createCmasSmsMessage(
                SmsEnvelope.SERVICE_CATEGORY_CMAS_PRESIDENTIAL_LEVEL_ALERT, 1234,
                BearerData.PRIORITY_EMERGENCY, BearerData.LANGUAGE_ENGLISH,
                UserData.ENCODING_7BIT_ASCII, null, -1, -1, -1, -1, -1);

        SmsCbMessage cbMessage = msg.parseBroadcastSms("", 0, 1);
        verifyCbValues(cbMessage);
        assertEquals(SmsEnvelope.SERVICE_CATEGORY_CMAS_PRESIDENTIAL_LEVEL_ALERT,
                cbMessage.getServiceCategory());
        assertEquals(1234, cbMessage.getSerialNumber());
        assertEquals(SmsCbMessage.MESSAGE_PRIORITY_EMERGENCY, cbMessage.getMessagePriority());
        assertEquals("en", cbMessage.getLanguageCode());
        assertEquals(null, cbMessage.getMessageBody());
        assertEquals(true, cbMessage.isEmergencyMessage());
        assertEquals(true, cbMessage.isCmasMessage());
        SmsCbCmasInfo cmasInfo = cbMessage.getCmasWarningInfo();
        assertEquals(SmsCbCmasInfo.CMAS_CLASS_PRESIDENTIAL_LEVEL_ALERT, cmasInfo.getMessageClass());
        assertEquals(SmsCbCmasInfo.CMAS_CATEGORY_UNKNOWN, cmasInfo.getCategory());
        assertEquals(SmsCbCmasInfo.CMAS_RESPONSE_TYPE_UNKNOWN, cmasInfo.getResponseType());
        assertEquals(SmsCbCmasInfo.CMAS_SEVERITY_UNKNOWN, cmasInfo.getSeverity());
        assertEquals(SmsCbCmasInfo.CMAS_URGENCY_UNKNOWN, cmasInfo.getUrgency());
        assertEquals(SmsCbCmasInfo.CMAS_CERTAINTY_UNKNOWN, cmasInfo.getCertainty());
    }

    // Make sure we don't throw an exception if we feed completely random data to BearerStream.
    @Test @SmallTest
    public void testRandomBearerStreamData() {
        Random r = new Random(54321);
        for (int run = 0; run < 1000; run++) {
            int len = r.nextInt(140);
            byte[] data = new byte[len];
            for (int i = 0; i < len; i++) {
                data[i] = (byte) r.nextInt(256);
            }
            // Rlog.d("CdmaSmsCbTest", "trying random bearer data run " + run + " length " + len);
            try {
                int category = 0x0ff0 + r.nextInt(32);  // half CMAS, half non-CMAS
                CdmaSmsMessage cdmaSmsMessage = createBroadcastParcel(category);
                SmsMessage msg = createMessageFromParcel(cdmaSmsMessage, data);
                SmsCbMessage cbMessage = msg.parseBroadcastSms("", 0, 1);
                // with random input, cbMessage will almost always be null (log when it isn't)
                if (cbMessage != null) {
                    Rlog.d("CdmaSmsCbTest", "success: " + cbMessage);
                }
            } catch (Exception e) {
                Rlog.d("CdmaSmsCbTest", "exception thrown", e);
                fail("Exception in decoder at run " + run + " length " + len + ": " + e);
            }
        }
    }

    // Make sure we don't throw an exception if we put random data in the UserData subparam.
    @Test @SmallTest
    public void testRandomUserData() {
        Random r = new Random(94040);
        for (int run = 0; run < 1000; run++) {
            int category = 0x0ff0 + r.nextInt(32);  // half CMAS, half non-CMAS
            CdmaSmsMessage cdmaSmsMessage = createBroadcastParcel(category);
            int len = r.nextInt(140);
            // Rlog.d("CdmaSmsCbTest", "trying random user data run " + run + " length " + len);

            try {
                BitwiseOutputStream bos = createBearerDataStream(r.nextInt(65536), r.nextInt(4),
                        r.nextInt(256));

                bos.write(8, SUBPARAM_USER_DATA);
                bos.write(8, len);

                for (int i = 0; i < len; i++) {
                    bos.write(8, r.nextInt(256));
                }

                SmsMessage msg = createMessageFromParcel(cdmaSmsMessage, bos.toByteArray());
                SmsCbMessage cbMessage = msg.parseBroadcastSms("", 0, 1);
            } catch (Exception e) {
                Rlog.d("CdmaSmsCbTest", "exception thrown", e);
                fail("Exception in decoder at run " + run + " length " + len + ": " + e);
            }
        }
    }

    /**
     * Initialize a Parcel for incoming Service Category Program Data teleservice. The caller will
     * write the bearer data and then convert it to an SmsMessage.
     * @return the initialized Parcel
     */
    private static CdmaSmsMessage createServiceCategoryProgramDataParcel() {
        CdmaSmsMessage msg = new CdmaSmsMessage();

        msg.teleserviceId = SmsEnvelope.TELESERVICE_SCPT;
        msg.isServicePresent = false;
        msg.serviceCategory = 0;

        // dummy address (RIL may generate a different dummy address for broadcasts)
        msg.address.digitMode = CdmaSmsAddress.DIGIT_MODE_4BIT_DTMF;
        msg.address.numberMode = CdmaSmsAddress.NUMBER_MODE_NOT_DATA_NETWORK;
        msg.address.numberType = CdmaSmsAddress.TON_UNKNOWN;
        msg.address.numberPlan = CdmaSmsAddress.NUMBERING_PLAN_ISDN_TELEPHONY;
        msg.subAddress.subaddressType = 0;
        msg.subAddress.odd = false;
        return msg;
    }

    private static final String CAT_EXTREME_THREAT = "Extreme Threat to Life and Property";
    private static final String CAT_SEVERE_THREAT = "Severe Threat to Life and Property";
    private static final String CAT_AMBER_ALERTS = "AMBER Alerts";

    @Test @SmallTest
    public void testServiceCategoryProgramDataAddCategory() throws Exception {
        CdmaSmsMessage cdmaSmsMessage = createServiceCategoryProgramDataParcel();
        BitwiseOutputStream bos = createBearerDataStream(123, -1, -1);

        int categoryNameLength = CAT_EXTREME_THREAT.length();
        int subparamLengthBits = (53 + (categoryNameLength * 7));
        int subparamLengthBytes = (subparamLengthBits + 7) / 8;
        int subparamPadBits = (subparamLengthBytes * 8) - subparamLengthBits;

        bos.write(8, SUBPARAM_SERVICE_CATEGORY_PROGRAM_DATA);
        bos.write(8, subparamLengthBytes);
        bos.write(5, UserData.ENCODING_7BIT_ASCII);

        bos.write(4, CdmaSmsCbProgramData.OPERATION_ADD_CATEGORY);
        bos.write(8, (SmsEnvelope.SERVICE_CATEGORY_CMAS_EXTREME_THREAT >>> 8));
        bos.write(8, (SmsEnvelope.SERVICE_CATEGORY_CMAS_EXTREME_THREAT & 0xff));
        bos.write(8, BearerData.LANGUAGE_ENGLISH);
        bos.write(8, 100);  // max messages
        bos.write(4, CdmaSmsCbProgramData.ALERT_OPTION_DEFAULT_ALERT);

        bos.write(8, categoryNameLength);
        for (int i = 0; i < categoryNameLength; i++) {
            bos.write(7, CAT_EXTREME_THREAT.charAt(i));
        }
        bos.write(subparamPadBits, 0);

        SmsMessage msg = createMessageFromParcel(cdmaSmsMessage, bos.toByteArray());
        assertNotNull(msg);
        msg.parseSms();
        List<CdmaSmsCbProgramData> programDataList = msg.getSmsCbProgramData();
        assertNotNull(programDataList);
        assertEquals(1, programDataList.size());
        CdmaSmsCbProgramData programData = programDataList.get(0);
        assertEquals(CdmaSmsCbProgramData.OPERATION_ADD_CATEGORY, programData.getOperation());
        assertEquals(SmsEnvelope.SERVICE_CATEGORY_CMAS_EXTREME_THREAT, programData.getCategory());
        assertEquals(CAT_EXTREME_THREAT, programData.getCategoryName());
        assertEquals(BearerData.LANGUAGE_ENGLISH, programData.getLanguage());
        assertEquals(100, programData.getMaxMessages());
        assertEquals(CdmaSmsCbProgramData.ALERT_OPTION_DEFAULT_ALERT, programData.getAlertOption());
    }

    @Test @SmallTest
    public void testServiceCategoryProgramDataDeleteTwoCategories() throws Exception {
        CdmaSmsMessage cdmaSmsMessage = createServiceCategoryProgramDataParcel();
        BitwiseOutputStream bos = createBearerDataStream(456, -1, -1);

        int category1NameLength = CAT_SEVERE_THREAT.length();
        int category2NameLength = CAT_AMBER_ALERTS.length();

        int subparamLengthBits = (101 + (category1NameLength * 7) + (category2NameLength * 7));
        int subparamLengthBytes = (subparamLengthBits + 7) / 8;
        int subparamPadBits = (subparamLengthBytes * 8) - subparamLengthBits;

        bos.write(8, SUBPARAM_SERVICE_CATEGORY_PROGRAM_DATA);
        bos.write(8, subparamLengthBytes);
        bos.write(5, UserData.ENCODING_7BIT_ASCII);

        bos.write(4, CdmaSmsCbProgramData.OPERATION_DELETE_CATEGORY);
        bos.write(8, (SmsEnvelope.SERVICE_CATEGORY_CMAS_SEVERE_THREAT >>> 8));
        bos.write(8, (SmsEnvelope.SERVICE_CATEGORY_CMAS_SEVERE_THREAT & 0xff));
        bos.write(8, BearerData.LANGUAGE_ENGLISH);
        bos.write(8, 0);  // max messages
        bos.write(4, CdmaSmsCbProgramData.ALERT_OPTION_NO_ALERT);

        bos.write(8, category1NameLength);
        for (int i = 0; i < category1NameLength; i++) {
            bos.write(7, CAT_SEVERE_THREAT.charAt(i));
        }

        bos.write(4, CdmaSmsCbProgramData.OPERATION_DELETE_CATEGORY);
        bos.write(8, (SmsEnvelope.SERVICE_CATEGORY_CMAS_CHILD_ABDUCTION_EMERGENCY >>> 8));
        bos.write(8, (SmsEnvelope.SERVICE_CATEGORY_CMAS_CHILD_ABDUCTION_EMERGENCY & 0xff));
        bos.write(8, BearerData.LANGUAGE_ENGLISH);
        bos.write(8, 0);  // max messages
        bos.write(4, CdmaSmsCbProgramData.ALERT_OPTION_NO_ALERT);

        bos.write(8, category2NameLength);
        for (int i = 0; i < category2NameLength; i++) {
            bos.write(7, CAT_AMBER_ALERTS.charAt(i));
        }

        bos.write(subparamPadBits, 0);

        SmsMessage msg = createMessageFromParcel(cdmaSmsMessage, bos.toByteArray());
        assertNotNull(msg);
        msg.parseSms();
        List<CdmaSmsCbProgramData> programDataList = msg.getSmsCbProgramData();
        assertNotNull(programDataList);
        assertEquals(2, programDataList.size());

        CdmaSmsCbProgramData programData = programDataList.get(0);
        assertEquals(CdmaSmsCbProgramData.OPERATION_DELETE_CATEGORY, programData.getOperation());
        assertEquals(SmsEnvelope.SERVICE_CATEGORY_CMAS_SEVERE_THREAT, programData.getCategory());
        assertEquals(CAT_SEVERE_THREAT, programData.getCategoryName());
        assertEquals(BearerData.LANGUAGE_ENGLISH, programData.getLanguage());
        assertEquals(0, programData.getMaxMessages());
        assertEquals(CdmaSmsCbProgramData.ALERT_OPTION_NO_ALERT, programData.getAlertOption());

        programData = programDataList.get(1);
        assertEquals(CdmaSmsCbProgramData.OPERATION_DELETE_CATEGORY, programData.getOperation());
        assertEquals(SmsEnvelope.SERVICE_CATEGORY_CMAS_CHILD_ABDUCTION_EMERGENCY,
                programData.getCategory());
        assertEquals(CAT_AMBER_ALERTS, programData.getCategoryName());
        assertEquals(BearerData.LANGUAGE_ENGLISH, programData.getLanguage());
        assertEquals(0, programData.getMaxMessages());
        assertEquals(CdmaSmsCbProgramData.ALERT_OPTION_NO_ALERT, programData.getAlertOption());
    }

    private static final byte[] CMAS_TEST_BEARER_DATA = {
        0x00, 0x03, 0x1C, 0x78, 0x00, 0x01, 0x59, 0x02, (byte) 0xB8, 0x00, 0x02, 0x10, (byte) 0xAA,
        0x68, (byte) 0xD3, (byte) 0xCD, 0x06, (byte) 0x9E, 0x68, 0x30, (byte) 0xA0, (byte) 0xE9,
        (byte) 0x97, (byte) 0x9F, 0x44, 0x1B, (byte) 0xF3, 0x20, (byte) 0xE9, (byte) 0xA3,
        0x2A, 0x08, 0x7B, (byte) 0xF6, (byte) 0xED, (byte) 0xCB, (byte) 0xCB, 0x1E, (byte) 0x9C,
        0x3B, 0x10, 0x4D, (byte) 0xDF, (byte) 0x8B, 0x4E,
        (byte) 0xCC, (byte) 0xA8, 0x20, (byte) 0xEC, (byte) 0xCB, (byte) 0xCB, (byte) 0xA2, 0x0A,
        0x7E, 0x79, (byte) 0xF4, (byte) 0xCB, (byte) 0xB5, 0x72, 0x0A, (byte) 0x9A, 0x34,
        (byte) 0xF3, 0x41, (byte) 0xA7, (byte) 0x9A, 0x0D, (byte) 0xFB, (byte) 0xB6, 0x79, 0x41,
        (byte) 0x85, 0x07, 0x4C, (byte) 0xBC, (byte) 0xFA, 0x2E, 0x00, 0x08, 0x20, 0x58, 0x38,
        (byte) 0x88, (byte) 0x80, 0x10, 0x54, 0x06, 0x38, 0x20, 0x60,
        0x30, (byte) 0xA8, (byte) 0x81, (byte) 0x90, 0x20, 0x08
    };

    // Test case for CMAS test message received on the Sprint network.
    @Test @SmallTest
    public void testDecodeRawBearerData() throws Exception {
        CdmaSmsMessage cdmaSmsMessage = createBroadcastParcel(SmsEnvelope.SERVICE_CATEGORY_CMAS_TEST_MESSAGE);
        SmsMessage msg = createMessageFromParcel(cdmaSmsMessage, CMAS_TEST_BEARER_DATA);

        SmsCbMessage cbMessage = msg.parseBroadcastSms("", 0, 1);
        assertNotNull("expected non-null for bearer data", cbMessage);
        assertEquals("geoScope", cbMessage.getGeographicalScope(), 1);
        assertEquals("serialNumber", cbMessage.getSerialNumber(), 51072);
        assertEquals("serviceCategory", cbMessage.getServiceCategory(),
                SmsEnvelope.SERVICE_CATEGORY_CMAS_TEST_MESSAGE);
        assertEquals("payload", cbMessage.getMessageBody(),
                "This is a test of the Commercial Mobile Alert System. This is only a test.");

        SmsCbCmasInfo cmasInfo = cbMessage.getCmasWarningInfo();
        assertNotNull("expected non-null for CMAS info", cmasInfo);
        assertEquals("category", cmasInfo.getCategory(), SmsCbCmasInfo.CMAS_CATEGORY_OTHER);
        assertEquals("responseType", cmasInfo.getResponseType(),
                SmsCbCmasInfo.CMAS_RESPONSE_TYPE_NONE);
        assertEquals("severity", cmasInfo.getSeverity(), SmsCbCmasInfo.CMAS_SEVERITY_SEVERE);
        assertEquals("urgency", cmasInfo.getUrgency(), SmsCbCmasInfo.CMAS_URGENCY_EXPECTED);
        assertEquals("certainty", cmasInfo.getCertainty(), SmsCbCmasInfo.CMAS_CERTAINTY_LIKELY);
    }
}