1 /* 2 * Copyright (C) 2006 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.internal.telephony.cdma.sms; 18 19 import android.telephony.TelephonyManager; 20 import android.test.AndroidTestCase; 21 import android.test.suitebuilder.annotation.SmallTest; 22 23 import com.android.internal.telephony.GsmAlphabet.TextEncodingDetails; 24 import com.android.internal.telephony.SmsHeader; 25 import com.android.internal.telephony.cdma.SmsMessage; 26 import com.android.internal.util.HexDump; 27 28 import java.util.ArrayList; 29 import java.util.Arrays; 30 31 public class CdmaSmsTest extends AndroidTestCase { 32 33 // CJK ideographs, Hiragana, Katakana, full width letters, Cyrillic, etc. 34 private static final String sUnicodeChars = "\u4e00\u4e01\u4e02\u4e03" + 35 "\u4e04\u4e05\u4e06\u4e07\u4e08\u4e09\u4e0a\u4e0b\u4e0c\u4e0d" + 36 "\u4e0e\u4e0f\u3041\u3042\u3043\u3044\u3045\u3046\u3047\u3048" + 37 "\u30a1\u30a2\u30a3\u30a4\u30a5\u30a6\u30a7\u30a8" + 38 "\uff10\uff11\uff12\uff13\uff14\uff15\uff16\uff17\uff18" + 39 "\uff70\uff71\uff72\uff73\uff74\uff75\uff76\uff77\uff78" + 40 "\u0400\u0401\u0402\u0403\u0404\u0405\u0406\u0407\u0408" + 41 "\u00a2\u00a9\u00ae\u2122"; 42 43 // "Hello, world" in Japanese. 44 private static final String sHelloWorldJa = "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c"; 45 46 @SmallTest 47 public void testCdmaSmsAddrParsing() throws Exception { 48 CdmaSmsAddress addr = CdmaSmsAddress.parse("6502531000"); 49 assertEquals(addr.ton, CdmaSmsAddress.TON_UNKNOWN); 50 assertEquals(addr.digitMode, CdmaSmsAddress.DIGIT_MODE_4BIT_DTMF); 51 assertEquals(addr.numberMode, CdmaSmsAddress.NUMBER_MODE_NOT_DATA_NETWORK); 52 assertEquals(addr.numberOfDigits, 10); 53 assertEquals(addr.origBytes.length, 10); 54 byte[] data = {6, 5, 10, 2, 5, 3, 1, 10, 10, 10}; 55 for (int i = 0; i < data.length; i++) { 56 assertEquals(addr.origBytes[i], data[i]); 57 } 58 addr = CdmaSmsAddress.parse("(650) 253-1000"); 59 assertEquals(addr.ton, CdmaSmsAddress.TON_UNKNOWN); 60 assertEquals(addr.digitMode, CdmaSmsAddress.DIGIT_MODE_4BIT_DTMF); 61 assertEquals(addr.numberMode, CdmaSmsAddress.NUMBER_MODE_NOT_DATA_NETWORK); 62 assertEquals(addr.numberOfDigits, 10); 63 assertEquals(addr.origBytes.length, 10); 64 byte[] data2 = {6, 5, 10, 2, 5, 3, 1, 10, 10, 10}; 65 for (int i = 0; i < data2.length; i++) { 66 assertEquals(addr.origBytes[i], data2[i]); 67 } 68 addr = CdmaSmsAddress.parse("650.253.1000"); 69 assertEquals(addr.ton, CdmaSmsAddress.TON_UNKNOWN); 70 assertEquals(addr.digitMode, CdmaSmsAddress.DIGIT_MODE_4BIT_DTMF); 71 assertEquals(addr.numberMode, CdmaSmsAddress.NUMBER_MODE_NOT_DATA_NETWORK); 72 assertEquals(addr.numberOfDigits, 10); 73 assertEquals(addr.origBytes.length, 10); 74 byte[] data5 = {6, 5, 10, 2, 5, 3, 1, 10, 10, 10}; 75 for (int i = 0; i < data2.length; i++) { 76 assertEquals(addr.origBytes[i], data5[i]); 77 } 78 addr = CdmaSmsAddress.parse("(+886) 917 222 555"); 79 assertEquals(addr.ton, CdmaSmsAddress.TON_INTERNATIONAL_OR_IP); 80 assertEquals(addr.digitMode, CdmaSmsAddress.DIGIT_MODE_8BIT_CHAR); 81 assertEquals(addr.numberMode, CdmaSmsAddress.NUMBER_MODE_NOT_DATA_NETWORK); 82 assertEquals(addr.numberOfDigits, 12); 83 assertEquals(addr.origBytes.length, 12); 84 String expectedAddr = "886917222555"; 85 for (int i = 0; i < addr.numberOfDigits; i++) { 86 assertEquals((int) expectedAddr.charAt(i), addr.origBytes[i]); 87 } 88 addr = CdmaSmsAddress.parse("(650) *253-1000 #600"); 89 byte[] data4 = {6, 5, 10, 11, 2, 5, 3, 1, 10, 10, 10, 12, 6, 10, 10}; 90 for (int i = 0; i < data4.length; i++) { 91 assertEquals(addr.origBytes[i], data4[i]); 92 } 93 String input = "x (at) y.com,a (at) b.com"; 94 addr = CdmaSmsAddress.parse(input); 95 assertEquals(addr.ton, CdmaSmsAddress.TON_NATIONAL_OR_EMAIL); 96 assertEquals(addr.digitMode, CdmaSmsAddress.DIGIT_MODE_8BIT_CHAR); 97 assertEquals(addr.numberMode, CdmaSmsAddress.NUMBER_MODE_DATA_NETWORK); 98 assertEquals(addr.numberOfDigits, 15); 99 assertEquals(addr.origBytes.length, 15); 100 assertEquals(new String(addr.origBytes), input); 101 addr = CdmaSmsAddress.parse("foo bar"); 102 assertEquals(addr.ton, CdmaSmsAddress.TON_UNKNOWN); 103 assertEquals(addr.digitMode, CdmaSmsAddress.DIGIT_MODE_8BIT_CHAR); 104 assertEquals(addr.numberMode, CdmaSmsAddress.NUMBER_MODE_DATA_NETWORK); 105 assertEquals(addr.numberOfDigits, 6); 106 assertEquals(addr.origBytes.length, 6); 107 assertEquals(new String(addr.origBytes), "foobar"); 108 addr = CdmaSmsAddress.parse("f\noo\tb a\rr"); 109 assertEquals(new String(addr.origBytes), "foobar"); 110 assertEquals(CdmaSmsAddress.parse("f\u0000oo bar"), null); 111 assertEquals(CdmaSmsAddress.parse("f\u0007oo bar"), null); 112 assertEquals(CdmaSmsAddress.parse("f\u0080oo bar"), null); 113 assertEquals(CdmaSmsAddress.parse("f\u1ECFboo\u001fbar"), null); 114 assertEquals(CdmaSmsAddress.parse("f\u0080oo bar"), null); 115 } 116 117 @SmallTest 118 public void testUserData7bitGsm() throws Exception { 119 String pdu = "00031040900112488ea794e074d69e1b7392c270326cde9e98"; 120 BearerData bearerData = BearerData.decode(HexDump.hexStringToByteArray(pdu)); 121 assertEquals("Test standard SMS", bearerData.userData.payloadStr); 122 } 123 124 @SmallTest 125 public void testUserData7bitAscii() throws Exception { 126 String pdu = "0003100160010610262d5ab500"; 127 BearerData bearerData = BearerData.decode(HexDump.hexStringToByteArray(pdu)); 128 assertEquals("bjjj", bearerData.userData.payloadStr); 129 } 130 131 @SmallTest 132 public void testUserData7bitAsciiTwo() throws Exception { 133 String pdu = "00031001d00109104539b4d052ebb3d0"; 134 BearerData bearerData = BearerData.decode(HexDump.hexStringToByteArray(pdu)); 135 assertEquals("SMS Rulz", bearerData.userData.payloadStr); 136 } 137 138 @SmallTest 139 public void testUserDataIa5() throws Exception { 140 String pdu = "00031002100109184539b4d052ebb3d0"; 141 BearerData bearerData = BearerData.decode(HexDump.hexStringToByteArray(pdu)); 142 assertEquals("SMS Rulz", bearerData.userData.payloadStr); 143 } 144 145 @SmallTest 146 public void testUserData7bitAsciiFeedback() throws Exception { 147 BearerData bearerData = new BearerData(); 148 bearerData.messageType = BearerData.MESSAGE_TYPE_DELIVER; 149 bearerData.messageId = 0; 150 bearerData.hasUserDataHeader = false; 151 UserData userData = new UserData(); 152 userData.payloadStr = "Test standard SMS"; 153 userData.msgEncoding = UserData.ENCODING_7BIT_ASCII; 154 userData.msgEncodingSet = true; 155 bearerData.userData = userData; 156 byte []encodedSms = BearerData.encode(bearerData); 157 BearerData revBearerData = BearerData.decode(encodedSms); 158 assertEquals(BearerData.MESSAGE_TYPE_DELIVER, revBearerData.messageType); 159 assertEquals(0, revBearerData.messageId); 160 assertEquals(false, revBearerData.hasUserDataHeader); 161 assertEquals(userData.msgEncoding, revBearerData.userData.msgEncoding); 162 assertEquals(userData.payloadStr.length(), revBearerData.userData.numFields); 163 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 164 userData.payloadStr = "Test \u007f standard \u0000 SMS"; 165 revBearerData = BearerData.decode(BearerData.encode(bearerData)); 166 assertEquals("Test standard SMS", revBearerData.userData.payloadStr); 167 userData.payloadStr = "Test \n standard \r SMS"; 168 revBearerData = BearerData.decode(BearerData.encode(bearerData)); 169 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 170 userData.payloadStr = ""; 171 revBearerData = BearerData.decode(BearerData.encode(bearerData)); 172 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 173 } 174 175 @SmallTest 176 public void testUserData7bitGsmFeedback() throws Exception { 177 BearerData bearerData = new BearerData(); 178 bearerData.messageType = BearerData.MESSAGE_TYPE_DELIVER; 179 bearerData.messageId = 0; 180 bearerData.hasUserDataHeader = false; 181 UserData userData = new UserData(); 182 userData.payloadStr = "Test standard SMS"; 183 userData.msgEncoding = UserData.ENCODING_GSM_7BIT_ALPHABET; 184 userData.msgEncodingSet = true; 185 bearerData.userData = userData; 186 byte []encodedSms = BearerData.encode(bearerData); 187 BearerData revBearerData = BearerData.decode(encodedSms); 188 assertEquals(BearerData.MESSAGE_TYPE_DELIVER, revBearerData.messageType); 189 assertEquals(0, revBearerData.messageId); 190 assertEquals(false, revBearerData.hasUserDataHeader); 191 assertEquals(userData.msgEncoding, revBearerData.userData.msgEncoding); 192 assertEquals(userData.payloadStr.length(), revBearerData.userData.numFields); 193 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 194 userData.payloadStr = "1234567"; 195 revBearerData = BearerData.decode(BearerData.encode(bearerData)); 196 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 197 userData.payloadStr = ""; 198 revBearerData = BearerData.decode(BearerData.encode(bearerData)); 199 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 200 userData.payloadStr = "12345678901234567890123456789012345678901234567890" + 201 "12345678901234567890123456789012345678901234567890" + 202 "12345678901234567890123456789012345678901234567890" + 203 "1234567890"; 204 revBearerData = BearerData.decode(BearerData.encode(bearerData)); 205 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 206 userData.payloadStr = "Test \u007f illegal \u0000 SMS chars"; 207 revBearerData = BearerData.decode(BearerData.encode(bearerData)); 208 assertEquals("Test illegal SMS chars", revBearerData.userData.payloadStr); 209 userData.payloadStr = "More @ testing\nis great^|^~woohoo"; 210 revBearerData = BearerData.decode(BearerData.encode(bearerData)); 211 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 212 SmsHeader.ConcatRef concatRef = new SmsHeader.ConcatRef(); 213 concatRef.refNumber = 0xEE; 214 concatRef.msgCount = 2; 215 concatRef.seqNumber = 2; 216 concatRef.isEightBits = true; 217 SmsHeader smsHeader = new SmsHeader(); 218 smsHeader.concatRef = concatRef; 219 byte[] encodedHeader = SmsHeader.toByteArray(smsHeader); 220 userData.userDataHeader = smsHeader; 221 revBearerData = BearerData.decode(BearerData.encode(bearerData)); 222 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 223 SmsHeader decodedHeader = revBearerData.userData.userDataHeader; 224 assertEquals(decodedHeader.concatRef.refNumber, concatRef.refNumber); 225 assertEquals(decodedHeader.concatRef.msgCount, concatRef.msgCount); 226 assertEquals(decodedHeader.concatRef.seqNumber, concatRef.seqNumber); 227 } 228 229 @SmallTest 230 public void testUserDataUtf16Feedback() throws Exception { 231 BearerData bearerData = new BearerData(); 232 bearerData.messageType = BearerData.MESSAGE_TYPE_DELIVER; 233 bearerData.messageId = 0; 234 bearerData.hasUserDataHeader = false; 235 UserData userData = new UserData(); 236 userData.payloadStr = "\u0160u\u1E5B\u0301r\u1ECFg\uD835\uDC1At\u00E9\u4E002\u3042"; 237 userData.msgEncoding = UserData.ENCODING_UNICODE_16; 238 userData.msgEncodingSet = true; 239 bearerData.userData = userData; 240 byte []encodedSms = BearerData.encode(bearerData); 241 BearerData revBearerData = BearerData.decode(encodedSms); 242 assertEquals(BearerData.MESSAGE_TYPE_DELIVER, revBearerData.messageType); 243 assertEquals(0, revBearerData.messageId); 244 assertEquals(false, revBearerData.hasUserDataHeader); 245 assertEquals(userData.msgEncoding, revBearerData.userData.msgEncoding); 246 assertEquals(userData.payloadStr.length(), revBearerData.userData.numFields); 247 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 248 userData.msgEncoding = UserData.ENCODING_OCTET; 249 userData.msgEncodingSet = false; 250 revBearerData = BearerData.decode(BearerData.encode(bearerData)); 251 assertEquals(BearerData.MESSAGE_TYPE_DELIVER, revBearerData.messageType); 252 assertEquals(0, revBearerData.messageId); 253 assertEquals(false, revBearerData.hasUserDataHeader); 254 assertEquals(userData.msgEncoding, revBearerData.userData.msgEncoding); 255 assertEquals(userData.payloadStr.length(), revBearerData.userData.numFields); 256 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 257 userData.payloadStr = "1234567"; 258 revBearerData = BearerData.decode(BearerData.encode(bearerData)); 259 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 260 userData.payloadStr = ""; 261 revBearerData = BearerData.decode(BearerData.encode(bearerData)); 262 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 263 } 264 265 @SmallTest 266 public void testMonolithicOne() throws Exception { 267 String pdu = "0003200010010410168d2002010503060812011101590501c706069706180000000701c108" + 268 "01c00901800a01e00b01030c01c00d01070e05039acc13880f018011020566"; 269 BearerData bearerData = BearerData.decode(HexDump.hexStringToByteArray(pdu)); 270 assertEquals(bearerData.messageType, BearerData.MESSAGE_TYPE_SUBMIT); 271 assertEquals(bearerData.messageId, 1); 272 assertEquals(bearerData.priority, BearerData.PRIORITY_EMERGENCY); 273 assertEquals(bearerData.privacy, BearerData.PRIVACY_CONFIDENTIAL); 274 assertEquals(bearerData.userAckReq, true); 275 assertEquals(bearerData.readAckReq, true); 276 assertEquals(bearerData.deliveryAckReq, true); 277 assertEquals(bearerData.reportReq, false); 278 assertEquals(bearerData.numberOfMessages, 3); 279 assertEquals(bearerData.alert, BearerData.ALERT_HIGH_PRIO); 280 assertEquals(bearerData.language, BearerData.LANGUAGE_HEBREW); 281 assertEquals(bearerData.callbackNumber.digitMode, CdmaSmsAddress.DIGIT_MODE_4BIT_DTMF); 282 assertEquals(bearerData.callbackNumber.numberMode, 283 CdmaSmsAddress.NUMBER_MODE_NOT_DATA_NETWORK); 284 assertEquals(bearerData.callbackNumber.ton, CdmaSmsAddress.TON_UNKNOWN); 285 assertEquals(bearerData.callbackNumber.numberPlan, CdmaSmsAddress.NUMBERING_PLAN_UNKNOWN); 286 assertEquals(bearerData.callbackNumber.numberOfDigits, 7); 287 assertEquals(bearerData.callbackNumber.address, "3598271"); 288 assertEquals(bearerData.displayMode, BearerData.DISPLAY_MODE_USER); 289 assertEquals(bearerData.depositIndex, 1382); 290 assertEquals(bearerData.userResponseCode, 5); 291 assertEquals(bearerData.msgCenterTimeStamp.year, 2008); 292 assertEquals(bearerData.msgCenterTimeStamp.month, 11); 293 assertEquals(bearerData.msgCenterTimeStamp.monthDay, 1); 294 assertEquals(bearerData.msgCenterTimeStamp.hour, 11); 295 assertEquals(bearerData.msgCenterTimeStamp.minute, 1); 296 assertEquals(bearerData.msgCenterTimeStamp.second, 59); 297 assertEquals(bearerData.validityPeriodAbsolute, null); 298 assertEquals(bearerData.validityPeriodRelative, 193); 299 assertEquals(bearerData.deferredDeliveryTimeAbsolute.year, 1997); 300 assertEquals(bearerData.deferredDeliveryTimeAbsolute.month, 5); 301 assertEquals(bearerData.deferredDeliveryTimeAbsolute.monthDay, 18); 302 assertEquals(bearerData.deferredDeliveryTimeAbsolute.hour, 0); 303 assertEquals(bearerData.deferredDeliveryTimeAbsolute.minute, 0); 304 assertEquals(bearerData.deferredDeliveryTimeAbsolute.second, 0); 305 assertEquals(bearerData.deferredDeliveryTimeRelative, 199); 306 assertEquals(bearerData.hasUserDataHeader, false); 307 assertEquals(bearerData.userData.msgEncoding, UserData.ENCODING_7BIT_ASCII); 308 assertEquals(bearerData.userData.numFields, 2); 309 assertEquals(bearerData.userData.payloadStr, "hi"); 310 } 311 312 @SmallTest 313 public void testMonolithicTwo() throws Exception { 314 String pdu = "0003200010010410168d200201050306081201110159050192060697061800000007013d0" + 315 "801c00901800a01e00b01030c01c00d01070e05039acc13880f018011020566"; 316 BearerData bearerData = BearerData.decode(HexDump.hexStringToByteArray(pdu)); 317 assertEquals(bearerData.messageType, BearerData.MESSAGE_TYPE_SUBMIT); 318 assertEquals(bearerData.messageId, 1); 319 assertEquals(bearerData.priority, BearerData.PRIORITY_EMERGENCY); 320 assertEquals(bearerData.privacy, BearerData.PRIVACY_CONFIDENTIAL); 321 assertEquals(bearerData.userAckReq, true); 322 assertEquals(bearerData.readAckReq, true); 323 assertEquals(bearerData.deliveryAckReq, true); 324 assertEquals(bearerData.reportReq, false); 325 assertEquals(bearerData.numberOfMessages, 3); 326 assertEquals(bearerData.alert, BearerData.ALERT_HIGH_PRIO); 327 assertEquals(bearerData.language, BearerData.LANGUAGE_HEBREW); 328 assertEquals(bearerData.callbackNumber.digitMode, CdmaSmsAddress.DIGIT_MODE_4BIT_DTMF); 329 assertEquals(bearerData.callbackNumber.numberMode, 330 CdmaSmsAddress.NUMBER_MODE_NOT_DATA_NETWORK); 331 assertEquals(bearerData.callbackNumber.ton, CdmaSmsAddress.TON_UNKNOWN); 332 assertEquals(bearerData.callbackNumber.numberPlan, CdmaSmsAddress.NUMBERING_PLAN_UNKNOWN); 333 assertEquals(bearerData.callbackNumber.numberOfDigits, 7); 334 assertEquals(bearerData.callbackNumber.address, "3598271"); 335 assertEquals(bearerData.displayMode, BearerData.DISPLAY_MODE_USER); 336 assertEquals(bearerData.depositIndex, 1382); 337 assertEquals(bearerData.userResponseCode, 5); 338 assertEquals(bearerData.msgCenterTimeStamp.year, 2008); 339 assertEquals(bearerData.msgCenterTimeStamp.month, 11); 340 assertEquals(bearerData.msgCenterTimeStamp.monthDay, 1); 341 assertEquals(bearerData.msgCenterTimeStamp.hour, 11); 342 assertEquals(bearerData.msgCenterTimeStamp.minute, 1); 343 assertEquals(bearerData.msgCenterTimeStamp.second, 59); 344 assertEquals(bearerData.validityPeriodAbsolute, null); 345 assertEquals(bearerData.validityPeriodRelative, 61); 346 assertEquals(bearerData.deferredDeliveryTimeAbsolute.year, 1997); 347 assertEquals(bearerData.deferredDeliveryTimeAbsolute.month, 5); 348 assertEquals(bearerData.deferredDeliveryTimeAbsolute.monthDay, 18); 349 assertEquals(bearerData.deferredDeliveryTimeAbsolute.hour, 0); 350 assertEquals(bearerData.deferredDeliveryTimeAbsolute.minute, 0); 351 assertEquals(bearerData.deferredDeliveryTimeAbsolute.second, 0); 352 assertEquals(bearerData.deferredDeliveryTimeRelative, 146); 353 assertEquals(bearerData.hasUserDataHeader, false); 354 assertEquals(bearerData.userData.msgEncoding, UserData.ENCODING_7BIT_ASCII); 355 assertEquals(bearerData.userData.numFields, 2); 356 assertEquals(bearerData.userData.payloadStr, "hi"); 357 } 358 359 @SmallTest 360 public void testUserDataHeaderConcatRefFeedback() throws Exception { 361 BearerData bearerData = new BearerData(); 362 bearerData.messageType = BearerData.MESSAGE_TYPE_DELIVER; 363 bearerData.messageId = 55; 364 SmsHeader.ConcatRef concatRef = new SmsHeader.ConcatRef(); 365 concatRef.refNumber = 0xEE; 366 concatRef.msgCount = 2; 367 concatRef.seqNumber = 2; 368 concatRef.isEightBits = true; 369 SmsHeader smsHeader = new SmsHeader(); 370 smsHeader.concatRef = concatRef; 371 byte[] encodedHeader = SmsHeader.toByteArray(smsHeader); 372 SmsHeader decodedHeader = SmsHeader.fromByteArray(encodedHeader); 373 assertEquals(decodedHeader.concatRef.refNumber, concatRef.refNumber); 374 assertEquals(decodedHeader.concatRef.msgCount, concatRef.msgCount); 375 assertEquals(decodedHeader.concatRef.seqNumber, concatRef.seqNumber); 376 assertEquals(decodedHeader.concatRef.isEightBits, concatRef.isEightBits); 377 assertEquals(decodedHeader.portAddrs, null); 378 UserData userData = new UserData(); 379 userData.payloadStr = "User Data Header (UDH) feedback test"; 380 userData.userDataHeader = smsHeader; 381 bearerData.userData = userData; 382 byte[] encodedSms = BearerData.encode(bearerData); 383 BearerData revBearerData = BearerData.decode(encodedSms); 384 decodedHeader = revBearerData.userData.userDataHeader; 385 assertEquals(decodedHeader.concatRef.refNumber, concatRef.refNumber); 386 assertEquals(decodedHeader.concatRef.msgCount, concatRef.msgCount); 387 assertEquals(decodedHeader.concatRef.seqNumber, concatRef.seqNumber); 388 assertEquals(decodedHeader.concatRef.isEightBits, concatRef.isEightBits); 389 assertEquals(decodedHeader.portAddrs, null); 390 } 391 392 @SmallTest 393 public void testUserDataHeaderIllegalConcatRef() throws Exception { 394 BearerData bearerData = new BearerData(); 395 bearerData.messageType = BearerData.MESSAGE_TYPE_DELIVER; 396 bearerData.messageId = 55; 397 SmsHeader.ConcatRef concatRef = new SmsHeader.ConcatRef(); 398 concatRef.refNumber = 0x10; 399 concatRef.msgCount = 0; 400 concatRef.seqNumber = 2; 401 concatRef.isEightBits = true; 402 SmsHeader smsHeader = new SmsHeader(); 403 smsHeader.concatRef = concatRef; 404 byte[] encodedHeader = SmsHeader.toByteArray(smsHeader); 405 SmsHeader decodedHeader = SmsHeader.fromByteArray(encodedHeader); 406 assertEquals(decodedHeader.concatRef, null); 407 concatRef.isEightBits = false; 408 encodedHeader = SmsHeader.toByteArray(smsHeader); 409 decodedHeader = SmsHeader.fromByteArray(encodedHeader); 410 assertEquals(decodedHeader.concatRef, null); 411 concatRef.msgCount = 1; 412 concatRef.seqNumber = 2; 413 encodedHeader = SmsHeader.toByteArray(smsHeader); 414 decodedHeader = SmsHeader.fromByteArray(encodedHeader); 415 assertEquals(decodedHeader.concatRef, null); 416 concatRef.msgCount = 1; 417 concatRef.seqNumber = 0; 418 encodedHeader = SmsHeader.toByteArray(smsHeader); 419 decodedHeader = SmsHeader.fromByteArray(encodedHeader); 420 assertEquals(decodedHeader.concatRef, null); 421 concatRef.msgCount = 2; 422 concatRef.seqNumber = 1; 423 encodedHeader = SmsHeader.toByteArray(smsHeader); 424 decodedHeader = SmsHeader.fromByteArray(encodedHeader); 425 assertEquals(decodedHeader.concatRef.msgCount, 2); 426 assertEquals(decodedHeader.concatRef.seqNumber, 1); 427 } 428 429 @SmallTest 430 public void testUserDataHeaderMixedFeedback() throws Exception { 431 BearerData bearerData = new BearerData(); 432 bearerData.messageType = BearerData.MESSAGE_TYPE_DELIVER; 433 bearerData.messageId = 42; 434 SmsHeader.ConcatRef concatRef = new SmsHeader.ConcatRef(); 435 concatRef.refNumber = 0x34; 436 concatRef.msgCount = 5; 437 concatRef.seqNumber = 2; 438 concatRef.isEightBits = false; 439 SmsHeader.PortAddrs portAddrs = new SmsHeader.PortAddrs(); 440 portAddrs.destPort = 88; 441 portAddrs.origPort = 66; 442 portAddrs.areEightBits = false; 443 SmsHeader smsHeader = new SmsHeader(); 444 smsHeader.concatRef = concatRef; 445 smsHeader.portAddrs = portAddrs; 446 byte[] encodedHeader = SmsHeader.toByteArray(smsHeader); 447 SmsHeader decodedHeader = SmsHeader.fromByteArray(encodedHeader); 448 assertEquals(decodedHeader.concatRef.refNumber, concatRef.refNumber); 449 assertEquals(decodedHeader.concatRef.msgCount, concatRef.msgCount); 450 assertEquals(decodedHeader.concatRef.seqNumber, concatRef.seqNumber); 451 assertEquals(decodedHeader.concatRef.isEightBits, concatRef.isEightBits); 452 assertEquals(decodedHeader.portAddrs.destPort, portAddrs.destPort); 453 assertEquals(decodedHeader.portAddrs.origPort, portAddrs.origPort); 454 assertEquals(decodedHeader.portAddrs.areEightBits, portAddrs.areEightBits); 455 UserData userData = new UserData(); 456 userData.payloadStr = "User Data Header (UDH) feedback test"; 457 userData.userDataHeader = smsHeader; 458 bearerData.userData = userData; 459 byte[] encodedSms = BearerData.encode(bearerData); 460 BearerData revBearerData = BearerData.decode(encodedSms); 461 decodedHeader = revBearerData.userData.userDataHeader; 462 assertEquals(decodedHeader.concatRef.refNumber, concatRef.refNumber); 463 assertEquals(decodedHeader.concatRef.msgCount, concatRef.msgCount); 464 assertEquals(decodedHeader.concatRef.seqNumber, concatRef.seqNumber); 465 assertEquals(decodedHeader.concatRef.isEightBits, concatRef.isEightBits); 466 assertEquals(decodedHeader.portAddrs.destPort, portAddrs.destPort); 467 assertEquals(decodedHeader.portAddrs.origPort, portAddrs.origPort); 468 assertEquals(decodedHeader.portAddrs.areEightBits, portAddrs.areEightBits); 469 } 470 471 @SmallTest 472 public void testReplyOption() throws Exception { 473 String pdu1 = "0003104090011648b6a794e0705476bf77bceae934fe5f6d94d87450080a0180"; 474 BearerData bd1 = BearerData.decode(HexDump.hexStringToByteArray(pdu1)); 475 assertEquals("Test Acknowledgement 1", bd1.userData.payloadStr); 476 assertEquals(true, bd1.userAckReq); 477 assertEquals(false, bd1.deliveryAckReq); 478 assertEquals(false, bd1.readAckReq); 479 assertEquals(false, bd1.reportReq); 480 String pdu2 = "0003104090011648b6a794e0705476bf77bceae934fe5f6d94d87490080a0140"; 481 BearerData bd2 = BearerData.decode(HexDump.hexStringToByteArray(pdu2)); 482 assertEquals("Test Acknowledgement 2", bd2.userData.payloadStr); 483 assertEquals(false, bd2.userAckReq); 484 assertEquals(true, bd2.deliveryAckReq); 485 assertEquals(false, bd2.readAckReq); 486 assertEquals(false, bd2.reportReq); 487 String pdu3 = "0003104090011648b6a794e0705476bf77bceae934fe5f6d94d874d0080a0120"; 488 BearerData bd3 = BearerData.decode(HexDump.hexStringToByteArray(pdu3)); 489 assertEquals("Test Acknowledgement 3", bd3.userData.payloadStr); 490 assertEquals(false, bd3.userAckReq); 491 assertEquals(false, bd3.deliveryAckReq); 492 assertEquals(true, bd3.readAckReq); 493 assertEquals(false, bd3.reportReq); 494 String pdu4 = "0003104090011648b6a794e0705476bf77bceae934fe5f6d94d87510080a0110"; 495 BearerData bd4 = BearerData.decode(HexDump.hexStringToByteArray(pdu4)); 496 assertEquals("Test Acknowledgement 4", bd4.userData.payloadStr); 497 assertEquals(false, bd4.userAckReq); 498 assertEquals(false, bd4.deliveryAckReq); 499 assertEquals(false, bd4.readAckReq); 500 assertEquals(true, bd4.reportReq); 501 } 502 503 @SmallTest 504 public void testReplyOptionFeedback() throws Exception { 505 BearerData bearerData = new BearerData(); 506 bearerData.messageType = BearerData.MESSAGE_TYPE_DELIVER; 507 bearerData.messageId = 0; 508 bearerData.hasUserDataHeader = false; 509 UserData userData = new UserData(); 510 userData.payloadStr = "test reply option"; 511 bearerData.userData = userData; 512 bearerData.userAckReq = true; 513 byte []encodedSms = BearerData.encode(bearerData); 514 BearerData revBearerData = BearerData.decode(encodedSms); 515 assertEquals(true, revBearerData.userAckReq); 516 assertEquals(false, revBearerData.deliveryAckReq); 517 assertEquals(false, revBearerData.readAckReq); 518 assertEquals(false, revBearerData.reportReq); 519 bearerData.userAckReq = false; 520 bearerData.deliveryAckReq = true; 521 encodedSms = BearerData.encode(bearerData); 522 revBearerData = BearerData.decode(encodedSms); 523 assertEquals(false, revBearerData.userAckReq); 524 assertEquals(true, revBearerData.deliveryAckReq); 525 assertEquals(false, revBearerData.readAckReq); 526 assertEquals(false, revBearerData.reportReq); 527 bearerData.deliveryAckReq = false; 528 bearerData.readAckReq = true; 529 encodedSms = BearerData.encode(bearerData); 530 revBearerData = BearerData.decode(encodedSms); 531 assertEquals(false, revBearerData.userAckReq); 532 assertEquals(false, revBearerData.deliveryAckReq); 533 assertEquals(true, revBearerData.readAckReq); 534 assertEquals(false, revBearerData.reportReq); 535 bearerData.readAckReq = false; 536 bearerData.reportReq = true; 537 encodedSms = BearerData.encode(bearerData); 538 revBearerData = BearerData.decode(encodedSms); 539 assertEquals(false, revBearerData.userAckReq); 540 assertEquals(false, revBearerData.deliveryAckReq); 541 assertEquals(false, revBearerData.readAckReq); 542 assertEquals(true, revBearerData.reportReq); 543 } 544 545 @SmallTest 546 public void testNumberOfMessages() throws Exception { 547 // Note that the message text below does not properly reflect 548 // the message count. The author of these messages was 549 // apparently unaware that the values are bcd encoded, and the 550 // values being tested against (not the ones in the message 551 // text) are actually correct. 552 String pdu1 = "000310409001124896a794e07595f69f199540ea759a0dc8e00b0163"; 553 BearerData bd1 = BearerData.decode(HexDump.hexStringToByteArray(pdu1)); 554 assertEquals("Test Voice mail 99", bd1.userData.payloadStr); 555 assertEquals(63, bd1.numberOfMessages); 556 String pdu2 = "00031040900113489ea794e07595f69f199540ea759a0988c0600b0164"; 557 BearerData bd2 = BearerData.decode(HexDump.hexStringToByteArray(pdu2)); 558 assertEquals("Test Voice mail 100", bd2.userData.payloadStr); 559 assertEquals(64, bd2.numberOfMessages); 560 } 561 562 @SmallTest 563 public void testCallbackNum() throws Exception { 564 String pdu1 = "00031040900112488ea794e070d436cb638bc5e035ce2f97900e06910431323334"; 565 BearerData bd1 = BearerData.decode(HexDump.hexStringToByteArray(pdu1)); 566 assertEquals("Test Callback nbr", bd1.userData.payloadStr); 567 assertEquals(CdmaSmsAddress.DIGIT_MODE_8BIT_CHAR, bd1.callbackNumber.digitMode); 568 assertEquals(CdmaSmsAddress.TON_INTERNATIONAL_OR_IP, bd1.callbackNumber.ton); 569 assertEquals(CdmaSmsAddress.NUMBER_MODE_NOT_DATA_NETWORK, bd1.callbackNumber.numberMode); 570 assertEquals(CdmaSmsAddress.NUMBERING_PLAN_ISDN_TELEPHONY, bd1.callbackNumber.numberPlan); 571 assertEquals("1234", bd1.callbackNumber.address); 572 } 573 574 @SmallTest 575 public void testCallbackNumDtmf() throws Exception { 576 String pdu1 = "00031002300109104539b4d052ebb3d00e07052d4c90a55080"; 577 BearerData bd1 = BearerData.decode(HexDump.hexStringToByteArray(pdu1)); 578 assertEquals("SMS Rulz", bd1.userData.payloadStr); 579 assertEquals(CdmaSmsAddress.DIGIT_MODE_4BIT_DTMF, bd1.callbackNumber.digitMode); 580 assertEquals(CdmaSmsAddress.TON_UNKNOWN, bd1.callbackNumber.ton); 581 assertEquals(CdmaSmsAddress.NUMBER_MODE_NOT_DATA_NETWORK, bd1.callbackNumber.numberMode); 582 assertEquals(CdmaSmsAddress.NUMBERING_PLAN_UNKNOWN, bd1.callbackNumber.numberPlan); 583 assertEquals("5099214001", bd1.callbackNumber.address); 584 } 585 586 @SmallTest 587 public void testCallbackNumFeedback() throws Exception { 588 BearerData bearerData = new BearerData(); 589 bearerData.messageType = BearerData.MESSAGE_TYPE_DELIVER; 590 bearerData.messageId = 0; 591 bearerData.hasUserDataHeader = false; 592 UserData userData = new UserData(); 593 userData.payloadStr = "test callback number"; 594 bearerData.userData = userData; 595 CdmaSmsAddress addr = new CdmaSmsAddress(); 596 addr.digitMode = CdmaSmsAddress.DIGIT_MODE_8BIT_CHAR; 597 addr.ton = CdmaSmsAddress.TON_NATIONAL_OR_EMAIL; 598 addr.numberMode = CdmaSmsAddress.NUMBER_MODE_NOT_DATA_NETWORK; 599 addr.numberPlan = CdmaSmsAddress.NUMBERING_PLAN_UNKNOWN; 600 addr.address = "8005551212"; 601 addr.numberOfDigits = (byte)addr.address.length(); 602 bearerData.callbackNumber = addr; 603 byte []encodedSms = BearerData.encode(bearerData); 604 BearerData revBearerData = BearerData.decode(encodedSms); 605 CdmaSmsAddress revAddr = revBearerData.callbackNumber; 606 assertEquals(addr.digitMode, revAddr.digitMode); 607 assertEquals(addr.ton, revAddr.ton); 608 assertEquals(addr.numberMode, revAddr.numberMode); 609 assertEquals(addr.numberPlan, revAddr.numberPlan); 610 assertEquals(addr.numberOfDigits, revAddr.numberOfDigits); 611 assertEquals(addr.address, revAddr.address); 612 addr.address = "8*55#1012"; 613 addr.numberOfDigits = (byte)addr.address.length(); 614 addr.digitMode = CdmaSmsAddress.DIGIT_MODE_4BIT_DTMF; 615 encodedSms = BearerData.encode(bearerData); 616 revBearerData = BearerData.decode(encodedSms); 617 revAddr = revBearerData.callbackNumber; 618 assertEquals(addr.digitMode, revAddr.digitMode); 619 assertEquals(addr.numberOfDigits, revAddr.numberOfDigits); 620 assertEquals(addr.address, revAddr.address); 621 } 622 623 @SmallTest 624 public void testPrivacyIndicator() throws Exception { 625 String pdu1 = "0003104090010c485f4194dfea34becf61b840090140"; 626 BearerData bd1 = BearerData.decode(HexDump.hexStringToByteArray(pdu1)); 627 assertEquals(bd1.privacy, BearerData.PRIVACY_RESTRICTED); 628 String pdu2 = "0003104090010c485f4194dfea34becf61b840090180"; 629 BearerData bd2 = BearerData.decode(HexDump.hexStringToByteArray(pdu2)); 630 assertEquals(bd2.privacy, BearerData.PRIVACY_CONFIDENTIAL); 631 String pdu3 = "0003104090010c485f4194dfea34becf61b8400901c0"; 632 BearerData bd3 = BearerData.decode(HexDump.hexStringToByteArray(pdu3)); 633 assertEquals(bd3.privacy, BearerData.PRIVACY_SECRET); 634 } 635 636 @SmallTest 637 public void testPrivacyIndicatorFeedback() throws Exception { 638 BearerData bearerData = new BearerData(); 639 bearerData.messageType = BearerData.MESSAGE_TYPE_DELIVER; 640 bearerData.messageId = 0; 641 bearerData.hasUserDataHeader = false; 642 UserData userData = new UserData(); 643 userData.payloadStr = "test privacy indicator"; 644 bearerData.userData = userData; 645 bearerData.privacy = BearerData.PRIVACY_SECRET; 646 bearerData.privacyIndicatorSet = true; 647 byte []encodedSms = BearerData.encode(bearerData); 648 BearerData revBearerData = BearerData.decode(encodedSms); 649 assertEquals(revBearerData.userData.payloadStr, userData.payloadStr); 650 assertEquals(revBearerData.privacyIndicatorSet, true); 651 assertEquals(revBearerData.privacy, BearerData.PRIVACY_SECRET); 652 bearerData.privacy = BearerData.PRIVACY_RESTRICTED; 653 encodedSms = BearerData.encode(bearerData); 654 revBearerData = BearerData.decode(encodedSms); 655 assertEquals(revBearerData.privacy, BearerData.PRIVACY_RESTRICTED); 656 } 657 658 @SmallTest 659 public void testMsgDeliveryAlert() throws Exception { 660 String pdu1 = "0003104090010d4866a794e07055965b91d040300c0100"; 661 BearerData bd1 = BearerData.decode(HexDump.hexStringToByteArray(pdu1)); 662 assertEquals(bd1.alert, 0); 663 assertEquals(bd1.userData.payloadStr, "Test Alert 0"); 664 String pdu2 = "0003104090010d4866a794e07055965b91d140300c0140"; 665 BearerData bd2 = BearerData.decode(HexDump.hexStringToByteArray(pdu2)); 666 assertEquals(bd2.alert, 1); 667 assertEquals(bd2.userData.payloadStr, "Test Alert 1"); 668 String pdu3 = "0003104090010d4866a794e07055965b91d240300c0180"; 669 BearerData bd3 = BearerData.decode(HexDump.hexStringToByteArray(pdu3)); 670 assertEquals(bd3.alert, 2); 671 assertEquals(bd3.userData.payloadStr, "Test Alert 2"); 672 String pdu4 = "0003104090010d4866a794e07055965b91d340300c01c0"; 673 BearerData bd4 = BearerData.decode(HexDump.hexStringToByteArray(pdu4)); 674 assertEquals(bd4.alert, 3); 675 assertEquals(bd4.userData.payloadStr, "Test Alert 3"); 676 String pdu5 = "00031000000126114F4CBCFA20DB979F3C39F2A0C9976" + 677 "69ED979794187665E5D1028EFA7A6840E1062D3D39A900C028000"; 678 BearerData bd5 = BearerData.decode(HexDump.hexStringToByteArray(pdu5)); 679 assertEquals(bd5.alert, BearerData.ALERT_MEDIUM_PRIO); 680 assertEquals(bd5.userData.payloadStr, "test message delivery alert (with 8 bits)"); 681 String pdu6 = "00031000000126114F4CBCFA20DB979F3C39F2A0C9976" + 682 "69ED979794187665E5D1028EFA7A6840C1062D3D39A900C00"; 683 BearerData bd6 = BearerData.decode(HexDump.hexStringToByteArray(pdu6)); 684 assertEquals(bd6.userData.payloadStr, "test message delivery alert (with 0 bits)"); 685 assertEquals(bd6.alertIndicatorSet, false); 686 } 687 688 @SmallTest 689 public void testMiscParams() throws Exception { 690 String pdu1 = "00031002400109104539b4d052ebb3d00c0180"; 691 BearerData bd1 = BearerData.decode(HexDump.hexStringToByteArray(pdu1)); 692 assertEquals(bd1.alert, BearerData.ALERT_MEDIUM_PRIO); 693 assertEquals(bd1.userData.payloadStr, "SMS Rulz"); 694 String pdu2 = "00031002500109104539b4d052ebb3d00801800901c0"; 695 BearerData bd2 = BearerData.decode(HexDump.hexStringToByteArray(pdu2)); 696 assertEquals(bd2.priority, BearerData.PRIORITY_URGENT); 697 assertEquals(bd2.privacy, BearerData.PRIVACY_SECRET); 698 assertEquals(bd2.userData.payloadStr, "SMS Rulz"); 699 String pdu3 = "00031002600109104539b4d052ebb3d00901400c01c0"; 700 BearerData bd3 = BearerData.decode(HexDump.hexStringToByteArray(pdu3)); 701 assertEquals(bd3.privacy, BearerData.PRIVACY_RESTRICTED); 702 assertEquals(bd3.alert, BearerData.ALERT_HIGH_PRIO); 703 assertEquals(bd3.userData.payloadStr, "SMS Rulz"); 704 String pdu4 = "00031002700109104539b4d052ebb3d00f0105"; 705 BearerData bd4 = BearerData.decode(HexDump.hexStringToByteArray(pdu4)); 706 assertEquals(bd4.displayMode, BearerData.DISPLAY_MODE_IMMEDIATE); 707 assertEquals(bd4.userData.payloadStr, "SMS Rulz"); 708 } 709 @SmallTest 710 public void testMsgDeliveryAlertFeedback() throws Exception { 711 BearerData bearerData = new BearerData(); 712 bearerData.messageType = BearerData.MESSAGE_TYPE_DELIVER; 713 bearerData.messageId = 0; 714 bearerData.hasUserDataHeader = false; 715 UserData userData = new UserData(); 716 userData.payloadStr = "test message delivery alert"; 717 bearerData.userData = userData; 718 bearerData.alert = BearerData.ALERT_MEDIUM_PRIO; 719 bearerData.alertIndicatorSet = true; 720 byte []encodedSms = BearerData.encode(bearerData); 721 BearerData revBearerData = BearerData.decode(encodedSms); 722 assertEquals(revBearerData.userData.payloadStr, userData.payloadStr); 723 assertEquals(revBearerData.alertIndicatorSet, true); 724 assertEquals(revBearerData.alert, bearerData.alert); 725 bearerData.alert = BearerData.ALERT_HIGH_PRIO; 726 encodedSms = BearerData.encode(bearerData); 727 revBearerData = BearerData.decode(encodedSms); 728 assertEquals(revBearerData.userData.payloadStr, userData.payloadStr); 729 assertEquals(revBearerData.alertIndicatorSet, true); 730 assertEquals(revBearerData.alert, bearerData.alert); 731 } 732 733 @SmallTest 734 public void testLanguageIndicator() throws Exception { 735 String pdu1 = "0003104090011748bea794e0731436ef3bd7c2e0352eef27a1c263fe58080d0101"; 736 BearerData bd1 = BearerData.decode(HexDump.hexStringToByteArray(pdu1)); 737 assertEquals(bd1.userData.payloadStr, "Test Language indicator"); 738 assertEquals(bd1.language, BearerData.LANGUAGE_ENGLISH); 739 String pdu2 = "0003104090011748bea794e0731436ef3bd7c2e0352eef27a1c263fe58080d0106"; 740 BearerData bd2 = BearerData.decode(HexDump.hexStringToByteArray(pdu2)); 741 assertEquals(bd2.userData.payloadStr, "Test Language indicator"); 742 assertEquals(bd2.language, BearerData.LANGUAGE_CHINESE); 743 } 744 745 @SmallTest 746 public void testLanguageIndicatorFeedback() throws Exception { 747 BearerData bearerData = new BearerData(); 748 bearerData.messageType = BearerData.MESSAGE_TYPE_DELIVER; 749 bearerData.messageId = 0; 750 bearerData.hasUserDataHeader = false; 751 UserData userData = new UserData(); 752 userData.payloadStr = "test language indicator"; 753 bearerData.userData = userData; 754 bearerData.language = BearerData.LANGUAGE_ENGLISH; 755 bearerData.languageIndicatorSet = true; 756 byte []encodedSms = BearerData.encode(bearerData); 757 BearerData revBearerData = BearerData.decode(encodedSms); 758 assertEquals(revBearerData.userData.payloadStr, userData.payloadStr); 759 assertEquals(revBearerData.languageIndicatorSet, true); 760 assertEquals(revBearerData.language, bearerData.language); 761 bearerData.language = BearerData.LANGUAGE_KOREAN; 762 encodedSms = BearerData.encode(bearerData); 763 revBearerData = BearerData.decode(encodedSms); 764 assertEquals(revBearerData.userData.payloadStr, userData.payloadStr); 765 assertEquals(revBearerData.languageIndicatorSet, true); 766 assertEquals(revBearerData.language, bearerData.language); 767 } 768 769 @SmallTest 770 public void testDisplayMode() throws Exception { 771 String pdu1 = "0003104090010c485f4194dfea34becf61b8400f0100"; 772 BearerData bd1 = BearerData.decode(HexDump.hexStringToByteArray(pdu1)); 773 assertEquals(bd1.displayMode, BearerData.DISPLAY_MODE_IMMEDIATE); 774 String pdu2 = "0003104090010c485f4194dfea34becf61b8400f0140"; 775 BearerData bd2 = BearerData.decode(HexDump.hexStringToByteArray(pdu2)); 776 assertEquals(bd2.displayMode, BearerData.DISPLAY_MODE_DEFAULT); 777 String pdu3 = "0003104090010c485f4194dfea34becf61b8400f0180"; 778 BearerData bd3 = BearerData.decode(HexDump.hexStringToByteArray(pdu3)); 779 assertEquals(bd3.displayMode, BearerData.DISPLAY_MODE_USER); 780 } 781 782 @SmallTest 783 public void testDisplayModeFeedback() throws Exception { 784 BearerData bearerData = new BearerData(); 785 bearerData.messageType = BearerData.MESSAGE_TYPE_DELIVER; 786 bearerData.messageId = 0; 787 bearerData.hasUserDataHeader = false; 788 UserData userData = new UserData(); 789 userData.payloadStr = "test display mode"; 790 bearerData.userData = userData; 791 bearerData.displayMode = BearerData.DISPLAY_MODE_IMMEDIATE; 792 bearerData.displayModeSet = true; 793 byte []encodedSms = BearerData.encode(bearerData); 794 BearerData revBearerData = BearerData.decode(encodedSms); 795 assertEquals(revBearerData.userData.payloadStr, userData.payloadStr); 796 assertEquals(revBearerData.displayModeSet, true); 797 assertEquals(revBearerData.displayMode, bearerData.displayMode); 798 bearerData.displayMode = BearerData.DISPLAY_MODE_USER; 799 encodedSms = BearerData.encode(bearerData); 800 revBearerData = BearerData.decode(encodedSms); 801 assertEquals(revBearerData.userData.payloadStr, userData.payloadStr); 802 assertEquals(revBearerData.displayModeSet, true); 803 assertEquals(revBearerData.displayMode, bearerData.displayMode); 804 } 805 806 @SmallTest 807 public void testIs91() throws Exception { 808 String pdu1 = "000320001001070c2039acc13880"; 809 BearerData bd1 = BearerData.decode(HexDump.hexStringToByteArray(pdu1)); 810 assertEquals(bd1.callbackNumber.address, "3598271"); 811 String pdu4 = "000320001001080c283c314724b34e"; 812 BearerData bd4 = BearerData.decode(HexDump.hexStringToByteArray(pdu4)); 813 assertEquals(bd4.userData.payloadStr, "ABCDEFG"); 814 } 815 816 @SmallTest 817 public void testUserDataHeaderWithEightCharMsg() throws Exception { 818 SmsHeader smsHeader = getConcatUserDataHeader(2, 2); 819 encodeDecodeAssertEquals("01234567", smsHeader, -1); 820 SmsHeader smsHeader2 = getOddLengthUserDataHeader(); 821 encodeDecodeAssertEquals("01234567", smsHeader2, -1); 822 } 823 824 @SmallTest 825 public void testShiftJis() throws Exception { 826 encodeDecodeAssertEquals(sHelloWorldJa, null, UserData.ENCODING_UNICODE_16); 827 encodeDecodeAssertEquals(sHelloWorldJa, null, UserData.ENCODING_SHIFT_JIS); 828 } 829 830 @SmallTest 831 public void testIgnoreReservedSubparam() throws Exception { 832 BearerData bearerData = new BearerData(); 833 bearerData.messageType = BearerData.MESSAGE_TYPE_DELIVER; 834 bearerData.messageId = 1234; 835 UserData userData = new UserData(); 836 userData.payloadStr = sHelloWorldJa; 837 bearerData.userData = userData; 838 byte[] encodedSms = BearerData.encode(bearerData); 839 BearerData revBearerData = BearerData.decode(encodedSms); 840 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 841 842 byte[] smsWithValidSubparam = Arrays.copyOf(encodedSms, encodedSms.length + 5); 843 smsWithValidSubparam[encodedSms.length] = 0x18; // BearerData.SUBPARAM_ID_LAST_DEFINED + 1 844 smsWithValidSubparam[encodedSms.length + 1] = 3; 845 smsWithValidSubparam[encodedSms.length + 2] = 0x12; 846 smsWithValidSubparam[encodedSms.length + 3] = 0x34; 847 smsWithValidSubparam[encodedSms.length + 4] = 0x56; 848 revBearerData = BearerData.decode(smsWithValidSubparam); 849 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 850 851 smsWithValidSubparam = Arrays.copyOf(encodedSms, encodedSms.length + 2); 852 smsWithValidSubparam[encodedSms.length] = 0x18; 853 smsWithValidSubparam[encodedSms.length + 1] = 0; 854 revBearerData = BearerData.decode(smsWithValidSubparam); 855 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 856 857 byte[] smsWithInvalidSubparam = Arrays.copyOf(encodedSms, encodedSms.length + 2); 858 smsWithInvalidSubparam[encodedSms.length] = 0x18; 859 smsWithInvalidSubparam[encodedSms.length + 1] = (byte) 1; 860 revBearerData = BearerData.decode(smsWithInvalidSubparam); 861 assertNull(revBearerData); 862 } 863 864 // Return a user data header for a concatenated message 865 private static SmsHeader getConcatUserDataHeader(int index, int total) { 866 SmsHeader smsHeader = new SmsHeader(); 867 SmsHeader.ConcatRef concatRef = new SmsHeader.ConcatRef(); 868 concatRef.refNumber = 0xEE; 869 concatRef.msgCount = total; 870 concatRef.seqNumber = index; 871 concatRef.isEightBits = true; 872 smsHeader.concatRef = concatRef; 873 return smsHeader; 874 } 875 876 // Return a user data header of odd length to verify correct UTF-16 header padding 877 private static SmsHeader getOddLengthUserDataHeader() { 878 SmsHeader smsHeader = new SmsHeader(); 879 SmsHeader.MiscElt miscElt = new SmsHeader.MiscElt(); 880 miscElt.id = 0x27; // reserved for future use; ignored on decode 881 miscElt.data = new byte[]{0x12, 0x34}; 882 smsHeader.miscEltList.add(miscElt); 883 return smsHeader; 884 } 885 886 private static void encodeDecodeAssertEquals(String payload, SmsHeader smsHeader, 887 int msgEncoding) throws Exception { 888 BearerData bearerData = new BearerData(); 889 bearerData.messageType = BearerData.MESSAGE_TYPE_DELIVER; 890 bearerData.messageId = 55; 891 UserData userData = new UserData(); 892 userData.payloadStr = payload; 893 userData.userDataHeader = smsHeader; // may be null 894 if (msgEncoding != -1) { 895 userData.msgEncoding = msgEncoding; 896 userData.msgEncodingSet = true; 897 } 898 bearerData.userData = userData; 899 byte[] encodedSms = BearerData.encode(bearerData); 900 BearerData revBearerData = BearerData.decode(encodedSms); 901 assertEquals(userData.payloadStr, revBearerData.userData.payloadStr); 902 if (smsHeader != null) { 903 assertTrue(revBearerData.hasUserDataHeader); 904 byte[] encodeHeader = SmsHeader.toByteArray(smsHeader); 905 byte[] decodeHeader = SmsHeader.toByteArray(revBearerData.userData.userDataHeader); 906 assertTrue(Arrays.equals(encodeHeader, decodeHeader)); 907 } else { 908 assertFalse(revBearerData.hasUserDataHeader); 909 } 910 } 911 912 @SmallTest 913 public void testFragmentText() throws Exception { 914 boolean isCdmaPhone = (TelephonyManager.getDefault().getPhoneType() == 915 TelephonyManager.PHONE_TYPE_CDMA); 916 // Valid 160 character ASCII text. 917 String text1 = "123456789012345678901234567890123456789012345678901234567890" + 918 "1234567890123456789012345678901234567890123456789012345678901234567890" + 919 "12345678901234567890123456789["; 920 TextEncodingDetails ted = SmsMessage.calculateLength(text1, false, true); 921 assertEquals(ted.msgCount, 1); 922 assertEquals(ted.codeUnitCount, 160); 923 assertEquals(ted.codeUnitSize, 1); 924 if (isCdmaPhone) { 925 ArrayList<String> fragments = android.telephony.SmsMessage.fragmentText(text1); 926 assertEquals(fragments.size(), 1); 927 } 928 929 /* 930 This is not a valid test: we will never encode a single-segment 931 EMS message. Leaving this here, since we may try to support 932 this in the future. 933 934 // Valid 160 character GSM text -- the last character is 935 // non-ASCII, and so this will currently generate a singleton 936 // EMS message, which is not necessarily supported by Verizon. 937 String text2 = "123456789012345678901234567890123456789012345678901234567890" + 938 "1234567890123456789012345678901234567890123456789012345678901234567890" + 939 "12345678901234567890123456789\u00a3"; // Trailing pound-currency sign. 940 ted = SmsMessage.calculateLength(text2, false); 941 assertEquals(ted.msgCount, 1); 942 assertEquals(ted.codeUnitCount, 160); 943 assertEquals(ted.codeUnitSize, 1); 944 if (isCdmaPhone) { 945 ArrayList<String> fragments = android.telephony.SmsMessage.fragmentText(text2); 946 assertEquals(fragments.size(), 1); 947 } 948 */ 949 950 // *IF* we supported single-segment EMS, this text would result in a 951 // single fragment with 7-bit encoding. But we don't, so this text 952 // results in three fragments of 16-bit encoding. 953 String text2 = "123456789012345678901234567890123456789012345678901234567890" + 954 "1234567890123456789012345678901234567890123456789012345678901234567890" + 955 "12345678901234567890123456789\u00a3"; // Trailing pound-currency sign. 956 ted = SmsMessage.calculateLength(text2, false, true); 957 assertEquals(3, ted.msgCount); 958 assertEquals(160, ted.codeUnitCount); 959 assertEquals(3, ted.codeUnitSize); 960 if (isCdmaPhone) { 961 ArrayList<String> fragments = android.telephony.SmsMessage.fragmentText(text2); 962 assertEquals(3, fragments.size()); 963 964 for (int i = 0; i < 3; i++) { 965 SmsHeader header = getConcatUserDataHeader(i + 1, 3); 966 SmsHeader header2 = getOddLengthUserDataHeader(); 967 encodeDecodeAssertEquals(fragments.get(i), header, -1); 968 encodeDecodeAssertEquals(fragments.get(i), header2, -1); 969 } 970 } 971 972 // Test case for multi-part UTF-16 message. 973 String text3 = sUnicodeChars + sUnicodeChars + sUnicodeChars; 974 ted = SmsMessage.calculateLength(text3, false, true); 975 assertEquals(3, ted.msgCount); 976 assertEquals(189, ted.codeUnitCount); 977 assertEquals(3, ted.codeUnitSize); 978 if (isCdmaPhone) { 979 ArrayList<String> fragments = android.telephony.SmsMessage.fragmentText(text3); 980 assertEquals(3, fragments.size()); 981 982 for (int i = 0; i < 3; i++) { 983 SmsHeader header = getConcatUserDataHeader(i + 1, 3); 984 SmsHeader header2 = getOddLengthUserDataHeader(); 985 encodeDecodeAssertEquals(fragments.get(i), header, -1); 986 encodeDecodeAssertEquals(fragments.get(i), header2, -1); 987 } 988 } 989 } 990 } 991