mirror of
https://github.com/nodejs/node.git
synced 2024-11-24 12:10:08 +01:00
f0e720a7fa
in test-fs-read-promises-position-validation.mjs As stated in https://github.com/nodejs/node/issues/50054 This looks like an oversight as test-fs-read-position-validation.mjs includes EOVERFLOW as an allowed error. Fixes https://github.com/nodejs/node/issues/50054 PR-URL: https://github.com/nodejs/node/pull/50128 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
85 lines
2.3 KiB
JavaScript
85 lines
2.3 KiB
JavaScript
import '../common/index.mjs';
|
|
import * as fixtures from '../common/fixtures.mjs';
|
|
import fs from 'fs';
|
|
import assert from 'assert';
|
|
|
|
// This test ensures that "position" argument is correctly validated
|
|
|
|
const filepath = fixtures.path('x.txt');
|
|
|
|
const buffer = Buffer.from('xyz\n');
|
|
const offset = 0;
|
|
const length = buffer.byteLength;
|
|
|
|
// allowedErrors is an array of acceptable internal errors
|
|
// For example, on some platforms read syscall might return -EFBIG
|
|
async function testValid(position, allowedErrors = []) {
|
|
let fh;
|
|
try {
|
|
fh = await fs.promises.open(filepath, 'r');
|
|
await fh.read(buffer, offset, length, position);
|
|
await fh.read({ buffer, offset, length, position });
|
|
await fh.read(buffer, { offset, length, position });
|
|
} catch (err) {
|
|
if (!allowedErrors.includes(err.code)) {
|
|
assert.fail(err);
|
|
}
|
|
} finally {
|
|
await fh?.close();
|
|
}
|
|
}
|
|
|
|
async function testInvalid(code, position) {
|
|
let fh;
|
|
try {
|
|
fh = await fs.promises.open(filepath, 'r');
|
|
await assert.rejects(
|
|
fh.read(buffer, offset, length, position),
|
|
{ code }
|
|
);
|
|
await assert.rejects(
|
|
fh.read({ buffer, offset, length, position }),
|
|
{ code }
|
|
);
|
|
await assert.rejects(
|
|
fh.read(buffer, { offset, length, position }),
|
|
{ code }
|
|
);
|
|
} finally {
|
|
await fh?.close();
|
|
}
|
|
}
|
|
|
|
{
|
|
await testValid(undefined);
|
|
await testValid(null);
|
|
await testValid(-1);
|
|
await testValid(-1n);
|
|
|
|
await testValid(0);
|
|
await testValid(0n);
|
|
await testValid(1);
|
|
await testValid(1n);
|
|
await testValid(9);
|
|
await testValid(9n);
|
|
await testValid(Number.MAX_SAFE_INTEGER, [ 'EFBIG', 'EOVERFLOW']);
|
|
|
|
await testValid(2n ** 63n - 1n - BigInt(length), [ 'EFBIG', 'EOVERFLOW']);
|
|
await testInvalid('ERR_OUT_OF_RANGE', 2n ** 63n);
|
|
await testInvalid('ERR_OUT_OF_RANGE', 2n ** 63n - BigInt(length));
|
|
|
|
await testInvalid('ERR_OUT_OF_RANGE', NaN);
|
|
await testInvalid('ERR_OUT_OF_RANGE', -Infinity);
|
|
await testInvalid('ERR_OUT_OF_RANGE', Infinity);
|
|
await testInvalid('ERR_OUT_OF_RANGE', -0.999);
|
|
await testInvalid('ERR_OUT_OF_RANGE', -(2n ** 64n));
|
|
await testInvalid('ERR_OUT_OF_RANGE', Number.MAX_SAFE_INTEGER + 1);
|
|
await testInvalid('ERR_OUT_OF_RANGE', Number.MAX_VALUE);
|
|
|
|
for (const badTypeValue of [
|
|
false, true, '1', Symbol(1), {}, [], () => {}, Promise.resolve(1),
|
|
]) {
|
|
testInvalid('ERR_INVALID_ARG_TYPE', badTypeValue);
|
|
}
|
|
}
|