dom/system/gonk/tests/test_ril_worker_sms_cdma.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 subscriptLoader.loadSubScript("resource://gre/modules/ril_consts.js", this);
michael@0 5
michael@0 6 function run_test() {
michael@0 7 run_next_test();
michael@0 8 }
michael@0 9
michael@0 10 function _getWorker() {
michael@0 11 let _postedMessage;
michael@0 12 let _worker = newWorker({
michael@0 13 postRILMessage: function(data) {
michael@0 14 },
michael@0 15 postMessage: function(message) {
michael@0 16 _postedMessage = message;
michael@0 17 }
michael@0 18 });
michael@0 19 return {
michael@0 20 get postedMessage() {
michael@0 21 return _postedMessage;
michael@0 22 },
michael@0 23 get worker() {
michael@0 24 return _worker;
michael@0 25 }
michael@0 26 };
michael@0 27 }
michael@0 28
michael@0 29 /*
michael@0 30 * Helper function to covert a HEX string to a byte array.
michael@0 31 *
michael@0 32 * @param hexString
michael@0 33 * A hexadecimal string of which the length is even.
michael@0 34 */
michael@0 35 function hexStringToBytes(hexString) {
michael@0 36 let bytes = [];
michael@0 37
michael@0 38 let length = hexString.length;
michael@0 39
michael@0 40 for (let i = 0; i < length; i += 2) {
michael@0 41 bytes.push(Number.parseInt(hexString.substr(i, 2), 16));
michael@0 42 }
michael@0 43
michael@0 44 return bytes;
michael@0 45 }
michael@0 46
michael@0 47 /*
michael@0 48 * Helper function to covert a byte array to a HEX string.
michael@0 49 *
michael@0 50 * @param bytes
michael@0 51 * Could be a regular byte array or Uint8Array.
michael@0 52 */
michael@0 53 function bytesToHexString(bytes) {
michael@0 54 let hexString = "";
michael@0 55 let hex;
michael@0 56
michael@0 57 for (let i = 0; i < bytes.length; i++) {
michael@0 58 hex = bytes[i].toString(16).toUpperCase();
michael@0 59 if (hex.length === 1) {
michael@0 60 hexString += "0";
michael@0 61 }
michael@0 62 hexString += hex;
michael@0 63 }
michael@0 64
michael@0 65 return hexString;
michael@0 66 }
michael@0 67
michael@0 68 /*
michael@0 69 * Helper function to ecode Opaque UserData
michael@0 70 *
michael@0 71 * @param msg_type
michael@0 72 * PDU_CDMA_MSG_TYPE_SUBMIT or PDU_CDMA_MSG_TYPE_DELIVER
michael@0 73 * @param data
michael@0 74 * The byte array of opaque data to be encoded.
michael@0 75 */
michael@0 76 function encodeOpaqueUserData(bitBufferHelper, options) {
michael@0 77 let bearerDataBuffer = [];
michael@0 78 bitBufferHelper.startWrite(bearerDataBuffer);
michael@0 79
michael@0 80 // Msg-Id
michael@0 81 bitBufferHelper.writeBits(PDU_CDMA_MSG_USERDATA_MSG_ID, 8);
michael@0 82 bitBufferHelper.writeBits(3, 8);
michael@0 83 bitBufferHelper.writeBits(options.msg_type, 4); // MSG_TYPE
michael@0 84 bitBufferHelper.writeBits(1, 16); // MSG_ID
michael@0 85 bitBufferHelper.flushWithPadding(); // HEADER_IND (1) + RESERVED (3)
michael@0 86
michael@0 87 // User Data
michael@0 88 bitBufferHelper.writeBits(PDU_CDMA_MSG_USERDATA_BODY, 8);
michael@0 89 let dataLength = options.data.length;
michael@0 90 bitBufferHelper.writeBits(2 + dataLength, 8); // 2 bytes for MSG_ENCODING, NUM_FIELDS
michael@0 91 bitBufferHelper.writeBits(PDU_CDMA_MSG_CODING_OCTET, 5); //MSG_ENCODING
michael@0 92 // MSG_TYPE is omitted if MSG_ENCODING is CODING_OCTET
michael@0 93 bitBufferHelper.writeBits(dataLength, 8); // NUM_FIELDS
michael@0 94 for (let i = 0; i < dataLength; i++) { // CHARi
michael@0 95 bitBufferHelper.writeBits(options.data[i], 8);
michael@0 96 }
michael@0 97 bitBufferHelper.flushWithPadding(); // RESERVED (3 filling bits)
michael@0 98
michael@0 99 return bearerDataBuffer;
michael@0 100 }
michael@0 101
michael@0 102 function newSmsParcel(cdmaPduHelper, pdu) {
michael@0 103 return newIncomingParcel(-1,
michael@0 104 RESPONSE_TYPE_UNSOLICITED,
michael@0 105 UNSOLICITED_RESPONSE_CDMA_NEW_SMS,
michael@0 106 pduToParcelData(cdmaPduHelper, pdu));
michael@0 107 }
michael@0 108
michael@0 109 /*
michael@0 110 * Helper function to encode PDU into Parcel.
michael@0 111 * See ril_cdma_sms.h for the structure definition of RIL_CDMA_SMS_Message
michael@0 112 *
michael@0 113 * @param teleservice
michael@0 114 * The Teleservice-Id of this PDU.
michael@0 115 * See PDU_CDMA_MSG_TELESERIVCIE_ID_XXX in ril_const.js.
michael@0 116 * @param address
michael@0 117 * The Orginating or Destinating address.
michael@0 118 * @param bearerData
michael@0 119 * The byte array of the encoded bearer data.
michael@0 120 */
michael@0 121 function pduToParcelData(cdmaPduHelper, pdu) {
michael@0 122
michael@0 123 let addrInfo = cdmaPduHelper.encodeAddr(pdu.address);
michael@0 124 // Teleservice, isServicePresent, ServiceCategory,
michael@0 125 // addrInfo {digitMode, numberMode, numberType, numberPlan, address.length, address}
michael@0 126 // Sub Address
michael@0 127 // bearerData length, bearerData.
michael@0 128 let dataLength = 4 + 4 + 4
michael@0 129 + (5 + addrInfo.address.length) * 4
michael@0 130 + 3 * 4
michael@0 131 + 4 + pdu.bearerData.length * 4;
michael@0 132
michael@0 133 let data = new Uint8Array(dataLength);
michael@0 134 let offset = 0;
michael@0 135
michael@0 136 function writeInt(value) {
michael@0 137 data[offset++] = value & 0xFF;
michael@0 138 data[offset++] = (value >> 8) & 0xFF;
michael@0 139 data[offset++] = (value >> 16) & 0xFF;
michael@0 140 data[offset++] = (value >> 24) & 0xFF;
michael@0 141 }
michael@0 142
michael@0 143 function writeByte(value) {
michael@0 144 data[offset++] = value & 0xFF;
michael@0 145 data[offset++] = 0;
michael@0 146 data[offset++] = 0;
michael@0 147 data[offset++] = 0;
michael@0 148 }
michael@0 149
michael@0 150 // Teleservice
michael@0 151 writeInt(pdu.teleservice);
michael@0 152
michael@0 153 // isServicePresent
michael@0 154 writeByte(0);
michael@0 155
michael@0 156 // ServiceCategory
michael@0 157 writeInt(PDU_CDMA_MSG_CATEGORY_UNSPEC);
michael@0 158
michael@0 159 // AddrInfo
michael@0 160 writeByte(addrInfo.digitMode);
michael@0 161 writeByte(addrInfo.numberMode);
michael@0 162 writeByte(addrInfo.numberType);
michael@0 163 writeByte(addrInfo.numberPlan);
michael@0 164 let addressLength = addrInfo.address.length;
michael@0 165 writeByte(addressLength);
michael@0 166 for (let i = 0; i < addressLength; i++) {
michael@0 167 writeByte(addrInfo.address[i]);
michael@0 168 }
michael@0 169
michael@0 170 // Subaddress
michael@0 171 writeByte(0);
michael@0 172 writeByte(0);
michael@0 173 writeByte(0);
michael@0 174
michael@0 175 // Bearer Data Length
michael@0 176 let dataLength = pdu.bearerData.length;
michael@0 177 writeByte(dataLength);
michael@0 178
michael@0 179 // Bearer Data
michael@0 180 for (let i = 0; i < dataLength; i++) {
michael@0 181 writeByte(pdu.bearerData[i]);
michael@0 182 }
michael@0 183
michael@0 184 return data;
michael@0 185 }
michael@0 186
michael@0 187 /**
michael@0 188 * Verify CDMA SMS Delivery ACK Message.
michael@0 189 */
michael@0 190 add_test(function test_processCdmaSmsStatusReport() {
michael@0 191 let workerHelper = _getWorker();
michael@0 192 let worker = workerHelper.worker;
michael@0 193 let context = worker.ContextPool._contexts[0];
michael@0 194
michael@0 195 function test_StatusReport(errorClass, msgStatus) {
michael@0 196 let msgId = 0;
michael@0 197 let sentSmsMap = context.RIL._pendingSentSmsMap;
michael@0 198
michael@0 199 sentSmsMap[msgId] = {};
michael@0 200
michael@0 201 let message = {
michael@0 202 SMSC: "",
michael@0 203 mti: 0,
michael@0 204 udhi: 0,
michael@0 205 sender: "0987654321",
michael@0 206 recipient: null,
michael@0 207 pid: PDU_PID_DEFAULT,
michael@0 208 epid: PDU_PID_DEFAULT,
michael@0 209 dcs: 0,
michael@0 210 mwi: null,
michael@0 211 replace: false,
michael@0 212 header: null,
michael@0 213 body: "Status: Sent, Dest: 0987654321",
michael@0 214 data: null,
michael@0 215 timestamp: new Date().valueOf(),
michael@0 216 language: null,
michael@0 217 status: null,
michael@0 218 scts: null,
michael@0 219 dt: null,
michael@0 220 encoding: PDU_CDMA_MSG_CODING_7BITS_ASCII,
michael@0 221 messageClass: GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL],
michael@0 222 messageType: PDU_CDMA_MSG_TYPE_P2P,
michael@0 223 serviceCategory: 0,
michael@0 224 subMsgType: PDU_CDMA_MSG_TYPE_DELIVER_ACK,
michael@0 225 msgId: msgId,
michael@0 226 errorClass: errorClass,
michael@0 227 msgStatus: msgStatus
michael@0 228 };
michael@0 229
michael@0 230 context.RIL._processCdmaSmsStatusReport(message);
michael@0 231
michael@0 232 let postedMessage = workerHelper.postedMessage;
michael@0 233
michael@0 234 // Check if pending token is removed.
michael@0 235 do_check_true((errorClass === 2) ? !!sentSmsMap[msgId] : !sentSmsMap[msgId]);
michael@0 236
michael@0 237 // Check the response message accordingly.
michael@0 238 if (errorClass === -1) {
michael@0 239 // Check if the report is treated as normal incoming SMS
michael@0 240 do_check_eq("sms-received", postedMessage.rilMessageType);
michael@0 241 } else if (errorClass === 2) {
michael@0 242 // Do nothing.
michael@0 243 } else {
michael@0 244 // Check Delivery Status
michael@0 245 if (errorClass === 0) {
michael@0 246 do_check_eq(postedMessage.deliveryStatus, GECKO_SMS_DELIVERY_STATUS_SUCCESS);
michael@0 247 } else {
michael@0 248 do_check_eq(postedMessage.deliveryStatus, GECKO_SMS_DELIVERY_STATUS_ERROR);
michael@0 249 }
michael@0 250 }
michael@0 251 }
michael@0 252
michael@0 253 test_StatusReport(-1, -1); // Message Status Sub-parameter is absent.
michael@0 254 test_StatusReport(0, 0); // 00|000000: no error|Message accepted
michael@0 255 test_StatusReport(2, 4); // 10|000100: temporary condition|Network congestion
michael@0 256 test_StatusReport(3, 5); // 11|000101: permanent condition|Network error
michael@0 257
michael@0 258 run_next_test();
michael@0 259 });
michael@0 260
michael@0 261 /**
michael@0 262 * Verify WAP Push over CDMA SMS Message.
michael@0 263 */
michael@0 264 add_test(function test_processCdmaSmsWapPush() {
michael@0 265 let workerHelper = _getWorker(),
michael@0 266 worker = workerHelper.worker,
michael@0 267 context = worker.ContextPool._contexts[0],
michael@0 268 bitBufferHelper = context.BitBufferHelper,
michael@0 269 cdmaPduHelper = context.CdmaPDUHelper;
michael@0 270
michael@0 271 function test_CdmaSmsWapPdu(wdpData, reversed) {
michael@0 272 let orig_address = "0987654321",
michael@0 273 hexString,
michael@0 274 fullDataHexString = "";
michael@0 275
michael@0 276 for (let i = 0; i < wdpData.length; i++) {
michael@0 277 let dataIndex = (reversed) ? (wdpData.length - i - 1) : i;
michael@0 278 hexString = "00"; // MSG_TYPE
michael@0 279 hexString += bytesToHexString([wdpData.length]); // TOTAL_SEG
michael@0 280 hexString += bytesToHexString([dataIndex]); // SEG_NUM (zero-based)
michael@0 281 if ((dataIndex === 0)) {
michael@0 282 hexString += "23F00B84"; // SOURCE_PORT, DEST_PORT for 1st segment
michael@0 283 }
michael@0 284 hexString += wdpData[dataIndex]; // WDP DATA
michael@0 285
michael@0 286 do_print("hexString: " + hexString);
michael@0 287
michael@0 288 fullDataHexString += wdpData[i];
michael@0 289
michael@0 290 let pdu = {
michael@0 291 teleservice: PDU_CDMA_MSG_TELESERIVCIE_ID_WAP,
michael@0 292 address: orig_address,
michael@0 293 bearerData: encodeOpaqueUserData(bitBufferHelper,
michael@0 294 { msg_type: PDU_CDMA_MSG_TYPE_DELIVER,
michael@0 295 data: hexStringToBytes(hexString) })
michael@0 296 };
michael@0 297
michael@0 298 worker.onRILMessage(0, newSmsParcel(cdmaPduHelper, pdu));
michael@0 299 }
michael@0 300
michael@0 301 let postedMessage = workerHelper.postedMessage;
michael@0 302
michael@0 303 do_print("fullDataHexString: " + fullDataHexString);
michael@0 304
michael@0 305 do_check_eq("sms-received", postedMessage.rilMessageType);
michael@0 306 do_check_eq(PDU_CDMA_MSG_TELESERIVCIE_ID_WAP, postedMessage.teleservice);
michael@0 307 do_check_eq(orig_address, postedMessage.sender);
michael@0 308 do_check_eq(0x23F0, postedMessage.header.originatorPort);
michael@0 309 do_check_eq(0x0B84, postedMessage.header.destinationPort);
michael@0 310 do_check_eq(fullDataHexString, bytesToHexString(postedMessage.data));
michael@0 311 }
michael@0 312
michael@0 313 // Verify Single WAP PDU
michael@0 314 test_CdmaSmsWapPdu(["000102030405060708090A0B0C0D0E0F"]);
michael@0 315
michael@0 316 run_next_test();
michael@0 317 });

mercurial