1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24const assert = require('assert'); 25 26const zero = []; 27const one = [ Buffer.from('asdf') ]; 28const long = []; 29for (let i = 0; i < 10; i++) long.push(Buffer.from('asdf')); 30 31const flatZero = Buffer.concat(zero); 32const flatOne = Buffer.concat(one); 33const flatLong = Buffer.concat(long); 34const flatLongLen = Buffer.concat(long, 40); 35 36assert.strictEqual(flatZero.length, 0); 37assert.strictEqual(flatOne.toString(), 'asdf'); 38 39const check = 'asdf'.repeat(10); 40 41// A special case where concat used to return the first item, 42// if the length is one. This check is to make sure that we don't do that. 43assert.notStrictEqual(flatOne, one[0]); 44assert.strictEqual(flatLong.toString(), check); 45assert.strictEqual(flatLongLen.toString(), check); 46 47[undefined, null, Buffer.from('hello')].forEach((value) => { 48 assert.throws(() => { 49 Buffer.concat(value); 50 }, { 51 code: 'ERR_INVALID_ARG_TYPE', 52 message: 'The "list" argument must be an instance of Array.' + 53 `${common.invalidArgTypeHelper(value)}` 54 }); 55}); 56 57[[42], ['hello', Buffer.from('world')]].forEach((value) => { 58 assert.throws(() => { 59 Buffer.concat(value); 60 }, { 61 code: 'ERR_INVALID_ARG_TYPE', 62 message: 'The "list[0]" argument must be an instance of Buffer ' + 63 `or Uint8Array.${common.invalidArgTypeHelper(value[0])}` 64 }); 65}); 66 67assert.throws(() => { 68 Buffer.concat([Buffer.from('hello'), 3]); 69}, { 70 code: 'ERR_INVALID_ARG_TYPE', 71 message: 'The "list[1]" argument must be an instance of Buffer ' + 72 'or Uint8Array. Received type number (3)' 73}); 74 75// eslint-disable-next-line node-core/crypto-check 76const random10 = common.hasCrypto ? 77 require('crypto').randomBytes(10) : 78 Buffer.alloc(10, 1); 79const empty = Buffer.alloc(0); 80 81assert.notDeepStrictEqual(random10, empty); 82assert.notDeepStrictEqual(random10, Buffer.alloc(10)); 83 84assert.deepStrictEqual(Buffer.concat([], 100), empty); 85assert.deepStrictEqual(Buffer.concat([random10], 0), empty); 86assert.deepStrictEqual(Buffer.concat([random10], 10), random10); 87assert.deepStrictEqual(Buffer.concat([random10, random10], 10), random10); 88assert.deepStrictEqual(Buffer.concat([empty, random10]), random10); 89assert.deepStrictEqual(Buffer.concat([random10, empty, empty]), random10); 90 91// The tail should be zero-filled 92assert.deepStrictEqual(Buffer.concat([empty], 100), Buffer.alloc(100)); 93assert.deepStrictEqual(Buffer.concat([empty], 4096), Buffer.alloc(4096)); 94assert.deepStrictEqual( 95 Buffer.concat([random10], 40), 96 Buffer.concat([random10, Buffer.alloc(30)])); 97 98assert.deepStrictEqual(Buffer.concat([new Uint8Array([0x41, 0x42]), 99 new Uint8Array([0x43, 0x44])]), 100 Buffer.from('ABCD')); 101