Debian Codegraph @

pkg/base1/test-fsinfo.ts

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 import QUnit, { f } from "qunit-tests";
3
4 import cockpit from "cockpit";
5 import { FsInfoClient, FsInfoState, fsinfo } from "cockpit/fsinfo";
6
7 function fsinfo_err(errno: "ENOENT" | "EPERM" | "EACCES" | "ENOTDIR" | "ELOOP") {
8 const problems = {
9 ENOENT: 'not-found',
10 EPERM: 'access-denied',
11 EACCES: 'access-denied',
12 ENOTDIR: 'not-directory',
13 ELOOP: 'internal-error',
14 };
15 const strerr = {
16 ENOENT: 'No such file or directory',
17 EPERM: 'Access denied',
18 EACCES: 'Permission denied',
19 ENOTDIR: 'Not a directory',
20 ELOOP: 'Too many levels of symbolic links',
21 };
22
23 return { error: { errno, problem: problems[errno], message: strerr[errno] } };
24 }
25
26 QUnit.test("fsinfo trivial", async assert => {
27 // Make sure that it's not an error to open with no attributes requested
28 assert.deepEqual(await fsinfo("/", []), {}, "Yup, '/' is still there.");
29 });
30
31 QUnit.test("fsinfo errors", async assert => {
32 assert.timeout(5000);
33
34 // Just test that errors are reported correctly in general. The unit test
35 // is more explicit about the specific error conditions that need to be
36 // checked for.
37 let thrown: string = '';
38 try {
39 await fsinfo("rel", []);
40 assert.ok(false, "fsinfo() error checking");
41 } catch (exc: any) { // eslint-disable-line @typescript-eslint/no-explicit-any
42 thrown = exc.problem;
43 }
44 assert.equal(thrown, 'protocol-error', "fsinfo() error checking");
45 });
46
47 QUnit.test("FsInfoClient errors", async assert => {
48 const client = new FsInfoClient("rel", []);
49 try {
50 // We want to get 'close' with a problem code, and no 'change'
51 const expected = await new Promise((resolve, reject) => {
52 client.on('close', (msg) => resolve(msg.problem));
53 client.on('change', reject);
54 });
55 assert.equal(expected, 'protocol-error', "FsInfoClient error checking");
56 } finally {
57 // Calling .close() after we got 'close' event is still OK.
58 client.close();
59 }
60 });
61
62 QUnit.test("fsinfo cases", async assert => {
63 assert.timeout(5000);
64
65 const dir = await cockpit.spawn([
66 'sh', '-c',
67 `
68 cd "$(mktemp -d)"
69 echo -n "$(pwd)"
70
71 # a normal directory
72 mkdir dir
73 echo dir file > dir/dir-file.txt
74 echo do not read this > dir-file.xtx
75
76 # a directory without +x (search)
77 mkdir no-x-dir
78 echo file > no-x-dir/no-x-dir-file.txt
79 chmod 644 no-x-dir
80
81 # a directory without +r (read)
82 mkdir no-r-dir
83 echo file > no-r-dir/no-r-dir-file.txt
84 chmod 311 no-r-dir
85
86 # a normal file
87 echo normal file > file
88
89 # a non-readable file
90 echo inaccessible file > no-r-file
91 chmod 0 no-r-file
92
93 # a device
94 ln -sf /dev/null dev
95
96 # a dangling symlink
97 ln -sf does-not-exist dangling
98
99 # a symlink pointing to itself
100 ln -sf loopy loopy
101 `
102 ]);
103
104 const cases: Record<string, FsInfoState> = {
105 dir: { info: { type: 'dir', entries: { 'dir-file.txt': { type: 'reg' } } } },
106
107 // can't stat() the file
108 'no-x-dir': { info: { type: "dir", entries: { "no-x-dir-file.txt": {} } } },
109
110 // can't read the directory, so no entries
111 'no-r-dir': { info: { type: "dir" } },
112
113 // normal file, can read its contents
114 file: { info: { type: "reg" } },
115
116 // can't read file, so no contents
117 'no-r-file': { info: { type: "reg" } },
118
119 // a device
120 dev: { info: { type: "chr" } },
121
122 // a dangling symlink
123 dangling: fsinfo_err('ENOENT'),
124
125 // a link pointing at itself
126 loopy: fsinfo_err('ELOOP'),
127 } as const;
128
129 try {
130 // Check the async one-shot non-watching API first
131 for (const [name, expected_state] of Object.entries(cases)) {
132 try {
133 const state = await fsinfo(`${dir}/${name}`, ['type', 'entries']);
134 assert.deepEqual(state, expected_state.info, f`fsinfo(${name})`);
135 } catch (exc) {
136 assert.deepEqual(exc, expected_state.error, f`fsinfo(${name})`);
137 }
138 }
139
140 // Now test the client (watcher)
141 for (let [name, expected_state] of Object.entries(cases)) {
142 const client = new FsInfoClient(`${dir}/${name}`, ['type', 'entries']);
143
144 // Watching requires read access to the directory
145 if (name.includes('no-r')) {
146 expected_state = fsinfo_err('EACCES');
147 }
148
149 // await the first state change: it's guaranteed to be the complete data
150 const value = await new Promise<FsInfoState>(resolve => { client.on('change', resolve) });
151 assert.deepEqual(value, expected_state, f`FsInfoClient(${name})`);
152
153 client.close();
154 }
155 } finally {
156 await cockpit.spawn(["chmod", "-R", "u+rwX", dir]);
157 await cockpit.spawn(["rm", "-rf", dir]);
158 }
159 });
160
161 QUnit.start();