1'use strict'; 2 3require('../common'); 4const assert = require('assert'); 5 6/* 7 * We need to check the following things: 8 * - We are correctly resolving big endian (doesn't mean anything for 8 bit) 9 * - Correctly resolving little endian (doesn't mean anything for 8 bit) 10 * - Correctly using the offsets 11 * - Correctly interpreting values that are beyond the signed range as unsigned 12 */ 13 14{ // OOB 15 const data = Buffer.alloc(8); 16 ['UInt8', 'UInt16BE', 'UInt16LE', 'UInt32BE', 'UInt32LE'].forEach((fn) => { 17 18 // Verify that default offset works fine. 19 data[`write${fn}`](23, undefined); 20 data[`write${fn}`](23); 21 22 ['', '0', null, {}, [], () => {}, true, false].forEach((o) => { 23 assert.throws( 24 () => data[`write${fn}`](23, o), 25 { code: 'ERR_INVALID_ARG_TYPE' }); 26 }); 27 28 [NaN, Infinity, -1, 1.01].forEach((o) => { 29 assert.throws( 30 () => data[`write${fn}`](23, o), 31 { code: 'ERR_OUT_OF_RANGE' }); 32 }); 33 }); 34} 35 36{ // Test 8 bit 37 const data = Buffer.alloc(4); 38 39 data.writeUInt8(23, 0); 40 data.writeUInt8(23, 1); 41 data.writeUInt8(23, 2); 42 data.writeUInt8(23, 3); 43 assert.ok(data.equals(new Uint8Array([23, 23, 23, 23]))); 44 45 data.writeUInt8(23, 0); 46 data.writeUInt8(23, 1); 47 data.writeUInt8(23, 2); 48 data.writeUInt8(23, 3); 49 assert.ok(data.equals(new Uint8Array([23, 23, 23, 23]))); 50 51 data.writeUInt8(255, 0); 52 assert.strictEqual(data[0], 255); 53 54 data.writeUInt8(255, 0); 55 assert.strictEqual(data[0], 255); 56} 57 58// Test 16 bit 59{ 60 let value = 0x2343; 61 const data = Buffer.alloc(4); 62 63 data.writeUInt16BE(value, 0); 64 assert.ok(data.equals(new Uint8Array([0x23, 0x43, 0, 0]))); 65 66 data.writeUInt16BE(value, 1); 67 assert.ok(data.equals(new Uint8Array([0x23, 0x23, 0x43, 0]))); 68 69 data.writeUInt16BE(value, 2); 70 assert.ok(data.equals(new Uint8Array([0x23, 0x23, 0x23, 0x43]))); 71 72 data.writeUInt16LE(value, 0); 73 assert.ok(data.equals(new Uint8Array([0x43, 0x23, 0x23, 0x43]))); 74 75 data.writeUInt16LE(value, 1); 76 assert.ok(data.equals(new Uint8Array([0x43, 0x43, 0x23, 0x43]))); 77 78 data.writeUInt16LE(value, 2); 79 assert.ok(data.equals(new Uint8Array([0x43, 0x43, 0x43, 0x23]))); 80 81 value = 0xff80; 82 data.writeUInt16LE(value, 0); 83 assert.ok(data.equals(new Uint8Array([0x80, 0xff, 0x43, 0x23]))); 84 85 data.writeUInt16BE(value, 0); 86 assert.ok(data.equals(new Uint8Array([0xff, 0x80, 0x43, 0x23]))); 87 88 value = 0xfffff; 89 ['writeUInt16BE', 'writeUInt16LE'].forEach((fn) => { 90 assert.throws( 91 () => data[fn](value, 0), 92 { 93 code: 'ERR_OUT_OF_RANGE', 94 message: 'The value of "value" is out of range. ' + 95 `It must be >= 0 and <= 65535. Received ${value}` 96 } 97 ); 98 }); 99} 100 101// Test 32 bit 102{ 103 const data = Buffer.alloc(6); 104 const value = 0xe7f90a6d; 105 106 data.writeUInt32BE(value, 0); 107 assert.ok(data.equals(new Uint8Array([0xe7, 0xf9, 0x0a, 0x6d, 0, 0]))); 108 109 data.writeUInt32BE(value, 1); 110 assert.ok(data.equals(new Uint8Array([0xe7, 0xe7, 0xf9, 0x0a, 0x6d, 0]))); 111 112 data.writeUInt32BE(value, 2); 113 assert.ok(data.equals(new Uint8Array([0xe7, 0xe7, 0xe7, 0xf9, 0x0a, 0x6d]))); 114 115 data.writeUInt32LE(value, 0); 116 assert.ok(data.equals(new Uint8Array([0x6d, 0x0a, 0xf9, 0xe7, 0x0a, 0x6d]))); 117 118 data.writeUInt32LE(value, 1); 119 assert.ok(data.equals(new Uint8Array([0x6d, 0x6d, 0x0a, 0xf9, 0xe7, 0x6d]))); 120 121 data.writeUInt32LE(value, 2); 122 assert.ok(data.equals(new Uint8Array([0x6d, 0x6d, 0x6d, 0x0a, 0xf9, 0xe7]))); 123} 124 125// Test 48 bit 126{ 127 const value = 0x1234567890ab; 128 const data = Buffer.allocUnsafe(6); 129 data.writeUIntBE(value, 0, 6); 130 assert.ok(data.equals(new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]))); 131 132 data.writeUIntLE(value, 0, 6); 133 assert.ok(data.equals(new Uint8Array([0xab, 0x90, 0x78, 0x56, 0x34, 0x12]))); 134} 135 136// Test UInt 137{ 138 const data = Buffer.alloc(8); 139 let val = 0x100; 140 141 // Check byteLength. 142 ['writeUIntBE', 'writeUIntLE'].forEach((fn) => { 143 ['', '0', null, {}, [], () => {}, true, false, undefined].forEach((bl) => { 144 assert.throws( 145 () => data[fn](23, 0, bl), 146 { code: 'ERR_INVALID_ARG_TYPE' }); 147 }); 148 149 [Infinity, -1].forEach((byteLength) => { 150 assert.throws( 151 () => data[fn](23, 0, byteLength), 152 { 153 code: 'ERR_OUT_OF_RANGE', 154 message: 'The value of "byteLength" is out of range. ' + 155 `It must be >= 1 and <= 6. Received ${byteLength}` 156 } 157 ); 158 }); 159 160 [NaN, 1.01].forEach((byteLength) => { 161 assert.throws( 162 () => data[fn](42, 0, byteLength), 163 { 164 code: 'ERR_OUT_OF_RANGE', 165 name: 'RangeError', 166 message: 'The value of "byteLength" is out of range. ' + 167 `It must be an integer. Received ${byteLength}` 168 }); 169 }); 170 }); 171 172 // Test 1 to 6 bytes. 173 for (let i = 1; i <= 6; i++) { 174 const range = i < 5 ? `= ${val - 1}` : ` 2 ** ${i * 8}`; 175 const received = i > 4 ? 176 String(val).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1_') : 177 val; 178 ['writeUIntBE', 'writeUIntLE'].forEach((fn) => { 179 assert.throws(() => { 180 data[fn](val, 0, i); 181 }, { 182 code: 'ERR_OUT_OF_RANGE', 183 name: 'RangeError', 184 message: 'The value of "value" is out of range. ' + 185 `It must be >= 0 and <${range}. Received ${received}` 186 }); 187 188 ['', '0', null, {}, [], () => {}, true, false].forEach((o) => { 189 assert.throws( 190 () => data[fn](23, o, i), 191 { 192 code: 'ERR_INVALID_ARG_TYPE', 193 name: 'TypeError' 194 }); 195 }); 196 197 [Infinity, -1, -4294967295].forEach((offset) => { 198 assert.throws( 199 () => data[fn](val - 1, offset, i), 200 { 201 code: 'ERR_OUT_OF_RANGE', 202 name: 'RangeError', 203 message: 'The value of "offset" is out of range. ' + 204 `It must be >= 0 and <= ${8 - i}. Received ${offset}` 205 }); 206 }); 207 208 [NaN, 1.01].forEach((offset) => { 209 assert.throws( 210 () => data[fn](val - 1, offset, i), 211 { 212 code: 'ERR_OUT_OF_RANGE', 213 name: 'RangeError', 214 message: 'The value of "offset" is out of range. ' + 215 `It must be an integer. Received ${offset}` 216 }); 217 }); 218 }); 219 220 val *= 0x100; 221 } 222} 223 224for (const fn of [ 225 'UInt8', 'UInt16LE', 'UInt16BE', 'UInt32LE', 'UInt32BE', 'UIntLE', 'UIntBE', 226 'BigUInt64LE', 'BigUInt64BE', 227]) { 228 const p = Buffer.prototype; 229 const lowerFn = fn.replace(/UInt/, 'Uint'); 230 assert.strictEqual(p[`write${fn}`], p[`write${lowerFn}`]); 231 assert.strictEqual(p[`read${fn}`], p[`read${lowerFn}`]); 232} 233