Files
chandra/compositor/host/face_test.mbt
T
Claude b097116ded
CI / test (push) Successful in 2m41s
Face unlock: Python, OpenCV and numpy from a root-owned pixi environment
- face/pixi.toml + pixi.lock (python 3.12, numpy, opencv-python-headless). `just install-face`
  passes the pixi on your PATH; install.py runs `pixi install --frozen` as root into
  /usr/local/lib/chandra/face-env (not the checkout's .pixi: the PAM module runs this Python as
  root, so it must not be user-writable), writes the helper's shebang for it and builds the PAM
  module with it as the default python. No python3-opencv needed; uninstall removes the env.
- The PAM module now also refuses a Python that isn't root's; group-writable is accepted only
  for group root (conda/pixi files are 0775 root:root), in the module and chandra-face's checks.
- Elevation re-runs on the same interpreter (sys.executable). "OpenCV is missing: run just
  install-face" instead of naming the distro package. Dropped a stray __pycache__.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hhiJfJNAYqWzQiuKMf
2026-11-26 01:34:45 +00:00

123 lines
5.1 KiB
MoonBit

///| Face unlock in Settings > Session (docs/face-unlock.md), against a fake chandra-face: a shell
/// script that answers `status` from files and changes them for enroll / remove / pam, the way
/// the real one does after run0 or pkexec. CHANDRA_AUTH_SHOTS=dir also saves a PNG of the tab.
///| Write the fake tool into `dir` and return its path.
fn fake_face_tool(dir : String) -> String {
let script =
#|#!/bin/sh
#|S="$(dirname "$0")"
#|case "$1" in
#|status)
#| echo "backend opencv"; echo "device /dev/video2"
#| echo "pam $(cat "$S/pam" 2>/dev/null)"
#| cat "$S/faces" 2>/dev/null ;;
#|enroll)
#| echo "info Look at the camera…"
#| for i in 1 2 3 4 5; do echo "progress $i/5"; done
#| printf 'face 2\tFace 2\t2026-11-26\n' >> "$S/faces"
#| echo "done Added Face 2" ;;
#|remove)
#| grep -v "^face $2 " "$S/faces" > "$S/faces.new"; mv "$S/faces.new" "$S/faces"
#| echo "done Removed face $2" ;;
#|pam)
#| if [ "$2" = enable ]; then echo "polkit-1,sudo" > "$S/pam"; else : > "$S/pam"; fi
#| echo "done ok" ;;
#|esac
ignore(@sys.mkdir_p(dir))
let tool = dir + "/chandra-face"
ignore(@sys.write_file_bytes(tool, @sys.str_to_utf8_bytes(script)))
ignore(@sys.write_file_bytes(dir + "/faces", @sys.str_to_utf8_bytes("face 1\tDesk\t2026-11-20\n")))
tool
}
///| Compose frames until `cond` holds (the tool runs on a socket), at most ~3 s.
fn face_settle(comp : @host.ChandraCompositor, cond : () -> Bool) -> Bool {
for i = 0; i < 300; i = i + 1 {
ignore(comp.compose_frame())
if cond() {
return true
}
@sys.sleep_ms(10)
}
false
}
///| The popup's texts.
fn face_texts(comp : @host.ChandraCompositor) -> Array[String] {
let slot = comp.shell.slot_of("session-settings")
comp.shell.slot_popup_texts(slot).map(t => t.0)
}
///| Click the popup text `label`.
fn face_click(comp : @host.ChandraCompositor, label : String, t : UInt) -> Unit raise {
let slot = comp.shell.slot_of("session-settings")
let pr = comp.shell.slot_popup_rect(slot)
match comp.shell.slot_popup_texts(slot).iter().find_first(x => x.0 == label) {
Some((_, x, y)) => {
comp.handle_input(@nested.InputEvent::PointerMove((pr.x + x + 4).to_double(), (pr.y + y).to_double()), t)
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), t + 1U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), t + 2U)
ignore(comp.compose_frame())
}
None => fail("no \"" + label + "\" in the popup")
}
}
///|
test "Settings > Session: face unlock lists faces, adds one, turns it on, removes one" {
let dir = "/tmp/chandra-face-test-" + @sys.getpid().to_string()
let tool = fake_face_tool(dir)
let comp = new_comp("face")
// spawn-socket runs a program; the script has no x bit (sys has no chmod): through sh.
comp.shell.set_face_tool(["/bin/sh", tool])
assert_true(comp.shell.open_settings(Session))
assert_true(face_settle(comp, fn() { face_texts(comp).contains("Desk") }))
let texts = face_texts(comp)
assert_true(texts.contains("Face unlock"))
assert_true(texts.contains("Use my face for passwords"))
assert_true(texts.contains("added 2026-11-20"))
assert_true(texts.contains("Add a face…"))
assert_false(comp.shell.face.in_use())
// Add a face: the tool's progress shows, then the new face.
face_click(comp, "Add a face…", 10U)
assert_true(face_settle(comp, fn() { face_texts(comp).contains("Face 2") }))
assert_true(face_texts(comp).contains("Added Face 2"))
// The switch: polkit-1 and sudo try the face first.
face_click(comp, "Use my face for passwords", 20U)
assert_true(face_settle(comp, fn() { comp.shell.face.in_use() }))
let shots = @sys.getenv("CHANDRA_AUTH_SHOTS")
if shots != "" {
ignore(@sys.write_file_bytes(shots + "/16-settings-face.png", @renderer.encode_png_compressed(comp.compose_frame())))
}
// Remove takes a second click.
face_click(comp, "Remove", 30U)
assert_true(face_texts(comp).contains("Click again to remove"))
assert_eq(comp.shell.face.faces.length(), 2)
face_click(comp, "Click again to remove", 40U)
assert_true(face_settle(comp, fn() { comp.shell.face.faces.length() == 1 }))
assert_eq(comp.shell.face.faces[0].1, "Face 2")
comp.close()
}
///|
test "face unlock status lines" {
let f = @shell.FaceUnlock::new()
f.apply_status([
"backend opencv", "device /dev/video0", "problem OpenCV is missing: run just install-face", "pam polkit-1,sudo",
"howdy login,gdm-password", "face 3\tGlasses\t2026-11-01",
])
assert_true(f.installed)
assert_eq(f.problem, "OpenCV is missing: run just install-face")
assert_true(f.in_use())
assert_eq(f.howdy, "login, gdm-password")
assert_eq(f.faces, [("3", "Glasses", "2026-11-01")])
// No tool: nothing is installed, whatever else was said.
f.apply_status([])
assert_false(f.installed)
assert_false(f.in_use())
assert_eq(@shell.face_progress("progress 2/5"), Some("Look at the camera… 2/5"))
assert_eq(@shell.face_progress("error couldn't see a face clearly"), Some("Couldn't see a face clearly"))
assert_eq(@shell.face_progress("face 1\tDesk\t2026-11-20"), None)
}