@zenfs/devel 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +157 -0
- package/README.md +46 -2
- package/dist/bin/make-index.d.ts +2 -0
- package/dist/bin/make-index.js +110 -0
- package/dist/bin/zci.d.ts +2 -0
- package/dist/bin/zci.js +51 -0
- package/dist/bin/zenfs-test.d.ts +2 -0
- package/dist/bin/zenfs-test.js +242 -0
- package/dist/ci.d.ts +15 -0
- package/dist/ci.js +74 -0
- package/dist/tests/common.d.ts +28 -0
- package/dist/tests/common.js +37 -0
- package/dist/tests/logs.d.ts +1 -0
- package/dist/tests/logs.js +21 -0
- package/dist/tests/setup.d.ts +9 -0
- package/dist/tests/setup.js +40 -0
- package/eslint.shared.js +60 -0
- package/package.json +91 -5
- package/tests/data/49chars.txt +1 -0
- package/tests/data/a.js +46 -0
- package/tests/data/a1.js +46 -0
- package/tests/data/elipses.txt +1 -0
- package/tests/data/empty.txt +0 -0
- package/tests/data/exit.js +22 -0
- package/tests/data/image.jpg +0 -0
- package/tests/data/utf8.txt +1 -0
- package/tests/data/x.txt +1 -0
- package/tests/data-license.md +19 -0
- package/tests/fs/append.test.ts +34 -0
- package/tests/fs/concurrency.test.ts +113 -0
- package/tests/fs/dir.test.ts +103 -0
- package/tests/fs/directory.test.ts +189 -0
- package/tests/fs/errors.test.ts +159 -0
- package/tests/fs/exists.test.ts +23 -0
- package/tests/fs/links.test.ts +308 -0
- package/tests/fs/open.test.ts +23 -0
- package/tests/fs/permissions.test.ts +344 -0
- package/tests/fs/read.test.ts +94 -0
- package/tests/fs/readFile.test.ts +58 -0
- package/tests/fs/rename.test.ts +93 -0
- package/tests/fs/scaling.test.ts +35 -0
- package/tests/fs/stat.test.ts +115 -0
- package/tests/fs/streams.test.ts +204 -0
- package/tests/fs/tempdir.test.ts +64 -0
- package/tests/fs/times.test.ts +85 -0
- package/tests/fs/truncate.test.ts +89 -0
- package/tests/fs/watch.test.ts +246 -0
- package/tests/fs/write.test.ts +118 -0
- package/tests/fs/xattr.test.ts +96 -0
- package/tests/readme.md +5 -0
- package/tests/setup/memory.ts +3 -0
- package/tests/tsconfig.json +8 -0
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
2
|
+
import { bindContext, fs as rootFS } from '@zenfs/core';
|
|
3
|
+
import { R_OK, W_OK, X_OK } from '@zenfs/core/constants';
|
|
4
|
+
import { defaultContext } from '@zenfs/core/internal/contexts';
|
|
5
|
+
import { hasAccess } from '@zenfs/core/internal/inode';
|
|
6
|
+
import { join } from '@zenfs/core/path';
|
|
7
|
+
import * as vfsConfig from '@zenfs/core/vfs/config';
|
|
8
|
+
import type { Exception } from 'kerium';
|
|
9
|
+
import assert from 'node:assert/strict';
|
|
10
|
+
import { suite, test } from 'node:test';
|
|
11
|
+
import { encodeUTF8 } from 'utilium';
|
|
12
|
+
import { config, fs } from '@zenfs/devel/tests/common';
|
|
13
|
+
|
|
14
|
+
const asyncMode = 0o777;
|
|
15
|
+
const syncMode = 0o644;
|
|
16
|
+
const file = 'a.js';
|
|
17
|
+
|
|
18
|
+
suite('Permissions', config('permissions'), () => {
|
|
19
|
+
test('chmod', config('sync', 'async'), async () => {
|
|
20
|
+
await fs.promises.chmod(file, asyncMode.toString(8));
|
|
21
|
+
|
|
22
|
+
const stats = await fs.promises.stat(file);
|
|
23
|
+
assert.equal(stats.mode & 0o777, asyncMode);
|
|
24
|
+
|
|
25
|
+
fs.chmodSync(file, syncMode);
|
|
26
|
+
assert.equal(fs.statSync(file).mode & 0o777, syncMode);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('fchmod', config('sync', 'async'), async () => {
|
|
30
|
+
await using handle = await fs.promises.open(file, 'a', 0o644);
|
|
31
|
+
|
|
32
|
+
await handle.chmod(asyncMode);
|
|
33
|
+
const stats = await handle.stat();
|
|
34
|
+
|
|
35
|
+
assert.equal(stats.mode & 0o777, asyncMode);
|
|
36
|
+
|
|
37
|
+
fs.fchmodSync(handle.fd, syncMode);
|
|
38
|
+
assert.equal(fs.statSync(file).mode & 0o777, syncMode);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('lchmod', config('lchmod', 'async'), async () => {
|
|
42
|
+
const link = 'symbolic-link';
|
|
43
|
+
|
|
44
|
+
await fs.promises.symlink(file, link);
|
|
45
|
+
await fs.promises.lchmod(link, asyncMode);
|
|
46
|
+
|
|
47
|
+
const stats = await fs.promises.lstat(link);
|
|
48
|
+
assert.equal(stats.mode & 0o777, asyncMode);
|
|
49
|
+
|
|
50
|
+
await fs.promises.lchmod(link, syncMode);
|
|
51
|
+
assert.equal((await fs.promises.lstat(link)).mode & 0o777, syncMode);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
async function test_item(path: string): Promise<void> {
|
|
55
|
+
const stats = await fs.promises.stat(path).catch((error: Exception) => {
|
|
56
|
+
assert.equal(error.code, 'EACCES');
|
|
57
|
+
});
|
|
58
|
+
if (!stats) return;
|
|
59
|
+
if (stats.isDirectory()) assert(hasAccess(defaultContext, stats, X_OK));
|
|
60
|
+
|
|
61
|
+
function checkError(access: number) {
|
|
62
|
+
return function (error: Exception) {
|
|
63
|
+
assert(Error.isError(error));
|
|
64
|
+
assert(!hasAccess(defaultContext, stats!, access));
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (stats.isDirectory()) {
|
|
69
|
+
for (const dir of await fs.promises.readdir(path)) {
|
|
70
|
+
await test('Access controls: ' + join(path, dir), () => test_item(join(path, dir)));
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
73
|
+
await fs.promises.readFile(path).catch(checkError(R_OK));
|
|
74
|
+
}
|
|
75
|
+
assert(hasAccess(defaultContext, stats, R_OK));
|
|
76
|
+
|
|
77
|
+
if (stats.isDirectory()) {
|
|
78
|
+
const testFile = join(path, '__test_file_plz_ignore.txt');
|
|
79
|
+
await fs.promises.writeFile(testFile, encodeUTF8('this is a test file, please ignore.')).catch(checkError(W_OK));
|
|
80
|
+
await fs.promises.unlink(testFile).catch(checkError(W_OK));
|
|
81
|
+
} else {
|
|
82
|
+
const handle = await fs.promises.open(path, 'a').catch(checkError(W_OK));
|
|
83
|
+
if (!handle) return;
|
|
84
|
+
await handle.close();
|
|
85
|
+
}
|
|
86
|
+
assert(hasAccess(defaultContext, stats, W_OK));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
test('unprivileged users can read files they do not own #326', async () => {
|
|
90
|
+
await rootFS.promises.writeFile('/root-owned.txt', 'shared', { mode: 0o644 });
|
|
91
|
+
const user = bindContext({ credentials: { uid: 1000, gid: 1000 } });
|
|
92
|
+
|
|
93
|
+
assert.equal(user.fs.readFileSync('/root-owned.txt', 'utf8'), 'shared');
|
|
94
|
+
assert.equal(await user.fs.promises.readFile('/root-owned.txt', 'utf8'), 'shared');
|
|
95
|
+
|
|
96
|
+
assert.throws(() => user.fs.writeFileSync('/root-owned.txt', 'nope'), { code: 'EACCES' });
|
|
97
|
+
await assert.rejects(user.fs.promises.writeFile('/root-owned.txt', 'nope'), { code: 'EACCES' });
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('access checks follow Linux #326', () => {
|
|
101
|
+
const as = (uid: number, gid: number, euid = uid, egid = gid, groups: number[] = []) =>
|
|
102
|
+
bindContext({ credentials: { uid, gid, euid, egid, groups } });
|
|
103
|
+
const file = (mode: number, uid = 1000, gid = 1000) => ({ mode: 0o100000 | mode, uid, gid });
|
|
104
|
+
|
|
105
|
+
assert(!hasAccess(as(1000, 0), file(0o600, 0, 0), R_OK));
|
|
106
|
+
assert(hasAccess(as(0, 1000), file(0o000, 5, 5), R_OK | W_OK));
|
|
107
|
+
|
|
108
|
+
assert(hasAccess(as(1000, 1000, 2000, 1000), file(0o600, 2000, 5), R_OK | W_OK));
|
|
109
|
+
assert(!hasAccess(as(2000, 1000, 1000, 1000), file(0o600, 2000, 5), R_OK));
|
|
110
|
+
assert(!hasAccess(as(1000, 0, 1000, 1000), file(0o060, 2000, 0), R_OK));
|
|
111
|
+
|
|
112
|
+
assert(!hasAccess(as(1000, 1000), file(0o077), R_OK));
|
|
113
|
+
assert(!hasAccess(as(1000, 1000), file(0o407, 2000), R_OK));
|
|
114
|
+
assert(hasAccess(as(1000, 3000), file(0o004, 2000, 2000), R_OK));
|
|
115
|
+
assert(hasAccess(as(1000, 3000, 1000, 3000, [2000]), file(0o040, 2000, 2000), R_OK));
|
|
116
|
+
assert(hasAccess(as(1000, 2000), file(0o021, 0, 2000), W_OK));
|
|
117
|
+
assert(!hasAccess(as(1000, 2000), file(0o021, 0, 2000), X_OK));
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test('stat needs no permission on the file itself #326', async () => {
|
|
121
|
+
await rootFS.promises.writeFile('/secret-stat.txt', 'x', { mode: 0o000 });
|
|
122
|
+
const user = bindContext({ credentials: { uid: 1000, gid: 1000 } });
|
|
123
|
+
|
|
124
|
+
assert.equal(user.fs.statSync('/secret-stat.txt').size, 1);
|
|
125
|
+
assert.equal((await user.fs.promises.lstat('/secret-stat.txt')).size, 1);
|
|
126
|
+
assert.throws(() => user.fs.readFileSync('/secret-stat.txt'), { code: 'EACCES' });
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('removing an entry needs write and search permission on its parent #326', async () => {
|
|
130
|
+
const alice = bindContext({ credentials: { uid: 1000, gid: 1000 } });
|
|
131
|
+
|
|
132
|
+
await rootFS.promises.mkdir('/removal-root', { mode: 0o755 });
|
|
133
|
+
await rootFS.promises.writeFile('/removal-root/file', 'x', { mode: 0o666 });
|
|
134
|
+
await rootFS.promises.mkdir('/removal-root/dir', { mode: 0o777 });
|
|
135
|
+
await rootFS.promises.mkdir('/removal-own', { mode: 0o755 });
|
|
136
|
+
await rootFS.promises.chown('/removal-own', 1000, 1000);
|
|
137
|
+
await rootFS.promises.writeFile('/removal-own/readonly', 'x', { mode: 0o444 });
|
|
138
|
+
await rootFS.promises.mkdir('/removal-own/readonly-dir', { mode: 0o555 });
|
|
139
|
+
await rootFS.promises.mkdir('/removal-unreadable', { mode: 0o300 });
|
|
140
|
+
await rootFS.promises.chown('/removal-unreadable', 1000, 1000);
|
|
141
|
+
await rootFS.promises.writeFile('/removal-unreadable/file', 'x');
|
|
142
|
+
|
|
143
|
+
await assert.rejects(alice.fs.promises.unlink('/removal-root/file'), { code: 'EACCES' });
|
|
144
|
+
assert.throws(() => alice.fs.unlinkSync('/removal-root/file'), { code: 'EACCES' });
|
|
145
|
+
await assert.rejects(alice.fs.promises.rmdir('/removal-root/dir'), { code: 'EACCES' });
|
|
146
|
+
assert.throws(() => alice.fs.rmdirSync('/removal-root/dir'), { code: 'EACCES' });
|
|
147
|
+
assert.throws(() => alice.fs.renameSync('/removal-root/file', '/removal-own/file'), { code: 'EACCES' });
|
|
148
|
+
await assert.rejects(alice.fs.promises.rename('/removal-own/readonly', '/removal-root/file'), { code: 'EACCES' });
|
|
149
|
+
|
|
150
|
+
await alice.fs.promises.unlink('/removal-own/readonly');
|
|
151
|
+
alice.fs.rmdirSync('/removal-own/readonly-dir');
|
|
152
|
+
assert.deepEqual(rootFS.readdirSync('/removal-own'), []);
|
|
153
|
+
|
|
154
|
+
await alice.fs.promises.rename('/removal-unreadable/file', '/removal-unreadable/renamed');
|
|
155
|
+
alice.fs.renameSync('/removal-unreadable/renamed', '/removal-unreadable/file');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test('sticky directories only let owners remove entries #326', async () => {
|
|
159
|
+
const alice = bindContext({ credentials: { uid: 1000, gid: 1000 } });
|
|
160
|
+
const bob = bindContext({ credentials: { uid: 2000, gid: 2000 } });
|
|
161
|
+
|
|
162
|
+
await rootFS.promises.mkdir('/sticky', { mode: 0o1777 });
|
|
163
|
+
await alice.fs.promises.writeFile('/sticky/alice', 'a', { mode: 0o666 });
|
|
164
|
+
await alice.fs.promises.mkdir('/sticky/alice-dir', { mode: 0o777 });
|
|
165
|
+
await bob.fs.promises.writeFile('/sticky/bob', 'b');
|
|
166
|
+
|
|
167
|
+
await assert.rejects(bob.fs.promises.unlink('/sticky/alice'), { code: 'EPERM' });
|
|
168
|
+
assert.throws(() => bob.fs.unlinkSync('/sticky/alice'), { code: 'EPERM' });
|
|
169
|
+
await assert.rejects(bob.fs.promises.rmdir('/sticky/alice-dir'), { code: 'EPERM' });
|
|
170
|
+
assert.throws(() => bob.fs.rmdirSync('/sticky/alice-dir'), { code: 'EPERM' });
|
|
171
|
+
assert.throws(() => bob.fs.renameSync('/sticky/alice', '/sticky/stolen'), { code: 'EPERM' });
|
|
172
|
+
await assert.rejects(alice.fs.promises.rename('/sticky/alice', '/sticky/bob'), { code: 'EPERM' });
|
|
173
|
+
|
|
174
|
+
await alice.fs.promises.unlink('/sticky/alice');
|
|
175
|
+
alice.fs.rmdirSync('/sticky/alice-dir');
|
|
176
|
+
await rootFS.promises.unlink('/sticky/bob');
|
|
177
|
+
assert.deepEqual(rootFS.readdirSync('/sticky'), []);
|
|
178
|
+
|
|
179
|
+
await rootFS.promises.mkdir('/sticky-alice', { mode: 0o1777 });
|
|
180
|
+
await rootFS.promises.chown('/sticky-alice', 1000, 1000);
|
|
181
|
+
await bob.fs.promises.writeFile('/sticky-alice/bob', 'b');
|
|
182
|
+
await alice.fs.promises.unlink('/sticky-alice/bob');
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test('only owners may change the mode, owner, or times of a file #326', async () => {
|
|
186
|
+
const alice = bindContext({ credentials: { uid: 1000, gid: 1000, groups: [3000] } });
|
|
187
|
+
const bob = bindContext({ credentials: { uid: 2000, gid: 2000 } });
|
|
188
|
+
|
|
189
|
+
await rootFS.promises.writeFile('/owned', 'x', { mode: 0o666 });
|
|
190
|
+
rootFS.chownSync('/owned', 1000, 1000);
|
|
191
|
+
|
|
192
|
+
assert.throws(() => bob.fs.chmodSync('/owned', 0o600), { code: 'EPERM' });
|
|
193
|
+
await assert.rejects(bob.fs.promises.chmod('/owned', 0o600), { code: 'EPERM' });
|
|
194
|
+
assert.throws(() => bob.fs.utimesSync('/owned', 1, 2), { code: 'EPERM' });
|
|
195
|
+
await assert.rejects(bob.fs.promises.utimes('/owned', 1, 2), { code: 'EPERM' });
|
|
196
|
+
assert.throws(() => bob.fs.chownSync('/owned', -1, 2000), { code: 'EPERM' });
|
|
197
|
+
assert.throws(() => bob.fs.chownSync('/owned', 1000, -1), { code: 'EPERM' });
|
|
198
|
+
bob.fs.chownSync('/owned', -1, -1);
|
|
199
|
+
|
|
200
|
+
alice.fs.chmodSync('/owned', 0o644);
|
|
201
|
+
await alice.fs.promises.utimes('/owned', 1, 2);
|
|
202
|
+
alice.fs.chownSync('/owned', -1, 3000);
|
|
203
|
+
await alice.fs.promises.chown('/owned', 1000, 1000);
|
|
204
|
+
assert.throws(() => alice.fs.chownSync('/owned', -1, 4000), { code: 'EPERM' });
|
|
205
|
+
assert.throws(() => alice.fs.chownSync('/owned', 2000, -1), { code: 'EPERM' });
|
|
206
|
+
|
|
207
|
+
rootFS.chownSync('/owned', 2000, 2000);
|
|
208
|
+
const stats = rootFS.statSync('/owned');
|
|
209
|
+
assert.equal(stats.mode & 0o777, 0o644);
|
|
210
|
+
assert.equal(stats.mtimeMs, 2000);
|
|
211
|
+
assert.equal(stats.uid, 2000);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test('changing metadata needs no read or write access, even on directories #327', async () => {
|
|
215
|
+
const alice = bindContext({ credentials: { uid: 1000, gid: 1000 } });
|
|
216
|
+
|
|
217
|
+
await rootFS.promises.writeFile('/metadata-file', 'x', { mode: 0o000 });
|
|
218
|
+
await rootFS.promises.mkdir('/metadata-dir', { mode: 0o000 });
|
|
219
|
+
|
|
220
|
+
for (const path of ['/metadata-file', '/metadata-dir']) {
|
|
221
|
+
rootFS.chownSync(path, 1000, 1000);
|
|
222
|
+
alice.fs.chmodSync(path, 0o500);
|
|
223
|
+
await alice.fs.promises.chmod(path, 0o400);
|
|
224
|
+
alice.fs.utimesSync(path, 1, 2);
|
|
225
|
+
await alice.fs.promises.utimes(path, 3, 4);
|
|
226
|
+
alice.fs.chownSync(path, -1, -1);
|
|
227
|
+
await alice.fs.promises.chown(path, -1, -1);
|
|
228
|
+
|
|
229
|
+
const stats = rootFS.statSync(path);
|
|
230
|
+
assert.equal(stats.mode & 0o777, 0o400);
|
|
231
|
+
assert.equal(stats.mtimeMs, 4000);
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test('moving a directory to another parent needs write permission on it', async () => {
|
|
236
|
+
const alice = bindContext({ credentials: { uid: 1000, gid: 1000 } });
|
|
237
|
+
|
|
238
|
+
await rootFS.promises.mkdir('/move-a', { mode: 0o777 });
|
|
239
|
+
await rootFS.promises.mkdir('/move-b', { mode: 0o777 });
|
|
240
|
+
await rootFS.promises.mkdir('/move-a/dir', { mode: 0o755 });
|
|
241
|
+
await rootFS.promises.writeFile('/move-a/file', 'x', { mode: 0o644 });
|
|
242
|
+
|
|
243
|
+
assert.throws(() => alice.fs.renameSync('/move-a/dir', '/move-b/dir'), { code: 'EACCES' });
|
|
244
|
+
await assert.rejects(alice.fs.promises.rename('/move-a/dir', '/move-b/dir'), { code: 'EACCES' });
|
|
245
|
+
await alice.fs.promises.rename('/move-a/dir', '/move-a/renamed');
|
|
246
|
+
await alice.fs.promises.rename('/move-a/file', '/move-b/file');
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test('chmod and chown clear setuid and setgid like Linux', async () => {
|
|
250
|
+
const alice = bindContext({ credentials: { uid: 1000, gid: 1000, groups: [3000] } });
|
|
251
|
+
const modeOf = (path: string) => rootFS.statSync(path).mode & 0o7777;
|
|
252
|
+
|
|
253
|
+
await rootFS.promises.mkdir('/sid', { mode: 0o777 });
|
|
254
|
+
await alice.fs.promises.writeFile('/sid/file', 'x');
|
|
255
|
+
await alice.fs.promises.mkdir('/sid/dir');
|
|
256
|
+
|
|
257
|
+
alice.fs.chmodSync('/sid/file', 0o6755);
|
|
258
|
+
assert.equal(modeOf('/sid/file'), 0o6755);
|
|
259
|
+
alice.fs.chownSync('/sid/file', -1, -1);
|
|
260
|
+
assert.equal(modeOf('/sid/file'), 0o755);
|
|
261
|
+
|
|
262
|
+
alice.fs.chmodSync('/sid/file', 0o2745);
|
|
263
|
+
alice.fs.chownSync('/sid/file', -1, 3000);
|
|
264
|
+
assert.equal(modeOf('/sid/file'), 0o2745);
|
|
265
|
+
|
|
266
|
+
rootFS.chmodSync('/sid/file', 0o4755);
|
|
267
|
+
rootFS.chownSync('/sid/file', 1000, 0);
|
|
268
|
+
assert.equal(modeOf('/sid/file'), 0o755);
|
|
269
|
+
|
|
270
|
+
alice.fs.chmodSync('/sid/file', 0o2755);
|
|
271
|
+
assert.equal(modeOf('/sid/file'), 0o755);
|
|
272
|
+
|
|
273
|
+
await alice.fs.promises.chmod('/sid/dir', 0o6755);
|
|
274
|
+
await alice.fs.promises.chown('/sid/dir', -1, -1);
|
|
275
|
+
assert.equal(modeOf('/sid/dir'), 0o6755);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
test('access checks see unsynced changes to an open file', async () => {
|
|
279
|
+
const alice = bindContext({ credentials: { uid: 1000, gid: 1000 } });
|
|
280
|
+
|
|
281
|
+
await rootFS.promises.writeFile('/unsynced', 'x', { mode: 0o644 });
|
|
282
|
+
const fd = rootFS.openSync('/unsynced', 'r');
|
|
283
|
+
rootFS.fchmodSync(fd, 0o600);
|
|
284
|
+
|
|
285
|
+
assert.throws(() => alice.fs.readFileSync('/unsynced'), { code: 'EACCES' });
|
|
286
|
+
await assert.rejects(alice.fs.promises.readFile('/unsynced'), { code: 'EACCES' });
|
|
287
|
+
rootFS.closeSync(fd);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
test('reaching a path needs search permission on every directory leading to it #326', async () => {
|
|
291
|
+
const alice = bindContext({ credentials: { uid: 1000, gid: 1000 } });
|
|
292
|
+
const bob = bindContext({ credentials: { uid: 2000, gid: 2000 } });
|
|
293
|
+
|
|
294
|
+
await rootFS.promises.mkdir('/private', { mode: 0o700 });
|
|
295
|
+
await rootFS.promises.chown('/private', 1000, 1000);
|
|
296
|
+
await rootFS.promises.mkdir('/private/inner', { mode: 0o755 });
|
|
297
|
+
await rootFS.promises.writeFile('/private/inner/readable.txt', 'secret', { mode: 0o644 });
|
|
298
|
+
await rootFS.promises.symlink('/private', '/private-link');
|
|
299
|
+
await rootFS.promises.mkdir('/search-only', { mode: 0o711 });
|
|
300
|
+
await rootFS.promises.writeFile('/search-only/known.txt', 'known', { mode: 0o644 });
|
|
301
|
+
await rootFS.promises.mkdir('/no-search', { mode: 0o722 });
|
|
302
|
+
|
|
303
|
+
assert.throws(() => bob.fs.readFileSync('/private-link/inner/readable.txt'), { code: 'EACCES' });
|
|
304
|
+
await assert.rejects(bob.fs.promises.readFile('/private-link/inner/readable.txt'), { code: 'EACCES' });
|
|
305
|
+
assert.throws(() => bob.fs.lstatSync('/private/inner'), { code: 'EACCES' });
|
|
306
|
+
assert.throws(() => bob.fs.mkdirSync('/no-search/dir'), { code: 'EACCES' });
|
|
307
|
+
assert.throws(() => bob.fs.writeFileSync('/no-search/file', 'x'), { code: 'EACCES' });
|
|
308
|
+
|
|
309
|
+
const { checkAccess, resolveFullWalk } = vfsConfig;
|
|
310
|
+
vfsConfig._setVFSConfig({ checkAccess, resolveFullWalk: true });
|
|
311
|
+
try {
|
|
312
|
+
assert.equal(alice.fs.readFileSync('/private/inner/readable.txt', 'utf8'), 'secret');
|
|
313
|
+
assert.equal(rootFS.readFileSync('/private/inner/readable.txt', 'utf8'), 'secret');
|
|
314
|
+
|
|
315
|
+
assert.throws(() => bob.fs.readFileSync('/private/inner/readable.txt'), { code: 'EACCES' });
|
|
316
|
+
await assert.rejects(bob.fs.promises.readFile('/private/inner/readable.txt'), { code: 'EACCES' });
|
|
317
|
+
assert.throws(() => bob.fs.statSync('/private/inner/readable.txt'), { code: 'EACCES' });
|
|
318
|
+
await assert.rejects(bob.fs.promises.stat('/private/inner/readable.txt'), { code: 'EACCES' });
|
|
319
|
+
assert.throws(() => bob.fs.readdirSync('/private/inner'), { code: 'EACCES' });
|
|
320
|
+
assert.throws(() => bob.fs.writeFileSync('/private/inner/new.txt', 'x'), { code: 'EACCES' });
|
|
321
|
+
assert.throws(() => bob.fs.unlinkSync('/private/inner/readable.txt'), { code: 'EACCES' });
|
|
322
|
+
assert.throws(() => bob.fs.mkdirSync('/private/inner/dir'), { code: 'EACCES' });
|
|
323
|
+
assert.throws(() => bob.fs.renameSync('/private/inner/readable.txt', '/stolen.txt'), { code: 'EACCES' });
|
|
324
|
+
assert(rootFS.existsSync('/private/inner/readable.txt'));
|
|
325
|
+
|
|
326
|
+
assert.equal(bob.fs.statSync('/private').mode & 0o777, 0o700);
|
|
327
|
+
assert.throws(() => bob.fs.readdirSync('/private'), { code: 'EACCES' });
|
|
328
|
+
|
|
329
|
+
assert.equal(bob.fs.readFileSync('/search-only/known.txt', 'utf8'), 'known');
|
|
330
|
+
assert.throws(() => bob.fs.readdirSync('/search-only'), { code: 'EACCES' });
|
|
331
|
+
|
|
332
|
+
assert.equal(bob.fs.existsSync('/private/inner/readable.txt'), false);
|
|
333
|
+
assert.equal(await bob.fs.promises.exists('/private/inner/readable.txt'), false);
|
|
334
|
+
assert.equal(bob.fs.existsSync('/search-only/known.txt'), true);
|
|
335
|
+
} finally {
|
|
336
|
+
vfsConfig._setVFSConfig({ checkAccess, resolveFullWalk });
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
const copy = { ...defaultContext.credentials };
|
|
341
|
+
Object.assign(defaultContext.credentials, { uid: 1000, gid: 1000, euid: 1000, egid: 1000 });
|
|
342
|
+
test('Access controls: /', () => test_item('/'));
|
|
343
|
+
Object.assign(defaultContext.credentials, copy);
|
|
344
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
2
|
+
import { sync } from '@zenfs/core';
|
|
3
|
+
import { Buffer } from 'buffer';
|
|
4
|
+
import assert from 'node:assert/strict';
|
|
5
|
+
import type { OpenMode, PathLike } from 'node:fs';
|
|
6
|
+
import { suite, test } from 'node:test';
|
|
7
|
+
import { promisify } from 'node:util';
|
|
8
|
+
import { config, fs } from '@zenfs/devel/tests/common';
|
|
9
|
+
|
|
10
|
+
const filepath = 'x.txt';
|
|
11
|
+
const expected = 'xyz\n';
|
|
12
|
+
const ellipses = '…'.repeat(10_000);
|
|
13
|
+
|
|
14
|
+
suite('read', () => {
|
|
15
|
+
test('read file asynchronously', config('async'), async () => {
|
|
16
|
+
await using handle = await fs.promises.open(filepath, 'r');
|
|
17
|
+
const { bytesRead, buffer } = await handle.read(Buffer.alloc(expected.length), 0, expected.length, 0);
|
|
18
|
+
assert.equal(bytesRead, expected.length);
|
|
19
|
+
assert.equal(buffer.toString(), expected);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('read file synchronously', config('sync'), () => {
|
|
23
|
+
const fd = fs.openSync(filepath, 'r');
|
|
24
|
+
const buffer = Buffer.alloc(expected.length);
|
|
25
|
+
const bytesRead = fs.readSync(fd, buffer, 0, expected.length, 0);
|
|
26
|
+
|
|
27
|
+
assert.equal(bytesRead, expected.length);
|
|
28
|
+
assert.equal(buffer.toString(), expected);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('Read a file and check its binary bytes asynchronously', config('async'), async () => {
|
|
32
|
+
const buff = await fs.promises.readFile('elipses.txt');
|
|
33
|
+
assert.equal(buff.length, 30_000);
|
|
34
|
+
assert.equal(buff.toString(), ellipses);
|
|
35
|
+
assert.equal((buff[1] << 8) | buff[0], 32994);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('Read a file and check its binary bytes synchronously', config('sync'), () => {
|
|
39
|
+
const buff = fs.readFileSync('elipses.txt');
|
|
40
|
+
assert.equal(buff.length, 30_000);
|
|
41
|
+
assert.equal(buff.toString(), ellipses);
|
|
42
|
+
assert.equal((buff[1] << 8) | buff[0], 32994);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const bufferAsync = Buffer.alloc(expected.length);
|
|
46
|
+
const bufferSync = Buffer.alloc(expected.length);
|
|
47
|
+
|
|
48
|
+
test('read file from handle asynchronously', config('async'), async () => {
|
|
49
|
+
await using handle = await fs.promises.open(filepath, 'r');
|
|
50
|
+
const { bytesRead } = await handle.read(bufferAsync, 0, expected.length, 0);
|
|
51
|
+
|
|
52
|
+
assert.equal(bytesRead, expected.length);
|
|
53
|
+
assert.equal(bufferAsync.toString(), expected);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('read file from handle synchronously', config('sync'), () => {
|
|
57
|
+
const fd = fs.openSync(filepath, 'r');
|
|
58
|
+
const bytesRead = fs.readSync(fd, bufferSync, 0, expected.length, 0);
|
|
59
|
+
|
|
60
|
+
assert.equal(bufferSync.toString(), expected);
|
|
61
|
+
assert.equal(bytesRead, expected.length);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('read file synchronously to non-zero offset', config('sync'), () => {
|
|
65
|
+
const fd = fs.openSync(filepath, 'r');
|
|
66
|
+
const buffer = Buffer.alloc(expected.length + 10);
|
|
67
|
+
const bytesRead = fs.readSync(fd, buffer, 10, expected.length, 0);
|
|
68
|
+
|
|
69
|
+
assert.equal(buffer.subarray(10, buffer.length).toString(), expected);
|
|
70
|
+
assert.equal(bytesRead, expected.length);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('read using callback API #239', config('sync', 'write'), async () => {
|
|
74
|
+
const path = '/text.txt';
|
|
75
|
+
|
|
76
|
+
fs.writeFileSync(path, 'hello world');
|
|
77
|
+
await sync();
|
|
78
|
+
|
|
79
|
+
const fd: number = (await promisify<PathLike, OpenMode, number | string>(fs.open)(path, 0, 0)) as any;
|
|
80
|
+
|
|
81
|
+
const read = promisify(fs.read);
|
|
82
|
+
|
|
83
|
+
const buf = Buffer.alloc(1024);
|
|
84
|
+
const n0 = await read(fd, buf, 0, 1024, undefined);
|
|
85
|
+
assert.equal(n0, 11);
|
|
86
|
+
assert.equal(buf.subarray(0, n0).toString('utf8'), 'hello world');
|
|
87
|
+
|
|
88
|
+
const n1 = await read(fd, buf, 0, 1024, undefined);
|
|
89
|
+
assert.equal(n1, 0);
|
|
90
|
+
assert.equal(buf.subarray(0, n1).toString('utf8'), '');
|
|
91
|
+
|
|
92
|
+
await promisify(fs.close)(fd);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { suite, test } from 'node:test';
|
|
4
|
+
import { config, fs } from '@zenfs/devel/tests/common';
|
|
5
|
+
|
|
6
|
+
suite('Reading', () => {
|
|
7
|
+
test('Cannot read a file with an invalid encoding', config('sync'), () => {
|
|
8
|
+
assert.throws(() => fs.readFileSync('a.js', 'wrong-encoding' as BufferEncoding));
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test('Reading past the end of a file should not be an error', config('async'), async () => {
|
|
12
|
+
await using handle = await fs.promises.open('a.js', 'r');
|
|
13
|
+
const { bytesRead } = await handle.read(new Uint8Array(10), 0, 10, 10000);
|
|
14
|
+
assert.equal(bytesRead, 0);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const dir = 'test-readfile-unlink';
|
|
18
|
+
const file = 'test-readfile-unlink/test.bin';
|
|
19
|
+
const data = new Uint8Array(512).fill(42);
|
|
20
|
+
|
|
21
|
+
test('create directory and write file', config('async', 'write', 'directories'), async () => {
|
|
22
|
+
await fs.promises.mkdir(dir);
|
|
23
|
+
await fs.promises.writeFile(file, data);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test('read file and verify its content', config('async'), async () => {
|
|
27
|
+
const read: Uint8Array = await fs.promises.readFile(file);
|
|
28
|
+
assert.equal(read.length, data.length);
|
|
29
|
+
assert.equal(read[0], 42);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('unlink file and remove directory', config('async', 'write', 'directories'), async () => {
|
|
33
|
+
await fs.promises.unlink(file);
|
|
34
|
+
await fs.promises.rmdir(dir);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const fileName = 'empty.txt';
|
|
38
|
+
|
|
39
|
+
test('read file', config('sync', 'async'), async () => {
|
|
40
|
+
assert.equal((await fs.promises.readFile(fileName)).toString(), '');
|
|
41
|
+
assert.equal(fs.readFileSync(fileName).toString(), '');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('read file with utf-8 encoding', config('sync', 'async'), async () => {
|
|
45
|
+
assert.equal(await fs.promises.readFile(fileName, 'utf8'), '');
|
|
46
|
+
assert.equal(fs.readFileSync(fileName, 'utf8'), '');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('read file synchronously and verify the content', config('sync'), () => {
|
|
50
|
+
const content = fs.readFileSync('elipses.txt', 'utf8');
|
|
51
|
+
|
|
52
|
+
assert.equal(content.length, 10000);
|
|
53
|
+
|
|
54
|
+
for (let i = 0; i < content.length; i++) {
|
|
55
|
+
assert.equal(content[i], '…');
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { suite, test } from 'node:test';
|
|
4
|
+
import { config, fs } from '@zenfs/devel/tests/common';
|
|
5
|
+
|
|
6
|
+
suite('Rename', config('rename'), () => {
|
|
7
|
+
/**
|
|
8
|
+
* Creates the following directory structure within `dir`:
|
|
9
|
+
* - _rename_me
|
|
10
|
+
* - lol.txt
|
|
11
|
+
* - file.dat
|
|
12
|
+
*/
|
|
13
|
+
async function populate(dir: string) {
|
|
14
|
+
await fs.promises.mkdir(dir + '/_rename_me');
|
|
15
|
+
await fs.promises.writeFile(dir + '/file.dat', 'filedata');
|
|
16
|
+
await fs.promises.writeFile(dir + '/_rename_me/lol.txt', 'lololol');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Check that the directory structure created in populate_directory remains.
|
|
21
|
+
*/
|
|
22
|
+
async function check_directory(dir: string) {
|
|
23
|
+
const contents = await fs.promises.readdir(dir);
|
|
24
|
+
assert.equal(contents.length, 2);
|
|
25
|
+
|
|
26
|
+
const subContents = await fs.promises.readdir(dir + '/_rename_me');
|
|
27
|
+
assert.equal(subContents.length, 1);
|
|
28
|
+
|
|
29
|
+
assert(await fs.promises.stat(dir + '/file.dat', { throwIfNoEntry: false }));
|
|
30
|
+
assert(await fs.promises.stat(dir + '/_rename_me/lol.txt', { throwIfNoEntry: false }));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
test('rename directory', config('async'), async () => {
|
|
34
|
+
const oldDir = '/rename_test';
|
|
35
|
+
const newDir = '/rename_test2';
|
|
36
|
+
|
|
37
|
+
await fs.promises.mkdir(oldDir);
|
|
38
|
+
|
|
39
|
+
await populate(oldDir);
|
|
40
|
+
|
|
41
|
+
await fs.promises.rename(oldDir, oldDir);
|
|
42
|
+
|
|
43
|
+
await check_directory(oldDir);
|
|
44
|
+
|
|
45
|
+
await fs.promises.rename(oldDir, newDir);
|
|
46
|
+
|
|
47
|
+
await check_directory(newDir);
|
|
48
|
+
|
|
49
|
+
assert(!(await fs.promises.stat(oldDir, { throwIfNoEntry: false })));
|
|
50
|
+
|
|
51
|
+
await fs.promises.mkdir(oldDir);
|
|
52
|
+
await populate(oldDir);
|
|
53
|
+
await fs.promises.rename(oldDir, newDir + '/newDir');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('rename file', config('async'), async () => {
|
|
57
|
+
const dir = '/rename_file_test';
|
|
58
|
+
const one = dir + '/fun.js';
|
|
59
|
+
const two = dir + '/fun2.js';
|
|
60
|
+
|
|
61
|
+
await fs.promises.mkdir(dir);
|
|
62
|
+
await fs.promises.writeFile(one, 'while(1) alert("Hey! Listen!");');
|
|
63
|
+
await fs.promises.rename(one, one);
|
|
64
|
+
await fs.promises.rename(one, two);
|
|
65
|
+
|
|
66
|
+
await fs.promises.writeFile(one, 'hey');
|
|
67
|
+
await fs.promises.rename(one, two);
|
|
68
|
+
|
|
69
|
+
assert.equal(await fs.promises.readFile(two, 'utf8'), 'hey');
|
|
70
|
+
assert(!(await fs.promises.stat(one, { throwIfNoEntry: false })));
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('File to Directory and Directory to File Rename', config('sync', 'async'), async () => {
|
|
74
|
+
const dir = '/rename_file_dir_test';
|
|
75
|
+
const file = '/rename_file_dir_test.txt';
|
|
76
|
+
|
|
77
|
+
await fs.promises.mkdir(dir);
|
|
78
|
+
await fs.promises.writeFile(file, 'file contents go here');
|
|
79
|
+
|
|
80
|
+
await assert.rejects(fs.promises.rename(file, dir), { code: 'EISDIR' });
|
|
81
|
+
assert.throws(() => fs.renameSync(file, dir), { code: 'EISDIR' });
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('rename directory inside itself', config('sync', 'async'), async () => {
|
|
85
|
+
const renDir1 = '/rename_dir_1';
|
|
86
|
+
const renDir2 = '/rename_dir_1/lol';
|
|
87
|
+
|
|
88
|
+
await fs.promises.mkdir(renDir1);
|
|
89
|
+
|
|
90
|
+
await assert.rejects(fs.promises.rename(renDir1, renDir2), { code: 'EINVAL' });
|
|
91
|
+
assert.throws(() => fs.renameSync(renDir1, renDir2), { code: 'EINVAL' });
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
2
|
+
import { sync } from '@zenfs/core';
|
|
3
|
+
import assert from 'node:assert/strict';
|
|
4
|
+
import { suite, test } from 'node:test';
|
|
5
|
+
import { config, fs } from '@zenfs/devel/tests/common';
|
|
6
|
+
|
|
7
|
+
const n_files = 130;
|
|
8
|
+
const huge_size = 0x1000000;
|
|
9
|
+
|
|
10
|
+
// Tests for having a lot of various things (number of inodes/files, individual file size, etc.).
|
|
11
|
+
suite('Scaling', config('write'), () => {
|
|
12
|
+
test('Lots of inodes/files', config('sync', 'async'), async () => {
|
|
13
|
+
fs.mkdirSync('/n');
|
|
14
|
+
|
|
15
|
+
for (let i = 0; i < n_files; i++) {
|
|
16
|
+
fs.writeFileSync('/n/' + i, i.toString(16));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
await sync();
|
|
20
|
+
assert.equal(fs.readdirSync('/n').length, n_files);
|
|
21
|
+
|
|
22
|
+
const results = [];
|
|
23
|
+
|
|
24
|
+
for (let i = 0; i < n_files; i++) {
|
|
25
|
+
results.push(fs.promises.readFile('/n/' + i, 'utf8').then(val => assert.equal(val, i.toString(16))));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
await Promise.all(results);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('Singular file size', config('sync'), () => {
|
|
32
|
+
fs.writeFileSync('/huge', new Uint8Array(huge_size));
|
|
33
|
+
assert.equal(fs.statSync('/huge').size, huge_size);
|
|
34
|
+
});
|
|
35
|
+
});
|