tor-browser

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

decode-non-utf8.any.js (2805B)


      1 // META: global=window,worker,shadowrealm
      2 
      3 'use strict';
      4 
      5 // The browser is assumed to use the same implementation as for TextDecoder, so
      6 // this file don't replicate the exhaustive checks it has. It is just a smoke
      7 // test that non-UTF-8 encodings work at all.
      8 
      9 const encodings = [
     10  {
     11    name: 'UTF-16BE',
     12    value: [108, 52],
     13    expected: "\u{6c34}",
     14    invalid: [0xD8, 0x00]
     15  },
     16  {
     17    name: 'UTF-16LE',
     18    value: [52, 108],
     19    expected: "\u{6c34}",
     20    invalid: [0x00, 0xD8]
     21  },
     22  {
     23    name: 'Shift_JIS',
     24    value: [144, 133],
     25    expected: "\u{6c34}",
     26    invalid: [255]
     27  },
     28  {
     29    name: 'ISO-2022-JP',
     30    value: [65, 66, 67, 0x1B, 65, 66, 67],
     31    expected: "ABC\u{fffd}ABC",
     32    invalid: [0x0E]
     33  },
     34  {
     35    name: 'ISO-8859-14',
     36    value: [100, 240, 114],
     37    expected: "d\u{0175}r",
     38    invalid: undefined  // all bytes are treated as valid
     39  }
     40 ];
     41 
     42 for (const encoding of encodings) {
     43  promise_test(async () => {
     44    const stream = new TextDecoderStream(encoding.name);
     45    const reader = stream.readable.getReader();
     46    const writer = stream.writable.getWriter();
     47    const writePromise = writer.write(new Uint8Array(encoding.value));
     48    const {value, done} = await reader.read();
     49    assert_false(done, 'readable should not be closed');
     50    assert_equals(value, encoding.expected, 'chunk should match expected');
     51    await writePromise;
     52  }, `TextDecoderStream should be able to decode ${encoding.name}`);
     53 
     54  if (!encoding.invalid)
     55    continue;
     56 
     57  promise_test(async t => {
     58    const stream = new TextDecoderStream(encoding.name);
     59    const reader = stream.readable.getReader();
     60    const writer = stream.writable.getWriter();
     61    const writePromise = writer.write(new Uint8Array(encoding.invalid));
     62    const closePromise = writer.close();
     63    const {value, done} = await reader.read();
     64    assert_false(done, 'readable should not be closed');
     65    assert_equals(value, '\u{FFFD}', 'output should be replacement character');
     66    await Promise.all([writePromise, closePromise]);
     67  }, `TextDecoderStream should be able to decode invalid sequences in ` +
     68     `${encoding.name}`);
     69 
     70  promise_test(async t => {
     71    const stream = new TextDecoderStream(encoding.name, {fatal: true});
     72    const reader = stream.readable.getReader();
     73    const writer = stream.writable.getWriter();
     74    const writePromise = writer.write(new Uint8Array(encoding.invalid));
     75    const closePromise = writer.close();
     76    await promise_rejects_js(t, TypeError, reader.read(),
     77                          'readable should be errored');
     78    await promise_rejects_js(t, TypeError,
     79                          Promise.all([writePromise, closePromise]),
     80                          'writable should be errored');
     81  }, `TextDecoderStream should be able to reject invalid sequences in ` +
     82     `${encoding.name}`);
     83 }