| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import cockpit from "cockpit"; |
| 3 |
import QUnit from "qunit-tests"; |
| 4 |
|
| 5 |
interface ChannelOptions { |
| 6 |
payload: string; |
| 7 |
path?: string; |
| 8 |
watch?: boolean; |
| 9 |
binary?: string | boolean | null; |
| 10 |
external?: Record<string, string>; |
| 11 |
} |
| 12 |
|
| 13 |
function channel_url(query: ChannelOptions): string { |
| 14 |
return "/cockpit/channel/" + cockpit.transport.csrf_token + "?" + window.btoa(JSON.stringify(query)); |
| 15 |
} |
| 16 |
|
| 17 |
QUnit.test("external get", async assert => { |
| 18 |
const resp = await fetch(channel_url({ |
| 19 |
payload: "fslist1", |
| 20 |
path: "/tmp", |
| 21 |
watch: false |
| 22 |
})); |
| 23 |
|
| 24 |
assert.equal(resp.status, 200, "got right status"); |
| 25 |
assert.equal(resp.statusText, "OK", "got right reason"); |
| 26 |
assert.equal(resp.headers.get("Content-Type"), "application/octet-stream", "default type"); |
| 27 |
const text = await resp.text(); |
| 28 |
assert.ok(text.indexOf('"present"'), "got listing"); |
| 29 |
}); |
| 30 |
|
| 31 |
QUnit.test("external fsread1", async assert => { |
| 32 |
const stat = await cockpit.spawn(["stat", "--format", "%s", "/usr/lib/os-release"]); |
| 33 |
const filesize = stat.replace(/\n$/, ""); |
| 34 |
|
| 35 |
const resp = await fetch(channel_url({ |
| 36 |
payload: "fsread1", |
| 37 |
path: '/usr/lib/os-release', |
| 38 |
binary: "raw", |
| 39 |
external: { |
| 40 |
"content-disposition": 'attachment; filename="foo"', |
| 41 |
"content-type": "application/octet-stream", |
| 42 |
} |
| 43 |
})); |
| 44 |
|
| 45 |
assert.equal(resp.status, 200, "got right status"); |
| 46 |
assert.equal(resp.statusText, "OK", "got right reason"); |
| 47 |
assert.equal(resp.headers.get("Content-Type"), "application/octet-stream", "expected type"); |
| 48 |
assert.equal(resp.headers.get("Content-Disposition"), 'attachment; filename="foo"', "expected disposition"); |
| 49 |
assert.equal(resp.headers.get("Content-Length"), filesize, "expected file size"); |
| 50 |
}); |
| 51 |
|
| 52 |
QUnit.test("external content-type default with binary:raw", async assert => { |
| 53 |
const resp = await fetch(channel_url({ |
| 54 |
payload: "fslist1", |
| 55 |
path: "/tmp", |
| 56 |
watch: false, |
| 57 |
binary: "raw", |
| 58 |
})); |
| 59 |
|
| 60 |
assert.equal(resp.status, 200, "got right status"); |
| 61 |
assert.equal(resp.headers.get("Content-Type"), "application/octet-stream", "default type for binary channel"); |
| 62 |
}); |
| 63 |
|
| 64 |
QUnit.module("tests that need test-server warnings disabled", hooks => { |
| 65 |
hooks.before(async () => { await fetch("/mock/expect-warnings") }); |
| 66 |
hooks.after(async () => { await fetch("/mock/dont-expect-warnings") }); |
| 67 |
|
| 68 |
QUnit.test("external rejects binary:empty-string", async assert => { |
| 69 |
const resp = await fetch(channel_url({ |
| 70 |
payload: "fslist1", |
| 71 |
path: "/tmp", |
| 72 |
watch: false, |
| 73 |
binary: "", |
| 74 |
})); |
| 75 |
|
| 76 |
// This should ideally be a 400 (rejected by -ws itself), but |
| 77 |
// currently -ws accepts any string and forwards it to the bridge, |
| 78 |
// which rejects it as a protocol error, resulting in a 500. |
| 79 |
assert.ok(resp.status === 400 || resp.status === 500, "empty string rejected"); |
| 80 |
}); |
| 81 |
|
| 82 |
QUnit.test("external rejects binary:false", async assert => { |
| 83 |
const resp = await fetch(channel_url({ |
| 84 |
payload: "fslist1", |
| 85 |
path: "/tmp", |
| 86 |
watch: false, |
| 87 |
binary: false, |
| 88 |
})); |
| 89 |
|
| 90 |
assert.equal(resp.status, 400, "false rejected"); |
| 91 |
}); |
| 92 |
|
| 93 |
QUnit.test("external rejects binary:null", async assert => { |
| 94 |
const resp = await fetch(channel_url({ |
| 95 |
payload: "fslist1", |
| 96 |
path: "/tmp", |
| 97 |
watch: false, |
| 98 |
binary: null, |
| 99 |
})); |
| 100 |
|
| 101 |
assert.equal(resp.status, 400, "null rejected"); |
| 102 |
}); |
| 103 |
}); |
| 104 |
|
| 105 |
QUnit.test("external ignores mixed-case Content-Type", async assert => { |
| 106 |
const resp = await fetch(channel_url({ |
| 107 |
payload: "fslist1", |
| 108 |
path: "/tmp", |
| 109 |
watch: false, |
| 110 |
external: { |
| 111 |
"Content-Type": "text/fancy", |
| 112 |
}, |
| 113 |
})); |
| 114 |
|
| 115 |
assert.equal(resp.status, 200, "got right status"); |
| 116 |
assert.equal(resp.headers.get("Content-Type"), "application/octet-stream", |
| 117 |
"only lowercase 'content-type' key is recognized"); |
| 118 |
}); |
| 119 |
|
| 120 |
QUnit.test("external headers", async assert => { |
| 121 |
const resp = await fetch(channel_url({ |
| 122 |
payload: "fslist1", |
| 123 |
path: "/tmp", |
| 124 |
watch: false, |
| 125 |
external: { |
| 126 |
"content-disposition": "my disposition; blah", |
| 127 |
"content-type": "test/blah", |
| 128 |
}, |
| 129 |
})); |
| 130 |
|
| 131 |
assert.equal(resp.status, 200, "got right status"); |
| 132 |
assert.equal(resp.headers.get("Content-Type"), "test/blah", "got type"); |
| 133 |
assert.equal(resp.headers.get("Content-Disposition"), "my disposition; blah", "got disposition"); |
| 134 |
}); |
| 135 |
|
| 136 |
QUnit.test("external invalid", async assert => { |
| 137 |
const resp = await fetch("/cockpit/channel/invalid"); |
| 138 |
assert.equal(resp.status, 404, "got not found"); |
| 139 |
}); |
| 140 |
|
| 141 |
QUnit.test("external no token", async assert => { |
| 142 |
const query = window.btoa(JSON.stringify({ |
| 143 |
payload: "fslist1", |
| 144 |
path: "/tmp", |
| 145 |
watch: false |
| 146 |
})); |
| 147 |
|
| 148 |
const resp = await fetch("/cockpit/channel/?" + query); |
| 149 |
assert.equal(resp.status, 404, "got not found"); |
| 150 |
}); |
| 151 |
|
| 152 |
QUnit.test("external websocket", async assert => { |
| 153 |
const query = window.btoa(JSON.stringify({ |
| 154 |
payload: "echo" |
| 155 |
})); |
| 156 |
|
| 157 |
const ws = new WebSocket("ws://" + window.location.host + "/cockpit/channel/" + |
| 158 |
cockpit.transport.csrf_token + '?' + query, "protocol-unused"); |
| 159 |
|
| 160 |
await new Promise<void>((resolve, reject) => { |
| 161 |
ws.onopen = () => resolve(); |
| 162 |
ws.onerror = () => reject(new Error("WebSocket connection failed")); |
| 163 |
}); |
| 164 |
assert.ok(true, "websocket is open"); |
| 165 |
|
| 166 |
try { |
| 167 |
ws.send("oh marmalade"); |
| 168 |
let ev = await new Promise<MessageEvent>(resolve => { ws.onmessage = resolve }); |
| 169 |
assert.equal(ev.data, "oh marmalade", "got payload"); |
| 170 |
|
| 171 |
ws.send("another test"); |
| 172 |
ev = await new Promise<MessageEvent>(resolve => { ws.onmessage = resolve }); |
| 173 |
assert.equal(ev.data, "another test", "got payload again"); |
| 174 |
} finally { |
| 175 |
ws.close(1000); |
| 176 |
} |
| 177 |
}); |
| 178 |
|
| 179 |
cockpit.transport.wait(function() { |
| 180 |
/* Tell tap driver not to worry about HTTP failures past this point */ |
| 181 |
console.log("cockpittest-tap-expect-resource-error"); |
| 182 |
QUnit.start(); |
| 183 |
}); |