1'use strict'; 2require('../common'); 3const assert = require('assert'); 4 5// Testing basic buffer read functions 6const buf = Buffer.from([0xa4, 0xfd, 0x48, 0xea, 0xcf, 0xff, 0xd9, 0x01, 0xde]); 7 8function read(buff, funx, args, expected) { 9 assert.strictEqual(buff[funx](...args), expected); 10 assert.throws( 11 () => buff[funx](-1, args[1]), 12 { code: 'ERR_OUT_OF_RANGE' } 13 ); 14} 15 16// Testing basic functionality of readDoubleBE() and readDoubleLE() 17read(buf, 'readDoubleBE', [1], -3.1827727774563287e+295); 18read(buf, 'readDoubleLE', [1], -6.966010051009108e+144); 19 20// Testing basic functionality of readFloatBE() and readFloatLE() 21read(buf, 'readFloatBE', [1], -1.6691549692541768e+37); 22read(buf, 'readFloatLE', [1], -7861303808); 23 24// Testing basic functionality of readInt8() 25read(buf, 'readInt8', [1], -3); 26 27// Testing basic functionality of readInt16BE() and readInt16LE() 28read(buf, 'readInt16BE', [1], -696); 29read(buf, 'readInt16LE', [1], 0x48fd); 30 31// Testing basic functionality of readInt32BE() and readInt32LE() 32read(buf, 'readInt32BE', [1], -45552945); 33read(buf, 'readInt32LE', [1], -806729475); 34 35// Testing basic functionality of readIntBE() and readIntLE() 36read(buf, 'readIntBE', [1, 1], -3); 37read(buf, 'readIntLE', [2, 1], 0x48); 38 39// Testing basic functionality of readUInt8() 40read(buf, 'readUInt8', [1], 0xfd); 41 42// Testing basic functionality of readUInt16BE() and readUInt16LE() 43read(buf, 'readUInt16BE', [2], 0x48ea); 44read(buf, 'readUInt16LE', [2], 0xea48); 45 46// Testing basic functionality of readUInt32BE() and readUInt32LE() 47read(buf, 'readUInt32BE', [1], 0xfd48eacf); 48read(buf, 'readUInt32LE', [1], 0xcfea48fd); 49 50// Testing basic functionality of readUIntBE() and readUIntLE() 51read(buf, 'readUIntBE', [2, 2], 0x48ea); 52read(buf, 'readUIntLE', [2, 2], 0xea48); 53 54// Error name and message 55const OOR_ERROR = 56{ 57 name: 'RangeError' 58}; 59 60const OOB_ERROR = 61{ 62 name: 'RangeError', 63 message: 'Attempt to access memory outside buffer bounds' 64}; 65 66// Attempt to overflow buffers, similar to previous bug in array buffers 67assert.throws( 68 () => Buffer.allocUnsafe(8).readFloatBE(0xffffffff), OOR_ERROR); 69 70assert.throws( 71 () => Buffer.allocUnsafe(8).readFloatLE(0xffffffff), OOR_ERROR); 72 73// Ensure negative values can't get past offset 74assert.throws( 75 () => Buffer.allocUnsafe(8).readFloatBE(-1), OOR_ERROR); 76assert.throws( 77 () => Buffer.allocUnsafe(8).readFloatLE(-1), OOR_ERROR); 78 79// Offset checks 80{ 81 const buf = Buffer.allocUnsafe(0); 82 83 assert.throws( 84 () => buf.readUInt8(0), OOB_ERROR); 85 assert.throws( 86 () => buf.readInt8(0), OOB_ERROR); 87} 88 89[16, 32].forEach((bit) => { 90 const buf = Buffer.allocUnsafe(bit / 8 - 1); 91 [`Int${bit}B`, `Int${bit}L`, `UInt${bit}B`, `UInt${bit}L`].forEach((fn) => { 92 assert.throws( 93 () => buf[`read${fn}E`](0), OOB_ERROR); 94 }); 95}); 96 97[16, 32].forEach((bits) => { 98 const buf = Buffer.from([0xFF, 0xFF, 0xFF, 0xFF]); 99 ['LE', 'BE'].forEach((endian) => { 100 assert.strictEqual(buf[`readUInt${bits}${endian}`](0), 101 (0xFFFFFFFF >>> (32 - bits))); 102 103 assert.strictEqual(buf[`readInt${bits}${endian}`](0), 104 (0xFFFFFFFF >> (32 - bits))); 105 }); 106}); 107