tor-browser

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

textdecoder-fatal.any.js (4722B)


      1 // META: global=window,dedicatedworker,shadowrealm
      2 // META: title=Encoding API: Fatal flag
      3 
      4 var bad = [
      5    { encoding: 'utf-8', input: [0xFF], name: 'invalid code' },
      6    { encoding: 'utf-8', input: [0xC0], name: 'ends early' },
      7    { encoding: 'utf-8', input: [0xE0], name: 'ends early 2' },
      8    { encoding: 'utf-8', input: [0xC0, 0x00], name: 'invalid trail' },
      9    { encoding: 'utf-8', input: [0xC0, 0xC0], name: 'invalid trail 2' },
     10    { encoding: 'utf-8', input: [0xE0, 0x00], name: 'invalid trail 3' },
     11    { encoding: 'utf-8', input: [0xE0, 0xC0], name: 'invalid trail 4' },
     12    { encoding: 'utf-8', input: [0xE0, 0x80, 0x00], name: 'invalid trail 5' },
     13    { encoding: 'utf-8', input: [0xE0, 0x80, 0xC0], name: 'invalid trail 6' },
     14    { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x80, 0x80], name: '> 0x10FFFF' },
     15    { encoding: 'utf-8', input: [0xFE, 0x80, 0x80, 0x80, 0x80, 0x80], name: 'obsolete lead byte' },
     16 
     17    // Overlong encodings
     18    { encoding: 'utf-8', input: [0xC0, 0x80], name: 'overlong U+0000 - 2 bytes' },
     19    { encoding: 'utf-8', input: [0xE0, 0x80, 0x80], name: 'overlong U+0000 - 3 bytes' },
     20    { encoding: 'utf-8', input: [0xF0, 0x80, 0x80, 0x80], name: 'overlong U+0000 - 4 bytes' },
     21    { encoding: 'utf-8', input: [0xF8, 0x80, 0x80, 0x80, 0x80], name: 'overlong U+0000 - 5 bytes' },
     22    { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x80, 0x80], name: 'overlong U+0000 - 6 bytes' },
     23 
     24    { encoding: 'utf-8', input: [0xC1, 0xBF], name: 'overlong U+007F - 2 bytes' },
     25    { encoding: 'utf-8', input: [0xE0, 0x81, 0xBF], name: 'overlong U+007F - 3 bytes' },
     26    { encoding: 'utf-8', input: [0xF0, 0x80, 0x81, 0xBF], name: 'overlong U+007F - 4 bytes' },
     27    { encoding: 'utf-8', input: [0xF8, 0x80, 0x80, 0x81, 0xBF], name: 'overlong U+007F - 5 bytes' },
     28    { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x81, 0xBF], name: 'overlong U+007F - 6 bytes' },
     29 
     30    { encoding: 'utf-8', input: [0xE0, 0x9F, 0xBF], name: 'overlong U+07FF - 3 bytes' },
     31    { encoding: 'utf-8', input: [0xF0, 0x80, 0x9F, 0xBF], name: 'overlong U+07FF - 4 bytes' },
     32    { encoding: 'utf-8', input: [0xF8, 0x80, 0x80, 0x9F, 0xBF], name: 'overlong U+07FF - 5 bytes' },
     33    { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x9F, 0xBF], name: 'overlong U+07FF - 6 bytes' },
     34 
     35    { encoding: 'utf-8', input: [0xF0, 0x8F, 0xBF, 0xBF], name: 'overlong U+FFFF - 4 bytes' },
     36    { encoding: 'utf-8', input: [0xF8, 0x80, 0x8F, 0xBF, 0xBF], name: 'overlong U+FFFF - 5 bytes' },
     37    { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x8F, 0xBF, 0xBF], name: 'overlong U+FFFF - 6 bytes' },
     38 
     39    { encoding: 'utf-8', input: [0xF8, 0x84, 0x8F, 0xBF, 0xBF], name: 'overlong U+10FFFF - 5 bytes' },
     40    { encoding: 'utf-8', input: [0xFC, 0x80, 0x84, 0x8F, 0xBF, 0xBF], name: 'overlong U+10FFFF - 6 bytes' },
     41 
     42    // UTF-16 surrogates encoded as code points in UTF-8
     43    { encoding: 'utf-8', input: [0xED, 0xA0, 0x80], name: 'lead surrogate' },
     44    { encoding: 'utf-8', input: [0xED, 0xB0, 0x80], name: 'trail surrogate' },
     45    { encoding: 'utf-8', input: [0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80], name: 'surrogate pair' },
     46 
     47    { encoding: 'utf-16le', input: [0x00], name: 'truncated code unit' },
     48    // Mismatched UTF-16 surrogates are exercised in utf16-surrogates.html
     49 
     50    // FIXME: Add legacy encoding cases
     51 ];
     52 
     53 bad.forEach(function(t) {
     54    test(function() {
     55        assert_throws_js(TypeError, function() {
     56            new TextDecoder(t.encoding, {fatal: true}).decode(new Uint8Array(t.input))
     57        });
     58    }, 'Fatal flag: ' + t.encoding + ' - ' + t.name);
     59 });
     60 
     61 test(function() {
     62    assert_true('fatal' in new TextDecoder(), 'The fatal attribute should exist on TextDecoder.');
     63    assert_equals(typeof  new TextDecoder().fatal, 'boolean', 'The type of the fatal attribute should be boolean.');
     64    assert_false(new TextDecoder().fatal, 'The fatal attribute should default to false.');
     65    assert_true(new TextDecoder('utf-8', {fatal: true}).fatal, 'The fatal attribute can be set using an option.');
     66 
     67 }, 'The fatal attribute of TextDecoder');
     68 
     69 test(() => {
     70  const bytes = new Uint8Array([226, 153, 165]);
     71  const decoder = new TextDecoder('utf-8', {fatal: true});
     72  assert_equals(decoder.decode(new DataView(bytes.buffer, 0, 3)),
     73                '♥',
     74                'decode() should decode full sequence');
     75  assert_throws_js(TypeError,
     76                   () => decoder.decode(new DataView(bytes.buffer, 0, 2)),
     77                   'decode() should throw on incomplete sequence');
     78  assert_equals(decoder.decode(new DataView(bytes.buffer, 0, 3)),
     79                '♥',
     80                'decode() should not throw on subsequent call');
     81 }, 'Error seen with fatal does not prevent future decodes');