Browse Source

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>
v7.x
Rich Trott 8 years ago
committed by Evan Lucas
parent
commit
a75fbe024a
  1. 38
      test/parallel/test-buffer-arraybuffer.js

38
test/parallel/test-buffer-arraybuffer.js

@ -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…
Cancel
Save