Browse Source

test: clean up / refactor buffer tests, remove duplication

Remove duplication of buffer tests, separate out into separate
files, update and cleanup code, move to using strictEqual where
possible.

PR-URL: https://github.com/nodejs/node/pull/8256
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
v7.x
James M Snell 8 years ago
parent
commit
7053922c1a
  1. 1375
      test/parallel/test-buffer-alloc.js
  2. 30
      test/parallel/test-buffer-compare.js
  3. 120
      test/parallel/test-buffer-copy.js
  4. 16
      test/parallel/test-buffer-equals.js
  5. 33
      test/parallel/test-buffer-failed-alloc-typed-arrays.js
  6. 47
      test/parallel/test-buffer-fill.js
  7. 33
      test/parallel/test-buffer-isencoding.js
  8. 24
      test/parallel/test-buffer-no-negative-allocation.js
  9. 13
      test/parallel/test-buffer-regression-649.js
  10. 63
      test/parallel/test-buffer-slice.js
  11. 84
      test/parallel/test-buffer-tostring-range.js
  12. 1531
      test/parallel/test-buffer.js

1375
test/parallel/test-buffer-alloc.js

File diff suppressed because it is too large

30
test/parallel/test-buffer-compare.js

@ -0,0 +1,30 @@
'use strict';
require('../common');
const assert = require('assert');
const b = Buffer.alloc(1, 'a');
const c = Buffer.alloc(1, 'c');
const d = Buffer.alloc(2, 'aa');
assert.strictEqual(b.compare(c), -1);
assert.strictEqual(c.compare(d), 1);
assert.strictEqual(d.compare(b), 1);
assert.strictEqual(b.compare(d), -1);
assert.strictEqual(b.compare(b), 0);
assert.strictEqual(Buffer.compare(b, c), -1);
assert.strictEqual(Buffer.compare(c, d), 1);
assert.strictEqual(Buffer.compare(d, b), 1);
assert.strictEqual(Buffer.compare(b, d), -1);
assert.strictEqual(Buffer.compare(c, c), 0);
assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(0)), 0);
assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(1)), -1);
assert.strictEqual(Buffer.compare(Buffer.alloc(1), Buffer.alloc(0)), 1);
assert.throws(() => Buffer.compare(Buffer.alloc(1), 'abc'));
assert.throws(() => Buffer.compare('abc', Buffer.alloc(1)));
assert.throws(() => Buffer.alloc(1).compare('abc'));

120
test/parallel/test-buffer-copy.js

@ -0,0 +1,120 @@
'use strict';
require('../common');
const assert = require('assert');
var b = Buffer.allocUnsafe(1024);
var c = Buffer.allocUnsafe(512);
var cntr = 0;
{
// copy 512 bytes, from 0 to 512.
b.fill(++cntr);
c.fill(++cntr);
const copied = b.copy(c, 0, 0, 512);
assert.strictEqual(512, copied);
for (let i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
}
}
{
// copy c into b, without specifying sourceEnd
b.fill(++cntr);
c.fill(++cntr);
const copied = c.copy(b, 0, 0);
assert.strictEqual(c.length, copied);
for (let i = 0; i < c.length; i++) {
assert.strictEqual(c[i], b[i]);
}
}
{
// copy c into b, without specifying sourceStart
b.fill(++cntr);
c.fill(++cntr);
const copied = c.copy(b, 0);
assert.strictEqual(c.length, copied);
for (let i = 0; i < c.length; i++) {
assert.strictEqual(c[i], b[i]);
}
}
{
// copy longer buffer b to shorter c without targetStart
b.fill(++cntr);
c.fill(++cntr);
const copied = b.copy(c);
assert.strictEqual(c.length, copied);
for (let i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
}
}
{
// copy starting near end of b to c
b.fill(++cntr);
c.fill(++cntr);
const copied = b.copy(c, 0, b.length - Math.floor(c.length / 2));
assert.strictEqual(Math.floor(c.length / 2), copied);
for (let i = 0; i < Math.floor(c.length / 2); i++) {
assert.strictEqual(b[b.length - Math.floor(c.length / 2) + i], c[i]);
}
for (let i = Math.floor(c.length / 2) + 1; i < c.length; i++) {
assert.strictEqual(c[c.length - 1], c[i]);
}
}
{
// try to copy 513 bytes, and check we don't overrun c
b.fill(++cntr);
c.fill(++cntr);
const copied = b.copy(c, 0, 0, 513);
assert.strictEqual(c.length, copied);
for (let i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
}
}
{
// copy 768 bytes from b into b
b.fill(++cntr);
b.fill(++cntr, 256);
const copied = b.copy(b, 0, 256, 1024);
assert.strictEqual(768, copied);
for (let i = 0; i < b.length; i++) {
assert.strictEqual(cntr, b[i]);
}
}
// copy string longer than buffer length (failure will segfault)
var bb = Buffer.allocUnsafe(10);
bb.fill('hello crazy world');
// try to copy from before the beginning of b
assert.doesNotThrow(() => { b.copy(c, 0, 100, 10); });
// copy throws at negative sourceStart
assert.throws(function() {
Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), 0, -1);
}, RangeError);
{
// check sourceEnd resets to targetEnd if former is greater than the latter
b.fill(++cntr);
c.fill(++cntr);
b.copy(c, 0, 0, 1025);
for (let i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
}
}
// throw with negative sourceEnd
assert.throws(() => b.copy(c, 0, 0, -1), RangeError);
// when sourceStart is greater than sourceEnd, zero copied
assert.strictEqual(b.copy(c, 0, 100, 10), 0);
// when targetStart > targetLength, zero copied
assert.strictEqual(b.copy(c, 512, 0, 10), 0);

