11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_cirequire('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci// Test OOB 71cb0ef41Sopenharmony_ci{ 81cb0ef41Sopenharmony_ci const buffer = Buffer.alloc(4); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci ['UInt8', 'UInt16BE', 'UInt16LE', 'UInt32BE', 'UInt32LE'].forEach((fn) => { 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci // Verify that default offset works fine. 131cb0ef41Sopenharmony_ci buffer[`read${fn}`](undefined); 141cb0ef41Sopenharmony_ci buffer[`read${fn}`](); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci ['', '0', null, {}, [], () => {}, true, false].forEach((o) => { 171cb0ef41Sopenharmony_ci assert.throws( 181cb0ef41Sopenharmony_ci () => buffer[`read${fn}`](o), 191cb0ef41Sopenharmony_ci { 201cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 211cb0ef41Sopenharmony_ci name: 'TypeError' 221cb0ef41Sopenharmony_ci }); 231cb0ef41Sopenharmony_ci }); 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci [Infinity, -1, -4294967295].forEach((offset) => { 261cb0ef41Sopenharmony_ci assert.throws( 271cb0ef41Sopenharmony_ci () => buffer[`read${fn}`](offset), 281cb0ef41Sopenharmony_ci { 291cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 301cb0ef41Sopenharmony_ci name: 'RangeError' 311cb0ef41Sopenharmony_ci }); 321cb0ef41Sopenharmony_ci }); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci [NaN, 1.01].forEach((offset) => { 351cb0ef41Sopenharmony_ci assert.throws( 361cb0ef41Sopenharmony_ci () => buffer[`read${fn}`](offset), 371cb0ef41Sopenharmony_ci { 381cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 391cb0ef41Sopenharmony_ci name: 'RangeError', 401cb0ef41Sopenharmony_ci message: 'The value of "offset" is out of range. ' + 411cb0ef41Sopenharmony_ci `It must be an integer. Received ${offset}` 421cb0ef41Sopenharmony_ci }); 431cb0ef41Sopenharmony_ci }); 441cb0ef41Sopenharmony_ci }); 451cb0ef41Sopenharmony_ci} 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci// Test 8 bit unsigned integers 481cb0ef41Sopenharmony_ci{ 491cb0ef41Sopenharmony_ci const data = Buffer.from([0xff, 0x2a, 0x2a, 0x2a]); 501cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt8(0), 255); 511cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt8(1), 42); 521cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt8(2), 42); 531cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt8(3), 42); 541cb0ef41Sopenharmony_ci} 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci// Test 16 bit unsigned integers 571cb0ef41Sopenharmony_ci{ 581cb0ef41Sopenharmony_ci const data = Buffer.from([0x00, 0x2a, 0x42, 0x3f]); 591cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt16BE(0), 0x2a); 601cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt16BE(1), 0x2a42); 611cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt16BE(2), 0x423f); 621cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt16LE(0), 0x2a00); 631cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt16LE(1), 0x422a); 641cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt16LE(2), 0x3f42); 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci data[0] = 0xfe; 671cb0ef41Sopenharmony_ci data[1] = 0xfe; 681cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt16BE(0), 0xfefe); 691cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt16LE(0), 0xfefe); 701cb0ef41Sopenharmony_ci} 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci// Test 32 bit unsigned integers 731cb0ef41Sopenharmony_ci{ 741cb0ef41Sopenharmony_ci const data = Buffer.from([0x32, 0x65, 0x42, 0x56, 0x23, 0xff]); 751cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt32BE(0), 0x32654256); 761cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt32BE(1), 0x65425623); 771cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt32BE(2), 0x425623ff); 781cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt32LE(0), 0x56426532); 791cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt32LE(1), 0x23564265); 801cb0ef41Sopenharmony_ci assert.strictEqual(data.readUInt32LE(2), 0xff235642); 811cb0ef41Sopenharmony_ci} 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci// Test UInt 841cb0ef41Sopenharmony_ci{ 851cb0ef41Sopenharmony_ci const buffer = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]); 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci assert.strictEqual(buffer.readUIntLE(0, 1), 0x01); 881cb0ef41Sopenharmony_ci assert.strictEqual(buffer.readUIntBE(0, 1), 0x01); 891cb0ef41Sopenharmony_ci assert.strictEqual(buffer.readUIntLE(0, 3), 0x030201); 901cb0ef41Sopenharmony_ci assert.strictEqual(buffer.readUIntBE(0, 3), 0x010203); 911cb0ef41Sopenharmony_ci assert.strictEqual(buffer.readUIntLE(0, 5), 0x0504030201); 921cb0ef41Sopenharmony_ci assert.strictEqual(buffer.readUIntBE(0, 5), 0x0102030405); 931cb0ef41Sopenharmony_ci assert.strictEqual(buffer.readUIntLE(0, 6), 0x060504030201); 941cb0ef41Sopenharmony_ci assert.strictEqual(buffer.readUIntBE(0, 6), 0x010203040506); 951cb0ef41Sopenharmony_ci assert.strictEqual(buffer.readUIntLE(1, 6), 0x070605040302); 961cb0ef41Sopenharmony_ci assert.strictEqual(buffer.readUIntBE(1, 6), 0x020304050607); 971cb0ef41Sopenharmony_ci assert.strictEqual(buffer.readUIntLE(2, 6), 0x080706050403); 981cb0ef41Sopenharmony_ci assert.strictEqual(buffer.readUIntBE(2, 6), 0x030405060708); 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci // Check byteLength. 1011cb0ef41Sopenharmony_ci ['readUIntBE', 'readUIntLE'].forEach((fn) => { 1021cb0ef41Sopenharmony_ci ['', '0', null, {}, [], () => {}, true, false, undefined].forEach((len) => { 1031cb0ef41Sopenharmony_ci assert.throws( 1041cb0ef41Sopenharmony_ci () => buffer[fn](0, len), 1051cb0ef41Sopenharmony_ci { code: 'ERR_INVALID_ARG_TYPE' }); 1061cb0ef41Sopenharmony_ci }); 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci [Infinity, -1].forEach((byteLength) => { 1091cb0ef41Sopenharmony_ci assert.throws( 1101cb0ef41Sopenharmony_ci () => buffer[fn](0, byteLength), 1111cb0ef41Sopenharmony_ci { 1121cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1131cb0ef41Sopenharmony_ci message: 'The value of "byteLength" is out of range. ' + 1141cb0ef41Sopenharmony_ci `It must be >= 1 and <= 6. Received ${byteLength}` 1151cb0ef41Sopenharmony_ci }); 1161cb0ef41Sopenharmony_ci }); 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci [NaN, 1.01].forEach((byteLength) => { 1191cb0ef41Sopenharmony_ci assert.throws( 1201cb0ef41Sopenharmony_ci () => buffer[fn](0, byteLength), 1211cb0ef41Sopenharmony_ci { 1221cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1231cb0ef41Sopenharmony_ci name: 'RangeError', 1241cb0ef41Sopenharmony_ci message: 'The value of "byteLength" is out of range. ' + 1251cb0ef41Sopenharmony_ci `It must be an integer. Received ${byteLength}` 1261cb0ef41Sopenharmony_ci }); 1271cb0ef41Sopenharmony_ci }); 1281cb0ef41Sopenharmony_ci }); 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci // Test 1 to 6 bytes. 1311cb0ef41Sopenharmony_ci for (let i = 1; i <= 6; i++) { 1321cb0ef41Sopenharmony_ci ['readUIntBE', 'readUIntLE'].forEach((fn) => { 1331cb0ef41Sopenharmony_ci ['', '0', null, {}, [], () => {}, true, false, undefined].forEach((o) => { 1341cb0ef41Sopenharmony_ci assert.throws( 1351cb0ef41Sopenharmony_ci () => buffer[fn](o, i), 1361cb0ef41Sopenharmony_ci { 1371cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 1381cb0ef41Sopenharmony_ci name: 'TypeError' 1391cb0ef41Sopenharmony_ci }); 1401cb0ef41Sopenharmony_ci }); 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ci [Infinity, -1, -4294967295].forEach((offset) => { 1431cb0ef41Sopenharmony_ci assert.throws( 1441cb0ef41Sopenharmony_ci () => buffer[fn](offset, i), 1451cb0ef41Sopenharmony_ci { 1461cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1471cb0ef41Sopenharmony_ci name: 'RangeError', 1481cb0ef41Sopenharmony_ci message: 'The value of "offset" is out of range. ' + 1491cb0ef41Sopenharmony_ci `It must be >= 0 and <= ${8 - i}. Received ${offset}` 1501cb0ef41Sopenharmony_ci }); 1511cb0ef41Sopenharmony_ci }); 1521cb0ef41Sopenharmony_ci 1531cb0ef41Sopenharmony_ci [NaN, 1.01].forEach((offset) => { 1541cb0ef41Sopenharmony_ci assert.throws( 1551cb0ef41Sopenharmony_ci () => buffer[fn](offset, i), 1561cb0ef41Sopenharmony_ci { 1571cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1581cb0ef41Sopenharmony_ci name: 'RangeError', 1591cb0ef41Sopenharmony_ci message: 'The value of "offset" is out of range. ' + 1601cb0ef41Sopenharmony_ci `It must be an integer. Received ${offset}` 1611cb0ef41Sopenharmony_ci }); 1621cb0ef41Sopenharmony_ci }); 1631cb0ef41Sopenharmony_ci }); 1641cb0ef41Sopenharmony_ci } 1651cb0ef41Sopenharmony_ci} 166