Blame SOURCES/0115-tests-Add-basic-unit-tests-for-fp-context.patch

73b847
From a4f2cc60e4676f8aaef221d14e94cd0250c5d591 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 14:38:41 +0100
73b847
Subject: [PATCH 115/181] tests: Add basic unit tests for fp-context
73b847
73b847
Link the tests with the private library using an utils library that will
73b847
be useful to share other tests functions
73b847
---
73b847
 meson.build             |   5 +-
73b847
 tests/meson.build       |  26 ++++++++++
73b847
 tests/test-fp-context.c | 106 ++++++++++++++++++++++++++++++++++++++++
73b847
 tests/test-runner.sh    |   3 ++
73b847
 tests/test-utils.c      |  66 +++++++++++++++++++++++++
73b847
 tests/test-utils.h      |  23 +++++++++
73b847
 6 files changed, 225 insertions(+), 4 deletions(-)
73b847
 create mode 100644 tests/test-fp-context.c
73b847
 create mode 100755 tests/test-runner.sh
73b847
 create mode 100644 tests/test-utils.c
73b847
 create mode 100644 tests/test-utils.h
73b847
73b847
diff --git a/meson.build b/meson.build
73b847
index 3f72118..8ea4a8b 100644
73b847
--- a/meson.build
73b847
+++ b/meson.build
73b847
@@ -199,10 +199,7 @@ if get_option('gtk-examples')
73b847
     subdir('demo')
73b847
 endif
73b847
 
73b847
-# The tests require introspeciton support to run
73b847
-if get_option('introspection')
73b847
-    subdir('tests')
73b847
-endif
73b847
+subdir('tests')
73b847
 
73b847
 pkgconfig = import('pkgconfig')
