Skip to content

Commit 5a92fc2

Browse files
larissayvetteMylesBorins
authored andcommitted
test: consolidate buffer.read() in a file
PR-URL: #11297 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 607158a commit 5a92fc2

File tree

2 files changed

+142
-78
lines changed

2 files changed

+142
-78
lines changed

test/parallel/test-buffer-alloc.js

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -830,94 +830,16 @@ assert.throws(() => Buffer.alloc(8).writeFloatLE(0, 5), RangeError);
830830
assert.throws(() => Buffer.alloc(16).writeDoubleLE(0, 9), RangeError);
831831

832832
// attempt to overflow buffers, similar to previous bug in array buffers
833-
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(0xffffffff),
834-
RangeError);
835833
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff),
836834
RangeError);
837-
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(0xffffffff),
838-
RangeError);
839835
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff),
840836
RangeError);
841837

842838

843839
// ensure negative values can't get past offset
844-
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError);
845840
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), RangeError);
846-
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError);
847841
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), RangeError);
848842

849-
// offset checks
850-
{
851-
const buf = Buffer.allocUnsafe(0);
852-
853-
assert.throws(() => buf.readUInt8(0), RangeError);
854-
assert.throws(() => buf.readInt8(0), RangeError);
855-
}
856-
857-
{
858-
const buf = Buffer.from([0xFF]);
859-
860-
assert.strictEqual(buf.readUInt8(0), 255);
861-
assert.strictEqual(buf.readInt8(0), -1);
862-
}
863-
864-
[16, 32].forEach((bits) => {
865-
const buf = Buffer.allocUnsafe(bits / 8 - 1);
866-
867-
assert.throws(() => buf[`readUInt${bits}BE`](0),
868-
RangeError,
869-
`readUInt${bits}BE()`);
870-
871-
assert.throws(() => buf[`readUInt${bits}LE`](0),
872-
RangeError,
873-
`readUInt${bits}LE()`);
874-
875-
assert.throws(() => buf[`readInt${bits}BE`](0),
876-
RangeError,
877-
`readInt${bits}BE()`);
878-
879-
assert.throws(() => buf[`readInt${bits}LE`](0),
880-
RangeError,
881-
`readInt${bits}LE()`);
882-
});
883-
884-
[16, 32].forEach((bits) => {
885-
const buf = Buffer.from([0xFF, 0xFF, 0xFF, 0xFF]);
886-
887-
assert.strictEqual(buf[`readUInt${bits}BE`](0),
888-
(0xFFFFFFFF >>> (32 - bits)));
889-
890-
assert.strictEqual(buf[`readUInt${bits}LE`](0),
891-
(0xFFFFFFFF >>> (32 - bits)));
892-
893-
assert.strictEqual(buf[`readInt${bits}BE`](0),
894-
(0xFFFFFFFF >> (32 - bits)));
895-
896-
assert.strictEqual(buf[`readInt${bits}LE`](0),
897-
(0xFFFFFFFF >> (32 - bits)));
898-
});
899-
900-
// test for common read(U)IntLE/BE
901-
{
902-
const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
903-
904-
assert.strictEqual(buf.readUIntLE(0, 1), 0x01);
905-
assert.strictEqual(buf.readUIntBE(0, 1), 0x01);
906-
assert.strictEqual(buf.readUIntLE(0, 3), 0x030201);
907-
assert.strictEqual(buf.readUIntBE(0, 3), 0x010203);
908-
assert.strictEqual(buf.readUIntLE(0, 5), 0x0504030201);
909-
assert.strictEqual(buf.readUIntBE(0, 5), 0x0102030405);
910-
assert.strictEqual(buf.readUIntLE(0, 6), 0x060504030201);
911-
assert.strictEqual(buf.readUIntBE(0, 6), 0x010203040506);
912-
assert.strictEqual(buf.readIntLE(0, 1), 0x01);
913-
assert.strictEqual(buf.readIntBE(0, 1), 0x01);
914-
assert.strictEqual(buf.readIntLE(0, 3), 0x030201);
915-
assert.strictEqual(buf.readIntBE(0, 3), 0x010203);
916-
assert.strictEqual(buf.readIntLE(0, 5), 0x0504030201);
917-
assert.strictEqual(buf.readIntBE(0, 5), 0x0102030405);
918-
assert.strictEqual(buf.readIntLE(0, 6), 0x060504030201);
919-
assert.strictEqual(buf.readIntBE(0, 6), 0x010203040506);
920-
}
921843

