Debian Codegraph @

test/test-dbverify.c

raw
1 /*
2 * Copyright © 2012 Red Hat, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software
5 * and its documentation for any purpose is hereby granted without
6 * fee, provided that the above copyright notice appear in all copies
7 * and that both that copyright notice and this permission notice
8 * appear in supporting documentation, and that the name of Red Hat
9 * not be used in advertising or publicity pertaining to distribution
10 * of the software without specific, written prior permission. Red
11 * Hat makes no representations about the suitability of this software
12 * for any purpose. It is provided "as is" without express or implied
13 * warranty.
14 *
15 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
17 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
19 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 *
23 * Authors:
24 * Peter Hutterer ([email protected])
25 */
26
27 #include "config.h"
28
29 #define _GNU_SOURCE
30 #include <dirent.h>
31 #include <fcntl.h>
32 #include <glib.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39
40 #include "libwacom.h"
41
42 static WacomDeviceDatabase *db_old;
43 static WacomDeviceDatabase *db_new;
44
45 static void
46 rmtmpdir(const char *tmpdir)
47 {
48 DIR *dir;
49 struct dirent *file;
50
51 dir = opendir(tmpdir);
52 if (!dir)
53 return;
54
55 while ((file = readdir(dir))) {
56 g_autofree char *path = NULL;
57
58 if (file->d_name[0] == '.')
59 continue;
60
61 g_assert(asprintf(&path, "%s/%s", tmpdir, file->d_name) != -1);
62 g_assert(path);
63 g_assert(remove(path) != -1);
64 }
65
66 closedir(dir);
67
68 g_assert(remove(tmpdir) != -1);
69 }
70
71 static void
72 find_matching(gconstpointer data)
73 {
74 g_autofree WacomDevice **devs_old, **devs_new;
75 WacomDevice **devices, **d;
76 WacomDevice *other;
77 gboolean found = FALSE;
78 int index = GPOINTER_TO_INT(data);
79
80 devs_old = libwacom_list_devices_from_database(db_old, NULL);
81 devs_new = libwacom_list_devices_from_database(db_new, NULL);
82
83 /* Make sure each device in old has a device in new */
84 devices = devs_old;
85 other = devs_new[index];
86 for (d = devices; *d; d++) {
87 /* Two different contexts, they cannot be the same pointer */
88 g_assert(other != *d);
89 if (libwacom_compare(other, *d, WCOMPARE_MATCHES) == 0) {
90 found = TRUE;
91 break;
92 }
93 }
94 g_assert_true(found);
95
96 /* Make sure each device in new has a device in old */
97 devices = devs_new;
98 other = devs_old[index];
99 found = FALSE;
100 for (d = devices; *d; d++) {
101 /* devices with multiple matches will have multiple
102 * devices in the list */
103 if (libwacom_compare(other, *d, WCOMPARE_MATCHES) == 0) {
104 found = TRUE;
105 break;
106 }
107 }
108 g_assert_true(found);
109 }
110
111 static void
112 test_database_size(void)
113 {
114 int sz1, sz2;
115 g_autofree WacomDevice **d1, **d2;
116
117 d1 = libwacom_list_devices_from_database(db_old, NULL);
118 d2 = libwacom_list_devices_from_database(db_new, NULL);
119 g_assert_nonnull(d1);
120 g_assert_nonnull(d2);
121
122 sz1 = 0;
123 for (WacomDevice **d = d1; *d; d++)
124 sz1++;
125 sz2 = 0;
126 for (WacomDevice **d = d2; *d; d++)
127 sz2++;
128 g_assert_cmpint(sz1, ==, sz2);
129 }
130
131 static int
132 compare_databases(WacomDeviceDatabase *new)
133 {
134 int i, rc;
135 g_autofree WacomDevice **devs_new;
136 WacomDevice **n;
137
138 g_test_add_func("/dbverify/database-sizes", test_database_size);
139
140 devs_new = libwacom_list_devices_from_database(new, NULL);
141
142 for (n = devs_new, i = 0; *n; n++, i++) {
143 char buf[1024];
144
145 /* We need to add the test index to avoid duplicate
146 test names */
147 snprintf(buf,
148 sizeof(buf),
149 "/dbverify/%03d/%04x:%04x-%s",
150 i,
151 libwacom_get_vendor_id(*n),
152 libwacom_get_product_id(*n),
153 libwacom_get_name(*n));
154 g_test_add_data_func(buf, GINT_TO_POINTER(i), find_matching);
155 }
156
157 rc = g_test_run();
158 return rc;
159 }
160
161 /* write out the current db, read it back in, compare */
162 static void
163 duplicate_database(WacomDeviceDatabase *db,
164 const char *dirname)
165 {
166 WacomDevice **device, **devices;
167 int i;
168
169 devices = libwacom_list_devices_from_database(db, NULL);
170 g_assert(devices);
171 g_assert(*devices);
172
173 for (device = devices, i = 0; *device; device++, i++) {
174 int i;
175 int fd;
176 g_autofree char *path = NULL;
177 int nstyli;
178 g_autofree const WacomStylus **styli = NULL;
179
180 g_assert(asprintf(&path,
181 "%s/%s.tablet",
182 dirname,
183 libwacom_get_match(*device)) != -1);
184 g_assert(path);
185 fd = open(path, O_WRONLY | O_CREAT, S_IRWXU);
186 g_assert(fd >= 0);
187 libwacom_print_device_description(fd, *device);
188 close(fd);
189
190 if (!libwacom_has_stylus(*device))
191 continue;
192
193 styli = libwacom_get_styli(*device, &nstyli);
194 for (i = 0; i < nstyli; i++) {
195 int fd_stylus;
196 const WacomStylus *stylus = styli[i];
197 g_autofree char *path =
198 g_strdup_printf("%s/%#x.stylus",
199 dirname,
200 libwacom_stylus_get_id(stylus));
201 fd_stylus = open(path, O_WRONLY | O_CREAT, S_IRWXU);
202 g_assert(fd_stylus >= 0);
203 libwacom_print_stylus_description(fd_stylus, stylus);
204 close(fd_stylus);
205 }
206 }
207
208 free(devices);
209 }
210
211 static WacomDeviceDatabase *
212 load_database(void)
213 {
214 WacomDeviceDatabase *db;
215 const char *datadir;
216
217 datadir = getenv("LIBWACOM_DATA_DIR");
218 if (!datadir)
219 datadir = TOPSRCDIR "/data";
220
221 db = libwacom_database_new_for_path(datadir);
222 if (!db)
223 printf("Failed to load data from %s", datadir);
224
225 g_assert(db);
226 return db;
227 }
228
229 int
230 main(int argc,
231 char **argv)
232 {
233 WacomDeviceDatabase *db;
234 g_autofree char *dirname;
235 int rc;
236
237 g_test_init(&argc, &argv, NULL);
238 g_test_set_nonfatal_assertions();
239
240 db = load_database();
241
242 dirname = g_dir_make_tmp("tmp.dbverify.XXXXXX", NULL);
243 g_assert(dirname);
244
245 duplicate_database(db, dirname);
246 db_new = libwacom_database_new_for_path(dirname);
247 g_assert(db_new);
248
249 db_old = db;
250 rc = compare_databases(db_new);
251 libwacom_database_destroy(db_new);
252 libwacom_database_destroy(db_old);
253
254 rmtmpdir(dirname);
255
256 return rc;
257 }
258
259 /* vim: set noexpandtab tabstop=8 shiftwidth=8: */