#!/usr/bin/env python3 """API semantics regression test for the fprintd shim (session bus). Checks the fprintd contract against our shim, no sensor touch required: * GetDevices / GetDefaultDevice * ListEnrolledFingers for the enrolled user vs another user * Claim twice -> AlreadyInUse * VerifyStart unclaimed -> ClaimDevice * VerifyStart other client -> error * Release other client -> error * VerifyStart unenrolled finger -> NoEnrolledPrints * claim owner disconnect -> automatic release (no stale claim) * EnrollStart validation -> InvalidFingername / ClaimDevice * EnrollStop idle -> NoActionInProgress * DeleteEnrolledFinger of a non-enrolled finger -> NoEnrolledPrints * DeleteEnrolledFingers other user -> PermissionDenied * deleting one of several reported fingers is metadata-only (no sensor access - the sensor holds a single template) Usage: dbus-run-session -- bash -c ' FP_SHIM_STATE_DIR=/tmp/opencode/shim-state-test \ python3 work/fprintd-shim/fprintd-shim.py --session & sleep 1 python3 work/fprintd-shim/apitest.py' Note: g_bus_get_sync() returns a per-process singleton connection, so other clients are spawned as real subprocesses (--client mode). """ import json import os import subprocess import sys import time import gi gi.require_version('Gio', '2.0') gi.require_version('GLib', '2.0') from gi.repository import Gio, GLib # noqa: E402 BUS_NAME = 'net.reactivated.Fprint' MANAGER_PATH = '/net/reactivated/Fprint/Manager' IFACE_MANAGER = 'net.reactivated.Fprint.Manager' IFACE_DEVICE = 'net.reactivated.Fprint.Device' DEVICE_PATH = '/net/reactivated/Fprint/Device/0' failures = [] def check(name, cond, detail=''): print(f'{"ok " if cond else "FAIL"} {name}' + (f' [{detail}]' if detail else '')) if not cond: failures.append(name) def call(proxy, method, params=None): try: return True, proxy.call_sync(method, params, Gio.DBusCallFlags.NONE, 5000, None) except GLib.Error as e: return False, e def make_proxies(): bus = Gio.bus_get_sync(Gio.BusType.SESSION, None) device = Gio.DBusProxy.new_sync(bus, Gio.DBusProxyFlags.NONE, None, BUS_NAME, DEVICE_PATH, IFACE_DEVICE, None) return device def write_state(users): """Seed the shim's state file (FP_SHIM_STATE_DIR must be set for the test).""" state_dir = os.environ.get('FP_SHIM_STATE_DIR', '/var/lib/fprintd-shim') os.makedirs(state_dir, exist_ok=True) with open(os.path.join(state_dir, 'state.json'), 'w') as f: json.dump({'users': users}, f) def client_main(mode): """One-shot client process: perform `mode`, print result, exit.""" device = make_proxies() if mode == 'claim': ok, res = call(device, 'Claim', GLib.Variant('(s)', ('nebulon',))) print('ok' if ok else f'error {res.message}') return 0 if mode == 'verify-start': ok, res = call(device, 'VerifyStart', GLib.Variant('(s)', ('any',))) print('ok' if ok else f'error {res.message}') return 0 if mode == 'enroll-start-invalid': ok, res = call(device, 'EnrollStart', GLib.Variant('(s)', ('any',))) print('ok' if ok else f'error {res.message}') return 0 if mode == 'enroll-stop': ok, res = call(device, 'EnrollStop') print('ok' if ok else f'error {res.message}') return 0 if mode == 'release': ok, res = call(device, 'Release') print('ok' if ok else f'error {res.message}') return 0 if mode == 'claim-and-exit': ok, res = call(device, 'Claim', GLib.Variant('(s)', ('nebulon',))) print('ok' if ok else f'error {res.message}') return 0 print(f'unknown mode {mode}') return 2 def spawn_client(mode): proc = subprocess.run([sys.executable, __file__, '--client', mode], capture_output=True, text=True, timeout=30) return proc.stdout.strip() def main(): bus = Gio.bus_get_sync(Gio.BusType.SESSION, None) manager = Gio.DBusProxy.new_sync(bus, Gio.DBusProxyFlags.NONE, None, BUS_NAME, MANAGER_PATH, IFACE_MANAGER, None) ok, devices = call(manager, 'GetDevices') devices = devices.unpack()[0] if ok else [] check('GetDevices returns one device', len(devices) == 1, str(list(devices))) if not devices: return 1 device = make_proxies() ok, res = call(device, 'ListEnrolledFingers', GLib.Variant('(s)', ('nebulon',))) check('ListEnrolledFingers(nebulon) -> right-index-finger', ok and res.unpack()[0] == ['right-index-finger'], str(res.unpack()[0]) if ok else res.message) out = spawn_client('verify-start') check('VerifyStart without claim -> ClaimDevice', 'error' in out and 'ClaimDevice' in out, out) ok, res = call(device, 'ListEnrolledFingers', GLib.Variant('(s)', ('someone-else',))) check('ListEnrolledFingers(other user) -> NoEnrolledPrints', not ok and 'NoEnrolledPrints' in str(res), '') ok, res = call(device, 'Claim', GLib.Variant('(s)', ('nebulon',))) check('Claim (client A) ok', ok, '' if ok else res.message) out = spawn_client('claim') check('second Claim -> AlreadyInUse', 'AlreadyInUse' in out, out) out = spawn_client('verify-start') check('VerifyStart from non-claimer -> error', 'error' in out, out) out = spawn_client('release') check('Release from non-claimer -> AlreadyInUse', 'AlreadyInUse' in out, out) ok, res = call(device, 'VerifyStart', GLib.Variant('(s)', ('left-thumb',))) check('VerifyStart(unenrolled finger) -> NoEnrolledPrints', not ok and 'NoEnrolledPrints' in str(res), '' if not ok else 'no error') call(device, 'Release') # Claim owner vanishes -> shim must drop the claim (pam_fprintd relies on # this: on PAM success it just disconnects without calling Release). out = spawn_client('claim-and-exit') check('client B claim ok', out == 'ok', out) time.sleep(0.5) # let the shim's NameOwnerChanged handler run ok, res = call(device, 'Claim', GLib.Variant('(s)', ('nebulon',))) check('claim after claimer disconnect ok (auto-release)', ok, '' if ok else res.message) # ---------------------------------------------------- enroll/delete API # (client A holds the claim from the auto-release check above) ok2, res = call(device, 'EnrollStart', GLib.Variant('(s)', ('any',))) check('EnrollStart("any") -> InvalidFingername', not ok2 and 'InvalidFingername' in str(res), '' if not ok2 else 'no error') ok2, res = call(device, 'EnrollStop') check('EnrollStop (claimed, idle) -> NoActionInProgress', not ok2 and 'NoActionInProgress' in str(res), '' if not ok2 else 'no error') call(device, 'Release') out = spawn_client('enroll-stop') check('EnrollStop (unclaimed) -> ClaimDevice', 'ClaimDevice' in out, out) ok2, res = call(device, 'DeleteEnrolledFingers', GLib.Variant('(s)', ('someone-else',))) check('DeleteEnrolledFingers(other user) -> PermissionDenied', not ok2 and 'PermissionDenied' in str(res), '' if not ok2 else 'no error') ok2, res = call(device, 'Claim', GLib.Variant('(s)', ('nebulon',))) check('re-claim for delete tests', ok2, '' if ok2 else res.message) if not ok2: print() print(f'{len(failures)} failure(s): {", ".join(failures)}') return 1 ok2, res = call(device, 'DeleteEnrolledFinger', GLib.Variant('(s)', ('left-thumb',))) check('DeleteEnrolledFinger(unenrolled) -> NoEnrolledPrints', not ok2 and 'NoEnrolledPrints' in str(res), '' if not ok2 else 'no error') # Deleting one of several reported fingers must be metadata-only (the # sensor holds a single template; no Wine run). Seed two fingers. write_state({'nebulon': ['right-index-finger', 'left-thumb']}) ok2, res = call(device, 'DeleteEnrolledFinger', GLib.Variant('(s)', ('left-thumb',))) check('DeleteEnrolledFinger(enrolled) ok', ok2, '' if ok2 else res.message) ok2, res = call(device, 'ListEnrolledFingers', GLib.Variant('(s)', ('nebulon',))) check('deleted finger gone, other stays', ok2 and res.unpack()[0] == ['right-index-finger'], str(res.unpack()[0]) if ok2 else res.message) ok2, res = call(device, 'DeleteEnrolledFinger', GLib.Variant('(s)', ('left-thumb',))) check('deleting it again -> NoEnrolledPrints', not ok2 and 'NoEnrolledPrints' in str(res), '' if not ok2 else 'no error') call(device, 'Release') print() if failures: print(f'{len(failures)} failure(s): {", ".join(failures)}') return 1 print('all API checks passed') return 0 if __name__ == '__main__': if '--client' in sys.argv: sys.exit(client_main(sys.argv[sys.argv.index('--client') + 1])) sys.exit(main())