Skip to content

Commit a755ef0

Browse files
TrottMylesBorins
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 <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent e1098a4 commit a755ef0

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Diff for: test/parallel/test-buffer-arraybuffer.js

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

0 commit comments

Comments
 (0)