Debian Codegraph @

pkg/base1/test-framed.js

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 import cockpit from "cockpit";
3 import QUnit from "qunit-tests";
4
5 /* This is *NOT* a development guide, we take lots of shortcuts here */
6
7 /* Top level window */
8 function parent_window(assert) {
9 const done = assert.async();
10 assert.expect(7);
11 window.assert = assert; // for the child frame
12
13 document.getElementById("qunit-header").textContent = "Cockpit Parent Frame";
14 window.name = "cockpit1";
15 let initialized = false;
16 let frame;
17
18 cockpit.transport.filter(function (message, channel, control) { // eslint-disable-line array-callback-return
19 if (initialized)
20 frame.postMessage(message, cockpit.transport.origin);
21 });
22
23 window.addEventListener("message", event => {
24 const message = event.data;
25 if (message.length === 0) {
26 done();
27 } else if (message.indexOf && message.indexOf('"init"') !== -1) {
28 initialized = true;
29 frame.postMessage("\n" + JSON.stringify({
30 command: "init",
31 version: 1,
32 a: "b",
33 host: "frame_host",
34 "channel-seed": cockpit.transport.options["channel-seed"] + "1!",
35 }), cockpit.transport.origin);
36 } else {
37 const ret = cockpit.transport.inject(message);
38 if (!ret) console.error("inject failed");
39 }
40 }, false);
41
42 /* This keeps coming up in tests ... how to open the transport */
43 const chan = cockpit.channel({ payload: "resource2" });
44 chan.addEventListener("close", () => {
45 assert.equal(cockpit.transport.host, "localhost", "parent cockpit.transport.host");
46 const iframe = document.createElement("iframe");
47 iframe.setAttribute("name", "cockpit1:blah");
48 iframe.setAttribute("src", window.location.href + "?sub");
49 document.body.appendChild(iframe);
50 frame = window.frames["cockpit1:blah"];
51 });
52 }
53
54 function child_frame() {
55 const assert = window.parent.assert;
56
57 let spawn_done = false;
58 let binary_done = false;
59
60 const promise = cockpit.spawn(["/bin/sh", "-c", "echo hi"], { host: "localhost" })
61 .then(resp => {
62 assert.equal(resp, "hi\n", "framed channel got output");
63 })
64 .always(() => {
65 assert.equal(promise.state(), "resolved", "framed channel closed");
66 assert.equal(cockpit.transport.host, "frame_host", "framed cockpit.transport.host");
67 spawn_done = true;
68 if (binary_done) {
69 cockpit.transport.close();
70 }
71 });
72
73 const channel = cockpit.channel({
74 payload: "echo",
75 binary: true,
76 host: "localhost"
77 });
78 channel.addEventListener("message", function(ev, payload) {
79 assert.equal(typeof payload[0], "number", "binary channel got a byte array");
80
81 let match = true;
82 for (let i = 0; i < payload.length; i++) {
83 if (payload[i] !== i)
84 match = false;
85 }
86 assert.equal(match, true, "binary channel got back right data");
87 channel.close();
88 });
89 channel.addEventListener("close", (ev, options) => {
90 assert.notOk(options.reason, "binary channel close cleanly");
91 binary_done = true;
92 if (spawn_done)
93 cockpit.transport.close();
94 });
95
96 const view = new Array(8);
97 for (let i = 0; i < 8; i++)
98 view[i] = i;
99 channel.send(view);
100 }
101
102 if (window.parent === window) {
103 QUnit.test("framed", parent_window);
104 QUnit.start();
105 } else {
106 child_frame();
107 }