| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import cockpit from "cockpit"; |
| 3 |
import QUnit from "qunit-tests"; |
| 4 |
|
| 5 |
let dir = "/uninitialized"; |
| 6 |
|
| 7 |
QUnit.test("simple read", async assert => { |
| 8 |
const file = cockpit.file(dir + "/foo"); |
| 9 |
assert.equal(file.path, dir + "/foo", "file has path"); |
| 10 |
assert.equal(await file.read(), "1234\n", "correct result"); |
| 11 |
}); |
| 12 |
|
| 13 |
QUnit.test("read non-existant", async assert => { |
| 14 |
assert.equal(await cockpit.file(dir + "/blah").read(), null, "correct result"); |
| 15 |
}); |
| 16 |
|
| 17 |
QUnit.test("parse read", async assert => { |
| 18 |
const resp = await cockpit.file(dir + "/foo.json", { syntax: JSON }).read(); |
| 19 |
assert.deepEqual(resp, { foo: 12 }, "correct result"); |
| 20 |
}); |
| 21 |
|
| 22 |
QUnit.test("parse read error", async assert => { |
| 23 |
try { |
| 24 |
await cockpit.file(dir + "/foo.bin", { syntax: JSON }).read(); |
| 25 |
assert.ok(false, "should have failed"); |
| 26 |
} catch (error) { |
| 27 |
assert.ok(error instanceof SyntaxError, "got SyntaxError error"); |
| 28 |
} |
| 29 |
}); |
| 30 |
|
| 31 |
QUnit.test("binary read", async assert => { |
| 32 |
const resp = await cockpit.file(dir + "/foo.bin", { binary: true }).read(); |
| 33 |
assert.deepEqual(resp, new Uint8Array([0, 1, 2, 3]), "correct result"); |
| 34 |
}); |
| 35 |
|
| 36 |
QUnit.test("read non-regular", async assert => { |
| 37 |
try { |
| 38 |
await cockpit.file(dir, { binary: true }).read(); |
| 39 |
assert.ok(false, "should have failed"); |
| 40 |
} catch (error) { |
| 41 |
assert.equal((error as cockpit.BasicError).problem, "internal-error", "got error"); |
| 42 |
} |
| 43 |
}); |
| 44 |
|
| 45 |
QUnit.test("read large", async assert => { |
| 46 |
const resp = await cockpit.file(dir + "/large.bin", { binary: true }).read(); |
| 47 |
assert.equal(resp.length, 512 * 1024, "correct result"); |
| 48 |
}); |
| 49 |
|
| 50 |
QUnit.test("read too large", async assert => { |
| 51 |
try { |
| 52 |
await cockpit.file(dir + "/large.bin", { binary: true, max_read_size: 8 * 1024 }).read(); |
| 53 |
assert.ok(false, "should have failed"); |
| 54 |
} catch (error) { |
| 55 |
assert.equal((error as cockpit.BasicError).problem, "too-large", "got error"); |
| 56 |
} |
| 57 |
}); |
| 58 |
|
| 59 |
/* regression: passing 'binary: false' made cockpit-ws close the whole connection */ |
| 60 |
QUnit.test("binary false", async assert => { |
| 61 |
await cockpit.file(dir + "/foo.bin", { binary: false }).read(); |
| 62 |
assert.ok(true, "did not crash"); |
| 63 |
}); |
| 64 |
|
| 65 |
QUnit.test("simple replace", async assert => { |
| 66 |
await cockpit.file(dir + "/bar").replace("4321\n"); |
| 67 |
const res = await cockpit.spawn(["cat", dir + "/bar"]); |
| 68 |
assert.equal(res, "4321\n", "correct content"); |
| 69 |
}); |
| 70 |
|
| 71 |
QUnit.test("empty replace", async assert => { |
| 72 |
await cockpit.file(`${dir}/bar`).replace(""); |
| 73 |
const res = await cockpit.spawn(["cat", `${dir}/bar`]); |
| 74 |
assert.equal(res, "", "correct content"); |
| 75 |
}); |
| 76 |
|
| 77 |
QUnit.test("stringify replace", async assert => { |
| 78 |
await cockpit.file(dir + "/bar", { syntax: JSON }).replace({ foo: 4321 }); |
| 79 |
const res = await cockpit.spawn(["cat", dir + "/bar"]); |
| 80 |
assert.deepEqual(JSON.parse(res), { foo: 4321 }, "correct content"); |
| 81 |
}); |
| 82 |
|
| 83 |
QUnit.test("stringify replace error", async assert => { |
| 84 |
// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 85 |
const cycle: any = { }; |
| 86 |
cycle.me = cycle; |
| 87 |
try { |
| 88 |
await cockpit.file(dir + "/bar", { syntax: JSON }).replace(cycle); |
| 89 |
assert.ok(false, "should have failed"); |
| 90 |
} catch (error) { |
| 91 |
assert.ok(error instanceof TypeError, "got stringify error"); |
| 92 |
} |
| 93 |
}); |
| 94 |
|
| 95 |
QUnit.test("binary replace", async assert => { |
| 96 |
await cockpit.file(dir + "/bar", { binary: true }).replace(new Uint8Array([3, 2, 1, 0])); |
| 97 |
const res = await cockpit.spawn(["cat", dir + "/bar"], { binary: true }); |
| 98 |
assert.deepEqual(res, new Uint8Array([3, 2, 1, 0]), "correct content"); |
| 99 |
}); |
| 100 |
|
| 101 |
QUnit.test("replace large", async assert => { |
| 102 |
const str = new Array(23 * 1023).join('abcdef12345'); |
| 103 |
await cockpit.file(dir + "/large").replace(str); |
| 104 |
const res = await cockpit.spawn(["cat", dir + "/large"]); |
| 105 |
assert.equal(res.length, str.length, "correct large length"); |
| 106 |
assert.ok(res == str, "correct large data"); |
| 107 |
}); |
| 108 |
|
| 109 |
QUnit.test("binary replace large", async assert => { |
| 110 |
const data = new Uint8Array(249 * 1023); |
| 111 |
const len = data.byteLength; |
| 112 |
for (let i = 0; i < len; i++) |
| 113 |
data[i] = i % 233; |
| 114 |
await cockpit.file(dir + "/large-binary", { binary: true }).replace(data); |
| 115 |
const res = await cockpit.spawn(["cat", dir + "/large-binary"], { binary: true }); |
| 116 |
let eq = true; |
| 117 |
assert.equal(res.byteLength, 249 * 1023, "check length"); |
| 118 |
assert.equal(res.byteLength, data.byteLength, "correct large length"); |
| 119 |
for (let i = 0; i < res.byteLength; i++) { |
| 120 |
if (res[i] !== data[i] || res[i] === undefined) { |
| 121 |
eq = false; |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
assert.ok(eq, "got back same data"); |
| 126 |
}); |
| 127 |
|
| 128 |
QUnit.test("fsreplace1 no path", async assert => { |
| 129 |
const invalid = cockpit.channel({ payload: "fsreplace1", attrs: {} }); |
| 130 |
await assert.rejects(invalid.wait(), /attribute 'path' required/, "rejects no path"); |
| 131 |
}); |
| 132 |
|
| 133 |
QUnit.test("fsreplace1 attrs", async assert => { |
| 134 |
await cockpit.init(); |
| 135 |
assert.propContains(cockpit.info.channels, { fsreplace1: ['attrs'] }); |
| 136 |
|
| 137 |
const valid = cockpit.channel({ payload: "fsreplace1", path: `${dir}/tmp`, attrs: {} }); |
| 138 |
await valid.wait(); |
| 139 |
valid.close(); // no-op |
| 140 |
|
| 141 |
const invalid = cockpit.channel({ payload: "fsreplace1", path: `${dir}/tmp`, attrs: { foo: 1 } }); |
| 142 |
await assert.rejects(invalid.wait(), /"attrs" contains unsupported key\(s\): foo/, "rejects non-empty attrs"); |
| 143 |
}); |
| 144 |
|
| 145 |
QUnit.test("remove", async assert => { |
| 146 |
const exists = await cockpit.spawn(["bash", "-c", "test -f " + dir + "/bar && echo exists"]); |
| 147 |
assert.equal(exists, "exists\n", "exists"); |
| 148 |
await cockpit.file(dir + "/bar").replace(null); |
| 149 |
const res = await cockpit.spawn(["bash", "-c", "test -f " + dir + "/bar || echo gone"]); |
| 150 |
assert.equal(res, "gone\n", "gone"); |
| 151 |
}); |
| 152 |
|
| 153 |
QUnit.test("abort replace", async assert => { |
| 154 |
const done = assert.async(); |
| 155 |
assert.expect(0); |
| 156 |
|
| 157 |
const file = cockpit.file(dir + "/bar"); |
| 158 |
|
| 159 |
let n = 0; |
| 160 |
function start_after_two() { |
| 161 |
n += 1; |
| 162 |
if (n == 2) |
| 163 |
done(); |
| 164 |
} |
| 165 |
|
| 166 |
const first = file.replace("1234\n"); |
| 167 |
const second = file.replace("abcd\n"); |
| 168 |
|
| 169 |
try { |
| 170 |
await first; |
| 171 |
throw new Error("unexpectedly succeeded first promise"); |
| 172 |
} catch (_error) { |
| 173 |
start_after_two(); |
| 174 |
} |
| 175 |
|
| 176 |
await second; |
| 177 |
start_after_two(); |
| 178 |
}); |
| 179 |
|
| 180 |
QUnit.test("replace with tag", async assert => { |
| 181 |
const done = assert.async(); |
| 182 |
const file = cockpit.file(dir + "/barfoo"); |
| 183 |
|
| 184 |
file.read() |
| 185 |
// @ts-expect-error cannot represent read promise signature with tag |
| 186 |
.then(async (content, tag_1) => { |
| 187 |
assert.equal(content, null, "file does not exist"); |
| 188 |
assert.equal(tag_1, "-", "first tag is -"); |
| 189 |
const tag_2 = await file.replace("klmn\n", tag_1); |
| 190 |
assert.notEqual(tag_2, "-", "second tag is not -"); |
| 191 |
const tag_3 = await file.replace("KLMN\n", tag_2); |
| 192 |
assert.notEqual(tag_3, tag_2, "third tag is different"); |
| 193 |
try { |
| 194 |
await file.replace("opqr\n", tag_2); |
| 195 |
assert.ok(false, "should have failed"); |
| 196 |
} catch (error) { |
| 197 |
assert.equal((error as cockpit.BasicError).problem, "change-conflict", "wrong tag is rejected"); |
| 198 |
done(); |
| 199 |
} |
| 200 |
}); |
| 201 |
}); |
| 202 |
|
| 203 |
QUnit.test("modify", async assert => { |
| 204 |
const file = cockpit.file(dir + "/quux"); |
| 205 |
|
| 206 |
let n = 0; |
| 207 |
await file.modify(old => { |
| 208 |
n += 1; |
| 209 |
assert.equal(old, null, "no old content"); |
| 210 |
return "ABCD\n"; |
| 211 |
}); |
| 212 |
assert.equal(n, 1, "callback called once"); |
| 213 |
|
| 214 |
n = 0; |
| 215 |
await file.modify(old => { |
| 216 |
n += 1; |
| 217 |
assert.equal(old, "ABCD\n", "correct old content"); |
| 218 |
return "dcba\n"; |
| 219 |
}); |
| 220 |
assert.equal(n, 1, "callback called once"); |
| 221 |
|
| 222 |
assert.equal(await cockpit.spawn(["cat", dir + "/quux"]), "dcba\n", "correct content"); |
| 223 |
|
| 224 |
// make sure that writing "" results in an empty file, not a deleted one |
| 225 |
n = 0; |
| 226 |
await file.modify(old => { |
| 227 |
n += 1; |
| 228 |
assert.equal(old, "dcba\n", "correct old content"); |
| 229 |
return ""; |
| 230 |
}); |
| 231 |
assert.equal(n, 1, "callback called once"); |
| 232 |
|
| 233 |
assert.equal(await cockpit.spawn(["cat", dir + "/quux"]), "", "correct content"); |
| 234 |
|
| 235 |
// make sure that writing null deletes the file |
| 236 |
n = 0; |
| 237 |
await file.modify(old => { |
| 238 |
n += 1; |
| 239 |
assert.equal(old, "", "correct old content"); |
| 240 |
return null; |
| 241 |
}); |
| 242 |
assert.equal(n, 1, "callback called once"); |
| 243 |
|
| 244 |
assert.rejects(cockpit.spawn(["cat", dir + "/quux"]), /No such file or directory/, "file deleted"); |
| 245 |
}); |
| 246 |
|
| 247 |
QUnit.test("modify with conflict", async assert => { |
| 248 |
const done = assert.async(); |
| 249 |
assert.expect(6); |
| 250 |
|
| 251 |
const file = cockpit.file(dir + "/baz"); |
| 252 |
|
| 253 |
let n = 0; |
| 254 |
file |
| 255 |
.modify(old => { |
| 256 |
n += 1; |
| 257 |
assert.equal(old, null, "no old content"); |
| 258 |
return "ABCD\n"; |
| 259 |
}) |
| 260 |
.finally(() => assert.equal(n, 1, "callback called once")) |
| 261 |
|
| 262 |
// @ts-expect-error cannot represent modify promise signature with tag |
| 263 |
.then(async (content, tag) => { |
| 264 |
let n = 0; |
| 265 |
await cockpit.spawn(["bash", "-c", "sleep 1; echo XYZ > " + dir + "/baz"]); |
| 266 |
await file.modify(old => { |
| 267 |
cockpit.assert(old !== null, "old is null"); |
| 268 |
n += 1; |
| 269 |
if (n == 1) |
| 270 |
assert.equal(old, "ABCD\n", "correct old (out-of-date) content"); |
| 271 |
else |
| 272 |
assert.equal(old, "XYZ\n", "correct old (current) content"); |
| 273 |
return old.toLowerCase(); |
| 274 |
}, content, tag); |
| 275 |
|
| 276 |
assert.equal(n, 2, "callback called twice"); |
| 277 |
const res = await cockpit.spawn(["cat", dir + "/baz"]); |
| 278 |
assert.equal(res, "xyz\n", "correct content"); |
| 279 |
done(); |
| 280 |
}); |
| 281 |
}); |
| 282 |
|
| 283 |
QUnit.test("watching", assert => { |
| 284 |
const done = assert.async(); |
| 285 |
assert.expect(7); |
| 286 |
|
| 287 |
const file = cockpit.file(dir + "/foobar"); |
| 288 |
let n = 0; |
| 289 |
const watch = file.watch((content, tag) => { |
| 290 |
n += 1; |
| 291 |
if (n == 1) { |
| 292 |
assert.equal(content, null, "initially non-existent"); |
| 293 |
assert.equal(tag, "-", "empty tag"); |
| 294 |
cockpit.spawn(["bash", "-c", "cd " + dir + " && echo 1234 > foobar.tmp && mv foobar.tmp foobar"]); |
| 295 |
} else if (n == 2) { |
| 296 |
assert.equal(content, "1234\n", "correct new content"); |
| 297 |
assert.notEqual(tag, "-"); |
| 298 |
cockpit.assert(tag !== null, "tag is null"); |
| 299 |
assert.ok(tag.length > 5, "tag has a reasonable size"); |
| 300 |
cockpit.spawn(["bash", "-c", "rm " + dir + "/foobar"]); |
| 301 |
} else if (n == 3) { |
| 302 |
assert.equal(content, null, "finally non-existent"); |
| 303 |
assert.equal(tag, "-", "empty tag"); |
| 304 |
watch.remove(); |
| 305 |
done(); |
| 306 |
} |
| 307 |
}); |
| 308 |
}); |
| 309 |
|
| 310 |
QUnit.test("binary watching", assert => { |
| 311 |
const done = assert.async(); |
| 312 |
assert.expect(3); |
| 313 |
|
| 314 |
const file = cockpit.file(dir + "/foobar", { binary: true }); |
| 315 |
let n = 0; |
| 316 |
const watch = file.watch(content => { |
| 317 |
n += 1; |
| 318 |
if (n == 1) { |
| 319 |
assert.equal(content, null, "initially non-existent"); |
| 320 |
cockpit.spawn(["bash", "-c", "cd " + dir + " && echo '//8BAg==' | base64 -d > foobar.tmp && mv foobar.tmp foobar"]); |
| 321 |
} else if (n == 2) { |
| 322 |
assert.deepEqual(content, new Uint8Array([255, 255, 1, 2]), "correct new content"); |
| 323 |
cockpit.spawn(["bash", "-c", "rm " + dir + "/foobar"]); |
| 324 |
} else if (n == 3) { |
| 325 |
assert.equal(content, null, "finally non-existent"); |
| 326 |
watch.remove(); |
| 327 |
done(); |
| 328 |
} |
| 329 |
}); |
| 330 |
}); |
| 331 |
|
| 332 |
QUnit.test("syntax watching", assert => { |
| 333 |
const done = assert.async(); |
| 334 |
assert.expect(3); |
| 335 |
|
| 336 |
const file = cockpit.file(dir + "/foobar.json", { syntax: JSON }); |
| 337 |
let n = 0; |
| 338 |
const watch = file.watch((content, _tag, err) => { |
| 339 |
n += 1; |
| 340 |
if (n == 1) { |
| 341 |
assert.equal(content, null, "initially non-existent"); |
| 342 |
cockpit.spawn(["bash", "-c", "cd " + dir + " && echo '[ 1, 2, 3, 4 ]' > foobar.json.tmp && mv foobar.json.tmp foobar.json"]); |
| 343 |
} else if (n == 2) { |
| 344 |
assert.deepEqual(content, [1, 2, 3, 4], "correct new content"); |
| 345 |
cockpit.spawn(["bash", "-c", "echo 'hi-there-this-is-not-json' > " + dir + "/foobar.json"]); |
| 346 |
} else if (n == 3) { |
| 347 |
assert.ok(err instanceof SyntaxError, "got SyntaxError error"); |
| 348 |
watch.remove(); |
| 349 |
done(); |
| 350 |
} else |
| 351 |
assert.ok(false, "not reached"); |
| 352 |
}); |
| 353 |
}); |
| 354 |
|
| 355 |
QUnit.test("watching without reading", assert => { |
| 356 |
const done = assert.async(); |
| 357 |
assert.expect(7); |
| 358 |
|
| 359 |
const file = cockpit.file(dir + "/foobar"); |
| 360 |
let n = 0; |
| 361 |
const watch = file.watch((content, tag) => { |
| 362 |
n += 1; |
| 363 |
if (n == 1) { |
| 364 |
assert.equal(content, null, "initially non-existent"); |
| 365 |
assert.equal(tag, "-", "empty tag"); |
| 366 |
cockpit.spawn(["bash", "-c", "cd " + dir + " && echo 1234 > foobar.tmp && mv foobar.tmp foobar"]); |
| 367 |
} else if (n == 2) { |
| 368 |
assert.equal(content, null, "no content as reading is disabled"); |
| 369 |
assert.notEqual(tag, "-"); |
| 370 |
cockpit.assert(tag !== null, "tag is null"); |
| 371 |
assert.ok(tag.length > 5, "tag has a reasonable size"); |
| 372 |
cockpit.spawn(["bash", "-c", "rm " + dir + "/foobar"]); |
| 373 |
} else if (n == 3) { |
| 374 |
assert.equal(content, null, "finally non-existent"); |
| 375 |
assert.equal(tag, "-", "empty tag"); |
| 376 |
watch.remove(); |
| 377 |
done(); |
| 378 |
} |
| 379 |
}, { read: false }); |
| 380 |
}); |
| 381 |
|
| 382 |
QUnit.test("watching without reading pre-created", async assert => { |
| 383 |
const done = assert.async(); |
| 384 |
assert.expect(3); |
| 385 |
|
| 386 |
// Pre-create fsinfo test file |
| 387 |
const file = cockpit.file(dir + "/fsinfo"); |
| 388 |
await file.replace("1234"); |
| 389 |
const watch = file.watch((content, tag) => { |
| 390 |
assert.equal(content, null, "non-existant because read is false"); |
| 391 |
assert.notEqual(tag, null, "non empty tag"); |
| 392 |
cockpit.assert(tag !== null, "tag is null"); |
| 393 |
assert.equal(tag.startsWith("1:"), true, "tag always starts with 1:"); |
| 394 |
watch.remove(); |
| 395 |
done(); |
| 396 |
}, { read: false }); |
| 397 |
}); |
| 398 |
|
| 399 |
QUnit.test("watching directory", assert => { |
| 400 |
const done = assert.async(); |
| 401 |
assert.expect(20); |
| 402 |
|
| 403 |
let n = 0; |
| 404 |
const watch = cockpit.channel({ payload: "fswatch1", path: dir }); |
| 405 |
watch.addEventListener("message", (_event, payload) => { |
| 406 |
const msg = JSON.parse(payload); |
| 407 |
n += 1; |
| 408 |
|
| 409 |
if (n == 1) { |
| 410 |
assert.equal(msg.event, "created", "world.txt created"); |
| 411 |
assert.equal(msg.path, dir + "/world.txt"); |
| 412 |
assert.equal(msg.type, "file"); |
| 413 |
assert.notEqual(msg.tag, "-"); |
| 414 |
} else if (n == 2) { |
| 415 |
assert.equal(msg.event, "changed", "world.txt changed"); |
| 416 |
assert.equal(msg.path, dir + "/world.txt"); |
| 417 |
assert.notEqual(msg.tag, "-"); |
| 418 |
} else if (n == 3) { |
| 419 |
assert.equal(msg.event, "done-hint", "world.txt done-hint"); |
| 420 |
assert.equal(msg.path, dir + "/world.txt"); |
| 421 |
assert.notEqual(msg.tag, "-"); |
| 422 |
|
| 423 |
cockpit.spawn(["chmod", "001", `${dir}/world.txt`]); |
| 424 |
} else if (n == 4) { |
| 425 |
assert.equal(msg.event, "attribute-changed", "world.txt attribute-changed"); |
| 426 |
assert.equal(msg.path, dir + "/world.txt"); |
| 427 |
assert.notEqual(msg.tag, "-"); |
| 428 |
|
| 429 |
cockpit.spawn(["rm", `${dir}/world.txt`]); |
| 430 |
} else if (n == 5) { |
| 431 |
assert.equal(msg.event, "deleted", "world.txt deleted"); |
| 432 |
assert.equal(msg.path, dir + "/world.txt"); |
| 433 |
assert.equal(msg.tag, "-"); |
| 434 |
|
| 435 |
cockpit.spawn(["mkdir", `${dir}/somedir`]); |
| 436 |
} else if (n == 6) { |
| 437 |
assert.equal(msg.event, "created", "somedir created"); |
| 438 |
assert.equal(msg.path, dir + "/somedir"); |
| 439 |
assert.equal(msg.type, "directory"); |
| 440 |
assert.notEqual(msg.tag, "-"); |
| 441 |
|
| 442 |
watch.close(); |
| 443 |
done(); |
| 444 |
} |
| 445 |
}); |
| 446 |
|
| 447 |
// trigger the first event |
| 448 |
cockpit.spawn(["sh", "-c", `echo hello > ${dir}/world.txt`]); |
| 449 |
}); |
| 450 |
|
| 451 |
QUnit.test("watching error", async assert => { |
| 452 |
const dir = await cockpit.spawn([ |
| 453 |
'sh', '-c', |
| 454 |
` |
| 455 |
cd "$(mktemp -d)" |
| 456 |
echo -n "$(pwd)" |
| 457 |
|
| 458 |
mkdir dir |
| 459 |
echo dir file > dir/dir-file.txt |
| 460 |
echo do not read this > dir-file.xtx |
| 461 |
chmod 0 dir |
| 462 |
` |
| 463 |
]); |
| 464 |
|
| 465 |
const file = cockpit.file(`${dir}/dir/file`); |
| 466 |
|
| 467 |
try { |
| 468 |
await new Promise<void>(resolve => { |
| 469 |
file.watch((content, tag, error) => { |
| 470 |
assert.equal(content, null); |
| 471 |
assert.equal(tag, null); |
| 472 |
cockpit.assert(error !== null, "error is null"); |
| 473 |
assert.equal(error.problem, 'access-denied'); |
| 474 |
resolve(); |
| 475 |
}); |
| 476 |
}); |
| 477 |
} finally { |
| 478 |
file.close(); |
| 479 |
await cockpit.spawn(["chmod", "-R", "u+rwX", dir]); |
| 480 |
await cockpit.spawn(["rm", "-rf", dir]); |
| 481 |
} |
| 482 |
}); |
| 483 |
|
| 484 |
QUnit.test("closing", assert => { |
| 485 |
const done = assert.async(); |
| 486 |
assert.expect(2); |
| 487 |
|
| 488 |
const file = cockpit.file(dir + "/foobarbaz"); |
| 489 |
const watch = file.watch(changed); |
| 490 |
|
| 491 |
let n = 0; |
| 492 |
function start_after_two() { |
| 493 |
n += 1; |
| 494 |
if (n == 2) { |
| 495 |
watch.remove(); |
| 496 |
done(); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
file.read() |
| 501 |
.then(() => { |
| 502 |
assert.ok(false, "read didn't complete"); |
| 503 |
}) |
| 504 |
.catch(error => { |
| 505 |
assert.equal(error.problem, "cancelled", "read got cancelled"); |
| 506 |
start_after_two(); |
| 507 |
}); |
| 508 |
|
| 509 |
function changed(_content: string | null, _tag: string | null, err: cockpit.BasicError | null) { |
| 510 |
if (err) { |
| 511 |
assert.equal(err.problem, "cancelled", "watch got cancelled"); |
| 512 |
start_after_two(); |
| 513 |
} else { |
| 514 |
assert.ok(false, "not reached"); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
file.close(); |
| 519 |
}); |
| 520 |
|
| 521 |
QUnit.test("channel options", async assert => { |
| 522 |
const data = await cockpit.file(dir + "/foo", { binary: true }).read(); |
| 523 |
assert.ok(data instanceof Uint8Array, "options applied, got binary data"); |
| 524 |
}); |
| 525 |
|
| 526 |
QUnit.test("remove testdir", async assert => { |
| 527 |
await cockpit.spawn(["rm", "-rf", dir]); |
| 528 |
assert.ok(true, "did not crash"); |
| 529 |
}); |
| 530 |
|
| 531 |
(async () => { |
| 532 |
const resp = await cockpit.spawn(["bash", "-c", "d=$(mktemp -d); echo '1234' >$d/foo; echo '{ \"foo\": 12 }' >$d/foo.json; echo -en '\\x00\\x01\\x02\\x03' >$d/foo.bin; dd if=/dev/zero of=$d/large.bin bs=1k count=512; echo $d"]); |
| 533 |
dir = resp.replace(/\n$/, ""); |
| 534 |
QUnit.start(); |
| 535 |
})(); |