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