16
test/parallel/test-buffer-equals.js

@ -0,0 +1,16 @@
'use strict';
require('../common');
const assert = require('assert');
const b = Buffer.from('abcdf');
const c = Buffer.from('abcdf');
const d = Buffer.from('abcde');
const e = Buffer.from('abcdef');
assert.ok(b.equals(c));
assert.ok(!c.equals(d));
assert.ok(!d.equals(e));
assert.ok(d.equals(d));
assert.throws(() => Buffer.alloc(1).equals('abc'));

33
test/parallel/test-buffer-failed-alloc-typed-arrays.js

@ -0,0 +1,33 @@
'use strict';
require('../common');
const assert = require('assert');
const SlowBuffer = require('buffer').SlowBuffer;
// Test failed or zero-sized Buffer allocations not affecting typed arrays.
// This test exists because of a regression that occurred. Because Buffer
// instances are allocated with the same underlying allocator as TypedArrays,
// but Buffer's can optional be non-zero filled, there was a regression that
// occurred when a Buffer allocated failed, the internal flag specifying
// whether or not to zero-fill was not being reset, causing TypedArrays to
// allocate incorrectly.
const zeroArray = new Uint32Array(10).fill(0);
const sizes = [1e10, 0, 0.1, -1, 'a', undefined, null, NaN];
const allocators = [
Buffer,
SlowBuffer,
Buffer.alloc,
Buffer.allocUnsafe,
Buffer.allocUnsafeSlow
];
for (const allocator of allocators) {
for (const size of sizes) {
try {
// These allocations are known to fail. If they do,
// Uint32Array should still produce a zeroed out result.
allocator(size);
} catch (e) {
assert.deepStrictEqual(new Uint32Array(10), zeroArray);
}
}
}

47
test/parallel/test-buffer-fill.js

@ -267,3 +267,50 @@ function testBufs(string, offset, length, encoding) {
assert.deepStrictEqual(buf1.fill.apply(buf1, arguments), assert.deepStrictEqual(buf1.fill.apply(buf1, arguments),
writeToFill.apply(null, arguments)); writeToFill.apply(null, arguments));
} }
// Make sure these throw.
assert.throws(() => Buffer.allocUnsafe(8).fill('a', -1));
assert.throws(() => Buffer.allocUnsafe(8).fill('a', 0, 9));
// Make sure this doesn't hang indefinitely.
Buffer.allocUnsafe(8).fill('');
Buffer.alloc(8, '');
{
const buf = Buffer.alloc(64, 10);
for (let i = 0; i < buf.length; i++)
assert.strictEqual(buf[i], 10);
buf.fill(11, 0, buf.length >> 1);
for (let i = 0; i < buf.length >> 1; i++)
assert.strictEqual(buf[i], 11);
for (let i = (buf.length >> 1) + 1; i < buf.length; i++)
assert.strictEqual(buf[i], 10);
buf.fill('h');
for (let i = 0; i < buf.length; i++)
assert.strictEqual('h'.charCodeAt(0), buf[i]);
buf.fill(0);
for (let i = 0; i < buf.length; i++)
assert.strictEqual(0, buf[i]);
buf.fill(null);
for (let i = 0; i < buf.length; i++)
assert.strictEqual(0, buf[i]);
buf.fill(1, 16, 32);
for (let i = 0; i < 16; i++)
assert.strictEqual(0, buf[i]);
for (let i = 16; i < 32; i++)
assert.strictEqual(1, buf[i]);
for (let i = 32; i < buf.length; i++)
assert.strictEqual(0, buf[i]);
}
{
const buf = Buffer.alloc(10, 'abc');
assert.strictEqual(buf.toString(), 'abcabcabca');
buf.fill('է');
assert.strictEqual(buf.toString(), 'էէէէէ');
}

33
test/parallel/test-buffer-isencoding.js

