11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_cirequire('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci// We need to check the following things: 71cb0ef41Sopenharmony_ci// - We are correctly resolving big endian (doesn't mean anything for 8 bit) 81cb0ef41Sopenharmony_ci// - Correctly resolving little endian (doesn't mean anything for 8 bit) 91cb0ef41Sopenharmony_ci// - Correctly using the offsets 101cb0ef41Sopenharmony_ci// - Correctly interpreting values that are beyond the signed range as unsigned 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci{ // OOB 131cb0ef41Sopenharmony_ci const data = Buffer.alloc(8); 141cb0ef41Sopenharmony_ci ['UInt8', 'UInt16BE', 'UInt16LE', 'UInt32BE', 'UInt32LE'].forEach((fn) => { 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci // Verify that default offset works fine. 171cb0ef41Sopenharmony_ci data[`write${fn}`](23, undefined); 181cb0ef41Sopenharmony_ci data[`write${fn}`](23); 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci ['', '0', null, {}, [], () => {}, true, false].forEach((o) => { 211cb0ef41Sopenharmony_ci assert.throws( 221cb0ef41Sopenharmony_ci () => data[`write${fn}`](23, o), 231cb0ef41Sopenharmony_ci { code: 'ERR_INVALID_ARG_TYPE' }); 241cb0ef41Sopenharmony_ci }); 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci [NaN, Infinity, -1, 1.01].forEach((o) => { 271cb0ef41Sopenharmony_ci assert.throws( 281cb0ef41Sopenharmony_ci () => data[`write${fn}`](23, o), 291cb0ef41Sopenharmony_ci { code: 'ERR_OUT_OF_RANGE' }); 301cb0ef41Sopenharmony_ci }); 311cb0ef41Sopenharmony_ci }); 321cb0ef41Sopenharmony_ci} 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci{ // Test 8 bit 351cb0ef41Sopenharmony_ci const data = Buffer.alloc(4); 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci data.writeUInt8(23, 0); 381cb0ef41Sopenharmony_ci data.writeUInt8(23, 1); 391cb0ef41Sopenharmony_ci data.writeUInt8(23, 2); 401cb0ef41Sopenharmony_ci data.writeUInt8(23, 3); 411cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([23, 23, 23, 23]))); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci data.writeUInt8(23, 0); 441cb0ef41Sopenharmony_ci data.writeUInt8(23, 1); 451cb0ef41Sopenharmony_ci data.writeUInt8(23, 2); 461cb0ef41Sopenharmony_ci data.writeUInt8(23, 3); 471cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([23, 23, 23, 23]))); 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci data.writeUInt8(255, 0); 501cb0ef41Sopenharmony_ci assert.strictEqual(data[0], 255); 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci data.writeUInt8(255, 0); 531cb0ef41Sopenharmony_ci assert.strictEqual(data[0], 255); 541cb0ef41Sopenharmony_ci} 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci// Test 16 bit 571cb0ef41Sopenharmony_ci{ 581cb0ef41Sopenharmony_ci let value = 0x2343; 591cb0ef41Sopenharmony_ci const data = Buffer.alloc(4); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci data.writeUInt16BE(value, 0); 621cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([0x23, 0x43, 0, 0]))); 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci data.writeUInt16BE(value, 1); 651cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([0x23, 0x23, 0x43, 0]))); 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci data.writeUInt16BE(value, 2); 681cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([0x23, 0x23, 0x23, 0x43]))); 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci data.writeUInt16LE(value, 0); 711cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([0x43, 0x23, 0x23, 0x43]))); 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci data.writeUInt16LE(value, 1); 741cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([0x43, 0x43, 0x23, 0x43]))); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci data.writeUInt16LE(value, 2); 771cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([0x43, 0x43, 0x43, 0x23]))); 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci value = 0xff80; 801cb0ef41Sopenharmony_ci data.writeUInt16LE(value, 0); 811cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([0x80, 0xff, 0x43, 0x23]))); 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci data.writeUInt16BE(value, 0); 841cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([0xff, 0x80, 0x43, 0x23]))); 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci value = 0xfffff; 871cb0ef41Sopenharmony_ci ['writeUInt16BE', 'writeUInt16LE'].forEach((fn) => { 881cb0ef41Sopenharmony_ci assert.throws( 891cb0ef41Sopenharmony_ci () => data[fn](value, 0), 901cb0ef41Sopenharmony_ci { 911cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 921cb0ef41Sopenharmony_ci message: 'The value of "value" is out of range. ' + 931cb0ef41Sopenharmony_ci `It must be >= 0 and <= 65535. Received ${value}` 941cb0ef41Sopenharmony_ci } 951cb0ef41Sopenharmony_ci ); 961cb0ef41Sopenharmony_ci }); 971cb0ef41Sopenharmony_ci} 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci// Test 32 bit 1001cb0ef41Sopenharmony_ci{ 1011cb0ef41Sopenharmony_ci const data = Buffer.alloc(6); 1021cb0ef41Sopenharmony_ci const value = 0xe7f90a6d; 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ci data.writeUInt32BE(value, 0); 1051cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([0xe7, 0xf9, 0x0a, 0x6d, 0, 0]))); 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci data.writeUInt32BE(value, 1); 1081cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([0xe7, 0xe7, 0xf9, 0x0a, 0x6d, 0]))); 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci data.writeUInt32BE(value, 2); 1111cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([0xe7, 0xe7, 0xe7, 0xf9, 0x0a, 0x6d]))); 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci data.writeUInt32LE(value, 0); 1141cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([0x6d, 0x0a, 0xf9, 0xe7, 0x0a, 0x6d]))); 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci data.writeUInt32LE(value, 1); 1171cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([0x6d, 0x6d, 0x0a, 0xf9, 0xe7, 0x6d]))); 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci data.writeUInt32LE(value, 2); 1201cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([0x6d, 0x6d, 0x6d, 0x0a, 0xf9, 0xe7]))); 1211cb0ef41Sopenharmony_ci} 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci// Test 48 bit 1241cb0ef41Sopenharmony_ci{ 1251cb0ef41Sopenharmony_ci const value = 0x1234567890ab; 1261cb0ef41Sopenharmony_ci const data = Buffer.allocUnsafe(6); 1271cb0ef41Sopenharmony_ci data.writeUIntBE(value, 0, 6); 1281cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]))); 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci data.writeUIntLE(value, 0, 6); 1311cb0ef41Sopenharmony_ci assert.ok(data.equals(new Uint8Array([0xab, 0x90, 0x78, 0x56, 0x34, 0x12]))); 1321cb0ef41Sopenharmony_ci} 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci// Test UInt 1351cb0ef41Sopenharmony_ci{ 1361cb0ef41Sopenharmony_ci const data = Buffer.alloc(8); 1371cb0ef41Sopenharmony_ci let val = 0x100; 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ci // Check byteLength. 1401cb0ef41Sopenharmony_ci ['writeUIntBE', 'writeUIntLE'].forEach((fn) => { 1411cb0ef41Sopenharmony_ci ['', '0', null, {}, [], () => {}, true, false, undefined].forEach((bl) => { 1421cb0ef41Sopenharmony_ci assert.throws( 1431cb0ef41Sopenharmony_ci () => data[fn](23, 0, bl), 1441cb0ef41Sopenharmony_ci { code: 'ERR_INVALID_ARG_TYPE' }); 1451cb0ef41Sopenharmony_ci }); 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_ci [Infinity, -1].forEach((byteLength) => { 1481cb0ef41Sopenharmony_ci assert.throws( 1491cb0ef41Sopenharmony_ci () => data[fn](23, 0, byteLength), 1501cb0ef41Sopenharmony_ci { 1511cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1521cb0ef41Sopenharmony_ci message: 'The value of "byteLength" is out of range. ' + 1531cb0ef41Sopenharmony_ci `It must be >= 1 and <= 6. Received ${byteLength}` 1541cb0ef41Sopenharmony_ci } 1551cb0ef41Sopenharmony_ci ); 1561cb0ef41Sopenharmony_ci }); 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci [NaN, 1.01].forEach((byteLength) => { 1591cb0ef41Sopenharmony_ci assert.throws( 1601cb0ef41Sopenharmony_ci () => data[fn](42, 0, byteLength), 1611cb0ef41Sopenharmony_ci { 1621cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1631cb0ef41Sopenharmony_ci name: 'RangeError', 1641cb0ef41Sopenharmony_ci message: 'The value of "byteLength" is out of range. ' + 1651cb0ef41Sopenharmony_ci `It must be an integer. Received ${byteLength}` 1661cb0ef41Sopenharmony_ci }); 1671cb0ef41Sopenharmony_ci }); 1681cb0ef41Sopenharmony_ci }); 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_ci // Test 1 to 6 bytes. 1711cb0ef41Sopenharmony_ci for (let i = 1; i <= 6; i++) { 1721cb0ef41Sopenharmony_ci const range = i < 5 ? `= ${val - 1}` : ` 2 ** ${i * 8}`; 1731cb0ef41Sopenharmony_ci const received = i > 4 ? 1741cb0ef41Sopenharmony_ci String(val).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1_') : 1751cb0ef41Sopenharmony_ci val; 1761cb0ef41Sopenharmony_ci ['writeUIntBE', 'writeUIntLE'].forEach((fn) => { 1771cb0ef41Sopenharmony_ci assert.throws(() => { 1781cb0ef41Sopenharmony_ci data[fn](val, 0, i); 1791cb0ef41Sopenharmony_ci }, { 1801cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1811cb0ef41Sopenharmony_ci name: 'RangeError', 1821cb0ef41Sopenharmony_ci message: 'The value of "value" is out of range. ' + 1831cb0ef41Sopenharmony_ci `It must be >= 0 and <${range}. Received ${received}` 1841cb0ef41Sopenharmony_ci }); 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_ci ['', '0', null, {}, [], () => {}, true, false].forEach((o) => { 1871cb0ef41Sopenharmony_ci assert.throws( 1881cb0ef41Sopenharmony_ci () => data[fn](23, o, i), 1891cb0ef41Sopenharmony_ci { 1901cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 1911cb0ef41Sopenharmony_ci name: 'TypeError' 1921cb0ef41Sopenharmony_ci }); 1931cb0ef41Sopenharmony_ci }); 1941cb0ef41Sopenharmony_ci 1951cb0ef41Sopenharmony_ci [Infinity, -1, -4294967295].forEach((offset) => { 1961cb0ef41Sopenharmony_ci assert.throws( 1971cb0ef41Sopenharmony_ci () => data[fn](val - 1, offset, i), 1981cb0ef41Sopenharmony_ci { 1991cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 2001cb0ef41Sopenharmony_ci name: 'RangeError', 2011cb0ef41Sopenharmony_ci message: 'The value of "offset" is out of range. ' + 2021cb0ef41Sopenharmony_ci `It must be >= 0 and <= ${8 - i}. Received ${offset}` 2031cb0ef41Sopenharmony_ci }); 2041cb0ef41Sopenharmony_ci }); 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ci [NaN, 1.01].forEach((offset) => { 2071cb0ef41Sopenharmony_ci assert.throws( 2081cb0ef41Sopenharmony_ci () => data[fn](val - 1, offset, i), 2091cb0ef41Sopenharmony_ci { 2101cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 2111cb0ef41Sopenharmony_ci name: 'RangeError', 2121cb0ef41Sopenharmony_ci message: 'The value of "offset" is out of range. ' + 2131cb0ef41Sopenharmony_ci `It must be an integer. Received ${offset}` 2141cb0ef41Sopenharmony_ci }); 2151cb0ef41Sopenharmony_ci }); 2161cb0ef41Sopenharmony_ci }); 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci val *= 0x100; 2191cb0ef41Sopenharmony_ci } 2201cb0ef41Sopenharmony_ci} 2211cb0ef41Sopenharmony_ci 2221cb0ef41Sopenharmony_cifor (const fn of [ 2231cb0ef41Sopenharmony_ci 'UInt8', 'UInt16LE', 'UInt16BE', 'UInt32LE', 'UInt32BE', 'UIntLE', 'UIntBE', 2241cb0ef41Sopenharmony_ci 'BigUInt64LE', 'BigUInt64BE', 2251cb0ef41Sopenharmony_ci]) { 2261cb0ef41Sopenharmony_ci const p = Buffer.prototype; 2271cb0ef41Sopenharmony_ci const lowerFn = fn.replace(/UInt/, 'Uint'); 2281cb0ef41Sopenharmony_ci assert.strictEqual(p[`write${fn}`], p[`write${lowerFn}`]); 2291cb0ef41Sopenharmony_ci assert.strictEqual(p[`read${fn}`], p[`read${lowerFn}`]); 2301cb0ef41Sopenharmony_ci} 231