mirror of
https://github.com/nodejs/node.git
synced 2025-08-16 06:08:50 +02:00
test: increase coverage for buffer.js
Add coverage for non-numeric byteOffset and length when using Buffer.from() with an ArrayBuffer. PR-URL: https://github.com/nodejs/node/pull/12476 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
d2d32ea5a2
commit
d78fd265dd
1 changed files with 38 additions and 0 deletions
|
@ -105,3 +105,41 @@ b.writeDoubleBE(11.11, 0, true);
|
|||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
// If byteOffset is not numeric, it defaults to 0.
|
||||
const ab = new ArrayBuffer(10);
|
||||
const expected = Buffer.from(ab, 0);
|
||||
assert.deepStrictEqual(Buffer.from(ab, 'fhqwhgads'), expected);
|
||||
assert.deepStrictEqual(Buffer.from(ab, NaN), expected);
|
||||
assert.deepStrictEqual(Buffer.from(ab, {}), expected);
|
||||
assert.deepStrictEqual(Buffer.from(ab, []), expected);
|
||||
|
||||
// If byteOffset can be converted to a number, it will be.
|
||||
assert.deepStrictEqual(Buffer.from(ab, [1]), Buffer.from(ab, 1));
|
||||
|
||||
// If byteOffset is Infinity, throw.
|
||||
assert.throws(
|
||||
() => { Buffer.from(ab, Infinity); },
|
||||
/^RangeError: 'offset' is out of bounds$/
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
// If length is not numeric, it defaults to 0.
|
||||
const ab = new ArrayBuffer(10);
|
||||
const expected = Buffer.from(ab, 0, 0);
|
||||
assert.deepStrictEqual(Buffer.from(ab, 0, 'fhqwhgads'), expected);
|
||||
assert.deepStrictEqual(Buffer.from(ab, 0, NaN), expected);
|
||||
assert.deepStrictEqual(Buffer.from(ab, 0, {}), expected);
|
||||
assert.deepStrictEqual(Buffer.from(ab, 0, []), expected);
|
||||
|
||||
// If length can be converted to a number, it will be.
|
||||
assert.deepStrictEqual(Buffer.from(ab, 0, [1]), Buffer.from(ab, 0, 1));
|
||||
|
||||
//If length is Infinity, throw.
|
||||
assert.throws(
|
||||
() => { Buffer.from(ab, 0, Infinity); },
|
||||
/^RangeError: 'length' is out of bounds$/
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue