forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest-buffer-read.js
More file actions
214 lines (176 loc) · 7.78 KB
/
test-buffer-read.js
File metadata and controls
214 lines (176 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
'use strict';
const common = require('../common');
const assert = require('assert');
// testing basic buffer read functions
const buf = Buffer.from([0xa4, 0xfd, 0x48, 0xea, 0xcf, 0xff, 0xd9, 0x01, 0xde]);
function read(buff, funx, args, expected) {
assert.strictEqual(buff[funx](...args), expected);
assert.throws(
() => buff[funx](-1),
common.expectsError({ code: 'ERR_INDEX_OUT_OF_RANGE' })
);
assert.doesNotThrow(
() => assert.strictEqual(buff[funx](...args, true), expected),
'noAssert does not change return value for valid ranges'
);
}
// testing basic functionality of readDoubleBE() and readDOubleLE()
read(buf, 'readDoubleBE', [1], -3.1827727774563287e+295);
read(buf, 'readDoubleLE', [1], -6.966010051009108e+144);
// testing basic functionality of readFLoatBE() and readFloatLE()
read(buf, 'readFloatBE', [1], -1.6691549692541768e+37);
read(buf, 'readFloatLE', [1], -7861303808);
// testing basic functionality of readInt8()
read(buf, 'readInt8', [1], -3);
// testing basic functionality of readInt16BE() and readInt16LE()
read(buf, 'readInt16BE', [1], -696);
read(buf, 'readInt16LE', [1], 0x48fd);
// testing basic functionality of readInt32BE() and readInt32LE()
read(buf, 'readInt32BE', [1], -45552945);
read(buf, 'readInt32LE', [1], -806729475);
// testing basic functionality of readIntBE() and readIntLE()
read(buf, 'readIntBE', [1, 1], -3);
read(buf, 'readIntLE', [2, 1], 0x48);
// testing basic functionality of readUInt8()
read(buf, 'readUInt8', [1], 0xfd);
// testing basic functionality of readUInt16BE() and readUInt16LE()
read(buf, 'readUInt16BE', [2], 0x48ea);
read(buf, 'readUInt16LE', [2], 0xea48);
// testing basic functionality of readUInt32BE() and readUInt32LE()
read(buf, 'readUInt32BE', [1], 0xfd48eacf);
read(buf, 'readUInt32LE', [1], 0xcfea48fd);
// testing basic functionality of readUIntBE() and readUIntLE()
read(buf, 'readUIntBE', [2, 0], 0xfd);
read(buf, 'readUIntLE', [2, 0], 0x48);
// attempt to overflow buffers, similar to previous bug in array buffers
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(0xffffffff),
RangeError);
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(0xffffffff),
RangeError);
// ensure negative values can't get past offset
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError);
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError);
// offset checks
{
const buf = Buffer.allocUnsafe(0);
assert.throws(() => buf.readUInt8(0), RangeError);
assert.throws(() => buf.readInt8(0), RangeError);
}
{
const buf = Buffer.from([0xFF]);
assert.strictEqual(buf.readUInt8(0), 255);
assert.strictEqual(buf.readInt8(0), -1);
}
[16, 32].forEach((bits) => {
const buf = Buffer.allocUnsafe(bits / 8 - 1);
assert.throws(() => buf[`readUInt${bits}BE`](0),
RangeError,
`readUInt${bits}BE()`);
assert.throws(() => buf[`readUInt${bits}LE`](0),
RangeError,
`readUInt${bits}LE()`);
assert.throws(() => buf[`readInt${bits}BE`](0),
RangeError,
`readInt${bits}BE()`);
assert.throws(() => buf[`readInt${bits}LE`](0),
RangeError,
`readInt${bits}LE()`);
});
[16, 32].forEach((bits) => {
const buf = Buffer.from([0xFF, 0xFF, 0xFF, 0xFF]);
assert.strictEqual(buf[`readUInt${bits}BE`](0),
(0xFFFFFFFF >>> (32 - bits)));
assert.strictEqual(buf[`readUInt${bits}LE`](0),
(0xFFFFFFFF >>> (32 - bits)));
assert.strictEqual(buf[`readInt${bits}BE`](0),
(0xFFFFFFFF >> (32 - bits)));
assert.strictEqual(buf[`readInt${bits}LE`](0),
(0xFFFFFFFF >> (32 - bits)));
});
// test for common read(U)IntLE/BE
{
const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
assert.strictEqual(buf.readUIntLE(0, 1), 0x01);
assert.strictEqual(buf.readUIntBE(0, 1), 0x01);
assert.strictEqual(buf.readUIntLE(0, 3), 0x030201);
assert.strictEqual(buf.readUIntBE(0, 3), 0x010203);
assert.strictEqual(buf.readUIntLE(0, 5), 0x0504030201);
assert.strictEqual(buf.readUIntBE(0, 5), 0x0102030405);
assert.strictEqual(buf.readUIntLE(0, 6), 0x060504030201);
assert.strictEqual(buf.readUIntBE(0, 6), 0x010203040506);
assert.strictEqual(buf.readIntLE(0, 1), 0x01);
assert.strictEqual(buf.readIntBE(0, 1), 0x01);
assert.strictEqual(buf.readIntLE(0, 3), 0x030201);
assert.strictEqual(buf.readIntBE(0, 3), 0x010203);
assert.strictEqual(buf.readIntLE(0, 5), 0x0504030201);
assert.strictEqual(buf.readIntBE(0, 5), 0x0102030405);
assert.strictEqual(buf.readIntLE(0, 6), 0x060504030201);
assert.strictEqual(buf.readIntBE(0, 6), 0x010203040506);
}
// https://github.com/nodejs/node/issues/8724 - specific to methods dealing
// with floating point types because they call out to C++ code.
{
// ERR_INDEX_OUT_OF_RANGE is optional, exceptions from the binding layer
// don't have it.
const re = /^RangeError( \[ERR_INDEX_OUT_OF_RANGE\])?: Index out of range$/;
const buf = Buffer.alloc(0);
assert.throws(() => buf.readFloatBE(0), re);
assert.throws(() => buf.readFloatLE(0), re);
assert.throws(() => buf.readDoubleBE(0), re);
assert.throws(() => buf.readDoubleLE(0), re);
for (const noAssert of [true, false]) {
assert.throws(() => buf.readFloatBE(0, noAssert), re);
assert.throws(() => buf.readFloatLE(0, noAssert), re);
assert.throws(() => buf.readDoubleBE(0, noAssert), re);
assert.throws(() => buf.readDoubleLE(0, noAssert), re);
}
}
{
const { readFloatBE, readFloatLE, readDoubleBE, readDoubleLE } =
Buffer.prototype;
const re = /^TypeError: argument should be a Buffer$/;
assert.throws(() => readFloatBE(0, true), re);
assert.throws(() => readFloatLE(0, true), re);
assert.throws(() => readDoubleBE(0, true), re);
assert.throws(() => readDoubleLE(0, true), re);
}
{
const { readFloatBE, readFloatLE, readDoubleBE, readDoubleLE } =
Buffer.prototype;
const re = /^TypeError: Cannot read property 'length' of undefined$/;
assert.throws(() => readFloatBE(0), re);
assert.throws(() => readFloatLE(0), re);
assert.throws(() => readDoubleBE(0), re);
assert.throws(() => readDoubleLE(0), re);
assert.throws(() => readFloatBE(0, false), re);
assert.throws(() => readFloatLE(0, false), re);
assert.throws(() => readDoubleBE(0, false), re);
assert.throws(() => readDoubleLE(0, false), re);
}
{
const re =
new RegExp('^TypeError: Method get TypedArray\\.prototype\\.length ' +
'called on incompatible receiver \\[object Object\\]$');
assert.throws(() => Buffer.prototype.readFloatBE(0), re);
assert.throws(() => Buffer.prototype.readFloatLE(0), re);
assert.throws(() => Buffer.prototype.readDoubleBE(0), re);
assert.throws(() => Buffer.prototype.readDoubleLE(0), re);
assert.throws(() => Buffer.prototype.readFloatBE(0, false), re);
assert.throws(() => Buffer.prototype.readFloatLE(0, false), re);
assert.throws(() => Buffer.prototype.readDoubleBE(0, false), re);
assert.throws(() => Buffer.prototype.readDoubleLE(0, false), re);
}
for (const noAssert of [true, false]) {
const re = /^TypeError: argument should be a Buffer$/;
// Wrong receiver.
assert.throws(() => Buffer.prototype.readFloatBE.call({}, 0, noAssert), re);
assert.throws(() => Buffer.prototype.readFloatLE.call({}, 0, noAssert), re);
assert.throws(() => Buffer.prototype.readDoubleBE.call({}, 0, noAssert), re);
assert.throws(() => Buffer.prototype.readDoubleLE.call({}, 0, noAssert), re);
if (noAssert === false) continue; // noAssert=false is tested above.
// Non-method call.
assert.throws(() => Buffer.prototype.readFloatBE(0, noAssert), re);
assert.throws(() => Buffer.prototype.readFloatLE(0, noAssert), re);
assert.throws(() => Buffer.prototype.readDoubleBE(0, noAssert), re);
assert.throws(() => Buffer.prototype.readDoubleLE(0, noAssert), re);
}