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'); 295 296{ 297 // Test that regular and URL-safe base64 both work 298 const expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff]; 299 assert.deepStrictEqual(Buffer.from('//++/++/++//', 'base64'), 300 Buffer.from(expected)); 301 assert.deepStrictEqual(Buffer.from('__--_--_--__', 'base64'), 302 Buffer.from(expected)); 303} 304 305{ 306 // big example 307 const quote = 'Man is distinguished, not only by his reason, but by this ' + 308 'singular passion from other animals, which is a lust ' + 309 'of the mind, that by a perseverance of delight in the ' + 310 'continued and indefatigable generation of knowledge, ' + 311 'exceeds the short vehemence of any carnal pleasure.'; 312 const expected = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb' + 313 '24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci' + 314 'BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQ' + 315 'gYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu' + 316 'dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZ' + 317 'GdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm' + 318 '5hbCBwbGVhc3VyZS4='; 319 assert.strictEqual((Buffer.from(quote)).toString('base64'), expected); 320 321 let b = Buffer.allocUnsafe(1024); 322 let bytesWritten = b.write(expected, 0, 'base64'); 323 assert.strictEqual(quote.length, bytesWritten); 324 assert.strictEqual(quote, b.toString('ascii', 0, quote.length)); 325 326 // Check that the base64 decoder ignores whitespace 327 const expectedWhite = `${expected.slice(0, 60)} \n` + 328 `${expected.slice(60, 120)} \n` + 329 `${expected.slice(120, 180)} \n` + 330 `${expected.slice(180, 240)} \n` + 331 `${expected.slice(240, 300)}\n` + 332 `${expected.slice(300, 360)}\n`; 333 b = Buffer.allocUnsafe(1024); 334 bytesWritten = b.write(expectedWhite, 0, 'base64'); 335 assert.strictEqual(quote.length, bytesWritten); 336 assert.strictEqual(quote, b.toString('ascii', 0, quote.length)); 337 338 // Check that the base64 decoder on the constructor works 339 // even in the presence of whitespace. 340 b = Buffer.from(expectedWhite, 'base64'); 341 assert.strictEqual(quote.length, b.length); 342 assert.strictEqual(quote, b.toString('ascii', 0, quote.length)); 343 344 // Check that the base64 decoder ignores illegal chars 345 const expectedIllegal = expected.slice(0, 60) + ' \x80' + 346 expected.slice(60, 120) + ' \xff' + 347 expected.slice(120, 180) + ' \x00' + 348 expected.slice(180, 240) + ' \x98' + 349 expected.slice(240, 300) + '\x03' + 350 expected.slice(300, 360); 351 b = Buffer.from(expectedIllegal, 'base64'); 352 assert.strictEqual(quote.length, b.length); 353 assert.strictEqual(quote, b.toString('ascii', 0, quote.length)); 354} 355 356assert.strictEqual(Buffer.from('', 'base64').toString(), ''); 357assert.strictEqual(Buffer.from('K', 'base64').toString(), ''); 358 359// multiple-of-4 with padding 360assert.strictEqual(Buffer.from('Kg==', 'base64').toString(), '*'); 361assert.strictEqual(Buffer.from('Kio=', 'base64').toString(), '*'.repeat(2)); 362assert.strictEqual(Buffer.from('Kioq', 'base64').toString(), '*'.repeat(3)); 363assert.strictEqual(Buffer.from('KioqKg==', 'base64').toString(), '*'.repeat(4)); 364assert.strictEqual(Buffer.from('KioqKio=', 'base64').toString(), '*'.repeat(5)); 365assert.strictEqual(Buffer.from('KioqKioq', 'base64').toString(), '*'.repeat(6)); 366assert.strictEqual(Buffer.from('KioqKioqKg==', 'base64').toString(), 367 '*'.repeat(7)); 368assert.strictEqual(Buffer.from('KioqKioqKio=', 'base64').toString(), 369 '*'.repeat(8)); 370assert.strictEqual(Buffer.from('KioqKioqKioq', 'base64').toString(), 371 '*'.repeat(9)); 372assert.strictEqual(Buffer.from('KioqKioqKioqKg==', 'base64').toString(), 373 '*'.repeat(10)); 374assert.strictEqual(Buffer.from('KioqKioqKioqKio=', 'base64').toString(), 375 '*'.repeat(11)); 376assert.strictEqual(Buffer.from('KioqKioqKioqKioq', 'base64').toString(), 377 '*'.repeat(12)); 378assert.strictEqual(Buffer.from('KioqKioqKioqKioqKg==', 'base64').toString(), 379 '*'.repeat(13)); 380assert.strictEqual(Buffer.from('KioqKioqKioqKioqKio=', 'base64').toString(), 381 '*'.repeat(14)); 382assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioq', 'base64').toString(), 383 '*'.repeat(15)); 384assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKg==', 'base64').toString(), 385 '*'.repeat(16)); 386assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKio=', 'base64').toString(), 387 '*'.repeat(17)); 388assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKioq', 'base64').toString(), 389 '*'.repeat(18)); 390assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKioqKg==', 391 'base64').toString(), 392 '*'.repeat(19)); 393assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKioqKio=', 394 'base64').toString(), 395 '*'.repeat(20)); 396 397// No padding, not a multiple of 4 398assert.strictEqual(Buffer.from('Kg', 'base64').toString(), '*'); 399assert.strictEqual(Buffer.from('Kio', 'base64').toString(), '*'.repeat(2)); 400assert.strictEqual(Buffer.from('KioqKg', 'base64').toString(), '*'.repeat(4)); 401assert.strictEqual(Buffer.from('KioqKio', 'base64').toString(), '*'.repeat(5)); 402assert.strictEqual(Buffer.from('KioqKioqKg', 'base64').toString(), 403 '*'.repeat(7)); 404assert.strictEqual(Buffer.from('KioqKioqKio', 'base64').toString(), 405 '*'.repeat(8)); 406assert.strictEqual(Buffer.from('KioqKioqKioqKg', 'base64').toString(), 407 '*'.repeat(10)); 408assert.strictEqual(Buffer.from('KioqKioqKioqKio', 'base64').toString(), 409 '*'.repeat(11)); 410assert.strictEqual(Buffer.from('KioqKioqKioqKioqKg', 'base64').toString(), 411 '*'.repeat(13)); 412assert.strictEqual(Buffer.from('KioqKioqKioqKioqKio', 'base64').toString(), 413 '*'.repeat(14)); 414assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKg', 'base64').toString(), 415 '*'.repeat(16)); 416assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKio', 'base64').toString(), 417 '*'.repeat(17)); 418assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKioqKg', 419 'base64').toString(), 420 '*'.repeat(19)); 421assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKioqKio', 422 'base64').toString(), 423 '*'.repeat(20)); 424 425// Handle padding graciously, multiple-of-4 or not 426assert.strictEqual( 427 Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw==', 'base64').length, 428 32 429); 430assert.strictEqual( 431 Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw=', 'base64').length, 432 32 433); 434assert.strictEqual( 435 Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw', 'base64').length, 436 32 437); 438assert.strictEqual( 439 Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg==', 'base64').length, 440 31 441); 442assert.strictEqual( 443 Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg=', 'base64').length, 444 31 445); 446assert.strictEqual( 447 Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg', 'base64').length, 448 31 449); 450 451{ 452// This string encodes single '.' character in UTF-16 453 const dot = Buffer.from('//4uAA==', 'base64'); 454 assert.strictEqual(dot[0], 0xff); 455 assert.strictEqual(dot[1], 0xfe); 456 assert.strictEqual(dot[2], 0x2e); 457 assert.strictEqual(dot[3], 0x00); 458 assert.strictEqual(dot.toString('base64'), '//4uAA=='); 459} 460 461{ 462 // Writing base64 at a position > 0 should not mangle the result. 463 // 464 // https://github.com/joyent/node/issues/402 465 const segments = ['TWFkbmVzcz8h', 'IFRoaXM=', 'IGlz', 'IG5vZGUuanMh']; 466 const b = Buffer.allocUnsafe(64); 467 let pos = 0; 468 469 for (let i = 0; i < segments.length; ++i) { 470 pos += b.write(segments[i], pos, 'base64'); 471 } 472 assert.strictEqual(b.toString('latin1', 0, pos), 473 'Madness?! This is node.js!'); 474} 475 476// Regression test for https://github.com/nodejs/node/issues/3496. 477assert.strictEqual(Buffer.from('=bad'.repeat(1e4), 'base64').length, 0); 478 479// Regression test for https://github.com/nodejs/node/issues/11987. 480assert.deepStrictEqual(Buffer.from('w0 ', 'base64'), 481 Buffer.from('w0', 'base64')); 482 483// Regression test for https://github.com/nodejs/node/issues/13657. 484assert.deepStrictEqual(Buffer.from(' YWJvcnVtLg', 'base64'), 485 Buffer.from('YWJvcnVtLg', 'base64')); 486 487{ 488 // Creating buffers larger than pool size. 489 const l = Buffer.poolSize + 5; 490 const s = 'h'.repeat(l); 491 const b = Buffer.from(s); 492 493 for (let i = 0; i < l; i++) { 494 assert.strictEqual(b[i], 'h'.charCodeAt(0)); 495 } 496 497 const sb = b.toString(); 498 assert.strictEqual(sb.length, s.length); 499 assert.strictEqual(sb, s); 500} 501 502{ 503 // test hex toString 504 const hexb = Buffer.allocUnsafe(256); 505 for (let i = 0; i < 256; i++) { 506 hexb[i] = i; 507 } 508 const hexStr = hexb.toString('hex'); 509 assert.strictEqual(hexStr, 510 '000102030405060708090a0b0c0d0e0f' + 511 '101112131415161718191a1b1c1d1e1f' + 512 '202122232425262728292a2b2c2d2e2f' + 513 '303132333435363738393a3b3c3d3e3f' + 514 '404142434445464748494a4b4c4d4e4f' + 515 '505152535455565758595a5b5c5d5e5f' + 516 '606162636465666768696a6b6c6d6e6f' + 517 '707172737475767778797a7b7c7d7e7f' + 518 '808182838485868788898a8b8c8d8e8f' + 519 '909192939495969798999a9b9c9d9e9f' + 520 'a0a1a2a3a4a5a6a7a8a9aaabacadaeaf' + 521 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' + 522 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf' + 523 'd0d1d2d3d4d5d6d7d8d9dadbdcdddedf' + 524 'e0e1e2e3e4e5e6e7e8e9eaebecedeeef' + 525 'f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'); 526 527 const hexb2 = Buffer.from(hexStr, 'hex'); 528 for (let i = 0; i < 256; i++) { 529 assert.strictEqual(hexb2[i], hexb[i]); 530 } 531} 532 533// Test single hex character is discarded. 534assert.strictEqual(Buffer.from('A', 'hex').length, 0); 535 536// Test that if a trailing character is discarded, rest of string is processed. 537assert.deepStrictEqual(Buffer.from('Abx', 'hex'), Buffer.from('Ab', 'hex')); 538 539// Test single base64 char encodes as 0. 540assert.strictEqual(Buffer.from('A', 'base64').length, 0); 541 542 543{ 544 // Test an invalid slice end. 545 const b = Buffer.from([1, 2, 3, 4, 5]); 546 const b2 = b.toString('hex', 1, 10000); 547 const b3 = b.toString('hex', 1, 5); 548 const b4 = b.toString('hex', 1); 549 assert.strictEqual(b2, b3); 550 assert.strictEqual(b2, b4); 551} 552 553function buildBuffer(data) { 554 if (Array.isArray(data)) { 555 const buffer = Buffer.allocUnsafe(data.length); 556 data.forEach((v, k) => buffer[k] = v); 557 return buffer; 558 } 559 return null; 560} 561 562const x = buildBuffer([0x81, 0xa3, 0x66, 0x6f, 0x6f, 0xa3, 0x62, 0x61, 0x72]); 563 564assert.strictEqual(x.inspect(), '<Buffer 81 a3 66 6f 6f a3 62 61 72>'); 565 566{ 567 const z = x.slice(4); 568 assert.strictEqual(z.length, 5); 569 assert.strictEqual(z[0], 0x6f); 570 assert.strictEqual(z[1], 0xa3); 571 assert.strictEqual(z[2], 0x62); 572 assert.strictEqual(z[3], 0x61); 573 assert.strictEqual(z[4], 0x72); 574} 575 576{ 577 const z = x.slice(0); 578 assert.strictEqual(z.length, x.length); 579} 580 581{ 582 const z = x.slice(0, 4); 583 assert.strictEqual(z.length, 4); 584 assert.strictEqual(z[0], 0x81); 585 assert.strictEqual(z[1], 0xa3); 586} 587 588{ 589 const z = x.slice(0, 9); 590 assert.strictEqual(z.length, 9); 591} 592 593{ 594 const z = x.slice(1, 4); 595 assert.strictEqual(z.length, 3); 596 assert.strictEqual(z[0], 0xa3); 597} 598 599{ 600 const z = x.slice(2, 4); 601 assert.strictEqual(z.length, 2); 602 assert.strictEqual(z[0], 0x66); 603 assert.strictEqual(z[1], 0x6f); 604} 605 606['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach((encoding) => { 607 const b = Buffer.allocUnsafe(10); 608 b.write('あいうえお', encoding); 609 assert.strictEqual(b.toString(encoding), 'あいうえお'); 610}); 611 612['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach((encoding) => { 613 const b = Buffer.allocUnsafe(11); 614 b.write('あいうえお', 1, encoding); 615 assert.strictEqual(b.toString(encoding, 1), 'あいうえお'); 616}); 617 618{ 619 // latin1 encoding should write only one byte per character. 620 const b = Buffer.from([0xde, 0xad, 0xbe, 0xef]); 621 let s = String.fromCharCode(0xffff); 622 b.write(s, 0, 'latin1'); 623 assert.strictEqual(b[0], 0xff); 624 assert.strictEqual(b[1], 0xad); 625 assert.strictEqual(b[2], 0xbe); 626 assert.strictEqual(b[3], 0xef); 627 s = String.fromCharCode(0xaaee); 628 b.write(s, 0, 'latin1'); 629 assert.strictEqual(b[0], 0xee); 630 assert.strictEqual(b[1], 0xad); 631 assert.strictEqual(b[2], 0xbe); 632 assert.strictEqual(b[3], 0xef); 633} 634 635{ 636 // Binary encoding should write only one byte per character. 637 const b = Buffer.from([0xde, 0xad, 0xbe, 0xef]); 638 let s = String.fromCharCode(0xffff); 639 b.write(s, 0, 'latin1'); 640 assert.strictEqual(b[0], 0xff); 641 assert.strictEqual(b[1], 0xad); 642 assert.strictEqual(b[2], 0xbe); 643 assert.strictEqual(b[3], 0xef); 644 s = String.fromCharCode(0xaaee); 645 b.write(s, 0, 'latin1'); 646 assert.strictEqual(b[0], 0xee); 647 assert.strictEqual(b[1], 0xad); 648 assert.strictEqual(b[2], 0xbe); 649 assert.strictEqual(b[3], 0xef); 650} 651 652{ 653 // https://github.com/nodejs/node-v0.x-archive/pull/1210 654 // Test UTF-8 string includes null character 655 let buf = Buffer.from('\0'); 656 assert.strictEqual(buf.length, 1); 657 buf = Buffer.from('\0\0'); 658 assert.strictEqual(buf.length, 2); 659} 660 661{ 662 const buf = Buffer.allocUnsafe(2); 663 assert.strictEqual(buf.write(''), 0); // 0bytes 664 assert.strictEqual(buf.write('\0'), 1); // 1byte (v8 adds null terminator) 665 assert.strictEqual(buf.write('a\0'), 2); // 1byte * 2 666 assert.strictEqual(buf.write('あ'), 0); // 3bytes 667 assert.strictEqual(buf.write('\0あ'), 1); // 1byte + 3bytes 668 assert.strictEqual(buf.write('\0\0あ'), 2); // 1byte * 2 + 3bytes 669} 670 671{ 672 const buf = Buffer.allocUnsafe(10); 673 assert.strictEqual(buf.write('あいう'), 9); // 3bytes * 3 (v8 adds null term.) 674 assert.strictEqual(buf.write('あいう\0'), 10); // 3bytes * 3 + 1byte 675} 676 677{ 678 // https://github.com/nodejs/node-v0.x-archive/issues/243 679 // Test write() with maxLength 680 const buf = Buffer.allocUnsafe(4); 681 buf.fill(0xFF); 682 assert.strictEqual(buf.write('abcd', 1, 2, 'utf8'), 2); 683 assert.strictEqual(buf[0], 0xFF); 684 assert.strictEqual(buf[1], 0x61); 685 assert.strictEqual(buf[2], 0x62); 686 assert.strictEqual(buf[3], 0xFF); 687 688 buf.fill(0xFF); 689 assert.strictEqual(buf.write('abcd', 1, 4), 3); 690 assert.strictEqual(buf[0], 0xFF); 691 assert.strictEqual(buf[1], 0x61); 692 assert.strictEqual(buf[2], 0x62); 693 assert.strictEqual(buf[3], 0x63); 694 695 buf.fill(0xFF); 696 assert.strictEqual(buf.write('abcd', 1, 2, 'utf8'), 2); 697 assert.strictEqual(buf[0], 0xFF); 698 assert.strictEqual(buf[1], 0x61); 699 assert.strictEqual(buf[2], 0x62); 700 assert.strictEqual(buf[3], 0xFF); 701 702 buf.fill(0xFF); 703 assert.strictEqual(buf.write('abcdef', 1, 2, 'hex'), 2); 704 assert.strictEqual(buf[0], 0xFF); 705 assert.strictEqual(buf[1], 0xAB); 706 assert.strictEqual(buf[2], 0xCD); 707 assert.strictEqual(buf[3], 0xFF); 708 709 ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach((encoding) => { 710 buf.fill(0xFF); 711 assert.strictEqual(buf.write('abcd', 0, 2, encoding), 2); 712 assert.strictEqual(buf[0], 0x61); 713 assert.strictEqual(buf[1], 0x00); 714 assert.strictEqual(buf[2], 0xFF); 715 assert.strictEqual(buf[3], 0xFF); 716 }); 717} 718 719{ 720 // Test offset returns are correct 721 const b = Buffer.allocUnsafe(16); 722 assert.strictEqual(b.writeUInt32LE(0, 0), 4); 723 assert.strictEqual(b.writeUInt16LE(0, 4), 6); 724 assert.strictEqual(b.writeUInt8(0, 6), 7); 725 assert.strictEqual(b.writeInt8(0, 7), 8); 726 assert.strictEqual(b.writeDoubleLE(0, 8), 16); 727} 728 729{ 730 // Test unmatched surrogates not producing invalid utf8 output 731 // ef bf bd = utf-8 representation of unicode replacement character 732 // see https://codereview.chromium.org/121173009/ 733 const buf = Buffer.from('ab\ud800cd', 'utf8'); 734 assert.strictEqual(buf[0], 0x61); 735 assert.strictEqual(buf[1], 0x62); 736 assert.strictEqual(buf[2], 0xef); 737 assert.strictEqual(buf[3], 0xbf); 738 assert.strictEqual(buf[4], 0xbd); 739 assert.strictEqual(buf[5], 0x63); 740 assert.strictEqual(buf[6], 0x64); 741} 742 743{ 744 // Test for buffer overrun 745 const buf = Buffer.from([0, 0, 0, 0, 0]); // length: 5 746 const sub = buf.slice(0, 4); // length: 4 747 assert.strictEqual(sub.write('12345', 'latin1'), 4); 748 assert.strictEqual(buf[4], 0); 749 assert.strictEqual(sub.write('12345', 'binary'), 4); 750 assert.strictEqual(buf[4], 0); 751} 752 753{ 754 // Test alloc with fill option 755 const buf = Buffer.alloc(5, '800A', 'hex'); 756 assert.strictEqual(buf[0], 128); 757 assert.strictEqual(buf[1], 10); 758 assert.strictEqual(buf[2], 128); 759 assert.strictEqual(buf[3], 10); 760 assert.strictEqual(buf[4], 128); 761} 762 763 764// Check for fractional length args, junk length args, etc. 765// https://github.com/joyent/node/issues/1758 766 767// Call .fill() first, stops valgrind warning about uninitialized memory reads. 768Buffer.allocUnsafe(3.3).fill().toString(); 769// Throws bad argument error in commit 43cb4ec 770Buffer.alloc(3.3).fill().toString(); 771assert.strictEqual(Buffer.allocUnsafe(3.3).length, 3); 772assert.strictEqual(Buffer.from({ length: 3.3 }).length, 3); 773assert.strictEqual(Buffer.from({ length: 'BAM' }).length, 0); 774 775// Make sure that strings are not coerced to numbers. 776assert.strictEqual(Buffer.from('99').length, 2); 777assert.strictEqual(Buffer.from('13.37').length, 5); 778 779// Ensure that the length argument is respected. 780['ascii', 'utf8', 'hex', 'base64', 'latin1', 'binary'].forEach((enc) => { 781 assert.strictEqual(Buffer.allocUnsafe(1).write('aaaaaa', 0, 1, enc), 1); 782}); 783 784{ 785 // Regression test, guard against buffer overrun in the base64 decoder. 786 const a = Buffer.allocUnsafe(3); 787 const b = Buffer.from('xxx'); 788 a.write('aaaaaaaa', 'base64'); 789 assert.strictEqual(b.toString(), 'xxx'); 790} 791 792// issue GH-3416 793Buffer.from(Buffer.allocUnsafe(0), 0, 0); 794 795// issue GH-5587 796assert.throws( 797 () => Buffer.alloc(8).writeFloatLE(0, 5), 798 outOfRangeError 799); 800assert.throws( 801 () => Buffer.alloc(16).writeDoubleLE(0, 9), 802 outOfRangeError 803); 804 805// Attempt to overflow buffers, similar to previous bug in array buffers 806assert.throws( 807 () => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff), 808 outOfRangeError 809); 810assert.throws( 811 () => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff), 812 outOfRangeError 813); 814 815// Ensure negative values can't get past offset 816assert.throws( 817 () => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), 818 outOfRangeError 819); 820assert.throws( 821 () => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), 822 outOfRangeError 823); 824 825// Test for common write(U)IntLE/BE 826{ 827 let buf = Buffer.allocUnsafe(3); 828 buf.writeUIntLE(0x123456, 0, 3); 829 assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]); 830 assert.strictEqual(buf.readUIntLE(0, 3), 0x123456); 831 832 buf.fill(0xFF); 833 buf.writeUIntBE(0x123456, 0, 3); 834 assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56]); 835 assert.strictEqual(buf.readUIntBE(0, 3), 0x123456); 836 837 buf.fill(0xFF); 838 buf.writeIntLE(0x123456, 0, 3); 839 assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]); 840 assert.strictEqual(buf.readIntLE(0, 3), 0x123456); 841 842 buf.fill(0xFF); 843 buf.writeIntBE(0x123456, 0, 3); 844 assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56]); 845 assert.strictEqual(buf.readIntBE(0, 3), 0x123456); 846 847 buf.fill(0xFF); 848 buf.writeIntLE(-0x123456, 0, 3); 849 assert.deepStrictEqual(buf.toJSON().data, [0xaa, 0xcb, 0xed]); 850 assert.strictEqual(buf.readIntLE(0, 3), -0x123456); 851 852 buf.fill(0xFF); 853 buf.writeIntBE(-0x123456, 0, 3); 854 assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcb, 0xaa]); 855 assert.strictEqual(buf.readIntBE(0, 3), -0x123456); 856 857 buf.fill(0xFF); 858 buf.writeIntLE(-0x123400, 0, 3); 859 assert.deepStrictEqual(buf.toJSON().data, [0x00, 0xcc, 0xed]); 860 assert.strictEqual(buf.readIntLE(0, 3), -0x123400); 861 862 buf.fill(0xFF); 863 buf.writeIntBE(-0x123400, 0, 3); 864 assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcc, 0x00]); 865 assert.strictEqual(buf.readIntBE(0, 3), -0x123400); 866 867 buf.fill(0xFF); 868 buf.writeIntLE(-0x120000, 0, 3); 869 assert.deepStrictEqual(buf.toJSON().data, [0x00, 0x00, 0xee]); 870 assert.strictEqual(buf.readIntLE(0, 3), -0x120000); 871 872 buf.fill(0xFF); 873 buf.writeIntBE(-0x120000, 0, 3); 874 assert.deepStrictEqual(buf.toJSON().data, [0xee, 0x00, 0x00]); 875 assert.strictEqual(buf.readIntBE(0, 3), -0x120000); 876 877 buf = Buffer.allocUnsafe(5); 878 buf.writeUIntLE(0x1234567890, 0, 5); 879 assert.deepStrictEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]); 880 assert.strictEqual(buf.readUIntLE(0, 5), 0x1234567890); 881 882 buf.fill(0xFF); 883 buf.writeUIntBE(0x1234567890, 0, 5); 884 assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]); 885 assert.strictEqual(buf.readUIntBE(0, 5), 0x1234567890); 886 887 buf.fill(0xFF); 888 buf.writeIntLE(0x1234567890, 0, 5); 889 assert.deepStrictEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]); 890 assert.strictEqual(buf.readIntLE(0, 5), 0x1234567890); 891 892 buf.fill(0xFF); 893 buf.writeIntBE(0x1234567890, 0, 5); 894 assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]); 895 assert.strictEqual(buf.readIntBE(0, 5), 0x1234567890); 896 897 buf.fill(0xFF); 898 buf.writeIntLE(-0x1234567890, 0, 5); 899 assert.deepStrictEqual(buf.toJSON().data, [0x70, 0x87, 0xa9, 0xcb, 0xed]); 900 assert.strictEqual(buf.readIntLE(0, 5), -0x1234567890); 901 902 buf.fill(0xFF); 903 buf.writeIntBE(-0x1234567890, 0, 5); 904 assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcb, 0xa9, 0x87, 0x70]); 905 assert.strictEqual(buf.readIntBE(0, 5), -0x1234567890); 906 907 buf.fill(0xFF); 908 buf.writeIntLE(-0x0012000000, 0, 5); 909 assert.deepStrictEqual(buf.toJSON().data, [0x00, 0x00, 0x00, 0xee, 0xff]); 910 assert.strictEqual(buf.readIntLE(0, 5), -0x0012000000); 911 912 buf.fill(0xFF); 913 buf.writeIntBE(-0x0012000000, 0, 5); 914 assert.deepStrictEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]); 915 assert.strictEqual(buf.readIntBE(0, 5), -0x0012000000); 916} 917 918// Regression test for https://github.com/nodejs/node-v0.x-archive/issues/5482: 919// should throw but not assert in C++ land. 920assert.throws( 921 () => Buffer.from('', 'buffer'), 922 { 923 code: 'ERR_UNKNOWN_ENCODING', 924 name: 'TypeError', 925 message: 'Unknown encoding: buffer' 926 } 927); 928 929// Regression test for https://github.com/nodejs/node-v0.x-archive/issues/6111. 930// Constructing a buffer from another buffer should a) work, and b) not corrupt 931// the source buffer. 932{ 933 const a = [...Array(128).keys()]; // [0, 1, 2, 3, ... 126, 127] 934 const b = Buffer.from(a); 935 const c = Buffer.from(b); 936 assert.strictEqual(b.length, a.length); 937 assert.strictEqual(c.length, a.length); 938 for (let i = 0, k = a.length; i < k; ++i) { 939 assert.strictEqual(a[i], i); 940 assert.strictEqual(b[i], i); 941 assert.strictEqual(c[i], i); 942 } 943} 944 945if (common.hasCrypto) { // eslint-disable-line node-core/crypto-check 946 // Test truncation after decode 947 const crypto = require('crypto'); 948 949 const b1 = Buffer.from('YW55=======', 'base64'); 950 const b2 = Buffer.from('YW55', 'base64'); 951 952 assert.strictEqual( 953 crypto.createHash('sha1').update(b1).digest('hex'), 954 crypto.createHash('sha1').update(b2).digest('hex') 955 ); 956} else { 957 common.printSkipMessage('missing crypto'); 958} 959 960const ps = Buffer.poolSize; 961Buffer.poolSize = 0; 962assert(Buffer.allocUnsafe(1).parent instanceof ArrayBuffer); 963Buffer.poolSize = ps; 964 965assert.throws( 966 () => Buffer.allocUnsafe(10).copy(), 967 { 968 code: 'ERR_INVALID_ARG_TYPE', 969 name: 'TypeError', 970 message: 'The "target" argument must be an instance of Buffer or ' + 971 'Uint8Array. Received undefined' 972 }); 973 974assert.throws(() => Buffer.from(), { 975 name: 'TypeError', 976 message: 'The first argument must be of type string or an instance of ' + 977 'Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined' 978}); 979assert.throws(() => Buffer.from(null), { 980 name: 'TypeError', 981 message: 'The first argument must be of type string or an instance of ' + 982 'Buffer, ArrayBuffer, or Array or an Array-like Object. Received null' 983}); 984 985// Test prototype getters don't throw 986assert.strictEqual(Buffer.prototype.parent, undefined); 987assert.strictEqual(Buffer.prototype.offset, undefined); 988assert.strictEqual(SlowBuffer.prototype.parent, undefined); 989assert.strictEqual(SlowBuffer.prototype.offset, undefined); 990 991 992{ 993 // Test that large negative Buffer length inputs don't affect the pool offset. 994 // Use the fromArrayLike() variant here because it's more lenient 995 // about its input and passes the length directly to allocate(). 996 assert.deepStrictEqual(Buffer.from({ length: -Buffer.poolSize }), 997 Buffer.from('')); 998 assert.deepStrictEqual(Buffer.from({ length: -100 }), 999 Buffer.from('')); 1000 1001 // Check pool offset after that by trying to write string into the pool. 1002 Buffer.from('abc'); 1003} 1004 1005 1006// Test that ParseArrayIndex handles full uint32 1007{ 1008 const errMsg = common.expectsError({ 1009 code: 'ERR_BUFFER_OUT_OF_BOUNDS', 1010 name: 'RangeError', 1011 message: '"offset" is outside of buffer bounds' 1012 }); 1013 assert.throws(() => Buffer.from(new ArrayBuffer(0), -1 >>> 0), errMsg); 1014} 1015 1016// ParseArrayIndex() should reject values that don't fit in a 32 bits size_t. 1017assert.throws(() => { 1018 const a = Buffer.alloc(1); 1019 const b = Buffer.alloc(1); 1020 a.copy(b, 0, 0x100000000, 0x100000001); 1021}, outOfRangeError); 1022 1023// Unpooled buffer (replaces SlowBuffer) 1024{ 1025 const ubuf = Buffer.allocUnsafeSlow(10); 1026 assert(ubuf); 1027 assert(ubuf.buffer); 1028 assert.strictEqual(ubuf.buffer.byteLength, 10); 1029} 1030 1031// Regression test to verify that an empty ArrayBuffer does not throw. 1032Buffer.from(new ArrayBuffer()); 1033 1034// Test that ArrayBuffer from a different context is detected correctly. 1035const arrayBuf = vm.runInNewContext('new ArrayBuffer()'); 1036Buffer.from(arrayBuf); 1037Buffer.from({ buffer: arrayBuf }); 1038 1039assert.throws(() => Buffer.alloc({ valueOf: () => 1 }), 1040 /"size" argument must be of type number/); 1041assert.throws(() => Buffer.alloc({ valueOf: () => -1 }), 1042 /"size" argument must be of type number/); 1043 1044assert.strictEqual(Buffer.prototype.toLocaleString, Buffer.prototype.toString); 1045{ 1046 const buf = Buffer.from('test'); 1047 assert.strictEqual(buf.toLocaleString(), buf.toString()); 1048} 1049 1050assert.throws(() => { 1051 Buffer.alloc(0x1000, 'This is not correctly encoded', 'hex'); 1052}, { 1053 code: 'ERR_INVALID_ARG_VALUE', 1054 name: 'TypeError' 1055}); 1056 1057assert.throws(() => { 1058 Buffer.alloc(0x1000, 'c', 'hex'); 1059}, { 1060 code: 'ERR_INVALID_ARG_VALUE', 1061 name: 'TypeError' 1062}); 1063 1064assert.throws(() => { 1065 Buffer.alloc(1, Buffer.alloc(0)); 1066}, { 1067 code: 'ERR_INVALID_ARG_VALUE', 1068 name: 'TypeError' 1069}); 1070 1071assert.throws(() => { 1072 Buffer.alloc(40, 'x', 20); 1073}, { 1074 code: 'ERR_INVALID_ARG_TYPE', 1075 name: 'TypeError' 1076}); 1077