11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// From: https://github.com/w3c/web-platform-tests/blob/39a67e2fff/encoding/textdecoder-fatal.html
41cb0ef41Sopenharmony_ci// With the twist that we specifically test for Node.js error codes
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst common = require('../common');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciif (!common.hasIntl)
91cb0ef41Sopenharmony_ci  common.skip('missing Intl');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst assert = require('assert');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst bad = [
141cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xFF], name: 'invalid code' },
151cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xC0], name: 'ends early' },
161cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xE0], name: 'ends early 2' },
171cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xC0, 0x00], name: 'invalid trail' },
181cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xC0, 0xC0], name: 'invalid trail 2' },
191cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xE0, 0x00], name: 'invalid trail 3' },
201cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xE0, 0xC0], name: 'invalid trail 4' },
211cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xE0, 0x80, 0x00], name: 'invalid trail 5' },
221cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xE0, 0x80, 0xC0], name: 'invalid trail 6' },
231cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x80, 0x80],
241cb0ef41Sopenharmony_ci    name: '> 0x10FFFF' },
251cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xFE, 0x80, 0x80, 0x80, 0x80, 0x80],
261cb0ef41Sopenharmony_ci    name: 'obsolete lead byte' },
271cb0ef41Sopenharmony_ci  // Overlong encodings
281cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xC0, 0x80], name: 'overlong U+0000 - 2 bytes' },
291cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xE0, 0x80, 0x80],
301cb0ef41Sopenharmony_ci    name: 'overlong U+0000 - 3 bytes' },
311cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xF0, 0x80, 0x80, 0x80],
321cb0ef41Sopenharmony_ci    name: 'overlong U+0000 - 4 bytes' },
331cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xF8, 0x80, 0x80, 0x80, 0x80],
341cb0ef41Sopenharmony_ci    name: 'overlong U+0000 - 5 bytes' },
351cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x80, 0x80],
361cb0ef41Sopenharmony_ci    name: 'overlong U+0000 - 6 bytes' },
371cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xC1, 0xBF], name: 'overlong U+007F - 2 bytes' },
381cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xE0, 0x81, 0xBF],
391cb0ef41Sopenharmony_ci    name: 'overlong U+007F - 3 bytes' },
401cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xF0, 0x80, 0x81, 0xBF],
411cb0ef41Sopenharmony_ci    name: 'overlong U+007F - 4 bytes' },
421cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xF8, 0x80, 0x80, 0x81, 0xBF],
431cb0ef41Sopenharmony_ci    name: 'overlong U+007F - 5 bytes' },
441cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x81, 0xBF],
451cb0ef41Sopenharmony_ci    name: 'overlong U+007F - 6 bytes' },
461cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xE0, 0x9F, 0xBF],
471cb0ef41Sopenharmony_ci    name: 'overlong U+07FF - 3 bytes' },
481cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xF0, 0x80, 0x9F, 0xBF],
491cb0ef41Sopenharmony_ci    name: 'overlong U+07FF - 4 bytes' },
501cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xF8, 0x80, 0x80, 0x9F, 0xBF],
511cb0ef41Sopenharmony_ci    name: 'overlong U+07FF - 5 bytes' },
521cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x9F, 0xBF],
531cb0ef41Sopenharmony_ci    name: 'overlong U+07FF - 6 bytes' },
541cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xF0, 0x8F, 0xBF, 0xBF],
551cb0ef41Sopenharmony_ci    name: 'overlong U+FFFF - 4 bytes' },
561cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xF8, 0x80, 0x8F, 0xBF, 0xBF],
571cb0ef41Sopenharmony_ci    name: 'overlong U+FFFF - 5 bytes' },
581cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x8F, 0xBF, 0xBF],
591cb0ef41Sopenharmony_ci    name: 'overlong U+FFFF - 6 bytes' },
601cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xF8, 0x84, 0x8F, 0xBF, 0xBF],
611cb0ef41Sopenharmony_ci    name: 'overlong U+10FFFF - 5 bytes' },
621cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xFC, 0x80, 0x84, 0x8F, 0xBF, 0xBF],
631cb0ef41Sopenharmony_ci    name: 'overlong U+10FFFF - 6 bytes' },
641cb0ef41Sopenharmony_ci  // UTF-16 surrogates encoded as code points in UTF-8
651cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xED, 0xA0, 0x80], name: 'lead surrogate' },
661cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xED, 0xB0, 0x80], name: 'trail surrogate' },
671cb0ef41Sopenharmony_ci  { encoding: 'utf-8', input: [0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80],
681cb0ef41Sopenharmony_ci    name: 'surrogate pair' },
691cb0ef41Sopenharmony_ci  { encoding: 'utf-16le', input: [0x00], name: 'truncated code unit' },
701cb0ef41Sopenharmony_ci  // Mismatched UTF-16 surrogates are exercised in utf16-surrogates.html
711cb0ef41Sopenharmony_ci  // FIXME: Add legacy encoding cases
721cb0ef41Sopenharmony_ci];
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_cibad.forEach((t) => {
751cb0ef41Sopenharmony_ci  assert.throws(
761cb0ef41Sopenharmony_ci    () => {
771cb0ef41Sopenharmony_ci      new TextDecoder(t.encoding, { fatal: true })
781cb0ef41Sopenharmony_ci        .decode(new Uint8Array(t.input));
791cb0ef41Sopenharmony_ci    }, {
801cb0ef41Sopenharmony_ci      code: 'ERR_ENCODING_INVALID_ENCODED_DATA',
811cb0ef41Sopenharmony_ci      name: 'TypeError'
821cb0ef41Sopenharmony_ci    }
831cb0ef41Sopenharmony_ci  );
841cb0ef41Sopenharmony_ci});
85