| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
'''fprintd mock template |
| 4 |
|
| 5 |
This creates the expected methods and properties of the |
| 6 |
net.reactivated.Fprint.Manager object (/net/reactivated/Fprint/Manager) |
| 7 |
but no devices. |
| 8 |
''' |
| 9 |
|
| 10 |
# This program is free software; you can redistribute it and/or modify it under |
| 11 |
# the terms of the GNU Lesser General Public License as published by the Free |
| 12 |
# Software Foundation; either version 3 of the License, or (at your option) any |
| 13 |
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text |
| 14 |
# of the license. |
| 15 |
|
| 16 |
__author__ = 'Bastien Nocera' |
| 17 |
__email__ = '[email protected]' |
| 18 |
__copyright__ = '(c) 2020 Red Hat Inc.' |
| 19 |
__license__ = 'LGPL 3+' |
| 20 |
|
| 21 |
import sys |
| 22 |
from gi.repository import GLib |
| 23 |
import dbus |
| 24 |
import asyncio |
| 25 |
|
| 26 |
from dbusmock import MOCK_IFACE, mockobject |
| 27 |
|
| 28 |
BUS_NAME = 'net.reactivated.Fprint' |
| 29 |
MAIN_OBJ = '/net/reactivated/Fprint/Manager' |
| 30 |
SYSTEM_BUS = True |
| 31 |
IS_OBJECT_MANAGER = False |
| 32 |
|
| 33 |
MAIN_IFACE = 'net.reactivated.Fprint.Manager' |
| 34 |
MANAGER_MOCK_IFACE = 'net.reactivated.Fprint.Manager.Mock' |
| 35 |
|
| 36 |
DEVICE_IFACE = 'net.reactivated.Fprint.Device' |
| 37 |
DEVICE_MOCK_IFACE = 'net.reactivated.Fprint.Device.Mock' |
| 38 |
|
| 39 |
VALID_FINGER_NAMES = [ |
| 40 |
'left-thumb', |
| 41 |
'left-index-finger', |
| 42 |
'left-middle-finger', |
| 43 |
'left-ring-finger', |
| 44 |
'left-little-finger', |
| 45 |
'right-thumb', |
| 46 |
'right-index-finger', |
| 47 |
'right-middle-finger', |
| 48 |
'right-ring-finger', |
| 49 |
'right-little-finger' |
| 50 |
] |
| 51 |
|
| 52 |
VALID_VERIFY_STATUS = [ |
| 53 |
'verify-no-match', |
| 54 |
'verify-match', |
| 55 |
'verify-retry-scan', |
| 56 |
'verify-too-fast', |
| 57 |
'verify-swipe-too-short', |
| 58 |
'verify-finger-not-centered', |
| 59 |
'verify-remove-and-retry', |
| 60 |
'verify-disconnected', |
| 61 |
'verify-unknown-error' |
| 62 |
] |
| 63 |
|
| 64 |
VALID_ENROLL_STATUS = [ |
| 65 |
'enroll-completed', |
| 66 |
'enroll-failed', |
| 67 |
'enroll-stage-passed', |
| 68 |
'enroll-retry-scan', |
| 69 |
'enroll-too-fast', |
| 70 |
'enroll-swipe-too-short', |
| 71 |
'enroll-finger-not-centered', |
| 72 |
'enroll-remove-and-retry', |
| 73 |
'enroll-data-full', |
| 74 |
'enroll-disconnected', |
| 75 |
'enroll-unknown-error' |
| 76 |
] |
| 77 |
|
| 78 |
def load(mock, parameters): |
| 79 |
fprintd = mockobject.objects[MAIN_OBJ] |
| 80 |
mock.last_device_id = 0 |
| 81 |
fprintd.fingers = {} |
| 82 |
mock.loop = asyncio.new_event_loop() |
| 83 |
|
| 84 |
@dbus.service.method(MAIN_IFACE, |
| 85 |
in_signature='', out_signature='ao') |
| 86 |
def GetDevices(self): |
| 87 |
return [(k) for k in mockobject.objects.keys() if "/Device/" in k] |
| 88 |
|
| 89 |
@dbus.service.method(MAIN_IFACE, |
| 90 |
in_signature='', out_signature='o') |
| 91 |
def GetDefaultDevice(self): |
| 92 |
devices = self.GetDevices() |
| 93 |
if len(devices) < 1: |
| 94 |
raise dbus.exceptions.DBusException( |
| 95 |
'No devices available', |
| 96 |
name='net.reactivated.Fprint.Error.NoSuchDevice') |
| 97 |
return devices[0] |
| 98 |
|
| 99 |
@dbus.service.method(MANAGER_MOCK_IFACE, |
| 100 |
in_signature='sisb', out_signature='s') |
| 101 |
def AddDevice(self, device_name, num_enroll_stages, scan_type, |
| 102 |
has_identification=False): |
| 103 |
'''Convenience method to add a fingerprint reader device |
| 104 |
|
| 105 |
You have to specify a device name, the number of enrollment |
| 106 |
stages it would use (> 0) and the scan type, as a string |
| 107 |
(either 'press' or 'swipe') |
| 108 |
''' |
| 109 |
|
| 110 |
if scan_type not in ['swipe', 'press']: |
| 111 |
raise dbus.exceptions.DBusException( |
| 112 |
'Invalid scan_type \'%s\'.' % scan_type, |
| 113 |
name='org.freedesktop.DBus.Error.InvalidArgs') |
| 114 |
|
| 115 |
if num_enroll_stages <= 0: |
| 116 |
raise dbus.exceptions.DBusException( |
| 117 |
'Invalid num_enroll_stages \'%s\'.' % num_enroll_stages, |
| 118 |
name='org.freedesktop.DBus.Error.InvalidArgs') |
| 119 |
|
| 120 |
self.last_device_id += 1 |
| 121 |
path = '/net/reactivated/Fprint/Device/%d' % self.last_device_id |
| 122 |
device_properties = { |
| 123 |
'name': dbus.String(device_name, variant_level=1), |
| 124 |
'num-enroll-stages': dbus.Int32(num_enroll_stages, variant_level=1), |
| 125 |
'scan-type': scan_type |
| 126 |
} |
| 127 |
|
| 128 |
self.AddObject(path, |
| 129 |
DEVICE_IFACE, |
| 130 |
# Properties |
| 131 |
device_properties, |
| 132 |
[]) |
| 133 |
|
| 134 |
device = mockobject.objects[path] |
| 135 |
device.fingers = {} |
| 136 |
device.has_identification = has_identification |
| 137 |
device.claimed_user = None |
| 138 |
device.action = None |
| 139 |
device.selected_finger = None |
| 140 |
device.verify_script = [] |
| 141 |
|
| 142 |
return path |
| 143 |
|
| 144 |
@dbus.service.method(MANAGER_MOCK_IFACE, |
| 145 |
in_signature='o') |
| 146 |
def RemoveDevice(self, path): |
| 147 |
# This isn't compatible with hotplugging devices, which fprintd doesn't |
| 148 |
# support yet, but it's meant to remove devices added to the mock for |
| 149 |
# testing purposes. |
| 150 |
if not path: |
| 151 |
raise dbus.exceptions.DBusException( |
| 152 |
'Invalid empty path.', |
| 153 |
name='org.freedesktop.DBus.Error.InvalidArgs') |
| 154 |
|
| 155 |
self.RemoveObject(path) |
| 156 |
|
| 157 |
@dbus.service.method(DEVICE_IFACE, |
| 158 |
in_signature='s', out_signature='as') |
| 159 |
def ListEnrolledFingers(device, user): |
| 160 |
if user in device.fingers: |
| 161 |
return device.fingers[user] |
| 162 |
raise dbus.exceptions.DBusException( |
| 163 |
'No enrolled prints in device %s for user %s' % (device.path, user), |
| 164 |
name='net.reactivated.Fprint.Error.NoEnrolledPrints') |
| 165 |
|
| 166 |
@dbus.service.method(DEVICE_IFACE, |
| 167 |
in_signature='s', out_signature='') |
| 168 |
def DeleteEnrolledFingers(device, user): |
| 169 |
device.fingers[user] = [] |
| 170 |
|
| 171 |
@dbus.service.method(DEVICE_IFACE, |
| 172 |
in_signature='', out_signature='') |
| 173 |
def DeleteEnrolledFingers2(device): |
| 174 |
if not device.claimed_user: |
| 175 |
raise dbus.exceptions.DBusException( |
| 176 |
'Device was not claimed before use', |
| 177 |
name='net.reactivated.Fprint.Error.ClaimDevice') |
| 178 |
if not device.fingers[device.claimed_user]: |
| 179 |
raise dbus.exceptions.DBusException( |
| 180 |
'No enrolled prints in device {} for user {}'.format(device.path, |
| 181 |
device.claimed_user), |
| 182 |
name='net.reactivated.Fprint.Error.NoEnrolledPrints') |
| 183 |
device.fingers[device.claimed_user] = [] |
| 184 |
|
| 185 |
@dbus.service.method(DEVICE_IFACE, |
| 186 |
in_signature='s', out_signature='') |
| 187 |
def Claim(device, user): |
| 188 |
if device.claimed_user: |
| 189 |
raise dbus.exceptions.DBusException( |
| 190 |
'Device already in use by %s' % device.claimed_user, |
| 191 |
name='net.reactivated.Fprint.Error.AlreadyInUse') |
| 192 |
|
| 193 |
device.claimed_user = user |
| 194 |
|
| 195 |
@dbus.service.method(DEVICE_IFACE, |
| 196 |
in_signature='', out_signature='') |
| 197 |
def Release(device): |
| 198 |
if not device.claimed_user: |
| 199 |
raise dbus.exceptions.DBusException( |
| 200 |
'Device was not claimed before use', |
| 201 |
name='net.reactivated.Fprint.Error.ClaimDevice') |
| 202 |
device.claimed_user = None |
| 203 |
device.action = None |
| 204 |
device.selected_finger = None |
| 205 |
|
| 206 |
def can_verify_finger(device, finger_name): |
| 207 |
# We should already have checked that there are enrolled fingers |
| 208 |
if finger_name == 'any': |
| 209 |
return True |
| 210 |
if finger_name in device.fingers[device.claimed_user]: |
| 211 |
return True |
| 212 |
return False |
| 213 |
|
| 214 |
def glib_sleep(timeout): |
| 215 |
waiting = True |
| 216 |
|
| 217 |
def done_waiting(): |
| 218 |
nonlocal waiting |
| 219 |
waiting = False |
| 220 |
|
| 221 |
GLib.timeout_add(timeout, done_waiting) |
| 222 |
while (waiting): |
| 223 |
GLib.main_context_default().iteration(True) |
| 224 |
|
| 225 |
def device_run_script(device, result, done): |
| 226 |
if result == 'MOCK: quit': |
| 227 |
sys.exit(0) |
| 228 |
|
| 229 |
# Emit signal |
| 230 |
device.EmitSignal(DEVICE_IFACE, 'VerifyStatus', 'sb', [ |
| 231 |
result, |
| 232 |
done |
| 233 |
]) |
| 234 |
|
| 235 |
@dbus.service.method(DEVICE_IFACE, |
| 236 |
in_signature='s', out_signature='') |
| 237 |
def VerifyStart(device, finger_name): |
| 238 |
if not device.claimed_user: |
| 239 |
raise dbus.exceptions.DBusException( |
| 240 |
'Device was not claimed before use', |
| 241 |
name='net.reactivated.Fprint.Error.ClaimDevice') |
| 242 |
if device.claimed_user not in device.fingers: |
| 243 |
raise dbus.exceptions.DBusException( |
| 244 |
'No enrolled prints for user \'%s\'' % device.claimed_user, |
| 245 |
name='net.reactivated.Fprint.Error.NoEnrolledPrints') |
| 246 |
if not finger_name: |
| 247 |
raise dbus.exceptions.DBusException( |
| 248 |
'Invalid empty finger_name.', |
| 249 |
name='org.freedesktop.DBus.Error.InvalidArgs') |
| 250 |
if not can_verify_finger(device, finger_name): |
| 251 |
raise dbus.exceptions.DBusException( |
| 252 |
'Finger \'%s\' not enrolled.' % finger_name, |
| 253 |
name='org.freedesktop.DBus.Error.Internal') |
| 254 |
if device.action: |
| 255 |
raise dbus.exceptions.DBusException( |
| 256 |
'Action \'%s\' already in progress' % device.action, |
| 257 |
name='net.reactivated.Fprint.Error.AlreadyInUse') |
| 258 |
device.action = 'verify' |
| 259 |
|
| 260 |
if finger_name == 'any' and not device.has_identification: |
| 261 |
finger_name = device.fingers[device.claimed_user][0] |
| 262 |
device.selected_finger = finger_name |
| 263 |
# Needs to happen after method return |
| 264 |
GLib.idle_add(device.EmitSignal, |
| 265 |
DEVICE_IFACE, 'VerifyFingerSelected', 's', [ |
| 266 |
device.selected_finger |
| 267 |
]) |
| 268 |
|
| 269 |
error = None |
| 270 |
base_delay = 0 |
| 271 |
while device.verify_script is not None and len(device.verify_script) > 0: |
| 272 |
result, done, timeout = device.verify_script.pop(0) |
| 273 |
|
| 274 |
# We stop when "timeout >= 0 and done" |
| 275 |
if result == 'MOCK: no-prints': |
| 276 |
# Special case to change return value of DBus call, ignores timeout |
| 277 |
error = dbus.exceptions.DBusException( |
| 278 |
'No enrolled prints for user \'%s\'' % device.claimed_user, |
| 279 |
name='net.reactivated.Fprint.Error.NoEnrolledPrints') |
| 280 |
|
| 281 |
elif timeout < 0: |
| 282 |
# Negative timeouts mean emitting before the DBus call returns |
| 283 |
device_run_script(device, result, done) |
| 284 |
glib_sleep(-timeout) |
| 285 |
|
| 286 |
else: |
| 287 |
# Positive or zero means emitting afterwards the given timeout |
| 288 |
base_delay += timeout |
| 289 |
GLib.timeout_add(base_delay, |
| 290 |
device_run_script, |
| 291 |
device, |
| 292 |
result, |
| 293 |
done) |
| 294 |
|
| 295 |
# Stop processing commands when the done flag is set |
| 296 |
if done: |
| 297 |
break |
| 298 |
|
| 299 |
if error: |
| 300 |
raise error |
| 301 |
|
| 302 |
@dbus.service.method(DEVICE_MOCK_IFACE, |
| 303 |
in_signature='sb', out_signature='') |
| 304 |
def EmitVerifyStatus(device, result, done): |
| 305 |
if (not device.action) or (device.action != 'verify'): |
| 306 |
raise dbus.exceptions.DBusException( |
| 307 |
'Cannot send verify statuses when not verifying', |
| 308 |
name='org.freedesktop.DBus.Error.InvalidArgs') |
| 309 |
if result not in VALID_VERIFY_STATUS: |
| 310 |
raise dbus.exceptions.DBusException( |
| 311 |
'Unknown verify status \'%s\'' % result, |
| 312 |
name='org.freedesktop.DBus.Error.InvalidArgs') |
| 313 |
device.EmitSignal(DEVICE_IFACE, 'VerifyStatus', 'sb', [ |
| 314 |
result, |
| 315 |
done |
| 316 |
]) |
| 317 |
|
| 318 |
@dbus.service.method(DEVICE_IFACE, |
| 319 |
in_signature='', out_signature='') |
| 320 |
def VerifyStop(device): |
| 321 |
if device.action != 'verify': |
| 322 |
raise dbus.exceptions.DBusException( |
| 323 |
'No verification to stop', |
| 324 |
name='net.reactivated.Fprint.Error.NoActionInProgress') |
| 325 |
device.action = None |
| 326 |
device.selected_finger = None |
| 327 |
|
| 328 |
@dbus.service.method(DEVICE_IFACE, |
| 329 |
in_signature='s', out_signature='') |
| 330 |
def EnrollStart(device, finger_name): |
| 331 |
if finger_name not in VALID_FINGER_NAMES: |
| 332 |
raise dbus.exceptions.DBusException( |
| 333 |
'Invalid finger name \'%s\'' % finger_name, |
| 334 |
name='net.reactivated.Fprint.Error.InvalidFingername') |
| 335 |
if not device.claimed_user: |
| 336 |
raise dbus.exceptions.DBusException( |
| 337 |
'Device was not claimed before use', |
| 338 |
name='net.reactivated.Fprint.Error.ClaimDevice') |
| 339 |
if device.action: |
| 340 |
raise dbus.exceptions.DBusException( |
| 341 |
'Action \'%s\' already in progress' % device.action, |
| 342 |
name='net.reactivated.Fprint.Error.AlreadyInUse') |
| 343 |
device.action = 'enroll' |
| 344 |
|
| 345 |
@dbus.service.method(DEVICE_MOCK_IFACE, |
| 346 |
in_signature='sb', out_signature='') |
| 347 |
def EmitEnrollStatus(device, result, done): |
| 348 |
if (not device.action) or (device.action != 'enroll'): |
| 349 |
raise dbus.exceptions.DBusException( |
| 350 |
'Cannot send enroll statuses when not enrolling', |
| 351 |
name='org.freedesktop.DBus.Error.InvalidArgs') |
| 352 |
if result not in VALID_ENROLL_STATUS: |
| 353 |
raise dbus.exceptions.DBusException( |
| 354 |
'Unknown enroll status \'%s\'' % result, |
| 355 |
name='org.freedesktop.DBus.Error.InvalidArgs') |
| 356 |
device.EmitSignal(DEVICE_IFACE, 'EnrollStatus', 'sb', [ |
| 357 |
result, |
| 358 |
done |
| 359 |
]) |
| 360 |
# FIXME save enrolled finger? |
| 361 |
|
| 362 |
@dbus.service.method(DEVICE_IFACE, |
| 363 |
in_signature='', out_signature='') |
| 364 |
def EnrollStop(device): |
| 365 |
if device.action != 'enroll': |
| 366 |
raise dbus.exceptions.DBusException( |
| 367 |
'No enrollment to stop', |
| 368 |
name='net.reactivated.Fprint.Error.NoActionInProgress') |
| 369 |
device.action = None |
| 370 |
|
| 371 |
@dbus.service.method(DEVICE_MOCK_IFACE, |
| 372 |
in_signature='sas', out_signature='') |
| 373 |
def SetEnrolledFingers(device, user, fingers): |
| 374 |
'''Convenience method to set the list of enrolled fingers. |
| 375 |
|
| 376 |
The device_path is the return value from AddDevice(), and the |
| 377 |
array of fingers must only contain valid finger names. |
| 378 |
|
| 379 |
Returns nothing. |
| 380 |
''' |
| 381 |
|
| 382 |
for k in fingers: |
| 383 |
if k not in VALID_FINGER_NAMES: |
| 384 |
raise dbus.exceptions.DBusException( |
| 385 |
'Invalid finger name \'%s\'' % k, |
| 386 |
name='org.freedesktop.DBus.Error.InvalidArgs') |
| 387 |
|
| 388 |
device.fingers[user] = fingers |
| 389 |
|
| 390 |
@dbus.service.method(DEVICE_MOCK_IFACE, |
| 391 |
in_signature='', out_signature='s') |
| 392 |
def GetSelectedFinger(device): |
| 393 |
'''Convenience method to get the finger under verification |
| 394 |
|
| 395 |
Returns the finger name that the user has selected for verifying |
| 396 |
''' |
| 397 |
if not device.selected_finger: |
| 398 |
raise dbus.exceptions.DBusException( |
| 399 |
'Device is not verifying', |
| 400 |
name='net.reactivated.Fprint.Error.NoActionInProgress') |
| 401 |
|
| 402 |
return device.selected_finger |
| 403 |
|
| 404 |
@dbus.service.method(DEVICE_MOCK_IFACE, |
| 405 |
in_signature='', out_signature='b') |
| 406 |
def HasIdentification(device): |
| 407 |
'''Convenience method to get if a device supports identification |
| 408 |
|
| 409 |
Returns whether identification is supported. |
| 410 |
''' |
| 411 |
|
| 412 |
return device.has_identification |
| 413 |
|
| 414 |
@dbus.service.method(DEVICE_MOCK_IFACE, |
| 415 |
in_signature='a(sbi)', out_signature='') |
| 416 |
def SetVerifyScript(device, script): |
| 417 |
'''Convenience method to set the verification script. |
| 418 |
|
| 419 |
After VerifyStart is called, signal results will be sent in order after |
| 420 |
a certain timeout declared in seconds. The array contains each |
| 421 |
'result' followed by the 'done' argument for VerifyStatus, and the |
| 422 |
amount of time to wait before each signal is sent. |
| 423 |
|
| 424 |
Returns nothing. |
| 425 |
''' |
| 426 |
|
| 427 |
device.verify_script = script |
| 428 |
|
| 429 |
@dbus.service.method(DEVICE_MOCK_IFACE, |
| 430 |
in_signature='s', out_signature='') |
| 431 |
def SetClaimed(device, user): |
| 432 |
if user == '': |
| 433 |
device.claimed_user = None |
| 434 |
else: |
| 435 |
device.claimed_user = user |