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