mirror of
https://github.com/nodejs/node.git
synced 2024-11-24 12:10:08 +01:00
41a6d82968
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: https://github.com/nodejs/node/pull/42772 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
74 lines
1.7 KiB
JavaScript
74 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
const { mustNotMutateObjectDeep } = require('../common');
|
|
const fixtures = require('../common/fixtures');
|
|
const fs = require('fs');
|
|
const assert = require('assert');
|
|
const filepath = fixtures.path('x.txt');
|
|
|
|
const expected = Buffer.from('xyz\n');
|
|
|
|
function runTest(defaultBuffer, options, errorCode = false) {
|
|
let fd;
|
|
try {
|
|
fd = fs.openSync(filepath, 'r');
|
|
if (errorCode) {
|
|
assert.throws(
|
|
() => fs.readSync(fd, defaultBuffer, options),
|
|
{ code: errorCode }
|
|
);
|
|
} else {
|
|
const result = fs.readSync(fd, defaultBuffer, options);
|
|
assert.strictEqual(result, expected.length);
|
|
assert.deepStrictEqual(defaultBuffer, expected);
|
|
}
|
|
} finally {
|
|
if (fd != null) fs.closeSync(fd);
|
|
}
|
|
}
|
|
|
|
for (const options of [
|
|
|
|
// Test options object
|
|
{ offset: 0 },
|
|
{ length: expected.length },
|
|
{ position: 0 },
|
|
{ offset: 0, length: expected.length },
|
|
{ offset: 0, position: 0 },
|
|
{ length: expected.length, position: 0 },
|
|
{ offset: 0, length: expected.length, position: 0 },
|
|
|
|
{ position: null },
|
|
{ position: -1 },
|
|
{ position: 0n },
|
|
|
|
// Test default params
|
|
{},
|
|
null,
|
|
undefined,
|
|
|
|
// Test malicious corner case: it works as {length: 4} but not intentionally
|
|
new String('4444'),
|
|
]) {
|
|
runTest(Buffer.allocUnsafe(expected.length), options);
|
|
}
|
|
|
|
for (const options of [
|
|
|
|
// Test various invalid options
|
|
false,
|
|
true,
|
|
Infinity,
|
|
42n,
|
|
Symbol(),
|
|
'amString',
|
|
[],
|
|
() => {},
|
|
|
|
// Test if arbitrary entity with expected .length is not mistaken for options
|
|
'4'.repeat(expected.length),
|
|
[4, 4, 4, 4],
|
|
]) {
|
|
runTest(Buffer.allocUnsafe(expected.length), mustNotMutateObjectDeep(options), 'ERR_INVALID_ARG_TYPE');
|
|
}
|