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
This commit is contained in:
Marco Trevisan (Treviño) 2019-12-05 14:38:41 +01:00
parent eeddd8c7bc
commit da46f53e82
6 changed files with 225 additions and 4 deletions

View file

@ -199,10 +199,7 @@ if get_option('gtk-examples')
subdir('demo') subdir('demo')
endif endif
# The tests require introspeciton support to run subdir('tests')
if get_option('introspection')
subdir('tests')
endif
pkgconfig = import('pkgconfig') pkgconfig = import('pkgconfig')
pkgconfig.generate( pkgconfig.generate(

View file

@ -48,6 +48,32 @@ if get_option('introspection')
endforeach endforeach
endif 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) gdb = find_program('gdb', required: false)
if gdb.found() if gdb.found()
add_test_setup('gdb', add_test_setup('gdb',

106
tests/test-fp-context.c Normal file
View file

@ -0,0 +1,106 @@
/*
* FpContext Unit tests
* Copyright (C) 2019 Marco Trevisan <marco.trevisan@canonical.com>
*
* 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 <libfprint/fprint.h>
#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 ();
}

3
tests/test-runner.sh Executable file
View file

@ -0,0 +1,3 @@
#!/bin/bash
exec $LIBFPRINT_TEST_WRAPPER $@

66
tests/test-utils.c Normal file
View file

@ -0,0 +1,66 @@
/*
* Unit tests for libfprint
* Copyright (C) 2019 Marco Trevisan <marco.trevisan@canonical.com>
*
* 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 <glib/gstdio.h>
#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);
}

23
tests/test-utils.h Normal file
View file

@ -0,0 +1,23 @@
/*
* Unit tests for libfprint
* Copyright (C) 2019 Marco Trevisan <marco.trevisan@canonical.com>
*
* 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 <glib.h>
void fpt_setup_virtual_device_environment (void);
void fpt_teardown_virtual_device_environment (void);