@ -0,0 +1,33 @@
'use strict';
require('../common');
const assert = require('assert');
[ 'hex',
'utf8',
'utf-8',
'ascii',
'latin1',
'binary',
'base64',
'ucs2',
'ucs-2',
'utf16le',
'utf-16le' ].forEach((enc) => {
assert.strictEqual(Buffer.isEncoding(enc), true);
});
[ 'utf9',
'utf-7',
'Unicode-FTW',
'new gnu gun',
false,
NaN,
{},
Infinity,
[],
1,
0,
-1 ].forEach((enc) => {
assert.strictEqual(Buffer.isEncoding(enc), false);
});

24
test/parallel/test-buffer-no-negative-allocation.js

@ -0,0 +1,24 @@
'use strict';
require('../common');
const assert = require('assert');
const msg = /"size" argument must not be negative/;
// Test that negative Buffer length inputs throw errors.
assert.throws(() => Buffer(-Buffer.poolSize), msg);
assert.throws(() => Buffer(-100), msg);
assert.throws(() => Buffer(-1), msg);
assert.throws(() => Buffer.alloc(-Buffer.poolSize), msg);
assert.throws(() => Buffer.alloc(-100), msg);
assert.throws(() => Buffer.alloc(-1), msg);
assert.throws(() => Buffer.allocUnsafe(-Buffer.poolSize), msg);
assert.throws(() => Buffer.allocUnsafe(-100), msg);
assert.throws(() => Buffer.allocUnsafe(-1), msg);
assert.throws(() => Buffer.allocUnsafeSlow(-Buffer.poolSize), msg);
assert.throws(() => Buffer.allocUnsafeSlow(-100), msg);
assert.throws(() => Buffer.allocUnsafeSlow(-1), msg);

13
test/parallel/test-buffer-regression-649.js

@ -0,0 +1,13 @@
'use strict';
require('../common');
const assert = require('assert');
const SlowBuffer = require('buffer').SlowBuffer;
// Regression test for https://github.com/nodejs/node/issues/649.
const len = 1422561062959;
assert.throws(() => Buffer(len).toString('utf8'));
assert.throws(() => SlowBuffer(len).toString('utf8'));
assert.throws(() => Buffer.alloc(len).toString('utf8'));
assert.throws(() => Buffer.allocUnsafe(len).toString('utf8'));
assert.throws(() => Buffer.allocUnsafeSlow(len).toString('utf8'));

63
test/parallel/test-buffer-slice.js

@ -0,0 +1,63 @@
'use strict';
require('../common');
const assert = require('assert');
assert.strictEqual(0, Buffer.from('hello').slice(0, 0).length);
assert.strictEqual(0, Buffer('hello').slice(0, 0).length);
const buf = Buffer.from('0123456789');
assert.equal(buf.slice(-10, 10), '0123456789');
assert.equal(buf.slice(-20, 10), '0123456789');
assert.equal(buf.slice(-20, -10), '');
assert.equal(buf.slice(), '0123456789');
assert.equal(buf.slice(0), '0123456789');
assert.equal(buf.slice(0, 0), '');
assert.equal(buf.slice(undefined), '0123456789');
assert.equal(buf.slice('foobar'), '0123456789');
assert.equal(buf.slice(undefined, undefined), '0123456789');
assert.equal(buf.slice(2), '23456789');
assert.equal(buf.slice(5), '56789');
assert.equal(buf.slice(10), '');
assert.equal(buf.slice(5, 8), '567');
assert.equal(buf.slice(8, -1), '8');
assert.equal(buf.slice(-10), '0123456789');
assert.equal(buf.slice(0, -9), '0');
assert.equal(buf.slice(0, -10), '');
assert.equal(buf.slice(0, -1), '012345678');
assert.equal(buf.slice(2, -2), '234567');
assert.equal(buf.slice(0, 65536), '0123456789');
assert.equal(buf.slice(65536, 0), '');
assert.equal(buf.slice(-5, -8), '');
assert.equal(buf.slice(-5, -3), '56');
assert.equal(buf.slice(-10, 10), '0123456789');
for (let i = 0, s = buf.toString(); i < buf.length; ++i) {
assert.equal(buf.slice(i), s.slice(i));
assert.equal(buf.slice(0, i), s.slice(0, i));
assert.equal(buf.slice(-i), s.slice(-i));
assert.equal(buf.slice(0, -i), s.slice(0, -i));
}
const utf16Buf = Buffer.from('0123456789', 'utf16le');
assert.deepStrictEqual(utf16Buf.slice(0, 6), Buffer.from('012', 'utf16le'));
assert.equal(buf.slice('0', '1'), '0');
assert.equal(buf.slice('-5', '10'), '56789');
assert.equal(buf.slice('-10', '10'), '0123456789');
assert.equal(buf.slice('-10', '-5'), '01234');
assert.equal(buf.slice('-10', '-0'), '');
assert.equal(buf.slice('111'), '');
assert.equal(buf.slice('0', '-111'), '');
// try to slice a zero length Buffer
// see https://github.com/joyent/node/issues/5881
Buffer.alloc(0).slice(0, 1);
{
// Single argument slice
assert.strictEqual('bcde', Buffer.from('abcde').slice(1).toString());
}
// slice(0,0).length === 0
assert.strictEqual(0, Buffer.from('hello').slice(0, 0).length);

