test_fprintd_utils.py 6.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/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)
34
        klass.sleep_time = 0.5
35
36
37
38
39
40
41
42
43
44
45
46
47
48

        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')

49
50
51
52
53
54
55
56
57
58
59
        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)]

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    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'])

77
78
79
    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,
80
                                   stdout=subprocess.PIPE,
81
82
                                   stderr=subprocess.STDOUT,
                                   universal_newlines=True)
83
84
85
86
        flags = fcntl.fcntl(process.stdout, fcntl.F_GETFL)
        fcntl.fcntl(process.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)

        self.addCleanup(lambda: print(process.stdout.read()))
87
88
        self.addCleanup(process.terminate)
        self.addCleanup(process.wait)
89

90
91
92
        if sleep:
            time.sleep(self.sleep_time)

93
94
95
96
97
98
        return process

    def get_process_output(self, process):
        out = process.stdout.read()
        self.addCleanup(print, out)
        return out
99
100

    def run_utility_process(self, utility_name, args=[], sleep=True, timeout=None):
101
        proc = self.start_utility_process(utility_name, args=args, sleep=sleep)
102
103
104
        ret = proc.wait(timeout=timeout if timeout is not None else self.sleep_time * 4)
        self.assertLessEqual(ret, 128)

105
        return self.get_process_output(proc)
106
107
108
109

    def test_fprintd_enroll(self):
        self.setup_device()

110
        process = self.start_utility_process('enroll', ['-f', 'right-index-finger', 'toto'])
111

112
113
        out = self.get_process_output(process)
        self.assertRegex(out, r'right-index-finger')
114
115

        self.device_mock.EmitEnrollStatus('enroll-completed', True)
116
        time.sleep(self.sleep_time)
117

118
119
        out = self.get_process_output(process)
        self.assertRegex(out, 'Enroll result: enroll-completed')
120
121
122
123

    def test_fprintd_verify(self):
        self.setup_device()

124
        process = self.start_utility_process('verify', ['toto'])
125

126
127
128
        out = self.get_process_output(process)
        self.assertRegex(out, r'left-little-finger')
        self.assertNotRegex(out, 'Verify result: verify-match \(done\)')
129
130

        self.device_mock.EmitVerifyStatus('verify-match', True)
131
        time.sleep(self.sleep_time)
132

133
134
        out = self.get_process_output(process)
        self.assertRegex(out, 'Verify result: verify-match \(done\)')
135

136
137
138
139
140
141
142
    def test_fprintd_verify_script(self):
        self.setup_device()
        script = [
            ( 'verify-match', True, 2 )
        ]
        self.device_mock.SetVerifyScript(script)

143
        process = self.start_utility_process('verify', ['toto'])
144

145
146
147
        out = self.get_process_output(process)
        self.assertRegex(out, r'left-little-finger')
        self.assertNotRegex(out, 'Verify result: verify-match \(done\)')
148
149
150

        time.sleep(2)

151
152
        out = self.get_process_output(process)
        self.assertRegex(out, 'Verify result: verify-match \(done\)')
153

154
155
156
157
    def test_fprintd_list(self):
        self.setup_device()

        # Rick has no fingerprints enrolled
158
        out = self.run_utility_process('list', ['rick'])
159
160
161
        self.assertRegex(out, r'has no fingers enrolled for')

        # Toto does
162
        out = self.run_utility_process('list', ['toto'])
163
164
165
166
167
168
        self.assertRegex(out, r'right-little-finger')

    def test_fprintd_delete(self):
        self.setup_device()

        # Has fingerprints enrolled
169
        out = self.run_utility_process('list', ['toto'])
170
171
172
173
        self.assertRegex(out, r'left-little-finger')
        self.assertRegex(out, r'right-little-finger')

        # Delete fingerprints
174
        out = self.run_utility_process('delete', ['toto'])
175
176
177
        self.assertRegex(out, r'Fingerprints deleted')

        # Doesn't have fingerprints
178
        out = self.run_utility_process('list', ['toto'])
179
180
181
182
183
        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))