Skip to content

Commit 8a25e31

Browse files
larissayvettejasnell
authored andcommitted
test: consolidate buffer.read() in a file
PR-URL: #11297 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent ca5215e commit 8a25e31

File tree

3 files changed

+142
-137
lines changed

3 files changed

+142
-137
lines changed

test/parallel/test-buffer-alloc.js

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

776776
// attempt to overflow buffers, similar to previous bug in array buffers
777-
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(0xffffffff),
778-
RangeError);
779777
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff),
780778
RangeError);
781-
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(0xffffffff),
782-
RangeError);
783779
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff),
784780
RangeError);
785781

786782

787783
// ensure negative values can't get past offset
788-
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError);
789784
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), RangeError);
790-
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError);
791785
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), RangeError);
792786

793-
// offset checks
794-
{
795-
const buf = Buffer.allocUnsafe(0);
796-
797-
assert.throws(() => buf.readUInt8(0), RangeError);
798-
assert.throws(() => buf.readInt8(0), RangeError);
799-
}
800-
801-
{
802-
const buf = Buffer.from([0xFF]);
803-
804-
assert.strictEqual(buf.readUInt8(0), 255);
805-
assert.strictEqual(buf.readInt8(0), -1);
806-
}
807-
808-
[16, 32].forEach((bits) => {
809-
const buf = Buffer.allocUnsafe(bits / 8 - 1);
810-
811-
assert.throws(() => buf[`readUInt${bits}BE`](0),
812-
RangeError,
813-
`readUInt${bits}BE()`);
814-
815-
assert.throws(() => buf[`readUInt${bits}LE`](0),
816-
RangeError,
817-
`readUInt${bits}LE()`);
818-
819-
assert.throws(() => buf[`readInt${bits}BE`](0),
820-
RangeError,
821-
`readInt${bits}BE()`);
822-
823-
assert.throws(() => buf[`readInt${bits}LE`](0),
824-
RangeError,
825-
`readInt${bits}LE()`);
826-
});
827-
828-
[16, 32].forEach((bits) => {
829-
const buf = Buffer.from([0xFF, 0xFF, 0xFF, 0xFF]);
830-
831-
assert.strictEqual(buf[`readUInt${bits}BE`](0),
832-
(0xFFFFFFFF >>> (32 - bits)));
833-
834-
assert.strictEqual(buf[`readUInt${bits}LE`](0),
835-
(0xFFFFFFFF >>> (32 - bits)));
836-
837-
assert.strictEqual(buf[`readInt${bits}BE`](0),
838-
(0xFFFFFFFF >> (32 - bits)));
839-
840-
assert.strictEqual(buf[`readInt${bits}LE`](0),
841-
(0xFFFFFFFF >> (32 - bits)));
842-
});
843-
844-
// test for common read(U)IntLE/BE
845-
{
846-
const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
847-
848-
assert.strictEqual(buf.readUIntLE(0, 1), 0x01);
849-
assert.strictEqual(buf.readUIntBE(0, 1), 0x01);
850-
assert.strictEqual(buf.readUIntLE(0, 3), 0x030201);
851-
assert.strictEqual(buf.readUIntBE(0, 3), 0x010203);
852-
assert.strictEqual(buf.readUIntLE(0, 5), 0x0504030201);
853-
assert.strictEqual(buf.readUIntBE(0, 5), 0x0102030405);
854-
assert.strictEqual(buf.readUIntLE(0, 6), 0x060504030201);
855-
assert.strictEqual(buf.readUIntBE(0, 6), 0x010203040506);
856-
assert.strictEqual(buf.readIntLE(0, 1), 0x01);
857-
assert.strictEqual(buf.readIntBE(0, 1), 0x01);
858-
assert.strictEqual(buf.readIntLE(0, 3), 0x030201);
859-
assert.strictEqual(buf.readIntBE(0, 3), 0x010203);
860-
assert.strictEqual(buf.readIntLE(0, 5), 0x0504030201);
861-
assert.strictEqual(buf.readIntBE(0, 5), 0x0102030405);
862-
assert.strictEqual(buf.readIntLE(0, 6), 0x060504030201);
863-
assert.strictEqual(buf.readIntBE(0, 6), 0x010203040506);
864-
}
865787

866788
// test for common write(U)IntLE/BE
867789
{

test/parallel/test-buffer-read-noassert.js

Lines changed: 0 additions & 59 deletions
This file was deleted.

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)