forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: nodejs#8256 Reviewed-By: Michaël Zasso <[email protected]>
- Loading branch information
Showing
12 changed files
with
926 additions
and
2,443 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
Oops, something went wrong.