| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import cockpit from "cockpit"; |
| 3 |
import QUnit from "qunit-tests"; |
| 4 |
|
| 5 |
function MockPeer() { |
| 6 |
/* |
| 7 |
* Events triggered here: |
| 8 |
* open(event, args) |
| 9 |
* recv(event, payload) |
| 10 |
* close(event, problem) |
| 11 |
* get(event, path) |
| 12 |
*/ |
| 13 |
cockpit.event_target(this); |
| 14 |
|
| 15 |
/* open: triggered when mock Channel is created */ |
| 16 |
this.onopened = function(event, channel, options) { |
| 17 |
/* nada */ |
| 18 |
}; |
| 19 |
|
| 20 |
/* close event: triggered when mock Channel is closed */ |
| 21 |
this.onclosed = function(event, channel, options) { |
| 22 |
/* nada */ |
| 23 |
}; |
| 24 |
|
| 25 |
this.oncontrol = function(event, channel, options) { |
| 26 |
/* nada */ |
| 27 |
}; |
| 28 |
|
| 29 |
/* send a message from peer back to channel */ |
| 30 |
this.send = function(channel, payload) { |
| 31 |
if (typeof (payload) != "string") |
| 32 |
payload = String(payload); |
| 33 |
window.setTimeout(function() { |
| 34 |
if (channel.valid) |
| 35 |
channel.dispatchEvent("message", payload); |
| 36 |
else |
| 37 |
console.log("dropping message after close from MockPeer"); |
| 38 |
}, 5); |
| 39 |
}; |
| 40 |
|
| 41 |
/* peer closes the channel */ |
| 42 |
this.close = function(channel, options) { |
| 43 |
console.assert(channel); |
| 44 |
window.setTimeout(function() { |
| 45 |
if (channel.valid) { |
| 46 |
channel.valid = false; |
| 47 |
channel.dispatchEvent("close", options || { }); |
| 48 |
} |
| 49 |
}, 5); |
| 50 |
}; |
| 51 |
|
| 52 |
const peer = this; |
| 53 |
let last_channel = 0; |
| 54 |
|
| 55 |
function MockChannel(options) { |
| 56 |
cockpit.event_target(this); |
| 57 |
this.number = last_channel++; |
| 58 |
this.options = options; |
| 59 |
this.valid = true; |
| 60 |
|
| 61 |
const channel = this; |
| 62 |
|
| 63 |
function Transport() { |
| 64 |
this.close = function(problem) { console.assert(arguments.length == 1) }; |
| 65 |
} |
| 66 |
|
| 67 |
this.transport = new Transport(); |
| 68 |
|
| 69 |
this.send = function(payload) { |
| 70 |
console.assert(arguments.length == 1); |
| 71 |
console.assert(this.valid); |
| 72 |
window.setTimeout(function() { peer.dispatchEvent("recv", channel, payload) }, 5); |
| 73 |
}; |
| 74 |
|
| 75 |
this.control = function(options) { |
| 76 |
console.assert(typeof options.command === 'string'); |
| 77 |
console.assert(options !== null && typeof options === 'object'); |
| 78 |
console.assert(arguments.length == 1); |
| 79 |
window.setTimeout(function() { peer.dispatchEvent("control", channel, options) }, 5); |
| 80 |
}; |
| 81 |
|
| 82 |
this.close = function(options) { |
| 83 |
console.assert(arguments.length <= 1); |
| 84 |
this.valid = false; |
| 85 |
window.setTimeout(function() { peer.dispatchEvent("closed", channel, options || { }) }, 5); |
| 86 |
this.dispatchEvent("close", options || { }); |
| 87 |
}; |
| 88 |
|
| 89 |
this.buffer = function(callback) { |
| 90 |
const buffers = []; |
| 91 |
buffers.callback = callback; |
| 92 |
buffers.squash = function squash() { |
| 93 |
return buffers.join(""); |
| 94 |
}; |
| 95 |
|
| 96 |
this.onmessage = function(event, data) { |
| 97 |
buffers.push(data); |
| 98 |
if (buffers.callback) { |
| 99 |
const block = buffers.squash(); |
| 100 |
if (block.length > 0) { |
| 101 |
const consumed = buffers.callback.call(this, block); |
| 102 |
if (typeof consumed !== "number" || consumed === block.length) { |
| 103 |
buffers.length = 0; |
| 104 |
} else { |
| 105 |
buffers.length = 1; |
| 106 |
buffers[0] = block.substring(consumed); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
return buffers; |
| 113 |
}; |
| 114 |
|
| 115 |
QUnit.testDone(function() { |
| 116 |
channel.valid = false; |
| 117 |
}); |
| 118 |
|
| 119 |
peer.dispatchEvent("opened", channel, options); |
| 120 |
} |
| 121 |
|
| 122 |
cockpit.channel = function(options) { |
| 123 |
return new MockChannel(options); |
| 124 |
}; |
| 125 |
} |
| 126 |
|
| 127 |
QUnit.test("public api", function (assert) { |
| 128 |
assert.equal(typeof cockpit.spawn, "function", "spawn is a function"); |
| 129 |
}); |
| 130 |
|
| 131 |
QUnit.test("simple request", async assert => { |
| 132 |
const peer = new MockPeer(); |
| 133 |
peer.addEventListener("opened", function(event, channel, options) { |
| 134 |
assert.deepEqual(channel.options.spawn, ["/the/path", "arg1", "arg2"], "passed spawn correctly"); |
| 135 |
assert.equal(channel.options.host, undefined, "had no host"); |
| 136 |
}); |
| 137 |
peer.addEventListener("recv", function(event, channel, payload) { |
| 138 |
assert.equal(payload, "input", "had input"); |
| 139 |
this.send(channel, "output"); |
| 140 |
this.close(channel); |
| 141 |
}); |
| 142 |
|
| 143 |
const resp = await cockpit.spawn(["/the/path", "arg1", "arg2"]) |
| 144 |
.input("input", true); |
| 145 |
assert.deepEqual(resp, "output", "returned right json"); |
| 146 |
}); |
| 147 |
|
| 148 |
QUnit.test("input large", function (assert) { |
| 149 |
const done = assert.async(); |
| 150 |
assert.expect(25); |
| 151 |
|
| 152 |
const str = new Array(128 * 1024).join('abcdef12345'); |
| 153 |
let output = ""; |
| 154 |
let count = 0; |
| 155 |
|
| 156 |
const peer = new MockPeer(); |
| 157 |
peer.addEventListener("recv", function(event, channel, payload) { |
| 158 |
assert.ok(typeof (payload) == "string", "got payload"); |
| 159 |
output += payload; |
| 160 |
count += 1; |
| 161 |
}); |
| 162 |
peer.addEventListener("control", function(event, channel, options) { |
| 163 |
if (options.command == "done") |
| 164 |
this.close(channel); |
| 165 |
}); |
| 166 |
|
| 167 |
cockpit.spawn(["/path/to/command"]) |
| 168 |
.input(str) |
| 169 |
.always(function() { |
| 170 |
assert.equal(this.state(), "resolved", "didn't fail"); |
| 171 |
assert.equal(str, output, "right output"); |
| 172 |
assert.ok(count > 1, "broken into multiple blocks"); |
| 173 |
done(); |
| 174 |
}); |
| 175 |
}); |
| 176 |
|
| 177 |
QUnit.test("binary large", function (assert) { |
| 178 |
const done = assert.async(); |
| 179 |
assert.expect(10); |
| 180 |
|
| 181 |
const data = new Uint8Array(249 * 1023); |
| 182 |
const len = data.byteLength; |
| 183 |
for (let i = 0; i < len; i++) |
| 184 |
data[i] = i % 233; |
| 185 |
|
| 186 |
let count = 0; |
| 187 |
|
| 188 |
const peer = new MockPeer(); |
| 189 |
peer.addEventListener("recv", function(event, channel, payload) { |
| 190 |
console.log(typeof (payload), payload.constructor); |
| 191 |
assert.equal(typeof (payload), "object", "got payload"); |
| 192 |
assert.equal(payload.constructor, Uint8Array, "right binary array"); |
| 193 |
count += 1; |
| 194 |
}); |
| 195 |
peer.addEventListener("control", function(event, channel, options) { |
| 196 |
console.log("control", options); |
| 197 |
if (options.command == "done") |
| 198 |
this.close(channel); |
| 199 |
}); |
| 200 |
|
| 201 |
cockpit.spawn(["/ptah/to/command"]) |
| 202 |
.input(data) |
| 203 |
.always(function() { |
| 204 |
assert.equal(this.state(), "resolved", "didn't fail"); |
| 205 |
assert.ok(count > 1, "broken into multiple blocks"); |
| 206 |
done(); |
| 207 |
}); |
| 208 |
}); |
| 209 |
|
| 210 |
QUnit.test("string command", function (assert) { |
| 211 |
const done = assert.async(); |
| 212 |
assert.expect(2); |
| 213 |
|
| 214 |
const peer = new MockPeer(); |
| 215 |
peer.addEventListener("opened", function(event, channel, options) { |
| 216 |
assert.deepEqual(channel.options.spawn, ["/the/path"], "passed spawn correctly"); |
| 217 |
assert.equal(channel.options.host, "hostname", "had host"); |
| 218 |
done(); |
| 219 |
}); |
| 220 |
|
| 221 |
cockpit.spawn("/the/path", { host: "hostname" }); |
| 222 |
}); |
| 223 |
|
| 224 |
QUnit.test("channel options", function (assert) { |
| 225 |
const done = assert.async(); |
| 226 |
assert.expect(1); |
| 227 |
|
| 228 |
const peer = new MockPeer(); |
| 229 |
peer.addEventListener("opened", function(event, channel) { |
| 230 |
assert.deepEqual(channel.options, { |
| 231 |
spawn: ["/the/path", "arg"], |
| 232 |
host: "the-other-host.example.com", |
| 233 |
"extra-option": "zerogjuggs", |
| 234 |
payload: "stream" |
| 235 |
}, "sent correctly"); |
| 236 |
done(); |
| 237 |
}); |
| 238 |
|
| 239 |
/* Don't care about the result ... */ |
| 240 |
const options = { "extra-option": "zerogjuggs", host: "the-other-host.example.com" }; |
| 241 |
cockpit.spawn(["/the/path", "arg"], options); |
| 242 |
}); |
| 243 |
|
| 244 |
QUnit.test("streaming", assert => { |
| 245 |
const done = assert.async(); |
| 246 |
assert.expect(15); |
| 247 |
|
| 248 |
const peer = new MockPeer(); |
| 249 |
peer.addEventListener("opened", function(event, channel) { |
| 250 |
for (let i = 0; i < 10; i++) |
| 251 |
this.send(channel, String(i)); |
| 252 |
this.close(channel); |
| 253 |
}); |
| 254 |
|
| 255 |
let at = 0; |
| 256 |
const promise = cockpit.spawn(["/unused"]) |
| 257 |
.stream(function(resp) { |
| 258 |
assert.equal(String(at), resp, "stream got right data"); |
| 259 |
if (at === 0) |
| 260 |
assert.strictEqual(this, promise, "stream got right this"); |
| 261 |
at++; |
| 262 |
}) |
| 263 |
.done(function(resp) { |
| 264 |
assert.ok(!resp, "stream didn't send data to done"); |
| 265 |
assert.strictEqual(this, promise, "done got right this"); |
| 266 |
}) |
| 267 |
.always(function() { |
| 268 |
assert.equal(this.state(), "resolved", "split response didn't fail"); |
| 269 |
assert.strictEqual(this, promise, "always got right this"); |
| 270 |
done(); |
| 271 |
}); |
| 272 |
}); |
| 273 |
|
| 274 |
QUnit.test("with problem", async assert => { |
| 275 |
const peer = new MockPeer(); |
| 276 |
peer.addEventListener("opened", (_event, channel) => { |
| 277 |
peer.close(channel, { problem: "not-found" }); |
| 278 |
}); |
| 279 |
|
| 280 |
try { |
| 281 |
await cockpit.spawn("/unused"); |
| 282 |
assert.ok(false, "should not be reached"); |
| 283 |
} catch (ex) { |
| 284 |
assert.equal(ex.problem, "not-found", "got problem"); |
| 285 |
assert.strictEqual(ex.exit_signal, null, "got no signal"); |
| 286 |
assert.strictEqual(ex.exit_status, null, "got no status"); |
| 287 |
} |
| 288 |
}); |
| 289 |
|
| 290 |
QUnit.test("with status", function (assert) { |
| 291 |
const done = assert.async(); |
| 292 |
assert.expect(5); |
| 293 |
|
| 294 |
const peer = new MockPeer(); |
| 295 |
peer.addEventListener("opened", function(event, channel) { |
| 296 |
peer.send(channel, "the data"); |
| 297 |
peer.close(channel, { "exit-status": 5 }); |
| 298 |
}); |
| 299 |
|
| 300 |
cockpit.spawn("/unused") |
| 301 |
.fail(function(ex, data) { |
| 302 |
assert.strictEqual(ex.problem, null, "got null problem"); |
| 303 |
assert.strictEqual(ex.exit_signal, null, "got no signal"); |
| 304 |
assert.strictEqual(ex.exit_status, 5, "got status"); |
| 305 |
assert.equal(data, "the data", "got data even with exit status"); |
| 306 |
}) |
| 307 |
.always(function() { |
| 308 |
assert.equal(this.state(), "rejected", "should fail"); |
| 309 |
done(); |
| 310 |
}); |
| 311 |
}); |
| 312 |
|
| 313 |
QUnit.test("with signal", function (assert) { |
| 314 |
const done = assert.async(); |
| 315 |
assert.expect(5); |
| 316 |
|
| 317 |
const peer = new MockPeer(); |
| 318 |
peer.addEventListener("opened", function(event, channel) { |
| 319 |
peer.send(channel, "signal data here"); |
| 320 |
peer.close(channel, { "exit-signal": "TERM" }); |
| 321 |
}); |
| 322 |
|
| 323 |
cockpit.spawn("/unused") |
| 324 |
.fail(function(ex, data) { |
| 325 |
assert.strictEqual(ex.problem, null, "got null problem"); |
| 326 |
assert.strictEqual(ex.exit_signal, "TERM", "got signal"); |
| 327 |
assert.strictEqual(ex.exit_status, null, "got no status"); |
| 328 |
assert.equal(data, "signal data here", "got data even with signal"); |
| 329 |
}) |
| 330 |
.always(function() { |
| 331 |
assert.equal(this.state(), "rejected", "should fail"); |
| 332 |
done(); |
| 333 |
}); |
| 334 |
}); |
| 335 |
|
| 336 |
QUnit.test("spawn promise recursive", function (assert) { |
| 337 |
assert.expect(7); |
| 338 |
|
| 339 |
const promise = cockpit.spawn(["/the/path", "arg1", "arg2"]); |
| 340 |
|
| 341 |
const target = { }; |
| 342 |
const promise2 = promise.promise(target); |
| 343 |
assert.strictEqual(promise2, target, "used target"); |
| 344 |
assert.equal(typeof promise2.done, "function", "promise2.done()"); |
| 345 |
assert.equal(typeof promise2.promise, "function", "promise2.promise()"); |
| 346 |
assert.equal(typeof promise2.input, "function", "promise2.input()"); |
| 347 |
|
| 348 |
const promise3 = promise2.promise(); |
| 349 |
assert.equal(typeof promise3.done, "function", "promise3.done()"); |
| 350 |
assert.equal(typeof promise3.promise, "function", "promise3.promise()"); |
| 351 |
assert.equal(typeof promise3.input, "function", "promise3.input()"); |
| 352 |
}); |
| 353 |
|
| 354 |
QUnit.start(); |