Debian Codegraph @

pkg/base1/test-dbus-common.js

raw
1 /*
2 * Copyright (C) 2015 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7 import QUnit, { skipWithPybridge } from "qunit-tests";
8
9 function deep_update(target, data) {
10 for (const prop in data) {
11 if (Object.prototype.toString.call(data[prop]) === '[object Object]') {
12 if (!target[prop])
13 target[prop] = {};
14 deep_update(target[prop], data[prop]);
15 } else {
16 target[prop] = data[prop];
17 }
18 }
19 }
20
21 export function common_dbus_tests(channel_options, bus_name) { // eslint-disable-line no-unused-vars
22 QUnit.test("call method", async assert => {
23 const dbus = cockpit.dbus(bus_name, channel_options);
24 assert.equal(typeof dbus.call, "function", "is a function");
25 const reply = await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
26 "HelloWorld", ["Browser-side JS"]);
27 assert.deepEqual(reply, ["Word! You said `Browser-side JS'. I'm Skeleton, btw!"], "reply");
28 });
29
30 QUnit.test("call method with timeout", async assert => {
31 const dbus = cockpit.dbus(bus_name, channel_options);
32 try {
33 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "NeverReturn",
34 [], { timeout: 10 });
35 assert.ok(false, "should not be reached");
36 } catch (ex) {
37 assert.ok([
38 "org.freedesktop.DBus.Error.Timeout",
39 "org.freedesktop.DBus.Error.NoReply"
40 ].indexOf(ex.name) >= 0);
41 }
42 });
43
44 QUnit.test("close immediately", assert => {
45 const done = assert.async();
46 assert.expect(1);
47 const dbus = cockpit.dbus(bus_name, channel_options);
48 dbus.addEventListener("close", (_event, options) => {
49 assert.equal(options.problem, "test-code", "got right code");
50 done();
51 });
52
53 window.setTimeout(() => dbus.close("test-code"), 100);
54 });
55
56 QUnit.test("call close", async assert => {
57 const dbus = cockpit.dbus(bus_name, channel_options);
58 try {
59 const promise = dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "HelloWorld", ["Browser-side JS"]);
60 dbus.close();
61 await promise;
62 // assert.ok(false, "should not be reached");
63 } catch (ex) {
64 assert.equal(ex.problem, "disconnected", "got right close code");
65 }
66 });
67
68 QUnit.test("call closed", async assert => {
69 const dbus = cockpit.dbus(bus_name, channel_options);
70 dbus.close("blah-blah");
71
72 try {
73 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
74 "HelloWorld", ["Browser-side JS"]);
75 assert.ok(false, "should not be reached");
76 } catch (ex) {
77 assert.equal(ex.problem, "blah-blah", "got right close code");
78 }
79 });
80
81 QUnit.test("primitive types", async assert => {
82 const dbus = cockpit.dbus(bus_name, channel_options);
83 const reply = await dbus.call(
84 "/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "TestPrimitiveTypes",
85 [10, true, 11, 12, 13, 14, 15, 16, 17, "a string", "/a/path", "asig", "ZWZnAA=="]);
86 assert.deepEqual(reply, [
87 20, false, 111, 1012, 10013, 100014, 1000015, 10000016, 17.0 / Math.PI,
88 "Word! You said `a string'. Rock'n'roll!", "/modified/a/path", "assgitasig",
89 "Ynl0ZXN0cmluZyH/AA=="
90 ], "round trip");
91 });
92
93 skipWithPybridge("integer bounds", async assert => {
94 const dbus = cockpit.dbus(bus_name, channel_options);
95
96 async function testNumber(type, value, valid) {
97 try {
98 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
99 "TestVariant", [{ t: type, v: value }]);
100 if (!valid)
101 assert.ok(false, "should not be reached");
102 } catch (ex) {
103 assert.equal(valid, false);
104 assert.equal(ex.name, "org.freedesktop.DBus.Error.InvalidArgs");
105 }
106 }
107
108 await testNumber('y', 0, true);
109 await testNumber('y', 0xff, true);
110 await testNumber('y', -1, false);
111 await testNumber('y', 0xff + 1, false);
112 await testNumber('n', -300, true);
113 await testNumber('n', 300, true);
114 await testNumber('n', -0x8000 - 1, false);
115 await testNumber('n', 0x7fff + 1, false);
116 await testNumber('q', 0, true);
117 await testNumber('q', 300, true);
118 await testNumber('q', -1, false);
119 await testNumber('q', 0xffff + 1, false);
120 await testNumber('i', -0xfffff, true);
121 await testNumber('i', 0xfffff, true);
122 await testNumber('i', -0x80000000 - 1, false);
123 await testNumber('i', 0x7fffffff + 1, false);
124 await testNumber('u', 0, true);
125 await testNumber('u', 0xfffff, true);
126 await testNumber('u', -1, false);
127 await testNumber('u', 0xffffffff + 1, false);
128 await testNumber('x', -0xfffffffff, true);
129 await testNumber('x', 0xfffffffff, true);
130 await testNumber('t', 0xfffffffff, true);
131 await testNumber('t', -1, false);
132 });
133
134 QUnit.test("non-primitive types", async assert => {
135 const dbus = cockpit.dbus(bus_name, channel_options);
136 const reply = await dbus.call(
137 "/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
138 "TestNonPrimitiveTypes", [
139 { one: "red", two: "blue" },
140 { first: [42, 42], second: [43, 43] },
141 [42, 'foo', 'bar'],
142 ["one", "two"],
143 ["/one", "/one/two"],
144 ["ass", "git"],
145 ["QUIA", "QkMA"]
146 ]);
147 assert.deepEqual(reply, [
148 "{'one': 'red', 'two': 'blue'}{'first': (42, 42), 'second': (43, 43)}(42, 'foo', 'bar')array_of_strings: [one, two] array_of_objpaths: [/one, /one/two] array_of_signatures: [signature 'ass', 'git'] array_of_bytestrings: [AB, BC] "
149 ], "round trip");
150 });
151
152 skipWithPybridge("variants", async assert => {
153 const dbus = cockpit.dbus(bus_name, channel_options);
154 const reply = await dbus.call(
155 "/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
156 "TestAsv", [{
157 one: cockpit.variant("s", "foo"),
158 two: cockpit.variant("o", "/bar"),
159 three: cockpit.variant("g", "assgit"),
160 four: cockpit.variant("y", 42),
161 five: cockpit.variant("d", 1000.0)
162 }]);
163 assert.deepEqual(reply, [
164 "{'one': <'foo'>, 'two': <objectpath '/bar'>, 'three': <signature 'assgit'>, 'four': <byte 0x2a>, 'five': <1000.0>}"
165 ], "round trip");
166 });
167
168 skipWithPybridge("bad variants", async assert => {
169 const dbus = cockpit.dbus(bus_name, channel_options);
170 try {
171 await dbus.call(
172 "/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "TestAsv",
173 [{
174 one: "foo",
175 two: "/bar",
176 three: "assgit",
177 four: 42,
178 five: 1000.0
179 }]);
180 assert.ok(false, "should not be reached");
181 } catch (ex) {
182 assert.equal(ex.name, "org.freedesktop.DBus.Error.InvalidArgs", "error name");
183 assert.equal(ex.message, "Unexpected type 'string' in argument", "error message");
184 }
185 });
186
187 QUnit.test("get all", async assert => {
188 const dbus = cockpit.dbus(bus_name, channel_options);
189 const reply = await dbus.call("/otree/frobber", "org.freedesktop.DBus.Properties", "GetAll",
190 ["com.redhat.Cockpit.DBusTests.Frobber"]);
191 assert.deepEqual(reply, [{
192 FinallyNormalName: { t: "s", v: "There aint no place like home" },
193 ReadonlyProperty: { t: "s", v: "blah" },
194 aay: { t: "aay", v: [] },
195 ag: { t: "ag", v: [] },
196 ao: { t: "ao", v: [] },
197 as: { t: "as", v: [] },
198 ay: { t: "ay", v: "QUJDYWJjAA==" },
199 b: { t: "b", v: false },
200 d: { t: "d", v: 43 },
201 g: { t: "g", v: "" },
202 i: { t: "i", v: 0 },
203 n: { t: "n", v: 0 },
204 o: { t: "o", v: "/" },
205 q: { t: "q", v: 0 },
206 s: { t: "s", v: "" },
207 t: { t: "t", v: 0 },
208 u: { t: "u", v: 0 },
209 x: { t: "x", v: 0 },
210 y: { t: "y", v: 42 }
211 }], "reply");
212 });
213
214 QUnit.test("call unimplemented", async assert => {
215 const dbus = cockpit.dbus(bus_name, channel_options);
216 try {
217 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "UnimplementedMethod", []);
218 assert.ok(false, "should not be reached");
219 } catch (ex) {
220 assert.equal(ex.name, "org.freedesktop.DBus.Error.UnknownMethod", "error name");
221 // Error message format varies between GDBus and sd-bus
222 assert.ok(ex.message.includes("UnimplementedMethod"), "error message mentions method");
223 assert.ok(ex.message.includes("com.redhat.Cockpit.DBusTests.Frobber"), "error message mentions interface");
224 }
225 });
226
227 skipWithPybridge("call bad base64", async assert => {
228 const dbus = cockpit.dbus(bus_name, channel_options);
229 try {
230 await dbus.call(
231 "/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "TestPrimitiveTypes",
232 [10, true, 11, 12, 13, 14, 15, 16, 17, "a string", "/a/path", "asig", "Yooohooo!~ bad base64"]);
233 assert.ok(false, "should not be reached");
234 } catch (ex) {
235 assert.equal(ex.name, "org.freedesktop.DBus.Error.InvalidArgs", "error name");
236 assert.equal(ex.message, "Invalid base64 in argument", "error message");
237 }
238 });
239
240 QUnit.test("call unknown", async assert => {
241 const dbus = cockpit.dbus(bus_name, channel_options);
242 try {
243 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "UnknownBlahMethod", [1]);
244 assert.ok(false, "should not be reached");
245 } catch (ex) {
246 assert.equal(ex.name, "org.freedesktop.DBus.Error.UnknownMethod", "error name");
247 assert.equal(ex.message, "Introspection data for method com.redhat.Cockpit.DBusTests.Frobber UnknownBlahMethod not available", "error message");
248 }
249 });
250
251 QUnit.test("signals", async assert => {
252 let received = false;
253 const dbus = cockpit.dbus(bus_name, channel_options);
254 dbus.subscribe({
255 interface: "com.redhat.Cockpit.DBusTests.Frobber",
256 path: "/otree/frobber"
257 }, (path, iface, signal, args) => {
258 if (received)
259 return;
260 assert.equal(path, "/otree/frobber", "got right path");
261 assert.equal(iface, "com.redhat.Cockpit.DBusTests.Frobber", "got right path");
262 assert.equal(signal, "TestSignal", "signals: got right path");
263 assert.deepEqual(args, [
264 43, ["foo", "frobber"], ["/foo", "/foo/bar"],
265 { first: [42, 42], second: [43, 43] }], "got right arguments");
266 received = true;
267 });
268
269 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "RequestSignalEmission", [0]);
270 assert.equal(received, true, "signal received");
271 });
272
273 QUnit.test("signal unsubscribe", async assert => {
274 let received = false;
275 const dbus = cockpit.dbus(bus_name, channel_options);
276
277 function on_signal() {
278 received = true;
279 }
280
281 const subscription = dbus.subscribe({
282 interface: "com.redhat.Cockpit.DBusTests.Frobber",
283 path: "/otree/frobber"
284 }, on_signal);
285
286 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "RequestSignalEmission", [0]);
287 assert.equal(received, true, "signal received");
288
289 subscription.remove();
290 received = false;
291 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "RequestSignalEmission", [0]);
292 assert.equal(received, false, "signal not received");
293 });
294
295 QUnit.test("with types", assert => {
296 const done = assert.async();
297 assert.expect(3);
298
299 const dbus = cockpit.dbus(bus_name, channel_options);
300 dbus.call("/bork", "borkety.Bork", "Echo",
301 [{ one: "red", two: "blue" }, 55, 66, 32],
302 { type: "a{ss}uit" })
303 .done(function(reply, options) {
304 assert.deepEqual(reply, [{ one: "red", two: "blue" }, 55, 66, 32], "round trip");
305 assert.equal(options.type, "a{ss}uit", "got back type");
306 })
307 .always(function() {
308 assert.equal(this.state(), "resolved", "finished successfully");
309 done();
310 });
311 });
312
313 skipWithPybridge("empty base64", assert => {
314 const done = assert.async();
315 assert.expect(3);
316
317 const dbus = cockpit.dbus(bus_name, channel_options);
318 dbus.call("/bork", "borkety.Bork", "Echo",
319 [""],
320 { type: "ay" })
321 .done(function(reply, options) {
322 assert.deepEqual(reply, [""], "round trip");
323 assert.equal(options.type, "ay", "got back type");
324 })
325 .always(function() {
326 assert.equal(this.state(), "resolved", "finished successfully");
327 done();
328 });
329 });
330
331 skipWithPybridge("bad object path", async assert => {
332 const dbus = cockpit.dbus(bus_name, channel_options);
333 try {
334 await dbus.call("invalid/path", "borkety.Bork", "Echo", [1]);
335 assert.ok(false, "should not be reached");
336 } catch (ex) {
337 assert.equal(ex.problem, "protocol-error", "error name");
338 assert.equal(ex.message, "object path is invalid in dbus \"call\": invalid/path", "error message");
339 }
340 });
341
342 skipWithPybridge("bad interface name", async assert => {
343 const dbus = cockpit.dbus(bus_name, channel_options);
344 try {
345 await dbus.call("/path", "!invalid!interface!", "Echo", [1]);
346 assert.ok(false, "should not be reached");
347 } catch (ex) {
348 assert.equal(ex.problem, "protocol-error", "error name");
349 assert.equal(ex.message, "interface name is invalid in dbus \"call\": !invalid!interface!", "error message");
350 }
351 });
352
353 skipWithPybridge("bad method name", async assert => {
354 const dbus = cockpit.dbus(bus_name, channel_options);
355 try {
356 await dbus.call("/path", "borkety.Bork", "!Invalid!Method!", [1]);
357 assert.ok(false, "should not be reached");
358 } catch (ex) {
359 assert.equal(ex.problem, "protocol-error", "error name");
360 assert.equal(ex.message, "member name is invalid in dbus \"call\": !Invalid!Method!", "error message");
361 }
362 });
363
364 skipWithPybridge("bad flags", async assert => {
365 const dbus = cockpit.dbus(bus_name, channel_options);
366 try {
367 await dbus.call("/path", "borkety.Bork", "Method", [1], { flags: 5 });
368 assert.ok(false, "should not be reached");
369 } catch (ex) {
370 assert.equal(ex.problem, "protocol-error", "error name");
371 assert.equal(ex.message, "the \"flags\" field is invalid in dbus call", "error message");
372 }
373 });
374
375 skipWithPybridge("bad types", async assert => {
376 const dbus = cockpit.dbus(bus_name, channel_options);
377 try {
378 await dbus.call("/bork", "borkety.Bork", "Echo", [1], { type: "!!%%" });
379 assert.ok(false, "should not be reached");
380 } catch (ex) {
381 assert.equal(ex.problem, "protocol-error", "error name");
382 assert.equal(ex.message, "the \"type\" signature is not valid in dbus call: !!%%", "error message");
383 }
384 });
385
386 skipWithPybridge("bad type invalid", async assert => {
387 const dbus = cockpit.dbus(bus_name, channel_options);
388 try {
389 await dbus.call("/bork", "borkety.Bork", "Echo", [1], { type: 5 }); // invalid
390 assert.ok(false, "should not be reached");
391 } catch (ex) {
392 assert.equal(ex.problem, "protocol-error", "error name");
393 assert.equal(ex.message, "the \"type\" field is invalid in call", "error message");
394 }
395 });
396
397 skipWithPybridge("bad dict type", async assert => {
398 const dbus = cockpit.dbus(bus_name, channel_options);
399 try {
400 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "Nobody",
401 [{ "!!!": "value" }], { type: "a{is}" });
402 assert.ok(false, "should not be reached");
403 } catch (ex) {
404 assert.equal(ex.name, "org.freedesktop.DBus.Error.InvalidArgs", "error name");
405 assert.equal(ex.message, "Unexpected key '!!!' in dict entry", "error message");
406 }
407 });
408
409 skipWithPybridge("bad object path", async assert => {
410 const dbus = cockpit.dbus(bus_name, channel_options);
411 try {
412 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "Nobody",
413 ["not/a/path"], { type: "o" });
414 assert.ok(false, "should not be reached");
415 } catch (ex) {
416 assert.equal(ex.name, "org.freedesktop.DBus.Error.InvalidArgs", "error name");
417 assert.equal(ex.message, "Invalid object path 'not/a/path'", "error message");
418 }
419 });
420
421 skipWithPybridge("bad signature", async assert => {
422 const dbus = cockpit.dbus(bus_name, channel_options);
423 try {
424 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "Nobody", ["bad signature"], { type: "g" });
425 assert.ok(false, "should not be reached");
426 } catch (ex) {
427 assert.equal(ex.name, "org.freedesktop.DBus.Error.InvalidArgs", "error name");
428 assert.equal(ex.message, "Invalid signature 'bad signature'", "error message");
429 }
430 });
431
432 QUnit.test("flags", assert => {
433 const done = assert.async();
434 assert.expect(3);
435
436 const dbus = cockpit.dbus(bus_name, channel_options);
437 dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
438 "HelloWorld", ["test"], { flags: "" })
439 .done(function(reply, options) {
440 assert.equal(typeof options.flags, "string", "is string");
441 assert.ok(options.flags.indexOf(">") !== -1 || options.flags.indexOf("<") !== -1, "has byte order");
442 })
443 .always(function() {
444 assert.equal(this.state(), "resolved", "finished successfully");
445 done();
446 });
447 });
448
449 QUnit.test("without introspection", async assert => {
450 const dbus = cockpit.dbus(bus_name, channel_options);
451 const reply = await dbus.call("/bork", "borkety.Bork", "Echo");
452 assert.deepEqual(reply, [], "round trip");
453 });
454
455 QUnit.test("watch path", async assert => {
456 const cache = { };
457
458 const dbus = cockpit.dbus(bus_name, channel_options);
459 const onnotify = (event, data) => deep_update(cache, data);
460 dbus.addEventListener("notify", onnotify);
461
462 await dbus.watch("/otree/frobber");
463 assert.equal(typeof cache["/otree/frobber"], "object", "has path");
464 assert.deepEqual(cache["/otree/frobber"]["com.redhat.Cockpit.DBusTests.Frobber"],
465 {
466 FinallyNormalName: "There aint no place like home",
467 ReadonlyProperty: "blah",
468 aay: [],
469 ag: [],
470 ao: [],
471 as: [],
472 ay: "QUJDYWJjAA==",
473 b: false,
474 d: 43,
475 g: "",
476 i: 0,
477 n: 0,
478 o: "/",
479 q: 0,
480 s: "",
481 t: 0,
482 u: 0,
483 x: 0,
484 y: 42
485 }, "correct data");
486 dbus.removeEventListener("notify", onnotify);
487 });
488
489 QUnit.test("watch object manager", async assert => {
490 const cache = { };
491
492 const dbus = cockpit.dbus(bus_name, channel_options);
493 const onnotify = (event, data) => deep_update(cache, data);
494 dbus.addEventListener("notify", onnotify);
495
496 await dbus.watch({ path_namespace: "/otree" });
497 assert.deepEqual(cache, {
498 "/otree/frobber": {
499 "com.redhat.Cockpit.DBusTests.Frobber":
500 {
501 FinallyNormalName: "There aint no place like home",
502 ReadonlyProperty: "blah",
503 aay: [],
504 ag: [],
505 ao: [],
506 as: [],
507 ay: "QUJDYWJjAA==",
508 b: false,
509 d: 43,
510 g: "",
511 i: 0,
512 n: 0,
513 o: "/",
514 q: 0,
515 s: "",
516 t: 0,
517 u: 0,
518 x: 0,
519 y: 42
520 }
521 }
522 }, "correct data");
523 dbus.removeEventListener("notify", onnotify);
524 });
525
526 QUnit.test("watch change", async assert => {
527 const cache = { };
528
529 const dbus = cockpit.dbus(bus_name, channel_options);
530 const onnotify_cache = (event, data) => deep_update(cache, data);
531 dbus.addEventListener("notify", onnotify_cache);
532
533 let onnotify_test_called = false;
534
535 const onnotify_test = (event, data) => {
536 assert.equal(typeof cache["/otree/frobber"], "object", "has path");
537 assert.deepEqual(cache, {
538 "/otree/frobber": {
539 "com.redhat.Cockpit.DBusTests.Frobber": {
540 FinallyNormalName: "There aint no place like home",
541 ReadonlyProperty: "blah",
542 aay: [],
543 ag: [],
544 ao: [],
545 as: [],
546 ay: "QUJDYWJjAA==",
547 b: false,
548 d: 43,
549 g: "",
550 i: 0,
551 n: 0,
552 o: "/",
553 q: 0,
554 s: "",
555 t: 0,
556 u: 0,
557 x: 0,
558 y: 42
559 }
560 }
561 }, "correct data");
562 dbus.removeEventListener("notify", onnotify_cache);
563 dbus.removeEventListener("notify", onnotify_test);
564 onnotify_test_called = true;
565 };
566 dbus.addEventListener("notify", onnotify_test);
567
568 await dbus.watch("/otree/frobber");
569 assert.ok(onnotify_test_called, "onnotify_test was called");
570 });
571
572 QUnit.test("watch barrier", async assert => {
573 const cache = { };
574
575 const dbus = cockpit.dbus(bus_name, channel_options);
576 const onnotify = (event, data) => deep_update(cache, data);
577 dbus.addEventListener("notify", onnotify);
578
579 dbus.watch({ path_namespace: "/otree" });
580
581 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "HelloWorld", ["Browser-side JS"]);
582 assert.deepEqual(cache["/otree/frobber"]["com.redhat.Cockpit.DBusTests.Frobber"],
583 {
584 FinallyNormalName: "There aint no place like home",
585 ReadonlyProperty: "blah",
586 aay: [],
587 ag: [],
588 ao: [],
589 as: [],
590 ay: "QUJDYWJjAA==",
591 b: false,
592 d: 43,
593 g: "",
594 i: 0,
595 n: 0,
596 o: "/",
597 q: 0,
598 s: "",
599 t: 0,
600 u: 0,
601 x: 0,
602 y: 42
603 }, "correct data");
604 dbus.removeEventListener("notify", onnotify);
605 });
606
607 QUnit.test("watch interfaces", async assert => {
608 const cache = { };
609
610 const dbus = cockpit.dbus(bus_name, channel_options);
611 const onnotify = (event, data) => deep_update(cache, data);
612 dbus.addEventListener("notify", onnotify);
613
614 await dbus.watch({ path_namespace: "/otree" });
615 assert.deepEqual(cache, {
616 "/otree/frobber": {
617 "com.redhat.Cockpit.DBusTests.Frobber":
618 {
619 FinallyNormalName: "There aint no place like home",
620 ReadonlyProperty: "blah",
621 aay: [],
622 ag: [],
623 ao: [],
624 as: [],
625 ay: "QUJDYWJjAA==",
626 b: false,
627 d: 43,
628 g: "",
629 i: 0,
630 n: 0,
631 o: "/",
632 q: 0,
633 s: "",
634 t: 0,
635 u: 0,
636 x: 0,
637 y: 42
638 }
639 }
640 }, "correct data");
641
642 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "AddAlpha", []);
643 assert.deepEqual(cache, {
644 "/otree/frobber": {
645 "com.redhat.Cockpit.DBusTests.Frobber":
646 {
647 FinallyNormalName: "There aint no place like home",
648 ReadonlyProperty: "blah",
649 aay: [],
650 ag: [],
651 ao: [],
652 as: [],
653 ay: "QUJDYWJjAA==",
654 b: false,
655 d: 43,
656 g: "",
657 i: 0,
658 n: 0,
659 o: "/",
660 q: 0,
661 s: "",
662 t: 0,
663 u: 0,
664 x: 0,
665 y: 42
666 },
667 "com.redhat.Cockpit.DBusTests.Alpha": {}
668 }
669 }, "correct data");
670
671 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "RemoveAlpha", []);
672 assert.deepEqual(cache, {
673 "/otree/frobber": {
674 "com.redhat.Cockpit.DBusTests.Frobber": {
675 FinallyNormalName: "There aint no place like home",
676 ReadonlyProperty: "blah",
677 aay: [],
678 ag: [],
679 ao: [],
680 as: [],
681 ay: "QUJDYWJjAA==",
682 b: false,
683 d: 43,
684 g: "",
685 i: 0,
686 n: 0,
687 o: "/",
688 q: 0,
689 s: "",
690 t: 0,
691 u: 0,
692 x: 0,
693 y: 42
694 },
695 "com.redhat.Cockpit.DBusTests.Alpha": null
696 }
697 }, "correct data");
698 dbus.removeEventListener("notify", onnotify);
699 });
700
701 skipWithPybridge("path loop", async assert => {
702 const name = "yo" + new Date().getTime();
703 const cache = { };
704
705 const dbus = cockpit.dbus(bus_name, channel_options);
706 const onnotify = (event, data) => Object.assign(cache, data);
707 dbus.addEventListener("notify", onnotify);
708
709 await dbus.watch({ path_namespace: "/cliques/" + name });
710 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "CreateClique", [name]);
711 const expect = { };
712 /* The same way mock-service.c calculates the paths */
713 for (let i = 0; i < 3; i++) {
714 expect["/cliques/" + name + "/" + i] = {
715 "com.redhat.Cockpit.DBusTests.Clique": {
716 Friend: "/cliques/" + name + "/" + (i + 1) % 3
717 }
718 };
719 }
720 assert.deepEqual(cache, expect, "got all data before method reply");
721 dbus.removeEventListener("notify", onnotify);
722 });
723
724 skipWithPybridge("path signal", async assert => {
725 const name = "yo" + new Date().getTime();
726 const cache = { };
727
728 const dbus = cockpit.dbus(bus_name, channel_options);
729 const onnotify = (event, data) => Object.assign(cache, data);
730 dbus.addEventListener("notify", onnotify);
731
732 await dbus.watch({ path: "/hidden/" + name });
733 assert.deepEqual(cache, { }, "no data yet");
734
735 dbus.subscribe({ path: "/hidden/" + name }, function(path, iface, args) {
736 assert.equal(typeof cache[path], "object", "have object");
737 assert.deepEqual(cache[path], {
738 "com.redhat.Cockpit.DBusTests.Hidden": { Name: name }
739 }, "got data before signal");
740 dbus.removeEventListener("notify", onnotify);
741 });
742 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "EmitHidden", [name]);
743 });
744
745 QUnit.test("proxy", async assert => {
746 const dbus = cockpit.dbus(bus_name, channel_options);
747 const proxy = dbus.proxy("com.redhat.Cockpit.DBusTests.Frobber", "/otree/frobber");
748 await proxy.wait();
749 assert.strictEqual(proxy.valid, true, "proxy: is valid");
750 assert.deepEqual(proxy.data, {
751 FinallyNormalName: "There aint no place like home",
752 ReadonlyProperty: "blah",
753 aay: [],
754 ag: [],
755 ao: [],
756 as: [],
757 ay: "QUJDYWJjAA==",
758 b: false,
759 d: 43,
760 g: "",
761 i: 0,
762 n: 0,
763 o: "/",
764 q: 0,
765 s: "",
766 t: 0,
767 u: 0,
768 x: 0,
769 y: 42
770 }, "correct data");
771
772 assert.strictEqual(proxy.FinallyNormalName, "There aint no place like home", "property value");
773 assert.strictEqual(proxy.ReadonlyProperty, "blah", "another property value");
774
775 assert.equal(typeof proxy.HelloWorld, "function", "has function defined");
776 const message = await proxy.HelloWorld("From a proxy");
777 assert.equal(message, "Word! You said `From a proxy'. I'm Skeleton, btw!", "method args");
778 });
779
780 QUnit.test("proxy call", async assert => {
781 const dbus = cockpit.dbus(bus_name, channel_options);
782 const proxy = dbus.proxy("com.redhat.Cockpit.DBusTests.Frobber", "/otree/frobber");
783
784 /* No wait */
785 const args = await proxy.call("HelloWorld", ["From a proxy"]);
786 assert.equal(args[0], "Word! You said `From a proxy'. I'm Skeleton, btw!", "method args");
787 });
788
789 QUnit.test("proxy call with timeout", async assert => {
790 const dbus = cockpit.dbus(bus_name, channel_options);
791 const proxy = dbus.proxy("com.redhat.Cockpit.DBusTests.Frobber", "/otree/frobber");
792
793 try {
794 await proxy.call('NeverReturn', [], { timeout: 10 });
795 assert.ok(false, "should not be reached");
796 } catch (ex) {
797 assert.ok(["org.freedesktop.DBus.Error.Timeout",
798 "org.freedesktop.DBus.Error.NoReply"].indexOf(ex.name) >= 0);
799 }
800 });
801
802 QUnit.test("proxy signal", async assert => {
803 let received = false;
804
805 const dbus = cockpit.dbus(bus_name, channel_options);
806 const proxy = dbus.proxy("com.redhat.Cockpit.DBusTests.Frobber", "/otree/frobber");
807
808 const onsignal = (event, name, args) => {
809 assert.equal(name, "TestSignal", "signals: got right name");
810 assert.deepEqual(args, [
811 43, ["foo", "frobber"], ["/foo", "/foo/bar"],
812 { first: [42, 42], second: [43, 43] }], "got right arguments");
813 received = true;
814 };
815 proxy.addEventListener("signal", onsignal);
816
817 await proxy.call("RequestSignalEmission", [0]);
818 assert.equal(received, true, "signal received");
819 proxy.removeEventListener("signal", onsignal);
820 });
821
822 QUnit.test("proxy explicit notify", async assert => {
823 const done = assert.async();
824 assert.expect(1);
825
826 const dbus = cockpit.dbus(bus_name, channel_options);
827 const proxy = dbus.proxy("com.redhat.Cockpit.DBusTests.Frobber", "/otree/frobber");
828
829 await proxy.wait();
830 const onchanged = () => {
831 assert.equal(proxy.FinallyNormalName, "externally injected");
832 proxy.removeEventListener("changed", onchanged);
833 done();
834 };
835 proxy.addEventListener("changed", onchanged);
836
837 dbus.notify({
838 "/otree/frobber": {
839 "com.redhat.Cockpit.DBusTests.Frobber": {
840 FinallyNormalName: "externally injected"
841 }
842 }
843 });
844 });
845
846 QUnit.test("proxies", async assert => {
847 const dbus = cockpit.dbus(bus_name, channel_options);
848
849 /* Just some cleanup */
850 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "DeleteAllObjects", []);
851 const proxies = dbus.proxies("com.redhat.Cockpit.DBusTests.Frobber", "/otree");
852 await proxies.wait();
853
854 let added;
855 proxies.addEventListener("added", function(event, proxy) {
856 added = proxy;
857 assert.strictEqual(added.valid, true, "added objects valid");
858 });
859
860 let changed;
861 proxies.addEventListener("changed", function(event, proxy) {
862 changed = proxy;
863 });
864
865 let removed;
866 proxies.addEventListener("removed", function(event, proxy) {
867 removed = proxy;
868 });
869
870 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "CreateObject", ["/otree/other"]);
871 assert.equal(typeof added, "object", "got added object");
872 assert.equal(typeof changed, "object", "no changed object yet");
873 assert.equal(typeof removed, "undefined", "no removed object yet");
874 assert.equal(added.path, "/otree/other", "added object correct");
875 assert.strictEqual(added, changed, "added fires changed");
876
877 changed = null;
878 await dbus.call(added.path, added.iface, "RequestPropertyMods", []);
879 assert.strictEqual(changed, added, "change fired");
880
881 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "DeleteObject", ["/otree/other"]);
882 assert.strictEqual(removed, added, "removed fired");
883 assert.strictEqual(removed.valid, false, "removed is invalid");
884 dbus.close();
885 });
886 }
887
888 export function dbus_track_tests(channel_options, bus_name) {
889 QUnit.test("track name", async assert => {
890 const name = "yo.x" + new Date().getTime();
891
892 const dbus = cockpit.dbus(bus_name, channel_options);
893 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "ClaimOtherName", [name]);
894
895 const other = cockpit.dbus(name, {
896 bus: channel_options.bus,
897 address: channel_options.address,
898 track: true
899 });
900 let gone = false;
901 other.addEventListener("close", (_event, data) => {
902 assert.strictEqual(data.problem, undefined, "no problem");
903 gone = true;
904 });
905 await other.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "HelloWorld", ["test"]);
906 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "ReleaseOtherName", [name]);
907 assert.strictEqual(gone, true, "close handler was called");
908 });
909
910 QUnit.test("no track name", async assert => {
911 const name = "yo.y" + new Date().getTime();
912
913 const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.Test", channel_options);
914 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "ClaimOtherName", [name]);
915
916 const other = cockpit.dbus(name, channel_options);
917 let gone = false;
918 other.addEventListener("close", () => { gone = true });
919
920 await other.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "HelloWorld", ["test"]);
921 await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "ReleaseOtherName", [name]);
922 try {
923 await other.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "HelloWorld", ["test"]);
924 assert.ok(false, "call after release should fail");
925 } catch (ex) {
926 assert.equal(ex.name, "org.freedesktop.DBus.Error.ServiceUnknown");
927 }
928 assert.equal(gone, false, "is not gone");
929 });
930
931 skipWithPybridge("receive readable fd", async assert => {
932 const done = assert.async();
933 assert.expect(3);
934
935 const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.Test", channel_options);
936 const [fd] = await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
937 "MakeTestFd", ["readable"]);
938 assert.equal(typeof (fd.internal), 'string');
939 assert.equal(fd.payload, 'stream');
940
941 const channel = cockpit.channel(fd);
942
943 channel.onmessage = (_event, data) => {
944 assert.equal(data, 'Hello, fd');
945 channel.close();
946 done();
947 };
948 });
949
950 skipWithPybridge("receive readable fd and ensure opening more than once fails", async assert => {
951 const done = assert.async();
952 assert.expect(6);
953
954 const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.Test", channel_options);
955 const [fd] = await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
956 "MakeTestFd", ["readable"]);
957 assert.equal(typeof (fd.internal), 'string');
958 assert.equal(fd.payload, 'stream');
959
960 const channel1 = cockpit.channel(fd);
961 assert.ok(channel1);
962 const channel2 = cockpit.channel(fd);
963
964 channel2.onclose = (_event, options) => {
965 assert.equal(options.channel, channel2.id);
966 assert.equal(options.command, 'close');
967 assert.equal(options.problem, 'not-found');
968 done();
969 };
970 });
971
972 skipWithPybridge("receive readable fd and ensure writing fails", async assert => {
973 const done = assert.async();
974 assert.expect(5);
975
976 const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.Test", channel_options);
977 const [fd] = await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
978 "MakeTestFd", ["readable"]);
979 assert.equal(typeof (fd.internal), 'string');
980 assert.equal(fd.payload, 'stream');
981
982 const channel = cockpit.channel(fd);
983 channel.send('Hello, fd');
984
985 channel.onclose = (_event, options) => {
986 assert.equal(options.channel, channel.id);
987 assert.equal(options.command, 'close');
988 assert.equal(options.problem, 'protocol-error');
989 done();
990 };
991 });
992
993 skipWithPybridge("receive writable fd", async assert => {
994 const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.Test", channel_options);
995 const [fd] = await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
996 "MakeTestFd", ["writable"]);
997 assert.equal(typeof (fd.internal), 'string');
998 assert.equal(fd.payload, 'stream');
999
1000 const channel = cockpit.channel(fd);
1001 channel.send('Hello, fd');
1002 channel.close();
1003 });
1004 }