Blame SOURCES/0116-tests-Add-fp-device-basic-unit-tests.patch

73b847
From 02b041eb6d6b5bd125993fc68b2cd275052fe8aa Mon Sep 17 00:00:00 2001
73b847
From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= <mail@3v1n0.net>
73b847
Date: Thu, 5 Dec 2019 17:05:21 +0100
73b847
Subject: [PATCH 116/181] tests: Add fp-device basic unit tests
73b847
73b847
Use the virtual image device as base for now, while the new setup allows
73b847
to create easily fake device drivers without including the driver in
73b847
libfprint itself and test all the fpi_device functionalities.
73b847
---
73b847
 tests/meson.build      |   1 +
73b847
 tests/test-fp-device.c | 238 +++++++++++++++++++++++++++++++++++++++++
73b847
 tests/test-utils.c     |  61 +++++++++++
73b847
 tests/test-utils.h     |  14 +++
73b847
 4 files changed, 314 insertions(+)
73b847
 create mode 100644 tests/test-fp-device.c
73b847
73b847
diff --git a/tests/meson.build b/tests/meson.build
73b847
index 307d9a1..1ef6e34 100644
73b847
--- a/tests/meson.build
73b847
+++ b/tests/meson.build
73b847
@@ -56,6 +56,7 @@ if 'virtual_image' in drivers
73b847
 
73b847
     unit_tests = [
73b847
         'fp-context',
73b847
+        'fp-device',
73b847
     ]
73b847
 
73b847
     foreach test_name: unit_tests
