tests: Add support for creating other virtual readers

Add support for creating more virtual readers.

Co-authored-by: Bastien Nocera <hadess@hadess.net>
This commit is contained in:
Benjamin Berg 2021-01-05 15:57:21 +01:00
parent 2f2da87240
commit 5df14206d8
4 changed files with 58 additions and 33 deletions

View file

@ -50,7 +50,7 @@ test_context_has_virtual_device (void)
GPtrArray *devices; GPtrArray *devices;
unsigned int i; unsigned int i;
fpt_setup_virtual_device_environment (); fpt_setup_virtual_device_environment (FPT_VIRTUAL_DEVICE_IMAGE);
context = fp_context_new (); context = fp_context_new ();
devices = fp_context_get_devices (context); devices = fp_context_get_devices (context);
@ -82,7 +82,7 @@ test_context_enumerates_new_devices (void)
context = fp_context_new (); context = fp_context_new ();
fpt_setup_virtual_device_environment (); fpt_setup_virtual_device_environment (FPT_VIRTUAL_DEVICE_IMAGE);
fp_context_enumerate (context); fp_context_enumerate (context);
devices = fp_context_get_devices (context); devices = fp_context_get_devices (context);
@ -120,7 +120,7 @@ context_device_removed_cb (FpContext *ctx, FpDevice *device, FptContext *tctx)
static void static void
test_context_remove_device_closed (void) test_context_remove_device_closed (void)
{ {
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
gboolean removed; gboolean removed;
tctx->user_data = NULL; tctx->user_data = NULL;
@ -162,7 +162,7 @@ close_done_cb (GObject *device, GAsyncResult *res, gpointer user_data)
static void static void
test_context_remove_device_closing (void) test_context_remove_device_closing (void)
{ {
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
g_autoptr(GError) close_error = NULL; g_autoptr(GError) close_error = NULL;
g_autoptr(GError) error = NULL; g_autoptr(GError) error = NULL;
gboolean removed; gboolean removed;
@ -207,7 +207,7 @@ test_context_remove_device_closing (void)
static void static void
test_context_remove_device_open (void) test_context_remove_device_open (void)
{ {
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
g_autoptr(GError) error = NULL; g_autoptr(GError) error = NULL;
gboolean removed = FALSE; gboolean removed = FALSE;
@ -267,7 +267,7 @@ open_done_cb (GObject *device, GAsyncResult *res, gpointer user_data)
static void static void
test_context_remove_device_opening (void) test_context_remove_device_opening (void)
{ {
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
g_autoptr(GError) close_error = NULL; g_autoptr(GError) close_error = NULL;
gboolean open_done = FALSE; gboolean open_done = FALSE;
gboolean removed; gboolean removed;
@ -327,7 +327,7 @@ enroll_done_cb (GObject *device, GAsyncResult *res, gpointer user_data)
static void static void
test_context_remove_device_active (void) test_context_remove_device_active (void)
{ {
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
g_autoptr(GError) error = NULL; g_autoptr(GError) error = NULL;
g_autoptr(GCancellable) cancellable = NULL; g_autoptr(GCancellable) cancellable = NULL;
g_autoptr(GError) enroll_error = NULL; g_autoptr(GError) enroll_error = NULL;

View file

@ -36,7 +36,7 @@ on_device_opened (FpDevice *dev, GAsyncResult *res, FptContext *tctx)
static void static void
test_device_open_async (void) test_device_open_async (void)
{ {
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
fp_device_open (tctx->device, NULL, (GAsyncReadyCallback) on_device_opened, tctx); fp_device_open (tctx->device, NULL, (GAsyncReadyCallback) on_device_opened, tctx);
@ -59,7 +59,7 @@ on_device_closed (FpDevice *dev, GAsyncResult *res, FptContext *tctx)
static void static void
test_device_close_async (void) test_device_close_async (void)
{ {
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
fp_device_open (tctx->device, NULL, (GAsyncReadyCallback) on_device_opened, tctx); fp_device_open (tctx->device, NULL, (GAsyncReadyCallback) on_device_opened, tctx);
while (!tctx->user_data) while (!tctx->user_data)
@ -76,7 +76,7 @@ static void
test_device_open_sync (void) test_device_open_sync (void)
{ {
g_autoptr(GError) error = NULL; g_autoptr(GError) error = NULL;
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
fp_device_open_sync (tctx->device, NULL, &error); fp_device_open_sync (tctx->device, NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
@ -97,7 +97,7 @@ static void
test_device_open_sync_notify (void) test_device_open_sync_notify (void)
{ {
g_autoptr(GError) error = NULL; g_autoptr(GError) error = NULL;
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
g_signal_connect (tctx->device, "notify::open", G_CALLBACK (on_open_notify), tctx); g_signal_connect (tctx->device, "notify::open", G_CALLBACK (on_open_notify), tctx);
fp_device_open_sync (tctx->device, NULL, &error); fp_device_open_sync (tctx->device, NULL, &error);
@ -109,7 +109,7 @@ static void
test_device_close_sync (void) test_device_close_sync (void)
{ {
g_autoptr(GError) error = NULL; g_autoptr(GError) error = NULL;
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
fp_device_open_sync (tctx->device, NULL, NULL); fp_device_open_sync (tctx->device, NULL, NULL);
fp_device_close_sync (tctx->device, NULL, &error); fp_device_close_sync (tctx->device, NULL, &error);
@ -131,7 +131,7 @@ static void
test_device_close_sync_notify (void) test_device_close_sync_notify (void)
{ {
g_autoptr(GError) error = NULL; g_autoptr(GError) error = NULL;
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
fp_device_open_sync (tctx->device, NULL, NULL); fp_device_open_sync (tctx->device, NULL, NULL);
@ -144,7 +144,7 @@ test_device_close_sync_notify (void)
static void static void
test_device_get_driver (void) test_device_get_driver (void)
{ {
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
fp_device_open_sync (tctx->device, NULL, NULL); fp_device_open_sync (tctx->device, NULL, NULL);
g_assert_cmpstr (fp_device_get_driver (tctx->device), ==, "virtual_image"); g_assert_cmpstr (fp_device_get_driver (tctx->device), ==, "virtual_image");
@ -153,7 +153,7 @@ test_device_get_driver (void)
static void static void
test_device_get_device_id (void) test_device_get_device_id (void)
{ {
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
fp_device_open_sync (tctx->device, NULL, NULL); fp_device_open_sync (tctx->device, NULL, NULL);
g_assert_cmpstr (fp_device_get_device_id (tctx->device), ==, "0"); g_assert_cmpstr (fp_device_get_device_id (tctx->device), ==, "0");
@ -162,7 +162,7 @@ test_device_get_device_id (void)
static void static void
test_device_get_name (void) test_device_get_name (void)
{ {
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
fp_device_open_sync (tctx->device, NULL, NULL); fp_device_open_sync (tctx->device, NULL, NULL);
g_assert_cmpstr (fp_device_get_name (tctx->device), ==, g_assert_cmpstr (fp_device_get_name (tctx->device), ==,
@ -172,7 +172,7 @@ test_device_get_name (void)
static void static void
test_device_get_scan_type (void) test_device_get_scan_type (void)
{ {
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
fp_device_open_sync (tctx->device, NULL, NULL); fp_device_open_sync (tctx->device, NULL, NULL);
g_assert_cmpint (fp_device_get_scan_type (tctx->device), ==, FP_SCAN_TYPE_SWIPE); g_assert_cmpint (fp_device_get_scan_type (tctx->device), ==, FP_SCAN_TYPE_SWIPE);
@ -181,7 +181,7 @@ test_device_get_scan_type (void)
static void static void
test_device_get_finger_status (void) test_device_get_finger_status (void)
{ {
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
fp_device_open_sync (tctx->device, NULL, NULL); fp_device_open_sync (tctx->device, NULL, NULL);
g_assert_cmpint (fp_device_get_finger_status (tctx->device), ==, FP_FINGER_STATUS_NONE); g_assert_cmpint (fp_device_get_finger_status (tctx->device), ==, FP_FINGER_STATUS_NONE);
@ -190,7 +190,7 @@ test_device_get_finger_status (void)
static void static void
test_device_get_nr_enroll_stages (void) test_device_get_nr_enroll_stages (void)
{ {
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
fp_device_open_sync (tctx->device, NULL, NULL); fp_device_open_sync (tctx->device, NULL, NULL);
g_assert_cmpuint (fp_device_get_nr_enroll_stages (tctx->device), ==, 5); g_assert_cmpuint (fp_device_get_nr_enroll_stages (tctx->device), ==, 5);
@ -199,7 +199,7 @@ test_device_get_nr_enroll_stages (void)
static void static void
test_device_supports_identify (void) test_device_supports_identify (void)
{ {
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
fp_device_open_sync (tctx->device, NULL, NULL); fp_device_open_sync (tctx->device, NULL, NULL);
g_assert_true (fp_device_supports_identify (tctx->device)); g_assert_true (fp_device_supports_identify (tctx->device));
@ -208,7 +208,7 @@ test_device_supports_identify (void)
static void static void
test_device_supports_capture (void) test_device_supports_capture (void)
{ {
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
fp_device_open_sync (tctx->device, NULL, NULL); fp_device_open_sync (tctx->device, NULL, NULL);
g_assert_true (fp_device_supports_capture (tctx->device)); g_assert_true (fp_device_supports_capture (tctx->device));
@ -217,7 +217,7 @@ test_device_supports_capture (void)
static void static void
test_device_has_storage (void) test_device_has_storage (void)
{ {
g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_imgdev (); g_autoptr(FptContext) tctx = fpt_context_new_with_virtual_device (FPT_VIRTUAL_DEVICE_IMAGE);
fp_device_open_sync (tctx->device, NULL, NULL); fp_device_open_sync (tctx->device, NULL, NULL);
g_assert_false (fp_device_has_storage (tctx->device)); g_assert_false (fp_device_has_storage (tctx->device));

View file

@ -22,16 +22,29 @@
#include "test-utils.h" #include "test-utils.h"
struct
{
const char *envvar;
const char *driver_id;
const char *device_id;
} devtype_vars[FPT_NUM_VIRTUAL_DEVICE_TYPES] = {
{ "FP_VIRTUAL_IMAGE", "virtual_image", "virtual_image" }, /* FPT_VIRTUAL_DEVICE_IMAGE */
};
static FptVirtualDeviceType global_devtype;
void void
fpt_teardown_virtual_device_environment (void) fpt_teardown_virtual_device_environment (void)
{ {
const char *path = g_getenv ("FP_VIRTUAL_IMAGE"); const char *path;
path = g_getenv (devtype_vars[global_devtype].envvar);
if (path) if (path)
{ {
g_autofree char *temp_dir = g_path_get_dirname (path); g_autofree char *temp_dir = g_path_get_dirname (path);
g_unsetenv ("FP_VIRTUAL_IMAGE"); g_unsetenv (devtype_vars[global_devtype].envvar);
g_unlink (path); g_unlink (path);
g_rmdir (temp_dir); g_rmdir (temp_dir);
} }
@ -44,19 +57,23 @@ on_signal_event (int sig)
} }
void void
fpt_setup_virtual_device_environment (void) fpt_setup_virtual_device_environment (FptVirtualDeviceType devtype)
{ {
g_autoptr(GError) error = NULL; g_autoptr(GError) error = NULL;
g_autofree char *temp_dir = NULL; g_autofree char *temp_dir = NULL;
g_autofree char *temp_path = NULL; g_autofree char *temp_path = NULL;
g_autofree char *filename = NULL;
g_assert_null (g_getenv ("FP_VIRTUAL_IMAGE")); g_assert_null (g_getenv (devtype_vars[devtype].envvar));
temp_dir = g_dir_make_tmp ("libfprint-XXXXXX", &error); temp_dir = g_dir_make_tmp ("libfprint-XXXXXX", &error);
g_assert_no_error (error); g_assert_no_error (error);
temp_path = g_build_filename (temp_dir, "virtual-image.socket", NULL); filename = g_strdup_printf ("%s.socket", devtype_vars[devtype].device_id);
g_setenv ("FP_VIRTUAL_IMAGE", temp_path, TRUE); temp_path = g_build_filename (temp_dir, filename, NULL);
g_setenv (devtype_vars[devtype].envvar, temp_path, TRUE);
global_devtype = devtype;
signal (SIGKILL, on_signal_event); signal (SIGKILL, on_signal_event);
signal (SIGABRT, on_signal_event); signal (SIGABRT, on_signal_event);
@ -78,13 +95,16 @@ fpt_context_new (void)
} }
FptContext * FptContext *
fpt_context_new_with_virtual_imgdev (void) fpt_context_new_with_virtual_device (FptVirtualDeviceType devtype)
{ {
FptContext *tctx; FptContext *tctx;
GPtrArray *devices; GPtrArray *devices;
unsigned int i; unsigned int i;
fpt_setup_virtual_device_environment (); g_assert_true (devtype >= FPT_VIRTUAL_DEVICE_IMAGE &&
devtype < FPT_NUM_VIRTUAL_DEVICE_TYPES);
fpt_setup_virtual_device_environment (devtype);
tctx = fpt_context_new (); tctx = fpt_context_new ();
devices = fp_context_get_devices (tctx->fp_context); devices = fp_context_get_devices (tctx->fp_context);
@ -96,7 +116,7 @@ fpt_context_new_with_virtual_imgdev (void)
{ {
FpDevice *device = devices->pdata[i]; FpDevice *device = devices->pdata[i];
if (g_strcmp0 (fp_device_get_driver (device), "virtual_image") == 0) if (g_strcmp0 (fp_device_get_driver (device), devtype_vars[devtype].driver_id) == 0)
{ {
tctx->device = device; tctx->device = device;
break; break;

View file

@ -19,7 +19,12 @@
#include <glib.h> #include <glib.h>
void fpt_setup_virtual_device_environment (void); typedef enum {
FPT_VIRTUAL_DEVICE_IMAGE = 0,
FPT_NUM_VIRTUAL_DEVICE_TYPES
} FptVirtualDeviceType;
void fpt_setup_virtual_device_environment (FptVirtualDeviceType devtype);
void fpt_teardown_virtual_device_environment (void); void fpt_teardown_virtual_device_environment (void);
typedef struct _FptContext typedef struct _FptContext
@ -30,7 +35,7 @@ typedef struct _FptContext
} FptContext; } FptContext;
FptContext * fpt_context_new (void); FptContext * fpt_context_new (void);
FptContext * fpt_context_new_with_virtual_imgdev (void); FptContext * fpt_context_new_with_virtual_device (FptVirtualDeviceType devtype);
void fpt_context_free (FptContext *test_context); void fpt_context_free (FptContext *test_context);