///| Session lock (lock.mbt): the compositor state machine, the `lock` control command, power /// button / `screen` blanking, lock-screen hiding/backdrop, input gating, and end-to-end /// polkit `unix-process` unlock + cancel/power-off/agent-restart flows. ///| Whether any draw op renders `text`. fn lock_has_text(ops : Array[@scene.DrawOp], text : String) -> Bool { ops.iter().any(op => match op { Text(_, _, t, _, _, _, _) | MonoText(_, _, t, _, _, _, _) => t == text _ => false }) } ///| Drain all lines currently queued on a test link. fn lock_drain_lines(conn : @polkit.LineConn) -> Array[Array[String]] { let out : Array[Array[String]] = [] for line in conn.read_lines() { out.push(@polkit.decode_line(line)) } out } ///| 1. Control commands: `lock`, `lock status`, `lock on-blank [on|off|status]`, invalid args. test "lock: chandra-ctl lock, status, and on-blank toggles" { let comp = new_comp("lock-cmd") assert_false(comp.is_locked()) assert_eq(comp.control_command("lock status").0, ["ok unlocked"]) assert_eq(comp.control_command("lock on-blank status").0, ["ok off"]) assert_eq(comp.control_command("lock on-blank").0, ["ok off"]) assert_eq(comp.control_command("lock on-blank on").0, ["ok on"]) assert_true(comp.lock_on_blank) assert_eq(comp.control_command("lock on-blank status").0, ["ok on"]) assert_eq(comp.control_command("lock on-blank off").0, ["ok off"]) assert_false(comp.lock_on_blank) let (out, ok) = comp.control_command("lock") assert_true(ok) assert_eq(out, ["ok locked"]) assert_true(comp.is_locked()) assert_eq(comp.control_command("lock status").0, ["ok locked"]) // Locking again while already locked is a harmless no-op. assert_true(comp.control_command("lock lock").1) assert_true(comp.is_locked()) comp.unlocked() assert_false(comp.is_locked()) // Bad arguments are rejected. assert_false(comp.control_command("lock bogus").1) assert_false(comp.control_command("lock status extra").1) assert_false(comp.control_command("lock on-blank maybe").1) assert_false(comp.control_command("lock on-blank on extra").1) comp.close() } ///| 2. Power button and `screen off/on` consistency. test "lock: power button and screen off/on respect lock_on_blank and wake locked" { let comp = new_comp("lock-power") // Off by default: blanking does not lock. assert_false(comp.lock_on_blank) comp.handle_input(@nested.InputEvent::Key(116U, true), 10U) assert_true(comp.manual_screen_off) assert_false(comp.is_locked()) comp.handle_input(@nested.InputEvent::Key(116U, true), 20U) assert_false(comp.manual_screen_off) assert_false(comp.is_locked()) // Enabled via on-blank: both power button and `screen off` lock, and waking requests unlock. assert_eq(comp.control_command("lock on-blank on").0, ["ok on"]) comp.handle_input(@nested.InputEvent::Key(116U, true), 30U) assert_true(comp.manual_screen_off) assert_true(comp.is_locked()) comp.handle_input(@nested.InputEvent::Key(116U, true), 40U) assert_false(comp.manual_screen_off) assert_true(comp.is_locked()) comp.unlocked() assert_false(comp.is_locked()) assert_eq(comp.control_command("screen off").0, ["ok off"]) assert_true(comp.manual_screen_off) assert_true(comp.is_locked()) assert_eq(comp.control_command("screen on").0, ["ok on"]) assert_false(comp.manual_screen_off) assert_true(comp.is_locked()) comp.close() } ///| 3. Locked scene hides windows, popups, desktop widgets, and panel while drawing lock backdrop. test "lock: locked scene hides desktop, windows, popups and panel behind lock backdrop" { let sock = "/tmp/wayland-moon-lock-scene-" + @sys.getpid().to_string() + ".sock" let theme = @scene.ThemeTokens::moon_night() let comp = @host.ChandraCompositor::new(sock, theme, 1280, 720).unwrap() let (c, shm, surf, xdg, _tl, _deco) = open_and_configure_client(comp, sock, "Secret Notes", "notes") commit_shm_frame(comp, c, shm, surf, xdg, 1UL) match comp.compose_scene() { Some(unlocked_scene) => { assert_true(unlocked_scene.windows.length() >= 1) assert_true(unlocked_scene.panel_ops.length() > 0) } None => fail("expected initial unlocked scene") } comp.lock_now() assert_true(comp.is_locked()) assert_true(comp.kbd_win is None) match comp.compose_scene() { Some(locked_scene) => { assert_eq(locked_scene.windows.length(), 0) assert_eq(locked_scene.popups.length(), 0) assert_eq(locked_scene.background_ops.length(), 0) assert_true(lock_has_text(locked_scene.panel_ops, "Screen locked")) assert_true(lock_has_text(locked_scene.panel_ops, "Tap or press any key to unlock")) } None => fail("expected locked scene") } comp.close() } ///| 4. Input gating when locked with no dialog open: only presses re-trigger unlock. test "lock: pointer move and key release do not re-trigger unlock; key/button press does" { let comp = new_comp("lock-input") let (mine, theirs) = sock_pair("lock-input") let peer = @polkit.LineConn::new(theirs) comp.auth_agent = Some({ conn: @polkit.LineConn::new(mine), demo: false, ready: true, error: "", started: 0UL, view: None, }) comp.lock_now() assert_true(comp.unlock_pending) assert_eq(lock_drain_lines(peer), [["unlock"]]) // Simulate previous unlock attempt failed -> dialog dismissed, prompt hidden. comp.unlock_failed() assert_false(comp.unlock_pending) // Pointer motion and releases must be swallowed WITHOUT sending `unlock`. comp.handle_input(@nested.InputEvent::PointerMove(50.0, 60.0), 10U) comp.handle_input(@nested.InputEvent::Key(28U, false), 11U) comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 12U) assert_false(comp.unlock_pending) assert_eq(lock_drain_lines(peer), []) // Key press triggers unlock once. comp.handle_input(@nested.InputEvent::Key(28U, true), 13U) assert_true(comp.unlock_pending) assert_eq(lock_drain_lines(peer), [["unlock"]]) // Simulate another failed attempt, then button press triggers unlock. comp.unlock_failed() assert_false(comp.unlock_pending) comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 14U) assert_true(comp.unlock_pending) assert_eq(lock_drain_lines(peer), [["unlock"]]) peer.close() comp.close() } ///| 5. Demo agent cannot unlock a locked session. test "lock: demo link unlocked message is ignored" { let comp = new_comp("lock-demo-ignore") let (mine, theirs) = sock_pair("lock-demo-ignore") let peer = @polkit.LineConn::new(theirs) comp.auth_demos.push({ conn: @polkit.LineConn::new(mine), demo: true, ready: true, error: "", started: 0UL, view: None, }) comp.lock_now() assert_true(comp.is_locked()) ignore(peer.send(["unlocked"])) comp.step() assert_true(comp.is_locked()) peer.close() comp.close() } ///| 6. End-to-end lock -> unix-process CheckAuthorization -> password & face unlock -> desktop restored. test "lock: e2e unlock through mock polkitd (unix-process) succeeds and restores desktop" { let bus = match auth_bus("lock-e2e") { Some(b) => b None => return } let (hpath, listen) = helper_socket("lock-e2e") let r = rig("lock-e2e", Some(bus), hpath) r.comp.start_auth_agent() assert_true(r.settle(fn() { r.ready() })) let a = r.authority() r.comp.lock_now() assert_true(r.comp.is_locked()) assert_true(r.settle(fn() { r.comp.auth_view().map(v => v.phase == Asking || v.phase == Starting).unwrap_or(false) })) assert_eq(a.check_subjects.length(), 1) match a.check_subjects[0] { Struct([Str(kind), _]) => assert_eq(kind, "unix-process") _ => fail("expected unix-process subject") } assert_eq(r.view().title, "Screen locked") let (h, _) = r.helper_accept(listen) helper_say(h, "PAM_TEXT_INFO Face unlock: looking for nandi…") helper_say(h, "PAM_TEXT_INFO Face unlock: recognized nandi") helper_say(h, "SUCCESS") assert_true(r.settle(fn() { !r.comp.is_locked() && r.comp.auth_view() is None })) assert_false(r.comp.unlock_pending) @sys.close_fd(h) @sys.close_fd(listen) @sys.unlink(hpath) r.comp.close() } ///| 7. Cancel unlock stays locked, ignores pointer release, then tap re-prompts and succeeds. test "lock: cancelling unlock keeps session locked until next tap raises it again" { let bus = match auth_bus("lock-cancel") { Some(b) => b None => return } let (hpath, listen) = helper_socket("lock-cancel") let r = rig("lock-cancel", Some(bus), hpath) r.comp.start_auth_agent() assert_true(r.settle(fn() { r.ready() })) let a = r.authority() r.comp.lock_now() assert_true(r.settle(fn() { r.comp.auth_view() is Some(_) })) let (h1, _) = r.helper_accept(listen) helper_say(h1, "PAM_PROMPT_ECHO_OFF Password: ") assert_true(r.settle(fn() { r.comp.auth_view().map(v => v.phase == Asking).unwrap_or(false) })) // Dismiss via Cancel button -> agent says unlock-failed -> session stays locked. auth_click(r.comp, Cancel) assert_true(r.settle(fn() { r.comp.auth_view() is None && !r.comp.unlock_pending && a.answers.length() == 1 })) assert_true(r.comp.is_locked()) @sys.close_fd(h1) // Moving pointer / releasing does not immediately re-open the dialog. r.comp.handle_input(@nested.InputEvent::PointerMove(20.0, 20.0), 10U) r.comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 11U) r.pass() assert_false(r.comp.unlock_pending) assert_true(r.comp.auth_view() is None) // Tapping wakes a new unlock attempt and succeeds. r.comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 12U) r.comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 13U) assert_true(r.settle(fn() { r.comp.auth_view() is Some(_) })) let (h2, _) = r.helper_accept(listen) helper_say(h2, "PAM_PROMPT_ECHO_OFF Password: ") assert_true(r.settle(fn() { r.comp.auth_view().map(v => v.phase == Asking).unwrap_or(false) })) auth_type(r.comp, "Right1") auth_press(r.comp, 28U) assert_true(r.settle(fn() { r.comp.auth_view().map(v => v.phase == Checking).unwrap_or(false) })) assert_eq(helper_lines(h2, 1), "Right1") helper_say(h2, "SUCCESS") assert_true(r.settle(fn() { !r.comp.is_locked() && r.comp.auth_view() is None })) @sys.close_fd(h2) @sys.close_fd(listen) @sys.unlink(hpath) r.comp.close() } ///| 8. Power off while unlock is in-flight cancels CheckAuthorization and re-prompts cleanly on wake. test "lock: power off cancels in-flight unlock via CancelCheckAuthorization" { let bus = match auth_bus("lock-pwrcancel") { Some(b) => b None => return } let (hpath, listen) = helper_socket("lock-pwrcancel") let r = rig("lock-pwrcancel", Some(bus), hpath) r.comp.start_auth_agent() assert_true(r.settle(fn() { r.ready() })) let a = r.authority() r.comp.lock_now() assert_true(r.settle(fn() { r.comp.auth_view() is Some(_) })) let (h1, _) = r.helper_accept(listen) // Power button blanks screen -> cancels unlock & calls CancelCheckAuthorization. r.comp.handle_input(@nested.InputEvent::Key(116U, true), 20U) assert_true(r.comp.manual_screen_off) assert_true(r.comp.is_locked()) assert_false(r.comp.unlock_pending) assert_true(r.settle(fn() { a.cancelled_ids.length() == 1 && r.comp.auth_view() is None })) @sys.close_fd(h1) // Wake screen -> starts fresh unlock request cleanly. r.comp.handle_input(@nested.InputEvent::Key(116U, true), 30U) assert_false(r.comp.manual_screen_off) assert_true(r.settle(fn() { r.comp.auth_view() is Some(_) })) let (h2, _) = r.helper_accept(listen) helper_say(h2, "SUCCESS") assert_true(r.settle(fn() { !r.comp.is_locked() && r.comp.auth_view() is None })) @sys.close_fd(h2) @sys.close_fd(listen) @sys.unlink(hpath) r.comp.close() } ///| 9. Agent disconnect/restart while locked clears unlock_pending and re-requests on ready. test "lock: agent drop clears unlock_pending and restarted agent resumes unlock on ready" { let bus = match auth_bus("lock-restart") { Some(b) => b None => return } let (hpath, listen) = helper_socket("lock-restart") let r = rig("lock-restart", Some(bus), hpath) r.comp.start_auth_agent() assert_true(r.settle(fn() { r.ready() })) r.comp.lock_now() assert_true(r.settle(fn() { r.comp.auth_view() is Some(_) })) let (h1, _) = r.helper_accept(listen) // Drop the first agent while locked -> auth_tick clears unlock_pending and restarts a new agent. r.agents[0].shutdown() assert_true(r.settle(fn() { r.agents.length() >= 2 && r.comp.auth_view() is Some(_) })) @sys.close_fd(h1) let (h2, _) = r.helper_accept(listen) helper_say(h2, "SUCCESS") assert_true(r.settle(fn() { !r.comp.is_locked() && r.comp.auth_view() is None })) @sys.close_fd(h2) @sys.close_fd(listen) @sys.unlink(hpath) r.comp.close() }