2 * Copyright © 2013 Red Hat, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 #include <sys/types.h>
32 #include <libevdev/libevdev-uinput.h>
33 #include "test-common.h"
35 START_TEST(test_new_device)
40 ck_assert(dev != NULL);
45 START_TEST(test_free_device)
51 START_TEST(test_init_from_invalid_fd)
54 struct libevdev *dev = NULL;
56 rc = libevdev_new_from_fd(-1, &dev);
58 ck_assert(dev == NULL);
59 ck_assert_int_eq(rc, -EBADF);
61 rc = libevdev_new_from_fd(STDIN_FILENO, &dev);
62 ck_assert(dev == NULL);
63 ck_assert_int_eq(rc, -ENOTTY);
67 START_TEST(test_init_and_change_fd)
69 struct uinput_device* uidev;
74 ck_assert(dev != NULL);
75 ck_assert_int_eq(libevdev_set_fd(dev, -1), -EBADF);
77 libevdev_set_log_function(test_logfunc_ignore_error, NULL);
78 ck_assert_int_eq(libevdev_change_fd(dev, -1), -1);
79 libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
81 rc = uinput_device_new_with_events(&uidev,
82 TEST_DEVICE_NAME, DEFAULT_IDS,
91 ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
92 ck_assert_int_eq(libevdev_set_fd(dev, uinput_device_get_fd(uidev)), 0);
94 libevdev_set_log_function(test_logfunc_ignore_error, NULL);
95 ck_assert_int_eq(libevdev_set_fd(dev, 0), -EBADF);
96 libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
98 ck_assert_int_eq(libevdev_get_fd(dev), uinput_device_get_fd(uidev));
100 ck_assert_int_eq(libevdev_change_fd(dev, 0), 0);
101 ck_assert_int_eq(libevdev_get_fd(dev), 0);
103 uinput_device_free(uidev);
108 static int log_fn_called = 0;
109 static char *logdata = "test";
110 static void logfunc(enum libevdev_log_priority priority,
112 const char *file, int line, const char *func,
113 const char *f, va_list args) {
114 ck_assert_int_eq(strcmp(logdata, data), 0);
118 START_TEST(test_log_init)
120 struct libevdev *dev = NULL;
121 enum libevdev_log_priority old;
123 old = libevdev_get_log_priority();
125 libevdev_set_log_priority(LIBEVDEV_LOG_DEBUG);
127 libevdev_set_log_function(logfunc, NULL);
128 libevdev_set_log_function(NULL, NULL);
130 dev = libevdev_new();
131 ck_assert(dev != NULL);
133 libevdev_set_log_function(logfunc, logdata);
134 libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
136 libevdev_set_log_function(NULL, NULL);
137 libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
139 libevdev_set_log_function(logfunc, logdata);
140 libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
142 /* libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL) should
143 trigger a log message. We called it three times, but only twice
144 with the logfunc set, thus, ensure we only called the logfunc
146 ck_assert_int_eq(log_fn_called, 2);
150 libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
154 libevdev_set_log_priority(old);
158 START_TEST(test_log_default_priority)
160 ck_assert_int_eq(libevdev_get_log_priority(), LIBEVDEV_LOG_INFO);
164 START_TEST(test_log_set_get_priority)
166 enum libevdev_log_priority pri;
167 enum libevdev_log_priority old;
169 old = libevdev_get_log_priority();
171 pri = LIBEVDEV_LOG_DEBUG;
172 libevdev_set_log_priority(pri);
173 ck_assert_int_eq(libevdev_get_log_priority(), pri);
175 pri = LIBEVDEV_LOG_INFO;
176 libevdev_set_log_priority(pri);
177 ck_assert_int_eq(libevdev_get_log_priority(), pri);
179 pri = LIBEVDEV_LOG_ERROR;
180 libevdev_set_log_priority(pri);
181 ck_assert_int_eq(libevdev_get_log_priority(), pri);
183 /* debug and above is clamped */
184 pri = LIBEVDEV_LOG_DEBUG + 1;
185 libevdev_set_log_priority(pri);
186 ck_assert_int_eq(libevdev_get_log_priority(), LIBEVDEV_LOG_DEBUG);
188 /* error and below is not clamped, we need this for another test */
189 pri = LIBEVDEV_LOG_ERROR - 1;
190 libevdev_set_log_priority(pri);
191 ck_assert_int_eq(libevdev_get_log_priority(), pri);
193 libevdev_set_log_priority(old);
197 START_TEST(test_log_priority)
199 struct libevdev *dev = NULL;
200 enum libevdev_log_priority old;
202 old = libevdev_get_log_priority();
204 libevdev_set_log_function(logfunc, logdata);
206 dev = libevdev_new();
207 ck_assert(dev != NULL);
209 libevdev_set_log_priority(LIBEVDEV_LOG_DEBUG);
210 libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
211 ck_assert_int_eq(log_fn_called, 1);
213 libevdev_set_log_priority(LIBEVDEV_LOG_INFO);
214 libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
215 ck_assert_int_eq(log_fn_called, 2);
217 libevdev_set_log_priority(LIBEVDEV_LOG_ERROR);
218 libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
219 ck_assert_int_eq(log_fn_called, 3);
221 /* we don't have any log msgs > ERROR at the moment, so test it by
222 setting an invalid priority. */
223 libevdev_set_log_priority(LIBEVDEV_LOG_ERROR - 1);
224 libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
225 ck_assert_int_eq(log_fn_called, 3);
229 libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
233 libevdev_set_log_priority(old);
237 static char *logdata_1 = "foo";
238 static char *logdata_2 = "bar";
239 static int log_data_fn_called = 0;
240 static void logfunc_data(enum libevdev_log_priority priority,
242 const char *file, int line, const char *func,
243 const char *f, va_list args) {
244 switch(log_data_fn_called) {
245 case 0: ck_assert(data == logdata_1); break;
246 case 1: ck_assert(data == logdata_2); break;
247 case 2: ck_assert(data == NULL); break;
251 log_data_fn_called++;
254 START_TEST(test_log_data)
256 struct libevdev *dev = NULL;
258 dev = libevdev_new();
259 ck_assert(dev != NULL);
261 libevdev_set_log_function(logfunc_data, logdata_1);
262 libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
264 libevdev_set_log_function(logfunc_data, logdata_2);
265 libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
267 libevdev_set_log_function(logfunc_data, NULL);
268 libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
274 START_TEST(test_device_init)
276 struct uinput_device* uidev;
277 struct libevdev *dev;
280 rc = uinput_device_new_with_events(&uidev,
281 TEST_DEVICE_NAME, DEFAULT_IDS,
290 ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
292 dev = libevdev_new();
293 ck_assert(dev != NULL);
294 rc = libevdev_set_fd(dev, uinput_device_get_fd(uidev));
295 ck_assert_msg(rc == 0, "Failed to init device: %s", strerror(-rc));;
297 uinput_device_free(uidev);
302 START_TEST(test_device_init_from_fd)
304 struct uinput_device* uidev;
305 struct libevdev *dev;
308 rc = uinput_device_new_with_events(&uidev,
309 TEST_DEVICE_NAME, DEFAULT_IDS,
318 ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
320 rc = libevdev_new_from_fd(uinput_device_get_fd(uidev), &dev);
321 ck_assert_msg(rc == 0, "Failed to init device: %s", strerror(-rc));;
323 uinput_device_free(uidev);
328 START_TEST(test_device_grab)
330 struct uinput_device* uidev;
331 struct libevdev *dev;
334 test_create_device(&uidev, &dev,
344 libevdev_set_log_function(test_logfunc_ignore_error, NULL);
345 rc = libevdev_grab(dev, 0);
346 ck_assert_int_eq(rc, -EINVAL);
347 rc = libevdev_grab(dev, 1);
348 ck_assert_int_eq(rc, -EINVAL);
349 libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
351 rc = libevdev_grab(dev, LIBEVDEV_UNGRAB);
352 ck_assert_int_eq(rc, 0);
353 rc = libevdev_grab(dev, LIBEVDEV_GRAB);
354 ck_assert_int_eq(rc, 0);
355 rc = libevdev_grab(dev, LIBEVDEV_GRAB);
356 ck_assert_int_eq(rc, 0);
357 rc = libevdev_grab(dev, LIBEVDEV_UNGRAB);
358 ck_assert_int_eq(rc, 0);
360 uinput_device_free(uidev);
365 START_TEST(test_set_clock_id)
367 struct uinput_device* uidev;
368 struct libevdev *dev;
371 test_create_device(&uidev, &dev,
381 rc = libevdev_set_clock_id(dev, CLOCK_REALTIME);
382 ck_assert_int_eq(rc, 0);
384 rc = libevdev_set_clock_id(dev, CLOCK_MONOTONIC);
385 ck_assert_int_eq(rc, 0);
387 rc = libevdev_set_clock_id(dev, CLOCK_MONOTONIC_RAW);
388 ck_assert_int_eq(rc, -EINVAL);
390 uinput_device_free(uidev);
395 START_TEST(test_clock_id_events)
397 struct uinput_device* uidev;
398 struct libevdev *dev, *dev2;
400 struct input_event ev1, ev2;
401 struct timespec t1_real, t2_real;
402 struct timespec t1_mono, t2_mono;
405 test_create_device(&uidev, &dev,
415 fd = open(uinput_device_get_devnode(uidev), O_RDONLY);
416 ck_assert_int_gt(fd, -1);
418 rc = libevdev_new_from_fd(fd, &dev2);
419 ck_assert_msg(rc == 0, "Failed to create second device: %s", strerror(-rc));
421 rc = libevdev_set_clock_id(dev2, CLOCK_MONOTONIC);
422 ck_assert_int_eq(rc, 0);
424 clock_gettime(CLOCK_REALTIME, &t1_real);
425 clock_gettime(CLOCK_MONOTONIC, &t1_mono);
426 uinput_device_event(uidev, EV_REL, REL_X, 1);
427 uinput_device_event(uidev, EV_SYN, SYN_REPORT, 0);
428 clock_gettime(CLOCK_REALTIME, &t2_real);
429 clock_gettime(CLOCK_MONOTONIC, &t2_mono);
431 rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev1);
432 ck_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS);
434 rc = libevdev_next_event(dev2, LIBEVDEV_READ_FLAG_NORMAL, &ev2);
435 ck_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS);
437 ck_assert_int_eq(ev1.type, ev2.type);
438 ck_assert_int_eq(ev1.code, ev2.code);
439 ck_assert_int_eq(ev1.value, ev2.value);
441 t1 = ev1.time.tv_sec * 1000000LL + ev1.time.tv_usec;
442 t2 = ev2.time.tv_sec * 1000000LL + ev2.time.tv_usec;
443 ck_assert_int_ne(t1, t2);
445 ck_assert_int_ge(ev1.time.tv_sec, t1_real.tv_sec);
446 ck_assert_int_ge(ev1.time.tv_usec, t1_real.tv_nsec/1000);
447 ck_assert_int_le(ev1.time.tv_sec, t2_real.tv_sec);
448 ck_assert_int_le(ev1.time.tv_usec, t2_real.tv_nsec/1000);
450 ck_assert_int_ge(ev2.time.tv_sec, t1_mono.tv_sec);
451 ck_assert_int_ge(ev2.time.tv_usec, t1_mono.tv_nsec/1000);
452 ck_assert_int_le(ev2.time.tv_sec, t2_mono.tv_sec);
453 ck_assert_int_le(ev2.time.tv_usec, t2_mono.tv_nsec/1000);
455 uinput_device_free(uidev);
463 libevdev_init_test(void)
465 Suite *s = suite_create("libevdev init tests");
467 TCase *tc = tcase_create("device init");
468 tcase_add_test(tc, test_new_device);
469 tcase_add_test(tc, test_free_device);
470 tcase_add_test(tc, test_init_from_invalid_fd);
471 tcase_add_test(tc, test_init_and_change_fd);
472 suite_add_tcase(s, tc);
474 tc = tcase_create("log init");
475 tcase_add_test(tc, test_log_init);
476 tcase_add_test(tc, test_log_priority);
477 tcase_add_test(tc, test_log_set_get_priority);
478 tcase_add_test(tc, test_log_default_priority);
479 tcase_add_test(tc, test_log_data);
480 suite_add_tcase(s, tc);
482 tc = tcase_create("device fd init");
483 tcase_add_test(tc, test_device_init);
484 tcase_add_test(tc, test_device_init_from_fd);
485 suite_add_tcase(s, tc);
487 tc = tcase_create("device grab");
488 tcase_add_test(tc, test_device_grab);
489 suite_add_tcase(s, tc);
491 tc = tcase_create("clock id");
492 tcase_add_test(tc, test_set_clock_id);
493 tcase_add_test(tc, test_clock_id_events);
494 suite_add_tcase(s, tc);