84
test/parallel/test-buffer-tostring-range.js

@ -0,0 +1,84 @@
'use strict';
require('../common');
const assert = require('assert');
const rangeBuffer = Buffer.from('abc');
// if start >= buffer's length, empty string will be returned
assert.strictEqual(rangeBuffer.toString('ascii', 3), '');
assert.strictEqual(rangeBuffer.toString('ascii', +Infinity), '');
assert.strictEqual(rangeBuffer.toString('ascii', 3.14, 3), '');
assert.strictEqual(rangeBuffer.toString('ascii', 'Infinity', 3), '');
// if end <= 0, empty string will be returned
assert.strictEqual(rangeBuffer.toString('ascii', 1, 0), '');
assert.strictEqual(rangeBuffer.toString('ascii', 1, -1.2), '');
assert.strictEqual(rangeBuffer.toString('ascii', 1, -100), '');
assert.strictEqual(rangeBuffer.toString('ascii', 1, -Infinity), '');
// if start < 0, start will be taken as zero
assert.strictEqual(rangeBuffer.toString('ascii', -1, 3), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', -1.99, 3), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', -Infinity, 3), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', '-1', 3), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', '-1.99', 3), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', '-Infinity', 3), 'abc');
// if start is an invalid integer, start will be taken as zero
assert.strictEqual(rangeBuffer.toString('ascii', 'node.js', 3), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', {}, 3), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', [], 3), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', NaN, 3), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', null, 3), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', undefined, 3), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', false, 3), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', '', 3), 'abc');
// but, if start is an integer when coerced, then it will be coerced and used.
assert.strictEqual(rangeBuffer.toString('ascii', '-1', 3), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', '1', 3), 'bc');
assert.strictEqual(rangeBuffer.toString('ascii', '-Infinity', 3), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', '3', 3), '');
assert.strictEqual(rangeBuffer.toString('ascii', Number(3), 3), '');
assert.strictEqual(rangeBuffer.toString('ascii', '3.14', 3), '');
assert.strictEqual(rangeBuffer.toString('ascii', '1.99', 3), 'bc');
assert.strictEqual(rangeBuffer.toString('ascii', '-1.99', 3), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', 1.99, 3), 'bc');
assert.strictEqual(rangeBuffer.toString('ascii', true, 3), 'bc');
// if end > buffer's length, end will be taken as buffer's length
assert.strictEqual(rangeBuffer.toString('ascii', 0, 5), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', 0, 6.99), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', 0, Infinity), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', 0, '5'), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', 0, '6.99'), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', 0, 'Infinity'), 'abc');
// if end is an invalid integer, end will be taken as buffer's length
assert.strictEqual(rangeBuffer.toString('ascii', 0, 'node.js'), '');
assert.strictEqual(rangeBuffer.toString('ascii', 0, {}), '');
assert.strictEqual(rangeBuffer.toString('ascii', 0, NaN), '');
assert.strictEqual(rangeBuffer.toString('ascii', 0, undefined), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', 0), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', 0, null), '');
assert.strictEqual(rangeBuffer.toString('ascii', 0, []), '');
assert.strictEqual(rangeBuffer.toString('ascii', 0, false), '');
assert.strictEqual(rangeBuffer.toString('ascii', 0, ''), '');
// but, if end is an integer when coerced, then it will be coerced and used.
assert.strictEqual(rangeBuffer.toString('ascii', 0, '-1'), '');
assert.strictEqual(rangeBuffer.toString('ascii', 0, '1'), 'a');
assert.strictEqual(rangeBuffer.toString('ascii', 0, '-Infinity'), '');
assert.strictEqual(rangeBuffer.toString('ascii', 0, '3'), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', 0, Number(3)), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', 0, '3.14'), 'abc');
assert.strictEqual(rangeBuffer.toString('ascii', 0, '1.99'), 'a');
assert.strictEqual(rangeBuffer.toString('ascii', 0, '-1.99'), '');
assert.strictEqual(rangeBuffer.toString('ascii', 0, 1.99), 'a');
assert.strictEqual(rangeBuffer.toString('ascii', 0, true), 'a');
// try toString() with a object as a encoding
assert.strictEqual(rangeBuffer.toString({toString: function() {
return 'ascii';
}}), 'abc');

1531
test/parallel/test-buffer.js

File diff suppressed because it is too large
Loading…
Cancel
Save