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

mercurial