11cb0ef41Sopenharmony_ciimport * as common from '../common/index.mjs'; 21cb0ef41Sopenharmony_ciimport * as fixtures from '../common/fixtures.mjs'; 31cb0ef41Sopenharmony_ciimport fs from 'fs'; 41cb0ef41Sopenharmony_ciimport assert from 'assert'; 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci// This test ensures that "position" argument is correctly validated 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst filepath = fixtures.path('x.txt'); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciconst buffer = Buffer.from('xyz\n'); 111cb0ef41Sopenharmony_ciconst offset = 0; 121cb0ef41Sopenharmony_ciconst length = buffer.byteLength; 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci// allowedErrors is an array of acceptable internal errors 151cb0ef41Sopenharmony_ci// For example, on some platforms read syscall might return -EFBIG or -EOVERFLOW 161cb0ef41Sopenharmony_cifunction testValid(position, allowedErrors = []) { 171cb0ef41Sopenharmony_ci let fdSync; 181cb0ef41Sopenharmony_ci try { 191cb0ef41Sopenharmony_ci fdSync = fs.openSync(filepath, 'r'); 201cb0ef41Sopenharmony_ci fs.readSync(fdSync, buffer, offset, length, position); 211cb0ef41Sopenharmony_ci fs.readSync(fdSync, buffer, common.mustNotMutateObjectDeep({ offset, length, position })); 221cb0ef41Sopenharmony_ci } catch (err) { 231cb0ef41Sopenharmony_ci if (!allowedErrors.includes(err.code)) { 241cb0ef41Sopenharmony_ci assert.fail(err); 251cb0ef41Sopenharmony_ci } 261cb0ef41Sopenharmony_ci } finally { 271cb0ef41Sopenharmony_ci if (fdSync) fs.closeSync(fdSync); 281cb0ef41Sopenharmony_ci } 291cb0ef41Sopenharmony_ci} 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_cifunction testInvalid(code, position, internalCatch = false) { 321cb0ef41Sopenharmony_ci let fdSync; 331cb0ef41Sopenharmony_ci try { 341cb0ef41Sopenharmony_ci fdSync = fs.openSync(filepath, 'r'); 351cb0ef41Sopenharmony_ci assert.throws( 361cb0ef41Sopenharmony_ci () => fs.readSync(fdSync, buffer, offset, length, position), 371cb0ef41Sopenharmony_ci { code } 381cb0ef41Sopenharmony_ci ); 391cb0ef41Sopenharmony_ci assert.throws( 401cb0ef41Sopenharmony_ci () => fs.readSync(fdSync, buffer, common.mustNotMutateObjectDeep({ offset, length, position })), 411cb0ef41Sopenharmony_ci { code } 421cb0ef41Sopenharmony_ci ); 431cb0ef41Sopenharmony_ci } finally { 441cb0ef41Sopenharmony_ci if (fdSync) fs.closeSync(fdSync); 451cb0ef41Sopenharmony_ci } 461cb0ef41Sopenharmony_ci} 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci{ 491cb0ef41Sopenharmony_ci testValid(undefined); 501cb0ef41Sopenharmony_ci testValid(null); 511cb0ef41Sopenharmony_ci testValid(-1); 521cb0ef41Sopenharmony_ci testValid(-1n); 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci testValid(0); 551cb0ef41Sopenharmony_ci testValid(0n); 561cb0ef41Sopenharmony_ci testValid(1); 571cb0ef41Sopenharmony_ci testValid(1n); 581cb0ef41Sopenharmony_ci testValid(9); 591cb0ef41Sopenharmony_ci testValid(9n); 601cb0ef41Sopenharmony_ci testValid(Number.MAX_SAFE_INTEGER, [ 'EFBIG', 'EOVERFLOW' ]); 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci testValid(2n ** 63n - 1n - BigInt(length), [ 'EFBIG', 'EOVERFLOW' ]); 631cb0ef41Sopenharmony_ci testInvalid('ERR_OUT_OF_RANGE', 2n ** 63n); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci // TODO(LiviaMedeiros): test `2n ** 63n - BigInt(length)` 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci testInvalid('ERR_OUT_OF_RANGE', NaN); 681cb0ef41Sopenharmony_ci testInvalid('ERR_OUT_OF_RANGE', -Infinity); 691cb0ef41Sopenharmony_ci testInvalid('ERR_OUT_OF_RANGE', Infinity); 701cb0ef41Sopenharmony_ci testInvalid('ERR_OUT_OF_RANGE', -0.999); 711cb0ef41Sopenharmony_ci testInvalid('ERR_OUT_OF_RANGE', -(2n ** 64n)); 721cb0ef41Sopenharmony_ci testInvalid('ERR_OUT_OF_RANGE', Number.MAX_SAFE_INTEGER + 1); 731cb0ef41Sopenharmony_ci testInvalid('ERR_OUT_OF_RANGE', Number.MAX_VALUE); 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci for (const badTypeValue of [ 761cb0ef41Sopenharmony_ci false, true, '1', Symbol(1), {}, [], () => {}, Promise.resolve(1), 771cb0ef41Sopenharmony_ci ]) { 781cb0ef41Sopenharmony_ci testInvalid('ERR_INVALID_ARG_TYPE', badTypeValue); 791cb0ef41Sopenharmony_ci } 801cb0ef41Sopenharmony_ci} 81