Debian Codegraph @

tests/pam/test_pam_fprintd.py

raw
1 #!/usr/bin/python3
2
3 # This program is free software; you can redistribute it and/or modify it under
4 # the terms of the GNU Lesser General Public License as published by the Free
5 # Software Foundation; either version 3 of the License, or (at your option) any
6 # later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
7 # of the license.
8
9 __author__ = 'Bastien Nocera'
10 __email__ = '[email protected]'
11 __copyright__ = '(c) 2020 Red Hat Inc.'
12 __license__ = 'LGPL 3+'
13
14 import tempfile
15 import unittest
16 import sys
17 import subprocess
18 import dbus
19 import dbusmock
20 import glob
21 import os
22 import shutil
23 import time
24 import pypamtest
25
26 PAM_SUCCESS = 0
27 PAM_AUTH_ERR = 7
28 PAM_AUTHINFO_UNAVAIL = 9
29 PAM_USER_UNKNOWN = 10
30 PAM_MAXTRIES = 11
31
32 class TestPamFprintd(dbusmock.DBusTestCase):
33 '''Test pam_fprintd'''
34
35 @classmethod
36 def start_monitor(klass):
37 '''Start dbus-monitor'''
38
39 workdir = os.environ['TOPBUILDDIR'] + '/tests/pam/'
40 klass.monitor_log = open(os.path.join(workdir, 'dbus-monitor.log'), 'wb', buffering=0)
41 klass.monitor = subprocess.Popen(['dbus-monitor', '--monitor', '--system'],
42 stdout=klass.monitor_log,
43 stderr=subprocess.STDOUT)
44
45 @classmethod
46 def stop_monitor(klass):
47 '''Stop dbus-monitor'''
48
49 assert klass.monitor
50 klass.monitor.terminate()
51 klass.monitor.wait()
52
53 klass.monitor_log.flush()
54 klass.monitor_log.close()
55
56 @classmethod
57 def setUpClass(klass):
58 dbusmock.DBusTestCase.setUpClass()
59
60 klass.start_system_bus()
61 klass.start_monitor()
62 klass.dbus_con = klass.get_dbus(True)
63
64 template_path = './'
65 if 'TOPSRCDIR' in os.environ:
66 template_path = os.environ['TOPSRCDIR'] + '/tests/'
67 klass.template_name = template_path + 'dbusmock/fprintd.py'
68 print ('Using template from %s' % klass.template_name)
69
70 @classmethod
71 def tearDownClass(klass):
72 klass.stop_monitor()
73
74 # Remove pam wrapper files, as they may break other tests
75 [shutil.rmtree(i) for i in glob.glob('/tmp/pam.[0-9A-z]')]
76
77 dbusmock.DBusTestCase.tearDownClass()
78
79 def setUp(self):
80 super().setUp()
81 (self.p_mock, self.obj_fprintd_manager) = self.spawn_server_template(
82 self.template_name, {})
83 self.obj_fprintd_mock = dbus.Interface(self.obj_fprintd_manager, 'net.reactivated.Fprint.Manager.Mock')
84
85 def tearDown(self):
86 self.p_mock.terminate()
87 self.p_mock.wait()
88 super().tearDown()
89
90 def setup_device(self):
91 device_path = self.obj_fprintd_mock.AddDevice('FDO Trigger Finger Laser Reader', 3, 'swipe', False)
92 self.device_mock = self.dbus_con.get_object('net.reactivated.Fprint', device_path)
93 self.device_mock.SetEnrolledFingers('toto', ['left-little-finger', 'right-little-finger'])
94
95 def test_pam_no_device(self):
96 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_AUTHINFO_UNAVAIL)
97 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
98
99 def test_pam_fprintd_identify_error(self):
100 self.setup_device()
101 script = [
102 ( 'verify-unknown-error', True, 2 )
103 ]
104 self.device_mock.SetVerifyScript(script)
105
106 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_AUTHINFO_UNAVAIL)
107 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
108
109 self.assertRegex(res.info[0], r'Swipe your left little finger across the fingerprint reader')
110 self.assertEqual(len(res.errors), 0)
111
112 def test_pam_fprintd_identify_error2(self):
113 self.setup_device()
114 script = [
115 ( 'verify-disconnected', True, 2 )
116 ]
117 self.device_mock.SetVerifyScript(script)
118
119 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_AUTHINFO_UNAVAIL)
120 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
121
122 self.assertRegex(res.info[0], r'Swipe your left little finger across the fingerprint reader')
123 self.assertEqual(len(res.errors), 0)
124
125 def test_pam_fprintd_identify_error3(self):
126 self.setup_device()
127 script = [
128 ( 'verify-INVALID', True, 2 )
129 ]
130 self.device_mock.SetVerifyScript(script)
131
132 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_AUTH_ERR)
133 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
134
135 self.assertRegex(res.info[0], r'Swipe your left little finger across the fingerprint reader')
136 self.assertEqual(len(res.errors), 1)
137 self.assertRegex(res.errors[0], r'An unknown error occurred')
138
139 def test_pam_fprintd_auth(self):
140 self.setup_device()
141 script = [
142 ( 'verify-match', True, 2 )
143 ]
144 self.device_mock.SetVerifyScript(script)
145
146 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_SUCCESS)
147 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
148
149 self.assertRegex(res.info[0], r'Swipe your left little finger across the fingerprint reader')
150 self.assertEqual(len(res.errors), 0)
151
152 # Check that we can stop verification and release the device. i.e.
153 # this has not been done by PAM already (the real fprintd would notice
154 # the disconnect, the mock service does not).
155 self.device_mock.VerifyStop()
156 self.device_mock.Release()
157
158 def test_pam_fprintd_no_fingers(self):
159 self.setup_device()
160 self.device_mock.SetEnrolledFingers('toto', dbus.Array(set([]), signature='s'))
161 script = [
162 ( 'verify-match', True, 1 )
163 ]
164 self.device_mock.SetVerifyScript(script)
165
166 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_AUTHINFO_UNAVAIL)
167 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
168
169 def test_pam_fprintd_retry(self):
170 self.setup_device()
171 script = [
172 ( 'verify-swipe-too-short', False, 1 ),
173 ( 'verify-finger-not-centered', False, 1 ),
174 ( 'verify-match', True, 1 )
175 ]
176 self.device_mock.SetVerifyScript(script)
177
178 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_SUCCESS)
179 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
180 self.assertRegex(res.info[0], r'Swipe your left little finger across the fingerprint reader')
181 self.assertRegex(res.errors[0], r'Swipe was too short, try again')
182 self.assertRegex(res.errors[1], r'Your finger was not centered, try swiping your finger again')
183
184 def test_pam_fprintd_no_fingers_while_verifying(self):
185 self.setup_device()
186 script = [
187 ( 'MOCK: no-prints', True, 1),
188 ( 'verify-match', True, 1 )
189 ]
190 self.device_mock.SetVerifyScript(script)
191
192 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_AUTHINFO_UNAVAIL)
193 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
194
195 def test_pam_fprintd_blocks_unexpected_auth(self):
196 self.setup_device()
197 script = [
198 ( 'verify-match', True, -500 ), # This one is sent before VerifyStart has completed
199 ( 'verify-no-match', True, 1 ),
200 ( 'verify-no-match', True, 1 ),
201 ( 'verify-no-match', True, 1 ),
202 ]
203 self.device_mock.SetVerifyScript(script)
204
205 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_MAXTRIES)
206 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
207
208 self.assertRegex(res.info[0], r'Swipe your left little finger across the fingerprint reader')
209 self.assertEqual(len(res.errors), 3)
210 self.assertRegex(res.errors[0], r'Failed to match fingerprint')
211 self.assertRegex(res.errors[0], r'Failed to match fingerprint')
212 self.assertRegex(res.errors[0], r'Failed to match fingerprint')
213
214 # Check that we can cannot stop verification or release the device. i.e.
215 # PAM should have correctly done this after verification was done.
216 with self.assertRaisesRegex(dbus.exceptions.DBusException, r'net\.reactivated\.Fprint\.Error\.NoActionInProgress'):
217 self.device_mock.VerifyStop()
218 with self.assertRaisesRegex(dbus.exceptions.DBusException, r'net\.reactivated\.Fprint\.Error\.ClaimDevice'):
219 self.device_mock.Release()
220
221 def test_pam_fprintd_blocks_unexpected_auth2(self):
222 self.setup_device()
223 script = [
224 ( 'verify-no-match', True, 1 ),
225 ( 'verify-match', True, -500 ), # This one is sent before VerifyStart has completed
226 ( 'verify-no-match', True, 1 ),
227 ( 'verify-no-match', True, 1 ),
228 ]
229 self.device_mock.SetVerifyScript(script)
230
231 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_MAXTRIES)
232 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
233
234 self.assertRegex(res.info[0], r'Swipe your left little finger across the fingerprint reader')
235 self.assertEqual(len(res.errors), 3)
236 self.assertRegex(res.errors[0], r'Failed to match fingerprint')
237 self.assertRegex(res.errors[0], r'Failed to match fingerprint')
238 self.assertRegex(res.errors[0], r'Failed to match fingerprint')
239
240 def test_pam_fprintd_dual_reader_auth(self):
241 device_path = self.obj_fprintd_mock.AddDevice('FDO Sandpaper Reader', 3, 'press', False)
242 sandpaper_device_mock = self.dbus_con.get_object('net.reactivated.Fprint', device_path)
243 sandpaper_device_mock.SetEnrolledFingers('toto', ['left-middle-finger', 'right-middle-finger'])
244 script = [
245 ( 'verify-match', True, 2 )
246 ]
247 sandpaper_device_mock.SetVerifyScript(script)
248
249 # Add a 2nd device
250 self.setup_device()
251
252 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_SUCCESS)
253 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
254
255 self.assertRegex(res.info[0], r'Place your left middle finger on FDO Sandpaper Reader')
256 self.assertEqual(len(res.errors), 0)
257
258 def test_pam_fprintd_multi_reader_not_all_enrolled(self):
259 # Add a 1st device with actual enrolled prints
260 device_path = self.obj_fprintd_mock.AddDevice('FDO Empty reader', 3, 'press', False)
261 empty_reader = self.dbus_con.get_object('net.reactivated.Fprint', device_path)
262 empty_reader.SetEnrolledFingers('toto', dbus.Array(set([]), signature='s'))
263
264 # Add a 2nd device with actual enrolled prints
265 device_path = self.obj_fprintd_mock.AddDevice('FDO Most Used Reader', 3, 'press', False)
266 sandpaper_device_mock = self.dbus_con.get_object('net.reactivated.Fprint', device_path)
267 sandpaper_device_mock.SetEnrolledFingers('toto', ['left-middle-finger', 'right-middle-finger'])
268 script = [
269 ( 'verify-match', True, 2 )
270 ]
271 sandpaper_device_mock.SetVerifyScript(script)
272
273 # Add a 3rd device, with only one enrolled finger
274 self.setup_device()
275 self.device_mock.SetEnrolledFingers('toto', ['left-middle-finger'])
276
277 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_SUCCESS)
278 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
279
280 self.assertRegex(res.info[0], r'Place your left middle finger on FDO Most Used Reader')
281 self.assertEqual(len(res.errors), 0)
282
283 def test_pam_fprintd_last_try_auth(self):
284 self.setup_device()
285 script = [
286 ( 'verify-no-match', True, 1 ),
287 ( 'verify-no-match', True, 1 ),
288 ( 'verify-match', True, 1 ),
289 ]
290 self.device_mock.SetVerifyScript(script)
291
292 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_SUCCESS)
293 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
294
295 self.assertRegex(res.info[0], r'Swipe your left little finger across the fingerprint reader')
296 self.assertEqual(len(res.errors), 2)
297 self.assertRegex(res.errors[0], r'Failed to match fingerprint')
298 self.assertRegex(res.errors[1], r'Failed to match fingerprint')
299
300 def test_pam_fprintd_failed_auth(self):
301 self.setup_device()
302 script = [
303 ( 'verify-no-match', True, 1 ),
304 ( 'verify-no-match', True, 1 ),
305 ( 'verify-no-match', True, 1 ),
306 ]
307 self.device_mock.SetVerifyScript(script)
308
309 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_MAXTRIES)
310 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
311
312 self.assertRegex(res.info[0], r'Swipe your left little finger across the fingerprint reader')
313 self.assertEqual(len(res.errors), 3)
314 self.assertRegex(res.errors[0], r'Failed to match fingerprint')
315 self.assertRegex(res.errors[1], r'Failed to match fingerprint')
316 self.assertRegex(res.errors[2], r'Failed to match fingerprint')
317
318 def test_pam_already_claimed(self):
319 self.setup_device()
320 script = [
321 ( 'verify-match', True, 2 )
322 ]
323 self.device_mock.SetVerifyScript(script)
324 self.device_mock.SetClaimed('toto')
325
326 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_AUTHINFO_UNAVAIL)
327 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
328
329 self.assertEqual(len(res.info), 0)
330 self.assertEqual(len(res.errors), 0)
331
332 def test_pam_timeout(self):
333 self.setup_device()
334
335 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_AUTHINFO_UNAVAIL)
336 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
337 self.assertRegex(res.info[1], r'Verification timed out')
338
339 def test_pam_notices_fprintd_disappearing(self):
340 self.setup_device()
341
342 script = [
343 ( 'MOCK: quit', True, 0 ),
344 ]
345 self.device_mock.SetVerifyScript(script)
346
347 tc = pypamtest.TestCase(pypamtest.PAMTEST_AUTHENTICATE, expected_rv=PAM_AUTHINFO_UNAVAIL)
348 res = pypamtest.run_pamtest("toto", "fprintd-pam-test", [tc], [ 'unused' ])
349 self.assertEqual(len(res.errors), 0)
350 self.assertEqual(len(res.info), 0)
351
352 if __name__ == '__main__':
353 if 'PAM_WRAPPER_SERVICE_DIR' not in os.environ:
354 print('Cannot run test without environment set correctly, run "meson test" instead')
355 sys.exit(1)
356 # set stream to sys.stderr to get debug output
357 unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2))