#!/usr/bin/env python3 import sys try: import gi import re import os from gi.repository import GLib, Gio import unittest import socket import struct import subprocess import shutil import glob import tempfile except Exception as e: print("Missing dependencies: %s" % str(e)) sys.exit(77) FPrint = None # Re-run the test with the passed wrapper if set wrapper = os.getenv('LIBFPRINT_TEST_WRAPPER') if wrapper: wrap_cmd = wrapper.split(' ') + [sys.executable, os.path.abspath(__file__)] + \ sys.argv[1:] os.unsetenv('LIBFPRINT_TEST_WRAPPER') sys.exit(subprocess.check_call(wrap_cmd)) ctx = GLib.main_context_default() class Connection: def __init__(self, addr): self.addr = addr def __enter__(self): self.con = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.con.connect(self.addr) return self.con def __exit__(self, exc_type, exc_val, exc_tb): self.con.close() del self.con class VirtualDevice(unittest.TestCase): @classmethod def setUpClass(cls): unittest.TestCase.setUpClass() cls.tmpdir = tempfile.mkdtemp(prefix='libfprint-') driver_name = cls.driver_name if hasattr(cls, 'driver_name') else None if not driver_name: driver_name = re.compile(r'(? 0*') notified_spec = None self.send_command('SET_ENROLL_STAGES', 0) self.assertEqual(self.dev.get_nr_enroll_stages(), 1) self.assertIsNone(notified_spec) GLib.test_assert_expected_messages_internal('libfprint-device', __file__, 0, 'test_change_enroll_stages') def test_quick_enroll(self): self.send_command('SET_ENROLL_STAGES', 1) self.assertEqual(self.dev.get_nr_enroll_stages(), 1) matching = self.enroll_print('testprint', FPrint.Finger.LEFT_LITTLE) self.assertEqual(matching.get_username(), 'testuser') self.assertEqual(matching.get_finger(), FPrint.Finger.LEFT_LITTLE) class VirtualDeviceStorage(VirtualDevice): def cleanup_device_storage(self): for print in self.dev.list_prints_sync(): self.assertTrue(self.dev.delete_print_sync(print, None)) def test_device_properties(self): self.assertEqual(self.dev.get_driver(), 'virtual_device_storage') self.assertEqual(self.dev.get_device_id(), '0') self.assertEqual(self.dev.get_name(), 'Virtual device with storage and identification for debugging') self.assertTrue(self.dev.is_open()) self.assertEqual(self.dev.get_scan_type(), FPrint.ScanType.SWIPE) self.assertEqual(self.dev.get_nr_enroll_stages(), 5) self.assertTrue(self.dev.supports_identify()) self.assertFalse(self.dev.supports_capture()) self.assertTrue(self.dev.has_storage()) def test_list_empty(self): self.cleanup_device_storage() self.assertFalse(self.dev.list_prints_sync()) def test_list_populated(self): self.cleanup_device_storage() self.send_command('INSERT', 'p1') print2 = self.enroll_print('p2', FPrint.Finger.LEFT_LITTLE) self.assertEqual({'p1', 'p2'}, {p.props.fpi_data.get_string() for p in self.dev.list_prints_sync()}) def test_list_delete(self): self.cleanup_device_storage() p = self.enroll_print('testprint', FPrint.Finger.RIGHT_THUMB) l = self.dev.list_prints_sync() print(l[0]) self.assertEqual(len(l), 1) print('blub', p.props.fpi_data, type(l[0].props.fpi_data)) assert p.equal(l[0]) self.dev.delete_print_sync(p) self.assertFalse(self.dev.list_prints_sync()) def test_list_delete_missing(self): self.cleanup_device_storage() p = self.enroll_print('testprint', FPrint.Finger.RIGHT_THUMB) self.send_command('REMOVE', 'testprint') with self.assertRaisesRegex(GLib.GError, 'Print was not found'): self.dev.delete_print_sync(p) if __name__ == '__main__': try: gi.require_version('FPrint', '2.0') from gi.repository import FPrint except Exception as e: print("Missing dependencies: %s" % str(e)) sys.exit(77) # avoid writing to stderr unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2))