Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2009 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 android.telephony.cts;
     18 
     19 
     20 import android.content.Context;
     21 import android.content.pm.PackageManager;
     22 import android.telephony.SmsMessage;
     23 import android.telephony.TelephonyManager;
     24 import android.test.AndroidTestCase;
     25 
     26 public class SmsMessageTest extends AndroidTestCase{
     27 
     28     private TelephonyManager mTelephonyManager;
     29     private PackageManager mPackageManager;
     30 
     31     private static final String DISPLAY_MESSAGE_BODY = "test subject /test body";
     32     private static final String DMB = "{ testBody[^~\\] }";
     33     private static final String EMAIL_ADD = "foo (at) example.com";
     34     private static final String EMAIL_FROM = "foo (at) example.com";
     35     private static final String MB = DMB;
     36     private static final String MESSAGE_BODY1 = "Test";
     37     private static final String MESSAGE_BODY2 = "(Subject)Test";
     38     private static final String MESSAGE_BODY3 = "\u2122\u00a9\u00aehello";
     39     private static final String MESSAGE_BODY4 = " ";
     40     private static final String MESSAGE_BODY5 = " ";
     41     private static final String OA = "foo (at) example.com";
     42     private static final String OA1 = "+14154255486";
     43     private static final String OA2 = "+15122977683";
     44     private static final String OA3 = "_@";
     45     private static final String OA4 = "\u0394@";
     46     // pseudo subject will always be empty
     47     private static final String PSEUDO_SUBJECT = "";
     48     private static final String SCA1 = "+16466220020";
     49     private static final String SCA2 = "+12063130012";
     50     private static final String SCA3 = "+14155551212";
     51     private static final String SCA4 = "+14155551212";
     52     private static final int NOT_CREATE_FROM_SIM = -1;
     53     private static final int NOT_CREATE_FROM_ICC = -1;
     54     private static final int PROTOCOL_IDENTIFIER = 0;
     55     private static final int SMS_NUMBER1 = 1;
     56     private static final int SMS_NUMBER2 = 1;
     57     private static final int SMS_NUMBER3 = 1;
     58     private static final int STATUS = 0;
     59     private static final int STATUS_ON_SIM_DEF = -1;
     60     private static final int STATUS_ON_ICC_DEF = -1;
     61     private static final int TPLAYER_LENGTH_FOR_PDU = 23;
     62     private static final long TIMESTAMP_MILLIS = 1149631383000l;
     63     private static final int SEPTETS_SKT = 80;
     64     private static final int SEPTETS_KT = 90;
     65 
     66     @Override
     67     protected void setUp() throws Exception {
     68         super.setUp();
     69         mTelephonyManager =
     70             (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
     71         mPackageManager = getContext().getPackageManager();
     72     }
     73 
     74     @SuppressWarnings("deprecation")
     75     public void testCreateFromPdu() throws Exception {
     76         if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)
     77                 || mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CDMA)) {
     78             // TODO: temp workaround, need to adjust test to use CDMA pdus
     79             return;
     80         }
     81 
     82         String pdu = "07916164260220F0040B914151245584F600006060605130308A04D4F29C0E";
     83         SmsMessage sms = SmsMessage.createFromPdu(hexStringToByteArray(pdu));
     84         assertEquals(SCA1, sms.getServiceCenterAddress());
     85         assertEquals(OA1, sms.getOriginatingAddress());
     86         assertEquals(MESSAGE_BODY1, sms.getMessageBody());
     87         assertEquals(TPLAYER_LENGTH_FOR_PDU, SmsMessage.getTPLayerLengthForPDU(pdu));
     88         int[] result = SmsMessage.calculateLength(sms.getMessageBody(), true);
     89         assertEquals(SMS_NUMBER1, result[0]);
     90         assertEquals(sms.getMessageBody().length(), result[1]);
     91         assertRemaining(sms.getMessageBody().length(), result[2]);
     92         assertEquals(SmsMessage.ENCODING_7BIT, result[3]);
     93         assertEquals(pdu, toHexString(sms.getPdu()));
     94 
     95         assertEquals(NOT_CREATE_FROM_SIM, sms.getIndexOnSim());
     96         assertEquals(NOT_CREATE_FROM_ICC, sms.getIndexOnIcc());
     97         assertEquals(PROTOCOL_IDENTIFIER, sms.getProtocolIdentifier());
     98         assertFalse(sms.isEmail());
     99         assertFalse(sms.isReplyPathPresent());
    100         assertFalse(sms.isStatusReportMessage());
    101         assertFalse(sms.isCphsMwiMessage());
    102         assertEquals(SmsMessage.MessageClass.UNKNOWN, sms.getMessageClass());
    103         assertEquals(STATUS, sms.getStatus());
    104         assertEquals(STATUS_ON_SIM_DEF, sms.getStatusOnSim());
    105         assertEquals(STATUS_ON_ICC_DEF, sms.getStatusOnIcc());
    106         assertEquals(TIMESTAMP_MILLIS, sms.getTimestampMillis());
    107 
    108         // Test create from null Pdu
    109         sms = SmsMessage.createFromPdu(null);
    110         assertNotNull(sms);
    111 
    112         //Test create from long Pdu
    113         pdu = "07912160130310F2040B915121927786F300036060924180008A0DA"
    114             + "8695DAC2E8FE9296A794E07";
    115         sms = SmsMessage.createFromPdu(hexStringToByteArray(pdu));
    116         assertEquals(SCA2, sms.getServiceCenterAddress());
    117         assertEquals(OA2, sms.getOriginatingAddress());
    118         assertEquals(MESSAGE_BODY2, sms.getMessageBody());
    119         CharSequence msgBody = sms.getMessageBody();
    120         result = SmsMessage.calculateLength(msgBody, false);
    121         assertEquals(SMS_NUMBER2, result[0]);
    122         assertEquals(sms.getMessageBody().length(), result[1]);
    123         assertRemaining(sms.getMessageBody().length(), result[2]);
    124         assertEquals(SmsMessage.ENCODING_7BIT, result[3]);
    125 
    126         // Test createFromPdu Ucs to Sms
    127         pdu = "07912160130300F4040B914151245584"
    128             + "F600087010807121352B10212200A900AE00680065006C006C006F";
    129         sms = SmsMessage.createFromPdu(hexStringToByteArray(pdu));
    130         assertEquals(MESSAGE_BODY3, sms.getMessageBody());
    131         result = SmsMessage.calculateLength(sms.getMessageBody(), true);
    132         assertEquals(SMS_NUMBER3, result[0]);
    133         assertEquals(sms.getMessageBody().length(), result[1]);
    134         assertRemaining(sms.getMessageBody().length(), result[2]);
    135         assertEquals(SmsMessage.ENCODING_7BIT, result[3]);
    136     }
    137 
    138     private void assertRemaining(int messageLength, int remaining) {
    139         if (TelephonyUtils.isSkt(mTelephonyManager)) {
    140             assertTrue(checkRemaining(SEPTETS_SKT, messageLength, remaining)
    141                     || checkRemaining(SmsMessage.MAX_USER_DATA_SEPTETS, messageLength, remaining));
    142         } else if (TelephonyUtils.isKt(mTelephonyManager)) {
    143             assertTrue(checkRemaining(SEPTETS_KT, messageLength, remaining)
    144                     || checkRemaining(SmsMessage.MAX_USER_DATA_SEPTETS, messageLength, remaining));
    145         } else {
    146             assertTrue(checkRemaining(SmsMessage.MAX_USER_DATA_SEPTETS, messageLength, remaining));
    147         }
    148     }
    149 
    150     private boolean checkRemaining(int total, int messageLength, int remaining) {
    151         return total - messageLength == remaining;
    152     }
    153 
    154     public void testCPHSVoiceMail() throws Exception {
    155         if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)
    156                 || mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CDMA)) {
    157             // TODO: temp workaround, need to adjust test to use CDMA pdus
    158             return;
    159         }
    160 
    161         // "set MWI flag"
    162         String pdu = "07912160130310F20404D0110041006060627171118A0120";
    163         SmsMessage sms = SmsMessage.createFromPdu(hexStringToByteArray(pdu));
    164         assertTrue(sms.isReplace());
    165         assertEquals(OA3, sms.getOriginatingAddress());
    166         assertEquals(MESSAGE_BODY4, sms.getMessageBody());
    167         assertTrue(sms.isMWISetMessage());
    168 
    169         // "clear mwi flag"
    170         pdu = "07912160130310F20404D0100041006021924193352B0120";
    171         sms = SmsMessage.createFromPdu(hexStringToByteArray(pdu));
    172         assertTrue(sms.isMWIClearMessage());
    173 
    174         // "clear MWI flag"
    175         pdu = "07912160130310F20404D0100041006060627161058A0120";
    176         sms = SmsMessage.createFromPdu(hexStringToByteArray(pdu));
    177         assertTrue(sms.isReplace());
    178         assertEquals(OA4, sms.getOriginatingAddress());
    179         assertEquals(MESSAGE_BODY5, sms.getMessageBody());
    180         assertTrue(sms.isMWIClearMessage());
    181 
    182         // "set MWI flag"
    183         pdu = "07912180958750F84401800500C87020026195702B06040102000200";
    184         sms = SmsMessage.createFromPdu(hexStringToByteArray(pdu));
    185         assertTrue(sms.isMWISetMessage());
    186         assertTrue(sms.isMwiDontStore());
    187 
    188         // "clear mwi flag"
    189         pdu = "07912180958750F84401800500C07020027160112B06040102000000";
    190         sms = SmsMessage.createFromPdu(hexStringToByteArray(pdu));
    191 
    192         assertTrue(sms.isMWIClearMessage());
    193         assertTrue(sms.isMwiDontStore());
    194     }
    195 
    196     public void testGetUserData() throws Exception {
    197         if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)
    198                 || mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CDMA)) {
    199             // TODO: temp workaround, need to adjust test to use CDMA pdus
    200             return;
    201         }
    202 
    203         String pdu = "07914140279510F6440A8111110301003BF56080207130138A8C0B05040B8423F"
    204             + "000032A02010106276170706C69636174696F6E2F766E642E7761702E6D6D732D"
    205             + "6D65737361676500AF848D0185B4848C8298524E453955304A6D7135514141426"
    206             + "66C414141414D7741414236514141414141008D908918802B3135313232393737"
    207             + "3638332F545950453D504C4D4E008A808E022B918805810306977F83687474703"
    208             + "A2F2F36";
    209         SmsMessage sms = SmsMessage.createFromPdu(hexStringToByteArray(pdu));
    210         byte[] userData = sms.getUserData();
    211         assertNotNull(userData);
    212     }
    213 
    214     public void testGetSubmitPdu() throws Exception {
    215         if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
    216             return;
    217         }
    218 
    219         String scAddress = null, destinationAddress = null;
    220         String message = null;
    221         boolean statusReportRequested = false;
    222 
    223         try {
    224             // null message, null destination
    225             SmsMessage.getSubmitPdu(scAddress, destinationAddress, message, statusReportRequested);
    226             fail("Should throw NullPointerException");
    227         } catch (NullPointerException expected) {
    228             // expected
    229         }
    230 
    231         message = "This is a test message";
    232         try {
    233             // non-null message
    234             SmsMessage.getSubmitPdu(scAddress, destinationAddress, message, statusReportRequested);
    235             fail("Should throw NullPointerException");
    236         } catch (NullPointerException expected) {
    237             // expected
    238         }
    239 
    240         if (mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
    241             // TODO: temp workaround, OCTET encoding for EMS not properly supported
    242             return;
    243         }
    244 
    245         scAddress = "1650253000";
    246         destinationAddress = "18004664411";
    247         message = "This is a test message";
    248         statusReportRequested = false;
    249         SmsMessage.SubmitPdu smsPdu =
    250             SmsMessage.getSubmitPdu(scAddress, destinationAddress, message, statusReportRequested);
    251         assertNotNull(smsPdu);
    252 
    253         smsPdu = SmsMessage.getSubmitPdu(scAddress, destinationAddress, (short)80,
    254                 message.getBytes(), statusReportRequested);
    255         assertNotNull(smsPdu);
    256     }
    257 
    258     public void testEmailGateway() throws Exception {
    259         if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)
    260                 || mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CDMA)) {
    261             // TODO: temp workaround, need to adjust test to use CDMA pdus
    262             return;
    263         }
    264 
    265         String pdu = "07914151551512f204038105f300007011103164638a28e6f71b50c687db" +
    266                          "7076d9357eb7412f7a794e07cdeb6275794c07bde8e5391d247e93f3";
    267 
    268         SmsMessage sms = SmsMessage.createFromPdu(hexStringToByteArray(pdu));
    269         assertEquals(SCA4, sms.getServiceCenterAddress());
    270         assertTrue(sms.isEmail());
    271         assertEquals(EMAIL_ADD, sms.getEmailFrom());
    272         assertEquals(EMAIL_ADD, sms.getDisplayOriginatingAddress());
    273         assertEquals(PSEUDO_SUBJECT, sms.getPseudoSubject());
    274 
    275         assertEquals(DISPLAY_MESSAGE_BODY, sms.getDisplayMessageBody());
    276         assertEquals(DISPLAY_MESSAGE_BODY, sms.getEmailBody());
    277 
    278         pdu = "07914151551512f204038105f400007011103105458a29e6f71b50c687db" +
    279                         "7076d9357eb741af0d0a442fcfe9c23739bfe16d289bdee6b5f1813629";
    280         sms = SmsMessage.createFromPdu(hexStringToByteArray(pdu));
    281         assertEquals(SCA3, sms.getServiceCenterAddress());
    282         assertTrue(sms.isEmail());
    283         assertEquals(OA, sms.getDisplayOriginatingAddress());
    284         assertEquals(EMAIL_FROM, sms.getEmailFrom());
    285         assertEquals(DMB, sms.getDisplayMessageBody());
    286         assertEquals(MB, sms.getEmailBody());
    287     }
    288 
    289     private final static char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    290             'A', 'B', 'C', 'D', 'E', 'F' };
    291 
    292     public static String toHexString(byte[] array) {
    293         int length = array.length;
    294         char[] buf = new char[length * 2];
    295 
    296         int bufIndex = 0;
    297         for (int i = 0 ; i < length; i++)
    298         {
    299             byte b = array[i];
    300             buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F];
    301             buf[bufIndex++] = HEX_DIGITS[b & 0x0F];
    302         }
    303 
    304         return new String(buf);
    305     }
    306 
    307     private static int toByte(char c) {
    308         if (c >= '0' && c <= '9') return (c - '0');
    309         if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
    310         if (c >= 'a' && c <= 'f') return (c - 'a' + 10);
    311 
    312         throw new RuntimeException ("Invalid hex char '" + c + "'");
    313     }
    314 
    315     private static byte[] hexStringToByteArray(String hexString) {
    316         int length = hexString.length();
    317         byte[] buffer = new byte[length / 2];
    318 
    319         for (int i = 0 ; i < length ; i += 2) {
    320             buffer[i / 2] =
    321                 (byte)((toByte(hexString.charAt(i)) << 4) | toByte(hexString.charAt(i+1)));
    322         }
    323 
    324         return buffer;
    325     }
    326 }
    327