Files
chandra/compositor/host/control_test.mbt
T
nandi d2e00b4658
CI / test (push) Failing after 1m2s
Remote: paste the phone clipboard, including images, on the desktop
- Web UI: Ctrl+V, Ctrl+Shift+V, Shift+Insert (Cmd+V on Mac) and a Paste button send the phone clipboard (text or PNG, up to 16 MB) to the desktop over POST /api/clipboard
- chandra-remote passes clipboard data to the compositor in a sealed memfd (clipboard-set-fd), replacing the 4 KB hex clipboard-set line
- Compositor: new clipboard-status and input-paste commands; text is typed into the launcher, password dialog and shell popups, otherwise the paste chord is sent (Ctrl+Shift+V in terminals)
- Paste data is captured when a paste starts, so a clipboard change mid-transfer can't corrupt it
- VNC clipboard text limit raised from 4 KB to 1 MB
- Upload endpoints are authorized with the same token check as /ws; the no-header cross-site case is refused with 403
2026-11-29 15:42:46 -08:00

325 lines
13 KiB
MoonBit

///| The control socket (control.mbt): chandra-ctl's commands and module reloads.
/// Needs the modules: run scripts/build-modules.sh (just test does).
///| A compositor with its control socket open in a fresh directory.
fn control_comp(tag : String) -> (@host.ChandraCompositor, String) raise {
let comp = new_comp("control-" + tag)
let dir = "/tmp/chandra-control-" + tag + "-" + @sys.getpid().to_string()
let path = @host.control_socket_path(dir, "/run/user/1000/wayland-moon-0")
assert_eq(path, dir + "/chandra/wayland-moon-0.sock")
assert_true(comp.start_control(path))
(comp, path)
}
///| Send `line` over a new connection, let the compositor run a pass, and read its reply lines.
fn ask(comp : @host.ChandraCompositor, path : String, line : String) -> Array[String] raise {
let fd = @sys.unix_connect(path)
assert_true(fd >= 0)
assert_true(@sys.sendmsg_fd(fd, @utf8.encode(line + "\n"), -1) > 0)
let raw : Array[Byte] = []
let buf : FixedArray[Byte] = FixedArray::make(4096, b'\x00')
let mut tries = 0
let mut done = false
while !done && tries < 50 {
comp.step()
while @sys.poll_read(fd, 5) > 0 {
let (n, _) = @sys.recvmsg_fd(fd, buf, 4096)
if n <= 0 {
break
}
for i = 0; i < n; i = i + 1 {
raw.push(buf[i])
}
}
let text = @utf8.decode_lossy(Bytes::from_array(raw))
let lines = text.split("\n").map(s => s.to_string()).filter(s => s != "").collect()
if text.has_suffix("\n") && lines.length() > 0 {
let last = lines[lines.length() - 1]
done = last.has_prefix("ok") || last.has_prefix("error")
}
tries = tries + 1
}
@sys.close_fd(fd)
let text = @utf8.decode_lossy(Bytes::from_array(raw))
text.split("\n").map(s => s.to_string()).filter(s => s != "").collect()
}
///| The slot ids of the panel, in order.
fn panel_order(comp : @host.ChandraCompositor) -> Array[String] {
comp.shell.loaded_modules().map(m => m.1)
}
///|
test "control socket path follows the Wayland socket's name" {
assert_eq(@host.control_socket_path("/run/user/1000", "/run/user/1000/wayland-moon-0"), "/run/user/1000/chandra/wayland-moon-0.sock")
assert_eq(@host.control_socket_path("/run/user/1000", "wayland-1"), "/run/user/1000/chandra/wayland-1.sock")
assert_eq(@host.control_socket_path("", "/tmp/wayland-moon-0"), "")
}
///|
test "ping, list and errors over the control socket" {
let (comp, path) = control_comp("basic")
assert_eq(ask(comp, path, "ping"), ["ok pong"])
let list = ask(comp, path, "list")
let n = comp.shell.loaded_modules().length()
assert_true(n > 3)
assert_eq(list.length(), n + 1)
assert_eq(list[n], "ok " + n.to_string() + " modules")
assert_true(list.iter().any(l => l.contains(" clock-digital ") && l.has_suffix("clock-digital.wasm")))
assert_eq(ask(comp, path, "bogus"), ["error unknown command 'bogus' (try: help)"])
assert_eq(ask(comp, path, "reload nope"), ["error no loaded module named 'nope'"])
assert_eq(ask(comp, path, " "), ["error empty command (try: help)"])
assert_eq(ask(comp, path, "help").last(), Some("ok"))
// Shortcuts by id: the overview opens and closes.
assert_eq(ask(comp, path, "shortcut overview"), ["ok overview"])
assert_true(comp.overview_open())
assert_eq(ask(comp, path, "shortcut overview"), ["ok overview"])
assert_false(comp.overview_open())
assert_eq(ask(comp, path, "shortcut nope"), ["error no shortcut 'nope'"])
assert_eq(ask(comp, path, "shortcut"), ["error usage: shortcut ID"])
// Several commands on one connection get one reply each, in order.
let both = ask(comp, path, "ping\nping")
assert_eq(both, ["ok pong", "ok pong"])
comp.close()
}
///|
test "reload every module keeps the panel's order" {
let (comp, path) = control_comp("all")
let before = panel_order(comp)
let slots_before = comp.shell.loaded_modules().map(m => m.0)
let reply = ask(comp, path, "reload")
assert_eq(reply.length(), before.length() + 1)
assert_eq(reply.last(), Some("ok " + before.length().to_string() + " reloaded"))
assert_eq(panel_order(comp), before)
// Every instance is new.
for s in comp.shell.loaded_modules().map(m => m.0) {
assert_false(slots_before.contains(s))
}
// And still draws.
ignore(comp.compose_frame())
assert_true(rect_of(comp, "clock-digital").w > 0)
comp.close()
}
///|
test "reload one module by name keeps its place and its settings" {
let (comp, path) = control_comp("one")
let slot = comp.shell.slot_of("clock-digital")
comp.shell.set_module_setting(slot, "week-numbers", "on")
let before = panel_order(comp)
let others = comp.shell.loaded_modules().filter(m => m.1 != "clock-digital").map(m => m.0)
let reply = ask(comp, path, "reload clock-digital")
assert_eq(reply.length(), 2)
assert_true(reply[0].has_prefix("reloaded w"))
assert_eq(reply[1], "ok 1 reloaded")
assert_eq(panel_order(comp), before)
assert_true(comp.shell.slot_of("clock-digital") != slot)
// Only the clock was replaced.
assert_eq(comp.shell.loaded_modules().filter(m => m.1 != "clock-digital").map(m => m.0), others)
// The new instance got the saved setting: its calendar shows week numbers.
ignore(comp.compose_frame())
click_module(comp, "clock-digital", 1U)
finish_slide(comp)
assert_true(cal_texts(comp, "clock-digital").contains("Wk"))
// By slot works too.
let s2 = comp.shell.slot_of("logout")
assert_eq(ask(comp, path, "reload " + s2).last(), Some("ok 1 reloaded"))
comp.close()
}
///|
test "a module that fails to reload stays loaded and the error is reported" {
let (comp, path) = control_comp("fail")
let dir = "/tmp/chandra-control-badmod-" + @sys.getpid().to_string()
ignore(@sys.mkdir_p(dir))
let good = @sys.read_file_bytes("build/components/inspector.wasm")
assert_true(good.length() > 0)
let copy = dir + "/inspector.wasm"
ignore(@sys.write_file_bytes(copy, good))
assert_true(comp.shell.load_module(copy))
let before = panel_order(comp)
ignore(@sys.write_file_bytes(copy, @utf8.encode("not wasm")))
let reply = ask(comp, path, "reload " + copy)
assert_eq(reply.length(), 2)
assert_true(reply[0].has_prefix("failed w"))
assert_true(reply[0].contains("Can't load module"))
assert_eq(reply[1], "error 1 of 1 failed to reload")
assert_eq(panel_order(comp), before)
// The other modules still reload; the result says one failed.
let all = ask(comp, path, "reload")
assert_eq(all.last(), Some("error 1 of " + before.length().to_string() + " failed to reload"))
assert_eq(panel_order(comp), before)
comp.close()
}
///|
test "a second compositor leaves a live control socket alone" {
let (comp, path) = control_comp("busy")
let other = new_comp("control-busy2")
assert_false(other.start_control(path))
assert_eq(ask(comp, path, "ping"), ["ok pong"])
other.close()
comp.close()
// Closing removes the socket; a new compositor can take the path again.
let third = new_comp("control-busy3")
assert_true(third.start_control(path))
third.close()
}
///|
test "remote status, refresh, osk, pointer/key/text/clipboard injection and OLED-off routing" {
let (comp, path) = control_comp("remote")
let st = ask(comp, path, "status")
assert_eq(st.length(), 1)
assert_true(st[0].has_prefix("ok 1280x720 rot=0 screen=on"))
assert_true(st[0].contains(" ptr="))
assert_true(st[0].contains(" osk="))
assert_eq(ask(comp, path, "refresh"), ["ok"])
assert_eq(ask(comp, path, "osk off"), ["ok off"])
assert_eq(ask(comp, path, "osk on"), ["ok on"])
assert_eq(ask(comp, path, "osk toggle"), ["ok off"])
// Turn screen off manually: remote pointer/key injection still moves the pointer and
// executes shortcuts WITHOUT unblanking the physical display.
assert_eq(ask(comp, path, "screen off"), ["ok off"])
assert_true(comp.manual_screen_off)
assert_eq(ask(comp, path, "input-ptr 240 180"), ["ok"])
assert_eq(comp.ptr_x.to_int(), 240)
assert_eq(comp.ptr_y.to_int(), 180)
assert_true(comp.manual_screen_off)
assert_eq(ask(comp, path, "input-btn 272 1"), ["ok"])
assert_eq(ask(comp, path, "input-btn 272 0"), ["ok"])
// Toggle launcher via Super tap (evdev 125 down/up) while screen is still blanked.
assert_eq(ask(comp, path, "input-key 125 1"), ["ok"])
assert_eq(ask(comp, path, "input-key 125 0"), ["ok"])
assert_true(comp.shell.launcher_open())
assert_true(comp.manual_screen_off)
// input-text types into the open launcher popup ("6869" = "hi").
assert_eq(ask(comp, path, "input-text 6869"), ["ok typed"])
// Close launcher with Esc tap ("1"), then check clipboard-set + input-text fallback.
assert_eq(ask(comp, path, "input-key-tap 1 0"), ["ok"])
assert_false(comp.shell.launcher_open())
assert_eq(ask(comp, path, "clipboard-set 616263"), ["ok"])
assert_true(comp.server.owns_selection())
assert_eq(ask(comp, path, "input-text 7879"), ["ok clipboard"])
assert_true(comp.server.owns_selection())
assert_eq(ask(comp, path, "screen on"), ["ok on"])
assert_false(comp.manual_screen_off)
comp.close()
}
///| Like `ask` for one command, with `file` sent along.
fn ask_with_file(
comp : @host.ChandraCompositor,
path : String,
line : String,
file : Int,
) -> String raise {
let fd = @sys.unix_connect(path)
assert_true(fd >= 0)
assert_true(@sys.sendmsg_fd(fd, @utf8.encode(line + "\n"), file) > 0)
let buf : FixedArray[Byte] = FixedArray::make(4096, b'\x00')
let raw : Array[Byte] = []
for _i = 0; _i < 50 && raw.length() == 0; _i = _i + 1 {
comp.step()
if @sys.poll_read(fd, 5) > 0 {
let (n, _) = @sys.recvmsg_fd(fd, buf, 4096)
for i = 0; i < n; i = i + 1 {
raw.push(buf[i])
}
}
}
@sys.close_fd(fd)
@utf8.decode_lossy(Bytes::from_array(raw)).trim().to_string()
}
///|
test "clipboard from a file sent along: images, long text, limits, and paste" {
let (comp, path) = control_comp("clipfd")
assert_eq(ask(comp, path, "clipboard-status"), ["ok serial=0"])
// An image larger than one read of the file.
let size = 700000
let png = Bytes::makei(size, i => (i * 7 % 251).to_byte())
let fd = @sys.memfd_create("clip", size)
assert_eq(@sys.pwrite_bytes(fd, png, 0), size)
assert_eq(
ask_with_file(comp, path, "clipboard-set-fd image/png " + size.to_string(), fd),
"ok serial=1",
)
assert_true(comp.server.owns_selection())
assert_eq(comp.server.own_mimes, ["image/png"])
assert_true(comp.server.own_data == png)
assert_true(comp.server.own_selection_text() is None)
// Refused: no file, a type or size that isn't taken, a file that is too short.
assert_true(ask(comp, path, "clipboard-set-fd image/png 4")[0].has_prefix("error usage"))
assert_eq(
ask_with_file(comp, path, "clipboard-set-fd text/html 4", fd),
"error unsupported type 'text/html'",
)
assert_eq(ask_with_file(comp, path, "clipboard-set-fd image/png 0", fd), "error invalid size")
assert_eq(
ask_with_file(comp, path, "clipboard-set-fd image/png 99999999", fd),
"error invalid size",
)
assert_eq(
ask_with_file(comp, path, "clipboard-set-fd image/png 700001", fd),
"error file is shorter than 700001 bytes",
)
assert_eq(ask(comp, path, "clipboard-status"), ["ok serial=1"])
@sys.close_fd(fd)
// Text: offered as text, and typed into an open popup as one line.
let text = @utf8.encode("fire\nfox\n")
let tfd = @sys.memfd_create("clip", text.length())
ignore(@sys.pwrite_bytes(tfd, text, 0))
assert_eq(
ask_with_file(comp, path, "clipboard-set-fd text/plain " + text.length().to_string(), tfd),
"ok serial=2",
)
@sys.close_fd(tfd)
assert_eq(comp.server.own_selection_text(), Some(text))
assert_eq(ask(comp, path, "input-paste"), ["ok keys"])
assert_eq(ask(comp, path, "input-paste 47 4"), ["ok keys"])
assert_true(ask(comp, path, "input-paste 47")[0].has_prefix("error usage"))
assert_eq(ask(comp, path, "shortcut launcher"), ["ok launcher"])
assert_eq(ask(comp, path, "input-paste 47 4"), ["ok typed"])
assert_true(comp.shell.launcher_open())
comp.close()
}
///|
test "a paste still being written keeps its data when the clipboard changes" {
let (comp, path) = control_comp("clipswap")
// A slow reader: a socket nobody reads from yet, so only part of the data fits.
let sink_path = path + ".sink"
let listener = @sys.unix_listen(sink_path)
let reader = @sys.unix_connect(sink_path)
let writer = @sys.unix_accept(listener)
assert_true(writer >= 0)
let first = Bytes::make(3000000, b'a')
comp.server.set_own_selection(["image/png"], first)
comp.server.transfers.push((writer, comp.server.own_data, 0, @sys.monotonic_ms() + 10000UL))
comp.server.pump_transfers()
assert_eq(comp.server.pending_transfers(), 1)
comp.server.set_own_selection(["image/png"], Bytes::make(10, b'b'))
let buf : FixedArray[Byte] = FixedArray::make(65536, b'\x00')
let mut got = 0
let mut all_first = true
for _i = 0; _i < 2000 && got < first.length(); _i = _i + 1 {
comp.server.pump_transfers()
if @sys.poll_read(reader, 5) > 0 {
let (n, _) = @sys.recvmsg_fd(reader, buf, 65536)
for j = 0; j < n; j = j + 1 {
all_first = all_first && buf[j] == b'a'
}
got = got + n
}
}
assert_eq(got, first.length())
assert_true(all_first)
assert_eq(comp.server.pending_transfers(), 0)
@sys.close_fd(reader)
@sys.close_fd(listener)
@sys.unlink(sink_path)
comp.close()
}