Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Unit tests for libfprint | ||
3 | * Copyright (C) 2019 Marco Trevisan <marco.trevisan@canonical.com> | ||
4 | * | ||
5 | * This library is free software; you can redistribute it and/or | ||
6 | * modify it under the terms of the GNU Lesser General Public | ||
7 | * License as published by the Free Software Foundation; either | ||
8 | * version 2.1 of the License, or (at your option) any later version. | ||
9 | * | ||
10 | * This library is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | * Lesser General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Lesser General Public | ||
16 | * License along with this library; if not, write to the Free Software | ||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | */ | ||
19 | |||
20 | #include <libfprint/fprint.h> | ||
21 | #include <glib/gstdio.h> | ||
22 | |||
23 | #include "test-utils.h" | ||
24 | |||
25 | struct | ||
26 | { | ||
27 | const char *envvar; | ||
28 | const char *driver_id; | ||
29 | const char *device_id; | ||
30 | } devtype_vars[FPT_NUM_VIRTUAL_DEVICE_TYPES] = { | ||
31 | { "FP_VIRTUAL_IMAGE", "virtual_image", "virtual_image" }, /* FPT_VIRTUAL_DEVICE_IMAGE */ | ||
32 | { "FP_VIRTUAL_DEVICE", "virtual_device", "virtual_device" }, /* FPT_VIRTUAL_DEVICE_NONIMAGE */ | ||
33 | { "FP_VIRTUAL_DEVICE_STORAGE", "virtual_device_storage", "virtual_device_storage" } /* FPT_VIRTUAL_DEVICE_NONIMAGE_STORAGE */ | ||
34 | }; | ||
35 | |||
36 | static FptVirtualDeviceType global_devtype; | ||
37 | |||
38 | void | ||
39 | 29 | fpt_teardown_virtual_device_environment (void) | |
40 | { | ||
41 | 29 | const char *path; | |
42 | |||
43 | 29 | path = g_getenv (devtype_vars[global_devtype].envvar); | |
44 | |||
45 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 5 times.
|
29 | if (path) |
46 | { | ||
47 | 48 | g_autofree char *temp_dir = g_path_get_dirname (path); | |
48 | |||
49 | 24 | g_unsetenv (devtype_vars[global_devtype].envvar); | |
50 | 24 | g_unlink (path); | |
51 | 24 | g_rmdir (temp_dir); | |
52 | } | ||
53 | 29 | } | |
54 | |||
55 | static void | ||
56 | ✗ | on_signal_event (int sig) | |
57 | { | ||
58 | ✗ | fpt_teardown_virtual_device_environment (); | |
59 | ✗ | } | |
60 | |||
61 | void | ||
62 | 24 | fpt_setup_virtual_device_environment (FptVirtualDeviceType devtype) | |
63 | { | ||
64 | 48 | g_autoptr(GError) error = NULL; | |
65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | g_autofree char *temp_dir = NULL; |
66 | 24 | g_autofree char *temp_path = NULL; | |
67 | 24 | g_autofree char *filename = NULL; | |
68 | |||
69 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
|
24 | g_assert_null (g_getenv (devtype_vars[devtype].envvar)); |
70 | |||
71 | 24 | temp_dir = g_dir_make_tmp ("libfprint-XXXXXX", &error); | |
72 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | g_assert_no_error (error); |
73 | |||
74 | 24 | filename = g_strdup_printf ("%s.socket", devtype_vars[devtype].device_id); | |
75 | 24 | temp_path = g_build_filename (temp_dir, filename, NULL); | |
76 | 24 | g_setenv (devtype_vars[devtype].envvar, temp_path, TRUE); | |
77 | |||
78 | 24 | global_devtype = devtype; | |
79 | |||
80 | 24 | signal (SIGKILL, on_signal_event); | |
81 | 24 | signal (SIGABRT, on_signal_event); | |
82 | 24 | signal (SIGSEGV, on_signal_event); | |
83 | 24 | signal (SIGTERM, on_signal_event); | |
84 | 24 | signal (SIGQUIT, on_signal_event); | |
85 | 24 | signal (SIGPIPE, on_signal_event); | |
86 | 24 | } | |
87 | |||
88 | FptContext * | ||
89 | 22 | fpt_context_new (void) | |
90 | { | ||
91 | 22 | FptContext *tctx; | |
92 | |||
93 | 22 | tctx = g_new0 (FptContext, 1); | |
94 | 22 | tctx->fp_context = fp_context_new (); | |
95 | |||
96 | 22 | return tctx; | |
97 | } | ||
98 | |||
99 | FptContext * | ||
100 | 22 | fpt_context_new_with_virtual_device (FptVirtualDeviceType devtype) | |
101 | { | ||
102 | 22 | FptContext *tctx; | |
103 | 22 | GPtrArray *devices; | |
104 | 22 | unsigned int i; | |
105 | |||
106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | g_assert_true (devtype >= FPT_VIRTUAL_DEVICE_IMAGE && |
107 | devtype < FPT_NUM_VIRTUAL_DEVICE_TYPES); | ||
108 | |||
109 | 22 | fpt_setup_virtual_device_environment (devtype); | |
110 | |||
111 | 22 | tctx = fpt_context_new (); | |
112 | 22 | devices = fp_context_get_devices (tctx->fp_context); | |
113 | |||
114 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | g_assert_nonnull (devices); |
115 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | g_assert_cmpuint (devices->len, ==, 1); |
116 | |||
117 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | for (i = 0; i < devices->len; ++i) |
118 | { | ||
119 | 22 | FpDevice *device = devices->pdata[i]; | |
120 | |||
121 |
1/2✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
|
22 | if (g_strcmp0 (fp_device_get_driver (device), devtype_vars[devtype].driver_id) == 0) |
122 | { | ||
123 | 22 | tctx->device = device; | |
124 | 22 | break; | |
125 | } | ||
126 | } | ||
127 | |||
128 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
|
22 | g_assert_true (FP_IS_DEVICE (tctx->device)); |
129 | 22 | g_object_add_weak_pointer (G_OBJECT (tctx->device), (gpointer) & tctx->device); | |
130 | |||
131 | 22 | return tctx; | |
132 | } | ||
133 | |||
134 | void | ||
135 | 22 | fpt_context_free (FptContext *tctx) | |
136 | { | ||
137 |
4/4✓ Branch 0 taken 17 times.
✓ Branch 1 taken 5 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 3 times.
|
22 | if (tctx->device && fp_device_is_open (tctx->device)) |
138 | { | ||
139 | 14 | g_autoptr(GError) error = NULL; | |
140 | |||
141 | 14 | fp_device_close_sync (tctx->device, NULL, &error); | |
142 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
|
14 | g_assert_no_error (error); |
143 | } | ||
144 | |||
145 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | g_clear_object (&tctx->fp_context); |
146 | 22 | g_free (tctx); | |
147 | |||
148 | 22 | fpt_teardown_virtual_device_environment (); | |
149 | 22 | } | |
150 |