| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import cockpit from "cockpit"; |
| 3 |
import QUnit from "qunit-tests"; |
| 4 |
|
| 5 |
/* Set this to a regexp to ignore that warning once */ |
| 6 |
function console_ignore_log(exp) { |
| 7 |
const console_log = console.log; |
| 8 |
console.log = function() { |
| 9 |
if (!exp.exec(arguments[0])) |
| 10 |
console_log.apply(console, arguments); |
| 11 |
console.log = console_log; |
| 12 |
}; |
| 13 |
} |
| 14 |
|
| 15 |
/* The other end of the mock websocket */ |
| 16 |
function MockPeer() { |
| 17 |
const self = this; |
| 18 |
const echos = { }; |
| 19 |
const nulls = { }; |
| 20 |
const queue = []; |
| 21 |
let pending = false; |
| 22 |
|
| 23 |
cockpit.event_target(this); |
| 24 |
|
| 25 |
/* These are events */ |
| 26 |
self.onopen = function(event) { }; |
| 27 |
self.onrecv = function(event, channel, payload) { |
| 28 |
/* A rudimentary echo channel implementation */ |
| 29 |
if (channel) { |
| 30 |
if (channel in echos || channel in nulls) |
| 31 |
queue.push([channel, payload]); |
| 32 |
} else { |
| 33 |
const command = JSON.parse(payload); |
| 34 |
if (command.command == "open") { |
| 35 |
let reply; |
| 36 |
if (command.payload == "echo") { |
| 37 |
echos[command.channel] = true; |
| 38 |
reply = { |
| 39 |
command: "ready", |
| 40 |
channel: command.channel |
| 41 |
}; |
| 42 |
} else if (command.payload == "null") { |
| 43 |
nulls[command.channel] = true; |
| 44 |
reply = { |
| 45 |
command: "ready", |
| 46 |
channel: command.channel |
| 47 |
}; |
| 48 |
} else { |
| 49 |
reply = { |
| 50 |
command: "close", |
| 51 |
channel: command.channel, |
| 52 |
problem: "not-supported", |
| 53 |
}; |
| 54 |
} |
| 55 |
queue.push([null, JSON.stringify(reply)]); |
| 56 |
} else if (command.command == "close") { |
| 57 |
delete echos[command.channel]; |
| 58 |
delete nulls[command.channel]; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
if (queue.length && !pending) { |
| 63 |
pending = true; |
| 64 |
window.setTimeout(function() { |
| 65 |
while (queue.length) { |
| 66 |
self.send.apply(self, queue.shift()); |
| 67 |
} |
| 68 |
pending = false; |
| 69 |
}, 0); |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
/* Methods filled in by MockWebSocket */ |
| 74 |
self.send = function(channel, payload) { throw Error("not reached") }; |
| 75 |
self.close = function(options) { throw Error("not reached") }; |
| 76 |
} |
| 77 |
|
| 78 |
window.mock = { url: "ws://url" }; |
| 79 |
let force_default_host = null; |
| 80 |
let mock_peer = new MockPeer(); |
| 81 |
|
| 82 |
QUnit.testDone(function() { |
| 83 |
mock_peer = new MockPeer(); |
| 84 |
cockpit.transport.close(); |
| 85 |
}); |
| 86 |
|
| 87 |
/* Mock WebSocket */ |
| 88 |
function MockWebSocket(url, protocol) { |
| 89 |
if (typeof url != "string") |
| 90 |
throw Error("WebSocket(@url) is not a string: " + typeof url); |
| 91 |
if (typeof protocol != "string") |
| 92 |
throw Error("WebSocket(@protocol) is not a string: " + typeof protocol); |
| 93 |
|
| 94 |
this.onopen = function(event) { }; |
| 95 |
this.onclose = function(event) { }; |
| 96 |
this.onmessage = function(event) { }; |
| 97 |
this.onerror = function(event) { }; |
| 98 |
this.readyState = 0; |
| 99 |
this.url = url; |
| 100 |
this.protocol = protocol; |
| 101 |
this.extensions = ""; |
| 102 |
this.binaryType = null; |
| 103 |
|
| 104 |
const ws = this; |
| 105 |
const mock = mock_peer; |
| 106 |
|
| 107 |
this.send = function(data) { |
| 108 |
if (typeof data != "string") |
| 109 |
throw Error("WebSocket.send(@data) is not a string: " + typeof data); |
| 110 |
const pos = data.indexOf("\n"); |
| 111 |
if (pos == -1) |
| 112 |
throw Error("Invalid frame sent to WebSocket: " + data); |
| 113 |
const channel = data.substring(0, pos); |
| 114 |
const payload = data.substring(pos + 1); |
| 115 |
window.setTimeout(function() { mock.dispatchEvent("recv", channel, payload) }, 5); |
| 116 |
}; |
| 117 |
|
| 118 |
this.close = function(code, reason) { |
| 119 |
if (typeof code != "number" && typeof code != "undefined") |
| 120 |
throw Error("WebSocket.close(@code) is not a number: " + typeof code); |
| 121 |
if (typeof reason != "string" && typeof reason != "undefined") |
| 122 |
throw Error("WebSocket.close(@reason) is not a number: " + typeof reason); |
| 123 |
if (this.readyState > 1) |
| 124 |
throw Error("WebSocket.close() called on a closed WebSocket" + this.readyState + " " + code + reason); |
| 125 |
this.readyState = 3; |
| 126 |
this.onclose({ name: "close", code: code || 1000, reason, wasClean: true }); |
| 127 |
}; |
| 128 |
|
| 129 |
/* Instantiate the global mock peer */ |
| 130 |
const sending = []; |
| 131 |
mock.send = function(channel, payload) { |
| 132 |
if (!channel) |
| 133 |
channel = ""; |
| 134 |
const event = { |
| 135 |
name: "message", |
| 136 |
data: channel.toString() + "\n" + payload |
| 137 |
}; |
| 138 |
sending.push(event); |
| 139 |
window.setTimeout(function() { |
| 140 |
if (ws.readyState == 1) |
| 141 |
ws.onmessage(sending.shift()); |
| 142 |
}, 5); |
| 143 |
}; |
| 144 |
|
| 145 |
mock.close = function(options) { |
| 146 |
if (!options) |
| 147 |
options = { }; |
| 148 |
window.setTimeout(function() { |
| 149 |
ws.close(options.reason ? 1000 : 1011, options.reason || ""); |
| 150 |
}, 5); |
| 151 |
}; |
| 152 |
|
| 153 |
/* Open shortly */ |
| 154 |
window.setTimeout(function() { |
| 155 |
ws.readyState = 1; |
| 156 |
mock.dispatchEvent("open"); |
| 157 |
ws.onopen({ name: "open" }); |
| 158 |
const init = { |
| 159 |
command: "init", |
| 160 |
version: 1, |
| 161 |
"channel-seed": "test", |
| 162 |
"csrf-token": "the-csrf-token", |
| 163 |
user: { |
| 164 |
user: "scruffy", |
| 165 |
name: "Scruffy the Janitor" |
| 166 |
}, |
| 167 |
system: { |
| 168 |
version: "zero.point.zero", |
| 169 |
build: "nasty stuff", |
| 170 |
} |
| 171 |
}; |
| 172 |
if (force_default_host) |
| 173 |
init.host = force_default_host; |
| 174 |
force_default_host = null; |
| 175 |
ws.onmessage({ data: "\n" + JSON.stringify(init) }); |
| 176 |
}, 5); |
| 177 |
} |
| 178 |
|
| 179 |
WebSocket = MockWebSocket; // eslint-disable-line no-global-assign |
| 180 |
|
| 181 |
function check_transport (assert, base_url, application, socket, url_root) { |
| 182 |
const old_url = window.mock.url; |
| 183 |
const arr = [base_url]; |
| 184 |
if (base_url.slice(-1) == '/') |
| 185 |
arr.push(base_url + "other"); |
| 186 |
else |
| 187 |
arr.push(base_url + '/', base_url + '/other'); |
| 188 |
|
| 189 |
window.mock.url = null; |
| 190 |
window.mock.url_root = url_root; |
| 191 |
for (let i = 0; i < arr.length; i++) { |
| 192 |
window.mock.pathname = arr[i]; |
| 193 |
assert.equal(cockpit.transport.application(), application, |
| 194 |
arr[i] + " transport.application is " + socket); |
| 195 |
assert.equal(cockpit.transport.uri(), "ws://" + window.location.host + socket, |
| 196 |
arr[i] + " transport.uri is " + socket); |
| 197 |
} |
| 198 |
|
| 199 |
window.mock.url = old_url; |
| 200 |
window.mock.url_root = null; |
| 201 |
window.mock.pathname = null; |
| 202 |
} |
| 203 |
|
| 204 |
QUnit.test("public api", function (assert) { |
| 205 |
const channel = cockpit.channel({ host: "host.example.com" }); |
| 206 |
assert.equal(typeof channel, "object", "cockpit.channel() constructor"); |
| 207 |
assert.equal(channel.options.host, "host.example.com", "channel.options is dict"); |
| 208 |
assert.ok(channel.id !== undefined, "channel.id is a field"); |
| 209 |
assert.ok(channel.toString().indexOf("host.example.com") > 0, "channel.toString()"); |
| 210 |
assert.equal(typeof channel.send, "function", "channel.send() is a function"); |
| 211 |
assert.equal(typeof channel.close, "function", "channel.close() is a function"); |
| 212 |
assert.strictEqual(channel.valid, true, "channel.valid is set"); |
| 213 |
assert.equal(typeof cockpit.logout, "function", "cockpit.logout is a function"); |
| 214 |
assert.equal(typeof cockpit.transport, "object", "cockpit.transport is an object"); |
| 215 |
assert.equal(typeof cockpit.transport.close, "function", "cockpit.transport.close is a function"); |
| 216 |
assert.equal(typeof cockpit.transport.options, "object", "cockpit.transport.options is a object"); |
| 217 |
|
| 218 |
if (window.location.origin) |
| 219 |
assert.equal(cockpit.transport.origin, window.location.origin, "cockpit.transport.origin is correct"); |
| 220 |
else |
| 221 |
assert.equal(typeof cockpit.transport.origin, "string", "cockpit.transport.origin is present"); |
| 222 |
|
| 223 |
check_transport(assert, '/', 'cockpit', '/cockpit/socket'); |
| 224 |
check_transport(assert, '/cockpit', 'cockpit', '/cockpit/socket'); |
| 225 |
check_transport(assert, '/cockpitother/', 'cockpit', '/cockpit/socket'); |
| 226 |
check_transport(assert, '/cockpita+pplication/', 'cockpit', '/cockpit/socket'); |
| 227 |
check_transport(assert, '/cockpit+application', 'cockpit+application', '/cockpit+application/socket'); |
| 228 |
check_transport(assert, '/=machine', 'cockpit+=machine', '/cockpit+=machine/socket'); |
| 229 |
check_transport(assert, '/url-root', 'cockpit', '/url-root/cockpit/socket', 'url-root'); |
| 230 |
check_transport(assert, '/url-root/cockpit', 'cockpit', '/url-root/cockpit/socket', 'url-root'); |
| 231 |
check_transport(assert, '/url-root/cockpit+application', 'cockpit+application', |
| 232 |
'/url-root/cockpit+application/socket', 'url-root'); |
| 233 |
check_transport(assert, '/url-root/=machine', 'cockpit+=machine', '/url-root/cockpit+=machine/socket', 'url-root'); |
| 234 |
}); |
| 235 |
|
| 236 |
QUnit.test("open channel", function (assert) { |
| 237 |
const done = assert.async(); |
| 238 |
assert.expect(8); |
| 239 |
|
| 240 |
const channel = cockpit.channel({ host: "scruffy" }); |
| 241 |
let is_inited = false; |
| 242 |
mock_peer.addEventListener("open", function(event) { |
| 243 |
assert.ok(true, "websocket connected"); |
| 244 |
}); |
| 245 |
mock_peer.addEventListener("recv", function(event, chan, payload) { |
| 246 |
const command = JSON.parse(payload); |
| 247 |
if (!is_inited) { |
| 248 |
assert.equal(typeof command, "object", "valid json"); |
| 249 |
assert.strictEqual(chan, "", "sent with empty channel"); |
| 250 |
assert.equal(command.command, "init", "got init"); |
| 251 |
assert.equal(command.version, 1, "got init version"); |
| 252 |
is_inited = true; |
| 253 |
} else { |
| 254 |
assert.equal(command.command, "open", "right command"); |
| 255 |
assert.strictEqual(command.channel, channel.id, "contains right channel"); |
| 256 |
assert.equal(command.host, "scruffy", "host as expected"); |
| 257 |
done(); |
| 258 |
} |
| 259 |
}); |
| 260 |
}); |
| 261 |
|
| 262 |
QUnit.test("multiple", function (assert) { |
| 263 |
const done = assert.async(); |
| 264 |
assert.expect(1); |
| 265 |
|
| 266 |
const channel = cockpit.channel({ host: "scruffy" }); |
| 267 |
const channelb = cockpit.channel({ host: "amy" }); |
| 268 |
|
| 269 |
const onrecv = event => { |
| 270 |
mock_peer.removeEventListener("recv", onrecv); |
| 271 |
assert.notStrictEqual(channel.id, channelb.id, "channels have different ids"); |
| 272 |
done(); |
| 273 |
}; |
| 274 |
mock_peer.addEventListener("recv", onrecv); |
| 275 |
}); |
| 276 |
|
| 277 |
QUnit.test("open no host", function (assert) { |
| 278 |
const done = assert.async(); |
| 279 |
assert.expect(3); |
| 280 |
|
| 281 |
const channel = cockpit.channel({ }); |
| 282 |
assert.ok(channel); |
| 283 |
mock_peer.addEventListener("open", function(event) { |
| 284 |
assert.ok(true, "websocket connected"); |
| 285 |
}); |
| 286 |
mock_peer.addEventListener("recv", function(event, chan, payload) { |
| 287 |
const command = JSON.parse(payload); |
| 288 |
if (command.command == "open") { |
| 289 |
assert.strictEqual(command.host, undefined, "host not included"); |
| 290 |
done(); |
| 291 |
} |
| 292 |
}); |
| 293 |
}); |
| 294 |
|
| 295 |
QUnit.test("open auto host", function (assert) { |
| 296 |
const done = assert.async(); |
| 297 |
assert.expect(3); |
| 298 |
|
| 299 |
force_default_host = "planetexpress"; |
| 300 |
const channel = cockpit.channel({ }); |
| 301 |
assert.ok(channel); |
| 302 |
mock_peer.addEventListener("open", function(event) { |
| 303 |
assert.ok(true, "websocket connected"); |
| 304 |
}); |
| 305 |
mock_peer.addEventListener("recv", function(event, chan, payload) { |
| 306 |
const command = JSON.parse(payload); |
| 307 |
if (command.command == "open") { |
| 308 |
assert.strictEqual(command.host, "planetexpress", "host automatically chosen"); |
| 309 |
done(); |
| 310 |
} |
| 311 |
}); |
| 312 |
}); |
| 313 |
|
| 314 |
QUnit.test("send message", function (assert) { |
| 315 |
const done = assert.async(); |
| 316 |
assert.expect(2); |
| 317 |
|
| 318 |
const channel = cockpit.channel({ }); |
| 319 |
mock_peer.addEventListener("open", function(event) { |
| 320 |
channel.send("Scruffy gonna die the way he lived"); |
| 321 |
}); |
| 322 |
mock_peer.addEventListener("recv", function(event, chan, payload) { |
| 323 |
/* Ignore the open and init messages */ |
| 324 |
if (!chan) |
| 325 |
return; |
| 326 |
assert.strictEqual(chan, channel.id, "sent with correct channel"); |
| 327 |
assert.equal(payload, "Scruffy gonna die the way he lived", "sent the right payload"); |
| 328 |
done(); |
| 329 |
}); |
| 330 |
}); |
| 331 |
|
| 332 |
QUnit.test("queue messages", function (assert) { |
| 333 |
const done = assert.async(); |
| 334 |
assert.expect(1); |
| 335 |
|
| 336 |
const sentence = []; |
| 337 |
const channel = cockpit.channel({ }); |
| 338 |
channel.send("Scruffy"); |
| 339 |
channel.send("knows"); |
| 340 |
channel.send("he"); |
| 341 |
channel.send("rules"); |
| 342 |
mock_peer.addEventListener("recv", function(event, chan, payload) { |
| 343 |
if (!chan) |
| 344 |
return; /* ignore control messages */ |
| 345 |
sentence.push(payload); |
| 346 |
if (sentence.length === 4) { |
| 347 |
assert.equal(sentence.join(" "), "Scruffy knows he rules", "messages queued and sent correctly"); |
| 348 |
done(); |
| 349 |
} |
| 350 |
}); |
| 351 |
}); |
| 352 |
|
| 353 |
QUnit.test("receive message", function (assert) { |
| 354 |
const done = assert.async(); |
| 355 |
assert.expect(1); |
| 356 |
|
| 357 |
const onrecv = (event, chan, payload) => { |
| 358 |
const cmd = JSON.parse(payload); |
| 359 |
if (cmd.command == "open") { |
| 360 |
mock_peer.removeEventListener("recv", onrecv); |
| 361 |
mock_peer.send(channel.id, "Oh, marrrrmalade!"); |
| 362 |
} |
| 363 |
}; |
| 364 |
mock_peer.addEventListener("recv", onrecv); |
| 365 |
|
| 366 |
const channel = cockpit.channel({ }); |
| 367 |
channel.addEventListener("message", function(event, message) { |
| 368 |
assert.equal(message, "Oh, marrrrmalade!", "got right message in channel"); |
| 369 |
done(); |
| 370 |
}); |
| 371 |
}); |
| 372 |
|
| 373 |
QUnit.test("close channel", function (assert) { |
| 374 |
const done = assert.async(2); |
| 375 |
assert.expect(4); |
| 376 |
|
| 377 |
mock_peer.addEventListener("recv", function(event, chan, payload) { |
| 378 |
const cmd = JSON.parse(payload); |
| 379 |
if (cmd.command == "init") { |
| 380 |
return; |
| 381 |
} else if (cmd.command == "open") { |
| 382 |
channel.close(); |
| 383 |
return; |
| 384 |
} |
| 385 |
assert.equal(cmd.command, "close", "sent close command"); |
| 386 |
assert.strictEqual(cmd.channel, channel.id, "correct channel"); |
| 387 |
mock_peer.send("", payload); |
| 388 |
done(); |
| 389 |
}); |
| 390 |
const channel = cockpit.channel({ }); |
| 391 |
channel.addEventListener("close", function(event, options) { |
| 392 |
assert.ok(true, "triggered event"); |
| 393 |
assert.ok(!options.problem, "no problem"); |
| 394 |
done(); |
| 395 |
}); |
| 396 |
}); |
| 397 |
|
| 398 |
QUnit.test("close early", function (assert) { |
| 399 |
const done = assert.async(); |
| 400 |
assert.expect(3); |
| 401 |
|
| 402 |
const channel = cockpit.channel({ }); |
| 403 |
channel.addEventListener("close", function(event, options) { |
| 404 |
assert.ok(true, "triggered event"); |
| 405 |
assert.equal(options.problem, "yo", "got problem"); |
| 406 |
done(); |
| 407 |
}); |
| 408 |
channel.close("yo"); |
| 409 |
assert.strictEqual(channel.valid, false, "no longer valid"); |
| 410 |
}); |
| 411 |
|
| 412 |
QUnit.test("close problem", function (assert) { |
| 413 |
const done = assert.async(); |
| 414 |
assert.expect(5); |
| 415 |
|
| 416 |
mock_peer.addEventListener("recv", function(event, chan, payload) { |
| 417 |
const cmd = JSON.parse(payload); |
| 418 |
if (cmd.command == "init") { |
| 419 |
return; |
| 420 |
} else if (cmd.command == "open") { |
| 421 |
channel.close({ problem: "problem" }); |
| 422 |
assert.strictEqual(channel.valid, false, "no longer valid"); |
| 423 |
return; |
| 424 |
} |
| 425 |
assert.equal(cmd.command, "close", "sent close command"); |
| 426 |
assert.equal(cmd.problem, "problem", "sent reason"); |
| 427 |
done(); |
| 428 |
}); |
| 429 |
const channel = cockpit.channel({ }); |
| 430 |
channel.addEventListener("close", function(event, options) { |
| 431 |
assert.ok(true, "triggered event"); |
| 432 |
assert.equal(options.problem, "problem", "set"); |
| 433 |
}); |
| 434 |
}); |
| 435 |
|
| 436 |
QUnit.test("close problem string", function (assert) { |
| 437 |
const done = assert.async(); |
| 438 |
assert.expect(5); |
| 439 |
|
| 440 |
const channel = cockpit.channel({ }); |
| 441 |
mock_peer.addEventListener("recv", function(event, chan, payload) { |
| 442 |
const cmd = JSON.parse(payload); |
| 443 |
if (cmd.command == "init") { |
| 444 |
return; |
| 445 |
} else if (cmd.command == "open") { |
| 446 |
channel.close("testo"); |
| 447 |
assert.strictEqual(channel.valid, false, "no longer valid"); |
| 448 |
return; |
| 449 |
} |
| 450 |
assert.equal(cmd.command, "close", "sent close command"); |
| 451 |
assert.equal(cmd.problem, "testo", "sent reason"); |
| 452 |
done(); |
| 453 |
}); |
| 454 |
channel.addEventListener("close", function(event, options) { |
| 455 |
assert.ok(true, "triggered event"); |
| 456 |
assert.equal(options.problem, "testo", "set"); |
| 457 |
}); |
| 458 |
}); |
| 459 |
|
| 460 |
QUnit.test("close peer", function (assert) { |
| 461 |
const done = assert.async(); |
| 462 |
assert.expect(5); |
| 463 |
|
| 464 |
mock_peer.addEventListener("recv", function(event, chan, payload) { |
| 465 |
const msg = JSON.parse(payload); |
| 466 |
if (msg.command == "init") |
| 467 |
return; |
| 468 |
const cmd = { |
| 469 |
command: "close", |
| 470 |
channel: channel.id, |
| 471 |
problem: "marmalade", |
| 472 |
extra: 5 |
| 473 |
}; |
| 474 |
mock_peer.send("", JSON.stringify(cmd)); |
| 475 |
}); |
| 476 |
|
| 477 |
const channel = cockpit.channel({ }); |
| 478 |
const channelb = cockpit.channel({ }); |
| 479 |
|
| 480 |
channel.addEventListener("close", function(event, options) { |
| 481 |
assert.ok(true, "triggered event"); |
| 482 |
assert.equal(options.problem, "marmalade", "received reason"); |
| 483 |
assert.equal(options.extra, 5, "received extra"); |
| 484 |
assert.strictEqual(channel.valid, false, "became invalid"); |
| 485 |
assert.strictEqual(channelb.valid, true, "correct channel"); |
| 486 |
done(); |
| 487 |
}); |
| 488 |
}); |
| 489 |
|
| 490 |
QUnit.test("close socket", function (assert) { |
| 491 |
const done = assert.async(); |
| 492 |
assert.expect(4); |
| 493 |
|
| 494 |
const channel = cockpit.channel({ }); |
| 495 |
const channelb = cockpit.channel({ }); |
| 496 |
|
| 497 |
channel.addEventListener("close", function(event, options) { |
| 498 |
assert.equal(options.problem, "disconnected", "received reason"); |
| 499 |
assert.strictEqual(channel.valid, false, "channel is invalid"); |
| 500 |
if (!channel.valid && !channelb.valid) |
| 501 |
done(); |
| 502 |
}); |
| 503 |
|
| 504 |
channelb.addEventListener("close", function(event, options) { |
| 505 |
assert.equal(options.problem, "disconnected", "received reason"); |
| 506 |
assert.strictEqual(channelb.valid, false, "other channel invalid"); |
| 507 |
if (!channel.valid && !channelb.valid) |
| 508 |
done(); |
| 509 |
}); |
| 510 |
|
| 511 |
mock_peer.close(); |
| 512 |
}); |
| 513 |
|
| 514 |
QUnit.test("wait ready", function (assert) { |
| 515 |
const done = assert.async(); |
| 516 |
assert.expect(5); |
| 517 |
|
| 518 |
const channel = cockpit.channel({ payload: "echo" }); |
| 519 |
channel.wait().then(function(options) { |
| 520 |
assert.ok(true, "channel is ready"); |
| 521 |
assert.equal(typeof options, "object", "wait options"); |
| 522 |
assert.ok(!!options, "wait options not null"); |
| 523 |
assert.equal(options.command, "ready", "wait is ready"); |
| 524 |
assert.strictEqual(channel.valid, true, "when valid"); |
| 525 |
}, function() { |
| 526 |
assert.ok(false, "should not fail"); |
| 527 |
}) |
| 528 |
.always(function() { |
| 529 |
done(); |
| 530 |
}); |
| 531 |
}); |
| 532 |
|
| 533 |
QUnit.test("wait close", function (assert) { |
| 534 |
const done = assert.async(); |
| 535 |
assert.expect(6); |
| 536 |
|
| 537 |
const channel = cockpit.channel({ payload: "unsupported" }); |
| 538 |
channel.wait().then(function() { |
| 539 |
assert.ok(false, "should not succeed"); |
| 540 |
}, function(options) { |
| 541 |
assert.ok(true, "channel is closed"); |
| 542 |
assert.equal(typeof options, "object", "wait options"); |
| 543 |
assert.ok(!!options, "wait options not null"); |
| 544 |
assert.equal(options.command, "close", "wait is close"); |
| 545 |
assert.equal(options.problem, "not-supported", "wait options has fields"); |
| 546 |
assert.strictEqual(channel.valid, false, "channel not valid"); |
| 547 |
}) |
| 548 |
.always(function() { |
| 549 |
done(); |
| 550 |
}); |
| 551 |
}); |
| 552 |
|
| 553 |
QUnit.test("wait callback", function (assert) { |
| 554 |
const done = assert.async(); |
| 555 |
assert.expect(5); |
| 556 |
|
| 557 |
const channel = cockpit.channel({ payload: "unsupported" }); |
| 558 |
channel.wait(function(options) { |
| 559 |
assert.equal(typeof options, "object", "wait options"); |
| 560 |
assert.ok(!!options, "wait options not null"); |
| 561 |
assert.equal(options.command, "close", "wait is close"); |
| 562 |
assert.equal(options.problem, "not-supported", "wait options has fields"); |
| 563 |
assert.strictEqual(channel.valid, false, "channel not valid"); |
| 564 |
}).always(function() { |
| 565 |
done(); |
| 566 |
}); |
| 567 |
}); |
| 568 |
|
| 569 |
QUnit.test("logout", function (assert) { |
| 570 |
const done = assert.async(); |
| 571 |
mock_peer.addEventListener("recv", function(event, chan, payload) { |
| 572 |
const cmd = JSON.parse(payload); |
| 573 |
if (cmd.command == "logout") { |
| 574 |
mock_peer.close("disconnected"); |
| 575 |
assert.strictEqual(cmd.disconnect, true, "disconnect set"); |
| 576 |
} |
| 577 |
}); |
| 578 |
|
| 579 |
let channel = cockpit.channel({ payload: "echo" }); |
| 580 |
let channelb = cockpit.channel({ payload: "echo" }); |
| 581 |
|
| 582 |
channel.addEventListener("close", function(event, options) { |
| 583 |
assert.equal(options.problem, "disconnected", "received reason"); |
| 584 |
assert.strictEqual(channel.valid, false, "channel is invalid"); |
| 585 |
channel = null; |
| 586 |
if (channel === null && channelb === null) |
| 587 |
done(); |
| 588 |
}); |
| 589 |
|
| 590 |
channelb.addEventListener("close", function(event, options) { |
| 591 |
assert.equal(options.problem, "disconnected", "received reason"); |
| 592 |
assert.strictEqual(channelb.valid, false, "other channel invalid"); |
| 593 |
channelb = null; |
| 594 |
if (channel === null && channelb === null) |
| 595 |
done(); |
| 596 |
}); |
| 597 |
|
| 598 |
cockpit.logout(false); |
| 599 |
}); |
| 600 |
|
| 601 |
QUnit.test("droppriv", function (assert) { |
| 602 |
const done = assert.async(); |
| 603 |
assert.expect(1); |
| 604 |
mock_peer.addEventListener("recv", function(event, chan, payload) { |
| 605 |
const cmd = JSON.parse(payload); |
| 606 |
if (cmd.command == "logout") { |
| 607 |
assert.strictEqual(cmd.disconnect, false, "disconnect not set"); |
| 608 |
done(); |
| 609 |
} |
| 610 |
}); |
| 611 |
|
| 612 |
cockpit.drop_privileges(); |
| 613 |
}); |
| 614 |
|
| 615 |
QUnit.test("info", function (assert) { |
| 616 |
const done = assert.async(); |
| 617 |
assert.expect(4); |
| 618 |
|
| 619 |
let info_changed = false; |
| 620 |
|
| 621 |
const onchanged = () => { |
| 622 |
assert.strictEqual(cockpit.info.version, "zero.point.zero", "cockpit.info.version"); |
| 623 |
assert.strictEqual(cockpit.info.build, "nasty stuff", "cockpit.info.build"); |
| 624 |
info_changed = true; |
| 625 |
}; |
| 626 |
cockpit.info.addEventListener("changed", onchanged); |
| 627 |
|
| 628 |
const onrecv = (event, chan, payload) => { |
| 629 |
const cmd = JSON.parse(payload); |
| 630 |
if (cmd.command == "open") { |
| 631 |
mock_peer.removeEventListener("recv", onrecv); |
| 632 |
cockpit.info.removeEventListener("changed", onchanged); |
| 633 |
assert.strictEqual(info_changed, true, "info changed event was called"); |
| 634 |
done(); |
| 635 |
} |
| 636 |
}; |
| 637 |
mock_peer.addEventListener("recv", onrecv); |
| 638 |
|
| 639 |
const channel = cockpit.channel({ host: "scruffy" }); |
| 640 |
assert.ok(channel); |
| 641 |
}); |
| 642 |
|
| 643 |
QUnit.test("send after close", function (assert) { |
| 644 |
const done = assert.async(); |
| 645 |
assert.expect(1); |
| 646 |
|
| 647 |
console_ignore_log(/sending message on closed.*/); |
| 648 |
|
| 649 |
let received_message = false; |
| 650 |
const channel = cockpit.channel({ }); |
| 651 |
mock_peer.addEventListener("recv", function(event, chan, payload) { |
| 652 |
if (chan) |
| 653 |
received_message = true; |
| 654 |
}); |
| 655 |
|
| 656 |
channel.close(); |
| 657 |
channel.send("Dern it."); |
| 658 |
|
| 659 |
window.setTimeout(function() { |
| 660 |
assert.ok(!received_message, "didn't send message"); |
| 661 |
done(); |
| 662 |
}, 50); |
| 663 |
}); |
| 664 |
|
| 665 |
QUnit.test("ignore other commands", function (assert) { |
| 666 |
const done = assert.async(); |
| 667 |
assert.expect(1); |
| 668 |
|
| 669 |
const channel = cockpit.channel({ payload: "echo" }); |
| 670 |
|
| 671 |
console_ignore_log(/unhandled control message.*/); |
| 672 |
|
| 673 |
mock_peer.send(0, JSON.stringify({ command: "ping" })); |
| 674 |
mock_peer.send(0, JSON.stringify({ command: "unexpected" })); |
| 675 |
|
| 676 |
window.setTimeout(function() { |
| 677 |
assert.ok(channel.valid, "other messages didn't screw up channel"); |
| 678 |
done(); |
| 679 |
}, 50); |
| 680 |
}); |
| 681 |
|
| 682 |
QUnit.test("filter message in", function (assert) { |
| 683 |
const done = assert.async(); |
| 684 |
assert.expect(14); |
| 685 |
|
| 686 |
let filtered = 0; |
| 687 |
let filtering = true; |
| 688 |
cockpit.transport.filter(function(message, channelid, control) { |
| 689 |
if (!filtering) |
| 690 |
return true; |
| 691 |
if (message[0] == '\n') { |
| 692 |
assert.strictEqual(channelid, "", "control message channel"); |
| 693 |
assert.equal(typeof control, "object", "control is a JSON object"); |
| 694 |
assert.equal(typeof control.command, "string", "control has a command"); |
| 695 |
return true; |
| 696 |
} else { |
| 697 |
assert.strictEqual(channelid, channel.id, "cockpit channel id"); |
| 698 |
assert.equal(control, undefined, "control is undefined"); |
| 699 |
filtered += 1; |
| 700 |
return (filtered != 1); |
| 701 |
} |
| 702 |
}); |
| 703 |
|
| 704 |
let received = 0; |
| 705 |
const channel = cockpit.channel({ payload: "echo" }); |
| 706 |
channel.addEventListener("message", function(data) { |
| 707 |
received += 1; |
| 708 |
|
| 709 |
if (received == 2) { |
| 710 |
assert.equal(filtered, 3, "filtered right amount"); |
| 711 |
assert.equal(received, 2, "let through right amount"); |
| 712 |
channel.close(); |
| 713 |
filtering = false; |
| 714 |
done(); |
| 715 |
} |
| 716 |
}); |
| 717 |
|
| 718 |
channel.send("one"); |
| 719 |
channel.send("two"); |
| 720 |
channel.send("three"); |
| 721 |
}); |
| 722 |
|
| 723 |
QUnit.test("inject message out", function (assert) { |
| 724 |
const done = assert.async(); |
| 725 |
assert.expect(4); |
| 726 |
|
| 727 |
const ret = cockpit.transport.inject("bree\nyellow"); |
| 728 |
assert.equal(ret, false, "failure returns false"); |
| 729 |
|
| 730 |
let first = true; |
| 731 |
mock_peer.addEventListener("recv", function(event, chan, payload) { |
| 732 |
if (first) { |
| 733 |
const ret = cockpit.transport.inject("bree\nyellow"); |
| 734 |
assert.equal(ret, true, "returned true"); |
| 735 |
first = false; |
| 736 |
return; |
| 737 |
} |
| 738 |
|
| 739 |
if (chan) { |
| 740 |
assert.equal(chan, "bree", "right channel"); |
| 741 |
assert.equal(payload, "yellow", "right payload"); |
| 742 |
channel.close(); |
| 743 |
done(); |
| 744 |
} |
| 745 |
}); |
| 746 |
|
| 747 |
const channel = cockpit.channel({ }); |
| 748 |
}); |
| 749 |
|
| 750 |
QUnit.test("inject message in", function (assert) { |
| 751 |
const done = assert.async(); |
| 752 |
assert.expect(3); |
| 753 |
|
| 754 |
const channel = cockpit.channel({ payload: "null" }); |
| 755 |
channel.addEventListener("control", function(ev, control) { |
| 756 |
if (control.command == "ready") { |
| 757 |
const payload = JSON.stringify({ command: "blah", blah: "marmalade", channel: channel.id }); |
| 758 |
const ret = cockpit.transport.inject("\n" + payload, false); |
| 759 |
assert.equal(ret, true, "returned true"); |
| 760 |
} else { |
| 761 |
assert.equal(control.command, "blah", "got right control message"); |
| 762 |
assert.equal(control.blah, "marmalade", "got right control data"); |
| 763 |
channel.close(); |
| 764 |
done(); |
| 765 |
} |
| 766 |
}); |
| 767 |
}); |
| 768 |
|
| 769 |
QUnit.test("transport options", function (assert) { |
| 770 |
const done = assert.async(); |
| 771 |
assert.expect(3); |
| 772 |
|
| 773 |
mock_peer.addEventListener("recv", function(event, chan, payload) { |
| 774 |
if (chan) { |
| 775 |
assert.equal(typeof cockpit.transport.options, "object", "is an object"); |
| 776 |
assert.deepEqual(cockpit.transport.options, { |
| 777 |
command: "init", |
| 778 |
version: 1, |
| 779 |
"channel-seed": "test", |
| 780 |
"csrf-token": "the-csrf-token", |
| 781 |
user: { |
| 782 |
user: "scruffy", |
| 783 |
name: "Scruffy the Janitor" |
| 784 |
}, |
| 785 |
system: { |
| 786 |
version: "zero.point.zero", |
| 787 |
build: "nasty stuff", |
| 788 |
} |
| 789 |
}, "is correct"); |
| 790 |
assert.equal(cockpit.transport.csrf_token, "the-csrf-token", "got csrf token"); |
| 791 |
channel.close(); |
| 792 |
done(); |
| 793 |
} |
| 794 |
}); |
| 795 |
|
| 796 |
const channel = cockpit.channel({ }); |
| 797 |
channel.send("blah"); |
| 798 |
}); |
| 799 |
|
| 800 |
QUnit.test("message", function (assert) { |
| 801 |
assert.expect(4); |
| 802 |
assert.strictEqual(cockpit.message("terminated"), "Your session has been terminated.", "problem code"); |
| 803 |
assert.strictEqual(cockpit.message({ problem: "timeout" }), "Connection has timed out.", "problem property"); |
| 804 |
assert.strictEqual(cockpit.message({ message: "The message", problem: "blah" }), "The message", "problem property"); |
| 805 |
assert.strictEqual(cockpit.message(55), "55", "invalid input"); |
| 806 |
}); |
| 807 |
|
| 808 |
window.location.hash = ""; |
| 809 |
QUnit.start(); |