From 31e34bd4bddba011bc0e00ba07544f5ebe3c9da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Wed, 27 Jan 2021 14:49:03 +0100 Subject: [PATCH] virtual-device: Emit data not found during verify/identify If trying to identify a print not in the storage we emit data not found error, this can be helpful to do further fprintd testing too --- libfprint/drivers/virtual-device-storage.c | 49 ++++++++++++++-------- libfprint/drivers/virtual-device.c | 15 +++++-- tests/virtual-device.py | 42 ++++++++++++++++--- 3 files changed, 80 insertions(+), 26 deletions(-) diff --git a/libfprint/drivers/virtual-device-storage.c b/libfprint/drivers/virtual-device-storage.c index dbda805..34e8971 100644 --- a/libfprint/drivers/virtual-device-storage.c +++ b/libfprint/drivers/virtual-device-storage.c @@ -33,22 +33,23 @@ G_DEFINE_TYPE (FpDeviceVirtualDeviceStorage, fpi_device_virtual_device_storage, fpi_device_virtual_device_get_type ()) +static GPtrArray * get_stored_prints (FpDeviceVirtualDevice * self); + static void dev_identify (FpDevice *dev) { g_autoptr(GError) error = NULL; FpDeviceVirtualDevice *self = FP_DEVICE_VIRTUAL_DEVICE (dev); - GPtrArray *prints; g_autofree char *scan_id = NULL; scan_id = start_scan_command (self, &error); if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_PENDING)) return; - fpi_device_get_identify_data (dev, &prints); - if (scan_id) { + g_autoptr(GPtrArray) stored = get_stored_prints (self); + GPtrArray *prints; GVariant *data = NULL; FpPrint *new_scan; FpPrint *match = NULL; @@ -56,17 +57,22 @@ dev_identify (FpDevice *dev) new_scan = fp_print_new (dev); fpi_print_set_type (new_scan, FPI_PRINT_RAW); - if (self->prints_storage) - fpi_print_set_device_stored (new_scan, TRUE); + fpi_print_set_device_stored (new_scan, TRUE); data = g_variant_new_string (scan_id); g_object_set (new_scan, "fpi-data", data, NULL); + fpi_device_get_identify_data (dev, &prints); g_debug ("Trying to identify print '%s' against a gallery of %u prints", scan_id, prints->len); - if (g_ptr_array_find_with_equal_func (prints, - new_scan, - (GEqualFunc) fp_print_equal, - &idx)) + if (!g_ptr_array_find_with_equal_func (stored, + new_scan, + (GEqualFunc) fp_print_equal, + NULL)) + error = fpi_device_error_new (FP_DEVICE_ERROR_DATA_NOT_FOUND); + else if (g_ptr_array_find_with_equal_func (prints, + new_scan, + (GEqualFunc) fp_print_equal, + &idx)) match = g_ptr_array_index (prints, idx); if (!self->match_reported) @@ -118,13 +124,28 @@ dev_list_insert_print (gpointer key, g_ptr_array_add (data->res, print); } +static GPtrArray * +get_stored_prints (FpDeviceVirtualDevice *self) +{ + GPtrArray * prints_list; + struct ListData data; + + prints_list = g_ptr_array_new_full (g_hash_table_size (self->prints_storage), + g_object_unref); + data.dev = FP_DEVICE (self); + data.res = prints_list; + + g_hash_table_foreach (self->prints_storage, dev_list_insert_print, &data); + + return prints_list; +} + static void dev_list (FpDevice *dev) { g_autoptr(GPtrArray) prints_list = NULL; g_autoptr(GError) error = NULL; FpDeviceVirtualDevice *vdev = FP_DEVICE_VIRTUAL_DEVICE (dev); - struct ListData data; process_cmds (vdev, FALSE, &error); if (should_wait_for_command (vdev, error)) @@ -136,13 +157,7 @@ dev_list (FpDevice *dev) return; } - prints_list = g_ptr_array_new_full (g_hash_table_size (vdev->prints_storage), g_object_unref); - data.dev = dev; - data.res = prints_list; - - g_hash_table_foreach (vdev->prints_storage, dev_list_insert_print, &data); - - fpi_device_list_complete (dev, g_steal_pointer (&prints_list), NULL); + fpi_device_list_complete (dev, get_stored_prints (vdev), NULL); } static void diff --git a/libfprint/drivers/virtual-device.c b/libfprint/drivers/virtual-device.c index f7767a6..ca658dd 100644 --- a/libfprint/drivers/virtual-device.c +++ b/libfprint/drivers/virtual-device.c @@ -478,22 +478,21 @@ dev_verify (FpDevice *dev) { g_autoptr(GError) error = NULL; FpDeviceVirtualDevice *self = FP_DEVICE_VIRTUAL_DEVICE (dev); - FpPrint *print; g_autofree char *scan_id = NULL; scan_id = start_scan_command (self, &error); if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_PENDING)) return; - fpi_device_get_verify_data (dev, &print); - if (scan_id) { GVariant *data = NULL; FpPrint *new_scan; + FpPrint *print; gboolean success; g_debug ("Virtual device scanned print %s", scan_id); + fpi_device_get_verify_data (dev, &print); new_scan = fp_print_new (dev); fpi_print_set_type (new_scan, FPI_PRINT_RAW); @@ -502,7 +501,15 @@ dev_verify (FpDevice *dev) data = g_variant_new_string (scan_id); g_object_set (new_scan, "fpi-data", data, NULL); - success = fp_print_equal (print, new_scan); + if (self->prints_storage && !g_hash_table_contains (self->prints_storage, scan_id)) + { + error = fpi_device_error_new (FP_DEVICE_ERROR_DATA_NOT_FOUND); + success = FALSE; + } + else + { + success = fp_print_equal (print, new_scan); + } if (!self->match_reported) { diff --git a/tests/virtual-device.py b/tests/virtual-device.py index 789f385..dea25eb 100644 --- a/tests/virtual-device.py +++ b/tests/virtual-device.py @@ -273,7 +273,7 @@ class VirtualDevice(unittest.TestCase): def check_verify(self, p, scan_nick, match, identify=False): if isinstance(scan_nick, str): self.send_command('SCAN', scan_nick) - else: + elif scan_nick is not None: self.send_auto(scan_nick) self.start_verify(p, identify) @@ -330,8 +330,15 @@ class VirtualDevice(unittest.TestCase): def test_enroll_verify_no_match(self): matching = self.enroll_print('testprint', FPrint.Finger.LEFT_RING) - self.check_verify(matching, 'not-testprint', match=False, - identify=self.dev.supports_identify()) + if self.dev.has_storage(): + with self.assertRaises(GLib.Error) as error: + self.check_verify(matching, 'not-testprint', match=False, + identify=self.dev.supports_identify()) + self.assertTrue(error.exception.matches(FPrint.DeviceError.quark(), + FPrint.DeviceError.DATA_NOT_FOUND)) + else: + self.check_verify(matching, 'not-testprint', match=False, + identify=self.dev.supports_identify()) def test_enroll_verify_error(self): matching = self.enroll_print('testprint', FPrint.Finger.LEFT_RING) @@ -546,7 +553,13 @@ class VirtualDevice(unittest.TestCase): self.wait_timeout(10) self.assertFalse(self._verify_completed) - self.complete_verify() + if self.dev.has_storage(): + with self.assertRaises(GLib.Error) as error: + self.complete_verify() + self.assertTrue(error.exception.matches(FPrint.DeviceError.quark(), + FPrint.DeviceError.DATA_NOT_FOUND)) + else: + self.complete_verify() self.assertTrue(self._verify_reported) def test_close_error(self): @@ -657,8 +670,10 @@ class VirtualDeviceStorage(VirtualDevice): p = self.enroll_print('testprint', FPrint.Finger.RIGHT_THUMB) self.send_command('REMOVE', 'testprint') - with self.assertRaisesRegex(GLib.GError, 'Print was not found'): + with self.assertRaises(GLib.Error) as error: self.dev.delete_print_sync(p) + self.assertTrue(error.exception.matches(FPrint.DeviceError.quark(), + FPrint.DeviceError.DATA_NOT_FOUND)) def test_identify_match(self): rt = self.enroll_print('right-thumb', FPrint.Finger.RIGHT_THUMB) @@ -679,6 +694,23 @@ class VirtualDeviceStorage(VirtualDevice): self.check_verify(FPrint.Print.new(self.dev), FPrint.DeviceRetry.TOO_SHORT, identify=True, match=False) + def test_delete_multiple_times(self): + rt = self.enroll_print('right-thumb', FPrint.Finger.RIGHT_THUMB) + self.dev.delete_print_sync(rt) + + with self.assertRaisesRegex(GLib.Error, 'Print was not found'): + self.dev.delete_print_sync(rt) + + def test_verify_missing_print(self): + with self.assertRaisesRegex(GLib.Error, 'Print was not found'): + self.check_verify(FPrint.Print.new(self.dev), + 'not-existing-print', False, identify=False) + + def test_identify_missing_print(self): + with self.assertRaisesRegex(GLib.Error, 'Print was not found'): + self.check_verify(FPrint.Print.new(self.dev), + 'not-existing-print', False, identify=True) + if __name__ == '__main__': try: