1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/common/tests/unit/test_utils_convert_string.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,132 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +"use strict"; 1.8 + 1.9 +Cu.import("resource://services-common/utils.js"); 1.10 + 1.11 +// A wise line of Greek verse, and the utf-8 byte encoding. 1.12 +// N.b., Greek begins at utf-8 ce 91 1.13 +const TEST_STR = "πόλλ' οἶδ' ἀλώπηξ, ἀλλ' ἐχῖνος ἓν μέγα"; 1.14 +const TEST_HEX = h("cf 80 cf 8c ce bb ce bb 27 20 ce bf e1 bc b6 ce"+ 1.15 + "b4 27 20 e1 bc 80 ce bb cf 8e cf 80 ce b7 ce be"+ 1.16 + "2c 20 e1 bc 80 ce bb ce bb 27 20 e1 bc 90 cf 87"+ 1.17 + "e1 bf 96 ce bd ce bf cf 82 20 e1 bc 93 ce bd 20"+ 1.18 + "ce bc ce ad ce b3 ce b1"); 1.19 +// Integer byte values for the above 1.20 +const TEST_BYTES = [207,128,207,140,206,187,206,187, 1.21 + 39, 32,206,191,225,188,182,206, 1.22 + 180, 39, 32,225,188,128,206,187, 1.23 + 207,142,207,128,206,183,206,190, 1.24 + 44, 32,225,188,128,206,187,206, 1.25 + 187, 39, 32,225,188,144,207,135, 1.26 + 225,191,150,206,189,206,191,207, 1.27 + 130, 32,225,188,147,206,189, 32, 1.28 + 206,188,206,173,206,179,206,177]; 1.29 + 1.30 +function run_test() { 1.31 + run_next_test(); 1.32 +} 1.33 + 1.34 +add_test(function test_compress_string() { 1.35 + const INPUT = "hello"; 1.36 + 1.37 + let result = CommonUtils.convertString(INPUT, "uncompressed", "deflate"); 1.38 + do_check_eq(result.length, 13); 1.39 + 1.40 + let result2 = CommonUtils.convertString(INPUT, "uncompressed", "deflate"); 1.41 + do_check_eq(result, result2); 1.42 + 1.43 + let result3 = CommonUtils.convertString(result, "deflate", "uncompressed"); 1.44 + do_check_eq(result3, INPUT); 1.45 + 1.46 + run_next_test(); 1.47 +}); 1.48 + 1.49 +add_test(function test_compress_utf8() { 1.50 + const INPUT = "Árvíztűrő tükörfúrógép いろはにほへとちりぬるを Pijamalı hasta, yağız şoföre çabucak güvendi."; 1.51 + let inputUTF8 = CommonUtils.encodeUTF8(INPUT); 1.52 + 1.53 + let compressed = CommonUtils.convertString(inputUTF8, "uncompressed", "deflate"); 1.54 + let uncompressed = CommonUtils.convertString(compressed, "deflate", "uncompressed"); 1.55 + 1.56 + do_check_eq(uncompressed, inputUTF8); 1.57 + 1.58 + let outputUTF8 = CommonUtils.decodeUTF8(uncompressed); 1.59 + do_check_eq(outputUTF8, INPUT); 1.60 + 1.61 + run_next_test(); 1.62 +}); 1.63 + 1.64 +add_test(function test_bad_argument() { 1.65 + let failed = false; 1.66 + try { 1.67 + CommonUtils.convertString(null, "uncompressed", "deflate"); 1.68 + } catch (ex) { 1.69 + failed = true; 1.70 + do_check_true(ex.message.startsWith("Input string must be defined")); 1.71 + } finally { 1.72 + do_check_true(failed); 1.73 + } 1.74 + 1.75 + run_next_test(); 1.76 +}); 1.77 + 1.78 +add_task(function test_stringAsHex() { 1.79 + do_check_eq(TEST_HEX, CommonUtils.stringAsHex(TEST_STR)); 1.80 +}); 1.81 + 1.82 +add_task(function test_hexAsString() { 1.83 + do_check_eq(TEST_STR, CommonUtils.hexAsString(TEST_HEX)); 1.84 +}); 1.85 + 1.86 +add_task(function test_hexToBytes() { 1.87 + let bytes = CommonUtils.hexToBytes(TEST_HEX); 1.88 + do_check_eq(TEST_BYTES.length, bytes.length); 1.89 + // Ensure that the decimal values of each byte are correct 1.90 + do_check_true(arraysEqual(TEST_BYTES, 1.91 + CommonUtils.stringToByteArray(bytes))); 1.92 +}); 1.93 + 1.94 +add_task(function test_bytesToHex() { 1.95 + // Create a list of our character bytes from the reference int values 1.96 + let bytes = CommonUtils.byteArrayToString(TEST_BYTES); 1.97 + do_check_eq(TEST_HEX, CommonUtils.bytesAsHex(bytes)); 1.98 +}); 1.99 + 1.100 +add_task(function test_stringToBytes() { 1.101 + do_check_true(arraysEqual(TEST_BYTES, 1.102 + CommonUtils.stringToByteArray(CommonUtils.stringToBytes(TEST_STR)))); 1.103 +}); 1.104 + 1.105 +add_task(function test_stringRoundTrip() { 1.106 + do_check_eq(TEST_STR, 1.107 + CommonUtils.hexAsString(CommonUtils.stringAsHex(TEST_STR))); 1.108 +}); 1.109 + 1.110 +add_task(function test_hexRoundTrip() { 1.111 + do_check_eq(TEST_HEX, 1.112 + CommonUtils.stringAsHex(CommonUtils.hexAsString(TEST_HEX))); 1.113 +}); 1.114 + 1.115 +add_task(function test_byteArrayRoundTrip() { 1.116 + do_check_true(arraysEqual(TEST_BYTES, 1.117 + CommonUtils.stringToByteArray(CommonUtils.byteArrayToString(TEST_BYTES)))); 1.118 +}); 1.119 + 1.120 +// turn formatted test vectors into normal hex strings 1.121 +function h(hexStr) { 1.122 + return hexStr.replace(/\s+/g, ""); 1.123 +} 1.124 + 1.125 +function arraysEqual(a1, a2) { 1.126 + if (a1.length !== a2.length) { 1.127 + return false; 1.128 + } 1.129 + for (let i = 0; i < a1.length; i++) { 1.130 + if (a1[i] !== a2[i]) { 1.131 + return false; 1.132 + } 1.133 + } 1.134 + return true; 1.135 +}