Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More test updates #190

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ Buffer.isEncoding = function isEncoding (encoding) {

Buffer.concat = function concat (list, length) {
if (!Array.isArray(list)) {
throw new TypeError('"list" argument must be an Array of Buffers')
throw new TypeError('The "list" argument must be one of type ' +
'Array, Buffer, or Uint8Array')
}

if (list.length === 0) {
Expand Down Expand Up @@ -438,7 +439,7 @@ Buffer.concat = function concat (list, length) {
)
}
} else if (!Buffer.isBuffer(buf)) {
throw new TypeError('"list" argument must be an Array of Buffers')
throw new TypeError('The "list" argument must be one of type Array, Buffer, or Uint8Array')
} else {
buf.copy(buffer, pos)
}
Expand Down Expand Up @@ -733,30 +734,41 @@ function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
byteOffset = +byteOffset // Coerce to Number.
if (numberIsNaN(byteOffset)) {
// byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
byteOffset = dir ? 0 : (buffer.length - 1)
// In Node.js the default value for lastindexOf is buffer.length -1. Using
// buffer.length instead leads to the same results, but makes the code
// simpler
byteOffset = dir ? 0 : buffer.length
}

// If the offset is greater than the length of the buffer, it will fail,
// except if the given value is an empty one, then the offset is returned
var offsetGreaterLength = false
// Normalize byteOffset: negative offsets start from the end of the buffer
if (byteOffset < 0) byteOffset = buffer.length + byteOffset
if (byteOffset >= buffer.length) {
if (dir) return -1
else byteOffset = buffer.length - 1
if (dir) offsetGreaterLength = true
byteOffset = buffer.length
} else if (byteOffset < 0) {
if (dir) byteOffset = 0
else return -1
else offsetGreaterLength = true
}

// Normalize val
if (typeof val === 'string') {
val = Buffer.from(val, encoding)
} else if (isInstance(val, Uint8Array)) {
val = Buffer.from(val, encoding)
}

// Special case: looking for empty string/buffer always passes
if (Buffer.isBuffer(val) && val.length === 0) {
return byteOffset
} else if (offsetGreaterLength) {
return -1
}

// Finally, search either indexOf (if dir is true) or lastIndexOf
if (Buffer.isBuffer(val)) {
// Special case: looking for empty string/buffer always fails
if (val.length === 0) {
return -1
}
return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
} else if (typeof val === 'number') {
val = val & 0xFF // Search for a byte value [0-255]
Expand All @@ -770,7 +782,8 @@ function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
return arrayIndexOf(buffer, [val], byteOffset, encoding, dir)
}

throw new TypeError('val must be string, number or Buffer')
throw new TypeError('The "value" argument must be one of type ' +
'string, Buffer, or Uint8Array. Received type ' + typeof val)
}

function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
Expand All @@ -782,7 +795,7 @@ function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
encoding = String(encoding).toLowerCase()
if (encoding === 'ucs2' || encoding === 'ucs-2' ||
encoding === 'utf16le' || encoding === 'utf-16le') {
if (arr.length < 2 || val.length < 2) {
if (arr.length < 2 || val.length < 2 || arr.length % 2 !== 0) {
return -1
}
indexSize = 2
Expand Down
18 changes: 9 additions & 9 deletions test/node/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ b.copy(Buffer.alloc(1), 0, 2048, 2048);
assert.strictEqual(writeTest.toString(), 'nodejs');
}

// Offset points to the end of the buffer
// Offset points to the end of the buffer and does not throw.
// (see https://github.com/nodejs/node/issues/8127).
assert.doesNotThrow(() => Buffer.alloc(1).write('', 1, 0));
Buffer.alloc(1).write('', 1, 0);

// ASCII slice test
{
Expand Down Expand Up @@ -925,7 +925,7 @@ common.expectsError(
}
}

if (common.hasCrypto) { // eslint-disable-line crypto-check
if (common.hasCrypto) { // eslint-disable-line node-core/crypto-check
// Test truncation after decode
const crypto = require('crypto');

Expand Down Expand Up @@ -973,7 +973,7 @@ assert.strictEqual(SlowBuffer.prototype.offset, undefined);
Buffer.from(''));

// Check pool offset after that by trying to write string into the pool.
assert.doesNotThrow(() => Buffer.from('abc'));
Buffer.from('abc');
}


Expand Down Expand Up @@ -1002,13 +1002,13 @@ common.expectsError(() => {
assert.strictEqual(ubuf.buffer.byteLength, 10);
}

// Regression test
assert.doesNotThrow(() => Buffer.from(new ArrayBuffer()));
// Regression test to verify that an empty ArrayBuffer does not throw.
Buffer.from(new ArrayBuffer());

// Test that ArrayBuffer from a different context is detected correctly
// Test that ArrayBuffer from a different context is detected correctly.
const arrayBuf = vm.runInNewContext('new ArrayBuffer()');
assert.doesNotThrow(() => Buffer.from(arrayBuf));
assert.doesNotThrow(() => Buffer.from({ buffer: arrayBuf }));
Buffer.from(arrayBuf);
Buffer.from({ buffer: arrayBuf });

assert.throws(() => Buffer.alloc({ valueOf: () => 1 }),
/"size" argument must be of type number/);
Expand Down
3 changes: 3 additions & 0 deletions test/node/test-buffer-arraybuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,6 @@ b.writeDoubleBE(11.11, 0, true);
});
}

