tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_rope_encode.js (4082B)


      1 var concat = [
      2  {
      3    head: "a",
      4    tail: "b",
      5    expected: "ab",
      6    name: "Latin1 and Latin1",
      7  },
      8  {
      9    head: "α",
     10    tail: "β",
     11    expected: "αβ",
     12    name: "UTF-16 and UTF-16",
     13  },
     14  {
     15    head: "a",
     16    tail: "β",
     17    expected: "aβ",
     18    name: "Latin1 and UTF-16",
     19  },
     20  {
     21    head: "α",
     22    tail: "b",
     23    expected: "αb",
     24    name: "UTF-16 and Latin1",
     25  },
     26  {
     27    head: "\uD83D",
     28    tail: "\uDE03",
     29    expected: "\uD83D\uDE03",
     30    name: "Surrogate pair",
     31  },
     32  {
     33    head: "a\uD83D",
     34    tail: "\uDE03b",
     35    expected: "a\uD83D\uDE03b",
     36    name: "Surrogate pair with prefix and suffix",
     37  },
     38  {
     39    head: "\uD83D",
     40    tail: "b",
     41    expected: "\uFFFDb",
     42    name: "Unpaired high surrogate and Latin1",
     43  },
     44  {
     45    head: "a\uD83D",
     46    tail: "b",
     47    expected: "a\uFFFDb",
     48    name: "Prefixed unpaired high surrogate and Latin1",
     49  },
     50  {
     51    head: "\uD83D",
     52    tail: "β",
     53    expected: "\uFFFDβ",
     54    name: "Unpaired high surrogate and UTF-16",
     55  },
     56  {
     57    head: "a\uD83D",
     58    tail: "β",
     59    expected: "a\uFFFDβ",
     60    name: "Prefixed unpaired high surrogate and UTF-16",
     61  },
     62 
     63  {
     64    head: "\uDE03",
     65    tail: "b",
     66    expected: "\uFFFDb",
     67    name: "Unpaired low surrogate and Latin1",
     68  },
     69  {
     70    head: "a\uDE03",
     71    tail: "b",
     72    expected: "a\uFFFDb",
     73    name: "Prefixed unpaired low surrogate and Latin1",
     74  },
     75  {
     76    head: "\uDE03",
     77    tail: "β",
     78    expected: "\uFFFDβ",
     79    name: "Unpaired low surrogate and UTF-16",
     80  },
     81  {
     82    head: "a\uDE03",
     83    tail: "β",
     84    expected: "a\uFFFDβ",
     85    name: "Prefixed unpaired low surrogate and UTF-16",
     86  },
     87 
     88  {
     89    head: "a",
     90    tail: "\uDE03",
     91    expected: "a\uFFFD",
     92    name: "Latin1 and unpaired low surrogate",
     93  },
     94  {
     95    head: "a",
     96    tail: "\uDE03b",
     97    expected: "a\uFFFDb",
     98    name: "Latin1 and suffixed unpaired low surrogate",
     99  },
    100  {
    101    head: "α",
    102    tail: "\uDE03",
    103    expected: "α\uFFFD",
    104    name: "UTF-16 and unpaired low surrogate",
    105  },
    106  {
    107    head: "α",
    108    tail: "\uDE03b",
    109    expected: "α\uFFFDb",
    110    name: "UTF-16 and suffixed unpaired low surrogate",
    111  },
    112 
    113  {
    114    head: "a",
    115    tail: "\uD83D",
    116    expected: "a\uFFFD",
    117    name: "Latin1 and unpaired high surrogate",
    118  },
    119  {
    120    head: "a",
    121    tail: "\uD83Db",
    122    expected: "a\uFFFDb",
    123    name: "Latin1 and suffixed unpaired high surrogate",
    124  },
    125  {
    126    head: "α",
    127    tail: "\uD83D",
    128    expected: "α\uFFFD",
    129    name: "UTF-16 and unpaired high surrogate",
    130  },
    131  {
    132    head: "α",
    133    tail: "\uD83Db",
    134    expected: "α\uFFFDb",
    135    name: "UTF-16 and suffixed unpaired high surrogate",
    136  },
    137 ];
    138 
    139 var testingFunctions = Cu.getJSTestingFunctions();
    140 concat.forEach(function (t) {
    141  test(function () {
    142    assert_true(
    143      testingFunctions.isSameCompartment(testingFunctions.newRope, this),
    144      "Must be in the same compartment"
    145    );
    146    var filler = "012345678901234567890123456789";
    147    var rope = testingFunctions.newRope(
    148      t.head,
    149      testingFunctions.newRope(t.tail, filler)
    150    );
    151    var encoded = new TextEncoder().encode(rope);
    152    var decoded = new TextDecoder().decode(encoded);
    153    assert_equals(decoded, t.expected + filler, "Must round-trip");
    154  }, t.name);
    155 });
    156 
    157 test(function () {
    158  assert_true(
    159    testingFunctions.isSameCompartment(testingFunctions.newRope, this),
    160    "Must be in the same compartment"
    161  );
    162 
    163  var filler = "012345678901234567890123456789";
    164 
    165  var a = testingFunctions.newRope(filler, "a");
    166  var ab = testingFunctions.newRope(a, "b");
    167  var abc = testingFunctions.newRope(ab, "c");
    168 
    169  var e = testingFunctions.newRope(filler, "e");
    170  var ef = testingFunctions.newRope(e, "f");
    171  var def = testingFunctions.newRope("d", ef);
    172 
    173  var abcdef = testingFunctions.newRope(abc, def);
    174  var abcdefab = testingFunctions.newRope(abcdef, ab);
    175 
    176  var encoded = new TextEncoder().encode(abcdefab);
    177  var decoded = new TextDecoder().decode(encoded);
    178  assert_equals(
    179    decoded,
    180    "012345678901234567890123456789abcd012345678901234567890123456789ef012345678901234567890123456789ab",
    181    "Must walk the DAG correctly"
    182  );
    183 }, "Complex rope DAG");