11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst { deepStrictEqual, strictEqual, throws } = require('assert'); 51cb0ef41Sopenharmony_ciconst { runInNewContext } = require('vm'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst checkString = 'test'; 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst check = Buffer.from(checkString); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciclass MyString extends String { 121cb0ef41Sopenharmony_ci constructor() { 131cb0ef41Sopenharmony_ci super(checkString); 141cb0ef41Sopenharmony_ci } 151cb0ef41Sopenharmony_ci} 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ciclass MyPrimitive { 181cb0ef41Sopenharmony_ci [Symbol.toPrimitive]() { 191cb0ef41Sopenharmony_ci return checkString; 201cb0ef41Sopenharmony_ci } 211cb0ef41Sopenharmony_ci} 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciclass MyBadPrimitive { 241cb0ef41Sopenharmony_ci [Symbol.toPrimitive]() { 251cb0ef41Sopenharmony_ci return 1; 261cb0ef41Sopenharmony_ci } 271cb0ef41Sopenharmony_ci} 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_cideepStrictEqual(Buffer.from(new String(checkString)), check); 301cb0ef41Sopenharmony_cideepStrictEqual(Buffer.from(new MyString()), check); 311cb0ef41Sopenharmony_cideepStrictEqual(Buffer.from(new MyPrimitive()), check); 321cb0ef41Sopenharmony_cideepStrictEqual( 331cb0ef41Sopenharmony_ci Buffer.from(runInNewContext('new String(checkString)', { checkString })), 341cb0ef41Sopenharmony_ci check 351cb0ef41Sopenharmony_ci); 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci[ 381cb0ef41Sopenharmony_ci {}, 391cb0ef41Sopenharmony_ci new Boolean(true), 401cb0ef41Sopenharmony_ci { valueOf() { return null; } }, 411cb0ef41Sopenharmony_ci { valueOf() { return undefined; } }, 421cb0ef41Sopenharmony_ci { valueOf: null }, 431cb0ef41Sopenharmony_ci Object.create(null), 441cb0ef41Sopenharmony_ci new Number(true), 451cb0ef41Sopenharmony_ci new MyBadPrimitive(), 461cb0ef41Sopenharmony_ci Symbol(), 471cb0ef41Sopenharmony_ci 5n, 481cb0ef41Sopenharmony_ci (one, two, three) => {}, 491cb0ef41Sopenharmony_ci undefined, 501cb0ef41Sopenharmony_ci null, 511cb0ef41Sopenharmony_ci].forEach((input) => { 521cb0ef41Sopenharmony_ci const errObj = { 531cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 541cb0ef41Sopenharmony_ci name: 'TypeError', 551cb0ef41Sopenharmony_ci message: 'The first argument must be of type string or an instance of ' + 561cb0ef41Sopenharmony_ci 'Buffer, ArrayBuffer, or Array or an Array-like Object.' + 571cb0ef41Sopenharmony_ci common.invalidArgTypeHelper(input) 581cb0ef41Sopenharmony_ci }; 591cb0ef41Sopenharmony_ci throws(() => Buffer.from(input), errObj); 601cb0ef41Sopenharmony_ci throws(() => Buffer.from(input, 'hex'), errObj); 611cb0ef41Sopenharmony_ci}); 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ciBuffer.allocUnsafe(10); // Should not throw. 641cb0ef41Sopenharmony_ciBuffer.from('deadbeaf', 'hex'); // Should not throw. 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci{ 681cb0ef41Sopenharmony_ci const u16 = new Uint16Array([0xffff]); 691cb0ef41Sopenharmony_ci const b16 = Buffer.copyBytesFrom(u16); 701cb0ef41Sopenharmony_ci u16[0] = 0; 711cb0ef41Sopenharmony_ci strictEqual(b16.length, 2); 721cb0ef41Sopenharmony_ci strictEqual(b16[0], 255); 731cb0ef41Sopenharmony_ci strictEqual(b16[1], 255); 741cb0ef41Sopenharmony_ci} 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci{ 771cb0ef41Sopenharmony_ci const u16 = new Uint16Array([0, 0xffff]); 781cb0ef41Sopenharmony_ci const b16 = Buffer.copyBytesFrom(u16, 1, 5); 791cb0ef41Sopenharmony_ci u16[0] = 0xffff; 801cb0ef41Sopenharmony_ci u16[1] = 0; 811cb0ef41Sopenharmony_ci strictEqual(b16.length, 2); 821cb0ef41Sopenharmony_ci strictEqual(b16[0], 255); 831cb0ef41Sopenharmony_ci strictEqual(b16[1], 255); 841cb0ef41Sopenharmony_ci} 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci{ 871cb0ef41Sopenharmony_ci const u32 = new Uint32Array([0xffffffff]); 881cb0ef41Sopenharmony_ci const b32 = Buffer.copyBytesFrom(u32); 891cb0ef41Sopenharmony_ci u32[0] = 0; 901cb0ef41Sopenharmony_ci strictEqual(b32.length, 4); 911cb0ef41Sopenharmony_ci strictEqual(b32[0], 255); 921cb0ef41Sopenharmony_ci strictEqual(b32[1], 255); 931cb0ef41Sopenharmony_ci strictEqual(b32[2], 255); 941cb0ef41Sopenharmony_ci strictEqual(b32[3], 255); 951cb0ef41Sopenharmony_ci} 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_cithrows(() => { 981cb0ef41Sopenharmony_ci Buffer.copyBytesFrom(); 991cb0ef41Sopenharmony_ci}, { 1001cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 1011cb0ef41Sopenharmony_ci}); 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci['', Symbol(), true, false, {}, [], () => {}, 1, 1n, null, undefined].forEach( 1041cb0ef41Sopenharmony_ci (notTypedArray) => throws(() => { 1051cb0ef41Sopenharmony_ci Buffer.copyBytesFrom('nope'); 1061cb0ef41Sopenharmony_ci }, { 1071cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 1081cb0ef41Sopenharmony_ci }) 1091cb0ef41Sopenharmony_ci); 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci['', Symbol(), true, false, {}, [], () => {}, 1n].forEach((notANumber) => 1121cb0ef41Sopenharmony_ci throws(() => { 1131cb0ef41Sopenharmony_ci Buffer.copyBytesFrom(new Uint8Array(1), notANumber); 1141cb0ef41Sopenharmony_ci }, { 1151cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 1161cb0ef41Sopenharmony_ci }) 1171cb0ef41Sopenharmony_ci); 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci[-1, NaN, 1.1, -Infinity].forEach((outOfRange) => 1201cb0ef41Sopenharmony_ci throws(() => { 1211cb0ef41Sopenharmony_ci Buffer.copyBytesFrom(new Uint8Array(1), outOfRange); 1221cb0ef41Sopenharmony_ci }, { 1231cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1241cb0ef41Sopenharmony_ci }) 1251cb0ef41Sopenharmony_ci); 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci['', Symbol(), true, false, {}, [], () => {}, 1n].forEach((notANumber) => 1281cb0ef41Sopenharmony_ci throws(() => { 1291cb0ef41Sopenharmony_ci Buffer.copyBytesFrom(new Uint8Array(1), 0, notANumber); 1301cb0ef41Sopenharmony_ci }, { 1311cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 1321cb0ef41Sopenharmony_ci }) 1331cb0ef41Sopenharmony_ci); 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci[-1, NaN, 1.1, -Infinity].forEach((outOfRange) => 1361cb0ef41Sopenharmony_ci throws(() => { 1371cb0ef41Sopenharmony_ci Buffer.copyBytesFrom(new Uint8Array(1), 0, outOfRange); 1381cb0ef41Sopenharmony_ci }, { 1391cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1401cb0ef41Sopenharmony_ci }) 1411cb0ef41Sopenharmony_ci); 142