11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_cirequire('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst { isUtf8, Buffer } = require('buffer'); 61cb0ef41Sopenharmony_ciconst { TextEncoder } = require('util'); 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst encoder = new TextEncoder(); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciassert.strictEqual(isUtf8(encoder.encode('hello')), true); 111cb0ef41Sopenharmony_ciassert.strictEqual(isUtf8(encoder.encode('ğ')), true); 121cb0ef41Sopenharmony_ciassert.strictEqual(isUtf8(Buffer.from([])), true); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci// Taken from test/fixtures/wpt/encoding/textdecoder-fatal.any.js 151cb0ef41Sopenharmony_ci[ 161cb0ef41Sopenharmony_ci [0xFF], // 'invalid code' 171cb0ef41Sopenharmony_ci [0xC0], // 'ends early' 181cb0ef41Sopenharmony_ci [0xE0], // 'ends early 2' 191cb0ef41Sopenharmony_ci [0xC0, 0x00], // 'invalid trail' 201cb0ef41Sopenharmony_ci [0xC0, 0xC0], // 'invalid trail 2' 211cb0ef41Sopenharmony_ci [0xE0, 0x00], // 'invalid trail 3' 221cb0ef41Sopenharmony_ci [0xE0, 0xC0], // 'invalid trail 4' 231cb0ef41Sopenharmony_ci [0xE0, 0x80, 0x00], // 'invalid trail 5' 241cb0ef41Sopenharmony_ci [0xE0, 0x80, 0xC0], // 'invalid trail 6' 251cb0ef41Sopenharmony_ci [0xFC, 0x80, 0x80, 0x80, 0x80, 0x80], // '> 0x10FFFF' 261cb0ef41Sopenharmony_ci [0xFE, 0x80, 0x80, 0x80, 0x80, 0x80], // 'obsolete lead byte' 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci // Overlong encodings 291cb0ef41Sopenharmony_ci [0xC0, 0x80], // 'overlong U+0000 - 2 bytes' 301cb0ef41Sopenharmony_ci [0xE0, 0x80, 0x80], // 'overlong U+0000 - 3 bytes' 311cb0ef41Sopenharmony_ci [0xF0, 0x80, 0x80, 0x80], // 'overlong U+0000 - 4 bytes' 321cb0ef41Sopenharmony_ci [0xF8, 0x80, 0x80, 0x80, 0x80], // 'overlong U+0000 - 5 bytes' 331cb0ef41Sopenharmony_ci [0xFC, 0x80, 0x80, 0x80, 0x80, 0x80], // 'overlong U+0000 - 6 bytes' 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci [0xC1, 0xBF], // 'overlong U+007F - 2 bytes' 361cb0ef41Sopenharmony_ci [0xE0, 0x81, 0xBF], // 'overlong U+007F - 3 bytes' 371cb0ef41Sopenharmony_ci [0xF0, 0x80, 0x81, 0xBF], // 'overlong U+007F - 4 bytes' 381cb0ef41Sopenharmony_ci [0xF8, 0x80, 0x80, 0x81, 0xBF], // 'overlong U+007F - 5 bytes' 391cb0ef41Sopenharmony_ci [0xFC, 0x80, 0x80, 0x80, 0x81, 0xBF], // 'overlong U+007F - 6 bytes' 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci [0xE0, 0x9F, 0xBF], // 'overlong U+07FF - 3 bytes' 421cb0ef41Sopenharmony_ci [0xF0, 0x80, 0x9F, 0xBF], // 'overlong U+07FF - 4 bytes' 431cb0ef41Sopenharmony_ci [0xF8, 0x80, 0x80, 0x9F, 0xBF], // 'overlong U+07FF - 5 bytes' 441cb0ef41Sopenharmony_ci [0xFC, 0x80, 0x80, 0x80, 0x9F, 0xBF], // 'overlong U+07FF - 6 bytes' 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci [0xF0, 0x8F, 0xBF, 0xBF], // 'overlong U+FFFF - 4 bytes' 471cb0ef41Sopenharmony_ci [0xF8, 0x80, 0x8F, 0xBF, 0xBF], // 'overlong U+FFFF - 5 bytes' 481cb0ef41Sopenharmony_ci [0xFC, 0x80, 0x80, 0x8F, 0xBF, 0xBF], // 'overlong U+FFFF - 6 bytes' 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci [0xF8, 0x84, 0x8F, 0xBF, 0xBF], // 'overlong U+10FFFF - 5 bytes' 511cb0ef41Sopenharmony_ci [0xFC, 0x80, 0x84, 0x8F, 0xBF, 0xBF], // 'overlong U+10FFFF - 6 bytes' 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci // UTF-16 surrogates encoded as code points in UTF-8 541cb0ef41Sopenharmony_ci [0xED, 0xA0, 0x80], // 'lead surrogate' 551cb0ef41Sopenharmony_ci [0xED, 0xB0, 0x80], // 'trail surrogate' 561cb0ef41Sopenharmony_ci [0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80], // 'surrogate pair' 571cb0ef41Sopenharmony_ci].forEach((input) => { 581cb0ef41Sopenharmony_ci assert.strictEqual(isUtf8(Buffer.from(input)), false); 591cb0ef41Sopenharmony_ci}); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci[ 621cb0ef41Sopenharmony_ci null, 631cb0ef41Sopenharmony_ci undefined, 641cb0ef41Sopenharmony_ci 'hello', 651cb0ef41Sopenharmony_ci true, 661cb0ef41Sopenharmony_ci false, 671cb0ef41Sopenharmony_ci].forEach((input) => { 681cb0ef41Sopenharmony_ci assert.throws( 691cb0ef41Sopenharmony_ci () => { isUtf8(input); }, 701cb0ef41Sopenharmony_ci { 711cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 721cb0ef41Sopenharmony_ci }, 731cb0ef41Sopenharmony_ci ); 741cb0ef41Sopenharmony_ci}); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci{ 771cb0ef41Sopenharmony_ci // Test with detached array buffers 781cb0ef41Sopenharmony_ci const arrayBuffer = new ArrayBuffer(1024); 791cb0ef41Sopenharmony_ci structuredClone(arrayBuffer, { transfer: [arrayBuffer] }); 801cb0ef41Sopenharmony_ci assert.throws( 811cb0ef41Sopenharmony_ci () => { isUtf8(arrayBuffer); }, 821cb0ef41Sopenharmony_ci { 831cb0ef41Sopenharmony_ci code: 'ERR_INVALID_STATE' 841cb0ef41Sopenharmony_ci } 851cb0ef41Sopenharmony_ci ); 861cb0ef41Sopenharmony_ci} 87