From 431755becdbadf902f335b06d2976a146b173dea Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Fri, 17 Jan 2020 14:47:31 +0100 Subject: [PATCH] tests: Add test for fprintd utils --- tests/Makefile.am | 2 +- tests/test_fprintd_utils.py | 148 ++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 1 deletion(-) create mode 100755 tests/test_fprintd_utils.py diff --git a/tests/Makefile.am b/tests/Makefile.am index cbeac8d..8280236 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,5 +1,5 @@ TESTS_ENVIRONMENT = export FPRINT_BUILD_DIR=$(abs_top_builddir)/src; export TOPSRCDIR=$(abs_top_srcdir); export PYTHON=@PYTHON@; -TESTS = fprintd.py +TESTS = fprintd.py test_fprintd_utils.py EXTRA_DIST = \ $(TESTS) \ diff --git a/tests/test_fprintd_utils.py b/tests/test_fprintd_utils.py new file mode 100755 index 0000000..7e777a8 --- /dev/null +++ b/tests/test_fprintd_utils.py @@ -0,0 +1,148 @@ +#!/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) + +class TestFprintd(dbusmock.DBusTestCase): + '''Test fprintd utilities''' + + @classmethod + def setUpClass(klass): + klass.start_system_bus() + klass.dbus_con = klass.get_dbus(True) + + 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') + + def setUp(self): + (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() + + 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) + self.device_mock.SetEnrolledFingers('toto', ['left-little-finger', 'right-little-finger']) + + def test_fprintd_enroll(self): + self.setup_device() + + mock_log = tempfile.NamedTemporaryFile() + process = subprocess.Popen([self.tools_prefix + 'fprintd-enroll', '-f', 'right-index-finger', 'toto'], + stdout=mock_log, + stderr=subprocess.STDOUT, + universal_newlines=True) + + time.sleep(0.5) + + with open(mock_log.name) as f: + out = f.read() + self.assertRegex(out, r'right-index-finger') + + self.device_mock.EmitEnrollStatus('enroll-completed', True) + time.sleep(0.5) + + with open(mock_log.name) as f: + out = f.read() + self.assertRegex(out, 'Enroll result: enroll-completed') + + def test_fprintd_verify(self): + self.setup_device() + + mock_log = tempfile.NamedTemporaryFile() + process = subprocess.Popen([self.tools_prefix + 'fprintd-verify', 'toto'], + stdout=mock_log, + stderr=subprocess.STDOUT, + universal_newlines=True) + + time.sleep(0.5) + + with open(mock_log.name) as f: + out = f.read() + self.assertRegex(out, r'left-little-finger') + + self.device_mock.EmitVerifyStatus('verify-match', True) + time.sleep(0.5) + + with open(mock_log.name) as f: + out = f.read() + self.assertRegex(out, 'Verify result: verify-match \(done\)') + + def test_fprintd_list(self): + self.setup_device() + + # Rick has no fingerprints enrolled + out = subprocess.check_output([self.tools_prefix + 'fprintd-list', 'rick'], + stderr=subprocess.STDOUT, + universal_newlines=True) + self.assertRegex(out, r'has no fingers enrolled for') + + # Toto does + out = subprocess.check_output([self.tools_prefix + 'fprintd-list', 'toto'], + universal_newlines=True) + self.assertRegex(out, r'left-little-finger') + self.assertRegex(out, r'right-little-finger') + + def test_fprintd_delete(self): + self.setup_device() + + # Has fingerprints enrolled + out = subprocess.check_output([self.tools_prefix + 'fprintd-list', 'toto'], + stderr=subprocess.STDOUT, + universal_newlines=True) + self.assertRegex(out, r'left-little-finger') + self.assertRegex(out, r'right-little-finger') + + # Delete fingerprints + out = subprocess.check_output([self.tools_prefix + 'fprintd-delete', 'toto'], + stderr=subprocess.STDOUT, + universal_newlines=True) + self.assertRegex(out, r'Fingerprints deleted') + + # Doesn't have fingerprints + out = subprocess.check_output([self.tools_prefix + 'fprintd-list', 'toto'], + stderr=subprocess.STDOUT, + universal_newlines=True) + self.assertRegex(out, r'has no fingers enrolled for') + +if __name__ == '__main__': + # avoid writing to stderr + unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2))