1'use strict'; 2 3// From: https://github.com/w3c/web-platform-tests/blob/39a67e2fff/encoding/textdecoder-fatal.html 4// With the twist that we specifically test for Node.js error codes 5 6const common = require('../common'); 7 8if (!common.hasIntl) 9 common.skip('missing Intl'); 10 11const assert = require('assert'); 12 13const bad = [ 14 { encoding: 'utf-8', input: [0xFF], name: 'invalid code' }, 15 { encoding: 'utf-8', input: [0xC0], name: 'ends early' }, 16 { encoding: 'utf-8', input: [0xE0], name: 'ends early 2' }, 17 { encoding: 'utf-8', input: [0xC0, 0x00], name: 'invalid trail' }, 18 { encoding: 'utf-8', input: [0xC0, 0xC0], name: 'invalid trail 2' }, 19 { encoding: 'utf-8', input: [0xE0, 0x00], name: 'invalid trail 3' }, 20 { encoding: 'utf-8', input: [0xE0, 0xC0], name: 'invalid trail 4' }, 21 { encoding: 'utf-8', input: [0xE0, 0x80, 0x00], name: 'invalid trail 5' }, 22 { encoding: 'utf-8', input: [0xE0, 0x80, 0xC0], name: 'invalid trail 6' }, 23 { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x80, 0x80], 24 name: '> 0x10FFFF' }, 25 { encoding: 'utf-8', input: [0xFE, 0x80, 0x80, 0x80, 0x80, 0x80], 26 name: 'obsolete lead byte' }, 27 // Overlong encodings 28 { encoding: 'utf-8', input: [0xC0, 0x80], name: 'overlong U+0000 - 2 bytes' }, 29 { encoding: 'utf-8', input: [0xE0, 0x80, 0x80], 30 name: 'overlong U+0000 - 3 bytes' }, 31 { encoding: 'utf-8', input: [0xF0, 0x80, 0x80, 0x80], 32 name: 'overlong U+0000 - 4 bytes' }, 33 { encoding: 'utf-8', input: [0xF8, 0x80, 0x80, 0x80, 0x80], 34 name: 'overlong U+0000 - 5 bytes' }, 35 { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x80, 0x80], 36 name: 'overlong U+0000 - 6 bytes' }, 37 { encoding: 'utf-8', input: [0xC1, 0xBF], name: 'overlong U+007F - 2 bytes' }, 38 { encoding: 'utf-8', input: [0xE0, 0x81, 0xBF], 39 name: 'overlong U+007F - 3 bytes' }, 40 { encoding: 'utf-8', input: [0xF0, 0x80, 0x81, 0xBF], 41 name: 'overlong U+007F - 4 bytes' }, 42 { encoding: 'utf-8', input: [0xF8, 0x80, 0x80, 0x81, 0xBF], 43 name: 'overlong U+007F - 5 bytes' }, 44 { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x81, 0xBF], 45 name: 'overlong U+007F - 6 bytes' }, 46 { encoding: 'utf-8', input: [0xE0, 0x9F, 0xBF], 47 name: 'overlong U+07FF - 3 bytes' }, 48 { encoding: 'utf-8', input: [0xF0, 0x80, 0x9F, 0xBF], 49 name: 'overlong U+07FF - 4 bytes' }, 50 { encoding: 'utf-8', input: [0xF8, 0x80, 0x80, 0x9F, 0xBF], 51 name: 'overlong U+07FF - 5 bytes' }, 52 { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x9F, 0xBF], 53 name: 'overlong U+07FF - 6 bytes' }, 54 { encoding: 'utf-8', input: [0xF0, 0x8F, 0xBF, 0xBF], 55 name: 'overlong U+FFFF - 4 bytes' }, 56 { encoding: 'utf-8', input: [0xF8, 0x80, 0x8F, 0xBF, 0xBF], 57 name: 'overlong U+FFFF - 5 bytes' }, 58 { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x8F, 0xBF, 0xBF], 59 name: 'overlong U+FFFF - 6 bytes' }, 60 { encoding: 'utf-8', input: [0xF8, 0x84, 0x8F, 0xBF, 0xBF], 61 name: 'overlong U+10FFFF - 5 bytes' }, 62 { encoding: 'utf-8', input: [0xFC, 0x80, 0x84, 0x8F, 0xBF, 0xBF], 63 name: 'overlong U+10FFFF - 6 bytes' }, 64 // UTF-16 surrogates encoded as code points in UTF-8 65 { encoding: 'utf-8', input: [0xED, 0xA0, 0x80], name: 'lead surrogate' }, 66 { encoding: 'utf-8', input: [0xED, 0xB0, 0x80], name: 'trail surrogate' }, 67 { encoding: 'utf-8', input: [0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80], 68 name: 'surrogate pair' }, 69 { encoding: 'utf-16le', input: [0x00], name: 'truncated code unit' }, 70 // Mismatched UTF-16 surrogates are exercised in utf16-surrogates.html 71 // FIXME: Add legacy encoding cases 72]; 73 74bad.forEach((t) => { 75 assert.throws( 76 () => { 77 new TextDecoder(t.encoding, { fatal: true }) 78 .decode(new Uint8Array(t.input)); 79 }, { 80 code: 'ERR_ENCODING_INVALID_ENCODED_DATA', 81 name: 'TypeError' 82 } 83 ); 84}); 85