2020-01-17 14:47:31 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify it under
|
|
|
|
|
# the terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
|
# Software Foundation; either version 3 of the License, or (at your option) any
|
|
|
|
|
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
|
|
|
|
|
# of the license.
|
|
|
|
|
|
|
|
|
|
__author__ = 'Bastien Nocera'
|
|
|
|
|
__email__ = 'hadess@hadess.net'
|
|
|
|
|
__copyright__ = '(c) 2020 Red Hat Inc.'
|
|
|
|
|
__license__ = 'LGPL 3+'
|
|
|
|
|
|
|
|
|
|
import tempfile
|
|
|
|
|
import unittest
|
|
|
|
|
import sys
|
|
|
|
|
import subprocess
|
|
|
|
|
import dbus
|
|
|
|
|
import dbus.mainloop.glib
|
|
|
|
|
import dbusmock
|
|
|
|
|
import fcntl
|
|
|
|
|
import os
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
|
|
|
|
|
2020-04-02 20:09:36 +02:00
|
|
|
class TestFprintdUtilsBase(dbusmock.DBusTestCase):
|
2020-01-17 14:47:31 +01:00
|
|
|
'''Test fprintd utilities'''
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(klass):
|
|
|
|
|
klass.start_system_bus()
|
|
|
|
|
klass.dbus_con = klass.get_dbus(True)
|
2020-01-31 12:13:17 +01:00
|
|
|
klass.sleep_time = 0.5
|
2020-01-17 14:47:31 +01:00
|
|
|
|
|
|
|
|
template_path = './'
|
|
|
|
|
if 'TOPSRCDIR' in os.environ:
|
|
|
|
|
template_path = os.environ['TOPSRCDIR'] + '/tests/'
|
|
|
|
|
klass.template_name = template_path + 'dbusmock/fprintd.py'
|
|
|
|
|
print ('Using template from %s' % klass.template_name)
|
|
|
|
|
|
|
|
|
|
klass.tools_prefix = ''
|
|
|
|
|
if 'FPRINT_BUILD_DIR' in os.environ:
|
|
|
|
|
klass.tools_prefix = os.environ['FPRINT_BUILD_DIR'] + '/../utils/'
|
|
|
|
|
print ('Using tools from %s' % klass.tools_prefix)
|
|
|
|
|
else:
|
|
|
|
|
print ('Using tools from $PATH')
|
|
|
|
|
|
2020-01-31 12:13:17 +01:00
|
|
|
klass.wrapper_args = []
|
|
|
|
|
klass.valgrind = False
|
|
|
|
|
if 'VALGRIND' in os.environ:
|
|
|
|
|
valgrind = os.environ['VALGRIND']
|
|
|
|
|
if valgrind is not None:
|
|
|
|
|
klass.valgrind = True
|
|
|
|
|
klass.sleep_time *= 4
|
|
|
|
|
klass.wrapper_args = ['valgrind', '--leak-check=full']
|
|
|
|
|
if os.path.exists(valgrind):
|
|
|
|
|
klass.wrapper_args += ['--suppressions={}'.format(valgrind)]
|
|
|
|
|
|
2020-02-14 18:31:40 +01:00
|
|
|
if 'ADDRESS_SANITIZER' in os.environ:
|
|
|
|
|
klass.sleep_time *= 2
|
|
|
|
|
|
2020-01-17 14:47:31 +01:00
|
|
|
def setUp(self):
|
2020-04-02 20:08:25 +02:00
|
|
|
super().setUp()
|
2020-01-17 14:47:31 +01:00
|
|
|
(self.p_mock, self.obj_fprintd_manager) = self.spawn_server_template(
|
|
|
|
|
self.template_name, {}, stdout=subprocess.PIPE)
|
|
|
|
|
# set log to nonblocking
|
|
|
|
|
flags = fcntl.fcntl(self.p_mock.stdout, fcntl.F_GETFL)
|
|
|
|
|
fcntl.fcntl(self.p_mock.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
|
|
|
|
|
self.obj_fprintd_mock = dbus.Interface(self.obj_fprintd_manager, 'net.reactivated.Fprint.Manager.Mock')
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
|
self.p_mock.terminate()
|
|
|
|
|
self.p_mock.wait()
|
2020-04-02 20:08:25 +02:00
|
|
|
super().tearDown()
|
2020-01-17 14:47:31 +01:00
|
|
|
|
|
|
|
|
def setup_device(self):
|
|
|
|
|
device_path = self.obj_fprintd_mock.AddDevice('FDO Trigger Finger Laser Reader', 3, 'swipe')
|
|
|
|
|
self.device_mock = self.dbus_con.get_object('net.reactivated.Fprint', device_path)
|
2020-04-02 20:38:28 +02:00
|
|
|
self.set_enrolled_fingers(['left-little-finger', 'right-little-finger'])
|
|
|
|
|
|
|
|
|
|
def set_enrolled_fingers(self, fingers, user='toto'):
|
|
|
|
|
self.enrolled_fingers = fingers
|
|
|
|
|
self.device_mock.SetEnrolledFingers('toto', self.enrolled_fingers,
|
|
|
|
|
signature='sas')
|
2020-01-17 14:47:31 +01:00
|
|
|
|
2020-02-14 17:28:19 +01:00
|
|
|
def start_utility_process(self, utility_name, args=[], sleep=True):
|
|
|
|
|
utility = [ os.path.join(self.tools_prefix, 'fprintd-{}'.format(utility_name)) ]
|
|
|
|
|
process = subprocess.Popen(self.wrapper_args + utility + args,
|
2020-02-14 18:16:23 +01:00
|
|
|
stdout=subprocess.PIPE,
|
2020-01-17 14:47:31 +01:00
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
|
universal_newlines=True)
|
2020-02-14 18:16:23 +01:00
|
|
|
flags = fcntl.fcntl(process.stdout, fcntl.F_GETFL)
|
|
|
|
|
fcntl.fcntl(process.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
|
|
|
|
|
|
2020-04-02 21:24:43 +02:00
|
|
|
self.addCleanup(self.try_stop_utility_process, process)
|
2020-01-17 14:47:31 +01:00
|
|
|
|
2020-02-14 17:28:19 +01:00
|
|
|
if sleep:
|
|
|
|
|
time.sleep(self.sleep_time)
|
|
|
|
|
|
2020-02-14 18:16:23 +01:00
|
|
|
return process
|
|
|
|
|
|
2020-04-02 21:24:43 +02:00
|
|
|
def stop_utility_process(self, process):
|
|
|
|
|
print(process.stdout.read())
|
|
|
|
|
process.terminate()
|
|
|
|
|
process.wait()
|
|
|
|
|
|
|
|
|
|
def try_stop_utility_process(self, process):
|
|
|
|
|
try:
|
|
|
|
|
self.stop_utility_process(process)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
2020-02-14 18:16:23 +01:00
|
|
|
def get_process_output(self, process):
|
|
|
|
|
out = process.stdout.read()
|
|
|
|
|
self.addCleanup(print, out)
|
|
|
|
|
return out
|
2020-02-14 17:28:19 +01:00
|
|
|
|
|
|
|
|
def run_utility_process(self, utility_name, args=[], sleep=True, timeout=None):
|
2020-02-14 18:16:23 +01:00
|
|
|
proc = self.start_utility_process(utility_name, args=args, sleep=sleep)
|
2020-02-14 17:28:19 +01:00
|
|
|
ret = proc.wait(timeout=timeout if timeout is not None else self.sleep_time * 4)
|
|
|
|
|
self.assertLessEqual(ret, 128)
|
|
|
|
|
|
2020-02-14 18:16:23 +01:00
|
|
|
return self.get_process_output(proc)
|
2020-02-14 17:28:19 +01:00
|
|
|
|
2020-04-02 20:09:36 +02:00
|
|
|
|
|
|
|
|
class TestFprintdUtils(TestFprintdUtilsBase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
super().setUp()
|
2020-02-14 17:28:19 +01:00
|
|
|
self.setup_device()
|
|
|
|
|
|
2020-04-02 20:09:36 +02:00
|
|
|
def test_fprintd_enroll(self):
|
2020-02-14 18:16:23 +01:00
|
|
|
process = self.start_utility_process('enroll', ['-f', 'right-index-finger', 'toto'])
|
2020-01-17 14:47:31 +01:00
|
|
|
|
2020-02-14 18:16:23 +01:00
|
|
|
out = self.get_process_output(process)
|
|
|
|
|
self.assertRegex(out, r'right-index-finger')
|
2020-01-17 14:47:31 +01:00
|
|
|
|
|
|
|
|
self.device_mock.EmitEnrollStatus('enroll-completed', True)
|
2020-01-31 12:13:17 +01:00
|
|
|
time.sleep(self.sleep_time)
|
2020-01-17 14:47:31 +01:00
|
|
|
|
2020-02-14 18:16:23 +01:00
|
|
|
out = self.get_process_output(process)
|
|
|
|
|
self.assertRegex(out, 'Enroll result: enroll-completed')
|
2020-01-17 14:47:31 +01:00
|
|
|
|
|
|
|
|
def test_fprintd_list(self):
|
|
|
|
|
# Rick has no fingerprints enrolled
|
2020-02-14 17:28:19 +01:00
|
|
|
out = self.run_utility_process('list', ['rick'])
|
2020-01-17 14:47:31 +01:00
|
|
|
self.assertRegex(out, r'has no fingers enrolled for')
|
|
|
|
|
|
|
|
|
|
# Toto does
|
2020-02-14 17:28:19 +01:00
|
|
|
out = self.run_utility_process('list', ['toto'])
|
2020-01-17 14:47:31 +01:00
|
|
|
self.assertRegex(out, r'right-little-finger')
|
|
|
|
|
|
|
|
|
|
def test_fprintd_delete(self):
|
|
|
|
|
# Has fingerprints enrolled
|
2020-02-14 17:28:19 +01:00
|
|
|
out = self.run_utility_process('list', ['toto'])
|
2020-01-17 14:47:31 +01:00
|
|
|
self.assertRegex(out, r'left-little-finger')
|
|
|
|
|
self.assertRegex(out, r'right-little-finger')
|
|
|
|
|
|
|
|
|
|
# Delete fingerprints
|
2020-02-14 17:28:19 +01:00
|
|
|
out = self.run_utility_process('delete', ['toto'])
|
2020-01-17 14:47:31 +01:00
|
|
|
self.assertRegex(out, r'Fingerprints deleted')
|
|
|
|
|
|
|
|
|
|
# Doesn't have fingerprints
|
2020-02-14 17:28:19 +01:00
|
|
|
out = self.run_utility_process('list', ['toto'])
|
2020-01-17 14:47:31 +01:00
|
|
|
self.assertRegex(out, r'has no fingers enrolled for')
|
|
|
|
|
|
2020-04-02 20:38:28 +02:00
|
|
|
|
|
|
|
|
class TestFprintdUtilsVerify(TestFprintdUtilsBase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
super().setUp()
|
|
|
|
|
self.setup_device()
|
|
|
|
|
|
|
|
|
|
def start_verify_process(self, user='toto', finger=None, checkEnrolled=True):
|
|
|
|
|
args = [user]
|
|
|
|
|
if finger:
|
|
|
|
|
args += ['-f', finger]
|
|
|
|
|
|
|
|
|
|
self.process = self.start_utility_process('verify', args)
|
|
|
|
|
out = self.get_process_output(self.process)
|
|
|
|
|
|
2020-04-02 21:25:13 +02:00
|
|
|
self.assertNotRegex(out, r'Device already in use by [A-z]+')
|
2020-04-02 20:38:28 +02:00
|
|
|
self.assertNotIn('Verify result:', out)
|
|
|
|
|
|
|
|
|
|
if checkEnrolled:
|
|
|
|
|
for f in self.enrolled_fingers:
|
|
|
|
|
self.assertIn(f, out)
|
|
|
|
|
|
|
|
|
|
def assertVerifyMatch(self, match):
|
|
|
|
|
self.assertIn('Verify result: {} (done)'.format(
|
|
|
|
|
'verify-match' if match else 'verify-no-match'),
|
|
|
|
|
self.get_process_output(self.process))
|
|
|
|
|
|
|
|
|
|
def test_fprintd_verify(self):
|
|
|
|
|
self.start_verify_process()
|
|
|
|
|
|
|
|
|
|
self.device_mock.EmitVerifyStatus('verify-match', True)
|
|
|
|
|
time.sleep(self.sleep_time)
|
|
|
|
|
self.assertVerifyMatch(True)
|
|
|
|
|
|
2020-04-02 22:23:41 +02:00
|
|
|
def test_fprintd_verify_enrolled_fingers(self):
|
|
|
|
|
for finger in self.enrolled_fingers:
|
|
|
|
|
self.start_verify_process(finger=finger)
|
|
|
|
|
|
|
|
|
|
self.device_mock.EmitVerifyStatus('verify-match', True)
|
|
|
|
|
time.sleep(self.sleep_time)
|
|
|
|
|
self.assertVerifyMatch(True)
|
|
|
|
|
|
2020-04-02 20:38:28 +02:00
|
|
|
def test_fprintd_verify_script(self):
|
|
|
|
|
script = [
|
|
|
|
|
( 'verify-match', True, 2 )
|
|
|
|
|
]
|
|
|
|
|
self.device_mock.SetVerifyScript(script)
|
|
|
|
|
|
|
|
|
|
self.start_verify_process()
|
|
|
|
|
time.sleep(self.sleep_time * 4)
|
|
|
|
|
self.assertVerifyMatch(True)
|
|
|
|
|
|
2020-04-02 21:25:13 +02:00
|
|
|
def test_fprintd_multiple_verify_fails(self):
|
|
|
|
|
self.start_verify_process()
|
|
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(AssertionError, r'Device already in use'):
|
|
|
|
|
self.start_verify_process()
|
|
|
|
|
|
2020-01-17 14:47:31 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
# avoid writing to stderr
|
|
|
|
|
unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2))
|