2017-02-10 16:18:38 +00:00
|
|
|
'use strict';
|
2019-12-25 17:02:16 +00:00
|
|
|
require('../common');
|
2017-02-10 16:18:38 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
|
2019-03-07 00:03:53 +00:00
|
|
|
// Testing basic buffer read functions
|
2017-02-10 16:18:38 +00:00
|
|
|
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);
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(
|
2017-02-03 15:05:28 +00:00
|
|
|
() => buff[funx](-1, args[1]),
|
2018-01-30 16:34:25 +00:00
|
|
|
{ code: 'ERR_OUT_OF_RANGE' }
|
2017-02-10 16:18:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-03 16:15:45 +00:00
|
|
|
// Testing basic functionality of readDoubleBE() and readDoubleLE()
|
2017-02-10 16:18:38 +00:00
|
|
|
read(buf, 'readDoubleBE', [1], -3.1827727774563287e+295);
|
|
|
|
read(buf, 'readDoubleLE', [1], -6.966010051009108e+144);
|
|
|
|
|
2018-12-03 16:15:45 +00:00
|
|
|
// Testing basic functionality of readFloatBE() and readFloatLE()
|
2017-02-10 16:18:38 +00:00
|
|
|
read(buf, 'readFloatBE', [1], -1.6691549692541768e+37);
|
|
|
|
read(buf, 'readFloatLE', [1], -7861303808);
|
|
|
|
|
2019-01-21 00:22:27 +00:00
|
|
|
// Testing basic functionality of readInt8()
|
2017-02-10 16:18:38 +00:00
|
|
|
read(buf, 'readInt8', [1], -3);
|
|
|
|
|
2018-12-03 16:15:45 +00:00
|
|
|
// Testing basic functionality of readInt16BE() and readInt16LE()
|
2017-02-10 16:18:38 +00:00
|
|
|
read(buf, 'readInt16BE', [1], -696);
|
|
|
|
read(buf, 'readInt16LE', [1], 0x48fd);
|
|
|
|
|
2018-12-03 16:15:45 +00:00
|
|
|
// Testing basic functionality of readInt32BE() and readInt32LE()
|
2017-02-10 16:18:38 +00:00
|
|
|
read(buf, 'readInt32BE', [1], -45552945);
|
|
|
|
read(buf, 'readInt32LE', [1], -806729475);
|
|
|
|
|
2018-12-10 12:27:32 +00:00
|
|
|
// Testing basic functionality of readIntBE() and readIntLE()
|
2017-02-10 16:18:38 +00:00
|
|
|
read(buf, 'readIntBE', [1, 1], -3);
|
|
|
|
read(buf, 'readIntLE', [2, 1], 0x48);
|
|
|
|
|
2019-01-21 00:22:27 +00:00
|
|
|
// Testing basic functionality of readUInt8()
|
2017-02-10 16:18:38 +00:00
|
|
|
read(buf, 'readUInt8', [1], 0xfd);
|
|
|
|
|
2018-12-03 16:15:45 +00:00
|
|
|
// Testing basic functionality of readUInt16BE() and readUInt16LE()
|
2017-02-10 16:18:38 +00:00
|
|
|
read(buf, 'readUInt16BE', [2], 0x48ea);
|
|
|
|
read(buf, 'readUInt16LE', [2], 0xea48);
|
|
|
|
|
2018-12-03 16:15:45 +00:00
|
|
|
// Testing basic functionality of readUInt32BE() and readUInt32LE()
|
2017-02-10 16:18:38 +00:00
|
|
|
read(buf, 'readUInt32BE', [1], 0xfd48eacf);
|
|
|
|
read(buf, 'readUInt32LE', [1], 0xcfea48fd);
|
|
|
|
|
2018-12-10 12:27:32 +00:00
|
|
|
// Testing basic functionality of readUIntBE() and readUIntLE()
|
2018-01-03 00:10:57 +00:00
|
|
|
read(buf, 'readUIntBE', [2, 2], 0x48ea);
|
|
|
|
read(buf, 'readUIntLE', [2, 2], 0xea48);
|
|
|
|
|
2018-10-29 15:46:32 +00:00
|
|
|
// Error name and message
|
|
|
|
const OOR_ERROR =
|
|
|
|
{
|
2019-03-16 11:09:14 +00:00
|
|
|
name: 'RangeError'
|
2018-10-29 15:46:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const OOB_ERROR =
|
|
|
|
{
|
2019-03-16 11:09:14 +00:00
|
|
|
name: 'RangeError',
|
2019-08-12 21:03:47 +00:00
|
|
|
message: 'Attempt to access memory outside buffer bounds'
|
2018-10-29 15:46:32 +00:00
|
|
|
};
|
|
|
|
|
2018-04-15 17:51:06 +00:00
|
|
|
// Attempt to overflow buffers, similar to previous bug in array buffers
|
2018-10-29 15:46:32 +00:00
|
|
|
assert.throws(
|
|
|
|
() => Buffer.allocUnsafe(8).readFloatBE(0xffffffff), OOR_ERROR);
|
|
|
|
|
|
|
|
assert.throws(
|
|
|
|
() => Buffer.allocUnsafe(8).readFloatLE(0xffffffff), OOR_ERROR);
|
2017-02-10 16:18:38 +00:00
|
|
|
|
2018-04-15 17:51:06 +00:00
|
|
|
// Ensure negative values can't get past offset
|
2018-10-29 15:46:32 +00:00
|
|
|
assert.throws(
|
|
|
|
() => Buffer.allocUnsafe(8).readFloatBE(-1), OOR_ERROR);
|
|
|
|
assert.throws(
|
|
|
|
() => Buffer.allocUnsafe(8).readFloatLE(-1), OOR_ERROR);
|
2017-02-10 16:18:38 +00:00
|
|
|
|
2018-04-15 17:51:06 +00:00
|
|
|
// Offset checks
|
2017-02-10 16:18:38 +00:00
|
|
|
{
|
|
|
|
const buf = Buffer.allocUnsafe(0);
|
|
|
|
|
2018-10-29 15:46:32 +00:00
|
|
|
assert.throws(
|
|
|
|
() => buf.readUInt8(0), OOB_ERROR);
|
|
|
|
assert.throws(
|
|
|
|
() => buf.readInt8(0), OOB_ERROR);
|
2017-02-10 16:18:38 +00:00
|
|
|
}
|
|
|
|
|
2018-04-15 17:51:06 +00:00
|
|
|
[16, 32].forEach((bit) => {
|
|
|
|
const buf = Buffer.allocUnsafe(bit / 8 - 1);
|
|
|
|
[`Int${bit}B`, `Int${bit}L`, `UInt${bit}B`, `UInt${bit}L`].forEach((fn) => {
|
|
|
|
assert.throws(
|
2018-10-29 15:46:32 +00:00
|
|
|
() => buf[`read${fn}E`](0), OOB_ERROR);
|
2018-04-15 17:51:06 +00:00
|
|
|
});
|
2017-02-10 16:18:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
[16, 32].forEach((bits) => {
|
|
|
|
const buf = Buffer.from([0xFF, 0xFF, 0xFF, 0xFF]);
|
2018-04-15 17:51:06 +00:00
|
|
|
['LE', 'BE'].forEach((endian) => {
|
|
|
|
assert.strictEqual(buf[`readUInt${bits}${endian}`](0),
|
|
|
|
(0xFFFFFFFF >>> (32 - bits)));
|
2017-02-10 16:18:38 +00:00
|
|
|
|
2018-04-15 17:51:06 +00:00
|
|
|
assert.strictEqual(buf[`readInt${bits}${endian}`](0),
|
|
|
|
(0xFFFFFFFF >> (32 - bits)));
|
|
|
|
});
|
2017-02-10 16:18:38 +00:00
|
|
|
});
|