73b847
 pkgconfig.generate(
73b847
diff --git a/tests/meson.build b/tests/meson.build
73b847
index 082ce86..307d9a1 100644
73b847
--- a/tests/meson.build
73b847
+++ b/tests/meson.build
73b847
@@ -48,6 +48,32 @@ if get_option('introspection')
73b847
     endforeach
73b847
 endif
73b847
 
73b847
+if 'virtual_image' in drivers
73b847
+    test_utils = static_library('fprint-test-utils',
73b847
+        sources: ['test-utils.c'],
73b847
+        dependencies: libfprint_private_dep,
73b847
+        install: false)
73b847
+
73b847
+    unit_tests = [
73b847
+        'fp-context',
73b847
+    ]
73b847
+
73b847
+    foreach test_name: unit_tests
73b847
+        basename = 'test-' + test_name
73b847
+        test_exe = executable(basename,
73b847
+            sources: basename + '.c',
73b847
+            dependencies: libfprint_private_dep,
73b847
+            c_args: common_cflags,
73b847
+            link_with: test_utils)
73b847
+        test(test_name,
73b847
+            find_program('test-runner.sh'),
73b847
+            suite: ['unit-tests'],
73b847
+            args: [test_exe],
73b847
+            env: envs,
73b847
+        )
73b847
+    endforeach
73b847
+endif
73b847
+
73b847
 gdb = find_program('gdb', required: false)
73b847
 if gdb.found()
73b847
     add_test_setup('gdb',
73b847
diff --git a/tests/test-fp-context.c b/tests/test-fp-context.c
73b847
new file mode 100644
73b847
index 0000000..01516b9
73b847
--- /dev/null
73b847
+++ b/tests/test-fp-context.c
73b847
@@ -0,0 +1,106 @@
73b847
+/*
73b847
+ * FpContext 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
+test_context_new (void)
73b847
+{
73b847
+  g_autoptr(FpContext) context = fp_context_new ();
73b847
+  g_assert_true (FP_CONTEXT (context));
73b847
+}
73b847
+
73b847
+static void
73b847
+test_context_has_no_devices (void)
73b847
+{
73b847
+  g_autoptr(FpContext) context = NULL;
73b847
+  GPtrArray *devices;
73b847
+
73b847
+  context = fp_context_new ();
73b847
+  devices = fp_context_get_devices (context);
73b847
+
73b847
+  g_assert_nonnull (devices);
73b847
+  g_assert_cmpuint (devices->len, ==, 0);
73b847
+}
73b847
+
73b847
+static void
73b847
+test_context_has_virtual_device (void)
73b847
+{
73b847
+  g_autoptr(FpContext) context = NULL;
73b847
+  FpDevice *virtual_device = NULL;
73b847
+  GPtrArray *devices;
73b847
+  unsigned int i;
73b847
+
73b847
+  fpt_setup_virtual_device_environment ();
73b847
+
73b847
+  context = fp_context_new ();
73b847
+  devices = fp_context_get_devices (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
+          virtual_device = device;
73b847
+          break;
73b847
+        }
73b847
+    }
73b847
+
73b847
+  g_assert_true (FP_IS_DEVICE (virtual_device));
73b847
+
73b847
+  fpt_teardown_virtual_device_environment ();
73b847
+}
73b847
+
73b847
+static void
73b847
+test_context_enumerates_new_devices (void)
73b847
+{
73b847
+  g_autoptr(FpContext) context = NULL;
73b847
+  GPtrArray *devices;
73b847
+
73b847
+  context = fp_context_new ();
73b847
+
73b847
+  fpt_setup_virtual_device_environment ();
73b847
+
73b847
+  fp_context_enumerate (context);
73b847
+  devices = fp_context_get_devices (context);
73b847
+
73b847
+  g_assert_nonnull (devices);
73b847
+  g_assert_cmpuint (devices->len, ==, 1);
73b847
+
73b847
+  fpt_teardown_virtual_device_environment ();
73b847
+}
73b847
+
73b847
+int
73b847
+main (int argc, char *argv[])
73b847
+{
73b847
+  g_test_init (&argc, &argv, NULL);
73b847
+
73b847
+  g_test_add_func ("/context/new", test_context_new);
73b847
+  g_test_add_func ("/context/no-devices", test_context_has_no_devices);
73b847
+  g_test_add_func ("/context/has-virtual-device", test_context_has_virtual_device);
73b847
+  g_test_add_func ("/context/enumerates-new-devices", test_context_enumerates_new_devices);
73b847
+
73b847
+  return g_test_run ();
73b847
+}
73b847
diff --git a/tests/test-runner.sh b/tests/test-runner.sh
73b847
new file mode 100755
73b847
index 0000000..18b038b
73b847
--- /dev/null
73b847
+++ b/tests/test-runner.sh
73b847
@@ -0,0 +1,3 @@
73b847
+#!/bin/bash
73b847
+
73b847
+exec $LIBFPRINT_TEST_WRAPPER $@
73b847
diff --git a/tests/test-utils.c b/tests/test-utils.c
73b847
new file mode 100644
73b847
index 0000000..f789058
73b847
--- /dev/null
73b847
+++ b/tests/test-utils.c
73b847
@@ -0,0 +1,66 @@
73b847
+/*
73b847
+ * Unit tests for libfprint
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 <glib/gstdio.h>
73b847
+
73b847
+#include "test-utils.h"
73b847
+
73b847
+void
73b847
+fpt_teardown_virtual_device_environment (void)
73b847
+{
73b847
+  const char *path = g_getenv ("FP_VIRTUAL_IMAGE");
73b847
+
73b847
+  if (path)
73b847
+    {
73b847
+      g_autofree char *temp_dir = g_path_get_dirname (path);
73b847
+
73b847
+      g_unsetenv ("FP_VIRTUAL_IMAGE");
73b847
+      g_unlink (path);
73b847
+      g_rmdir (temp_dir);
73b847
+    }
73b847
+}
73b847
+
73b847
+static void
73b847
+on_signal_event (int sig)
73b847
+{
73b847
+  fpt_teardown_virtual_device_environment ();
73b847
+}
73b847
+
73b847
+void
73b847
+fpt_setup_virtual_device_environment (void)
73b847
+{
73b847
+  g_autoptr(GError) error = NULL;
73b847
+  g_autofree char *temp_dir = NULL;
73b847
+  g_autofree char *temp_path = NULL;
73b847
+
73b847
+  g_assert_null (g_getenv ("FP_VIRTUAL_IMAGE"));
73b847
+
73b847
+  temp_dir = g_dir_make_tmp ("libfprint-XXXXXX", &error);
73b847
+  g_assert_no_error (error);
73b847
+
73b847
+  temp_path = g_build_filename (temp_dir, "virtual-image.socket", NULL);
73b847
+  g_setenv ("FP_VIRTUAL_IMAGE", temp_path, TRUE);
73b847
+
73b847
+  signal (SIGKILL, on_signal_event);
73b847
+  signal (SIGABRT, on_signal_event);
73b847
+  signal (SIGSEGV, on_signal_event);
73b847
+  signal (SIGTERM, on_signal_event);
73b847
+  signal (SIGQUIT, on_signal_event);
73b847
+  signal (SIGPIPE, on_signal_event);
73b847
+}
73b847
diff --git a/tests/test-utils.h b/tests/test-utils.h
73b847
new file mode 100644
73b847
index 0000000..369da78
73b847
--- /dev/null
73b847
+++ b/tests/test-utils.h
73b847
@@ -0,0 +1,23 @@
73b847
+/*
73b847
+ * Unit tests for libfprint
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 <glib.h>
73b847
+
73b847
+void fpt_setup_virtual_device_environment (void);
73b847
+void fpt_teardown_virtual_device_environment (void);
73b847
-- 
73b847
2.24.1
73b847