dom/mobilemessage/tests/test_wsp_pdu_helper.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/mobilemessage/tests/test_wsp_pdu_helper.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1357 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +let WSP = {};
     1.8 +subscriptLoader.loadSubScript("resource://gre/modules/WspPduHelper.jsm", WSP);
     1.9 +WSP.debug = do_print;
    1.10 +
    1.11 +function run_test() {
    1.12 +  run_next_test();
    1.13 +}
    1.14 +
    1.15 +//
    1.16 +// Test target: ensureHeader
    1.17 +//
    1.18 +
    1.19 +add_test(function test_ensureHeader() {
    1.20 +  do_check_throws(function() {
    1.21 +      WSP.ensureHeader({}, "no-such-property");
    1.22 +    }, "FatalCodeError"
    1.23 +  );
    1.24 +
    1.25 +  run_next_test();
    1.26 +});
    1.27 +
    1.28 +//
    1.29 +// Test target: skipValue()
    1.30 +//
    1.31 +
    1.32 +add_test(function test_skipValue() {
    1.33 +  function func(data) {
    1.34 +    return WSP.skipValue(data);
    1.35 +  }
    1.36 +
    1.37 +  // Test for zero-valued first octet:
    1.38 +  wsp_decode_test_ex(func, [0], null);
    1.39 +  // Test first octet < 31
    1.40 +  wsp_decode_test_ex(func, [1, 2], [2]);
    1.41 +  // Test first octet = 31
    1.42 +  wsp_decode_test_ex(func, [31, 0], null);
    1.43 +  wsp_decode_test_ex(func, [31, 1, 2], [2]);
    1.44 +  // Test first octet <= 127
    1.45 +  wsp_decode_test_ex(func, strToCharCodeArray("Hello world!"), "Hello world!");
    1.46 +  // Test first octet >= 128
    1.47 +  wsp_decode_test_ex(func, [0x80 | 0x01], 0x01);
    1.48 +
    1.49 +  run_next_test();
    1.50 +});
    1.51 +
    1.52 +//
    1.53 +// Test target: Octet
    1.54 +//
    1.55 +
    1.56 +//// Octet.decode ////
    1.57 +
    1.58 +add_test(function test_Octet_decode() {
    1.59 +  wsp_decode_test(WSP.Octet, [1], 1);
    1.60 +  wsp_decode_test(WSP.Octet, [], null, "RangeError");
    1.61 +
    1.62 +  run_next_test();
    1.63 +});
    1.64 +
    1.65 +//// Octet.decodeMultiple ////
    1.66 +
    1.67 +add_test(function test_Octet_decodeMultiple() {
    1.68 +  wsp_decode_test_ex(function(data) {
    1.69 +      return WSP.Octet.decodeMultiple(data, 3);
    1.70 +    }, [0, 1, 2], [0, 1, 2], null
    1.71 +  );
    1.72 +  wsp_decode_test_ex(function(data) {
    1.73 +      return WSP.Octet.decodeMultiple(data, 3);
    1.74 +    }, new Uint8Array([0, 1, 2]), new Uint8Array([0, 1, 2]), null
    1.75 +  );
    1.76 +  wsp_decode_test_ex(function(data) {
    1.77 +      return WSP.Octet.decodeMultiple(data, 4);
    1.78 +    }, [0, 1, 2], null, "RangeError"
    1.79 +  );
    1.80 +
    1.81 +  run_next_test();
    1.82 +});
    1.83 +
    1.84 +//// Octet.decodeEqualTo ////
    1.85 +
    1.86 +add_test(function test_Octet_decodeEqualTo() {
    1.87 +  wsp_decode_test_ex(function(data) {
    1.88 +      return WSP.Octet.decodeEqualTo(data, 1);
    1.89 +    }, [1], 1, null
    1.90 +  );
    1.91 +  wsp_decode_test_ex(function(data) {
    1.92 +      return WSP.Octet.decodeEqualTo(data, 2);
    1.93 +    }, [1], null, "CodeError"
    1.94 +  );
    1.95 +  wsp_decode_test_ex(function(data) {
    1.96 +      return WSP.Octet.decodeEqualTo(data, 2);
    1.97 +    }, [], null, "RangeError"
    1.98 +  );
    1.99 +
   1.100 +  run_next_test();
   1.101 +});
   1.102 +
   1.103 +//// Octet.encode ////
   1.104 +
   1.105 +add_test(function test_Octet_encode() {
   1.106 +  for (let i = 0; i < 256; i++) {
   1.107 +    wsp_encode_test(WSP.Octet, i, [i]);
   1.108 +  }
   1.109 +
   1.110 +  run_next_test();
   1.111 +});
   1.112 +
   1.113 +//// Octet.encodeMultiple ////
   1.114 +
   1.115 +add_test(function test_Octet_encodeMultiple() {
   1.116 +  wsp_encode_test_ex(function(data, input) {
   1.117 +    WSP.Octet.encodeMultiple(data, input);
   1.118 +    return data.array;
   1.119 +  }, [0, 1, 2, 3], [0, 1, 2, 3]);
   1.120 +
   1.121 +  run_next_test();
   1.122 +});
   1.123 +
   1.124 +//
   1.125 +// Test target: Text
   1.126 +//
   1.127 +
   1.128 +//// Text.decode ////
   1.129 +
   1.130 +add_test(function test_Text_decode() {
   1.131 +  for (let i = 0; i < 256; i++) {
   1.132 +    if (i == 0) {
   1.133 +      wsp_decode_test(WSP.Text, [0], null, "NullCharError");
   1.134 +    } else if ((i < WSP.CTLS) || (i == WSP.DEL)) {
   1.135 +      wsp_decode_test(WSP.Text, [i], null, "CodeError");
   1.136 +    } else {
   1.137 +      wsp_decode_test(WSP.Text, [i], String.fromCharCode(i));
   1.138 +    }
   1.139 +  }
   1.140 +  // Test \r\n(SP|HT)* sequence:
   1.141 +  wsp_decode_test(WSP.Text, strToCharCodeArray("\r\n \t \t \t", true), " ");
   1.142 +  wsp_decode_test(WSP.Text, strToCharCodeArray("\r\n \t \t \t"), " ");
   1.143 +  wsp_decode_test(WSP.Text, strToCharCodeArray("\r\n \t \t \tA"), " ");
   1.144 +
   1.145 +  run_next_test();
   1.146 +});
   1.147 +
   1.148 +//// Text.encode ////
   1.149 +
   1.150 +add_test(function test_Text_encode() {
   1.151 +  for (let i = 0; i < 256; i++) {
   1.152 +    if ((i < WSP.CTLS) || (i == WSP.DEL)) {
   1.153 +      wsp_encode_test(WSP.Text, String.fromCharCode(i), null, "CodeError");
   1.154 +    } else {
   1.155 +      wsp_encode_test(WSP.Text, String.fromCharCode(i), [i]);
   1.156 +    }
   1.157 +  }
   1.158 +
   1.159 +  run_next_test();
   1.160 +});
   1.161 +
   1.162 +//
   1.163 +// Test target: NullTerminatedTexts
   1.164 +//
   1.165 +
   1.166 +//// NullTerminatedTexts.decode ////
   1.167 +
   1.168 +add_test(function test_NullTerminatedTexts_decode() {
   1.169 +  // Test incompleted string:
   1.170 +  wsp_decode_test(WSP.NullTerminatedTexts, strToCharCodeArray(" ", true), null, "RangeError");
   1.171 +  // Test control char:
   1.172 +  wsp_decode_test(WSP.NullTerminatedTexts, strToCharCodeArray(" \n"), null, "CodeError");
   1.173 +  // Test normal string:
   1.174 +  wsp_decode_test(WSP.NullTerminatedTexts, strToCharCodeArray(""), "");
   1.175 +  wsp_decode_test(WSP.NullTerminatedTexts, strToCharCodeArray("oops"), "oops");
   1.176 +  // Test \r\n(SP|HT)* sequence:
   1.177 +  wsp_decode_test(WSP.NullTerminatedTexts, strToCharCodeArray("A\r\n \t \t \tB"), "A B");
   1.178 +
   1.179 +  run_next_test();
   1.180 +});
   1.181 +
   1.182 +//// NullTerminatedTexts.encode ////
   1.183 +
   1.184 +add_test(function test_NullTerminatedTexts_encode() {
   1.185 +  wsp_encode_test(WSP.NullTerminatedTexts, "", [0]);
   1.186 +  wsp_encode_test(WSP.NullTerminatedTexts, "Hello, World!",
   1.187 +                  strToCharCodeArray("Hello, World!"));
   1.188 +
   1.189 +  run_next_test();
   1.190 +});
   1.191 +
   1.192 +//
   1.193 +// Test target: Token
   1.194 +//
   1.195 +
   1.196 +let TOKEN_SEPS = "()<>@,;:\\\"/[]?={} \t";
   1.197 +
   1.198 +//// Token.decode ////
   1.199 +
   1.200 +add_test(function test_Token_decode() {
   1.201 +  for (let i = 0; i < 256; i++) {
   1.202 +    if (i == 0) {
   1.203 +      wsp_decode_test(WSP.Token, [i], null, "NullCharError");
   1.204 +    } else if ((i < WSP.CTLS) || (i >= WSP.ASCIIS)
   1.205 +        || (TOKEN_SEPS.indexOf(String.fromCharCode(i)) >= 0)) {
   1.206 +      wsp_decode_test(WSP.Token, [i], null, "CodeError");
   1.207 +    } else {
   1.208 +      wsp_decode_test(WSP.Token, [i], String.fromCharCode(i));
   1.209 +    }
   1.210 +  }
   1.211 +
   1.212 +  run_next_test();
   1.213 +});
   1.214 +
   1.215 +//// Token.encode ////
   1.216 +
   1.217 +add_test(function test_Token_encode() {
   1.218 +  for (let i = 0; i < 256; i++) {
   1.219 +    if ((i < WSP.CTLS) || (i >= WSP.ASCIIS)
   1.220 +        || (TOKEN_SEPS.indexOf(String.fromCharCode(i)) >= 0)) {
   1.221 +      wsp_encode_test(WSP.Token, String.fromCharCode(i), null, "CodeError");
   1.222 +    } else {
   1.223 +      wsp_encode_test(WSP.Token, String.fromCharCode(i), [i]);
   1.224 +    }
   1.225 +  }
   1.226 +
   1.227 +  run_next_test();
   1.228 +});
   1.229 +
   1.230 +//
   1.231 +// Test target: URIC
   1.232 +//
   1.233 +
   1.234 +//// URIC.decode ////
   1.235 +
   1.236 +add_test(function test_URIC_decode() {
   1.237 +  let uric = "!#$%&'()*+,-./0123456789:;=?@ABCDEFGHIJKLMN"
   1.238 +             + "OPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~";
   1.239 +  for (let i = 0; i < 256; i++) {
   1.240 +    if (i == 0) {
   1.241 +      wsp_decode_test(WSP.URIC, [i], null, "NullCharError");
   1.242 +    } else if (uric.indexOf(String.fromCharCode(i)) >= 0) {
   1.243 +      wsp_decode_test(WSP.URIC, [i], String.fromCharCode(i));
   1.244 +    } else {
   1.245 +      wsp_decode_test(WSP.URIC, [i], null, "CodeError");
   1.246 +    }
   1.247 +  }
   1.248 +
   1.249 +  run_next_test();
   1.250 +});
   1.251 +
   1.252 +//
   1.253 +// Test target: TextString
   1.254 +//
   1.255 +
   1.256 +//// TextString.decode ////
   1.257 +
   1.258 +add_test(function test_TextString_decode() {
   1.259 +  // Test quoted string
   1.260 +  wsp_decode_test(WSP.TextString, [127, 128, 0], String.fromCharCode(128));
   1.261 +  // Test illegal quoted string
   1.262 +  wsp_decode_test(WSP.TextString, [127, 32, 0], null, "CodeError");
   1.263 +  // Test illegal unquoted string
   1.264 +  wsp_decode_test(WSP.TextString, [128, 0], null, "CodeError");
   1.265 +  // Test normal string
   1.266 +  wsp_decode_test(WSP.TextString, [32, 0], " ");
   1.267 +
   1.268 +  run_next_test();
   1.269 +});
   1.270 +
   1.271 +//// TextString.encode ////
   1.272 +
   1.273 +add_test(function test_TextString_encode() {
   1.274 +  // Test quoted string
   1.275 +  wsp_encode_test(WSP.TextString, String.fromCharCode(128), [127, 128, 0]);
   1.276 +  // Test normal string
   1.277 +  wsp_encode_test(WSP.TextString, "Mozilla", strToCharCodeArray("Mozilla"));
   1.278 +
   1.279 +  run_next_test();
   1.280 +});
   1.281 +
   1.282 +//
   1.283 +// Test target: TokenText
   1.284 +//
   1.285 +
   1.286 +//// TokenText.decode ////
   1.287 +
   1.288 +add_test(function test_TokenText_decode() {
   1.289 +  wsp_decode_test(WSP.TokenText, [65], null, "RangeError");
   1.290 +  wsp_decode_test(WSP.TokenText, [0], "");
   1.291 +  wsp_decode_test(WSP.TokenText, [65, 0], "A");
   1.292 +
   1.293 +  run_next_test();
   1.294 +});
   1.295 +
   1.296 +//// TokenText.encode ////
   1.297 +
   1.298 +add_test(function test_TokenText_encode() {
   1.299 +  wsp_encode_test(WSP.TokenText, "B2G", strToCharCodeArray("B2G"));
   1.300 +
   1.301 +  run_next_test();
   1.302 +});
   1.303 +
   1.304 +//
   1.305 +// Test target: QuotedString
   1.306 +//
   1.307 +
   1.308 +//// QuotedString.decode ////
   1.309 +
   1.310 +add_test(function test_QuotedString_decode() {
   1.311 +  // Test non-quoted string
   1.312 +  wsp_decode_test(WSP.QuotedString, [32, 0], null, "CodeError");
   1.313 +  // Test incompleted string
   1.314 +  wsp_decode_test(WSP.QuotedString, [34, 32], null, "RangeError");
   1.315 +  wsp_decode_test(WSP.QuotedString, [34, 32, 0], " ");
   1.316 +
   1.317 +  run_next_test();
   1.318 +});
   1.319 +
   1.320 +//// QuotedString.encode ////
   1.321 +
   1.322 +add_test(function test_QuotedString_encode() {
   1.323 +  wsp_encode_test(WSP.QuotedString, "B2G", [34].concat(strToCharCodeArray("B2G")));
   1.324 +
   1.325 +  run_next_test();
   1.326 +});
   1.327 +
   1.328 +//
   1.329 +// Test target: ShortInteger
   1.330 +//
   1.331 +
   1.332 +//// ShortInteger.decode ////
   1.333 +
   1.334 +add_test(function test_ShortInteger_decode() {
   1.335 +  for (let i = 0; i < 256; i++) {
   1.336 +    if (i & 0x80) {
   1.337 +      wsp_decode_test(WSP.ShortInteger, [i], i & 0x7F);
   1.338 +    } else {
   1.339 +      wsp_decode_test(WSP.ShortInteger, [i], null, "CodeError");
   1.340 +    }
   1.341 +  }
   1.342 +
   1.343 +  run_next_test();
   1.344 +});
   1.345 +
   1.346 +//// ShortInteger.encode ////
   1.347 +
   1.348 +add_test(function test_ShortInteger_encode() {
   1.349 +  for (let i = 0; i < 256; i++) {
   1.350 +    if (i & 0x80) {
   1.351 +      wsp_encode_test(WSP.ShortInteger, i, null, "CodeError");
   1.352 +    } else {
   1.353 +      wsp_encode_test(WSP.ShortInteger, i, [0x80 | i]);
   1.354 +    }
   1.355 +  }
   1.356 +
   1.357 +  run_next_test();
   1.358 +});
   1.359 +
   1.360 +//
   1.361 +// Test target: LongInteger
   1.362 +//
   1.363 +
   1.364 +//// LongInteger.decode ////
   1.365 +
   1.366 +function LongInteger_decode_testcases(target) {
   1.367 +  // Test LongInteger of zero octet
   1.368 +  wsp_decode_test(target, [0, 0], null, "CodeError");
   1.369 +  wsp_decode_test(target, [1, 0x80], 0x80);
   1.370 +  wsp_decode_test(target, [2, 0x80, 2], 0x8002);
   1.371 +  wsp_decode_test(target, [3, 0x80, 2, 3], 0x800203);
   1.372 +  wsp_decode_test(target, [4, 0x80, 2, 3, 4], 0x80020304);
   1.373 +  wsp_decode_test(target, [5, 0x80, 2, 3, 4, 5], 0x8002030405);
   1.374 +  wsp_decode_test(target, [6, 0x80, 2, 3, 4, 5, 6], 0x800203040506);
   1.375 +  // Test LongInteger of more than 6 octets
   1.376 +  wsp_decode_test(target, [7, 0x80, 2, 3, 4, 5, 6, 7], [0x80, 2, 3, 4, 5, 6, 7]);
   1.377 +  // Test LongInteger of more than 30 octets
   1.378 +  wsp_decode_test(target, [31], null, "CodeError");
   1.379 +}
   1.380 +add_test(function test_LongInteger_decode() {
   1.381 +  LongInteger_decode_testcases(WSP.LongInteger);
   1.382 +
   1.383 +  run_next_test();
   1.384 +});
   1.385 +
   1.386 +//// LongInteger.encode ////
   1.387 +
   1.388 +function LongInteger_encode_testcases(target) {
   1.389 +  wsp_encode_test(target, 0x80,           [1, 0x80]);
   1.390 +  wsp_encode_test(target, 0x8002,         [2, 0x80, 2]);
   1.391 +  wsp_encode_test(target, 0x800203,       [3, 0x80, 2, 3]);
   1.392 +  wsp_encode_test(target, 0x80020304,     [4, 0x80, 2, 3, 4]);
   1.393 +  wsp_encode_test(target, 0x8002030405,   [5, 0x80, 2, 3, 4, 5]);
   1.394 +  wsp_encode_test(target, 0x800203040506, [6, 0x80, 2, 3, 4, 5, 6]);
   1.395 +  // Test LongInteger of more than 6 octets
   1.396 +  wsp_encode_test(target, 0x1000000000000, null, "CodeError");
   1.397 +  // Test input empty array
   1.398 +  wsp_encode_test(target, [], null, "CodeError");
   1.399 +  // Test input octets array of length 1..30
   1.400 +  let array = [];
   1.401 +  for (let i = 1; i <= 30; i++) {
   1.402 +    array.push(i);
   1.403 +    wsp_encode_test(target, array, [i].concat(array));
   1.404 +  }
   1.405 +  // Test input octets array of 31 elements.
   1.406 +  array.push(31);
   1.407 +  wsp_encode_test(target, array, null, "CodeError");
   1.408 +}
   1.409 +add_test(function test_LongInteger_encode() {
   1.410 +  wsp_encode_test(WSP.LongInteger, 0, [1, 0]);
   1.411 +
   1.412 +  LongInteger_encode_testcases(WSP.LongInteger);
   1.413 +
   1.414 +  run_next_test();
   1.415 +});
   1.416 +
   1.417 +//
   1.418 +// Test target: UintVar
   1.419 +//
   1.420 +
   1.421 +//// UintVar.decode ////
   1.422 +
   1.423 +add_test(function test_UintVar_decode() {
   1.424 +  wsp_decode_test(WSP.UintVar, [0x80], null, "RangeError");
   1.425 +  // Test up to max 53 bits integer
   1.426 +  wsp_decode_test(WSP.UintVar, [0x7F], 0x7F);
   1.427 +  wsp_decode_test(WSP.UintVar, [0xFF, 0x7F], 0x3FFF);
   1.428 +  wsp_decode_test(WSP.UintVar, [0xFF, 0xFF, 0x7F], 0x1FFFFF);
   1.429 +  wsp_decode_test(WSP.UintVar, [0xFF, 0xFF, 0xFF, 0x7F], 0xFFFFFFF);
   1.430 +  wsp_decode_test(WSP.UintVar, [0xFF, 0xFF, 0xFF, 0xFF, 0x7F], 0x7FFFFFFFF);
   1.431 +  wsp_decode_test(WSP.UintVar, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F], 0x3FFFFFFFFFF);
   1.432 +  wsp_decode_test(WSP.UintVar, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F], 0x1FFFFFFFFFFFF);
   1.433 +  wsp_decode_test(WSP.UintVar, [0x8F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F], 0x1FFFFFFFFFFFFF);
   1.434 +  wsp_decode_test(WSP.UintVar, [0x01, 0x02], 1);
   1.435 +  wsp_decode_test(WSP.UintVar, [0x80, 0x01, 0x02], 1);
   1.436 +  wsp_decode_test(WSP.UintVar, [0x80, 0x80, 0x80, 0x01, 0x2], 1);
   1.437 +
   1.438 +  run_next_test();
   1.439 +});
   1.440 +
   1.441 +//// UintVar.encode ////
   1.442 +
   1.443 +add_test(function test_UintVar_encode() {
   1.444 +  // Test up to max 53 bits integer
   1.445 +  wsp_encode_test(WSP.UintVar, 0, [0]);
   1.446 +  wsp_encode_test(WSP.UintVar, 0x7F, [0x7F]);
   1.447 +  wsp_encode_test(WSP.UintVar, 0x3FFF, [0xFF, 0x7F]);
   1.448 +  wsp_encode_test(WSP.UintVar, 0x1FFFFF, [0xFF, 0xFF, 0x7F]);
   1.449 +  wsp_encode_test(WSP.UintVar, 0xFFFFFFF, [0xFF, 0xFF, 0xFF, 0x7F]);
   1.450 +  wsp_encode_test(WSP.UintVar, 0x7FFFFFFFF, [0xFF, 0xFF, 0xFF, 0xFF, 0x7F]);
   1.451 +  wsp_encode_test(WSP.UintVar, 0x3FFFFFFFFFF, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F]);
   1.452 +  wsp_encode_test(WSP.UintVar, 0x1FFFFFFFFFFFF, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F]);
   1.453 +  wsp_encode_test(WSP.UintVar, 0x1FFFFFFFFFFFFF, [0x8F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F]);
   1.454 +
   1.455 +  run_next_test();
   1.456 +});
   1.457 +
   1.458 +//
   1.459 +// Test target: ConstrainedEncoding
   1.460 +//
   1.461 +
   1.462 +//// ConstrainedEncoding.decode ////
   1.463 +
   1.464 +add_test(function test_ConstrainedEncoding_decode() {
   1.465 +  wsp_decode_test(WSP.ConstrainedEncoding, [0x80], 0);
   1.466 +  wsp_decode_test(WSP.ConstrainedEncoding, [32, 0], " ");
   1.467 +
   1.468 +  run_next_test();
   1.469 +});
   1.470 +
   1.471 +//// ConstrainedEncoding.encode ////
   1.472 +
   1.473 +add_test(function test_ConstrainedEncoding_encode() {
   1.474 +  wsp_encode_test(WSP.ConstrainedEncoding, 0, [0x80 | 0]);
   1.475 +  wsp_encode_test(WSP.ConstrainedEncoding, "A", [65, 0]);
   1.476 +
   1.477 +  run_next_test();
   1.478 +});
   1.479 +
   1.480 +//
   1.481 +// Test target: ValueLength
   1.482 +//
   1.483 +
   1.484 +//// ValueLength.decode ////
   1.485 +
   1.486 +add_test(function test_ValueLength_decode() {
   1.487 +  for (let i = 0; i < 256; i++) {
   1.488 +    if (i < 31) {
   1.489 +      wsp_decode_test(WSP.ValueLength, [i, 0x8F, 0x7F], i);
   1.490 +    } else if (i == 31) {
   1.491 +      wsp_decode_test(WSP.ValueLength, [i, 0x8F, 0x7F], 0x7FF);
   1.492 +    } else {
   1.493 +      wsp_decode_test(WSP.ValueLength, [i, 0x8F, 0x7F], null, "CodeError");
   1.494 +    }
   1.495 +  }
   1.496 +
   1.497 +  run_next_test();
   1.498 +});
   1.499 +
   1.500 +//// ValueLength.encode ////
   1.501 +
   1.502 +add_test(function test_ValueLength_encode() {
   1.503 +  for (let i = 0; i < 256; i++) {
   1.504 +    if (i < 31) {
   1.505 +      wsp_encode_test(WSP.ValueLength, i, [i]);
   1.506 +    } else if (i < 128) {
   1.507 +      wsp_encode_test(WSP.ValueLength, i, [31, i]);
   1.508 +    } else {
   1.509 +      wsp_encode_test(WSP.ValueLength, i, [31, (0x80 | (i / 128)), i % 128]);
   1.510 +    }
   1.511 +  }
   1.512 +
   1.513 +  run_next_test();
   1.514 +});
   1.515 +
   1.516 +//
   1.517 +// Test target: NoValue
   1.518 +//
   1.519 +
   1.520 +//// NoValue.decode ////
   1.521 +
   1.522 +add_test(function test_NoValue_decode() {
   1.523 +  wsp_decode_test(WSP.NoValue, [0], null);
   1.524 +  for (let i = 1; i < 256; i++) {
   1.525 +    wsp_decode_test(WSP.NoValue, [i], null, "CodeError");
   1.526 +  }
   1.527 +
   1.528 +  run_next_test();
   1.529 +});
   1.530 +
   1.531 +//// NoValue.encode ////
   1.532 +
   1.533 +add_test(function test_NoValue_encode() {
   1.534 +  wsp_encode_test(WSP.NoValue, undefined, [0]);
   1.535 +  wsp_encode_test(WSP.NoValue, null, [0]);
   1.536 +  wsp_encode_test(WSP.NoValue, 0, null, "CodeError");
   1.537 +  wsp_encode_test(WSP.NoValue, "", null, "CodeError");
   1.538 +  wsp_encode_test(WSP.NoValue, [], null, "CodeError");
   1.539 +  wsp_encode_test(WSP.NoValue, {}, null, "CodeError");
   1.540 +
   1.541 +  run_next_test();
   1.542 +});
   1.543 +
   1.544 +//
   1.545 +// Test target: TextValue
   1.546 +//
   1.547 +
   1.548 +//// TextValue.decode ////
   1.549 +
   1.550 +add_test(function test_TextValue_decode() {
   1.551 +  wsp_decode_test(WSP.TextValue, [0], null);
   1.552 +  wsp_decode_test(WSP.TextValue, [65, 0], "A");
   1.553 +  wsp_decode_test(WSP.TextValue, [32, 0], null, "CodeError");
   1.554 +  wsp_decode_test(WSP.TextValue, [34, 32, 0], " ");
   1.555 +
   1.556 +  run_next_test();
   1.557 +});
   1.558 +
   1.559 +//// TextValue.encode ////
   1.560 +
   1.561 +add_test(function test_TextValue_encode() {
   1.562 +  wsp_encode_test(WSP.TextValue, undefined, [0]);
   1.563 +  wsp_encode_test(WSP.TextValue, null, [0]);
   1.564 +  wsp_encode_test(WSP.TextValue, "", [0]);
   1.565 +  wsp_encode_test(WSP.TextValue, "A", [65, 0]);
   1.566 +  wsp_encode_test(WSP.TextValue, "\x80", [34, 128, 0]);
   1.567 +
   1.568 +  run_next_test();
   1.569 +});
   1.570 +
   1.571 +//
   1.572 +// Test target: IntegerValue
   1.573 +//
   1.574 +
   1.575 +//// IntegerValue.decode ////
   1.576 +
   1.577 +add_test(function test_IntegerValue_decode() {
   1.578 +  for (let i = 128; i < 256; i++) {
   1.579 +    wsp_decode_test(WSP.IntegerValue, [i], i & 0x7F);
   1.580 +  }
   1.581 +
   1.582 +  LongInteger_decode_testcases(WSP.IntegerValue);
   1.583 +
   1.584 +  run_next_test();
   1.585 +});
   1.586 +
   1.587 +//// IntegerValue.decode ////
   1.588 +
   1.589 +add_test(function test_IntegerValue_encode() {
   1.590 +  for (let i = 0; i < 128; i++) {
   1.591 +    wsp_encode_test(WSP.IntegerValue, i, [0x80 | i]);
   1.592 +  }
   1.593 +
   1.594 +  LongInteger_encode_testcases(WSP.IntegerValue);
   1.595 +
   1.596 +  run_next_test();
   1.597 +});
   1.598 +
   1.599 +//
   1.600 +// Test target: DateValue
   1.601 +//
   1.602 +
   1.603 +//// DateValue.decode ////
   1.604 +
   1.605 +add_test(function test_DateValue_decode() {
   1.606 +  wsp_decode_test(WSP.DateValue, [0, 0], null, "CodeError");
   1.607 +  wsp_decode_test(WSP.DateValue, [1, 0x80], new Date(0x80 * 1000));
   1.608 +  wsp_decode_test(WSP.DateValue, [31], null, "CodeError");
   1.609 +
   1.610 +  run_next_test();
   1.611 +});
   1.612 +
   1.613 +//// DateValue.encode ////
   1.614 +
   1.615 +add_test(function test_DateValue_encode() {
   1.616 +  wsp_encode_test(WSP.DateValue, new Date(0x80 * 1000), [1, 0x80]);
   1.617 +
   1.618 +  run_next_test();
   1.619 +});
   1.620 +
   1.621 +//
   1.622 +// Test target: DeltaSecondsValue
   1.623 +//
   1.624 +// DeltaSecondsValue is only an alias of IntegerValue.
   1.625 +
   1.626 +//
   1.627 +// Test target: QValue
   1.628 +//
   1.629 +
   1.630 +//// QValue.decode ////
   1.631 +
   1.632 +add_test(function test_QValue_decode() {
   1.633 +  wsp_decode_test(WSP.QValue, [0], null, "CodeError");
   1.634 +  wsp_decode_test(WSP.QValue, [1], 0);
   1.635 +  wsp_decode_test(WSP.QValue, [100], 0.99);
   1.636 +  wsp_decode_test(WSP.QValue, [101], 0.001);
   1.637 +  wsp_decode_test(WSP.QValue, [0x88, 0x4B], 0.999);
   1.638 +  wsp_decode_test(WSP.QValue, [0x88, 0x4C], null, "CodeError");
   1.639 +
   1.640 +  run_next_test();
   1.641 +});
   1.642 +
   1.643 +//// QValue.encode ////
   1.644 +
   1.645 +add_test(function test_QValue_encode() {
   1.646 +  wsp_encode_test(WSP.QValue, 0, [1]);
   1.647 +  wsp_encode_test(WSP.QValue, 0.99, [100]);
   1.648 +  wsp_encode_test(WSP.QValue, 0.001, [101]);
   1.649 +  wsp_encode_test(WSP.QValue, 0.999, [0x88, 0x4B]);
   1.650 +  wsp_encode_test(WSP.QValue, 1, null, "CodeError");
   1.651 +
   1.652 +  run_next_test();
   1.653 +});
   1.654 +
   1.655 +//
   1.656 +// Test target: VersionValue
   1.657 +//
   1.658 +
   1.659 +//// VersionValue.decode ////
   1.660 +
   1.661 +add_test(function test_VersionValue_decode() {
   1.662 +  for (let major = 1; major < 8; major++) {
   1.663 +    let version = (major << 4) | 0x0F;
   1.664 +    wsp_decode_test(WSP.VersionValue, [0x80 | version], version);
   1.665 +    wsp_decode_test(WSP.VersionValue, [major + 0x30, 0], version);
   1.666 +
   1.667 +    for (let minor = 0; minor < 15; minor++) {
   1.668 +      version = (major << 4) | minor;
   1.669 +      wsp_decode_test(WSP.VersionValue, [0x80 | version], version);
   1.670 +      if (minor >= 10) {
   1.671 +        wsp_decode_test(WSP.VersionValue, [major + 0x30, 0x2E, 0x31, (minor - 10) + 0x30, 0], version);
   1.672 +      } else {
   1.673 +        wsp_decode_test(WSP.VersionValue, [major + 0x30, 0x2E, minor + 0x30, 0], version);
   1.674 +      }
   1.675 +    }
   1.676 +  }
   1.677 +
   1.678 +  run_next_test();
   1.679 +});
   1.680 +
   1.681 +//// VersionValue.encode ////
   1.682 +
   1.683 +add_test(function test_VersionValue_encode() {
   1.684 +  for (let major = 1; major < 8; major++) {
   1.685 +    let version = (major << 4) | 0x0F;
   1.686 +    wsp_encode_test(WSP.VersionValue, version, [0x80 | version]);
   1.687 +
   1.688 +    for (let minor = 0; minor < 15; minor++) {
   1.689 +      version = (major << 4) | minor;
   1.690 +      wsp_encode_test(WSP.VersionValue, version, [0x80 | version]);
   1.691 +    }
   1.692 +  }
   1.693 +
   1.694 +  run_next_test();
   1.695 +});
   1.696 +
   1.697 +//
   1.698 +// Test target: UriValue
   1.699 +//
   1.700 +
   1.701 +//// UriValue.decode ////
   1.702 +
   1.703 +add_test(function test_UriValue_decode() {
   1.704 +  wsp_decode_test(WSP.UriValue, [97], null, "RangeError");
   1.705 +  wsp_decode_test(WSP.UriValue, [0], "");
   1.706 +  wsp_decode_test(WSP.UriValue, [65, 0], "A");
   1.707 +
   1.708 +  run_next_test();
   1.709 +});
   1.710 +
   1.711 +//
   1.712 +// Test target: TypeValue
   1.713 +//
   1.714 +
   1.715 +//// TypeValue.decode ////
   1.716 +
   1.717 +add_test(function test_TypeValue_decode() {
   1.718 +  // Test for string-typed return value from ConstrainedEncoding
   1.719 +  wsp_decode_test(WSP.TypeValue, [65, 0], "a");
   1.720 +  // Test for number-typed return value from ConstrainedEncoding
   1.721 +  wsp_decode_test(WSP.TypeValue, [0x33 | 0x80],
   1.722 +                  "application/vnd.wap.multipart.related");
   1.723 +  wsp_decode_test(WSP.TypeValue, [0x1d | 0x80], "image/gif");
   1.724 +  // Test for NotWellKnownEncodingError
   1.725 +  wsp_decode_test(WSP.TypeValue, [0x59 | 0x80], null, "NotWellKnownEncodingError");
   1.726 +
   1.727 +  run_next_test();
   1.728 +});
   1.729 +
   1.730 +//// TypeValue.encode ////
   1.731 +
   1.732 +add_test(function test_TypeValue_encode() {
   1.733 +  wsp_encode_test(WSP.TypeValue, "no/such.type",
   1.734 +                  [110, 111, 47, 115, 117, 99, 104, 46, 116, 121, 112, 101, 0]);
   1.735 +  wsp_encode_test(WSP.TypeValue, "application/vnd.wap.multipart.related",
   1.736 +                  [0x33 | 0x80]);
   1.737 +  wsp_encode_test(WSP.TypeValue, "image/gif",
   1.738 +                  [0x1d | 0x80]);
   1.739 +
   1.740 +  run_next_test();
   1.741 +});
   1.742 +
   1.743 +//
   1.744 +// Test target: Parameter
   1.745 +//
   1.746 +
   1.747 +//// Parameter.decodeTypedParameter ////
   1.748 +
   1.749 +add_test(function test_Parameter_decodeTypedParameter() {
   1.750 +  function func(data) {
   1.751 +    return WSP.Parameter.decodeTypedParameter(data);
   1.752 +  }
   1.753 +
   1.754 +  // Test for array-typed return value from IntegerValue
   1.755 +  wsp_decode_test_ex(func, [7, 0, 0, 0, 0, 0, 0, 0], null, "CodeError");
   1.756 +  // Test for number-typed return value from IntegerValue
   1.757 +  wsp_decode_test_ex(func, [1, 0, 0], {name: "q", value: null});
   1.758 +  // Test for NotWellKnownEncodingError
   1.759 +  wsp_decode_test_ex(func, [1, 0xFF], null, "NotWellKnownEncodingError");
   1.760 +  // Test for parameter specific decoder
   1.761 +  wsp_decode_test_ex(func, [1, 0, 100], {name: "q", value: 0.99});
   1.762 +  // Test for TextValue
   1.763 +  wsp_decode_test_ex(func, [1, 0x10, 48, 46, 57, 57, 0],
   1.764 +                     {name: "secure", value: "0.99"});
   1.765 +  // Test for TextString
   1.766 +  wsp_decode_test_ex(func, [1, 0x0A, 60, 115, 109, 105, 108, 62, 0],
   1.767 +                     {name: "start", value: "<smil>"});
   1.768 +  // Test for skipValue
   1.769 +  wsp_decode_test_ex(func, [1, 0x0A, 128], null);
   1.770 +
   1.771 +  run_next_test();
   1.772 +});
   1.773 +
   1.774 +//// Parameter.decodeUntypedParameter ////
   1.775 +
   1.776 +add_test(function test_Parameter_decodeUntypedParameter() {
   1.777 +  function func (data) {
   1.778 +    return WSP.Parameter.decodeUntypedParameter(data);
   1.779 +  }
   1.780 +
   1.781 +  wsp_decode_test_ex(func, [1], null, "CodeError");
   1.782 +  wsp_decode_test_ex(func, [65, 0, 0], {name: "a", value: null});
   1.783 +  // Test for IntegerValue
   1.784 +  wsp_decode_test_ex(func, [65, 0, 1, 0], {name: "a", value: 0});
   1.785 +  // Test for TextValue
   1.786 +  wsp_decode_test_ex(func, [65, 0, 66, 0], {name: "a", value: "B"});
   1.787 +
   1.788 +  run_next_test();
   1.789 +});
   1.790 +
   1.791 +//// Parameter.decode ////
   1.792 +
   1.793 +add_test(function test_Parameter_decode() {
   1.794 +  wsp_decode_test(WSP.Parameter, [1, 0x0A, 60, 115, 109, 105, 108, 62, 0],
   1.795 +                  {name: "start", value: "<smil>"});
   1.796 +  wsp_decode_test(WSP.Parameter, [65, 0, 66, 0], {name: "a", value: "B"});
   1.797 +
   1.798 +  run_next_test();
   1.799 +});
   1.800 +
   1.801 +//// Parameter.decodeMultiple ////
   1.802 +
   1.803 +add_test(function test_Parameter_decodeMultiple() {
   1.804 +  wsp_decode_test_ex(function(data) {
   1.805 +      return WSP.Parameter.decodeMultiple(data, 13);
   1.806 +    }, [1, 0x0A, 60, 115, 109, 105, 108, 62, 0, 65, 0, 66, 0], {start: "<smil>", a: "B"}
   1.807 +  );
   1.808 +
   1.809 +  run_next_test();
   1.810 +});
   1.811 +
   1.812 +//// Parameter.encodeTypedParameter ////
   1.813 +
   1.814 +add_test(function test_Parameter_encodeTypedParameter() {
   1.815 +  function func(data, input) {
   1.816 +    WSP.Parameter.encodeTypedParameter(data, input);
   1.817 +    return data.array;
   1.818 +  }
   1.819 +
   1.820 +  // Test for NotWellKnownEncodingError
   1.821 +  wsp_encode_test_ex(func, {name: "xxx", value: 0}, null, "NotWellKnownEncodingError");
   1.822 +  wsp_encode_test_ex(func, {name: "q", value: 0}, [0x80, 1]);
   1.823 +  wsp_encode_test_ex(func, {name: "name", value: "A"}, [0x85, 65, 0]);
   1.824 +
   1.825 +  run_next_test();
   1.826 +});
   1.827 +
   1.828 +//// Parameter.encodeUntypedParameter ////
   1.829 +
   1.830 +add_test(function test_Parameter_encodeUntypedParameter() {
   1.831 +  function func(data, input) {
   1.832 +    WSP.Parameter.encodeUntypedParameter(data, input);
   1.833 +    return data.array;
   1.834 +  }
   1.835 +
   1.836 +  wsp_encode_test_ex(func, {name: "q", value: 0}, [113, 0, 0x80]);
   1.837 +  wsp_encode_test_ex(func, {name: "name", value: "A"}, [110, 97, 109, 101, 0, 65, 0]);
   1.838 +
   1.839 +  run_next_test();
   1.840 +});
   1.841 +
   1.842 +//// Parameter.encodeMultiple ////
   1.843 +
   1.844 +add_test(function test_Parameter_encodeMultiple() {
   1.845 +  function func(data, input) {
   1.846 +    WSP.Parameter.encodeMultiple(data, input);
   1.847 +    return data.array;
   1.848 +  }
   1.849 +
   1.850 +  wsp_encode_test_ex(func, {q: 0, n: "A"}, [0x80, 1, 110, 0, 65, 0]);
   1.851 +
   1.852 +  run_next_test();
   1.853 +});
   1.854 +
   1.855 +//// Parameter.encode ////
   1.856 +
   1.857 +add_test(function test_Parameter_encode() {
   1.858 +
   1.859 +  wsp_encode_test(WSP.Parameter, {name: "q", value: 0}, [0x80, 1]);
   1.860 +  wsp_encode_test(WSP.Parameter, {name: "n", value: "A"}, [110, 0, 65, 0]);
   1.861 +
   1.862 +  run_next_test();
   1.863 +});
   1.864 +
   1.865 +//
   1.866 +// Test target: Header
   1.867 +//
   1.868 +
   1.869 +//// Header.decode ////
   1.870 +
   1.871 +add_test(function test_Header_decode() {
   1.872 +  wsp_decode_test(WSP.Header, [0x34 | 0x80, 0x80], {name: "push-flag", value: 0});
   1.873 +  wsp_decode_test(WSP.Header, [65, 0, 66, 0], {name: "a", value: "B"});
   1.874 +
   1.875 +  run_next_test();
   1.876 +});
   1.877 +
   1.878 +//
   1.879 +// Test target: WellKnownHeader
   1.880 +//
   1.881 +
   1.882 +//// WellKnownHeader.decode ////
   1.883 +
   1.884 +add_test(function test_WellKnownHeader_decode() {
   1.885 +  wsp_decode_test(WSP.WellKnownHeader, [0xFF], null, "NotWellKnownEncodingError");
   1.886 +  let (entry = WSP.WSP_HEADER_FIELDS["push-flag"]) {
   1.887 +    // Test for Short-Integer
   1.888 +    wsp_decode_test(WSP.WellKnownHeader, [entry.number | 0x80, 0x80],
   1.889 +                        {name: entry.name, value: 0});
   1.890 +    // Test for NoValue
   1.891 +    wsp_decode_test(WSP.WellKnownHeader, [entry.number | 0x80, 0],
   1.892 +                        {name: entry.name, value: null});
   1.893 +    // Test for TokenText
   1.894 +    wsp_decode_test(WSP.WellKnownHeader, [entry.number | 0x80, 65, 0],
   1.895 +                        {name: entry.name, value: "A"});
   1.896 +    // Test for QuotedString
   1.897 +    wsp_decode_test(WSP.WellKnownHeader, [entry.number | 0x80, 34, 128, 0],
   1.898 +                        {name: entry.name, value: String.fromCharCode(128)});
   1.899 +    // Test for skipValue
   1.900 +    wsp_decode_test(WSP.WellKnownHeader, [entry.number | 0x80, 2, 0, 0], null);
   1.901 +  }
   1.902 +
   1.903 +  run_next_test();
   1.904 +});
   1.905 +
   1.906 +//
   1.907 +// Test target: ApplicationHeader
   1.908 +//
   1.909 +
   1.910 +//// ApplicationHeader.decode ////
   1.911 +
   1.912 +add_test(function test_ApplicationHeader_decode() {
   1.913 +  wsp_decode_test(WSP.ApplicationHeader, [5, 0, 66, 0], null, "CodeError");
   1.914 +  wsp_decode_test(WSP.ApplicationHeader, [65, 0, 66, 0], {name: "a", value: "B"});
   1.915 +  // Test for skipValue
   1.916 +  wsp_decode_test(WSP.ApplicationHeader, [65, 0, 2, 0, 0], null);
   1.917 +
   1.918 +  run_next_test();
   1.919 +});
   1.920 +
   1.921 +//// ApplicationHeader.encode ////
   1.922 +
   1.923 +add_test(function test_ApplicationHeader_encode() {
   1.924 +  // Test invalid header name string:
   1.925 +  wsp_encode_test(WSP.ApplicationHeader, {name: undefined, value: "asdf"}, null, "CodeError");
   1.926 +  wsp_encode_test(WSP.ApplicationHeader, {name: null, value: "asdf"}, null, "CodeError");
   1.927 +  wsp_encode_test(WSP.ApplicationHeader, {name: "", value: "asdf"}, null, "CodeError");
   1.928 +  wsp_encode_test(WSP.ApplicationHeader, {name: "a b", value: "asdf"}, null, "CodeError");
   1.929 +  // Test value string:
   1.930 +  wsp_encode_test(WSP.ApplicationHeader, {name: "asdf", value: undefined},
   1.931 +                  strToCharCodeArray("asdf").concat([0]));
   1.932 +  wsp_encode_test(WSP.ApplicationHeader, {name: "asdf", value: null},
   1.933 +                  strToCharCodeArray("asdf").concat([0]));
   1.934 +  wsp_encode_test(WSP.ApplicationHeader, {name: "asdf", value: ""},
   1.935 +                  strToCharCodeArray("asdf").concat([0]));
   1.936 +  wsp_encode_test(WSP.ApplicationHeader, {name: "asdf", value: "fdsa"},
   1.937 +                  strToCharCodeArray("asdf").concat(strToCharCodeArray("fdsa")));
   1.938 +
   1.939 +  run_next_test();
   1.940 +});
   1.941 +
   1.942 +//
   1.943 +// Test target: FieldName
   1.944 +//
   1.945 +
   1.946 +//// FieldName.decode ////
   1.947 +
   1.948 +add_test(function test_FieldName_decode() {
   1.949 +  wsp_decode_test(WSP.FieldName, [0], "");
   1.950 +  wsp_decode_test(WSP.FieldName, [65, 0], "a");
   1.951 +  wsp_decode_test(WSP.FieldName, [97, 0], "a");
   1.952 +  let (entry = WSP.WSP_HEADER_FIELDS["content-length"]) {
   1.953 +    wsp_decode_test(WSP.FieldName, [entry.number | 0x80], entry.name);
   1.954 +  }
   1.955 +  wsp_decode_test(WSP.FieldName, [0xFF], null, "NotWellKnownEncodingError");
   1.956 +
   1.957 +  run_next_test();
   1.958 +});
   1.959 +
   1.960 +//// FieldName.encode ////
   1.961 +
   1.962 +add_test(function test_FieldName_encode() {
   1.963 +  wsp_encode_test(WSP.FieldName, "", [0]);
   1.964 +  wsp_encode_test(WSP.FieldName, "date", [0x92]);
   1.965 +
   1.966 +  run_next_test();
   1.967 +});
   1.968 +
   1.969 +//
   1.970 +// Test target: AcceptCharsetValue
   1.971 +//
   1.972 +
   1.973 +//// AcceptCharsetValue.decode ////
   1.974 +
   1.975 +add_test(function test_AcceptCharsetValue_decode() {
   1.976 +  wsp_decode_test(WSP.AcceptCharsetValue, [0xFF], null, "CodeError");
   1.977 +  // Test for Any-Charset
   1.978 +  wsp_decode_test(WSP.AcceptCharsetValue, [128], {charset: "*"});
   1.979 +  // Test for Constrained-Charset
   1.980 +  wsp_decode_test(WSP.AcceptCharsetValue, [65, 0], {charset: "A"});
   1.981 +  let (entry = WSP.WSP_WELL_KNOWN_CHARSETS["utf-8"]) {
   1.982 +    wsp_decode_test(WSP.AcceptCharsetValue, [entry.number | 0x80], {charset: entry.name});
   1.983 +  }
   1.984 +  // Test for Accept-Charset-General-Form
   1.985 +  wsp_decode_test(WSP.AcceptCharsetValue, [1, 128], {charset: "*"});
   1.986 +  let (entry = WSP.WSP_WELL_KNOWN_CHARSETS["utf-8"]) {
   1.987 +    wsp_decode_test(WSP.AcceptCharsetValue, [2, 1, entry.number], {charset: entry.name});
   1.988 +    wsp_decode_test(WSP.AcceptCharsetValue, [1, entry.number | 0x80], {charset: entry.name});
   1.989 +  }
   1.990 +  wsp_decode_test(WSP.AcceptCharsetValue, [3, 65, 0, 100], {charset: "A", q: 0.99});
   1.991 +
   1.992 +  run_next_test();
   1.993 +});
   1.994 +
   1.995 +//// AcceptCharsetValue.encodeAnyCharset ////
   1.996 +
   1.997 +add_test(function test_AcceptCharsetValue_encodeAnyCharset() {
   1.998 +  function func(data, input) {
   1.999 +    WSP.AcceptCharsetValue.encodeAnyCharset(data, input);
  1.1000 +    return data.array;
  1.1001 +  }
  1.1002 +
  1.1003 +  wsp_encode_test_ex(func, null, [0x80]);
  1.1004 +  wsp_encode_test_ex(func, undefined, [0x80]);
  1.1005 +  wsp_encode_test_ex(func, {}, [0x80]);
  1.1006 +  wsp_encode_test_ex(func, {charset: null}, [0x80]);
  1.1007 +  wsp_encode_test_ex(func, {charset: "*"}, [0x80]);
  1.1008 +  wsp_encode_test_ex(func, {charset: "en"}, null, "CodeError");
  1.1009 +
  1.1010 +  run_next_test();
  1.1011 +});
  1.1012 +
  1.1013 +//
  1.1014 +// Test target: WellKnownCharset
  1.1015 +//
  1.1016 +
  1.1017 +//// WellKnownCharset.decode ////
  1.1018 +
  1.1019 +add_test(function test_WellKnownCharset_decode() {
  1.1020 +  wsp_decode_test(WSP.WellKnownCharset, [0xFF], null, "NotWellKnownEncodingError");
  1.1021 +  // Test for Any-Charset
  1.1022 +  wsp_decode_test(WSP.WellKnownCharset, [128], {charset: "*"});
  1.1023 +  // Test for number-typed return value from IntegerValue
  1.1024 +  wsp_decode_test(WSP.WellKnownCharset, [1, 3], {charset: "us-ascii"});
  1.1025 +  wsp_decode_test(WSP.WellKnownCharset, [1, 4], {charset: "iso-8859-1"});
  1.1026 +  wsp_decode_test(WSP.WellKnownCharset, [1, 5], {charset: "iso-8859-2"});
  1.1027 +  wsp_decode_test(WSP.WellKnownCharset, [1, 6], {charset: "iso-8859-3"});
  1.1028 +  wsp_decode_test(WSP.WellKnownCharset, [1, 7], {charset: "iso-8859-4"});
  1.1029 +  wsp_decode_test(WSP.WellKnownCharset, [1, 8], {charset: "iso-8859-5"});
  1.1030 +  wsp_decode_test(WSP.WellKnownCharset, [1, 9], {charset: "iso-8859-6"});
  1.1031 +  wsp_decode_test(WSP.WellKnownCharset, [1, 10], {charset: "iso-8859-7"});
  1.1032 +  wsp_decode_test(WSP.WellKnownCharset, [1, 11], {charset: "iso-8859-8"});
  1.1033 +  wsp_decode_test(WSP.WellKnownCharset, [1, 12], {charset: "iso-8859-9"});
  1.1034 +  wsp_decode_test(WSP.WellKnownCharset, [1, 13], {charset: "iso-8859-10"});
  1.1035 +  wsp_decode_test(WSP.WellKnownCharset, [1, 17], {charset: "shift_jis"});
  1.1036 +  wsp_decode_test(WSP.WellKnownCharset, [1, 18], {charset: "euc-jp"});
  1.1037 +  wsp_decode_test(WSP.WellKnownCharset, [1, 37], {charset: "iso-2022-kr"});
  1.1038 +  wsp_decode_test(WSP.WellKnownCharset, [1, 38], {charset: "euc-kr"});
  1.1039 +  wsp_decode_test(WSP.WellKnownCharset, [1, 39], {charset: "iso-2022-jp"});
  1.1040 +  wsp_decode_test(WSP.WellKnownCharset, [1, 40], {charset: "iso-2022-jp-2"});
  1.1041 +  wsp_decode_test(WSP.WellKnownCharset, [1, 81], {charset: "iso-8859-6-e"});
  1.1042 +  wsp_decode_test(WSP.WellKnownCharset, [1, 82], {charset: "iso-8859-6-i"});
  1.1043 +  wsp_decode_test(WSP.WellKnownCharset, [1, 84], {charset: "iso-8859-8-e"});
  1.1044 +  wsp_decode_test(WSP.WellKnownCharset, [1, 85], {charset: "iso-8859-8-i"});
  1.1045 +  wsp_decode_test(WSP.WellKnownCharset, [1, 1000], {charset: "iso-10646-ucs-2"});
  1.1046 +  wsp_decode_test(WSP.WellKnownCharset, [1, 1015], {charset: "utf-16"});
  1.1047 +  wsp_decode_test(WSP.WellKnownCharset, [1, 2025], {charset: "gb2312"});
  1.1048 +  wsp_decode_test(WSP.WellKnownCharset, [1, 2026], {charset: "big5"});
  1.1049 +  wsp_decode_test(WSP.WellKnownCharset, [1, 2084], {charset: "koi8-r"});
  1.1050 +  wsp_decode_test(WSP.WellKnownCharset, [1, 2252], {charset: "windows-1252"});
  1.1051 +  wsp_decode_test(WSP.WellKnownCharset, [2, 3, 247], {charset: "utf-16"});
  1.1052 +  // Test for array-typed return value from IntegerValue
  1.1053 +  wsp_decode_test(WSP.WellKnownCharset, [7, 0, 0, 0, 0, 0, 0, 0, 3], null, "CodeError");
  1.1054 +
  1.1055 +  run_next_test();
  1.1056 +});
  1.1057 +
  1.1058 +//// WellKnownCharset.encode ////
  1.1059 +
  1.1060 +add_test(function test_WellKnownCharset_encode() {
  1.1061 +  // Test for Any-charset
  1.1062 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "*"}, [0x80]);
  1.1063 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "us-ascii"}, [128 + 3]);
  1.1064 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-1"}, [128 + 4]);
  1.1065 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-2"}, [128 + 5]);
  1.1066 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-3"}, [128 + 6]);
  1.1067 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-4"}, [128 + 7]);
  1.1068 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-5"}, [128 + 8]);
  1.1069 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-6"}, [128 + 9]);
  1.1070 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-7"}, [128 + 10]);
  1.1071 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-8"}, [128 + 11]);
  1.1072 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-9"}, [128 + 12]);
  1.1073 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-10"}, [128 + 13]);
  1.1074 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "shift_jis"}, [128 + 17]);
  1.1075 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "euc-jp"}, [128 + 18]);
  1.1076 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-2022-kr"}, [128 + 37]);
  1.1077 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "euc-kr"}, [128 + 38]);
  1.1078 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-2022-jp"}, [128 + 39]);
  1.1079 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-2022-jp-2"}, [128 + 40]);
  1.1080 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-6-e"}, [128 + 81]);
  1.1081 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-6-i"}, [128 + 82]);
  1.1082 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-8-e"}, [128 + 84]);
  1.1083 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-8-i"}, [128 + 85]);
  1.1084 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "UTF-8"}, [128 + 106]);
  1.1085 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-10646-ucs-2"}, [2, 0x3, 0xe8]);
  1.1086 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "UTF-16"}, [2, 0x3, 0xf7]);
  1.1087 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "gb2312"}, [2, 0x7, 0xe9]);
  1.1088 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "big5"}, [2, 0x7, 0xea]);
  1.1089 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "koi8-r"}, [2, 0x8, 0x24]);
  1.1090 +  wsp_encode_test(WSP.WellKnownCharset, {charset: "windows-1252"}, [2, 0x8, 0xcc]);
  1.1091 +
  1.1092 +  run_next_test();
  1.1093 +});
  1.1094 +
  1.1095 +//
  1.1096 +// Test target: ContentTypeValue
  1.1097 +//
  1.1098 +
  1.1099 +//// ContentTypeValue.decodeConstrainedMedia ////
  1.1100 +
  1.1101 +add_test(function test_ContentTypeValue_decodeConstrainedMedia() {
  1.1102 +  function func(data) {
  1.1103 +    return WSP.ContentTypeValue.decodeConstrainedMedia(data);
  1.1104 +  }
  1.1105 +
  1.1106 +  // Test for string-typed return value from ConstrainedEncoding
  1.1107 +  wsp_decode_test_ex(func, [65, 0], {media: "a", params: null});
  1.1108 +  // Test for number-typed return value from ConstrainedEncoding
  1.1109 +  for(let ix = 0; ix <WSP.WSP_WELL_KNOWN_CONTENT_TYPES.length ; ++ix){
  1.1110 +    wsp_decode_test_ex(func, [WSP.WSP_WELL_KNOWN_CONTENT_TYPES[ix].number | 0x80],
  1.1111 +      {media: WSP.WSP_WELL_KNOWN_CONTENT_TYPES[ix].value, params: null});
  1.1112 +  }
  1.1113 +  // Test for NotWellKnownEncodingError
  1.1114 +  wsp_decode_test_ex(func, [0x59 | 0x80], null, "NotWellKnownEncodingError");
  1.1115 +
  1.1116 +  run_next_test();
  1.1117 +});
  1.1118 +
  1.1119 +//// ContentTypeValue.decodeMedia ////
  1.1120 +
  1.1121 +add_test(function test_ContentTypeValue_decodeMedia() {
  1.1122 +  function func(data) {
  1.1123 +    return WSP.ContentTypeValue.decodeMedia(data);
  1.1124 +  }
  1.1125 +
  1.1126 +  // Test for NullTerminatedTexts
  1.1127 +  wsp_decode_test_ex(func, [65, 0], "a");
  1.1128 +  // Test for IntegerValue
  1.1129 +  wsp_decode_test_ex(func, [0x3E | 0x80], "application/vnd.wap.mms-message");
  1.1130 +  wsp_decode_test_ex(func, [0x59 | 0x80], null, "NotWellKnownEncodingError");
  1.1131 +
  1.1132 +  run_next_test();
  1.1133 +});
  1.1134 +
  1.1135 +//// ContentTypeValue.decodeMediaType ////
  1.1136 +
  1.1137 +add_test(function test_ContentTypeValue_decodeMediaType() {
  1.1138 +  wsp_decode_test_ex(function(data) {
  1.1139 +      return WSP.ContentTypeValue.decodeMediaType(data, 1);
  1.1140 +    }, [0x3E | 0x80],
  1.1141 +    {media: "application/vnd.wap.mms-message", params: null}
  1.1142 +  );
  1.1143 +  wsp_decode_test_ex(function(data) {
  1.1144 +      return WSP.ContentTypeValue.decodeMediaType(data, 14);
  1.1145 +    }, [0x3E | 0x80, 1, 0x0A, 60, 115, 109, 105, 108, 62, 0, 65, 0, 66, 0],
  1.1146 +    {media: "application/vnd.wap.mms-message", params: {start: "<smil>", a: "B"}}
  1.1147 +  );
  1.1148 +
  1.1149 +  run_next_test();
  1.1150 +});
  1.1151 +
  1.1152 +//// ContentTypeValue.decodeContentGeneralForm ////
  1.1153 +
  1.1154 +add_test(function test_ContentTypeValue_decodeContentGeneralForm() {
  1.1155 +  wsp_decode_test_ex(function(data) {
  1.1156 +      return WSP.ContentTypeValue.decodeContentGeneralForm(data);
  1.1157 +    }, [14, 0x3E | 0x80, 1, 0x0A, 60, 115, 109, 105, 108, 62, 0, 65, 0, 66, 0],
  1.1158 +    {media: "application/vnd.wap.mms-message", params: {start: "<smil>", a: "B"}}
  1.1159 +  );
  1.1160 +
  1.1161 +  run_next_test();
  1.1162 +});
  1.1163 +
  1.1164 +//// ContentTypeValue.decode ////
  1.1165 +
  1.1166 +add_test(function test_ContentTypeValue_decode() {
  1.1167 +  wsp_decode_test(WSP.ContentTypeValue,
  1.1168 +    [14, 0x3E | 0x80, 1, 0x0A, 60, 115, 109, 105, 108, 62, 0, 65, 0, 66, 0],
  1.1169 +    {media: "application/vnd.wap.mms-message", params: {start: "<smil>", a: "B"}}
  1.1170 +  );
  1.1171 +
  1.1172 +  wsp_decode_test(WSP.ContentTypeValue, [0x33 | 0x80],
  1.1173 +    {media: "application/vnd.wap.multipart.related", params: null}
  1.1174 +  );
  1.1175 +
  1.1176 +  run_next_test();
  1.1177 +});
  1.1178 +
  1.1179 +//// ContentTypeValue.encodeConstrainedMedia ////
  1.1180 +
  1.1181 +add_test(function test_ContentTypeValue_encodeConstrainedMedia() {
  1.1182 +  function func(data, input) {
  1.1183 +    WSP.ContentTypeValue.encodeConstrainedMedia(data, input);
  1.1184 +    return data.array;
  1.1185 +  }
  1.1186 +
  1.1187 +  // Test media type with additional parameters.
  1.1188 +  wsp_encode_test_ex(func, {media: "a", params: [{a: "b"}]}, null, "CodeError");
  1.1189 +  wsp_encode_test_ex(func, {media: "no/such.type"},
  1.1190 +                     [110, 111, 47, 115, 117, 99, 104, 46, 116, 121, 112, 101, 0]);
  1.1191 +  for(let ix = 0; ix <WSP.WSP_WELL_KNOWN_CONTENT_TYPES.length ; ++ix){
  1.1192 +    wsp_encode_test_ex(func, {media: WSP.WSP_WELL_KNOWN_CONTENT_TYPES[ix].value},
  1.1193 +    [WSP.WSP_WELL_KNOWN_CONTENT_TYPES[ix].number | 0x80]);
  1.1194 +  }
  1.1195 +  wsp_encode_test_ex(func, {media: "TexT/X-hdml"}, [0x04 | 0x80]);
  1.1196 +  wsp_encode_test_ex(func, {media: "appLication/*"}, [0x10 | 0x80]);
  1.1197 +
  1.1198 +  run_next_test();
  1.1199 +});
  1.1200 +
  1.1201 +//// ContentTypeValue.encodeMediaType ////
  1.1202 +
  1.1203 +add_test(function test_ContentTypeValue_encodeMediaType() {
  1.1204 +  function func(data, input) {
  1.1205 +    WSP.ContentTypeValue.encodeMediaType(data, input);
  1.1206 +    return data.array;
  1.1207 +  }
  1.1208 +
  1.1209 +  wsp_encode_test_ex(func, {media: "no/such.type"},
  1.1210 +                     [110, 111, 47, 115, 117, 99, 104, 46, 116, 121, 112, 101, 0]);
  1.1211 +  wsp_encode_test_ex(func, {media: "application/vnd.wap.multipart.related"},
  1.1212 +                     [0x33 | 0x80]);
  1.1213 +  wsp_encode_test_ex(func, {media: "a", params: {b: "c", q: 0}},
  1.1214 +                     [97, 0, 98, 0, 99, 0, 128, 1]);
  1.1215 +
  1.1216 +  run_next_test();
  1.1217 +});
  1.1218 +
  1.1219 +//// ContentTypeValue.encodeContentGeneralForm ////
  1.1220 +
  1.1221 +add_test(function test_ContentTypeValue_encodeContentGeneralForm() {
  1.1222 +  function func(data, input) {
  1.1223 +    WSP.ContentTypeValue.encodeContentGeneralForm(data, input);
  1.1224 +    return data.array;
  1.1225 +  }
  1.1226 +
  1.1227 +  wsp_encode_test_ex(func, {media: "a", params: {b: "c", q: 0}},
  1.1228 +                     [8, 97, 0, 98, 0, 99, 0, 128, 1]);
  1.1229 +
  1.1230 +  run_next_test();
  1.1231 +});
  1.1232 +
  1.1233 +//// ContentTypeValue.encode ////
  1.1234 +
  1.1235 +add_test(function test_ContentTypeValue_encode() {
  1.1236 +  wsp_encode_test(WSP.ContentTypeValue, {media: "no/such.type"},
  1.1237 +                  [110, 111, 47, 115, 117, 99, 104, 46, 116, 121, 112, 101, 0]);
  1.1238 +  wsp_encode_test(WSP.ContentTypeValue,
  1.1239 +                  {media: "application/vnd.wap.multipart.related"},
  1.1240 +                  [0x33 | 0x80]);
  1.1241 +  wsp_encode_test(WSP.ContentTypeValue, {media: "a", params: {b: "c", q: 0}},
  1.1242 +                  [8, 97, 0, 98, 0, 99, 0, 128, 1]);
  1.1243 +
  1.1244 +  run_next_test();
  1.1245 +});
  1.1246 +
  1.1247 +//
  1.1248 +// Test target: ApplicationIdValue
  1.1249 +//
  1.1250 +
  1.1251 +//// ApplicationIdValue.decode ////
  1.1252 +
  1.1253 +add_test(function test_ApplicationIdValue_decode() {
  1.1254 +  wsp_decode_test(WSP.ApplicationIdValue, [0], "");
  1.1255 +  wsp_decode_test(WSP.ApplicationIdValue, [65, 0], "A");
  1.1256 +  wsp_decode_test(WSP.ApplicationIdValue, [97, 0], "a");
  1.1257 +  let (entry = WSP.OMNA_PUSH_APPLICATION_IDS["x-wap-application:mms.ua"]) {
  1.1258 +    wsp_decode_test(WSP.ApplicationIdValue, [entry.number | 0x80], entry.urn);
  1.1259 +    wsp_decode_test(WSP.ApplicationIdValue, [1, entry.number], entry.urn);
  1.1260 +  }
  1.1261 +  wsp_decode_test(WSP.ApplicationIdValue, [0xFF], null, "NotWellKnownEncodingError");
  1.1262 +
  1.1263 +  run_next_test();
  1.1264 +});
  1.1265 +
  1.1266 +//
  1.1267 +// Test target: PduHelper
  1.1268 +//
  1.1269 +
  1.1270 +//// PduHelper.parseHeaders ////
  1.1271 +
  1.1272 +add_test(function test_PduHelper_parseHeaders() {
  1.1273 +  wsp_decode_test_ex(function(data) {
  1.1274 +      return WSP.PduHelper.parseHeaders(data, data.array.length);
  1.1275 +    }, [0x80 | 0x05, 2, 0x23, 0x28, 0x80 | 0x2F, 0x80 | 0x04],
  1.1276 +    {"age": 9000, "x-wap-application-id": "x-wap-application:mms.ua"}
  1.1277 +  );
  1.1278 +
  1.1279 +  run_next_test();
  1.1280 +});
  1.1281 +
  1.1282 +//// PduHelper.decodeStringContent ////
  1.1283 +
  1.1284 +add_test(function StringContent_decode() {
  1.1285 +  //Test for utf-8
  1.1286 +  let (entry = WSP.WSP_WELL_KNOWN_CHARSETS["utf-8"]) {
  1.1287 +    // "Mozilla" in full width.
  1.1288 +    let str = "\uff2d\uff4f\uff5a\uff49\uff4c\uff4c\uff41";
  1.1289 +
  1.1290 +    let conv = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
  1.1291 +               .createInstance(Ci.nsIScriptableUnicodeConverter);
  1.1292 +    conv.charset = entry.converter;
  1.1293 +
  1.1294 +    let raw = conv.convertToByteArray(str);
  1.1295 +    let data = {array: raw, offset: 0};
  1.1296 +    let octetArray = WSP.Octet.decodeMultiple(data, data.array.length);
  1.1297 +    wsp_decode_test_ex(function(data) {
  1.1298 +        return WSP.PduHelper.decodeStringContent(data.array, "utf-8");
  1.1299 +      }, octetArray, str);
  1.1300 +  }
  1.1301 +
  1.1302 +  let (entry = WSP.WSP_WELL_KNOWN_CHARSETS["utf-16"]) {
  1.1303 +    // "Mozilla" in full width.
  1.1304 +    let str = "\u004d\u006F\u007A\u0069\u006C\u006C\u0061";
  1.1305 +
  1.1306 +    let conv = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
  1.1307 +               .createInstance(Ci.nsIScriptableUnicodeConverter);
  1.1308 +    conv.charset = entry.converter;
  1.1309 +
  1.1310 +    let raw = conv.convertToByteArray(str);
  1.1311 +    let data = {array: raw, offset: 0};
  1.1312 +    let octetArray = WSP.Octet.decodeMultiple(data, data.array.length);
  1.1313 +    wsp_decode_test_ex(function(data) {
  1.1314 +        return WSP.PduHelper.decodeStringContent(data.array, "utf-16");
  1.1315 +      }, raw, str);
  1.1316 +  }
  1.1317 +
  1.1318 +  run_next_test();
  1.1319 +});
  1.1320 +
  1.1321 +//// PduHelper.composeMultiPart ////
  1.1322 +
  1.1323 +add_test(function test_PduHelper_composeMultiPart() {
  1.1324 +  let multiStream = Components.classes["@mozilla.org/io/multiplex-input-stream;1"]
  1.1325 +                    .createInstance(Ci.nsIMultiplexInputStream);
  1.1326 +  let uint8Array = new Uint8Array(5);
  1.1327 +  uint8Array[0] = 0x00;
  1.1328 +  uint8Array[1] = 0x01;
  1.1329 +  uint8Array[2] = 0x02;
  1.1330 +  uint8Array[3] = 0x03;
  1.1331 +  uint8Array[4] = 0x04;
  1.1332 +
  1.1333 +  let parts = [
  1.1334 +      {
  1.1335 +        content: "content",
  1.1336 +        headers: {
  1.1337 +            "content-type": {
  1.1338 +                media: "text/plain",
  1.1339 +                params: {}
  1.1340 +            }
  1.1341 +        }
  1.1342 +      },
  1.1343 +      {
  1.1344 +        content: uint8Array,
  1.1345 +        headers: {
  1.1346 +            "content-type": {
  1.1347 +                media: "text/plain",
  1.1348 +                params: {}
  1.1349 +            }
  1.1350 +        }
  1.1351 +      }
  1.1352 +    ];
  1.1353 +
  1.1354 +  let beforeCompose = JSON.stringify(parts);
  1.1355 +  WSP.PduHelper.composeMultiPart(multiStream, parts);
  1.1356 +  let afterCompose = JSON.stringify(parts);
  1.1357 +
  1.1358 +  do_check_eq(beforeCompose, afterCompose);
  1.1359 +  run_next_test();
  1.1360 +});

mercurial