From fb5b04ca3450fea87583399cab4e4a996900b002 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 17 Aug 2016 20:19:46 -0700 Subject: [PATCH] test: sync node.js tests to master (node v6.4.0) --- test/node/test-buffer-alloc.js | 19 ++++++++++++++++++- test/node/test-buffer.js | 17 ++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/test/node/test-buffer-alloc.js b/test/node/test-buffer-alloc.js index a0a12a7..b524f51 100644 --- a/test/node/test-buffer-alloc.js +++ b/test/node/test-buffer-alloc.js @@ -983,7 +983,6 @@ assert.equal(0, Buffer.from('hello').slice(0, 0).length); Buffer.allocUnsafe(3.3).fill().toString(); // throws bad argument error in commit 43cb4ec Buffer.alloc(3.3).fill().toString(); -assert.equal(Buffer.allocUnsafe(-1).length, 0); assert.equal(Buffer.allocUnsafe(NaN).length, 0); assert.equal(Buffer.allocUnsafe(3.3).length, 3); assert.equal(Buffer.from({length: 3.3}).length, 3); @@ -1482,3 +1481,21 @@ assert.doesNotThrow(() => { Buffer.from(new ArrayBuffer()); }); +assert.throws(() => Buffer.alloc(-Buffer.poolSize), + '"size" argument must not be negative'); +assert.throws(() => Buffer.alloc(-100), + '"size" argument must not be negative'); +assert.throws(() => Buffer.allocUnsafe(-Buffer.poolSize), + '"size" argument must not be negative'); +assert.throws(() => Buffer.allocUnsafe(-100), + '"size" argument must not be negative'); +assert.throws(() => Buffer.allocUnsafeSlow(-Buffer.poolSize), + '"size" argument must not be negative'); +assert.throws(() => Buffer.allocUnsafeSlow(-100), + '"size" argument must not be negative'); + +assert.throws(() => Buffer.alloc({ valueOf: () => 1 }), + /"size" argument must be a number/); +assert.throws(() => Buffer.alloc({ valueOf: () => -1 }), + /"size" argument must be a number/); + diff --git a/test/node/test-buffer.js b/test/node/test-buffer.js index b74bda0..2e94de2 100644 --- a/test/node/test-buffer.js +++ b/test/node/test-buffer.js @@ -1007,7 +1007,6 @@ assert.equal(0, Buffer('hello').slice(0, 0).length); // Call .fill() first, stops valgrind warning about uninitialized memory reads. Buffer(3.3).fill().toString(); // throws bad argument error in commit 43cb4ec -assert.equal(Buffer(-1).length, 0); assert.equal(Buffer(NaN).length, 0); assert.equal(Buffer(3.3).length, 3); assert.equal(Buffer({length: 3.3}).length, 3); @@ -1493,10 +1492,10 @@ assert.equal(SlowBuffer.prototype.offset, undefined); { // Test that large negative Buffer length inputs don't affect the pool offset. - assert.deepStrictEqual(Buffer(-Buffer.poolSize), Buffer.from('')); - assert.deepStrictEqual(Buffer(-100), Buffer.from('')); - assert.deepStrictEqual(Buffer.allocUnsafe(-Buffer.poolSize), Buffer.from('')); - assert.deepStrictEqual(Buffer.allocUnsafe(-100), Buffer.from('')); + // Use the fromArrayLike() variant here because it's more lenient + // about its input and passes the length directly to allocate(). + assert.deepStrictEqual(Buffer({ length: -Buffer.poolSize }), Buffer.from('')); + assert.deepStrictEqual(Buffer({ length: -100 }), Buffer.from('')); // Check pool offset after that by trying to write string into the pool. assert.doesNotThrow(() => Buffer.from('abc')); @@ -1525,3 +1524,11 @@ assert.equal(SlowBuffer.prototype.offset, undefined); } } +// Test that large negative Buffer length inputs throw errors. +assert.throws(() => Buffer(-Buffer.poolSize), + '"size" argument must not be negative'); +assert.throws(() => Buffer(-100), + '"size" argument must not be negative'); +assert.throws(() => Buffer(-1), + '"size" argument must not be negative'); + -- 2.34.1