1'use strict'; 2const common = require('../common'); 3 4const assert = require('assert'); 5const vm = require('vm'); 6 7const SlowBuffer = require('buffer').SlowBuffer; 8 9// Verify the maximum Uint8Array size. There is no concrete limit by spec. The 10// internal limits should be updated if this fails. 11assert.throws( 12 () => new Uint8Array(2 ** 32 + 1), 13 { message: 'Invalid typed array length: 4294967297' } 14); 15 16const b = Buffer.allocUnsafe(1024); 17assert.strictEqual(b.length, 1024); 18 19b[0] = -1; 20assert.strictEqual(b[0], 255); 21 22for (let i = 0; i < 1024; i++) { 23 b[i] = i % 256; 24} 25 26for (let i = 0; i < 1024; i++) { 27 assert.strictEqual(i % 256, b[i]); 28} 29 30const c = Buffer.allocUnsafe(512); 31assert.strictEqual(c.length, 512); 32 33const d = Buffer.from([]); 34assert.strictEqual(d.length, 0); 35 36// Test offset properties 37{ 38 const b = Buffer.alloc(128); 39 assert.strictEqual(b.length, 128); 40 assert.strictEqual(b.byteOffset, 0); 41 assert.strictEqual(b.offset, 0); 42} 43 44// Test creating a Buffer from a Uint8Array 45{ 46 const ui8 = new Uint8Array(4).fill(42); 47 const e = Buffer.from(ui8); 48 for (const [index, value] of e.entries()) { 49 assert.strictEqual(value, ui8[index]); 50 } 51} 52// Test creating a Buffer from a Uint8Array (old constructor) 53{ 54 const ui8 = new Uint8Array(4).fill(42); 55 const e = Buffer(ui8); 56 for (const [key, value] of e.entries()) { 57 assert.strictEqual(value, ui8[key]); 58 } 59} 60 61// Test creating a Buffer from a Uint32Array 62// Note: it is implicitly interpreted as Array of integers modulo 256 63{ 64 const ui32 = new Uint32Array(4).fill(42); 65 const e = Buffer.from(ui32); 66 for (const [index, value] of e.entries()) { 67 assert.strictEqual(value, ui32[index]); 68 } 69} 70// Test creating a Buffer from a Uint32Array (old constructor) 71// Note: it is implicitly interpreted as Array of integers modulo 256 72{ 73 const ui32 = new Uint32Array(4).fill(42); 74 const e = Buffer(ui32); 75 for (const [key, value] of e.entries()) { 76 assert.strictEqual(value, ui32[key]); 77 } 78} 79 80// Test invalid encoding for Buffer.toString 81assert.throws(() => b.toString('invalid'), 82 /Unknown encoding: invalid/); 83// Invalid encoding for Buffer.write 84assert.throws(() => b.write('test string', 0, 5, 'invalid'), 85 /Unknown encoding: invalid/); 86// Unsupported arguments for Buffer.write 87assert.throws(() => b.write('test', 'utf8', 0), 88 { code: 'ERR_INVALID_ARG_TYPE' }); 89 90// Try to create 0-length buffers. Should not throw. 91Buffer.from(''); 92Buffer.from('', 'ascii'); 93Buffer.from('', 'latin1'); 94Buffer.alloc(0); 95Buffer.allocUnsafe(0); 96new Buffer(''); 97new Buffer('', 'ascii'); 98new Buffer('', 'latin1'); 99new Buffer('', 'binary'); 100Buffer(0); 101 102const outOfRangeError = { 103 code: 'ERR_OUT_OF_RANGE', 104 name: 'RangeError' 105}; 106 107// Try to write a 0-length string beyond the end of b 108assert.throws(() => b.write('', 2048), outOfRangeError); 109 110// Throw when writing to negative offset 111assert.throws(() => b.write('a', -1), outOfRangeError); 112 113// Throw when writing past bounds from the pool 114assert.throws(() => b.write('a', 2048), outOfRangeError); 115 116// Throw when writing to negative offset 117assert.throws(() => b.write('a', -1), outOfRangeError); 118 119// Try to copy 0 bytes worth of data into an empty buffer 120b.copy(Buffer.alloc(0), 0, 0, 0); 121 122// Try to copy 0 bytes past the end of the target buffer 123b.copy(Buffer.alloc(0), 1, 1, 1); 124b.copy(Buffer.alloc(1), 1, 1, 1); 125 126// Try to copy 0 bytes from past the end of the source buffer 127b.copy(Buffer.alloc(1), 0, 1024, 1024); 128 129// Testing for smart defaults and ability to pass string values as offset 130{ 131 const writeTest = Buffer.from('abcdes'); 132 writeTest.write('n', 'ascii'); 133 assert.throws( 134 () => writeTest.write('o', '1', 'ascii'), 135 { code: 'ERR_INVALID_ARG_TYPE' } 136 ); 137 writeTest.write('o', 1, 'ascii'); 138 writeTest.write('d', 2, 'ascii'); 139 writeTest.write('e', 3, 'ascii'); 140 writeTest.write('j', 4, 'ascii'); 141 assert.strictEqual(writeTest.toString(), 'nodejs'); 142} 143 144// Offset points to the end of the buffer and does not throw. 145// (see https://github.com/nodejs/node/issues/8127). 146Buffer.alloc(1).write('', 1, 0); 147 148// ASCII slice test 149{ 150 const asciiString = 'hello world'; 151 152 for (let i = 0; i < asciiString.length; i++) { 153 b[i] = asciiString.charCodeAt(i); 154 } 155 const asciiSlice = b.toString('ascii', 0, asciiString.length); 156 assert.strictEqual(asciiString, asciiSlice); 157} 158 159{ 160 const asciiString = 'hello world'; 161 const offset = 100; 162 163 assert.strictEqual(asciiString.length, b.write(asciiString, offset, 'ascii')); 164 const asciiSlice = b.toString('ascii', offset, offset + asciiString.length); 165 assert.strictEqual(asciiString, asciiSlice); 166} 167 168{ 169 const asciiString = 'hello world'; 170 const offset = 100; 171 172 const sliceA = b.slice(offset, offset + asciiString.length); 173 const sliceB = b.slice(offset, offset + asciiString.length); 174 for (let i = 0; i < asciiString.length; i++) { 175 assert.strictEqual(sliceA[i], sliceB[i]); 176 } 177} 178 179// UTF-8 slice test 180{ 181 const utf8String = '¡hέlló wôrld!'; 182 const offset = 100; 183 184 b.write(utf8String, 0, Buffer.byteLength(utf8String), 'utf8'); 185 let utf8Slice = b.toString('utf8', 0, Buffer.byteLength(utf8String)); 186 assert.strictEqual(utf8String, utf8Slice); 187 188 assert.strictEqual(Buffer.byteLength(utf8String), 189 b.write(utf8String, offset, 'utf8')); 190 utf8Slice = b.toString('utf8', offset, 191 offset + Buffer.byteLength(utf8String)); 192 assert.strictEqual(utf8String, utf8Slice); 193 194 const sliceA = b.slice(offset, offset + Buffer.byteLength(utf8String)); 195 const sliceB = b.slice(offset, offset + Buffer.byteLength(utf8String)); 196 for (let i = 0; i < Buffer.byteLength(utf8String); i++) { 197 assert.strictEqual(sliceA[i], sliceB[i]); 198 } 199} 200 201{ 202 const slice = b.slice(100, 150); 203 assert.strictEqual(slice.length, 50); 204 for (let i = 0; i < 50; i++) { 205 assert.strictEqual(b[100 + i], slice[i]); 206 } 207} 208 209{ 210 // Make sure only top level parent propagates from allocPool 211 const b = Buffer.allocUnsafe(5); 212 const c = b.slice(0, 4); 213 const d = c.slice(0, 2); 214 assert.strictEqual(b.parent, c.parent); 215 assert.strictEqual(b.parent, d.parent); 216} 217 218{ 219 // Also from a non-pooled instance 220 const b = Buffer.allocUnsafeSlow(5); 221 const c = b.slice(0, 4); 222 const d = c.slice(0, 2); 223 assert.strictEqual(c.parent, d.parent); 224} 225 226{ 227 // Bug regression test 228 const testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語 229 const buffer = Buffer.allocUnsafe(32); 230 const size = buffer.write(testValue, 0, 'utf8'); 231 const slice = buffer.toString('utf8', 0, size); 232 assert.strictEqual(slice, testValue); 233} 234 235{ 236 // Test triple slice 237 const a = Buffer.allocUnsafe(8); 238 for (let i = 0; i < 8; i++) a[i] = i; 239 const b = a.slice(4, 8); 240 assert.strictEqual(b[0], 4); 241 assert.strictEqual(b[1], 5); 242 assert.strictEqual(b[2], 6); 243 assert.strictEqual(b[3], 7); 244 const c = b.slice(2, 4); 245 assert.strictEqual(c[0], 6); 246 assert.strictEqual(c[1], 7); 247} 248 249{ 250 const d = Buffer.from([23, 42, 255]); 251 assert.strictEqual(d.length, 3); 252 assert.strictEqual(d[0], 23); 253 assert.strictEqual(d[1], 42); 254 assert.strictEqual(d[2], 255); 255 assert.deepStrictEqual(d, Buffer.from(d)); 256} 257 258{ 259 // Test for proper UTF-8 Encoding 260 const e = Buffer.from('über'); 261 assert.deepStrictEqual(e, Buffer.from([195, 188, 98, 101, 114])); 262} 263 264{ 265 // Test for proper ascii Encoding, length should be 4 266 const f = Buffer.from('über', 'ascii'); 267 assert.deepStrictEqual(f, Buffer.from([252, 98, 101, 114])); 268} 269 270['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach((encoding) => { 271 { 272 // Test for proper UTF16LE encoding, length should be 8 273 const f = Buffer.from('über', encoding); 274 assert.deepStrictEqual(f, Buffer.from([252, 0, 98, 0, 101, 0, 114, 0])); 275 } 276 277 { 278 // Length should be 12 279 const f = Buffer.from('привет', encoding); 280 assert.deepStrictEqual( 281 f, Buffer.from([63, 4, 64, 4, 56, 4, 50, 4, 53, 4, 66, 4]) 282 ); 283 assert.strictEqual(f.toString(encoding), 'привет'); 284 } 285 286 { 287 const f = Buffer.from([0, 0, 0, 0, 0]); 288 assert.strictEqual(f.length, 5); 289 const size = f.write('あいうえお', encoding); 290 assert.strictEqual(size, 4); 291 assert.deepStrictEqual(f, Buffer.from([0x42, 0x30, 0x44, 0x30, 0x00])); 292 } 293}); 294 295{ 296 const f = Buffer.from('\uD83D\uDC4D', 'utf-16le'); // THUMBS UP SIGN (U+1F44D) 297 assert.strictEqual(f.length, 4); 298 assert.deepStrictEqual(f, Buffer.from('3DD84DDC', 'hex')); 299} 300 301// Test construction from arrayish object 302{ 303 const arrayIsh = { 0: 0, 1: 1, 2: 2, 3: 3, length: 4 }; 304 let g = Buffer.from(arrayIsh); 305 assert.deepStrictEqual(g, Buffer.from([0, 1, 2, 3])); 306 const strArrayIsh = { 0: '0', 1: '1', 2: '2', 3: '3', length: 4 }; 307 g = Buffer.from(strArrayIsh); 308 assert.deepStrictEqual(g, Buffer.from([0, 1, 2, 3])); 309} 310 311// 312// Test toString('base64') 313// 314assert.strictEqual((Buffer.from('Man')).toString('base64'), 'TWFu'); 315assert.strictEqual((Buffer.from('Woman')).toString('base64'), 'V29tYW4='); 316 317// 318// Test toString('base64url') 319// 320assert.strictEqual((Buffer.from('Man')).toString('base64url'), 'TWFu'); 321assert.strictEqual((Buffer.from('Woman')).toString('base64url'), 'V29tYW4'); 322 323{ 324 // Test that regular and URL-safe base64 both work both ways 325 const expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff]; 326 assert.deepStrictEqual(Buffer.from('//++/++/++//', 'base64'), 327 Buffer.from(expected)); 328 assert.deepStrictEqual(Buffer.from('__--_--_--__', 'base64'), 329 Buffer.from(expected)); 330 assert.deepStrictEqual(Buffer.from('//++/++/++//', 'base64url'), 331 Buffer.from(expected)); 332 assert.deepStrictEqual(Buffer.from('__--_--_--__', 'base64url'), 333 Buffer.from(expected)); 334} 335 336const base64flavors = ['base64', 'base64url']; 337 338{ 339 // Test that regular and URL-safe base64 both work both ways with padding 340 const expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff, 0xfb]; 341 assert.deepStrictEqual(Buffer.from('//++/++/++//+w==', 'base64'), 342 Buffer.from(expected)); 343 assert.deepStrictEqual(Buffer.from('//++/++/++//+w==', 'base64'), 344 Buffer.from(expected)); 345 assert.deepStrictEqual(Buffer.from('//++/++/++//+w==', 'base64url'), 346 Buffer.from(expected)); 347 assert.deepStrictEqual(Buffer.from('//++/++/++//+w==', 'base64url'), 348 Buffer.from(expected)); 349} 350 351{ 352 // big example 353 const quote = 'Man is distinguished, not only by his reason, but by this ' + 354 'singular passion from other animals, which is a lust ' + 355 'of the mind, that by a perseverance of delight in the ' + 356 'continued and indefatigable generation of knowledge, ' + 357 'exceeds the short vehemence of any carnal pleasure.'; 358 const expected = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb' + 359 '24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci' + 360 'BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQ' + 361 'gYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu' + 362 'dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZ' + 363 'GdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm' + 364 '5hbCBwbGVhc3VyZS4='; 365 assert.strictEqual(Buffer.from(quote).toString('base64'), expected); 366 assert.strictEqual( 367 Buffer.from(quote).toString('base64url'), 368 expected.replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '') 369 ); 370 371 base64flavors.forEach((encoding) => { 372 let b = Buffer.allocUnsafe(1024); 373 let bytesWritten = b.write(expected, 0, encoding); 374 assert.strictEqual(quote.length, bytesWritten); 375 assert.strictEqual(quote, b.toString('ascii', 0, quote.length)); 376 377 // Check that the base64 decoder ignores whitespace 378 const expectedWhite = `${expected.slice(0, 60)} \n` + 379 `${expected.slice(60, 120)} \n` + 380 `${expected.slice(120, 180)} \n` + 381 `${expected.slice(180, 240)} \n` + 382 `${expected.slice(240, 300)}\n` + 383 `${expected.slice(300, 360)}\n`; 384 b = Buffer.allocUnsafe(1024); 385 bytesWritten = b.write(expectedWhite, 0, encoding); 386 assert.strictEqual(quote.length, bytesWritten); 387 assert.strictEqual(quote, b.toString('ascii', 0, quote.length)); 388 389 // Check that the base64 decoder on the constructor works 390 // even in the presence of whitespace. 391 b = Buffer.from(expectedWhite, encoding); 392 assert.strictEqual(quote.length, b.length); 393 assert.strictEqual(quote, b.toString('ascii', 0, quote.length)); 394 395 // Check that the base64 decoder ignores illegal chars 396 const expectedIllegal = expected.slice(0, 60) + ' \x80' + 397 expected.slice(60, 120) + ' \xff' + 398 expected.slice(120, 180) + ' \x00' + 399 expected.slice(180, 240) + ' \x98' + 400 expected.slice(240, 300) + '\x03' + 401 expected.slice(300, 360); 402 b = Buffer.from(expectedIllegal, encoding); 403 assert.strictEqual(quote.length, b.length); 404 assert.strictEqual(quote, b.toString('ascii', 0, quote.length)); 405 }); 406} 407 408base64flavors.forEach((encoding) => { 409 assert.strictEqual(Buffer.from('', encoding).toString(), ''); 410 assert.strictEqual(Buffer.from('K', encoding).toString(), ''); 411 412 // multiple-of-4 with padding 413 assert.strictEqual(Buffer.from('Kg==', encoding).toString(), '*'); 414 assert.strictEqual(Buffer.from('Kio=', encoding).toString(), '*'.repeat(2)); 415 assert.strictEqual(Buffer.from('Kioq', encoding).toString(), '*'.repeat(3)); 416 assert.strictEqual( 417 Buffer.from('KioqKg==', encoding).toString(), '*'.repeat(4)); 418 assert.strictEqual( 419 Buffer.from('KioqKio=', encoding).toString(), '*'.repeat(5)); 420 assert.strictEqual( 421 Buffer.from('KioqKioq', encoding).toString(), '*'.repeat(6)); 422 assert.strictEqual(Buffer.from('KioqKioqKg==', encoding).toString(), 423 '*'.repeat(7)); 424 assert.strictEqual(Buffer.from('KioqKioqKio=', encoding).toString(), 425 '*'.repeat(8)); 426 assert.strictEqual(Buffer.from('KioqKioqKioq', encoding).toString(), 427 '*'.repeat(9)); 428 assert.strictEqual(Buffer.from('KioqKioqKioqKg==', encoding).toString(), 429 '*'.repeat(10)); 430 assert.strictEqual(Buffer.from('KioqKioqKioqKio=', encoding).toString(), 431 '*'.repeat(11)); 432 assert.strictEqual(Buffer.from('KioqKioqKioqKioq', encoding).toString(), 433 '*'.repeat(12)); 434 assert.strictEqual(Buffer.from('KioqKioqKioqKioqKg==', encoding).toString(), 435 '*'.repeat(13)); 436 assert.strictEqual(Buffer.from('KioqKioqKioqKioqKio=', encoding).toString(), 437 '*'.repeat(14)); 438 assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioq', encoding).toString(), 439 '*'.repeat(15)); 440 assert.strictEqual( 441 Buffer.from('KioqKioqKioqKioqKioqKg==', encoding).toString(), 442 '*'.repeat(16)); 443 assert.strictEqual( 444 Buffer.from('KioqKioqKioqKioqKioqKio=', encoding).toString(), 445 '*'.repeat(17)); 446 assert.strictEqual( 447 Buffer.from('KioqKioqKioqKioqKioqKioq', encoding).toString(), 448 '*'.repeat(18)); 449 assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKioqKg==', 450 encoding).toString(), 451 '*'.repeat(19)); 452 assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKioqKio=', 453 encoding).toString(), 454 '*'.repeat(20)); 455 456 // No padding, not a multiple of 4 457 assert.strictEqual(Buffer.from('Kg', encoding).toString(), '*'); 458 assert.strictEqual(Buffer.from('Kio', encoding).toString(), '*'.repeat(2)); 459 assert.strictEqual(Buffer.from('KioqKg', encoding).toString(), '*'.repeat(4)); 460 assert.strictEqual( 461 Buffer.from('KioqKio', encoding).toString(), '*'.repeat(5)); 462 assert.strictEqual(Buffer.from('KioqKioqKg', encoding).toString(), 463 '*'.repeat(7)); 464 assert.strictEqual(Buffer.from('KioqKioqKio', encoding).toString(), 465 '*'.repeat(8)); 466 assert.strictEqual(Buffer.from('KioqKioqKioqKg', encoding).toString(), 467 '*'.repeat(10)); 468 assert.strictEqual(Buffer.from('KioqKioqKioqKio', encoding).toString(), 469 '*'.repeat(11)); 470 assert.strictEqual(Buffer.from('KioqKioqKioqKioqKg', encoding).toString(), 471 '*'.repeat(13)); 472 assert.strictEqual(Buffer.from('KioqKioqKioqKioqKio', encoding).toString(), 473 '*'.repeat(14)); 474 assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKg', encoding).toString(), 475 '*'.repeat(16)); 476 assert.strictEqual( 477 Buffer.from('KioqKioqKioqKioqKioqKio', encoding).toString(), 478 '*'.repeat(17)); 479 assert.strictEqual( 480 Buffer.from('KioqKioqKioqKioqKioqKioqKg', encoding).toString(), 481 '*'.repeat(19)); 482 assert.strictEqual( 483 Buffer.from('KioqKioqKioqKioqKioqKioqKio', encoding).toString(), 484 '*'.repeat(20)); 485}); 486 487// Handle padding graciously, multiple-of-4 or not 488assert.strictEqual( 489 Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw==', 'base64').length, 490 32 491); 492assert.strictEqual( 493 Buffer.from('72INjkR5fchcxk9-VgdGPFJDxUBFR5_rMFsghgxADiw==', 'base64url') 494 .length, 495 32 496); 497assert.strictEqual( 498 Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw=', 'base64').length, 499 32 500); 501assert.strictEqual( 502 Buffer.from('72INjkR5fchcxk9-VgdGPFJDxUBFR5_rMFsghgxADiw=', 'base64url') 503 .length, 504 32 505); 506assert.strictEqual( 507 Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw', 'base64').length, 508 32 509); 510assert.strictEqual( 511 Buffer.from('72INjkR5fchcxk9-VgdGPFJDxUBFR5_rMFsghgxADiw', 'base64url') 512 .length, 513 32 514); 515assert.strictEqual( 516 Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg==', 'base64').length, 517 31 518); 519assert.strictEqual( 520 Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg==', 'base64url') 521 .length, 522 31 523); 524assert.strictEqual( 525 Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg=', 'base64').length, 526 31 527); 528assert.strictEqual( 529 Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg=', 'base64url') 530 .length, 531 31 532); 533assert.strictEqual( 534 Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg', 'base64').length, 535 31 536); 537assert.strictEqual( 538 Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg', 'base64url').length, 539 31 540); 541 542{ 543// This string encodes single '.' character in UTF-16 544 const dot = Buffer.from('//4uAA==', 'base64'); 545 assert.strictEqual(dot[0], 0xff); 546 assert.strictEqual(dot[1], 0xfe); 547 assert.strictEqual(dot[2], 0x2e); 548 assert.strictEqual(dot[3], 0x00); 549 assert.strictEqual(dot.toString('base64'), '//4uAA=='); 550} 551 552{ 553// This string encodes single '.' character in UTF-16 554 const dot = Buffer.from('//4uAA', 'base64url'); 555 assert.strictEqual(dot[0], 0xff); 556 assert.strictEqual(dot[1], 0xfe); 557 assert.strictEqual(dot[2], 0x2e); 558 assert.strictEqual(dot[3], 0x00); 559 assert.strictEqual(dot.toString('base64url'), '__4uAA'); 560} 561 562{ 563 // Writing base64 at a position > 0 should not mangle the result. 564 // 565 // https://github.com/joyent/node/issues/402 566 const segments = ['TWFkbmVzcz8h', 'IFRoaXM=', 'IGlz', 'IG5vZGUuanMh']; 567 const b = Buffer.allocUnsafe(64); 568 let pos = 0; 569 570 for (let i = 0; i < segments.length; ++i) { 571 pos += b.write(segments[i], pos, 'base64'); 572 } 573 assert.strictEqual(b.toString('latin1', 0, pos), 574 'Madness?! This is node.js!'); 575} 576 577{ 578 // Writing base64url at a position > 0 should not mangle the result. 579 // 580 // https://github.com/joyent/node/issues/402 581 const segments = ['TWFkbmVzcz8h', 'IFRoaXM', 'IGlz', 'IG5vZGUuanMh']; 582 const b = Buffer.allocUnsafe(64); 583 let pos = 0; 584 585 for (let i = 0; i < segments.length; ++i) { 586 pos += b.write(segments[i], pos, 'base64url'); 587 } 588 assert.strictEqual(b.toString('latin1', 0, pos), 589 'Madness?! This is node.js!'); 590} 591 592// Regression test for https://github.com/nodejs/node/issues/3496. 593assert.strictEqual(Buffer.from('=bad'.repeat(1e4), 'base64').length, 0); 594 595// Regression test for https://github.com/nodejs/node/issues/11987. 596assert.deepStrictEqual(Buffer.from('w0 ', 'base64'), 597 Buffer.from('w0', 'base64')); 598 599// Regression test for https://github.com/nodejs/node/issues/13657. 600assert.deepStrictEqual(Buffer.from(' YWJvcnVtLg', 'base64'), 601 Buffer.from('YWJvcnVtLg', 'base64')); 602 603{ 604 // Creating buffers larger than pool size. 605 const l = Buffer.poolSize + 5; 606 const s = 'h'.repeat(l); 607 const b = Buffer.from(s); 608 609 for (let i = 0; i < l; i++) { 610 assert.strictEqual(b[i], 'h'.charCodeAt(0)); 611 } 612 613 const sb = b.toString(); 614 assert.strictEqual(sb.length, s.length); 615 assert.strictEqual(sb, s); 616} 617 618{ 619 // test hex toString 620 const hexb = Buffer.allocUnsafe(256); 621 for (let i = 0; i < 256; i++) { 622 hexb[i] = i; 623 } 624 const hexStr = hexb.toString('hex'); 625 assert.strictEqual(hexStr, 626 '000102030405060708090a0b0c0d0e0f' + 627 '101112131415161718191a1b1c1d1e1f' + 628 '202122232425262728292a2b2c2d2e2f' + 629 '303132333435363738393a3b3c3d3e3f' + 630 '404142434445464748494a4b4c4d4e4f' + 631 '505152535455565758595a5b5c5d5e5f' + 632 '606162636465666768696a6b6c6d6e6f' + 633 '707172737475767778797a7b7c7d7e7f' + 634 '808182838485868788898a8b8c8d8e8f' + 635 '909192939495969798999a9b9c9d9e9f' + 636 'a0a1a2a3a4a5a6a7a8a9aaabacadaeaf' + 637 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' + 638 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf' + 639 'd0d1d2d3d4d5d6d7d8d9dadbdcdddedf' + 640 'e0e1e2e3e4e5e6e7e8e9eaebecedeeef' + 641 'f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'); 642 643 const hexb2 = Buffer.from(hexStr, 'hex'); 644 for (let i = 0; i < 256; i++) { 645 assert.strictEqual(hexb2[i], hexb[i]); 646 } 647} 648 649// Test single hex character is discarded. 650assert.strictEqual(Buffer.from('A', 'hex').length, 0); 651 652// Test that if a trailing character is discarded, rest of string is processed. 653assert.deepStrictEqual(Buffer.from('Abx', 'hex'), Buffer.from('Ab', 'hex')); 654 655// Test single base64 char encodes as 0. 656assert.strictEqual(Buffer.from('A', 'base64').length, 0); 657 658 659{ 660 // Test an invalid slice end. 661 const b = Buffer.from([1, 2, 3, 4, 5]); 662 const b2 = b.toString('hex', 1, 10000); 663 const b3 = b.toString('hex', 1, 5); 664 const b4 = b.toString('hex', 1); 665 assert.strictEqual(b2, b3); 666 assert.strictEqual(b2, b4); 667} 668 669function buildBuffer(data) { 670 if (Array.isArray(data)) { 671 const buffer = Buffer.allocUnsafe(data.length); 672 data.forEach((v, k) => buffer[k] = v); 673 return buffer; 674 } 675 return null; 676} 677 678const x = buildBuffer([0x81, 0xa3, 0x66, 0x6f, 0x6f, 0xa3, 0x62, 0x61, 0x72]); 679 680assert.strictEqual(x.inspect(), '<Buffer 81 a3 66 6f 6f a3 62 61 72>'); 681 682{ 683 const z = x.slice(4); 684 assert.strictEqual(z.length, 5); 685 assert.strictEqual(z[0], 0x6f); 686 assert.strictEqual(z[1], 0xa3); 687 assert.strictEqual(z[2], 0x62); 688 assert.strictEqual(z[3], 0x61); 689 assert.strictEqual(z[4], 0x72); 690} 691 692{ 693 const z = x.slice(0); 694 assert.strictEqual(z.length, x.length); 695} 696 697{ 698 const z = x.slice(0, 4); 699 assert.strictEqual(z.length, 4); 700 assert.strictEqual(z[0], 0x81); 701 assert.strictEqual(z[1], 0xa3); 702} 703 704{ 705 const z = x.slice(0, 9); 706 assert.strictEqual(z.length, 9); 707} 708 709{ 710 const z = x.slice(1, 4); 711 assert.strictEqual(z.length, 3); 712 assert.strictEqual(z[0], 0xa3); 713} 714 715{ 716 const z = x.slice(2, 4); 717 assert.strictEqual(z.length, 2); 718 assert.strictEqual(z[0], 0x66); 719 assert.strictEqual(z[1], 0x6f); 720} 721 722['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach((encoding) => { 723 const b = Buffer.allocUnsafe(10); 724 b.write('あいうえお', encoding); 725 assert.strictEqual(b.toString(encoding), 'あいうえお'); 726}); 727 728['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach((encoding) => { 729 const b = Buffer.allocUnsafe(11); 730 b.write('あいうえお', 1, encoding); 731 assert.strictEqual(b.toString(encoding, 1), 'あいうえお'); 732}); 733 734{ 735 // latin1 encoding should write only one byte per character. 736 const b = Buffer.from([0xde, 0xad, 0xbe, 0xef]); 737 let s = String.fromCharCode(0xffff); 738 b.write(s, 0, 'latin1'); 739 assert.strictEqual(b[0], 0xff); 740 assert.strictEqual(b[1], 0xad); 741 assert.strictEqual(b[2], 0xbe); 742 assert.strictEqual(b[3], 0xef); 743 s = String.fromCharCode(0xaaee); 744 b.write(s, 0, 'latin1'); 745 assert.strictEqual(b[0], 0xee); 746 assert.strictEqual(b[1], 0xad); 747 assert.strictEqual(b[2], 0xbe); 748 assert.strictEqual(b[3], 0xef); 749} 750 751{ 752 // Binary encoding should write only one byte per character. 753 const b = Buffer.from([0xde, 0xad, 0xbe, 0xef]); 754 let s = String.fromCharCode(0xffff); 755 b.write(s, 0, 'latin1'); 756 assert.strictEqual(b[0], 0xff); 757 assert.strictEqual(b[1], 0xad); 758 assert.strictEqual(b[2], 0xbe); 759 assert.strictEqual(b[3], 0xef); 760 s = String.fromCharCode(0xaaee); 761 b.write(s, 0, 'latin1'); 762 assert.strictEqual(b[0], 0xee); 763 assert.strictEqual(b[1], 0xad); 764 assert.strictEqual(b[2], 0xbe); 765 assert.strictEqual(b[3], 0xef); 766} 767 768{ 769 // https://github.com/nodejs/node-v0.x-archive/pull/1210 770 // Test UTF-8 string includes null character 771 let buf = Buffer.from('\0'); 772 assert.strictEqual(buf.length, 1); 773 buf = Buffer.from('\0\0'); 774 assert.strictEqual(buf.length, 2); 775} 776 777{ 778 const buf = Buffer.allocUnsafe(2); 779 assert.strictEqual(buf.write(''), 0); // 0bytes 780 assert.strictEqual(buf.write('\0'), 1); // 1byte (v8 adds null terminator) 781 assert.strictEqual(buf.write('a\0'), 2); // 1byte * 2 782 assert.strictEqual(buf.write('あ'), 0); // 3bytes 783 assert.strictEqual(buf.write('\0あ'), 1); // 1byte + 3bytes 784 assert.strictEqual(buf.write('\0\0あ'), 2); // 1byte * 2 + 3bytes 785} 786 787{ 788 const buf = Buffer.allocUnsafe(10); 789 assert.strictEqual(buf.write('あいう'), 9); // 3bytes * 3 (v8 adds null term.) 790 assert.strictEqual(buf.write('あいう\0'), 10); // 3bytes * 3 + 1byte 791} 792 793{ 794 // https://github.com/nodejs/node-v0.x-archive/issues/243 795 // Test write() with maxLength 796 const buf = Buffer.allocUnsafe(4); 797 buf.fill(0xFF); 798 assert.strictEqual(buf.write('abcd', 1, 2, 'utf8'), 2); 799 assert.strictEqual(buf[0], 0xFF); 800 assert.strictEqual(buf[1], 0x61); 801 assert.strictEqual(buf[2], 0x62); 802 assert.strictEqual(buf[3], 0xFF); 803 804 buf.fill(0xFF); 805 assert.strictEqual(buf.write('abcd', 1, 4), 3); 806 assert.strictEqual(buf[0], 0xFF); 807 assert.strictEqual(buf[1], 0x61); 808 assert.strictEqual(buf[2], 0x62); 809 assert.strictEqual(buf[3], 0x63); 810 811 buf.fill(0xFF); 812 assert.strictEqual(buf.write('abcd', 1, 2, 'utf8'), 2); 813 assert.strictEqual(buf[0], 0xFF); 814 assert.strictEqual(buf[1], 0x61); 815 assert.strictEqual(buf[2], 0x62); 816 assert.strictEqual(buf[3], 0xFF); 817 818 buf.fill(0xFF); 819 assert.strictEqual(buf.write('abcdef', 1, 2, 'hex'), 2); 820 assert.strictEqual(buf[0], 0xFF); 821 assert.strictEqual(buf[1], 0xAB); 822 assert.strictEqual(buf[2], 0xCD); 823 assert.strictEqual(buf[3], 0xFF); 824 825 ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach((encoding) => { 826 buf.fill(0xFF); 827 assert.strictEqual(buf.write('abcd', 0, 2, encoding), 2); 828 assert.strictEqual(buf[0], 0x61); 829 assert.strictEqual(buf[1], 0x00); 830 assert.strictEqual(buf[2], 0xFF); 831 assert.strictEqual(buf[3], 0xFF); 832 }); 833} 834 835{ 836 // Test offset returns are correct 837 const b = Buffer.allocUnsafe(16); 838 assert.strictEqual(b.writeUInt32LE(0, 0), 4); 839 assert.strictEqual(b.writeUInt16LE(0, 4), 6); 840 assert.strictEqual(b.writeUInt8(0, 6), 7); 841 assert.strictEqual(b.writeInt8(0, 7), 8); 842 assert.strictEqual(b.writeDoubleLE(0, 8), 16); 843} 844 845{ 846 // Test unmatched surrogates not producing invalid utf8 output 847 // ef bf bd = utf-8 representation of unicode replacement character 848 // see https://codereview.chromium.org/121173009/ 849 const buf = Buffer.from('ab\ud800cd', 'utf8'); 850 assert.strictEqual(buf[0], 0x61); 851 assert.strictEqual(buf[1], 0x62); 852 assert.strictEqual(buf[2], 0xef); 853 assert.strictEqual(buf[3], 0xbf); 854 assert.strictEqual(buf[4], 0xbd); 855 assert.strictEqual(buf[5], 0x63); 856 assert.strictEqual(buf[6], 0x64); 857} 858 859{ 860 // Test for buffer overrun 861 const buf = Buffer.from([0, 0, 0, 0, 0]); // length: 5 862 const sub = buf.slice(0, 4); // length: 4 863 assert.strictEqual(sub.write('12345', 'latin1'), 4); 864 assert.strictEqual(buf[4], 0); 865 assert.strictEqual(sub.write('12345', 'binary'), 4); 866 assert.strictEqual(buf[4], 0); 867} 868 869{ 870 // Test alloc with fill option 871 const buf = Buffer.alloc(5, '800A', 'hex'); 872 assert.strictEqual(buf[0], 128); 873 assert.strictEqual(buf[1], 10); 874 assert.strictEqual(buf[2], 128); 875 assert.strictEqual(buf[3], 10); 876 assert.strictEqual(buf[4], 128); 877} 878 879 880// Check for fractional length args, junk length args, etc. 881// https://github.com/joyent/node/issues/1758 882 883// Call .fill() first, stops valgrind warning about uninitialized memory reads. 884Buffer.allocUnsafe(3.3).fill().toString(); 885// Throws bad argument error in commit 43cb4ec 886Buffer.alloc(3.3).fill().toString(); 887assert.strictEqual(Buffer.allocUnsafe(3.3).length, 3); 888assert.strictEqual(Buffer.from({ length: 3.3 }).length, 3); 889assert.strictEqual(Buffer.from({ length: 'BAM' }).length, 0); 890 891// Make sure that strings are not coerced to numbers. 892assert.strictEqual(Buffer.from('99').length, 2); 893assert.strictEqual(Buffer.from('13.37').length, 5); 894 895// Ensure that the length argument is respected. 896['ascii', 'utf8', 'hex', 'base64', 'latin1', 'binary'].forEach((enc) => { 897 assert.strictEqual(Buffer.allocUnsafe(1).write('aaaaaa', 0, 1, enc), 1); 898}); 899 900{ 901 // Regression test, guard against buffer overrun in the base64 decoder. 902 const a = Buffer.allocUnsafe(3); 903 const b = Buffer.from('xxx'); 904 a.write('aaaaaaaa', 'base64'); 905 assert.strictEqual(b.toString(), 'xxx'); 906} 907 908// issue GH-3416 909Buffer.from(Buffer.allocUnsafe(0), 0, 0); 910 911// issue GH-5587 912assert.throws( 913 () => Buffer.alloc(8).writeFloatLE(0, 5), 914 outOfRangeError 915); 916assert.throws( 917 () => Buffer.alloc(16).writeDoubleLE(0, 9), 918 outOfRangeError 919); 920 921// Attempt to overflow buffers, similar to previous bug in array buffers 922assert.throws( 923 () => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff), 924 outOfRangeError 925); 926assert.throws( 927 () => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff), 928 outOfRangeError 929); 930 931// Ensure negative values can't get past offset 932assert.throws( 933 () => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), 934 outOfRangeError 935); 936assert.throws( 937 () => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), 938 outOfRangeError 939); 940 941// Test for common write(U)IntLE/BE 942{ 943 let buf = Buffer.allocUnsafe(3); 944 buf.writeUIntLE(0x123456, 0, 3); 945 assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]); 946 assert.strictEqual(buf.readUIntLE(0, 3), 0x123456); 947 948 buf.fill(0xFF); 949 buf.writeUIntBE(0x123456, 0, 3); 950 assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56]); 951 assert.strictEqual(buf.readUIntBE(0, 3), 0x123456); 952 953 buf.fill(0xFF); 954 buf.writeIntLE(0x123456, 0, 3); 955 assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]); 956 assert.strictEqual(buf.readIntLE(0, 3), 0x123456); 957 958 buf.fill(0xFF); 959 buf.writeIntBE(0x123456, 0, 3); 960 assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56]); 961 assert.strictEqual(buf.readIntBE(0, 3), 0x123456); 962 963 buf.fill(0xFF); 964 buf.writeIntLE(-0x123456, 0, 3); 965 assert.deepStrictEqual(buf.toJSON().data, [0xaa, 0xcb, 0xed]); 966 assert.strictEqual(buf.readIntLE(0, 3), -0x123456); 967 968 buf.fill(0xFF); 969 buf.writeIntBE(-0x123456, 0, 3); 970 assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcb, 0xaa]); 971 assert.strictEqual(buf.readIntBE(0, 3), -0x123456); 972 973 buf.fill(0xFF); 974 buf.writeIntLE(-0x123400, 0, 3); 975 assert.deepStrictEqual(buf.toJSON().data, [0x00, 0xcc, 0xed]); 976 assert.strictEqual(buf.readIntLE(0, 3), -0x123400); 977 978 buf.fill(0xFF); 979 buf.writeIntBE(-0x123400, 0, 3); 980 assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcc, 0x00]); 981 assert.strictEqual(buf.readIntBE(0, 3), -0x123400); 982 983 buf.fill(0xFF); 984 buf.writeIntLE(-0x120000, 0, 3); 985 assert.deepStrictEqual(buf.toJSON().data, [0x00, 0x00, 0xee]); 986 assert.strictEqual(buf.readIntLE(0, 3), -0x120000); 987 988 buf.fill(0xFF); 989 buf.writeIntBE(-0x120000, 0, 3); 990 assert.deepStrictEqual(buf.toJSON().data, [0xee, 0x00, 0x00]); 991 assert.strictEqual(buf.readIntBE(0, 3), -0x120000); 992 993 buf = Buffer.allocUnsafe(5); 994 buf.writeUIntLE(0x1234567890, 0, 5); 995 assert.deepStrictEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]); 996 assert.strictEqual(buf.readUIntLE(0, 5), 0x1234567890); 997 998 buf.fill(0xFF); 999 buf.writeUIntBE(0x1234567890, 0, 5); 1000 assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]); 1001 assert.strictEqual(buf.readUIntBE(0, 5), 0x1234567890); 1002 1003 buf.fill(0xFF); 1004 buf.writeIntLE(0x1234567890, 0, 5); 1005 assert.deepStrictEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]); 1006 assert.strictEqual(buf.readIntLE(0, 5), 0x1234567890); 1007 1008 buf.fill(0xFF); 1009 buf.writeIntBE(0x1234567890, 0, 5); 1010 assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]); 1011 assert.strictEqual(buf.readIntBE(0, 5), 0x1234567890); 1012 1013 buf.fill(0xFF); 1014 buf.writeIntLE(-0x1234567890, 0, 5); 1015 assert.deepStrictEqual(buf.toJSON().data, [0x70, 0x87, 0xa9, 0xcb, 0xed]); 1016 assert.strictEqual(buf.readIntLE(0, 5), -0x1234567890); 1017 1018 buf.fill(0xFF); 1019 buf.writeIntBE(-0x1234567890, 0, 5); 1020 assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcb, 0xa9, 0x87, 0x70]); 1021 assert.strictEqual(buf.readIntBE(0, 5), -0x1234567890); 1022 1023 buf.fill(0xFF); 1024 buf.writeIntLE(-0x0012000000, 0, 5); 1025 assert.deepStrictEqual(buf.toJSON().data, [0x00, 0x00, 0x00, 0xee, 0xff]); 1026 assert.strictEqual(buf.readIntLE(0, 5), -0x0012000000); 1027 1028 buf.fill(0xFF); 1029 buf.writeIntBE(-0x0012000000, 0, 5); 1030 assert.deepStrictEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]); 1031 assert.strictEqual(buf.readIntBE(0, 5), -0x0012000000); 1032} 1033 1034// Regression test for https://github.com/nodejs/node-v0.x-archive/issues/5482: 1035// should throw but not assert in C++ land. 1036assert.throws( 1037 () => Buffer.from('', 'buffer'), 1038 { 1039 code: 'ERR_UNKNOWN_ENCODING', 1040 name: 'TypeError', 1041 message: 'Unknown encoding: buffer' 1042 } 1043); 1044 1045// Regression test for https://github.com/nodejs/node-v0.x-archive/issues/6111. 1046// Constructing a buffer from another buffer should a) work, and b) not corrupt 1047// the source buffer. 1048{ 1049 const a = [...Array(128).keys()]; // [0, 1, 2, 3, ... 126, 127] 1050 const b = Buffer.from(a); 1051 const c = Buffer.from(b); 1052 assert.strictEqual(b.length, a.length); 1053 assert.strictEqual(c.length, a.length); 1054 for (let i = 0, k = a.length; i < k; ++i) { 1055 assert.strictEqual(a[i], i); 1056 assert.strictEqual(b[i], i); 1057 assert.strictEqual(c[i], i); 1058 } 1059} 1060 1061if (common.hasCrypto) { // eslint-disable-line node-core/crypto-check 1062 // Test truncation after decode 1063 const crypto = require('crypto'); 1064 1065 const b1 = Buffer.from('YW55=======', 'base64'); 1066 const b2 = Buffer.from('YW55', 'base64'); 1067 1068 assert.strictEqual( 1069 crypto.createHash('sha1').update(b1).digest('hex'), 1070 crypto.createHash('sha1').update(b2).digest('hex') 1071 ); 1072} else { 1073 common.printSkipMessage('missing crypto'); 1074} 1075 1076const ps = Buffer.poolSize; 1077Buffer.poolSize = 0; 1078assert(Buffer.allocUnsafe(1).parent instanceof ArrayBuffer); 1079Buffer.poolSize = ps; 1080 1081assert.throws( 1082 () => Buffer.allocUnsafe(10).copy(), 1083 { 1084 code: 'ERR_INVALID_ARG_TYPE', 1085 name: 'TypeError', 1086 message: 'The "target" argument must be an instance of Buffer or ' + 1087 'Uint8Array. Received undefined' 1088 }); 1089 1090assert.throws(() => Buffer.from(), { 1091 name: 'TypeError', 1092 message: 'The first argument must be of type string or an instance of ' + 1093 'Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined' 1094}); 1095assert.throws(() => Buffer.from(null), { 1096 name: 'TypeError', 1097 message: 'The first argument must be of type string or an instance of ' + 1098 'Buffer, ArrayBuffer, or Array or an Array-like Object. Received null' 1099}); 1100 1101// Test prototype getters don't throw 1102assert.strictEqual(Buffer.prototype.parent, undefined); 1103assert.strictEqual(Buffer.prototype.offset, undefined); 1104assert.strictEqual(SlowBuffer.prototype.parent, undefined); 1105assert.strictEqual(SlowBuffer.prototype.offset, undefined); 1106 1107 1108{ 1109 // Test that large negative Buffer length inputs don't affect the pool offset. 1110 // Use the fromArrayLike() variant here because it's more lenient 1111 // about its input and passes the length directly to allocate(). 1112 assert.deepStrictEqual(Buffer.from({ length: -Buffer.poolSize }), 1113 Buffer.from('')); 1114 assert.deepStrictEqual(Buffer.from({ length: -100 }), 1115 Buffer.from('')); 1116 1117 // Check pool offset after that by trying to write string into the pool. 1118 Buffer.from('abc'); 1119} 1120 1121 1122// Test that ParseArrayIndex handles full uint32 1123{ 1124 const errMsg = common.expectsError({ 1125 code: 'ERR_BUFFER_OUT_OF_BOUNDS', 1126 name: 'RangeError', 1127 message: '"offset" is outside of buffer bounds' 1128 }); 1129 assert.throws(() => Buffer.from(new ArrayBuffer(0), -1 >>> 0), errMsg); 1130} 1131 1132// ParseArrayIndex() should reject values that don't fit in a 32 bits size_t. 1133assert.throws(() => { 1134 const a = Buffer.alloc(1); 1135 const b = Buffer.alloc(1); 1136 a.copy(b, 0, 0x100000000, 0x100000001); 1137}, outOfRangeError); 1138 1139// Unpooled buffer (replaces SlowBuffer) 1140{ 1141 const ubuf = Buffer.allocUnsafeSlow(10); 1142 assert(ubuf); 1143 assert(ubuf.buffer); 1144 assert.strictEqual(ubuf.buffer.byteLength, 10); 1145} 1146 1147// Regression test to verify that an empty ArrayBuffer does not throw. 1148Buffer.from(new ArrayBuffer()); 1149 1150// Test that ArrayBuffer from a different context is detected correctly. 1151const arrayBuf = vm.runInNewContext('new ArrayBuffer()'); 1152Buffer.from(arrayBuf); 1153Buffer.from({ buffer: arrayBuf }); 1154 1155assert.throws(() => Buffer.alloc({ valueOf: () => 1 }), 1156 /"size" argument must be of type number/); 1157assert.throws(() => Buffer.alloc({ valueOf: () => -1 }), 1158 /"size" argument must be of type number/); 1159 1160assert.strictEqual(Buffer.prototype.toLocaleString, Buffer.prototype.toString); 1161{ 1162 const buf = Buffer.from('test'); 1163 assert.strictEqual(buf.toLocaleString(), buf.toString()); 1164} 1165 1166assert.throws(() => { 1167 Buffer.alloc(0x1000, 'This is not correctly encoded', 'hex'); 1168}, { 1169 code: 'ERR_INVALID_ARG_VALUE', 1170 name: 'TypeError' 1171}); 1172 1173assert.throws(() => { 1174 Buffer.alloc(0x1000, 'c', 'hex'); 1175}, { 1176 code: 'ERR_INVALID_ARG_VALUE', 1177 name: 'TypeError' 1178}); 1179 1180assert.throws(() => { 1181 Buffer.alloc(1, Buffer.alloc(0)); 1182}, { 1183 code: 'ERR_INVALID_ARG_VALUE', 1184 name: 'TypeError' 1185}); 1186 1187assert.throws(() => { 1188 Buffer.alloc(40, 'x', 20); 1189}, { 1190 code: 'ERR_INVALID_ARG_TYPE', 1191 name: 'TypeError' 1192}); 1193