Debian Codegraph @

pkg/base1/test-dbus.js

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 import cockpit from "cockpit";
3 import QUnit, { skipWithPybridge } from "qunit-tests";
4
5 import { common_dbus_tests, dbus_track_tests } from "./test-dbus-common.js";
6
7 /* with a name */
8 const options = {
9 bus: "session"
10 };
11 common_dbus_tests(options, "com.redhat.Cockpit.DBusTests.Test");
12 dbus_track_tests(options, "com.redhat.Cockpit.DBusTests.Test");
13
14 QUnit.test("proxy no stutter", function (assert) {
15 const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.Test", { bus: "session" });
16
17 const proxy = dbus.proxy();
18 assert.equal(proxy.iface, "com.redhat.Cockpit.DBusTests.Test", "interface auto chosen");
19 assert.equal(proxy.path, "/com/redhat/Cockpit/DBusTests/Test", "path auto chosen");
20 });
21
22 QUnit.test("proxies no stutter", function (assert) {
23 const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.Test", { bus: "session" });
24
25 const proxies = dbus.proxies();
26 assert.equal(proxies.iface, "com.redhat.Cockpit.DBusTests.Test", "interface auto chosen");
27 assert.equal(proxies.path_namespace, "/", "path auto chosen");
28 });
29
30 QUnit.test("exposed client and options", function (assert) {
31 const options = { host: "localhost", bus: "session" };
32 const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.Test", options);
33 const proxy = dbus.proxy("com.redhat.Cockpit.DBusTests.Frobber", "/otree/frobber");
34 const proxies = dbus.proxies("com.redhat.Cockpit.DBusTests.Frobber");
35
36 assert.deepEqual(dbus.options, options, "client object exposes options");
37 assert.strictEqual(proxy.client, dbus, "proxy object exposes client");
38 assert.strictEqual(proxies.client, dbus, "proxies object exposes client");
39 });
40
41 QUnit.test("subscriptions on closed client", function (assert) {
42 function on_signal() {
43 }
44
45 const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.Test", { bus: "session" });
46 dbus.close();
47
48 const subscription = dbus.subscribe({
49 interface: "com.redhat.Cockpit.DBusTests.Frobber",
50 path: "/otree/frobber"
51 }, on_signal);
52 assert.ok(subscription, "can subscribe");
53
54 subscription.remove();
55 assert.ok(true, "can unsubscribe");
56 });
57
58 QUnit.test("watch promise recursive", function (assert) {
59 assert.expect(7);
60
61 const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.Test", { bus: "session" });
62 const promise = dbus.watch("/otree/frobber");
63
64 const target = { };
65 const promise2 = promise.promise(target);
66 assert.strictEqual(promise2, target, "used target");
67 assert.equal(typeof promise2.done, "function", "promise2.done()");
68 assert.equal(typeof promise2.promise, "function", "promise2.promise()");
69 assert.equal(typeof promise2.remove, "function", "promise2.remove()");
70
71 const promise3 = promise2.promise();
72 assert.equal(typeof promise3.done, "function", "promise3.done()");
73 assert.equal(typeof promise3.promise, "function", "promise3.promise()");
74 assert.equal(typeof promise3.remove, "function", "promise3.remove()");
75 });
76
77 QUnit.test("owned messages", function (assert) {
78 const done = assert.async();
79 assert.expect(9);
80
81 const name = "yo.x" + new Date().getTime();
82 let times_changed = 0;
83
84 const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.Test", { bus: "session" });
85 let other = null;
86 let org_owner = null;
87 function on_owner (event, owner) {
88 if (times_changed === 0) {
89 assert.strictEqual(typeof owner, "string", "initial owner string");
90 assert.ok(owner.length > 1, "initial owner not empty");
91 org_owner = owner;
92 } else if (times_changed === 1) {
93 assert.strictEqual(owner, null, "no owner");
94 } else if (times_changed === 2) {
95 // owner is the same because the server
96 // dbus connection is too.
97 assert.strictEqual(owner, org_owner, "has owner again");
98 }
99 times_changed++;
100 }
101
102 function acquire_name () {
103 dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
104 "ClaimOtherName", [name])
105 .always(function() {
106 assert.equal(this.state(), "resolved", "name claimed");
107 if (!other) {
108 other = cockpit.dbus(name, { bus: "session" });
109 other.addEventListener("owner", on_owner);
110 release_name();
111 } else {
112 assert.strictEqual(times_changed, 3, "owner changed three times");
113 done();
114 }
115 });
116 }
117
118 function release_name () {
119 other.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
120 "HelloWorld", ["test"])
121 .always(function() {
122 assert.equal(this.state(), "resolved", "called on other name");
123
124 dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
125 "ReleaseOtherName", [name])
126 .always(function() {
127 assert.equal(this.state(), "resolved", "name released");
128 acquire_name();
129 });
130 });
131 }
132 acquire_name();
133 });
134
135 QUnit.test("owned message for absent service", assert => {
136 const done = assert.async();
137 assert.expect(1);
138
139 const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.NotExisting", { bus: "session" });
140 dbus.addEventListener("owner", (_event, owner) => {
141 assert.strictEqual(owner, null, "no owner");
142 done();
143 });
144 });
145
146 skipWithPybridge("bad dbus address", function (assert) {
147 const done = assert.async();
148 assert.expect(1);
149
150 const dbus = cockpit.dbus(null, { bus: "none", address: "bad" });
151 dbus.addEventListener("close", (event, options) => {
152 assert.equal(options.problem, "protocol-error", "bad address closed");
153 done();
154 });
155 });
156
157 skipWithPybridge("bad dbus bus", function (assert) {
158 const done = assert.async();
159 assert.expect(1);
160
161 const dbus = cockpit.dbus(null, { bus: "bad" });
162 dbus.addEventListener("close", (event, options) => {
163 assert.equal(options.problem, "protocol-error", "bad bus format");
164 done();
165 });
166 });
167
168 QUnit.test("wait ready", function (assert) {
169 const done = assert.async();
170 assert.expect(1);
171
172 const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.Test", { bus: "session" });
173 dbus.wait().then(function(options) {
174 assert.ok(!!dbus.unique_name, "wait fills unique_name");
175 }, function() {
176 assert.ok(false, "shouldn't fail");
177 })
178 .always(function() {
179 done();
180 });
181 });
182
183 QUnit.test("wait fail", function (assert) {
184 const done = assert.async();
185 assert.expect(1);
186
187 const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.NotExisting", { bus: "session" });
188 dbus.wait().then(function(options) {
189 assert.ok(false, "shouldn't succeed");
190 }, function() {
191 assert.ok(true, "should fail");
192 })
193 .always(function() {
194 done();
195 });
196 });
197
198 skipWithPybridge("no default name", function (assert) {
199 const done = assert.async();
200 assert.expect(1);
201
202 const dbus = cockpit.dbus(null, { bus: "session" });
203 dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
204 "HelloWorld", ["Browser-side JS"], { name: "com.redhat.Cockpit.DBusTests.Test" })
205 .then(function(reply) {
206 assert.deepEqual(reply, ["Word! You said `Browser-side JS'. I'm Skeleton, btw!"], "replied");
207 }, function(ex) {
208 assert.ok(false, "shouldn't fail");
209 })
210 .always(function() {
211 done();
212 });
213 });
214
215 skipWithPybridge("no default name bad", function (assert) {
216 const done = assert.async();
217 assert.expect(2);
218
219 const dbus = cockpit.dbus(null, { bus: "session" });
220 dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
221 "HelloWorld", ["Browser-side JS"], { name: 5 })
222 .then(function(reply) {
223 assert.ok(false, "shouldn't succeed");
224 }, function(ex) {
225 assert.equal(ex.problem, "protocol-error", "error problem");
226 assert.equal(ex.message, "the \"name\" field is invalid in dbus call", "error message");
227 })
228 .always(function() {
229 done();
230 });
231 });
232
233 skipWithPybridge("no default name invalid", function (assert) {
234 const done = assert.async();
235 assert.expect(2);
236
237 const dbus = cockpit.dbus(null, { bus: "session" });
238 dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
239 "HelloWorld", ["Browser-side JS"], { name: "!invalid!" })
240 .then(function(reply) {
241 assert.ok(false, "shouldn't succeed");
242 }, function(ex) {
243 assert.equal(ex.problem, "protocol-error", "error problem");
244 assert.equal(ex.message, "the \"name\" field in dbus call is not a valid bus name: !invalid!", "error message");
245 })
246 .always(function() {
247 done();
248 });
249 });
250
251 skipWithPybridge("no default name missing", function (assert) {
252 const done = assert.async();
253 assert.expect(2);
254
255 const dbus = cockpit.dbus(null, { bus: "session" });
256 dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
257 "HelloWorld", ["Browser-side JS"])
258 .then(function(reply) {
259 assert.ok(false, "shouldn't succeed");
260 }, function(ex) {
261 assert.equal(ex.problem, "protocol-error", "error problem");
262 assert.equal(ex.message, "the \"name\" field is missing in dbus call", "error message");
263 })
264 .always(function() {
265 done();
266 });
267 });
268
269 skipWithPybridge("no default name second", function (assert) {
270 const done = assert.async();
271 assert.expect(2);
272
273 const dbus = cockpit.dbus(null, { bus: "session" });
274 dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "TellMeYourName", [],
275 { name: "com.redhat.Cockpit.DBusTests.Test" })
276 .then(function(reply) {
277 assert.deepEqual(reply, ["com.redhat.Cockpit.DBusTests.Test"], "right name");
278 return dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "TellMeYourName", [],
279 { name: "com.redhat.Cockpit.DBusTests.Second" })
280 .then(function(reply) {
281 assert.deepEqual(reply, ["com.redhat.Cockpit.DBusTests.Second"], "second name");
282 }, function(ex) {
283 assert.ok(false, "shouldn't fail");
284 });
285 }, function(ex) {
286 console.log(ex);
287 assert.ok(false, "shouldn't fail");
288 })
289 .always(function() {
290 done();
291 });
292 });
293
294 skipWithPybridge("override default name", function (assert) {
295 const done = assert.async();
296 assert.expect(2);
297
298 const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.Test", { bus: "session" });
299 dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "TellMeYourName", [])
300 .then(function(reply) {
301 assert.deepEqual(reply, ["com.redhat.Cockpit.DBusTests.Test"], "right name");
302 return dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "TellMeYourName", [],
303 { name: "com.redhat.Cockpit.DBusTests.Second" })
304 .then(function(reply) {
305 assert.deepEqual(reply, ["com.redhat.Cockpit.DBusTests.Second"], "second name");
306 }, function(ex) {
307 assert.ok(false, "shouldn't fail");
308 });
309 }, function(ex) {
310 console.log(ex);
311 assert.ok(false, "shouldn't fail");
312 })
313 .always(function() {
314 done();
315 });
316 });
317
318 skipWithPybridge("watch no default name", function (assert) {
319 const done = assert.async();
320 assert.expect(1);
321
322 const cache = { };
323
324 const dbus = cockpit.dbus(null, { bus: "session" });
325 dbus.addEventListener("notify", function(event, data) {
326 Object.assign(cache, data);
327 });
328
329 dbus.watch({ path: "/otree/frobber", name: "com.redhat.Cockpit.DBusTests.Second" })
330 .then(function() {
331 assert.equal(typeof cache["/otree/frobber"], "object", "has path");
332 }, function(ex) {
333 assert.ok(false, "shouldn't fail");
334 })
335 .always(function() {
336 dbus.close();
337 done();
338 });
339 });
340
341 skipWithPybridge("watch missing name", function (assert) {
342 const done = assert.async();
343 assert.expect(2);
344
345 const dbus = cockpit.dbus(null, { bus: "session", other: "option" });
346 dbus.watch("/otree/frobber")
347 .then(function() {
348 assert.ok(false, "shouldn't succeed");
349 }, function(ex) {
350 assert.equal(ex.problem, "protocol-error", "error problem");
351 assert.equal(ex.message, "session: no \"name\" specified in match", "error message");
352 })
353 .always(function() {
354 dbus.close();
355 done();
356 });
357 });
358
359 skipWithPybridge("shared client", function (assert) {
360 const done = assert.async();
361 assert.expect(2);
362
363 const dbus1 = cockpit.dbus(null, { bus: "session" });
364 const dbus2 = cockpit.dbus(null, { bus: "session" });
365
366 /* Is identical */
367 assert.strictEqual(dbus1, dbus2, "shared bus returned");
368
369 /* Closing shouldn't close shared */
370 dbus1.close();
371
372 dbus2.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
373 "HelloWorld", ["Browser-side JS"], { name: "com.redhat.Cockpit.DBusTests.Test" })
374 .then(function(reply) {
375 assert.deepEqual(reply, ["Word! You said `Browser-side JS'. I'm Skeleton, btw!"],
376 "call still works");
377 }, function(ex) {
378 assert.ok(false, "shouldn't fail");
379 })
380 .always(function() {
381 done();
382 });
383 });
384
385 QUnit.test("not shared option", function (assert) {
386 assert.expect(1);
387
388 const dbus1 = cockpit.dbus(null, { bus: "session" });
389 const dbus2 = cockpit.dbus(null, { bus: "session", other: "option" });
390
391 /* Should not be identical */
392 assert.notStrictEqual(dbus1, dbus2, "shared bus returned");
393
394 /* Closing shouldn't close shared */
395 dbus1.close();
396 dbus2.close();
397 });
398
399 skipWithPybridge("emit signal type", function (assert) {
400 const done = assert.async();
401 assert.expect(4);
402
403 let received = false;
404 const dbus = cockpit.dbus(null, { bus: "session", other: "option" });
405 dbus.wait(function() {
406 dbus.subscribe({ path: "/bork", name: dbus.unique_name }, function(path, iface, signal, args) {
407 assert.equal(path, "/bork", "reflected path");
408 assert.equal(iface, "borkety.Bork", "reflected interface");
409 assert.equal(signal, "Bork", "reflected signal");
410 assert.deepEqual(args, [1, 2, 3, 4, "Bork"], "reflected arguments");
411 received = true;
412 dbus.close();
413 done();
414 });
415
416 dbus.addEventListener("close", function(event, ex) {
417 if (!received) {
418 console.log(ex);
419 assert.ok(false, "shouldn't fail");
420 done();
421 }
422 });
423
424 dbus.signal("/bork", "borkety.Bork", "Bork", [1, 2, 3, 4, "Bork"],
425 { type: "iiiis" });
426 });
427 });
428
429 skipWithPybridge("emit signal no meta", function (assert) {
430 const done = assert.async();
431 assert.expect(2);
432
433 const dbus = cockpit.dbus(null, { bus: "session", other: "option" });
434
435 function closed(event, ex) {
436 assert.equal(ex.problem, "protocol-error", "correct problem");
437 assert.equal(ex.message, "signal argument types for signal borkety.Bork Bork unknown", "correct message");
438 dbus.removeEventListener("close", closed);
439 dbus.close();
440 done();
441 }
442
443 dbus.addEventListener("close", closed);
444 dbus.signal("/bork", "borkety.Bork", "Bork", [1, 2, 3, 4, "Bork"]);
445 });
446
447 async function internal_test(assert, options) {
448 const dbus = cockpit.dbus(null, options);
449 const resp = await dbus.call("/", "org.freedesktop.DBus.Introspectable", "Introspect");
450 assert.ok(String(resp[0]).indexOf("<node") !== -1, "introspected internal");
451 }
452
453 QUnit.test("internal dbus", async assert => internal_test(assert, { bus: "internal" }));
454
455 skipWithPybridge("internal dbus bus none",
456 async assert => internal_test(assert, { bus: "none" }));
457
458 skipWithPybridge("internal dbus bus none with address",
459 async assert => internal_test(assert, { bus: "none", address: "internal" }));
460
461 skipWithPybridge("separate dbus connections for channel groups", function (assert) {
462 const done = assert.async();
463 assert.expect(4);
464
465 const channel1 = cockpit.channel({ payload: 'dbus-json3', group: 'foo', bus: 'session' });
466 const channel2 = cockpit.channel({ payload: 'dbus-json3', group: 'bar', bus: 'session' });
467 const channel3 = cockpit.channel({ payload: 'dbus-json3', group: 'foo', bus: 'session' });
468 const channel4 = cockpit.channel({ payload: 'dbus-json3', group: 'baz', bus: 'session' });
469
470 Promise.all([
471 channel1.wait(), channel2.wait(), channel3.wait(), channel4.wait()
472 ]).then(function ([ready1, ready2, ready3, ready4]) {
473 assert.equal(ready1['unique-name'], ready3['unique-name']);
474 assert.notEqual(ready1['unique-name'], ready2['unique-name']);
475 assert.notEqual(ready1['unique-name'], ready4['unique-name']);
476 assert.notEqual(ready2['unique-name'], ready4['unique-name']);
477 done();
478 });
479 });
480
481 QUnit.test("cockpit.Config internal D-Bus API", async assert => {
482 const dbus = cockpit.dbus(null, { bus: "internal" });
483
484 // Get temp config dir to see where to place our test config
485 const reply = await dbus.call("/environment", "org.freedesktop.DBus.Properties", "Get",
486 ["cockpit.Environment", "Variables"]);
487 const configDir = reply[0].v.XDG_CONFIG_DIRS;
488 await cockpit.file(configDir + "/cockpit/cockpit.conf").replace(`
489 [SomeSection]
490 SomeA = one
491 SomethingElse = 2
492 LargeNum = 12345
493
494 [Other]
495 Flavor=chocolate
496 Empty=
497 `);
498 const proxy = dbus.proxy("cockpit.Config", "/config");
499 await proxy.wait();
500 await proxy.Reload();
501
502 // test GetString()
503 assert.equal(await proxy.GetString("SomeSection", "SomeA"), "one");
504 assert.equal(await proxy.GetString("Other", "Empty"), "");
505
506 // test GetUInt()
507
508 // this key exists, ignores default
509 assert.equal(await proxy.GetUInt("SomeSection", "SomethingElse", 10, 100, 0), 2);
510 // this key does not exist, return default
511 assert.equal(await proxy.GetUInt("SomeSection", "NotExisting", 10, 100, 0), 10);
512 // out of bounds, clamp to minimum
513 assert.equal(await proxy.GetUInt("SomeSection", "SomethingElse", 42, 50, 5), 5);
514 // out of bounds, clamp to maximum
515 assert.equal(await proxy.GetUInt("SomeSection", "LargeNum", 42, 50, 5), 50);
516 // not an integer value, returns default
517 assert.equal(await proxy.GetUInt("SomeSection", "SomeA", 10, 100, 0), 10);
518
519 // test GetString with non-existing section
520 assert.rejects(proxy.GetString("UnknownSection", "SomeKey"),
521 /key.*UnknownSection.*not exist/,
522 "unknown section raises an error");
523
524 // test GetString with non-existing key in existing section
525 assert.rejects(proxy.GetString("SomeSection", "UnknownKey"),
526 /key.*UnknownKey.*not exist/,
527 "unknown key raises an error");
528
529 // empty config
530 await cockpit.file(configDir + "/cockpit/cockpit.conf").replace("");
531 await proxy.Reload();
532 assert.rejects(proxy.GetString("SomeSection", "SomeA"),
533 /key.*SomeSection.*not exist/,
534 "query in empty config raises an error");
535
536 // broken config (missing section header)
537 await cockpit.file(configDir + "/cockpit/cockpit.conf").replace("SomeA = two");
538 await proxy.Reload();
539 assert.rejects(proxy.GetString("SomeSection", "SomeA"),
540 /key.*SomeSection.*not exist/,
541 "query in broken config raises an error");
542 });
543
544 QUnit.test("nonexisting address", async assert => {
545 const dbus = cockpit.dbus("org.freedesktop.DBus", { address: "unix:path=/nonexisting", bus: "none" });
546
547 try {
548 await dbus.call("/org/freedesktop/DBus", "org.freedesktop.DBus", "Hello", []);
549 assert.ok(false, "should not be reached");
550 } catch (ex) {
551 assert.equal(ex.problem, "protocol-error", "got right close code");
552 assert.equal(ex.message, "failed to connect to none bus: [Errno 2] sd_bus_start: No such file or directory",
553 "error message");
554 }
555 });
556
557 QUnit.start();