apitest.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/usr/bin/env python3
  2. """API semantics regression test for the fprintd shim (session bus).
  3. Checks the fprintd contract against our shim, no sensor touch required:
  4. * GetDevices / GetDefaultDevice
  5. * ListEnrolledFingers for the enrolled user vs another user
  6. * Claim twice -> AlreadyInUse
  7. * VerifyStart unclaimed -> ClaimDevice
  8. * VerifyStart other client -> error
  9. * Release other client -> error
  10. * VerifyStart unenrolled finger -> NoEnrolledPrints
  11. * claim owner disconnect -> automatic release (no stale claim)
  12. * EnrollStart validation -> InvalidFingername / ClaimDevice
  13. * EnrollStop idle -> NoActionInProgress
  14. * DeleteEnrolledFinger of a non-enrolled finger -> NoEnrolledPrints
  15. * DeleteEnrolledFingers other user -> PermissionDenied
  16. * deleting one of several reported fingers is metadata-only (no sensor
  17. access - the sensor holds a single template)
  18. Usage:
  19. dbus-run-session -- bash -c '
  20. FP_SHIM_STATE_DIR=/tmp/opencode/shim-state-test \
  21. python3 work/fprintd-shim/fprintd-shim.py --session &
  22. sleep 1
  23. python3 work/fprintd-shim/apitest.py'
  24. Note: g_bus_get_sync() returns a per-process singleton connection, so other
  25. clients are spawned as real subprocesses (--client mode).
  26. """
  27. import json
  28. import os
  29. import subprocess
  30. import sys
  31. import time
  32. import gi
  33. gi.require_version('Gio', '2.0')
  34. gi.require_version('GLib', '2.0')
  35. from gi.repository import Gio, GLib # noqa: E402
  36. BUS_NAME = 'net.reactivated.Fprint'
  37. MANAGER_PATH = '/net/reactivated/Fprint/Manager'
  38. IFACE_MANAGER = 'net.reactivated.Fprint.Manager'
  39. IFACE_DEVICE = 'net.reactivated.Fprint.Device'
  40. DEVICE_PATH = '/net/reactivated/Fprint/Device/0'
  41. failures = []
  42. def check(name, cond, detail=''):
  43. print(f'{"ok " if cond else "FAIL"} {name}' + (f' [{detail}]' if detail else ''))
  44. if not cond:
  45. failures.append(name)
  46. def call(proxy, method, params=None):
  47. try:
  48. return True, proxy.call_sync(method, params, Gio.DBusCallFlags.NONE, 5000, None)
  49. except GLib.Error as e:
  50. return False, e
  51. def make_proxies():
  52. bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
  53. device = Gio.DBusProxy.new_sync(bus, Gio.DBusProxyFlags.NONE, None,
  54. BUS_NAME, DEVICE_PATH, IFACE_DEVICE, None)
  55. return device
  56. def write_state(users):
  57. """Seed the shim's state file (FP_SHIM_STATE_DIR must be set for the test)."""
  58. state_dir = os.environ.get('FP_SHIM_STATE_DIR', '/var/lib/fprintd-shim')
  59. os.makedirs(state_dir, exist_ok=True)
  60. with open(os.path.join(state_dir, 'state.json'), 'w') as f:
  61. json.dump({'users': users}, f)
  62. def client_main(mode):
  63. """One-shot client process: perform `mode`, print result, exit."""
  64. device = make_proxies()
  65. if mode == 'claim':
  66. ok, res = call(device, 'Claim', GLib.Variant('(s)', ('nebulon',)))
  67. print('ok' if ok else f'error {res.message}')
  68. return 0
  69. if mode == 'verify-start':
  70. ok, res = call(device, 'VerifyStart', GLib.Variant('(s)', ('any',)))
  71. print('ok' if ok else f'error {res.message}')
  72. return 0
  73. if mode == 'enroll-start-invalid':
  74. ok, res = call(device, 'EnrollStart', GLib.Variant('(s)', ('any',)))
  75. print('ok' if ok else f'error {res.message}')
  76. return 0
  77. if mode == 'enroll-stop':
  78. ok, res = call(device, 'EnrollStop')
  79. print('ok' if ok else f'error {res.message}')
  80. return 0
  81. if mode == 'release':
  82. ok, res = call(device, 'Release')
  83. print('ok' if ok else f'error {res.message}')
  84. return 0
  85. if mode == 'claim-and-exit':
  86. ok, res = call(device, 'Claim', GLib.Variant('(s)', ('nebulon',)))
  87. print('ok' if ok else f'error {res.message}')
  88. return 0
  89. print(f'unknown mode {mode}')
  90. return 2
  91. def spawn_client(mode):
  92. proc = subprocess.run([sys.executable, __file__, '--client', mode],
  93. capture_output=True, text=True, timeout=30)
  94. return proc.stdout.strip()
  95. def main():
  96. bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
  97. manager = Gio.DBusProxy.new_sync(bus, Gio.DBusProxyFlags.NONE, None,
  98. BUS_NAME, MANAGER_PATH, IFACE_MANAGER, None)
  99. ok, devices = call(manager, 'GetDevices')
  100. devices = devices.unpack()[0] if ok else []
  101. check('GetDevices returns one device', len(devices) == 1, str(list(devices)))
  102. if not devices:
  103. return 1
  104. device = make_proxies()
  105. ok, res = call(device, 'ListEnrolledFingers', GLib.Variant('(s)', ('nebulon',)))
  106. check('ListEnrolledFingers(nebulon) -> right-index-finger',
  107. ok and res.unpack()[0] == ['right-index-finger'],
  108. str(res.unpack()[0]) if ok else res.message)
  109. out = spawn_client('verify-start')
  110. check('VerifyStart without claim -> ClaimDevice',
  111. 'error' in out and 'ClaimDevice' in out, out)
  112. ok, res = call(device, 'ListEnrolledFingers', GLib.Variant('(s)', ('someone-else',)))
  113. check('ListEnrolledFingers(other user) -> NoEnrolledPrints',
  114. not ok and 'NoEnrolledPrints' in str(res), '')
  115. ok, res = call(device, 'Claim', GLib.Variant('(s)', ('nebulon',)))
  116. check('Claim (client A) ok', ok, '' if ok else res.message)
  117. out = spawn_client('claim')
  118. check('second Claim -> AlreadyInUse', 'AlreadyInUse' in out, out)
  119. out = spawn_client('verify-start')
  120. check('VerifyStart from non-claimer -> error', 'error' in out, out)
  121. out = spawn_client('release')
  122. check('Release from non-claimer -> AlreadyInUse', 'AlreadyInUse' in out, out)
  123. ok, res = call(device, 'VerifyStart', GLib.Variant('(s)', ('left-thumb',)))
  124. check('VerifyStart(unenrolled finger) -> NoEnrolledPrints',
  125. not ok and 'NoEnrolledPrints' in str(res), '' if not ok else 'no error')
  126. call(device, 'Release')
  127. # Claim owner vanishes -> shim must drop the claim (pam_fprintd relies on
  128. # this: on PAM success it just disconnects without calling Release).
  129. out = spawn_client('claim-and-exit')
  130. check('client B claim ok', out == 'ok', out)
  131. time.sleep(0.5) # let the shim's NameOwnerChanged handler run
  132. ok, res = call(device, 'Claim', GLib.Variant('(s)', ('nebulon',)))
  133. check('claim after claimer disconnect ok (auto-release)', ok,
  134. '' if ok else res.message)
  135. # ---------------------------------------------------- enroll/delete API
  136. # (client A holds the claim from the auto-release check above)
  137. ok2, res = call(device, 'EnrollStart', GLib.Variant('(s)', ('any',)))
  138. check('EnrollStart("any") -> InvalidFingername',
  139. not ok2 and 'InvalidFingername' in str(res),
  140. '' if not ok2 else 'no error')
  141. ok2, res = call(device, 'EnrollStop')
  142. check('EnrollStop (claimed, idle) -> NoActionInProgress',
  143. not ok2 and 'NoActionInProgress' in str(res), '' if not ok2 else 'no error')
  144. call(device, 'Release')
  145. out = spawn_client('enroll-stop')
  146. check('EnrollStop (unclaimed) -> ClaimDevice', 'ClaimDevice' in out, out)
  147. ok2, res = call(device, 'DeleteEnrolledFingers', GLib.Variant('(s)', ('someone-else',)))
  148. check('DeleteEnrolledFingers(other user) -> PermissionDenied',
  149. not ok2 and 'PermissionDenied' in str(res), '' if not ok2 else 'no error')
  150. ok2, res = call(device, 'Claim', GLib.Variant('(s)', ('nebulon',)))
  151. check('re-claim for delete tests', ok2, '' if ok2 else res.message)
  152. if not ok2:
  153. print()
  154. print(f'{len(failures)} failure(s): {", ".join(failures)}')
  155. return 1
  156. ok2, res = call(device, 'DeleteEnrolledFinger', GLib.Variant('(s)', ('left-thumb',)))
  157. check('DeleteEnrolledFinger(unenrolled) -> NoEnrolledPrints',
  158. not ok2 and 'NoEnrolledPrints' in str(res), '' if not ok2 else 'no error')
  159. # Deleting one of several reported fingers must be metadata-only (the
  160. # sensor holds a single template; no Wine run). Seed two fingers.
  161. write_state({'nebulon': ['right-index-finger', 'left-thumb']})
  162. ok2, res = call(device, 'DeleteEnrolledFinger', GLib.Variant('(s)', ('left-thumb',)))
  163. check('DeleteEnrolledFinger(enrolled) ok', ok2, '' if ok2 else res.message)
  164. ok2, res = call(device, 'ListEnrolledFingers', GLib.Variant('(s)', ('nebulon',)))
  165. check('deleted finger gone, other stays',
  166. ok2 and res.unpack()[0] == ['right-index-finger'],
  167. str(res.unpack()[0]) if ok2 else res.message)
  168. ok2, res = call(device, 'DeleteEnrolledFinger', GLib.Variant('(s)', ('left-thumb',)))
  169. check('deleting it again -> NoEnrolledPrints',
  170. not ok2 and 'NoEnrolledPrints' in str(res), '' if not ok2 else 'no error')
  171. call(device, 'Release')
  172. print()
  173. if failures:
  174. print(f'{len(failures)} failure(s): {", ".join(failures)}')
  175. return 1
  176. print('all API checks passed')
  177. return 0
  178. if __name__ == '__main__':
  179. if '--client' in sys.argv:
  180. sys.exit(client_main(sys.argv[sys.argv.index('--client') + 1]))
  181. sys.exit(main())