Skip to content

Commit a75fbe0

Browse files
Trottevanlucas
authored andcommitted
test: increase coverage for buffer.js
Add coverage for non-numeric byteOffset and length when using Buffer.from() with an ArrayBuffer. PR-URL: #12476 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 38278db commit a75fbe0

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

test/parallel/test-buffer-arraybuffer.js

+38
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,41 @@ b.writeDoubleBE(11.11, 0, true);
105105
return true;
106106
});
107107
}
108+
109+
{
110+
// If byteOffset is not numeric, it defaults to 0.
111+
const ab = new ArrayBuffer(10);
112+
const expected = Buffer.from(ab, 0);
113+
assert.deepStrictEqual(Buffer.from(ab, 'fhqwhgads'), expected);
114+
assert.deepStrictEqual(Buffer.from(ab, NaN), expected);
115+
assert.deepStrictEqual(Buffer.from(ab, {}), expected);
116+
assert.deepStrictEqual(Buffer.from(ab, []), expected);
117+
118+
// If byteOffset can be converted to a number, it will be.
119+
assert.deepStrictEqual(Buffer.from(ab, [1]), Buffer.from(ab, 1));
120+
121+
// If byteOffset is Infinity, throw.
122+
assert.throws(
123+
() => { Buffer.from(ab, Infinity); },
124+
/^RangeError: 'offset' is out of bounds$/
125+
);
126+
}
127+
128+
{
129+
// If length is not numeric, it defaults to 0.
130+
const ab = new ArrayBuffer(10);
131+
const expected = Buffer.from(ab, 0, 0);
132+
assert.deepStrictEqual(Buffer.from(ab, 0, 'fhqwhgads'), expected);
133+
assert.deepStrictEqual(Buffer.from(ab, 0, NaN), expected);
134+
assert.deepStrictEqual(Buffer.from(ab, 0, {}), expected);
135+
assert.deepStrictEqual(Buffer.from(ab, 0, []), expected);
136+
137+
// If length can be converted to a number, it will be.
138+
assert.deepStrictEqual(Buffer.from(ab, 0, [1]), Buffer.from(ab, 0, 1));
139+
140+
//If length is Infinity, throw.
141+
assert.throws(
142+
() => { Buffer.from(ab, 0, Infinity); },
143+
/^RangeError: 'length' is out of bounds$/
144+
);
145+
}

0 commit comments

Comments
 (0)