selftest.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env python3
  2. """Smoke-test client for the fprintd shim; mimics pam_fprintd's call flow.
  3. GetDevices -> ListEnrolledFingers -> Claim -> VerifyStart('any')
  4. waits for VerifyStatus (touch the sensor) until --timeout
  5. -> VerifyStop -> Release
  6. Exits 0 when a verify outcome (match/no-match) or a clean timeout was seen.
  7. Usage:
  8. # on the system bus (installed service):
  9. work/fprintd-shim/selftest.py [--user nebulon] [--timeout 12]
  10. # on a private session bus (no root needed):
  11. dbus-run-session -- bash -c '
  12. python3 work/fprintd-shim/fprintd-shim.py --session &
  13. sleep 1
  14. python3 work/fprintd-shim/selftest.py --session --user nebulon --timeout 12
  15. kill %1'
  16. """
  17. import argparse
  18. import sys
  19. import gi
  20. gi.require_version('Gio', '2.0')
  21. gi.require_version('GLib', '2.0')
  22. from gi.repository import Gio, GLib # noqa: E402
  23. BUS_NAME = 'net.reactivated.Fprint'
  24. MANAGER_PATH = '/net/reactivated/Fprint/Manager'
  25. IFACE_MANAGER = 'net.reactivated.Fprint.Manager'
  26. IFACE_DEVICE = 'net.reactivated.Fprint.Device'
  27. def main():
  28. parser = argparse.ArgumentParser()
  29. parser.add_argument('--session', action='store_true')
  30. parser.add_argument('--user', default='nebulon')
  31. parser.add_argument('--timeout', type=float, default=12.0,
  32. help='seconds to wait for a verify result')
  33. args = parser.parse_args()
  34. bus_type = Gio.BusType.SESSION if args.session else Gio.BusType.SYSTEM
  35. bus = Gio.bus_get_sync(bus_type, None)
  36. manager = Gio.DBusProxy.new_sync(
  37. bus, Gio.DBusProxyFlags.NONE, None, BUS_NAME, MANAGER_PATH,
  38. IFACE_MANAGER, None)
  39. devices = manager.call_sync('GetDevices', None,
  40. Gio.DBusCallFlags.NONE, 5000, None).unpack()[0]
  41. print(f'GetDevices -> {list(devices)}')
  42. if not devices:
  43. print('FAIL: no devices')
  44. return 1
  45. default = manager.call_sync('GetDefaultDevice', None,
  46. Gio.DBusCallFlags.NONE, 5000, None).unpack()[0]
  47. print(f'GetDefaultDevice -> {default}')
  48. device = Gio.DBusProxy.new_sync(
  49. bus, Gio.DBusProxyFlags.NONE, None, BUS_NAME, default,
  50. IFACE_DEVICE, None)
  51. print(f'properties -> name={device.get_cached_property("name")!r} '
  52. f'scan-type={device.get_cached_property("scan-type")!r} '
  53. f'num-enroll-stages={device.get_cached_property("num-enroll-stages")!r}')
  54. result = {'status': None, 'reason': 'timeout before any verify result'}
  55. loop = GLib.MainLoop()
  56. def on_signal(_conn, _sender, _path, iface, signal, params):
  57. if iface != IFACE_DEVICE:
  58. return
  59. if signal == 'VerifyFingerSelected':
  60. print(f'VerifyFingerSelected -> {params.unpack()[0]}')
  61. elif signal == 'VerifyStatus':
  62. status, done = params.unpack()
  63. print(f'VerifyStatus -> {status} (done={done})')
  64. result['status'] = status
  65. result['reason'] = 'terminal verify status'
  66. if done:
  67. loop.quit()
  68. bus.signal_subscribe(BUS_NAME, IFACE_DEVICE, None, default, None,
  69. Gio.DBusSignalFlags.NONE, on_signal)
  70. try:
  71. fingers = device.call_sync('ListEnrolledFingers', GLib.Variant('(s)', (args.user,)),
  72. Gio.DBusCallFlags.NONE, 5000, None).unpack()[0]
  73. print(f'ListEnrolledFingers -> {list(fingers)}')
  74. except GLib.Error as e:
  75. print(f'ListEnrolledFingers -> error {e.message}')
  76. fingers = []
  77. if not fingers:
  78. print('FAIL: no enrolled fingers reported')
  79. return 1
  80. device.call_sync('Claim', GLib.Variant('(s)', (args.user,)),
  81. Gio.DBusCallFlags.NONE, 5000, None)
  82. print('Claim -> ok')
  83. print(f' (touch the sensor, waiting {args.timeout:.0f}s)')
  84. device.call_sync('VerifyStart', GLib.Variant('(s)', ('any',)),
  85. Gio.DBusCallFlags.NONE, 5000, None)
  86. def give_up():
  87. result['reason'] = 'timeout waiting for a verify result'
  88. loop.quit()
  89. return GLib.SOURCE_REMOVE
  90. GLib.timeout_add(int(args.timeout * 1000), give_up)
  91. loop.run()
  92. try:
  93. device.call_sync('VerifyStop', None, Gio.DBusCallFlags.NONE, 5000, None)
  94. print('VerifyStop -> ok')
  95. except GLib.Error as e:
  96. print(f'VerifyStop -> {e.message}')
  97. device.call_sync('Release', None, Gio.DBusCallFlags.NONE, 5000, None)
  98. print('Release -> ok')
  99. print(f'result: {result["reason"]}'
  100. + (f' ({result["status"]})' if result['status'] else ''))
  101. if result['status'] == 'verify-match':
  102. return 0
  103. if result['status'] == 'verify-no-match':
  104. return 1
  105. return 2
  106. if __name__ == '__main__':
  107. sys.exit(main())