dom/system/gonk/tests/test_ril_worker_sms.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 const ESCAPE = "\uffff";
michael@0 7 const RESCTL = "\ufffe";
michael@0 8
michael@0 9 function run_test() {
michael@0 10 run_next_test();
michael@0 11 }
michael@0 12
michael@0 13 /**
michael@0 14 * Verify receiving SMS-DELIVERY messages
michael@0 15 */
michael@0 16
michael@0 17 function hexToNibble(nibble) {
michael@0 18 nibble &= 0x0f;
michael@0 19 if (nibble < 10) {
michael@0 20 nibble += 48; // ASCII '0'
michael@0 21 } else {
michael@0 22 nibble += 55; // ASCII 'A'
michael@0 23 }
michael@0 24 return nibble;
michael@0 25 }
michael@0 26
michael@0 27 function pduToParcelData(pdu) {
michael@0 28 let dataLength = 4 + pdu.length * 4 + 4;
michael@0 29 let data = new Uint8Array(dataLength);
michael@0 30 let offset = 0;
michael@0 31
michael@0 32 // String length
michael@0 33 data[offset++] = pdu.length & 0xFF;
michael@0 34 data[offset++] = (pdu.length >> 8) & 0xFF;
michael@0 35 data[offset++] = (pdu.length >> 16) & 0xFF;
michael@0 36 data[offset++] = (pdu.length >> 24) & 0xFF;
michael@0 37
michael@0 38 // PDU data
michael@0 39 for (let i = 0; i < pdu.length; i++) {
michael@0 40 let hi = (pdu[i] >>> 4) & 0x0F;
michael@0 41 let lo = pdu[i] & 0x0F;
michael@0 42
michael@0 43 data[offset++] = hexToNibble(hi);
michael@0 44 data[offset++] = 0;
michael@0 45 data[offset++] = hexToNibble(lo);
michael@0 46 data[offset++] = 0;
michael@0 47 }
michael@0 48
michael@0 49 // String delimitor
michael@0 50 data[offset++] = 0;
michael@0 51 data[offset++] = 0;
michael@0 52 data[offset++] = 0;
michael@0 53 data[offset++] = 0;
michael@0 54
michael@0 55 return data;
michael@0 56 }
michael@0 57
michael@0 58 function compose7bitPdu(lst, sst, data, septets) {
michael@0 59 if ((lst == 0) && (sst == 0)) {
michael@0 60 return [0x00, // SMSC
michael@0 61 PDU_MTI_SMS_DELIVER, // firstOctet
michael@0 62 1, 0x00, 0, // senderAddress
michael@0 63 0x00, // protocolIdentifier
michael@0 64 PDU_DCS_MSG_CODING_7BITS_ALPHABET, // dataCodingScheme
michael@0 65 0, 0, 0, 0, 0, 0, 0, // y m d h m s tz
michael@0 66 septets] // userDataLength
michael@0 67 .concat(data);
michael@0 68 }
michael@0 69
michael@0 70 return [0x00, // SMSC
michael@0 71 PDU_MTI_SMS_DELIVER | PDU_UDHI, // firstOctet
michael@0 72 1, 0x00, 0, // senderAddress
michael@0 73 0x00, // protocolIdentifier
michael@0 74 PDU_DCS_MSG_CODING_7BITS_ALPHABET, // dataCodingScheme
michael@0 75 0, 0, 0, 0, 0, 0, 0, // y m d h m s tz
michael@0 76 8 + septets, // userDataLength
michael@0 77 6, // user data header length
michael@0 78 PDU_IEI_NATIONAL_LANGUAGE_LOCKING_SHIFT, 1, lst, // PDU_IEI_NATIONAL_LANGUAGE_LOCKING_SHIFT
michael@0 79 PDU_IEI_NATIONAL_LANGUAGE_SINGLE_SHIFT, 1, sst] // PDU_IEI_NATIONAL_LANGUAGE_SINGLE_SHIFT
michael@0 80 .concat(data);
michael@0 81 }
michael@0 82
michael@0 83 function composeUcs2Pdu(rawBytes) {
michael@0 84 return [0x00, // SMSC
michael@0 85 PDU_MTI_SMS_DELIVER, // firstOctet
michael@0 86 1, 0x00, 0, // senderAddress
michael@0 87 0x00, // protocolIdentifier
michael@0 88 PDU_DCS_MSG_CODING_16BITS_ALPHABET, // dataCodingScheme
michael@0 89 0, 0, 0, 0, 0, 0, 0, // y m d h m s tz
michael@0 90 rawBytes.length] // userDataLength
michael@0 91 .concat(rawBytes);
michael@0 92 }
michael@0 93
michael@0 94 function newSmsParcel(pdu) {
michael@0 95 return newIncomingParcel(-1,
michael@0 96 RESPONSE_TYPE_UNSOLICITED,
michael@0 97 UNSOLICITED_RESPONSE_NEW_SMS,
michael@0 98 pduToParcelData(pdu));
michael@0 99 }
michael@0 100
michael@0 101 function removeSpecialChar(str, needle) {
michael@0 102 for (let i = 0; i < needle.length; i++) {
michael@0 103 let pos;
michael@0 104 while ((pos = str.indexOf(needle[i])) >= 0) {
michael@0 105 str = str.substring(0, pos) + str.substring(pos + 1);
michael@0 106 }
michael@0 107 }
michael@0 108 return str;
michael@0 109 }
michael@0 110
michael@0 111 function newWriteHexOctetAsUint8Worker() {
michael@0 112 let worker = newWorker({
michael@0 113 postRILMessage: function(data) {
michael@0 114 // Do nothing
michael@0 115 },
michael@0 116 postMessage: function(message) {
michael@0 117 // Do nothing
michael@0 118 }
michael@0 119 });
michael@0 120
michael@0 121 let context = worker.ContextPool._contexts[0];
michael@0 122 context.GsmPDUHelper.writeHexOctet = function(value) {
michael@0 123 context.Buf.writeUint8(value);
michael@0 124 };
michael@0 125
michael@0 126 return worker;
michael@0 127 }
michael@0 128
michael@0 129 function add_test_receiving_sms(expected, pdu) {
michael@0 130 add_test(function test_receiving_sms() {
michael@0 131 let worker = newWorker({
michael@0 132 postRILMessage: function(data) {
michael@0 133 // Do nothing
michael@0 134 },
michael@0 135 postMessage: function(message) {
michael@0 136 do_print("fullBody: " + message.fullBody);
michael@0 137 do_check_eq(expected, message.fullBody)
michael@0 138 }
michael@0 139 });
michael@0 140
michael@0 141 do_print("expect: " + expected);
michael@0 142 do_print("pdu: " + pdu);
michael@0 143 worker.onRILMessage(0, newSmsParcel(pdu));
michael@0 144
michael@0 145 run_next_test();
michael@0 146 });
michael@0 147 }
michael@0 148
michael@0 149 let test_receiving_7bit_alphabets__ril;
michael@0 150 let test_receiving_7bit_alphabets__worker;
michael@0 151 function test_receiving_7bit_alphabets(lst, sst) {
michael@0 152 if (!test_receiving_7bit_alphabets__ril) {
michael@0 153 test_receiving_7bit_alphabets__ril = newRadioInterface();
michael@0 154 test_receiving_7bit_alphabets__worker = newWriteHexOctetAsUint8Worker();
michael@0 155 }
michael@0 156 let ril = test_receiving_7bit_alphabets__ril;
michael@0 157 let worker = test_receiving_7bit_alphabets__worker;
michael@0 158 let context = worker.ContextPool._contexts[0];
michael@0 159 let helper = context.GsmPDUHelper;
michael@0 160 let buf = context.Buf;
michael@0 161
michael@0 162 function get7bitRawBytes(expected) {
michael@0 163 buf.outgoingIndex = 0;
michael@0 164 helper.writeStringAsSeptets(expected, 0, lst, sst);
michael@0 165
michael@0 166 let subArray = buf.outgoingBytes.subarray(0, buf.outgoingIndex);
michael@0 167 return Array.slice(subArray);
michael@0 168 }
michael@0 169
michael@0 170 let langTable = PDU_NL_LOCKING_SHIFT_TABLES[lst];
michael@0 171 let langShiftTable = PDU_NL_SINGLE_SHIFT_TABLES[sst];
michael@0 172
michael@0 173 let text = removeSpecialChar(langTable + langShiftTable, ESCAPE + RESCTL);
michael@0 174 for (let i = 0; i < text.length;) {
michael@0 175 let len = Math.min(70, text.length - i);
michael@0 176 let expected = text.substring(i, i + len);
michael@0 177 let septets = ril._countGsm7BitSeptets(expected, langTable, langShiftTable);
michael@0 178 let rawBytes = get7bitRawBytes(expected);
michael@0 179 let pdu = compose7bitPdu(lst, sst, rawBytes, septets);
michael@0 180 add_test_receiving_sms(expected, pdu);
michael@0 181
michael@0 182 i += len;
michael@0 183 }
michael@0 184 }
michael@0 185
michael@0 186 function test_receiving_ucs2_alphabets(text) {
michael@0 187 let worker = test_receiving_7bit_alphabets__worker;
michael@0 188 let context = worker.ContextPool._contexts[0];
michael@0 189 let buf = context.Buf;
michael@0 190
michael@0 191 function getUCS2RawBytes(expected) {
michael@0 192 buf.outgoingIndex = 0;
michael@0 193 context.GsmPDUHelper.writeUCS2String(expected);
michael@0 194
michael@0 195 let subArray = buf.outgoingBytes.subarray(0, buf.outgoingIndex);
michael@0 196 return Array.slice(subArray);
michael@0 197 }
michael@0 198
michael@0 199 for (let i = 0; i < text.length;) {
michael@0 200 let len = Math.min(70, text.length - i);
michael@0 201 let expected = text.substring(i, i + len);
michael@0 202 let rawBytes = getUCS2RawBytes(expected);
michael@0 203 let pdu = composeUcs2Pdu(rawBytes);
michael@0 204 add_test_receiving_sms(expected, pdu);
michael@0 205
michael@0 206 i += len;
michael@0 207 }
michael@0 208 }
michael@0 209
michael@0 210 let ucs2str = "";
michael@0 211 for (let lst = 0; lst < PDU_NL_LOCKING_SHIFT_TABLES.length; lst++) {
michael@0 212 ucs2str += PDU_NL_LOCKING_SHIFT_TABLES[lst];
michael@0 213 for (let sst = 0; sst < PDU_NL_SINGLE_SHIFT_TABLES.length; sst++) {
michael@0 214 test_receiving_7bit_alphabets(lst, sst);
michael@0 215
michael@0 216 if (lst == 0) {
michael@0 217 ucs2str += PDU_NL_SINGLE_SHIFT_TABLES[sst];
michael@0 218 }
michael@0 219 }
michael@0 220 }
michael@0 221 test_receiving_ucs2_alphabets(ucs2str);
michael@0 222
michael@0 223 // Bug 820220: B2G SMS: wrong order and truncated content in multi-part messages
michael@0 224 add_test(function test_sendSMS_UCS2_without_langIndex_langShiftIndex_defined() {
michael@0 225 let worker = newWriteHexOctetAsUint8Worker();
michael@0 226 let context = worker.ContextPool._contexts[0];
michael@0 227
michael@0 228 context.Buf.sendParcel = function() {
michael@0 229 // Each sendParcel() call represents one outgoing segment of a multipart
michael@0 230 // SMS message. Here, we have the first segment send, so it's "Hello "
michael@0 231 // only.
michael@0 232 //
michael@0 233 // 4(parcel size) + 4(request type) + 4(token)
michael@0 234 // + 4(two messages) + 4(null SMSC) + 4(message string length)
michael@0 235 // + 1(first octet) + 1(message reference)
michael@0 236 // + 2(DA len, TOA) + 4(addr)
michael@0 237 // + 1(pid) + 1(dcs)
michael@0 238 // + 1(UDL) + 6(UDHL, type, len, ref, max, seq)
michael@0 239 // + 12(2 * strlen("Hello "))
michael@0 240 // + 4(two delimitors) = 57
michael@0 241 //
michael@0 242 // If we have additional 6(type, len, langIndex, type len, langShiftIndex)
michael@0 243 // octets here, then bug 809553 is not fixed.
michael@0 244 do_check_eq(this.outgoingIndex, 57);
michael@0 245
michael@0 246 run_next_test();
michael@0 247 };
michael@0 248
michael@0 249 context.RIL.sendSMS({
michael@0 250 number: "1",
michael@0 251 segmentMaxSeq: 2,
michael@0 252 fullBody: "Hello World!",
michael@0 253 dcs: PDU_DCS_MSG_CODING_16BITS_ALPHABET,
michael@0 254 segmentRef16Bit: false,
michael@0 255 userDataHeaderLength: 5,
michael@0 256 requestStatusReport: true,
michael@0 257 segments: [
michael@0 258 {
michael@0 259 body: "Hello ",
michael@0 260 encodedBodyLength: 12,
michael@0 261 }, {
michael@0 262 body: "World!",
michael@0 263 encodedBodyLength: 12,
michael@0 264 }
michael@0 265 ],
michael@0 266 });
michael@0 267 });

mercurial