1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/system/gonk/tests/test_ril_worker_sms_gsmpduhelper.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,235 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +subscriptLoader.loadSubScript("resource://gre/modules/ril_consts.js", this); 1.8 + 1.9 +function run_test() { 1.10 + run_next_test(); 1.11 +} 1.12 + 1.13 +/** 1.14 + * Verify GsmPDUHelper#readDataCodingScheme. 1.15 + */ 1.16 +add_test(function test_GsmPDUHelper_readDataCodingScheme() { 1.17 + let worker = newWorker({ 1.18 + postRILMessage: function(data) { 1.19 + // Do nothing 1.20 + }, 1.21 + postMessage: function(message) { 1.22 + // Do nothing 1.23 + } 1.24 + }); 1.25 + 1.26 + let context = worker.ContextPool._contexts[0]; 1.27 + let helper = context.GsmPDUHelper; 1.28 + function test_dcs(dcs, encoding, messageClass, mwi) { 1.29 + helper.readHexOctet = function() { 1.30 + return dcs; 1.31 + } 1.32 + 1.33 + let msg = {}; 1.34 + helper.readDataCodingScheme(msg); 1.35 + 1.36 + do_check_eq(msg.dcs, dcs); 1.37 + do_check_eq(msg.encoding, encoding); 1.38 + do_check_eq(msg.messageClass, messageClass); 1.39 + do_check_eq(msg.mwi == null, mwi == null); 1.40 + if (mwi != null) { 1.41 + do_check_eq(msg.mwi.active, mwi.active); 1.42 + do_check_eq(msg.mwi.discard, mwi.discard); 1.43 + do_check_eq(msg.mwi.msgCount, mwi.msgCount); 1.44 + } 1.45 + } 1.46 + 1.47 + // Group 00xx 1.48 + // Bit 3 and 2 indicate the character set being used. 1.49 + test_dcs(0x00, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.50 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL]); 1.51 + test_dcs(0x04, PDU_DCS_MSG_CODING_8BITS_ALPHABET, 1.52 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL]); 1.53 + test_dcs(0x08, PDU_DCS_MSG_CODING_16BITS_ALPHABET, 1.54 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL]); 1.55 + test_dcs(0x0C, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.56 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL]); 1.57 + // Bit 4, if set to 0, indicates that bits 1 to 0 are reserved and have no 1.58 + // message class meaning. 1.59 + test_dcs(0x01, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.60 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL]); 1.61 + test_dcs(0x02, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.62 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL]); 1.63 + test_dcs(0x03, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.64 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL]); 1.65 + // Bit 4, if set to 1, indicates that bits 1 to 0 have a message class meaning. 1.66 + test_dcs(0x10, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.67 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_0]); 1.68 + test_dcs(0x11, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.69 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_1]); 1.70 + test_dcs(0x12, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.71 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_2]); 1.72 + test_dcs(0x13, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.73 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_3]); 1.74 + 1.75 + // Group 01xx 1.76 + test_dcs(0x50, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.77 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_0]); 1.78 + 1.79 + // Group 1000..1011: reserved 1.80 + test_dcs(0x8F, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.81 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL]); 1.82 + test_dcs(0x9F, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.83 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL]); 1.84 + test_dcs(0xAF, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.85 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL]); 1.86 + test_dcs(0xBF, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.87 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL]); 1.88 + 1.89 + // Group 1100: Message Waiting Indication Group: Discard Message 1.90 + // Bit 3 indicates Indication Sense: 1.91 + test_dcs(0xC0, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.92 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL], 1.93 + {active: false, discard: true, msgCount: 0}); 1.94 + test_dcs(0xC8, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.95 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL], 1.96 + {active: true, discard: true, msgCount: -1}); 1.97 + // Bit 2 is reserved, and set to 0: 1.98 + test_dcs(0xCC, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.99 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL], 1.100 + {active: true, discard: true, msgCount: -1}); 1.101 + 1.102 + // Group 1101: Message Waiting Indication Group: Store Message 1.103 + // Bit 3 indicates Indication Sense: 1.104 + test_dcs(0xD0, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.105 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL], 1.106 + {active: false, discard: false, msgCount: 0}); 1.107 + test_dcs(0xD8, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.108 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL], 1.109 + {active: true, discard: false, msgCount: -1}); 1.110 + // Bit 2 is reserved, and set to 0: 1.111 + test_dcs(0xDC, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.112 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL], 1.113 + {active: true, discard: false, msgCount: -1}); 1.114 + 1.115 + // Group 1110: Message Waiting Indication Group: Store Message, UCS2 1.116 + // Bit 3 indicates Indication Sense: 1.117 + test_dcs(0xE0, PDU_DCS_MSG_CODING_16BITS_ALPHABET, 1.118 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL], 1.119 + {active: false, discard: false, msgCount: 0}); 1.120 + test_dcs(0xE8, PDU_DCS_MSG_CODING_16BITS_ALPHABET, 1.121 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL], 1.122 + {active: true, discard: false, msgCount: -1}); 1.123 + // Bit 2 is reserved, and set to 0: 1.124 + test_dcs(0xEC, PDU_DCS_MSG_CODING_16BITS_ALPHABET, 1.125 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_NORMAL], 1.126 + {active: true, discard: false, msgCount: -1}); 1.127 + 1.128 + // Group 1111 1.129 + test_dcs(0xF0, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.130 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_0]); 1.131 + test_dcs(0xF1, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.132 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_1]); 1.133 + test_dcs(0xF2, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.134 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_2]); 1.135 + test_dcs(0xF3, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.136 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_3]); 1.137 + test_dcs(0xF4, PDU_DCS_MSG_CODING_8BITS_ALPHABET, 1.138 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_0]); 1.139 + test_dcs(0xF5, PDU_DCS_MSG_CODING_8BITS_ALPHABET, 1.140 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_1]); 1.141 + test_dcs(0xF6, PDU_DCS_MSG_CODING_8BITS_ALPHABET, 1.142 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_2]); 1.143 + test_dcs(0xF7, PDU_DCS_MSG_CODING_8BITS_ALPHABET, 1.144 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_3]); 1.145 + // Bit 3 is reserved and should be set to 0, but if it doesn't we should 1.146 + // ignore it. 1.147 + test_dcs(0xF8, PDU_DCS_MSG_CODING_7BITS_ALPHABET, 1.148 + GECKO_SMS_MESSAGE_CLASSES[PDU_DCS_MSG_CLASS_0]); 1.149 + 1.150 + run_next_test(); 1.151 +}); 1.152 + 1.153 +/** 1.154 + * Verify GsmPDUHelper#writeStringAsSeptets() padding bits handling. 1.155 + */ 1.156 +add_test(function test_GsmPDUHelper_writeStringAsSeptets() { 1.157 + let worker = newWorker({ 1.158 + postRILMessage: function(data) { 1.159 + // Do nothing 1.160 + }, 1.161 + postMessage: function(message) { 1.162 + // Do nothing 1.163 + } 1.164 + }); 1.165 + 1.166 + let context = worker.ContextPool._contexts[0]; 1.167 + let helper = context.GsmPDUHelper; 1.168 + helper.resetOctetWritten = function() { 1.169 + helper.octetsWritten = 0; 1.170 + }; 1.171 + helper.writeHexOctet = function() { 1.172 + helper.octetsWritten++; 1.173 + }; 1.174 + 1.175 + let base = "AAAAAAAA"; // Base string of 8 characters long 1.176 + for (let len = 0; len < 8; len++) { 1.177 + let str = base.substring(0, len); 1.178 + 1.179 + for (let paddingBits = 0; paddingBits < 8; paddingBits++) { 1.180 + do_print("Verifying GsmPDUHelper.writeStringAsSeptets(" 1.181 + + str + ", " + paddingBits + ", <default>, <default>)"); 1.182 + helper.resetOctetWritten(); 1.183 + helper.writeStringAsSeptets(str, paddingBits, PDU_NL_IDENTIFIER_DEFAULT, 1.184 + PDU_NL_IDENTIFIER_DEFAULT); 1.185 + do_check_eq(Math.ceil(((len * 7) + paddingBits) / 8), 1.186 + helper.octetsWritten); 1.187 + } 1.188 + } 1.189 + 1.190 + run_next_test(); 1.191 +}); 1.192 + 1.193 +/** 1.194 + * Verify GsmPDUHelper#readAddress 1.195 + */ 1.196 +add_test(function test_GsmPDUHelper_readAddress() { 1.197 + let worker = newWorker({ 1.198 + postRILMessage: function(data) { 1.199 + // Do nothing 1.200 + }, 1.201 + postMessage: function(message) { 1.202 + // Do nothing 1.203 + } 1.204 + }); 1.205 + 1.206 + let context = worker.ContextPool._contexts[0]; 1.207 + let helper = context.GsmPDUHelper; 1.208 + function test_address(addrHex, addrString) { 1.209 + let uint16Array = []; 1.210 + let ix = 0; 1.211 + for (let i = 0; i < addrHex.length; ++i) { 1.212 + uint16Array[i] = addrHex[i].charCodeAt(); 1.213 + } 1.214 + 1.215 + context.Buf.readUint16 = function(){ 1.216 + if(ix >= uint16Array.length) { 1.217 + do_throw("out of range in uint16Array"); 1.218 + } 1.219 + return uint16Array[ix++]; 1.220 + } 1.221 + let length = helper.readHexOctet(); 1.222 + let parsedAddr = helper.readAddress(length); 1.223 + do_check_eq(parsedAddr, addrString); 1.224 + } 1.225 + 1.226 + // For AlphaNumeric 1.227 + test_address("04D01100", "_@"); 1.228 + test_address("04D01000", "\u0394@"); 1.229 + 1.230 + // Direct prepand 1.231 + test_address("0B914151245584F6", "+14154255486"); 1.232 + test_address("0E914151245584B633", "+14154255486#33"); 1.233 + 1.234 + // PDU_TOA_NATIONAL 1.235 + test_address("0BA14151245584F6", "14154255486"); 1.236 + 1.237 + run_next_test(); 1.238 +});