11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst vm = require('vm'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst SlowBuffer = require('buffer').SlowBuffer; 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci// Verify the maximum Uint8Array size. There is no concrete limit by spec. The 101cb0ef41Sopenharmony_ci// internal limits should be updated if this fails. 111cb0ef41Sopenharmony_ciassert.throws( 121cb0ef41Sopenharmony_ci () => new Uint8Array(2 ** 32 + 1), 131cb0ef41Sopenharmony_ci { message: 'Invalid typed array length: 4294967297' } 141cb0ef41Sopenharmony_ci); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciconst b = Buffer.allocUnsafe(1024); 171cb0ef41Sopenharmony_ciassert.strictEqual(b.length, 1024); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_cib[0] = -1; 201cb0ef41Sopenharmony_ciassert.strictEqual(b[0], 255); 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_cifor (let i = 0; i < 1024; i++) { 231cb0ef41Sopenharmony_ci b[i] = i % 256; 241cb0ef41Sopenharmony_ci} 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_cifor (let i = 0; i < 1024; i++) { 271cb0ef41Sopenharmony_ci assert.strictEqual(i % 256, b[i]); 281cb0ef41Sopenharmony_ci} 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciconst c = Buffer.allocUnsafe(512); 311cb0ef41Sopenharmony_ciassert.strictEqual(c.length, 512); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ciconst d = Buffer.from([]); 341cb0ef41Sopenharmony_ciassert.strictEqual(d.length, 0); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci// Test offset properties 371cb0ef41Sopenharmony_ci{ 381cb0ef41Sopenharmony_ci const b = Buffer.alloc(128); 391cb0ef41Sopenharmony_ci assert.strictEqual(b.length, 128); 401cb0ef41Sopenharmony_ci assert.strictEqual(b.byteOffset, 0); 411cb0ef41Sopenharmony_ci assert.strictEqual(b.offset, 0); 421cb0ef41Sopenharmony_ci} 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci// Test creating a Buffer from a Uint8Array 451cb0ef41Sopenharmony_ci{ 461cb0ef41Sopenharmony_ci const ui8 = new Uint8Array(4).fill(42); 471cb0ef41Sopenharmony_ci const e = Buffer.from(ui8); 481cb0ef41Sopenharmony_ci for (const [index, value] of e.entries()) { 491cb0ef41Sopenharmony_ci assert.strictEqual(value, ui8[index]); 501cb0ef41Sopenharmony_ci } 511cb0ef41Sopenharmony_ci} 521cb0ef41Sopenharmony_ci// Test creating a Buffer from a Uint8Array (old constructor) 531cb0ef41Sopenharmony_ci{ 541cb0ef41Sopenharmony_ci const ui8 = new Uint8Array(4).fill(42); 551cb0ef41Sopenharmony_ci const e = Buffer(ui8); 561cb0ef41Sopenharmony_ci for (const [key, value] of e.entries()) { 571cb0ef41Sopenharmony_ci assert.strictEqual(value, ui8[key]); 581cb0ef41Sopenharmony_ci } 591cb0ef41Sopenharmony_ci} 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci// Test creating a Buffer from a Uint32Array 621cb0ef41Sopenharmony_ci// Note: it is implicitly interpreted as Array of integers modulo 256 631cb0ef41Sopenharmony_ci{ 641cb0ef41Sopenharmony_ci const ui32 = new Uint32Array(4).fill(42); 651cb0ef41Sopenharmony_ci const e = Buffer.from(ui32); 661cb0ef41Sopenharmony_ci for (const [index, value] of e.entries()) { 671cb0ef41Sopenharmony_ci assert.strictEqual(value, ui32[index]); 681cb0ef41Sopenharmony_ci } 691cb0ef41Sopenharmony_ci} 701cb0ef41Sopenharmony_ci// Test creating a Buffer from a Uint32Array (old constructor) 711cb0ef41Sopenharmony_ci// Note: it is implicitly interpreted as Array of integers modulo 256 721cb0ef41Sopenharmony_ci{ 731cb0ef41Sopenharmony_ci const ui32 = new Uint32Array(4).fill(42); 741cb0ef41Sopenharmony_ci const e = Buffer(ui32); 751cb0ef41Sopenharmony_ci for (const [key, value] of e.entries()) { 761cb0ef41Sopenharmony_ci assert.strictEqual(value, ui32[key]); 771cb0ef41Sopenharmony_ci } 781cb0ef41Sopenharmony_ci} 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci// Test invalid encoding for Buffer.toString 811cb0ef41Sopenharmony_ciassert.throws(() => b.toString('invalid'), 821cb0ef41Sopenharmony_ci /Unknown encoding: invalid/); 831cb0ef41Sopenharmony_ci// Invalid encoding for Buffer.write 841cb0ef41Sopenharmony_ciassert.throws(() => b.write('test string', 0, 5, 'invalid'), 851cb0ef41Sopenharmony_ci /Unknown encoding: invalid/); 861cb0ef41Sopenharmony_ci// Unsupported arguments for Buffer.write 871cb0ef41Sopenharmony_ciassert.throws(() => b.write('test', 'utf8', 0), 881cb0ef41Sopenharmony_ci { code: 'ERR_INVALID_ARG_TYPE' }); 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci// Try to create 0-length buffers. Should not throw. 911cb0ef41Sopenharmony_ciBuffer.from(''); 921cb0ef41Sopenharmony_ciBuffer.from('', 'ascii'); 931cb0ef41Sopenharmony_ciBuffer.from('', 'latin1'); 941cb0ef41Sopenharmony_ciBuffer.alloc(0); 951cb0ef41Sopenharmony_ciBuffer.allocUnsafe(0); 961cb0ef41Sopenharmony_cinew Buffer(''); 971cb0ef41Sopenharmony_cinew Buffer('', 'ascii'); 981cb0ef41Sopenharmony_cinew Buffer('', 'latin1'); 991cb0ef41Sopenharmony_cinew Buffer('', 'binary'); 1001cb0ef41Sopenharmony_ciBuffer(0); 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ciconst outOfRangeError = { 1031cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1041cb0ef41Sopenharmony_ci name: 'RangeError' 1051cb0ef41Sopenharmony_ci}; 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci// Try to write a 0-length string beyond the end of b 1081cb0ef41Sopenharmony_ciassert.throws(() => b.write('', 2048), outOfRangeError); 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci// Throw when writing to negative offset 1111cb0ef41Sopenharmony_ciassert.throws(() => b.write('a', -1), outOfRangeError); 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci// Throw when writing past bounds from the pool 1141cb0ef41Sopenharmony_ciassert.throws(() => b.write('a', 2048), outOfRangeError); 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci// Throw when writing to negative offset 1171cb0ef41Sopenharmony_ciassert.throws(() => b.write('a', -1), outOfRangeError); 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci// Try to copy 0 bytes worth of data into an empty buffer 1201cb0ef41Sopenharmony_cib.copy(Buffer.alloc(0), 0, 0, 0); 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci// Try to copy 0 bytes past the end of the target buffer 1231cb0ef41Sopenharmony_cib.copy(Buffer.alloc(0), 1, 1, 1); 1241cb0ef41Sopenharmony_cib.copy(Buffer.alloc(1), 1, 1, 1); 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ci// Try to copy 0 bytes from past the end of the source buffer 1271cb0ef41Sopenharmony_cib.copy(Buffer.alloc(1), 0, 1024, 1024); 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci// Testing for smart defaults and ability to pass string values as offset 1301cb0ef41Sopenharmony_ci{ 1311cb0ef41Sopenharmony_ci const writeTest = Buffer.from('abcdes'); 1321cb0ef41Sopenharmony_ci writeTest.write('n', 'ascii'); 1331cb0ef41Sopenharmony_ci assert.throws( 1341cb0ef41Sopenharmony_ci () => writeTest.write('o', '1', 'ascii'), 1351cb0ef41Sopenharmony_ci { code: 'ERR_INVALID_ARG_TYPE' } 1361cb0ef41Sopenharmony_ci ); 1371cb0ef41Sopenharmony_ci writeTest.write('o', 1, 'ascii'); 1381cb0ef41Sopenharmony_ci writeTest.write('d', 2, 'ascii'); 1391cb0ef41Sopenharmony_ci writeTest.write('e', 3, 'ascii'); 1401cb0ef41Sopenharmony_ci writeTest.write('j', 4, 'ascii'); 1411cb0ef41Sopenharmony_ci assert.strictEqual(writeTest.toString(), 'nodejs'); 1421cb0ef41Sopenharmony_ci} 1431cb0ef41Sopenharmony_ci 1441cb0ef41Sopenharmony_ci// Offset points to the end of the buffer and does not throw. 1451cb0ef41Sopenharmony_ci// (see https://github.com/nodejs/node/issues/8127). 1461cb0ef41Sopenharmony_ciBuffer.alloc(1).write('', 1, 0); 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_ci// ASCII slice test 1491cb0ef41Sopenharmony_ci{ 1501cb0ef41Sopenharmony_ci const asciiString = 'hello world'; 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ci for (let i = 0; i < asciiString.length; i++) { 1531cb0ef41Sopenharmony_ci b[i] = asciiString.charCodeAt(i); 1541cb0ef41Sopenharmony_ci } 1551cb0ef41Sopenharmony_ci const asciiSlice = b.toString('ascii', 0, asciiString.length); 1561cb0ef41Sopenharmony_ci assert.strictEqual(asciiString, asciiSlice); 1571cb0ef41Sopenharmony_ci} 1581cb0ef41Sopenharmony_ci 1591cb0ef41Sopenharmony_ci{ 1601cb0ef41Sopenharmony_ci const asciiString = 'hello world'; 1611cb0ef41Sopenharmony_ci const offset = 100; 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ci assert.strictEqual(asciiString.length, b.write(asciiString, offset, 'ascii')); 1641cb0ef41Sopenharmony_ci const asciiSlice = b.toString('ascii', offset, offset + asciiString.length); 1651cb0ef41Sopenharmony_ci assert.strictEqual(asciiString, asciiSlice); 1661cb0ef41Sopenharmony_ci} 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ci{ 1691cb0ef41Sopenharmony_ci const asciiString = 'hello world'; 1701cb0ef41Sopenharmony_ci const offset = 100; 1711cb0ef41Sopenharmony_ci 1721cb0ef41Sopenharmony_ci const sliceA = b.slice(offset, offset + asciiString.length); 1731cb0ef41Sopenharmony_ci const sliceB = b.slice(offset, offset + asciiString.length); 1741cb0ef41Sopenharmony_ci for (let i = 0; i < asciiString.length; i++) { 1751cb0ef41Sopenharmony_ci assert.strictEqual(sliceA[i], sliceB[i]); 1761cb0ef41Sopenharmony_ci } 1771cb0ef41Sopenharmony_ci} 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ci// UTF-8 slice test 1801cb0ef41Sopenharmony_ci{ 1811cb0ef41Sopenharmony_ci const utf8String = '¡hέlló wôrld!'; 1821cb0ef41Sopenharmony_ci const offset = 100; 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ci b.write(utf8String, 0, Buffer.byteLength(utf8String), 'utf8'); 1851cb0ef41Sopenharmony_ci let utf8Slice = b.toString('utf8', 0, Buffer.byteLength(utf8String)); 1861cb0ef41Sopenharmony_ci assert.strictEqual(utf8String, utf8Slice); 1871cb0ef41Sopenharmony_ci 1881cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.byteLength(utf8String), 1891cb0ef41Sopenharmony_ci b.write(utf8String, offset, 'utf8')); 1901cb0ef41Sopenharmony_ci utf8Slice = b.toString('utf8', offset, 1911cb0ef41Sopenharmony_ci offset + Buffer.byteLength(utf8String)); 1921cb0ef41Sopenharmony_ci assert.strictEqual(utf8String, utf8Slice); 1931cb0ef41Sopenharmony_ci 1941cb0ef41Sopenharmony_ci const sliceA = b.slice(offset, offset + Buffer.byteLength(utf8String)); 1951cb0ef41Sopenharmony_ci const sliceB = b.slice(offset, offset + Buffer.byteLength(utf8String)); 1961cb0ef41Sopenharmony_ci for (let i = 0; i < Buffer.byteLength(utf8String); i++) { 1971cb0ef41Sopenharmony_ci assert.strictEqual(sliceA[i], sliceB[i]); 1981cb0ef41Sopenharmony_ci } 1991cb0ef41Sopenharmony_ci} 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ci{ 2021cb0ef41Sopenharmony_ci const slice = b.slice(100, 150); 2031cb0ef41Sopenharmony_ci assert.strictEqual(slice.length, 50); 2041cb0ef41Sopenharmony_ci for (let i = 0; i < 50; i++) { 2051cb0ef41Sopenharmony_ci assert.strictEqual(b[100 + i], slice[i]); 2061cb0ef41Sopenharmony_ci } 2071cb0ef41Sopenharmony_ci} 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_ci{ 2101cb0ef41Sopenharmony_ci // Make sure only top level parent propagates from allocPool 2111cb0ef41Sopenharmony_ci const b = Buffer.allocUnsafe(5); 2121cb0ef41Sopenharmony_ci const c = b.slice(0, 4); 2131cb0ef41Sopenharmony_ci const d = c.slice(0, 2); 2141cb0ef41Sopenharmony_ci assert.strictEqual(b.parent, c.parent); 2151cb0ef41Sopenharmony_ci assert.strictEqual(b.parent, d.parent); 2161cb0ef41Sopenharmony_ci} 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci{ 2191cb0ef41Sopenharmony_ci // Also from a non-pooled instance 2201cb0ef41Sopenharmony_ci const b = Buffer.allocUnsafeSlow(5); 2211cb0ef41Sopenharmony_ci const c = b.slice(0, 4); 2221cb0ef41Sopenharmony_ci const d = c.slice(0, 2); 2231cb0ef41Sopenharmony_ci assert.strictEqual(c.parent, d.parent); 2241cb0ef41Sopenharmony_ci} 2251cb0ef41Sopenharmony_ci 2261cb0ef41Sopenharmony_ci{ 2271cb0ef41Sopenharmony_ci // Bug regression test 2281cb0ef41Sopenharmony_ci const testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語 2291cb0ef41Sopenharmony_ci const buffer = Buffer.allocUnsafe(32); 2301cb0ef41Sopenharmony_ci const size = buffer.write(testValue, 0, 'utf8'); 2311cb0ef41Sopenharmony_ci const slice = buffer.toString('utf8', 0, size); 2321cb0ef41Sopenharmony_ci assert.strictEqual(slice, testValue); 2331cb0ef41Sopenharmony_ci} 2341cb0ef41Sopenharmony_ci 2351cb0ef41Sopenharmony_ci{ 2361cb0ef41Sopenharmony_ci // Test triple slice 2371cb0ef41Sopenharmony_ci const a = Buffer.allocUnsafe(8); 2381cb0ef41Sopenharmony_ci for (let i = 0; i < 8; i++) a[i] = i; 2391cb0ef41Sopenharmony_ci const b = a.slice(4, 8); 2401cb0ef41Sopenharmony_ci assert.strictEqual(b[0], 4); 2411cb0ef41Sopenharmony_ci assert.strictEqual(b[1], 5); 2421cb0ef41Sopenharmony_ci assert.strictEqual(b[2], 6); 2431cb0ef41Sopenharmony_ci assert.strictEqual(b[3], 7); 2441cb0ef41Sopenharmony_ci const c = b.slice(2, 4); 2451cb0ef41Sopenharmony_ci assert.strictEqual(c[0], 6); 2461cb0ef41Sopenharmony_ci assert.strictEqual(c[1], 7); 2471cb0ef41Sopenharmony_ci} 2481cb0ef41Sopenharmony_ci 2491cb0ef41Sopenharmony_ci{ 2501cb0ef41Sopenharmony_ci const d = Buffer.from([23, 42, 255]); 2511cb0ef41Sopenharmony_ci assert.strictEqual(d.length, 3); 2521cb0ef41Sopenharmony_ci assert.strictEqual(d[0], 23); 2531cb0ef41Sopenharmony_ci assert.strictEqual(d[1], 42); 2541cb0ef41Sopenharmony_ci assert.strictEqual(d[2], 255); 2551cb0ef41Sopenharmony_ci assert.deepStrictEqual(d, Buffer.from(d)); 2561cb0ef41Sopenharmony_ci} 2571cb0ef41Sopenharmony_ci 2581cb0ef41Sopenharmony_ci{ 2591cb0ef41Sopenharmony_ci // Test for proper UTF-8 Encoding 2601cb0ef41Sopenharmony_ci const e = Buffer.from('über'); 2611cb0ef41Sopenharmony_ci assert.deepStrictEqual(e, Buffer.from([195, 188, 98, 101, 114])); 2621cb0ef41Sopenharmony_ci} 2631cb0ef41Sopenharmony_ci 2641cb0ef41Sopenharmony_ci{ 2651cb0ef41Sopenharmony_ci // Test for proper ascii Encoding, length should be 4 2661cb0ef41Sopenharmony_ci const f = Buffer.from('über', 'ascii'); 2671cb0ef41Sopenharmony_ci assert.deepStrictEqual(f, Buffer.from([252, 98, 101, 114])); 2681cb0ef41Sopenharmony_ci} 2691cb0ef41Sopenharmony_ci 2701cb0ef41Sopenharmony_ci['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach((encoding) => { 2711cb0ef41Sopenharmony_ci { 2721cb0ef41Sopenharmony_ci // Test for proper UTF16LE encoding, length should be 8 2731cb0ef41Sopenharmony_ci const f = Buffer.from('über', encoding); 2741cb0ef41Sopenharmony_ci assert.deepStrictEqual(f, Buffer.from([252, 0, 98, 0, 101, 0, 114, 0])); 2751cb0ef41Sopenharmony_ci } 2761cb0ef41Sopenharmony_ci 2771cb0ef41Sopenharmony_ci { 2781cb0ef41Sopenharmony_ci // Length should be 12 2791cb0ef41Sopenharmony_ci const f = Buffer.from('привет', encoding); 2801cb0ef41Sopenharmony_ci assert.deepStrictEqual( 2811cb0ef41Sopenharmony_ci f, Buffer.from([63, 4, 64, 4, 56, 4, 50, 4, 53, 4, 66, 4]) 2821cb0ef41Sopenharmony_ci ); 2831cb0ef41Sopenharmony_ci assert.strictEqual(f.toString(encoding), 'привет'); 2841cb0ef41Sopenharmony_ci } 2851cb0ef41Sopenharmony_ci 2861cb0ef41Sopenharmony_ci { 2871cb0ef41Sopenharmony_ci const f = Buffer.from([0, 0, 0, 0, 0]); 2881cb0ef41Sopenharmony_ci assert.strictEqual(f.length, 5); 2891cb0ef41Sopenharmony_ci const size = f.write('あいうえお', encoding); 2901cb0ef41Sopenharmony_ci assert.strictEqual(size, 4); 2911cb0ef41Sopenharmony_ci assert.deepStrictEqual(f, Buffer.from([0x42, 0x30, 0x44, 0x30, 0x00])); 2921cb0ef41Sopenharmony_ci } 2931cb0ef41Sopenharmony_ci}); 2941cb0ef41Sopenharmony_ci 2951cb0ef41Sopenharmony_ci{ 2961cb0ef41Sopenharmony_ci const f = Buffer.from('\uD83D\uDC4D', 'utf-16le'); // THUMBS UP SIGN (U+1F44D) 2971cb0ef41Sopenharmony_ci assert.strictEqual(f.length, 4); 2981cb0ef41Sopenharmony_ci assert.deepStrictEqual(f, Buffer.from('3DD84DDC', 'hex')); 2991cb0ef41Sopenharmony_ci} 3001cb0ef41Sopenharmony_ci 3011cb0ef41Sopenharmony_ci// Test construction from arrayish object 3021cb0ef41Sopenharmony_ci{ 3031cb0ef41Sopenharmony_ci const arrayIsh = { 0: 0, 1: 1, 2: 2, 3: 3, length: 4 }; 3041cb0ef41Sopenharmony_ci let g = Buffer.from(arrayIsh); 3051cb0ef41Sopenharmony_ci assert.deepStrictEqual(g, Buffer.from([0, 1, 2, 3])); 3061cb0ef41Sopenharmony_ci const strArrayIsh = { 0: '0', 1: '1', 2: '2', 3: '3', length: 4 }; 3071cb0ef41Sopenharmony_ci g = Buffer.from(strArrayIsh); 3081cb0ef41Sopenharmony_ci assert.deepStrictEqual(g, Buffer.from([0, 1, 2, 3])); 3091cb0ef41Sopenharmony_ci} 3101cb0ef41Sopenharmony_ci 3111cb0ef41Sopenharmony_ci// 3121cb0ef41Sopenharmony_ci// Test toString('base64') 3131cb0ef41Sopenharmony_ci// 3141cb0ef41Sopenharmony_ciassert.strictEqual((Buffer.from('Man')).toString('base64'), 'TWFu'); 3151cb0ef41Sopenharmony_ciassert.strictEqual((Buffer.from('Woman')).toString('base64'), 'V29tYW4='); 3161cb0ef41Sopenharmony_ci 3171cb0ef41Sopenharmony_ci// 3181cb0ef41Sopenharmony_ci// Test toString('base64url') 3191cb0ef41Sopenharmony_ci// 3201cb0ef41Sopenharmony_ciassert.strictEqual((Buffer.from('Man')).toString('base64url'), 'TWFu'); 3211cb0ef41Sopenharmony_ciassert.strictEqual((Buffer.from('Woman')).toString('base64url'), 'V29tYW4'); 3221cb0ef41Sopenharmony_ci 3231cb0ef41Sopenharmony_ci{ 3241cb0ef41Sopenharmony_ci // Test that regular and URL-safe base64 both work both ways 3251cb0ef41Sopenharmony_ci const expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff]; 3261cb0ef41Sopenharmony_ci assert.deepStrictEqual(Buffer.from('//++/++/++//', 'base64'), 3271cb0ef41Sopenharmony_ci Buffer.from(expected)); 3281cb0ef41Sopenharmony_ci assert.deepStrictEqual(Buffer.from('__--_--_--__', 'base64'), 3291cb0ef41Sopenharmony_ci Buffer.from(expected)); 3301cb0ef41Sopenharmony_ci assert.deepStrictEqual(Buffer.from('//++/++/++//', 'base64url'), 3311cb0ef41Sopenharmony_ci Buffer.from(expected)); 3321cb0ef41Sopenharmony_ci assert.deepStrictEqual(Buffer.from('__--_--_--__', 'base64url'), 3331cb0ef41Sopenharmony_ci Buffer.from(expected)); 3341cb0ef41Sopenharmony_ci} 3351cb0ef41Sopenharmony_ci 3361cb0ef41Sopenharmony_ciconst base64flavors = ['base64', 'base64url']; 3371cb0ef41Sopenharmony_ci 3381cb0ef41Sopenharmony_ci{ 3391cb0ef41Sopenharmony_ci // Test that regular and URL-safe base64 both work both ways with padding 3401cb0ef41Sopenharmony_ci const expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff, 0xfb]; 3411cb0ef41Sopenharmony_ci assert.deepStrictEqual(Buffer.from('//++/++/++//+w==', 'base64'), 3421cb0ef41Sopenharmony_ci Buffer.from(expected)); 3431cb0ef41Sopenharmony_ci assert.deepStrictEqual(Buffer.from('//++/++/++//+w==', 'base64'), 3441cb0ef41Sopenharmony_ci Buffer.from(expected)); 3451cb0ef41Sopenharmony_ci assert.deepStrictEqual(Buffer.from('//++/++/++//+w==', 'base64url'), 3461cb0ef41Sopenharmony_ci Buffer.from(expected)); 3471cb0ef41Sopenharmony_ci assert.deepStrictEqual(Buffer.from('//++/++/++//+w==', 'base64url'), 3481cb0ef41Sopenharmony_ci Buffer.from(expected)); 3491cb0ef41Sopenharmony_ci} 3501cb0ef41Sopenharmony_ci 3511cb0ef41Sopenharmony_ci{ 3521cb0ef41Sopenharmony_ci // big example 3531cb0ef41Sopenharmony_ci const quote = 'Man is distinguished, not only by his reason, but by this ' + 3541cb0ef41Sopenharmony_ci 'singular passion from other animals, which is a lust ' + 3551cb0ef41Sopenharmony_ci 'of the mind, that by a perseverance of delight in the ' + 3561cb0ef41Sopenharmony_ci 'continued and indefatigable generation of knowledge, ' + 3571cb0ef41Sopenharmony_ci 'exceeds the short vehemence of any carnal pleasure.'; 3581cb0ef41Sopenharmony_ci const expected = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb' + 3591cb0ef41Sopenharmony_ci '24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci' + 3601cb0ef41Sopenharmony_ci 'BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQ' + 3611cb0ef41Sopenharmony_ci 'gYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu' + 3621cb0ef41Sopenharmony_ci 'dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZ' + 3631cb0ef41Sopenharmony_ci 'GdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm' + 3641cb0ef41Sopenharmony_ci '5hbCBwbGVhc3VyZS4='; 3651cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from(quote).toString('base64'), expected); 3661cb0ef41Sopenharmony_ci assert.strictEqual( 3671cb0ef41Sopenharmony_ci Buffer.from(quote).toString('base64url'), 3681cb0ef41Sopenharmony_ci expected.replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '') 3691cb0ef41Sopenharmony_ci ); 3701cb0ef41Sopenharmony_ci 3711cb0ef41Sopenharmony_ci base64flavors.forEach((encoding) => { 3721cb0ef41Sopenharmony_ci let b = Buffer.allocUnsafe(1024); 3731cb0ef41Sopenharmony_ci let bytesWritten = b.write(expected, 0, encoding); 3741cb0ef41Sopenharmony_ci assert.strictEqual(quote.length, bytesWritten); 3751cb0ef41Sopenharmony_ci assert.strictEqual(quote, b.toString('ascii', 0, quote.length)); 3761cb0ef41Sopenharmony_ci 3771cb0ef41Sopenharmony_ci // Check that the base64 decoder ignores whitespace 3781cb0ef41Sopenharmony_ci const expectedWhite = `${expected.slice(0, 60)} \n` + 3791cb0ef41Sopenharmony_ci `${expected.slice(60, 120)} \n` + 3801cb0ef41Sopenharmony_ci `${expected.slice(120, 180)} \n` + 3811cb0ef41Sopenharmony_ci `${expected.slice(180, 240)} \n` + 3821cb0ef41Sopenharmony_ci `${expected.slice(240, 300)}\n` + 3831cb0ef41Sopenharmony_ci `${expected.slice(300, 360)}\n`; 3841cb0ef41Sopenharmony_ci b = Buffer.allocUnsafe(1024); 3851cb0ef41Sopenharmony_ci bytesWritten = b.write(expectedWhite, 0, encoding); 3861cb0ef41Sopenharmony_ci assert.strictEqual(quote.length, bytesWritten); 3871cb0ef41Sopenharmony_ci assert.strictEqual(quote, b.toString('ascii', 0, quote.length)); 3881cb0ef41Sopenharmony_ci 3891cb0ef41Sopenharmony_ci // Check that the base64 decoder on the constructor works 3901cb0ef41Sopenharmony_ci // even in the presence of whitespace. 3911cb0ef41Sopenharmony_ci b = Buffer.from(expectedWhite, encoding); 3921cb0ef41Sopenharmony_ci assert.strictEqual(quote.length, b.length); 3931cb0ef41Sopenharmony_ci assert.strictEqual(quote, b.toString('ascii', 0, quote.length)); 3941cb0ef41Sopenharmony_ci 3951cb0ef41Sopenharmony_ci // Check that the base64 decoder ignores illegal chars 3961cb0ef41Sopenharmony_ci const expectedIllegal = expected.slice(0, 60) + ' \x80' + 3971cb0ef41Sopenharmony_ci expected.slice(60, 120) + ' \xff' + 3981cb0ef41Sopenharmony_ci expected.slice(120, 180) + ' \x00' + 3991cb0ef41Sopenharmony_ci expected.slice(180, 240) + ' \x98' + 4001cb0ef41Sopenharmony_ci expected.slice(240, 300) + '\x03' + 4011cb0ef41Sopenharmony_ci expected.slice(300, 360); 4021cb0ef41Sopenharmony_ci b = Buffer.from(expectedIllegal, encoding); 4031cb0ef41Sopenharmony_ci assert.strictEqual(quote.length, b.length); 4041cb0ef41Sopenharmony_ci assert.strictEqual(quote, b.toString('ascii', 0, quote.length)); 4051cb0ef41Sopenharmony_ci }); 4061cb0ef41Sopenharmony_ci} 4071cb0ef41Sopenharmony_ci 4081cb0ef41Sopenharmony_cibase64flavors.forEach((encoding) => { 4091cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('', encoding).toString(), ''); 4101cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('K', encoding).toString(), ''); 4111cb0ef41Sopenharmony_ci 4121cb0ef41Sopenharmony_ci // multiple-of-4 with padding 4131cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('Kg==', encoding).toString(), '*'); 4141cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('Kio=', encoding).toString(), '*'.repeat(2)); 4151cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('Kioq', encoding).toString(), '*'.repeat(3)); 4161cb0ef41Sopenharmony_ci assert.strictEqual( 4171cb0ef41Sopenharmony_ci Buffer.from('KioqKg==', encoding).toString(), '*'.repeat(4)); 4181cb0ef41Sopenharmony_ci assert.strictEqual( 4191cb0ef41Sopenharmony_ci Buffer.from('KioqKio=', encoding).toString(), '*'.repeat(5)); 4201cb0ef41Sopenharmony_ci assert.strictEqual( 4211cb0ef41Sopenharmony_ci Buffer.from('KioqKioq', encoding).toString(), '*'.repeat(6)); 4221cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKg==', encoding).toString(), 4231cb0ef41Sopenharmony_ci '*'.repeat(7)); 4241cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKio=', encoding).toString(), 4251cb0ef41Sopenharmony_ci '*'.repeat(8)); 4261cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKioq', encoding).toString(), 4271cb0ef41Sopenharmony_ci '*'.repeat(9)); 4281cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKioqKg==', encoding).toString(), 4291cb0ef41Sopenharmony_ci '*'.repeat(10)); 4301cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKioqKio=', encoding).toString(), 4311cb0ef41Sopenharmony_ci '*'.repeat(11)); 4321cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKioqKioq', encoding).toString(), 4331cb0ef41Sopenharmony_ci '*'.repeat(12)); 4341cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKioqKioqKg==', encoding).toString(), 4351cb0ef41Sopenharmony_ci '*'.repeat(13)); 4361cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKioqKioqKio=', encoding).toString(), 4371cb0ef41Sopenharmony_ci '*'.repeat(14)); 4381cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioq', encoding).toString(), 4391cb0ef41Sopenharmony_ci '*'.repeat(15)); 4401cb0ef41Sopenharmony_ci assert.strictEqual( 4411cb0ef41Sopenharmony_ci Buffer.from('KioqKioqKioqKioqKioqKg==', encoding).toString(), 4421cb0ef41Sopenharmony_ci '*'.repeat(16)); 4431cb0ef41Sopenharmony_ci assert.strictEqual( 4441cb0ef41Sopenharmony_ci Buffer.from('KioqKioqKioqKioqKioqKio=', encoding).toString(), 4451cb0ef41Sopenharmony_ci '*'.repeat(17)); 4461cb0ef41Sopenharmony_ci assert.strictEqual( 4471cb0ef41Sopenharmony_ci Buffer.from('KioqKioqKioqKioqKioqKioq', encoding).toString(), 4481cb0ef41Sopenharmony_ci '*'.repeat(18)); 4491cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKioqKg==', 4501cb0ef41Sopenharmony_ci encoding).toString(), 4511cb0ef41Sopenharmony_ci '*'.repeat(19)); 4521cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKioqKio=', 4531cb0ef41Sopenharmony_ci encoding).toString(), 4541cb0ef41Sopenharmony_ci '*'.repeat(20)); 4551cb0ef41Sopenharmony_ci 4561cb0ef41Sopenharmony_ci // No padding, not a multiple of 4 4571cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('Kg', encoding).toString(), '*'); 4581cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('Kio', encoding).toString(), '*'.repeat(2)); 4591cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKg', encoding).toString(), '*'.repeat(4)); 4601cb0ef41Sopenharmony_ci assert.strictEqual( 4611cb0ef41Sopenharmony_ci Buffer.from('KioqKio', encoding).toString(), '*'.repeat(5)); 4621cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKg', encoding).toString(), 4631cb0ef41Sopenharmony_ci '*'.repeat(7)); 4641cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKio', encoding).toString(), 4651cb0ef41Sopenharmony_ci '*'.repeat(8)); 4661cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKioqKg', encoding).toString(), 4671cb0ef41Sopenharmony_ci '*'.repeat(10)); 4681cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKioqKio', encoding).toString(), 4691cb0ef41Sopenharmony_ci '*'.repeat(11)); 4701cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKioqKioqKg', encoding).toString(), 4711cb0ef41Sopenharmony_ci '*'.repeat(13)); 4721cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKioqKioqKio', encoding).toString(), 4731cb0ef41Sopenharmony_ci '*'.repeat(14)); 4741cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKg', encoding).toString(), 4751cb0ef41Sopenharmony_ci '*'.repeat(16)); 4761cb0ef41Sopenharmony_ci assert.strictEqual( 4771cb0ef41Sopenharmony_ci Buffer.from('KioqKioqKioqKioqKioqKio', encoding).toString(), 4781cb0ef41Sopenharmony_ci '*'.repeat(17)); 4791cb0ef41Sopenharmony_ci assert.strictEqual( 4801cb0ef41Sopenharmony_ci Buffer.from('KioqKioqKioqKioqKioqKioqKg', encoding).toString(), 4811cb0ef41Sopenharmony_ci '*'.repeat(19)); 4821cb0ef41Sopenharmony_ci assert.strictEqual( 4831cb0ef41Sopenharmony_ci Buffer.from('KioqKioqKioqKioqKioqKioqKio', encoding).toString(), 4841cb0ef41Sopenharmony_ci '*'.repeat(20)); 4851cb0ef41Sopenharmony_ci}); 4861cb0ef41Sopenharmony_ci 4871cb0ef41Sopenharmony_ci// Handle padding graciously, multiple-of-4 or not 4881cb0ef41Sopenharmony_ciassert.strictEqual( 4891cb0ef41Sopenharmony_ci Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw==', 'base64').length, 4901cb0ef41Sopenharmony_ci 32 4911cb0ef41Sopenharmony_ci); 4921cb0ef41Sopenharmony_ciassert.strictEqual( 4931cb0ef41Sopenharmony_ci Buffer.from('72INjkR5fchcxk9-VgdGPFJDxUBFR5_rMFsghgxADiw==', 'base64url') 4941cb0ef41Sopenharmony_ci .length, 4951cb0ef41Sopenharmony_ci 32 4961cb0ef41Sopenharmony_ci); 4971cb0ef41Sopenharmony_ciassert.strictEqual( 4981cb0ef41Sopenharmony_ci Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw=', 'base64').length, 4991cb0ef41Sopenharmony_ci 32 5001cb0ef41Sopenharmony_ci); 5011cb0ef41Sopenharmony_ciassert.strictEqual( 5021cb0ef41Sopenharmony_ci Buffer.from('72INjkR5fchcxk9-VgdGPFJDxUBFR5_rMFsghgxADiw=', 'base64url') 5031cb0ef41Sopenharmony_ci .length, 5041cb0ef41Sopenharmony_ci 32 5051cb0ef41Sopenharmony_ci); 5061cb0ef41Sopenharmony_ciassert.strictEqual( 5071cb0ef41Sopenharmony_ci Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw', 'base64').length, 5081cb0ef41Sopenharmony_ci 32 5091cb0ef41Sopenharmony_ci); 5101cb0ef41Sopenharmony_ciassert.strictEqual( 5111cb0ef41Sopenharmony_ci Buffer.from('72INjkR5fchcxk9-VgdGPFJDxUBFR5_rMFsghgxADiw', 'base64url') 5121cb0ef41Sopenharmony_ci .length, 5131cb0ef41Sopenharmony_ci 32 5141cb0ef41Sopenharmony_ci); 5151cb0ef41Sopenharmony_ciassert.strictEqual( 5161cb0ef41Sopenharmony_ci Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg==', 'base64').length, 5171cb0ef41Sopenharmony_ci 31 5181cb0ef41Sopenharmony_ci); 5191cb0ef41Sopenharmony_ciassert.strictEqual( 5201cb0ef41Sopenharmony_ci Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg==', 'base64url') 5211cb0ef41Sopenharmony_ci .length, 5221cb0ef41Sopenharmony_ci 31 5231cb0ef41Sopenharmony_ci); 5241cb0ef41Sopenharmony_ciassert.strictEqual( 5251cb0ef41Sopenharmony_ci Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg=', 'base64').length, 5261cb0ef41Sopenharmony_ci 31 5271cb0ef41Sopenharmony_ci); 5281cb0ef41Sopenharmony_ciassert.strictEqual( 5291cb0ef41Sopenharmony_ci Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg=', 'base64url') 5301cb0ef41Sopenharmony_ci .length, 5311cb0ef41Sopenharmony_ci 31 5321cb0ef41Sopenharmony_ci); 5331cb0ef41Sopenharmony_ciassert.strictEqual( 5341cb0ef41Sopenharmony_ci Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg', 'base64').length, 5351cb0ef41Sopenharmony_ci 31 5361cb0ef41Sopenharmony_ci); 5371cb0ef41Sopenharmony_ciassert.strictEqual( 5381cb0ef41Sopenharmony_ci Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg', 'base64url').length, 5391cb0ef41Sopenharmony_ci 31 5401cb0ef41Sopenharmony_ci); 5411cb0ef41Sopenharmony_ci 5421cb0ef41Sopenharmony_ci{ 5431cb0ef41Sopenharmony_ci// This string encodes single '.' character in UTF-16 5441cb0ef41Sopenharmony_ci const dot = Buffer.from('//4uAA==', 'base64'); 5451cb0ef41Sopenharmony_ci assert.strictEqual(dot[0], 0xff); 5461cb0ef41Sopenharmony_ci assert.strictEqual(dot[1], 0xfe); 5471cb0ef41Sopenharmony_ci assert.strictEqual(dot[2], 0x2e); 5481cb0ef41Sopenharmony_ci assert.strictEqual(dot[3], 0x00); 5491cb0ef41Sopenharmony_ci assert.strictEqual(dot.toString('base64'), '//4uAA=='); 5501cb0ef41Sopenharmony_ci} 5511cb0ef41Sopenharmony_ci 5521cb0ef41Sopenharmony_ci{ 5531cb0ef41Sopenharmony_ci// This string encodes single '.' character in UTF-16 5541cb0ef41Sopenharmony_ci const dot = Buffer.from('//4uAA', 'base64url'); 5551cb0ef41Sopenharmony_ci assert.strictEqual(dot[0], 0xff); 5561cb0ef41Sopenharmony_ci assert.strictEqual(dot[1], 0xfe); 5571cb0ef41Sopenharmony_ci assert.strictEqual(dot[2], 0x2e); 5581cb0ef41Sopenharmony_ci assert.strictEqual(dot[3], 0x00); 5591cb0ef41Sopenharmony_ci assert.strictEqual(dot.toString('base64url'), '__4uAA'); 5601cb0ef41Sopenharmony_ci} 5611cb0ef41Sopenharmony_ci 5621cb0ef41Sopenharmony_ci{ 5631cb0ef41Sopenharmony_ci // Writing base64 at a position > 0 should not mangle the result. 5641cb0ef41Sopenharmony_ci // 5651cb0ef41Sopenharmony_ci // https://github.com/joyent/node/issues/402 5661cb0ef41Sopenharmony_ci const segments = ['TWFkbmVzcz8h', 'IFRoaXM=', 'IGlz', 'IG5vZGUuanMh']; 5671cb0ef41Sopenharmony_ci const b = Buffer.allocUnsafe(64); 5681cb0ef41Sopenharmony_ci let pos = 0; 5691cb0ef41Sopenharmony_ci 5701cb0ef41Sopenharmony_ci for (let i = 0; i < segments.length; ++i) { 5711cb0ef41Sopenharmony_ci pos += b.write(segments[i], pos, 'base64'); 5721cb0ef41Sopenharmony_ci } 5731cb0ef41Sopenharmony_ci assert.strictEqual(b.toString('latin1', 0, pos), 5741cb0ef41Sopenharmony_ci 'Madness?! This is node.js!'); 5751cb0ef41Sopenharmony_ci} 5761cb0ef41Sopenharmony_ci 5771cb0ef41Sopenharmony_ci{ 5781cb0ef41Sopenharmony_ci // Writing base64url at a position > 0 should not mangle the result. 5791cb0ef41Sopenharmony_ci // 5801cb0ef41Sopenharmony_ci // https://github.com/joyent/node/issues/402 5811cb0ef41Sopenharmony_ci const segments = ['TWFkbmVzcz8h', 'IFRoaXM', 'IGlz', 'IG5vZGUuanMh']; 5821cb0ef41Sopenharmony_ci const b = Buffer.allocUnsafe(64); 5831cb0ef41Sopenharmony_ci let pos = 0; 5841cb0ef41Sopenharmony_ci 5851cb0ef41Sopenharmony_ci for (let i = 0; i < segments.length; ++i) { 5861cb0ef41Sopenharmony_ci pos += b.write(segments[i], pos, 'base64url'); 5871cb0ef41Sopenharmony_ci } 5881cb0ef41Sopenharmony_ci assert.strictEqual(b.toString('latin1', 0, pos), 5891cb0ef41Sopenharmony_ci 'Madness?! This is node.js!'); 5901cb0ef41Sopenharmony_ci} 5911cb0ef41Sopenharmony_ci 5921cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node/issues/3496. 5931cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('=bad'.repeat(1e4), 'base64').length, 0); 5941cb0ef41Sopenharmony_ci 5951cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node/issues/11987. 5961cb0ef41Sopenharmony_ciassert.deepStrictEqual(Buffer.from('w0 ', 'base64'), 5971cb0ef41Sopenharmony_ci Buffer.from('w0', 'base64')); 5981cb0ef41Sopenharmony_ci 5991cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node/issues/13657. 6001cb0ef41Sopenharmony_ciassert.deepStrictEqual(Buffer.from(' YWJvcnVtLg', 'base64'), 6011cb0ef41Sopenharmony_ci Buffer.from('YWJvcnVtLg', 'base64')); 6021cb0ef41Sopenharmony_ci 6031cb0ef41Sopenharmony_ci{ 6041cb0ef41Sopenharmony_ci // Creating buffers larger than pool size. 6051cb0ef41Sopenharmony_ci const l = Buffer.poolSize + 5; 6061cb0ef41Sopenharmony_ci const s = 'h'.repeat(l); 6071cb0ef41Sopenharmony_ci const b = Buffer.from(s); 6081cb0ef41Sopenharmony_ci 6091cb0ef41Sopenharmony_ci for (let i = 0; i < l; i++) { 6101cb0ef41Sopenharmony_ci assert.strictEqual(b[i], 'h'.charCodeAt(0)); 6111cb0ef41Sopenharmony_ci } 6121cb0ef41Sopenharmony_ci 6131cb0ef41Sopenharmony_ci const sb = b.toString(); 6141cb0ef41Sopenharmony_ci assert.strictEqual(sb.length, s.length); 6151cb0ef41Sopenharmony_ci assert.strictEqual(sb, s); 6161cb0ef41Sopenharmony_ci} 6171cb0ef41Sopenharmony_ci 6181cb0ef41Sopenharmony_ci{ 6191cb0ef41Sopenharmony_ci // test hex toString 6201cb0ef41Sopenharmony_ci const hexb = Buffer.allocUnsafe(256); 6211cb0ef41Sopenharmony_ci for (let i = 0; i < 256; i++) { 6221cb0ef41Sopenharmony_ci hexb[i] = i; 6231cb0ef41Sopenharmony_ci } 6241cb0ef41Sopenharmony_ci const hexStr = hexb.toString('hex'); 6251cb0ef41Sopenharmony_ci assert.strictEqual(hexStr, 6261cb0ef41Sopenharmony_ci '000102030405060708090a0b0c0d0e0f' + 6271cb0ef41Sopenharmony_ci '101112131415161718191a1b1c1d1e1f' + 6281cb0ef41Sopenharmony_ci '202122232425262728292a2b2c2d2e2f' + 6291cb0ef41Sopenharmony_ci '303132333435363738393a3b3c3d3e3f' + 6301cb0ef41Sopenharmony_ci '404142434445464748494a4b4c4d4e4f' + 6311cb0ef41Sopenharmony_ci '505152535455565758595a5b5c5d5e5f' + 6321cb0ef41Sopenharmony_ci '606162636465666768696a6b6c6d6e6f' + 6331cb0ef41Sopenharmony_ci '707172737475767778797a7b7c7d7e7f' + 6341cb0ef41Sopenharmony_ci '808182838485868788898a8b8c8d8e8f' + 6351cb0ef41Sopenharmony_ci '909192939495969798999a9b9c9d9e9f' + 6361cb0ef41Sopenharmony_ci 'a0a1a2a3a4a5a6a7a8a9aaabacadaeaf' + 6371cb0ef41Sopenharmony_ci 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' + 6381cb0ef41Sopenharmony_ci 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf' + 6391cb0ef41Sopenharmony_ci 'd0d1d2d3d4d5d6d7d8d9dadbdcdddedf' + 6401cb0ef41Sopenharmony_ci 'e0e1e2e3e4e5e6e7e8e9eaebecedeeef' + 6411cb0ef41Sopenharmony_ci 'f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'); 6421cb0ef41Sopenharmony_ci 6431cb0ef41Sopenharmony_ci const hexb2 = Buffer.from(hexStr, 'hex'); 6441cb0ef41Sopenharmony_ci for (let i = 0; i < 256; i++) { 6451cb0ef41Sopenharmony_ci assert.strictEqual(hexb2[i], hexb[i]); 6461cb0ef41Sopenharmony_ci } 6471cb0ef41Sopenharmony_ci} 6481cb0ef41Sopenharmony_ci 6491cb0ef41Sopenharmony_ci// Test single hex character is discarded. 6501cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('A', 'hex').length, 0); 6511cb0ef41Sopenharmony_ci 6521cb0ef41Sopenharmony_ci// Test that if a trailing character is discarded, rest of string is processed. 6531cb0ef41Sopenharmony_ciassert.deepStrictEqual(Buffer.from('Abx', 'hex'), Buffer.from('Ab', 'hex')); 6541cb0ef41Sopenharmony_ci 6551cb0ef41Sopenharmony_ci// Test single base64 char encodes as 0. 6561cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('A', 'base64').length, 0); 6571cb0ef41Sopenharmony_ci 6581cb0ef41Sopenharmony_ci 6591cb0ef41Sopenharmony_ci{ 6601cb0ef41Sopenharmony_ci // Test an invalid slice end. 6611cb0ef41Sopenharmony_ci const b = Buffer.from([1, 2, 3, 4, 5]); 6621cb0ef41Sopenharmony_ci const b2 = b.toString('hex', 1, 10000); 6631cb0ef41Sopenharmony_ci const b3 = b.toString('hex', 1, 5); 6641cb0ef41Sopenharmony_ci const b4 = b.toString('hex', 1); 6651cb0ef41Sopenharmony_ci assert.strictEqual(b2, b3); 6661cb0ef41Sopenharmony_ci assert.strictEqual(b2, b4); 6671cb0ef41Sopenharmony_ci} 6681cb0ef41Sopenharmony_ci 6691cb0ef41Sopenharmony_cifunction buildBuffer(data) { 6701cb0ef41Sopenharmony_ci if (Array.isArray(data)) { 6711cb0ef41Sopenharmony_ci const buffer = Buffer.allocUnsafe(data.length); 6721cb0ef41Sopenharmony_ci data.forEach((v, k) => buffer[k] = v); 6731cb0ef41Sopenharmony_ci return buffer; 6741cb0ef41Sopenharmony_ci } 6751cb0ef41Sopenharmony_ci return null; 6761cb0ef41Sopenharmony_ci} 6771cb0ef41Sopenharmony_ci 6781cb0ef41Sopenharmony_ciconst x = buildBuffer([0x81, 0xa3, 0x66, 0x6f, 0x6f, 0xa3, 0x62, 0x61, 0x72]); 6791cb0ef41Sopenharmony_ci 6801cb0ef41Sopenharmony_ciassert.strictEqual(x.inspect(), '<Buffer 81 a3 66 6f 6f a3 62 61 72>'); 6811cb0ef41Sopenharmony_ci 6821cb0ef41Sopenharmony_ci{ 6831cb0ef41Sopenharmony_ci const z = x.slice(4); 6841cb0ef41Sopenharmony_ci assert.strictEqual(z.length, 5); 6851cb0ef41Sopenharmony_ci assert.strictEqual(z[0], 0x6f); 6861cb0ef41Sopenharmony_ci assert.strictEqual(z[1], 0xa3); 6871cb0ef41Sopenharmony_ci assert.strictEqual(z[2], 0x62); 6881cb0ef41Sopenharmony_ci assert.strictEqual(z[3], 0x61); 6891cb0ef41Sopenharmony_ci assert.strictEqual(z[4], 0x72); 6901cb0ef41Sopenharmony_ci} 6911cb0ef41Sopenharmony_ci 6921cb0ef41Sopenharmony_ci{ 6931cb0ef41Sopenharmony_ci const z = x.slice(0); 6941cb0ef41Sopenharmony_ci assert.strictEqual(z.length, x.length); 6951cb0ef41Sopenharmony_ci} 6961cb0ef41Sopenharmony_ci 6971cb0ef41Sopenharmony_ci{ 6981cb0ef41Sopenharmony_ci const z = x.slice(0, 4); 6991cb0ef41Sopenharmony_ci assert.strictEqual(z.length, 4); 7001cb0ef41Sopenharmony_ci assert.strictEqual(z[0], 0x81); 7011cb0ef41Sopenharmony_ci assert.strictEqual(z[1], 0xa3); 7021cb0ef41Sopenharmony_ci} 7031cb0ef41Sopenharmony_ci 7041cb0ef41Sopenharmony_ci{ 7051cb0ef41Sopenharmony_ci const z = x.slice(0, 9); 7061cb0ef41Sopenharmony_ci assert.strictEqual(z.length, 9); 7071cb0ef41Sopenharmony_ci} 7081cb0ef41Sopenharmony_ci 7091cb0ef41Sopenharmony_ci{ 7101cb0ef41Sopenharmony_ci const z = x.slice(1, 4); 7111cb0ef41Sopenharmony_ci assert.strictEqual(z.length, 3); 7121cb0ef41Sopenharmony_ci assert.strictEqual(z[0], 0xa3); 7131cb0ef41Sopenharmony_ci} 7141cb0ef41Sopenharmony_ci 7151cb0ef41Sopenharmony_ci{ 7161cb0ef41Sopenharmony_ci const z = x.slice(2, 4); 7171cb0ef41Sopenharmony_ci assert.strictEqual(z.length, 2); 7181cb0ef41Sopenharmony_ci assert.strictEqual(z[0], 0x66); 7191cb0ef41Sopenharmony_ci assert.strictEqual(z[1], 0x6f); 7201cb0ef41Sopenharmony_ci} 7211cb0ef41Sopenharmony_ci 7221cb0ef41Sopenharmony_ci['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach((encoding) => { 7231cb0ef41Sopenharmony_ci const b = Buffer.allocUnsafe(10); 7241cb0ef41Sopenharmony_ci b.write('あいうえお', encoding); 7251cb0ef41Sopenharmony_ci assert.strictEqual(b.toString(encoding), 'あいうえお'); 7261cb0ef41Sopenharmony_ci}); 7271cb0ef41Sopenharmony_ci 7281cb0ef41Sopenharmony_ci['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach((encoding) => { 7291cb0ef41Sopenharmony_ci const b = Buffer.allocUnsafe(11); 7301cb0ef41Sopenharmony_ci b.write('あいうえお', 1, encoding); 7311cb0ef41Sopenharmony_ci assert.strictEqual(b.toString(encoding, 1), 'あいうえお'); 7321cb0ef41Sopenharmony_ci}); 7331cb0ef41Sopenharmony_ci 7341cb0ef41Sopenharmony_ci{ 7351cb0ef41Sopenharmony_ci // latin1 encoding should write only one byte per character. 7361cb0ef41Sopenharmony_ci const b = Buffer.from([0xde, 0xad, 0xbe, 0xef]); 7371cb0ef41Sopenharmony_ci let s = String.fromCharCode(0xffff); 7381cb0ef41Sopenharmony_ci b.write(s, 0, 'latin1'); 7391cb0ef41Sopenharmony_ci assert.strictEqual(b[0], 0xff); 7401cb0ef41Sopenharmony_ci assert.strictEqual(b[1], 0xad); 7411cb0ef41Sopenharmony_ci assert.strictEqual(b[2], 0xbe); 7421cb0ef41Sopenharmony_ci assert.strictEqual(b[3], 0xef); 7431cb0ef41Sopenharmony_ci s = String.fromCharCode(0xaaee); 7441cb0ef41Sopenharmony_ci b.write(s, 0, 'latin1'); 7451cb0ef41Sopenharmony_ci assert.strictEqual(b[0], 0xee); 7461cb0ef41Sopenharmony_ci assert.strictEqual(b[1], 0xad); 7471cb0ef41Sopenharmony_ci assert.strictEqual(b[2], 0xbe); 7481cb0ef41Sopenharmony_ci assert.strictEqual(b[3], 0xef); 7491cb0ef41Sopenharmony_ci} 7501cb0ef41Sopenharmony_ci 7511cb0ef41Sopenharmony_ci{ 7521cb0ef41Sopenharmony_ci // Binary encoding should write only one byte per character. 7531cb0ef41Sopenharmony_ci const b = Buffer.from([0xde, 0xad, 0xbe, 0xef]); 7541cb0ef41Sopenharmony_ci let s = String.fromCharCode(0xffff); 7551cb0ef41Sopenharmony_ci b.write(s, 0, 'latin1'); 7561cb0ef41Sopenharmony_ci assert.strictEqual(b[0], 0xff); 7571cb0ef41Sopenharmony_ci assert.strictEqual(b[1], 0xad); 7581cb0ef41Sopenharmony_ci assert.strictEqual(b[2], 0xbe); 7591cb0ef41Sopenharmony_ci assert.strictEqual(b[3], 0xef); 7601cb0ef41Sopenharmony_ci s = String.fromCharCode(0xaaee); 7611cb0ef41Sopenharmony_ci b.write(s, 0, 'latin1'); 7621cb0ef41Sopenharmony_ci assert.strictEqual(b[0], 0xee); 7631cb0ef41Sopenharmony_ci assert.strictEqual(b[1], 0xad); 7641cb0ef41Sopenharmony_ci assert.strictEqual(b[2], 0xbe); 7651cb0ef41Sopenharmony_ci assert.strictEqual(b[3], 0xef); 7661cb0ef41Sopenharmony_ci} 7671cb0ef41Sopenharmony_ci 7681cb0ef41Sopenharmony_ci{ 7691cb0ef41Sopenharmony_ci // https://github.com/nodejs/node-v0.x-archive/pull/1210 7701cb0ef41Sopenharmony_ci // Test UTF-8 string includes null character 7711cb0ef41Sopenharmony_ci let buf = Buffer.from('\0'); 7721cb0ef41Sopenharmony_ci assert.strictEqual(buf.length, 1); 7731cb0ef41Sopenharmony_ci buf = Buffer.from('\0\0'); 7741cb0ef41Sopenharmony_ci assert.strictEqual(buf.length, 2); 7751cb0ef41Sopenharmony_ci} 7761cb0ef41Sopenharmony_ci 7771cb0ef41Sopenharmony_ci{ 7781cb0ef41Sopenharmony_ci const buf = Buffer.allocUnsafe(2); 7791cb0ef41Sopenharmony_ci assert.strictEqual(buf.write(''), 0); // 0bytes 7801cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('\0'), 1); // 1byte (v8 adds null terminator) 7811cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('a\0'), 2); // 1byte * 2 7821cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('あ'), 0); // 3bytes 7831cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('\0あ'), 1); // 1byte + 3bytes 7841cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('\0\0あ'), 2); // 1byte * 2 + 3bytes 7851cb0ef41Sopenharmony_ci} 7861cb0ef41Sopenharmony_ci 7871cb0ef41Sopenharmony_ci{ 7881cb0ef41Sopenharmony_ci const buf = Buffer.allocUnsafe(10); 7891cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('あいう'), 9); // 3bytes * 3 (v8 adds null term.) 7901cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('あいう\0'), 10); // 3bytes * 3 + 1byte 7911cb0ef41Sopenharmony_ci} 7921cb0ef41Sopenharmony_ci 7931cb0ef41Sopenharmony_ci{ 7941cb0ef41Sopenharmony_ci // https://github.com/nodejs/node-v0.x-archive/issues/243 7951cb0ef41Sopenharmony_ci // Test write() with maxLength 7961cb0ef41Sopenharmony_ci const buf = Buffer.allocUnsafe(4); 7971cb0ef41Sopenharmony_ci buf.fill(0xFF); 7981cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('abcd', 1, 2, 'utf8'), 2); 7991cb0ef41Sopenharmony_ci assert.strictEqual(buf[0], 0xFF); 8001cb0ef41Sopenharmony_ci assert.strictEqual(buf[1], 0x61); 8011cb0ef41Sopenharmony_ci assert.strictEqual(buf[2], 0x62); 8021cb0ef41Sopenharmony_ci assert.strictEqual(buf[3], 0xFF); 8031cb0ef41Sopenharmony_ci 8041cb0ef41Sopenharmony_ci buf.fill(0xFF); 8051cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('abcd', 1, 4), 3); 8061cb0ef41Sopenharmony_ci assert.strictEqual(buf[0], 0xFF); 8071cb0ef41Sopenharmony_ci assert.strictEqual(buf[1], 0x61); 8081cb0ef41Sopenharmony_ci assert.strictEqual(buf[2], 0x62); 8091cb0ef41Sopenharmony_ci assert.strictEqual(buf[3], 0x63); 8101cb0ef41Sopenharmony_ci 8111cb0ef41Sopenharmony_ci buf.fill(0xFF); 8121cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('abcd', 1, 2, 'utf8'), 2); 8131cb0ef41Sopenharmony_ci assert.strictEqual(buf[0], 0xFF); 8141cb0ef41Sopenharmony_ci assert.strictEqual(buf[1], 0x61); 8151cb0ef41Sopenharmony_ci assert.strictEqual(buf[2], 0x62); 8161cb0ef41Sopenharmony_ci assert.strictEqual(buf[3], 0xFF); 8171cb0ef41Sopenharmony_ci 8181cb0ef41Sopenharmony_ci buf.fill(0xFF); 8191cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('abcdef', 1, 2, 'hex'), 2); 8201cb0ef41Sopenharmony_ci assert.strictEqual(buf[0], 0xFF); 8211cb0ef41Sopenharmony_ci assert.strictEqual(buf[1], 0xAB); 8221cb0ef41Sopenharmony_ci assert.strictEqual(buf[2], 0xCD); 8231cb0ef41Sopenharmony_ci assert.strictEqual(buf[3], 0xFF); 8241cb0ef41Sopenharmony_ci 8251cb0ef41Sopenharmony_ci ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach((encoding) => { 8261cb0ef41Sopenharmony_ci buf.fill(0xFF); 8271cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('abcd', 0, 2, encoding), 2); 8281cb0ef41Sopenharmony_ci assert.strictEqual(buf[0], 0x61); 8291cb0ef41Sopenharmony_ci assert.strictEqual(buf[1], 0x00); 8301cb0ef41Sopenharmony_ci assert.strictEqual(buf[2], 0xFF); 8311cb0ef41Sopenharmony_ci assert.strictEqual(buf[3], 0xFF); 8321cb0ef41Sopenharmony_ci }); 8331cb0ef41Sopenharmony_ci} 8341cb0ef41Sopenharmony_ci 8351cb0ef41Sopenharmony_ci{ 8361cb0ef41Sopenharmony_ci // Test offset returns are correct 8371cb0ef41Sopenharmony_ci const b = Buffer.allocUnsafe(16); 8381cb0ef41Sopenharmony_ci assert.strictEqual(b.writeUInt32LE(0, 0), 4); 8391cb0ef41Sopenharmony_ci assert.strictEqual(b.writeUInt16LE(0, 4), 6); 8401cb0ef41Sopenharmony_ci assert.strictEqual(b.writeUInt8(0, 6), 7); 8411cb0ef41Sopenharmony_ci assert.strictEqual(b.writeInt8(0, 7), 8); 8421cb0ef41Sopenharmony_ci assert.strictEqual(b.writeDoubleLE(0, 8), 16); 8431cb0ef41Sopenharmony_ci} 8441cb0ef41Sopenharmony_ci 8451cb0ef41Sopenharmony_ci{ 8461cb0ef41Sopenharmony_ci // Test unmatched surrogates not producing invalid utf8 output 8471cb0ef41Sopenharmony_ci // ef bf bd = utf-8 representation of unicode replacement character 8481cb0ef41Sopenharmony_ci // see https://codereview.chromium.org/121173009/ 8491cb0ef41Sopenharmony_ci const buf = Buffer.from('ab\ud800cd', 'utf8'); 8501cb0ef41Sopenharmony_ci assert.strictEqual(buf[0], 0x61); 8511cb0ef41Sopenharmony_ci assert.strictEqual(buf[1], 0x62); 8521cb0ef41Sopenharmony_ci assert.strictEqual(buf[2], 0xef); 8531cb0ef41Sopenharmony_ci assert.strictEqual(buf[3], 0xbf); 8541cb0ef41Sopenharmony_ci assert.strictEqual(buf[4], 0xbd); 8551cb0ef41Sopenharmony_ci assert.strictEqual(buf[5], 0x63); 8561cb0ef41Sopenharmony_ci assert.strictEqual(buf[6], 0x64); 8571cb0ef41Sopenharmony_ci} 8581cb0ef41Sopenharmony_ci 8591cb0ef41Sopenharmony_ci{ 8601cb0ef41Sopenharmony_ci // Test for buffer overrun 8611cb0ef41Sopenharmony_ci const buf = Buffer.from([0, 0, 0, 0, 0]); // length: 5 8621cb0ef41Sopenharmony_ci const sub = buf.slice(0, 4); // length: 4 8631cb0ef41Sopenharmony_ci assert.strictEqual(sub.write('12345', 'latin1'), 4); 8641cb0ef41Sopenharmony_ci assert.strictEqual(buf[4], 0); 8651cb0ef41Sopenharmony_ci assert.strictEqual(sub.write('12345', 'binary'), 4); 8661cb0ef41Sopenharmony_ci assert.strictEqual(buf[4], 0); 8671cb0ef41Sopenharmony_ci} 8681cb0ef41Sopenharmony_ci 8691cb0ef41Sopenharmony_ci{ 8701cb0ef41Sopenharmony_ci // Test alloc with fill option 8711cb0ef41Sopenharmony_ci const buf = Buffer.alloc(5, '800A', 'hex'); 8721cb0ef41Sopenharmony_ci assert.strictEqual(buf[0], 128); 8731cb0ef41Sopenharmony_ci assert.strictEqual(buf[1], 10); 8741cb0ef41Sopenharmony_ci assert.strictEqual(buf[2], 128); 8751cb0ef41Sopenharmony_ci assert.strictEqual(buf[3], 10); 8761cb0ef41Sopenharmony_ci assert.strictEqual(buf[4], 128); 8771cb0ef41Sopenharmony_ci} 8781cb0ef41Sopenharmony_ci 8791cb0ef41Sopenharmony_ci 8801cb0ef41Sopenharmony_ci// Check for fractional length args, junk length args, etc. 8811cb0ef41Sopenharmony_ci// https://github.com/joyent/node/issues/1758 8821cb0ef41Sopenharmony_ci 8831cb0ef41Sopenharmony_ci// Call .fill() first, stops valgrind warning about uninitialized memory reads. 8841cb0ef41Sopenharmony_ciBuffer.allocUnsafe(3.3).fill().toString(); 8851cb0ef41Sopenharmony_ci// Throws bad argument error in commit 43cb4ec 8861cb0ef41Sopenharmony_ciBuffer.alloc(3.3).fill().toString(); 8871cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.allocUnsafe(3.3).length, 3); 8881cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from({ length: 3.3 }).length, 3); 8891cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from({ length: 'BAM' }).length, 0); 8901cb0ef41Sopenharmony_ci 8911cb0ef41Sopenharmony_ci// Make sure that strings are not coerced to numbers. 8921cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('99').length, 2); 8931cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('13.37').length, 5); 8941cb0ef41Sopenharmony_ci 8951cb0ef41Sopenharmony_ci// Ensure that the length argument is respected. 8961cb0ef41Sopenharmony_ci['ascii', 'utf8', 'hex', 'base64', 'latin1', 'binary'].forEach((enc) => { 8971cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.allocUnsafe(1).write('aaaaaa', 0, 1, enc), 1); 8981cb0ef41Sopenharmony_ci}); 8991cb0ef41Sopenharmony_ci 9001cb0ef41Sopenharmony_ci{ 9011cb0ef41Sopenharmony_ci // Regression test, guard against buffer overrun in the base64 decoder. 9021cb0ef41Sopenharmony_ci const a = Buffer.allocUnsafe(3); 9031cb0ef41Sopenharmony_ci const b = Buffer.from('xxx'); 9041cb0ef41Sopenharmony_ci a.write('aaaaaaaa', 'base64'); 9051cb0ef41Sopenharmony_ci assert.strictEqual(b.toString(), 'xxx'); 9061cb0ef41Sopenharmony_ci} 9071cb0ef41Sopenharmony_ci 9081cb0ef41Sopenharmony_ci// issue GH-3416 9091cb0ef41Sopenharmony_ciBuffer.from(Buffer.allocUnsafe(0), 0, 0); 9101cb0ef41Sopenharmony_ci 9111cb0ef41Sopenharmony_ci// issue GH-5587 9121cb0ef41Sopenharmony_ciassert.throws( 9131cb0ef41Sopenharmony_ci () => Buffer.alloc(8).writeFloatLE(0, 5), 9141cb0ef41Sopenharmony_ci outOfRangeError 9151cb0ef41Sopenharmony_ci); 9161cb0ef41Sopenharmony_ciassert.throws( 9171cb0ef41Sopenharmony_ci () => Buffer.alloc(16).writeDoubleLE(0, 9), 9181cb0ef41Sopenharmony_ci outOfRangeError 9191cb0ef41Sopenharmony_ci); 9201cb0ef41Sopenharmony_ci 9211cb0ef41Sopenharmony_ci// Attempt to overflow buffers, similar to previous bug in array buffers 9221cb0ef41Sopenharmony_ciassert.throws( 9231cb0ef41Sopenharmony_ci () => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff), 9241cb0ef41Sopenharmony_ci outOfRangeError 9251cb0ef41Sopenharmony_ci); 9261cb0ef41Sopenharmony_ciassert.throws( 9271cb0ef41Sopenharmony_ci () => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff), 9281cb0ef41Sopenharmony_ci outOfRangeError 9291cb0ef41Sopenharmony_ci); 9301cb0ef41Sopenharmony_ci 9311cb0ef41Sopenharmony_ci// Ensure negative values can't get past offset 9321cb0ef41Sopenharmony_ciassert.throws( 9331cb0ef41Sopenharmony_ci () => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), 9341cb0ef41Sopenharmony_ci outOfRangeError 9351cb0ef41Sopenharmony_ci); 9361cb0ef41Sopenharmony_ciassert.throws( 9371cb0ef41Sopenharmony_ci () => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), 9381cb0ef41Sopenharmony_ci outOfRangeError 9391cb0ef41Sopenharmony_ci); 9401cb0ef41Sopenharmony_ci 9411cb0ef41Sopenharmony_ci// Test for common write(U)IntLE/BE 9421cb0ef41Sopenharmony_ci{ 9431cb0ef41Sopenharmony_ci let buf = Buffer.allocUnsafe(3); 9441cb0ef41Sopenharmony_ci buf.writeUIntLE(0x123456, 0, 3); 9451cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]); 9461cb0ef41Sopenharmony_ci assert.strictEqual(buf.readUIntLE(0, 3), 0x123456); 9471cb0ef41Sopenharmony_ci 9481cb0ef41Sopenharmony_ci buf.fill(0xFF); 9491cb0ef41Sopenharmony_ci buf.writeUIntBE(0x123456, 0, 3); 9501cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56]); 9511cb0ef41Sopenharmony_ci assert.strictEqual(buf.readUIntBE(0, 3), 0x123456); 9521cb0ef41Sopenharmony_ci 9531cb0ef41Sopenharmony_ci buf.fill(0xFF); 9541cb0ef41Sopenharmony_ci buf.writeIntLE(0x123456, 0, 3); 9551cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]); 9561cb0ef41Sopenharmony_ci assert.strictEqual(buf.readIntLE(0, 3), 0x123456); 9571cb0ef41Sopenharmony_ci 9581cb0ef41Sopenharmony_ci buf.fill(0xFF); 9591cb0ef41Sopenharmony_ci buf.writeIntBE(0x123456, 0, 3); 9601cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56]); 9611cb0ef41Sopenharmony_ci assert.strictEqual(buf.readIntBE(0, 3), 0x123456); 9621cb0ef41Sopenharmony_ci 9631cb0ef41Sopenharmony_ci buf.fill(0xFF); 9641cb0ef41Sopenharmony_ci buf.writeIntLE(-0x123456, 0, 3); 9651cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0xaa, 0xcb, 0xed]); 9661cb0ef41Sopenharmony_ci assert.strictEqual(buf.readIntLE(0, 3), -0x123456); 9671cb0ef41Sopenharmony_ci 9681cb0ef41Sopenharmony_ci buf.fill(0xFF); 9691cb0ef41Sopenharmony_ci buf.writeIntBE(-0x123456, 0, 3); 9701cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcb, 0xaa]); 9711cb0ef41Sopenharmony_ci assert.strictEqual(buf.readIntBE(0, 3), -0x123456); 9721cb0ef41Sopenharmony_ci 9731cb0ef41Sopenharmony_ci buf.fill(0xFF); 9741cb0ef41Sopenharmony_ci buf.writeIntLE(-0x123400, 0, 3); 9751cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0x00, 0xcc, 0xed]); 9761cb0ef41Sopenharmony_ci assert.strictEqual(buf.readIntLE(0, 3), -0x123400); 9771cb0ef41Sopenharmony_ci 9781cb0ef41Sopenharmony_ci buf.fill(0xFF); 9791cb0ef41Sopenharmony_ci buf.writeIntBE(-0x123400, 0, 3); 9801cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcc, 0x00]); 9811cb0ef41Sopenharmony_ci assert.strictEqual(buf.readIntBE(0, 3), -0x123400); 9821cb0ef41Sopenharmony_ci 9831cb0ef41Sopenharmony_ci buf.fill(0xFF); 9841cb0ef41Sopenharmony_ci buf.writeIntLE(-0x120000, 0, 3); 9851cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0x00, 0x00, 0xee]); 9861cb0ef41Sopenharmony_ci assert.strictEqual(buf.readIntLE(0, 3), -0x120000); 9871cb0ef41Sopenharmony_ci 9881cb0ef41Sopenharmony_ci buf.fill(0xFF); 9891cb0ef41Sopenharmony_ci buf.writeIntBE(-0x120000, 0, 3); 9901cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0xee, 0x00, 0x00]); 9911cb0ef41Sopenharmony_ci assert.strictEqual(buf.readIntBE(0, 3), -0x120000); 9921cb0ef41Sopenharmony_ci 9931cb0ef41Sopenharmony_ci buf = Buffer.allocUnsafe(5); 9941cb0ef41Sopenharmony_ci buf.writeUIntLE(0x1234567890, 0, 5); 9951cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]); 9961cb0ef41Sopenharmony_ci assert.strictEqual(buf.readUIntLE(0, 5), 0x1234567890); 9971cb0ef41Sopenharmony_ci 9981cb0ef41Sopenharmony_ci buf.fill(0xFF); 9991cb0ef41Sopenharmony_ci buf.writeUIntBE(0x1234567890, 0, 5); 10001cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]); 10011cb0ef41Sopenharmony_ci assert.strictEqual(buf.readUIntBE(0, 5), 0x1234567890); 10021cb0ef41Sopenharmony_ci 10031cb0ef41Sopenharmony_ci buf.fill(0xFF); 10041cb0ef41Sopenharmony_ci buf.writeIntLE(0x1234567890, 0, 5); 10051cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]); 10061cb0ef41Sopenharmony_ci assert.strictEqual(buf.readIntLE(0, 5), 0x1234567890); 10071cb0ef41Sopenharmony_ci 10081cb0ef41Sopenharmony_ci buf.fill(0xFF); 10091cb0ef41Sopenharmony_ci buf.writeIntBE(0x1234567890, 0, 5); 10101cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]); 10111cb0ef41Sopenharmony_ci assert.strictEqual(buf.readIntBE(0, 5), 0x1234567890); 10121cb0ef41Sopenharmony_ci 10131cb0ef41Sopenharmony_ci buf.fill(0xFF); 10141cb0ef41Sopenharmony_ci buf.writeIntLE(-0x1234567890, 0, 5); 10151cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0x70, 0x87, 0xa9, 0xcb, 0xed]); 10161cb0ef41Sopenharmony_ci assert.strictEqual(buf.readIntLE(0, 5), -0x1234567890); 10171cb0ef41Sopenharmony_ci 10181cb0ef41Sopenharmony_ci buf.fill(0xFF); 10191cb0ef41Sopenharmony_ci buf.writeIntBE(-0x1234567890, 0, 5); 10201cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcb, 0xa9, 0x87, 0x70]); 10211cb0ef41Sopenharmony_ci assert.strictEqual(buf.readIntBE(0, 5), -0x1234567890); 10221cb0ef41Sopenharmony_ci 10231cb0ef41Sopenharmony_ci buf.fill(0xFF); 10241cb0ef41Sopenharmony_ci buf.writeIntLE(-0x0012000000, 0, 5); 10251cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0x00, 0x00, 0x00, 0xee, 0xff]); 10261cb0ef41Sopenharmony_ci assert.strictEqual(buf.readIntLE(0, 5), -0x0012000000); 10271cb0ef41Sopenharmony_ci 10281cb0ef41Sopenharmony_ci buf.fill(0xFF); 10291cb0ef41Sopenharmony_ci buf.writeIntBE(-0x0012000000, 0, 5); 10301cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]); 10311cb0ef41Sopenharmony_ci assert.strictEqual(buf.readIntBE(0, 5), -0x0012000000); 10321cb0ef41Sopenharmony_ci} 10331cb0ef41Sopenharmony_ci 10341cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node-v0.x-archive/issues/5482: 10351cb0ef41Sopenharmony_ci// should throw but not assert in C++ land. 10361cb0ef41Sopenharmony_ciassert.throws( 10371cb0ef41Sopenharmony_ci () => Buffer.from('', 'buffer'), 10381cb0ef41Sopenharmony_ci { 10391cb0ef41Sopenharmony_ci code: 'ERR_UNKNOWN_ENCODING', 10401cb0ef41Sopenharmony_ci name: 'TypeError', 10411cb0ef41Sopenharmony_ci message: 'Unknown encoding: buffer' 10421cb0ef41Sopenharmony_ci } 10431cb0ef41Sopenharmony_ci); 10441cb0ef41Sopenharmony_ci 10451cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node-v0.x-archive/issues/6111. 10461cb0ef41Sopenharmony_ci// Constructing a buffer from another buffer should a) work, and b) not corrupt 10471cb0ef41Sopenharmony_ci// the source buffer. 10481cb0ef41Sopenharmony_ci{ 10491cb0ef41Sopenharmony_ci const a = [...Array(128).keys()]; // [0, 1, 2, 3, ... 126, 127] 10501cb0ef41Sopenharmony_ci const b = Buffer.from(a); 10511cb0ef41Sopenharmony_ci const c = Buffer.from(b); 10521cb0ef41Sopenharmony_ci assert.strictEqual(b.length, a.length); 10531cb0ef41Sopenharmony_ci assert.strictEqual(c.length, a.length); 10541cb0ef41Sopenharmony_ci for (let i = 0, k = a.length; i < k; ++i) { 10551cb0ef41Sopenharmony_ci assert.strictEqual(a[i], i); 10561cb0ef41Sopenharmony_ci assert.strictEqual(b[i], i); 10571cb0ef41Sopenharmony_ci assert.strictEqual(c[i], i); 10581cb0ef41Sopenharmony_ci } 10591cb0ef41Sopenharmony_ci} 10601cb0ef41Sopenharmony_ci 10611cb0ef41Sopenharmony_ciif (common.hasCrypto) { // eslint-disable-line node-core/crypto-check 10621cb0ef41Sopenharmony_ci // Test truncation after decode 10631cb0ef41Sopenharmony_ci const crypto = require('crypto'); 10641cb0ef41Sopenharmony_ci 10651cb0ef41Sopenharmony_ci const b1 = Buffer.from('YW55=======', 'base64'); 10661cb0ef41Sopenharmony_ci const b2 = Buffer.from('YW55', 'base64'); 10671cb0ef41Sopenharmony_ci 10681cb0ef41Sopenharmony_ci assert.strictEqual( 10691cb0ef41Sopenharmony_ci crypto.createHash('sha1').update(b1).digest('hex'), 10701cb0ef41Sopenharmony_ci crypto.createHash('sha1').update(b2).digest('hex') 10711cb0ef41Sopenharmony_ci ); 10721cb0ef41Sopenharmony_ci} else { 10731cb0ef41Sopenharmony_ci common.printSkipMessage('missing crypto'); 10741cb0ef41Sopenharmony_ci} 10751cb0ef41Sopenharmony_ci 10761cb0ef41Sopenharmony_ciconst ps = Buffer.poolSize; 10771cb0ef41Sopenharmony_ciBuffer.poolSize = 0; 10781cb0ef41Sopenharmony_ciassert(Buffer.allocUnsafe(1).parent instanceof ArrayBuffer); 10791cb0ef41Sopenharmony_ciBuffer.poolSize = ps; 10801cb0ef41Sopenharmony_ci 10811cb0ef41Sopenharmony_ciassert.throws( 10821cb0ef41Sopenharmony_ci () => Buffer.allocUnsafe(10).copy(), 10831cb0ef41Sopenharmony_ci { 10841cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 10851cb0ef41Sopenharmony_ci name: 'TypeError', 10861cb0ef41Sopenharmony_ci message: 'The "target" argument must be an instance of Buffer or ' + 10871cb0ef41Sopenharmony_ci 'Uint8Array. Received undefined' 10881cb0ef41Sopenharmony_ci }); 10891cb0ef41Sopenharmony_ci 10901cb0ef41Sopenharmony_ciassert.throws(() => Buffer.from(), { 10911cb0ef41Sopenharmony_ci name: 'TypeError', 10921cb0ef41Sopenharmony_ci message: 'The first argument must be of type string or an instance of ' + 10931cb0ef41Sopenharmony_ci 'Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined' 10941cb0ef41Sopenharmony_ci}); 10951cb0ef41Sopenharmony_ciassert.throws(() => Buffer.from(null), { 10961cb0ef41Sopenharmony_ci name: 'TypeError', 10971cb0ef41Sopenharmony_ci message: 'The first argument must be of type string or an instance of ' + 10981cb0ef41Sopenharmony_ci 'Buffer, ArrayBuffer, or Array or an Array-like Object. Received null' 10991cb0ef41Sopenharmony_ci}); 11001cb0ef41Sopenharmony_ci 11011cb0ef41Sopenharmony_ci// Test prototype getters don't throw 11021cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.prototype.parent, undefined); 11031cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.prototype.offset, undefined); 11041cb0ef41Sopenharmony_ciassert.strictEqual(SlowBuffer.prototype.parent, undefined); 11051cb0ef41Sopenharmony_ciassert.strictEqual(SlowBuffer.prototype.offset, undefined); 11061cb0ef41Sopenharmony_ci 11071cb0ef41Sopenharmony_ci 11081cb0ef41Sopenharmony_ci{ 11091cb0ef41Sopenharmony_ci // Test that large negative Buffer length inputs don't affect the pool offset. 11101cb0ef41Sopenharmony_ci // Use the fromArrayLike() variant here because it's more lenient 11111cb0ef41Sopenharmony_ci // about its input and passes the length directly to allocate(). 11121cb0ef41Sopenharmony_ci assert.deepStrictEqual(Buffer.from({ length: -Buffer.poolSize }), 11131cb0ef41Sopenharmony_ci Buffer.from('')); 11141cb0ef41Sopenharmony_ci assert.deepStrictEqual(Buffer.from({ length: -100 }), 11151cb0ef41Sopenharmony_ci Buffer.from('')); 11161cb0ef41Sopenharmony_ci 11171cb0ef41Sopenharmony_ci // Check pool offset after that by trying to write string into the pool. 11181cb0ef41Sopenharmony_ci Buffer.from('abc'); 11191cb0ef41Sopenharmony_ci} 11201cb0ef41Sopenharmony_ci 11211cb0ef41Sopenharmony_ci 11221cb0ef41Sopenharmony_ci// Test that ParseArrayIndex handles full uint32 11231cb0ef41Sopenharmony_ci{ 11241cb0ef41Sopenharmony_ci const errMsg = common.expectsError({ 11251cb0ef41Sopenharmony_ci code: 'ERR_BUFFER_OUT_OF_BOUNDS', 11261cb0ef41Sopenharmony_ci name: 'RangeError', 11271cb0ef41Sopenharmony_ci message: '"offset" is outside of buffer bounds' 11281cb0ef41Sopenharmony_ci }); 11291cb0ef41Sopenharmony_ci assert.throws(() => Buffer.from(new ArrayBuffer(0), -1 >>> 0), errMsg); 11301cb0ef41Sopenharmony_ci} 11311cb0ef41Sopenharmony_ci 11321cb0ef41Sopenharmony_ci// ParseArrayIndex() should reject values that don't fit in a 32 bits size_t. 11331cb0ef41Sopenharmony_ciassert.throws(() => { 11341cb0ef41Sopenharmony_ci const a = Buffer.alloc(1); 11351cb0ef41Sopenharmony_ci const b = Buffer.alloc(1); 11361cb0ef41Sopenharmony_ci a.copy(b, 0, 0x100000000, 0x100000001); 11371cb0ef41Sopenharmony_ci}, outOfRangeError); 11381cb0ef41Sopenharmony_ci 11391cb0ef41Sopenharmony_ci// Unpooled buffer (replaces SlowBuffer) 11401cb0ef41Sopenharmony_ci{ 11411cb0ef41Sopenharmony_ci const ubuf = Buffer.allocUnsafeSlow(10); 11421cb0ef41Sopenharmony_ci assert(ubuf); 11431cb0ef41Sopenharmony_ci assert(ubuf.buffer); 11441cb0ef41Sopenharmony_ci assert.strictEqual(ubuf.buffer.byteLength, 10); 11451cb0ef41Sopenharmony_ci} 11461cb0ef41Sopenharmony_ci 11471cb0ef41Sopenharmony_ci// Regression test to verify that an empty ArrayBuffer does not throw. 11481cb0ef41Sopenharmony_ciBuffer.from(new ArrayBuffer()); 11491cb0ef41Sopenharmony_ci 11501cb0ef41Sopenharmony_ci// Test that ArrayBuffer from a different context is detected correctly. 11511cb0ef41Sopenharmony_ciconst arrayBuf = vm.runInNewContext('new ArrayBuffer()'); 11521cb0ef41Sopenharmony_ciBuffer.from(arrayBuf); 11531cb0ef41Sopenharmony_ciBuffer.from({ buffer: arrayBuf }); 11541cb0ef41Sopenharmony_ci 11551cb0ef41Sopenharmony_ciassert.throws(() => Buffer.alloc({ valueOf: () => 1 }), 11561cb0ef41Sopenharmony_ci /"size" argument must be of type number/); 11571cb0ef41Sopenharmony_ciassert.throws(() => Buffer.alloc({ valueOf: () => -1 }), 11581cb0ef41Sopenharmony_ci /"size" argument must be of type number/); 11591cb0ef41Sopenharmony_ci 11601cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.prototype.toLocaleString, Buffer.prototype.toString); 11611cb0ef41Sopenharmony_ci{ 11621cb0ef41Sopenharmony_ci const buf = Buffer.from('test'); 11631cb0ef41Sopenharmony_ci assert.strictEqual(buf.toLocaleString(), buf.toString()); 11641cb0ef41Sopenharmony_ci} 11651cb0ef41Sopenharmony_ci 11661cb0ef41Sopenharmony_ciassert.throws(() => { 11671cb0ef41Sopenharmony_ci Buffer.alloc(0x1000, 'This is not correctly encoded', 'hex'); 11681cb0ef41Sopenharmony_ci}, { 11691cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_VALUE', 11701cb0ef41Sopenharmony_ci name: 'TypeError' 11711cb0ef41Sopenharmony_ci}); 11721cb0ef41Sopenharmony_ci 11731cb0ef41Sopenharmony_ciassert.throws(() => { 11741cb0ef41Sopenharmony_ci Buffer.alloc(0x1000, 'c', 'hex'); 11751cb0ef41Sopenharmony_ci}, { 11761cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_VALUE', 11771cb0ef41Sopenharmony_ci name: 'TypeError' 11781cb0ef41Sopenharmony_ci}); 11791cb0ef41Sopenharmony_ci 11801cb0ef41Sopenharmony_ciassert.throws(() => { 11811cb0ef41Sopenharmony_ci Buffer.alloc(1, Buffer.alloc(0)); 11821cb0ef41Sopenharmony_ci}, { 11831cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_VALUE', 11841cb0ef41Sopenharmony_ci name: 'TypeError' 11851cb0ef41Sopenharmony_ci}); 11861cb0ef41Sopenharmony_ci 11871cb0ef41Sopenharmony_ciassert.throws(() => { 11881cb0ef41Sopenharmony_ci Buffer.alloc(40, 'x', 20); 11891cb0ef41Sopenharmony_ci}, { 11901cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 11911cb0ef41Sopenharmony_ci name: 'TypeError' 11921cb0ef41Sopenharmony_ci}); 1193