922844
// test for common write(U)IntLE/BE
923845
{

test/parallel/test-buffer-read.js

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
5+
// testing basic buffer read functions
6+
const buf = Buffer.from([0xa4, 0xfd, 0x48, 0xea, 0xcf, 0xff, 0xd9, 0x01, 0xde]);
7+
8+
function read(buff, funx, args, expected) {
9+
10+
assert.strictEqual(buff[funx](...args), expected);
11+
assert.throws(
12+
() => buff[funx](-1),
13+
/^RangeError: Index out of range$/
14+
);
15+
16+
assert.doesNotThrow(
17+
() => assert.strictEqual(buff[funx](...args, true), expected),
18+
'noAssert does not change return value for valid ranges'
19+
);
20+
21+
}
22+
23+
// testing basic functionality of readDoubleBE() and readDOubleLE()
24+
read(buf, 'readDoubleBE', [1], -3.1827727774563287e+295);
25+
read(buf, 'readDoubleLE', [1], -6.966010051009108e+144);
26+
27+
// testing basic functionality of readFLoatBE() and readFloatLE()
28+
read(buf, 'readFloatBE', [1], -1.6691549692541768e+37);
29+
read(buf, 'readFloatLE', [1], -7861303808);
30+
31+
// testing basic functionality of readInt8()
32+
read(buf, 'readInt8', [1], -3);
33+
34+
// testing basic functionality of readInt16BE() and readInt16LE()
35+
read(buf, 'readInt16BE', [1], -696);
36+
read(buf, 'readInt16LE', [1], 0x48fd);
37+
38+
// testing basic functionality of readInt32BE() and readInt32LE()
39+
read(buf, 'readInt32BE', [1], -45552945);
40+
read(buf, 'readInt32LE', [1], -806729475);
41+
42+
// testing basic functionality of readIntBE() and readIntLE()
43+
read(buf, 'readIntBE', [1, 1], -3);
44+
read(buf, 'readIntLE', [2, 1], 0x48);
45+
46+
// testing basic functionality of readUInt8()
47+
read(buf, 'readUInt8', [1], 0xfd);
48+
49+
// testing basic functionality of readUInt16BE() and readUInt16LE()
50+
read(buf, 'readUInt16BE', [2], 0x48ea);
51+
read(buf, 'readUInt16LE', [2], 0xea48);
52+
53+
// testing basic functionality of readUInt32BE() and readUInt32LE()
54+
read(buf, 'readUInt32BE', [1], 0xfd48eacf);
55+
read(buf, 'readUInt32LE', [1], 0xcfea48fd);
56+
57+
// testing basic functionality of readUIntBE() and readUIntLE()
58+
read(buf, 'readUIntBE', [2, 0], 0xfd);
59+
read(buf, 'readUIntLE', [2, 0], 0x48);
60+
61+
// attempt to overflow buffers, similar to previous bug in array buffers
62+
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(0xffffffff),
63+
RangeError);
64+
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(0xffffffff),
65+
RangeError);
66+
67+
// ensure negative values can't get past offset
68+
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError);
69+
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError);
70+
71+
// offset checks
72+
{
73+
const buf = Buffer.allocUnsafe(0);
74+
75+
assert.throws(() => buf.readUInt8(0), RangeError);
76+
assert.throws(() => buf.readInt8(0), RangeError);
77+
}
78+
79+
{
80+
const buf = Buffer.from([0xFF]);
81+
82+
assert.strictEqual(buf.readUInt8(0), 255);
83+
assert.strictEqual(buf.readInt8(0), -1);
84+
}
85+
86+
[16, 32].forEach((bits) => {
87+
const buf = Buffer.allocUnsafe(bits / 8 - 1);
88+
89+
assert.throws(() => buf[`readUInt${bits}BE`](0),
90+
RangeError,
91+
`readUInt${bits}BE()`);
92+
93+
assert.throws(() => buf[`readUInt${bits}LE`](0),
94+
RangeError,
95+
`readUInt${bits}LE()`);
96+
97+
assert.throws(() => buf[`readInt${bits}BE`](0),
98+
RangeError,
99+
`readInt${bits}BE()`);
100+
101+
assert.throws(() => buf[`readInt${bits}LE`](0),
102+
RangeError,
103+
`readInt${bits}LE()`);
104+
});
105+
106+
[16, 32].forEach((bits) => {
107+
const buf = Buffer.from([0xFF, 0xFF, 0xFF, 0xFF]);
108+
109+
assert.strictEqual(buf[`readUInt${bits}BE`](0),
110+
(0xFFFFFFFF >>> (32 - bits)));
111+
112+
assert.strictEqual(buf[`readUInt${bits}LE`](0),
113+
(0xFFFFFFFF >>> (32 - bits)));
114+
115+
assert.strictEqual(buf[`readInt${bits}BE`](0),
116+
(0xFFFFFFFF >> (32 - bits)));
117+
118+
assert.strictEqual(buf[`readInt${bits}LE`](0),
119+
(0xFFFFFFFF >> (32 - bits)));
120+
});
121+
122+
// test for common read(U)IntLE/BE
123+
{
124+
const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
125+
126+
assert.strictEqual(buf.readUIntLE(0, 1), 0x01);
127+
assert.strictEqual(buf.readUIntBE(0, 1), 0x01);
128+
assert.strictEqual(buf.readUIntLE(0, 3), 0x030201);
129+
assert.strictEqual(buf.readUIntBE(0, 3), 0x010203);
130+
assert.strictEqual(buf.readUIntLE(0, 5), 0x0504030201);
131+
assert.strictEqual(buf.readUIntBE(0, 5), 0x0102030405);
132+
assert.strictEqual(buf.readUIntLE(0, 6), 0x060504030201);
133+
assert.strictEqual(buf.readUIntBE(0, 6), 0x010203040506);
134+
assert.strictEqual(buf.readIntLE(0, 1), 0x01);
135+
assert.strictEqual(buf.readIntBE(0, 1), 0x01);
136+
assert.strictEqual(buf.readIntLE(0, 3), 0x030201);
137+
assert.strictEqual(buf.readIntBE(0, 3), 0x010203);
138+
assert.strictEqual(buf.readIntLE(0, 5), 0x0504030201);
139+
assert.strictEqual(buf.readIntBE(0, 5), 0x0102030405);
140+
assert.strictEqual(buf.readIntLE(0, 6), 0x060504030201);
141+
assert.strictEqual(buf.readIntBE(0, 6), 0x010203040506);
142+
}

0 commit comments

Comments
 (0)