forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest-fs-promises-readfile.js
90 lines (74 loc) · 2.63 KB
/
test-fs-promises-readfile.js
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
// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const { writeFile, readFile } = require('fs').promises;
const tmpdir = require('../common/tmpdir');
const { internalBinding } = require('internal/test/binding');
const fsBinding = internalBinding('fs');
tmpdir.refresh();
const fn = tmpdir.resolve('large-file');
// Creating large buffer with random content
const largeBuffer = Buffer.from(
Array.from({ length: 1024 ** 2 + 19 }, (_, index) => index)
);
async function createLargeFile() {
// Writing buffer to a file then try to read it
await writeFile(fn, largeBuffer);
}
async function validateReadFile() {
const readBuffer = await readFile(fn);
assert.strictEqual(readBuffer.equals(largeBuffer), true);
}
async function validateReadFileProc() {
// Test to make sure reading a file under the /proc directory works. Adapted
// from test-fs-read-file-sync-hostname.js.
// Refs:
// - https://groups.google.com/forum/#!topic/nodejs-dev/rxZ_RoH1Gn0
// - https://github.com/nodejs/node/issues/21331
// Test is Linux-specific.
if (!common.isLinux)
return;
const hostname = await readFile('/proc/sys/kernel/hostname');
assert.ok(hostname.length > 0);
}
function validateReadFileAbortLogicBefore() {
const signal = AbortSignal.abort();
assert.rejects(readFile(fn, { signal }), {
name: 'AbortError'
}).then(common.mustCall());
}
function validateReadFileAbortLogicDuring() {
const controller = new AbortController();
const signal = controller.signal;
process.nextTick(() => controller.abort());
assert.rejects(readFile(fn, { signal }), {
name: 'AbortError'
}).then(common.mustCall());
}
async function validateWrongSignalParam() {
// Verify that if something different than Abortcontroller.signal
// is passed, ERR_INVALID_ARG_TYPE is thrown
await assert.rejects(async () => {
const callback = common.mustNotCall();
await readFile(fn, { signal: 'hello' }, callback);
}, { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' });
}
async function validateZeroByteLiar() {
const originalFStat = fsBinding.fstat;
fsBinding.fstat = common.mustCall(
() => (/* stat fields */ [0, 1, 2, 3, 4, 5, 6, 7, 0 /* size */])
);
const readBuffer = await readFile(fn);
assert.strictEqual(readBuffer.toString(), largeBuffer.toString());
fsBinding.fstat = originalFStat;
}
(async () => {
await createLargeFile();
await validateReadFile();
await validateReadFileProc();
await validateReadFileAbortLogicBefore();
await validateReadFileAbortLogicDuring();
await validateWrongSignalParam();
await validateZeroByteLiar();
})().then(common.mustCall());