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.

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 "use strict";
     6 Cu.import("resource://services-common/utils.js");
     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];
    27 function run_test() {
    28   run_next_test();
    29 }
    31 add_test(function test_compress_string() {
    32   const INPUT = "hello";
    34   let result = CommonUtils.convertString(INPUT, "uncompressed", "deflate");
    35   do_check_eq(result.length, 13);
    37   let result2 = CommonUtils.convertString(INPUT, "uncompressed", "deflate");
    38   do_check_eq(result, result2);
    40   let result3 = CommonUtils.convertString(result, "deflate", "uncompressed");
    41   do_check_eq(result3, INPUT);
    43   run_next_test();
    44 });
    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);
    50   let compressed = CommonUtils.convertString(inputUTF8, "uncompressed", "deflate");
    51   let uncompressed = CommonUtils.convertString(compressed, "deflate", "uncompressed");
    53   do_check_eq(uncompressed, inputUTF8);
    55   let outputUTF8 = CommonUtils.decodeUTF8(uncompressed);
    56   do_check_eq(outputUTF8, INPUT);
    58   run_next_test();
    59 });
    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   }
    72   run_next_test();
    73 });
    75 add_task(function test_stringAsHex() {
    76   do_check_eq(TEST_HEX, CommonUtils.stringAsHex(TEST_STR));
    77 });
    79 add_task(function test_hexAsString() {
    80   do_check_eq(TEST_STR, CommonUtils.hexAsString(TEST_HEX));
    81 });
    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 });
    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 });
    97 add_task(function test_stringToBytes() {
    98   do_check_true(arraysEqual(TEST_BYTES,
    99       CommonUtils.stringToByteArray(CommonUtils.stringToBytes(TEST_STR))));
   100 });
   102 add_task(function test_stringRoundTrip() {
   103   do_check_eq(TEST_STR,
   104     CommonUtils.hexAsString(CommonUtils.stringAsHex(TEST_STR)));
   105 });
   107 add_task(function test_hexRoundTrip() {
   108   do_check_eq(TEST_HEX,
   109     CommonUtils.stringAsHex(CommonUtils.hexAsString(TEST_HEX)));
   110 });
   112 add_task(function test_byteArrayRoundTrip() {
   113   do_check_true(arraysEqual(TEST_BYTES,
   114     CommonUtils.stringToByteArray(CommonUtils.byteArrayToString(TEST_BYTES))));
   115 });
   117 // turn formatted test vectors into normal hex strings
   118 function h(hexStr) {
   119   return hexStr.replace(/\s+/g, "");
   120 }
   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 }

mercurial