// Test an array like entry with the length set to NaN.
assert.deepStrictEqual(Buffer.from({ length: NaN }), Buffer.alloc(0));

2 changes: 1 addition & 1 deletion test/node/test-buffer-ascii.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Copyright Joyent, Inc. and other Node contributors.var Buffer = require('../../').Buffer;
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
Expand All @@ -19,6 +18,7 @@
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var Buffer = require('../../').Buffer;

'use strict';
require('./common');
Expand Down
8 changes: 2 additions & 6 deletions test/node/test-buffer-bad-overload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ var Buffer = require('../../').Buffer;
const common = require('./common');
const assert = require('assert');

assert.doesNotThrow(function() {
Buffer.allocUnsafe(10);
});
Buffer.allocUnsafe(10); // Should not throw.

const err = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
Expand All @@ -17,7 +15,5 @@ assert.throws(function() {
Buffer.from(10, 'hex');
}, err);

assert.doesNotThrow(function() {
Buffer.from('deadbeaf', 'hex');
});
Buffer.from('deadbeaf', 'hex'); // Should not throw.

90 changes: 71 additions & 19 deletions test/node/test-buffer-concat.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
'use strict';
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var Buffer = require('../../').Buffer;

'use strict';
const common = require('./common');
const assert = require('assert');

const zero = [];
const one = [ Buffer.from('asdf') ];
const long = [];
for (let i = 0; i < 10; i++) long.push(Buffer.from('asdf'));

var assert = require('assert');
const flatZero = Buffer.concat(zero);
const flatOne = Buffer.concat(one);
const flatLong = Buffer.concat(long);
const flatLongLen = Buffer.concat(long, 40);

var zero = [];
var one = [ Buffer.from('asdf') ];
var long = [];
for (var i = 0; i < 10; i++) long.push(Buffer.from('asdf'));
assert.strictEqual(flatZero.length, 0);
assert.strictEqual(flatOne.toString(), 'asdf');

var flatZero = Buffer.concat(zero);
var flatOne = Buffer.concat(one);
var flatLong = Buffer.concat(long);
var flatLongLen = Buffer.concat(long, 40);
const check = 'asdf'.repeat(10);

assert(flatZero.length === 0);
assert(flatOne.toString() === 'asdf');
// A special case where concat used to return the first item,
// if the length is one. This check is to make sure that we don't do that.
assert(flatOne !== one[0]);
assert(flatLong.toString() === (new Array(10 + 1).join('asdf')));
assert(flatLongLen.toString() === (new Array(10 + 1).join('asdf')));
assert.notStrictEqual(flatOne, one[0]);
assert.strictEqual(flatLong.toString(), check);
assert.strictEqual(flatLongLen.toString(), check);

assertWrongList();
assertWrongList(null);
Expand All @@ -30,11 +53,40 @@ assertWrongList(['hello', 'world']);
assertWrongList(['hello', Buffer.from('world')]);

function assertWrongList(value) {
assert.throws(function() {
common.expectsError(() => {
Buffer.concat(value);
}, function(err) {
return err instanceof TypeError &&
err.message === '"list" argument must be an Array of Buffers';
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "list" argument must be one of type ' +
'Array, Buffer, or Uint8Array'
});
}

// eslint-disable-next-line node-core/crypto-check
const random10 = common.hasCrypto ?
require('crypto').randomBytes(10) :
Buffer.alloc(10, 1);
const empty = Buffer.alloc(0);

assert.notDeepStrictEqual(random10, empty);
assert.notDeepStrictEqual(random10, Buffer.alloc(10));

assert.deepStrictEqual(Buffer.concat([], 100), empty);
assert.deepStrictEqual(Buffer.concat([random10], 0), empty);
assert.deepStrictEqual(Buffer.concat([random10], 10), random10);
assert.deepStrictEqual(Buffer.concat([random10, random10], 10), random10);
assert.deepStrictEqual(Buffer.concat([empty, random10]), random10);
assert.deepStrictEqual(Buffer.concat([random10, empty, empty]), random10);

// The tail should be zero-filled
assert.deepStrictEqual(Buffer.concat([empty], 100), Buffer.alloc(100));
assert.deepStrictEqual(Buffer.concat([empty], 4096), Buffer.alloc(4096));
assert.deepStrictEqual(
Buffer.concat([random10], 40),
Buffer.concat([random10, Buffer.alloc(30)]));

assert.deepStrictEqual(Buffer.concat([new Uint8Array([0x41, 0x42]),
new Uint8Array([0x43, 0x44])]),
Buffer.from('ABCD'));

Loading