From a4f2cc60e4676f8aaef221d14e94cd0250c5d591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Thu, 5 Dec 2019 14:38:41 +0100 Subject: [PATCH 115/181] tests: Add basic unit tests for fp-context Link the tests with the private library using an utils library that will be useful to share other tests functions --- meson.build | 5 +- tests/meson.build | 26 ++++++++++ tests/test-fp-context.c | 106 ++++++++++++++++++++++++++++++++++++++++ tests/test-runner.sh | 3 ++ tests/test-utils.c | 66 +++++++++++++++++++++++++ tests/test-utils.h | 23 +++++++++ 6 files changed, 225 insertions(+), 4 deletions(-) create mode 100644 tests/test-fp-context.c create mode 100755 tests/test-runner.sh create mode 100644 tests/test-utils.c create mode 100644 tests/test-utils.h diff --git a/meson.build b/meson.build index 3f72118..8ea4a8b 100644 --- a/meson.build +++ b/meson.build @@ -199,10 +199,7 @@ if get_option('gtk-examples') subdir('demo') endif -# The tests require introspeciton support to run -if get_option('introspection') - subdir('tests') -endif +subdir('tests') pkgconfig = import('pkgconfig') pkgconfig.generate( diff --git a/tests/meson.build b/tests/meson.build index 082ce86..307d9a1 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -48,6 +48,32 @@ if get_option('introspection') endforeach endif +if 'virtual_image' in drivers + test_utils = static_library('fprint-test-utils', + sources: ['test-utils.c'], + dependencies: libfprint_private_dep, + install: false) + + unit_tests = [ + 'fp-context', + ] + + foreach test_name: unit_tests + basename = 'test-' + test_name + test_exe = executable(basename, + sources: basename + '.c', + dependencies: libfprint_private_dep, + c_args: common_cflags, + link_with: test_utils) + test(test_name, + find_program('test-runner.sh'), + suite: ['unit-tests'], + args: [test_exe], + env: envs, + ) + endforeach +endif + gdb = find_program('gdb', required: false) if gdb.found() add_test_setup('gdb', diff --git a/tests/test-fp-context.c b/tests/test-fp-context.c new file mode 100644 index 0000000..01516b9 --- /dev/null +++ b/tests/test-fp-context.c @@ -0,0 +1,106 @@ +/* + * FpContext Unit tests + * Copyright (C) 2019 Marco Trevisan + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include "test-utils.h" + +static void +test_context_new (void) +{ + g_autoptr(FpContext) context = fp_context_new (); + g_assert_true (FP_CONTEXT (context)); +} + +static void +test_context_has_no_devices (void) +{ + g_autoptr(FpContext) context = NULL; + GPtrArray *devices; + + context = fp_context_new (); + devices = fp_context_get_devices (context); + + g_assert_nonnull (devices); + g_assert_cmpuint (devices->len, ==, 0); +} + +static void +test_context_has_virtual_device (void) +{ + g_autoptr(FpContext) context = NULL; + FpDevice *virtual_device = NULL; + GPtrArray *devices; + unsigned int i; + + fpt_setup_virtual_device_environment (); + + context = fp_context_new (); + devices = fp_context_get_devices (context); + + g_assert_nonnull (devices); + g_assert_cmpuint (devices->len, ==, 1); + + for (i = 0; i < devices->len; ++i) + { + FpDevice *device = devices->pdata[i]; + + if (g_strcmp0 (fp_device_get_driver (device), "virtual_image") == 0) + { + virtual_device = device; + break; + } + } + + g_assert_true (FP_IS_DEVICE (virtual_device)); + + fpt_teardown_virtual_device_environment (); +} + +static void +test_context_enumerates_new_devices (void) +{ + g_autoptr(FpContext) context = NULL; + GPtrArray *devices; + + context = fp_context_new (); + + fpt_setup_virtual_device_environment (); + + fp_context_enumerate (context); + devices = fp_context_get_devices (context); + + g_assert_nonnull (devices); + g_assert_cmpuint (devices->len, ==, 1); + + fpt_teardown_virtual_device_environment (); +} + +int +main (int argc, char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/context/new", test_context_new); + g_test_add_func ("/context/no-devices", test_context_has_no_devices); + g_test_add_func ("/context/has-virtual-device", test_context_has_virtual_device); + g_test_add_func ("/context/enumerates-new-devices", test_context_enumerates_new_devices); + + return g_test_run (); +} diff --git a/tests/test-runner.sh b/tests/test-runner.sh new file mode 100755 index 0000000..18b038b --- /dev/null +++ b/tests/test-runner.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +exec $LIBFPRINT_TEST_WRAPPER $@ diff --git a/tests/test-utils.c b/tests/test-utils.c new file mode 100644 index 0000000..f789058 --- /dev/null +++ b/tests/test-utils.c @@ -0,0 +1,66 @@ +/* + * Unit tests for libfprint + * Copyright (C) 2019 Marco Trevisan + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include "test-utils.h" + +void +fpt_teardown_virtual_device_environment (void) +{ + const char *path = g_getenv ("FP_VIRTUAL_IMAGE"); + + if (path) + { + g_autofree char *temp_dir = g_path_get_dirname (path); + + g_unsetenv ("FP_VIRTUAL_IMAGE"); + g_unlink (path); + g_rmdir (temp_dir); + } +} + +static void +on_signal_event (int sig) +{ + fpt_teardown_virtual_device_environment (); +} + +void +fpt_setup_virtual_device_environment (void) +{ + g_autoptr(GError) error = NULL; + g_autofree char *temp_dir = NULL; + g_autofree char *temp_path = NULL; + + g_assert_null (g_getenv ("FP_VIRTUAL_IMAGE")); + + temp_dir = g_dir_make_tmp ("libfprint-XXXXXX", &error); + g_assert_no_error (error); + + temp_path = g_build_filename (temp_dir, "virtual-image.socket", NULL); + g_setenv ("FP_VIRTUAL_IMAGE", temp_path, TRUE); + + signal (SIGKILL, on_signal_event); + signal (SIGABRT, on_signal_event); + signal (SIGSEGV, on_signal_event); + signal (SIGTERM, on_signal_event); + signal (SIGQUIT, on_signal_event); + signal (SIGPIPE, on_signal_event); +} diff --git a/tests/test-utils.h b/tests/test-utils.h new file mode 100644 index 0000000..369da78 --- /dev/null +++ b/tests/test-utils.h @@ -0,0 +1,23 @@ +/* + * Unit tests for libfprint + * Copyright (C) 2019 Marco Trevisan + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +void fpt_setup_virtual_device_environment (void); +void fpt_teardown_virtual_device_environment (void); -- 2.24.1