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