|
| 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