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