forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-fs-read-position-validation.mjs
More file actions
116 lines (105 loc) Β· 3.64 KB
/
test-fs-read-position-validation.mjs
File metadata and controls
116 lines (105 loc) Β· 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import * as common from '../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 or -EOVERFLOW
async function testValid(position, allowedErrors = []) {
return new Promise((resolve, reject) => {
fs.open(filepath, 'r', common.mustSucceed((fd) => {
let callCount = 3;
const handler = common.mustCall((err) => {
callCount--;
if (err && !allowedErrors.includes(err.code)) {
fs.close(fd, common.mustSucceed());
reject(err);
} else if (callCount === 0) {
fs.close(fd, common.mustSucceed(resolve));
}
}, callCount);
fs.read(fd, buffer, offset, length, position, handler);
fs.read(fd, { buffer, offset, length, position }, handler);
fs.read(fd, buffer, common.mustNotMutateObjectDeep({ offset, length, position }), handler);
}));
});
}
async function testInvalid(code, position) {
return new Promise((resolve, reject) => {
fs.open(filepath, 'r', common.mustSucceed((fd) => {
try {
assert.throws(
() => fs.read(fd, buffer, offset, length, position, common.mustNotCall()),
{ code }
);
assert.throws(
() => fs.read(fd, { buffer, offset, length, position }, common.mustNotCall()),
{ code }
);
assert.throws(
() => fs.read(fd, buffer, common.mustNotMutateObjectDeep({ offset, length, position }), common.mustNotCall()),
{ code }
);
resolve();
} catch (err) {
reject(err);
} finally {
fs.close(fd, common.mustSucceed());
}
}));
});
}
{
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),
]) {
await testInvalid('ERR_INVALID_ARG_TYPE', badTypeValue);
}
}
{
const emptyBuffer = Buffer.alloc(0);
await new Promise((resolve, reject) => {
fs.open(filepath, 'r', common.mustSucceed((fd) => {
try {
assert.throws(
() => fs.read(fd, emptyBuffer, 0, 0, { not: 'a number' }, common.mustNotCall()),
{ code: 'ERR_INVALID_ARG_TYPE' }
);
assert.throws(
() => fs.read(fd, { buffer: emptyBuffer, offset: 0, length: 0, position: 'string' }, common.mustNotCall()),
{ code: 'ERR_INVALID_ARG_TYPE' }
);
resolve();
} catch (err) {
reject(err);
} finally {
fs.close(fd, common.mustSucceed());
}
}));
});
}