From a75fbe024acee3d03b48b945cf450291c322ff6a Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 17 Apr 2017 16:25:45 -0700 Subject: [PATCH] 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 Reviewed-By: James M Snell --- test/parallel/test-buffer-arraybuffer.js | 38 ++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/parallel/test-buffer-arraybuffer.js b/test/parallel/test-buffer-arraybuffer.js index 4353af17ef..7ef25fec00 100644 --- a/test/parallel/test-buffer-arraybuffer.js +++ b/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$/ + ); +}