|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 "use strict"; |
|
5 |
|
6 Cu.import("resource://services-common/utils.js"); |
|
7 |
|
8 // A wise line of Greek verse, and the utf-8 byte encoding. |
|
9 // N.b., Greek begins at utf-8 ce 91 |
|
10 const TEST_STR = "πόλλ' οἶδ' ἀλώπηξ, ἀλλ' ἐχῖνος ἓν μέγα"; |
|
11 const TEST_HEX = h("cf 80 cf 8c ce bb ce bb 27 20 ce bf e1 bc b6 ce"+ |
|
12 "b4 27 20 e1 bc 80 ce bb cf 8e cf 80 ce b7 ce be"+ |
|
13 "2c 20 e1 bc 80 ce bb ce bb 27 20 e1 bc 90 cf 87"+ |
|
14 "e1 bf 96 ce bd ce bf cf 82 20 e1 bc 93 ce bd 20"+ |
|
15 "ce bc ce ad ce b3 ce b1"); |
|
16 // Integer byte values for the above |
|
17 const TEST_BYTES = [207,128,207,140,206,187,206,187, |
|
18 39, 32,206,191,225,188,182,206, |
|
19 180, 39, 32,225,188,128,206,187, |
|
20 207,142,207,128,206,183,206,190, |
|
21 44, 32,225,188,128,206,187,206, |
|
22 187, 39, 32,225,188,144,207,135, |
|
23 225,191,150,206,189,206,191,207, |
|
24 130, 32,225,188,147,206,189, 32, |
|
25 206,188,206,173,206,179,206,177]; |
|
26 |
|
27 function run_test() { |
|
28 run_next_test(); |
|
29 } |
|
30 |
|
31 add_test(function test_compress_string() { |
|
32 const INPUT = "hello"; |
|
33 |
|
34 let result = CommonUtils.convertString(INPUT, "uncompressed", "deflate"); |
|
35 do_check_eq(result.length, 13); |
|
36 |
|
37 let result2 = CommonUtils.convertString(INPUT, "uncompressed", "deflate"); |
|
38 do_check_eq(result, result2); |
|
39 |
|
40 let result3 = CommonUtils.convertString(result, "deflate", "uncompressed"); |
|
41 do_check_eq(result3, INPUT); |
|
42 |
|
43 run_next_test(); |
|
44 }); |
|
45 |
|
46 add_test(function test_compress_utf8() { |
|
47 const INPUT = "Árvíztűrő tükörfúrógép いろはにほへとちりぬるを Pijamalı hasta, yağız şoföre çabucak güvendi."; |
|
48 let inputUTF8 = CommonUtils.encodeUTF8(INPUT); |
|
49 |
|
50 let compressed = CommonUtils.convertString(inputUTF8, "uncompressed", "deflate"); |
|
51 let uncompressed = CommonUtils.convertString(compressed, "deflate", "uncompressed"); |
|
52 |
|
53 do_check_eq(uncompressed, inputUTF8); |
|
54 |
|
55 let outputUTF8 = CommonUtils.decodeUTF8(uncompressed); |
|
56 do_check_eq(outputUTF8, INPUT); |
|
57 |
|
58 run_next_test(); |
|
59 }); |
|
60 |
|
61 add_test(function test_bad_argument() { |
|
62 let failed = false; |
|
63 try { |
|
64 CommonUtils.convertString(null, "uncompressed", "deflate"); |
|
65 } catch (ex) { |
|
66 failed = true; |
|
67 do_check_true(ex.message.startsWith("Input string must be defined")); |
|
68 } finally { |
|
69 do_check_true(failed); |
|
70 } |
|
71 |
|
72 run_next_test(); |
|
73 }); |
|
74 |
|
75 add_task(function test_stringAsHex() { |
|
76 do_check_eq(TEST_HEX, CommonUtils.stringAsHex(TEST_STR)); |
|
77 }); |
|
78 |
|
79 add_task(function test_hexAsString() { |
|
80 do_check_eq(TEST_STR, CommonUtils.hexAsString(TEST_HEX)); |
|
81 }); |
|
82 |
|
83 add_task(function test_hexToBytes() { |
|
84 let bytes = CommonUtils.hexToBytes(TEST_HEX); |
|
85 do_check_eq(TEST_BYTES.length, bytes.length); |
|
86 // Ensure that the decimal values of each byte are correct |
|
87 do_check_true(arraysEqual(TEST_BYTES, |
|
88 CommonUtils.stringToByteArray(bytes))); |
|
89 }); |
|
90 |
|
91 add_task(function test_bytesToHex() { |
|
92 // Create a list of our character bytes from the reference int values |
|
93 let bytes = CommonUtils.byteArrayToString(TEST_BYTES); |
|
94 do_check_eq(TEST_HEX, CommonUtils.bytesAsHex(bytes)); |
|
95 }); |
|
96 |
|
97 add_task(function test_stringToBytes() { |
|
98 do_check_true(arraysEqual(TEST_BYTES, |
|
99 CommonUtils.stringToByteArray(CommonUtils.stringToBytes(TEST_STR)))); |
|
100 }); |
|
101 |
|
102 add_task(function test_stringRoundTrip() { |
|
103 do_check_eq(TEST_STR, |
|
104 CommonUtils.hexAsString(CommonUtils.stringAsHex(TEST_STR))); |
|
105 }); |
|
106 |
|
107 add_task(function test_hexRoundTrip() { |
|
108 do_check_eq(TEST_HEX, |
|
109 CommonUtils.stringAsHex(CommonUtils.hexAsString(TEST_HEX))); |
|
110 }); |
|
111 |
|
112 add_task(function test_byteArrayRoundTrip() { |
|
113 do_check_true(arraysEqual(TEST_BYTES, |
|
114 CommonUtils.stringToByteArray(CommonUtils.byteArrayToString(TEST_BYTES)))); |
|
115 }); |
|
116 |
|
117 // turn formatted test vectors into normal hex strings |
|
118 function h(hexStr) { |
|
119 return hexStr.replace(/\s+/g, ""); |
|
120 } |
|
121 |
|
122 function arraysEqual(a1, a2) { |
|
123 if (a1.length !== a2.length) { |
|
124 return false; |
|
125 } |
|
126 for (let i = 0; i < a1.length; i++) { |
|
127 if (a1[i] !== a2[i]) { |
|
128 return false; |
|
129 } |
|
130 } |
|
131 return true; |
|
132 } |