73b847
diff --git a/tests/test-fp-device.c b/tests/test-fp-device.c
73b847
new file mode 100644
73b847
index 0000000..a279b46
73b847
--- /dev/null
73b847
+++ b/tests/test-fp-device.c
73b847
@@ -0,0 +1,238 @@
73b847
+/*
73b847
+ * FpDevice Unit tests
73b847
+ * Copyright (C) 2019 Marco Trevisan <marco.trevisan@canonical.com>
73b847
+ *
73b847
+ * This library is free software; you can redistribute it and/or
73b847
+ * modify it under the terms of the GNU Lesser General Public
73b847
+ * License as published by the Free Software Foundation; either
73b847
+ * version 2.1 of the License, or (at your option) any later version.
73b847
+ *
73b847
+ * This library is distributed in the hope that it will be useful,
73b847
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
73b847
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
73b847
+ * Lesser General Public License for more details.
73b847
+ *
73b847
+ * You should have received a copy of the GNU Lesser General Public
73b847
+ * License along with this library; if not, write to the Free Software
73b847
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73b847
+ */
73b847
+
73b847
+#include <libfprint/fprint.h>
73b847
+
73b847
+#include "test-utils.h"
73b847
+
73b847
+static void
73b847
+on_device_opened (FpDevice *dev, GAsyncResult *res, FptContext *tctx)
73b847
+{
73b847
+  g_autoptr(GError) error = NULL;
73b847
+
73b847
+  g_assert_true (fp_device_open_finish (dev, res, &error));
73b847
+  g_assert_no_error (error);
73b847
+  g_assert_true (fp_device_is_open (tctx->device));
73b847
+
73b847
+  tctx->user_data = GUINT_TO_POINTER (TRUE);
73b847
+}
73b847
+
73b847
+static void
73b847
+test_device_open_async (void)
73b847
+{
73b847
+  g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev ();
73b847
+
73b847
+  fp_device_open (tctx->device, NULL, (GAsyncReadyCallback) on_device_opened, tctx);
73b847
+
73b847
+  while (!GPOINTER_TO_UINT (tctx->user_data))
73b847
+    g_main_context_iteration (NULL, TRUE);
73b847
+}
73b847
+
73b847
+static void
73b847
+on_device_closed (FpDevice *dev, GAsyncResult *res, FptContext *tctx)
73b847
+{
73b847
+  g_autoptr(GError) error = NULL;
73b847
+
73b847
+  g_assert_true (fp_device_close_finish (dev, res, &error));
73b847
+  g_assert_no_error (error);
73b847
+  g_assert_false (fp_device_is_open (tctx->device));
73b847
+
73b847
+  tctx->user_data = GUINT_TO_POINTER (TRUE);
73b847
+}
73b847
+
73b847
+static void
73b847
+test_device_close_async (void)
73b847
+{
73b847
+  g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev ();
73b847
+
73b847
+  fp_device_open (tctx->device, NULL, (GAsyncReadyCallback) on_device_opened, tctx);
73b847
+  while (!tctx->user_data)
73b847
+    g_main_context_iteration (NULL, TRUE);
73b847
+
73b847
+  tctx->user_data = GUINT_TO_POINTER (FALSE);
73b847
+  fp_device_close (tctx->device, NULL, (GAsyncReadyCallback) on_device_closed, tctx);
73b847
+
73b847
+  while (!GPOINTER_TO_UINT (tctx->user_data))
73b847
+    g_main_context_iteration (NULL, TRUE);
73b847
+}
73b847
+
73b847
+static void
73b847
+test_device_open_sync (void)
73b847
+{
73b847
+  g_autoptr(GError) error = NULL;
73b847
+  g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev ();
73b847
+
73b847
+  fp_device_open_sync (tctx->device, NULL, &error);
73b847
+  g_assert_no_error (error);
73b847
+  g_assert_true (fp_device_is_open (tctx->device));
73b847
+
73b847
+  fp_device_open_sync (tctx->device, NULL, &error);
73b847
+  g_assert_error (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_ALREADY_OPEN);
73b847
+}
73b847
+
73b847
+static void
73b847
+on_open_notify (FpDevice *rdev, GParamSpec *spec, FptContext *tctx)
73b847
+{
73b847
+  g_assert_cmpstr (spec->name, ==, "open");
73b847
+  tctx->user_data = GUINT_TO_POINTER (TRUE);
73b847
+}
73b847
+
73b847
+static void
73b847
+test_device_open_sync_notify (void)
73b847
+{
73b847
+  g_autoptr(GError) error = NULL;
73b847
+  g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev ();
73b847
+
73b847
+  g_signal_connect (tctx->device, "notify::open", G_CALLBACK (on_open_notify), tctx);
73b847
+  fp_device_open_sync (tctx->device, NULL, &error);
73b847
+  g_assert_no_error (error);
73b847
+  g_assert_true (GPOINTER_TO_INT (tctx->user_data));
73b847
+}
73b847
+
73b847
+static void
73b847
+test_device_close_sync (void)
73b847
+{
73b847
+  g_autoptr(GError) error = NULL;
73b847
+  g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev ();
73b847
+
73b847
+  fp_device_open_sync (tctx->device, NULL, NULL);
73b847
+  fp_device_close_sync (tctx->device, NULL, &error);
73b847
+  g_assert_no_error (error);
73b847
+  g_assert_false (fp_device_is_open (tctx->device));
73b847
+
73b847
+  fp_device_close_sync (tctx->device, NULL, &error);
73b847
+  g_assert_error (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_NOT_OPEN);
73b847
+}
73b847
+
73b847
+static void
73b847
+on_close_notify (FpDevice *rdev, GParamSpec *spec, FptContext *tctx)
73b847
+{
73b847
+  g_assert_cmpstr (spec->name, ==, "open");
73b847
+  tctx->user_data = GUINT_TO_POINTER (TRUE);
73b847
+}
73b847
+
73b847
+static void
73b847
+test_device_close_sync_notify (void)
73b847
+{
73b847
+  g_autoptr(GError) error = NULL;
73b847
+  g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev ();
73b847
+
73b847
+  fp_device_open_sync (tctx->device, NULL, NULL);
73b847
+
73b847
+  g_signal_connect (tctx->device, "notify::open", G_CALLBACK (on_close_notify), tctx);
73b847
+  fp_device_close_sync (tctx->device, NULL, &error);
73b847
+  g_assert_no_error (error);
73b847
+  g_assert_true (GPOINTER_TO_INT (tctx->user_data));
73b847
+}
73b847
+
73b847
+static void
73b847
+test_device_get_driver (void)
73b847
+{
73b847
+  g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev ();
73b847
+
73b847
+  fp_device_open_sync (tctx->device, NULL, NULL);
73b847
+  g_assert_cmpstr (fp_device_get_driver (tctx->device), ==, "virtual_image");
73b847
+}
73b847
+
73b847
+static void
73b847
+test_device_get_device_id (void)
73b847
+{
73b847
+  g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev ();
73b847
+
73b847
+  fp_device_open_sync (tctx->device, NULL, NULL);
73b847
+  g_assert_cmpstr (fp_device_get_device_id (tctx->device), ==, "0");
73b847
+}
73b847
+
73b847
+static void
73b847
+test_device_get_name (void)
73b847
+{
73b847
+  g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev ();
73b847
+
73b847
+  fp_device_open_sync (tctx->device, NULL, NULL);
73b847
+  g_assert_cmpstr (fp_device_get_name (tctx->device), ==,
73b847
+                   "Virtual image device for debugging");
73b847
+}
73b847
+
73b847
+static void
73b847
+test_device_get_scan_type (void)
73b847
+{
73b847
+  g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev ();
73b847
+
73b847
+  fp_device_open_sync (tctx->device, NULL, NULL);
73b847
+  g_assert_cmpint (fp_device_get_scan_type (tctx->device), ==, FP_SCAN_TYPE_SWIPE);
73b847
+}
73b847
+
73b847
+static void
73b847
+test_device_get_nr_enroll_stages (void)
73b847
+{
73b847
+  g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev ();
73b847
+
73b847
+  fp_device_open_sync (tctx->device, NULL, NULL);
73b847
+  g_assert_cmpuint (fp_device_get_nr_enroll_stages (tctx->device), ==, 5);
73b847
+}
73b847
+
73b847
+static void
73b847
+test_device_supports_identify (void)
73b847
+{
73b847
+  g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev ();
73b847
+
73b847
+  fp_device_open_sync (tctx->device, NULL, NULL);
73b847
+  g_assert_true (fp_device_supports_identify (tctx->device));
73b847
+}
73b847
+
73b847
+static void
73b847
+test_device_supports_capture (void)
73b847
+{
73b847
+  g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev ();
73b847
+
73b847
+  fp_device_open_sync (tctx->device, NULL, NULL);
73b847
+  g_assert_true (fp_device_supports_capture (tctx->device));
73b847
+}
73b847
+
73b847
+static void
73b847
+test_device_has_storage (void)
73b847
+{
73b847
+  g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev ();
73b847
+
73b847
+  fp_device_open_sync (tctx->device, NULL, NULL);
73b847
+  g_assert_false (fp_device_has_storage (tctx->device));
73b847
+}
73b847
+
73b847
+int
73b847
+main (int argc, char *argv[])
73b847
+{
73b847
+  g_test_init (&argc, &argv, NULL);
73b847
+
73b847
+  g_test_add_func ("/device/async/open", test_device_open_async);
73b847
+  g_test_add_func ("/device/async/close", test_device_close_async);
73b847
+  g_test_add_func ("/device/sync/open", test_device_open_sync);
73b847
+  g_test_add_func ("/device/sync/open/notify", test_device_open_sync_notify);
73b847
+  g_test_add_func ("/device/sync/close", test_device_close_sync);
73b847
+  g_test_add_func ("/device/sync/close/notify", test_device_close_sync_notify);
73b847
+  g_test_add_func ("/device/sync/get_driver", test_device_get_driver);
73b847
+  g_test_add_func ("/device/sync/get_device_id", test_device_get_device_id);
73b847
+  g_test_add_func ("/device/sync/get_name", test_device_get_name);
73b847
+  g_test_add_func ("/device/sync/get_scan_type", test_device_get_scan_type);
73b847
+  g_test_add_func ("/device/sync/get_nr_enroll_stages", test_device_get_nr_enroll_stages);
73b847
+  g_test_add_func ("/device/sync/supports_identify", test_device_supports_identify);
73b847
+  g_test_add_func ("/device/sync/supports_capture", test_device_supports_capture);
73b847
+  g_test_add_func ("/device/sync/has_storage", test_device_has_storage);
73b847
+
73b847
+  return g_test_run ();
73b847
+}
73b847
diff --git a/tests/test-utils.c b/tests/test-utils.c
73b847
index f789058..834a90e 100644
73b847
--- a/tests/test-utils.c
73b847
+++ b/tests/test-utils.c
73b847
@@ -17,6 +17,7 @@
73b847
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73b847
  */
