Skip to content

Commit add7625

Browse files
larissayvetteitaloacasas
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 83fe819 commit add7625

File tree

3 files changed

+142
-137
lines changed

3 files changed

+142
-137
lines changed

test/parallel/test-buffer-alloc.js

-78
Original file line numberDiff line numberDiff line change
@@ -776,94 +776,16 @@ assert.throws(() => Buffer.alloc(8).writeFloatLE(0, 5), RangeError);
776776
assert.throws(() => Buffer.alloc(16).writeDoubleLE(0, 9), RangeError);
777777

778778
// attempt to overflow buffers, similar to previous bug in array buffers
779-
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(0xffffffff),
780-
RangeError);
781779
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff),
782780
RangeError);
783-
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(0xffffffff),
784-
RangeError);
785781
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff),
786782
RangeError);
787783

788784

789785
// ensure negative values can't get past offset
790-
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError);
791786
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), RangeError);
792-
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError);
793787
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), RangeError);
794788

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

868790
// test for common write(U)IntLE/BE
869791
{

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

-59
This file was deleted.

test/parallel/test-buffer-read.js

+142
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)