73b847
 
73b847
+#include <libfprint/fprint.h>
73b847
 #include <glib/gstdio.h>
73b847
 
73b847
 #include "test-utils.h"
73b847
@@ -64,3 +65,63 @@ fpt_setup_virtual_device_environment (void)
73b847
   signal (SIGQUIT, on_signal_event);
73b847
   signal (SIGPIPE, on_signal_event);
73b847
 }
73b847
+
73b847
+FptContext *
73b847
+fpt_context_new (void)
73b847
+{
73b847
+  FptContext *tctx;
73b847
+
73b847
+  tctx = g_new0 (FptContext, 1);
73b847
+  tctx->fp_context = fp_context_new ();
73b847
+
73b847
+  return tctx;
73b847
+}
73b847
+
73b847
+FptContext *
73b847
+fpt_context_new_with_virtual_imgdev (void)
73b847
+{
73b847
+  FptContext *tctx;
73b847
+  GPtrArray *devices;
73b847
+  unsigned int i;
73b847
+
73b847
+  fpt_setup_virtual_device_environment ();
73b847
+
73b847
+  tctx = fpt_context_new ();
73b847
+  devices = fp_context_get_devices (tctx->fp_context);
73b847
+
73b847
+  g_assert_nonnull (devices);
73b847
+  g_assert_cmpuint (devices->len, ==, 1);
73b847
+
73b847
+  for (i = 0; i < devices->len; ++i)
73b847
+    {
73b847
+      FpDevice *device = devices->pdata[i];
73b847
+
73b847
+      if (g_strcmp0 (fp_device_get_driver (device), "virtual_image") == 0)
73b847
+        {
73b847
+          tctx->device = device;
73b847
+          break;
73b847
+        }
73b847
+    }
73b847
+
73b847
+  g_assert_true (FP_IS_DEVICE (tctx->device));
73b847
+  g_object_add_weak_pointer (G_OBJECT (tctx->device), (gpointer) & tctx->device);
73b847
+
73b847
+  return tctx;
73b847
+}
73b847
+
73b847
+void
73b847
+fpt_context_free (FptContext *tctx)
73b847
+{
73b847
+  if (tctx->device && fp_device_is_open (tctx->device))
73b847
+    {
73b847
+      g_autoptr(GError) error = NULL;
73b847
+
73b847
+      fp_device_close_sync (tctx->device, NULL, &error);
73b847
+      g_assert_no_error (error);
73b847
+    }
73b847
+
73b847
+  g_clear_object (&tctx->fp_context);
73b847
+  g_free (tctx);
73b847
+
73b847
+  fpt_teardown_virtual_device_environment ();
73b847
+}
73b847
diff --git a/tests/test-utils.h b/tests/test-utils.h
73b847
index 369da78..4bc1e69 100644
73b847
--- a/tests/test-utils.h
73b847
+++ b/tests/test-utils.h
73b847
@@ -21,3 +21,17 @@
73b847
 
73b847
 void fpt_setup_virtual_device_environment (void);
73b847
 void fpt_teardown_virtual_device_environment (void);
73b847
+
73b847
+typedef struct _FptContext
73b847
+{
73b847
+  FpContext *fp_context;
73b847
+  FpDevice  *device;
73b847
+  gpointer   user_data;
73b847
+} FptContext;
73b847
+
73b847
+FptContext * fpt_context_new (void);
73b847
+FptContext * fpt_context_new_with_virtual_imgdev (void);
73b847
+
73b847
+void fpt_context_free (FptContext *test_context);
73b847
+
73b847
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (FptContext, fpt_context_free)
73b847
-- 
73b847
2.24.1
73b847