Drop semicolons after getter/setter macros
[platform/upstream/libevdev.git] / test / test-libevdev-init.c
1 /*
2  * Copyright © 2013 Red Hat, Inc.
3  *
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.
13  *
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
20  * OF THIS SOFTWARE.
21  */
22
23 #include <config.h>
24 #include <errno.h>
25 #include <inttypes.h>
26 #include <unistd.h>
27 #include <time.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31
32 #include <libevdev/libevdev-uinput.h>
33 #include "test-common.h"
34
35 START_TEST(test_new_device)
36 {
37         struct libevdev *dev;
38
39         dev = libevdev_new();
40         ck_assert(dev != NULL);
41         libevdev_free(dev);
42 }
43 END_TEST
44
45 START_TEST(test_free_device)
46 {
47         libevdev_free(NULL);
48 }
49 END_TEST
50
51 START_TEST(test_init_from_invalid_fd)
52 {
53         int rc;
54         struct libevdev *dev = NULL;
55
56         rc = libevdev_new_from_fd(-1, &dev);
57
58         ck_assert(dev == NULL);
59         ck_assert_int_eq(rc, -EBADF);
60
61         rc = libevdev_new_from_fd(STDIN_FILENO, &dev);
62         ck_assert(dev == NULL);
63         ck_assert_int_eq(rc, -ENOTTY);
64 }
65 END_TEST
66
67 START_TEST(test_init_and_change_fd)
68 {
69         struct uinput_device* uidev;
70         struct libevdev *dev;
71         int rc;
72
73         dev = libevdev_new();
74         ck_assert(dev != NULL);
75         ck_assert_int_eq(libevdev_set_fd(dev, -1), -EBADF);
76         ck_assert_int_eq(libevdev_change_fd(dev, -1), -1);
77
78         rc = uinput_device_new_with_events(&uidev,
79                                            TEST_DEVICE_NAME, DEFAULT_IDS,
80                                            EV_SYN, SYN_REPORT,
81                                            EV_REL, REL_X,
82                                            EV_REL, REL_Y,
83                                            EV_REL, REL_WHEEL,
84                                            EV_KEY, BTN_LEFT,
85                                            EV_KEY, BTN_MIDDLE,
86                                            EV_KEY, BTN_RIGHT,
87                                            -1);
88         ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
89         ck_assert_int_eq(libevdev_set_fd(dev, uinput_device_get_fd(uidev)), 0);
90         ck_assert_int_eq(libevdev_set_fd(dev, 0), -EBADF);
91
92         ck_assert_int_eq(libevdev_get_fd(dev), uinput_device_get_fd(uidev));
93
94         ck_assert_int_eq(libevdev_change_fd(dev, 0), 0);
95         ck_assert_int_eq(libevdev_get_fd(dev), 0);
96
97         uinput_device_free(uidev);
98         libevdev_free(dev);
99 }
100 END_TEST
101
102
103 static int log_fn_called = 0;
104 static char *logdata = "test";
105 static void logfunc(enum libevdev_log_priority priority,
106                     void *data,
107                     const char *file, int line, const char *func,
108                     const char *f, va_list args) {
109         ck_assert_int_eq(strcmp(logdata, data), 0);
110         log_fn_called++;
111 }
112
113 START_TEST(test_log_init)
114 {
115         struct libevdev *dev = NULL;
116
117         libevdev_set_log_function(logfunc, NULL);
118         libevdev_set_log_function(NULL, NULL);
119
120         dev = libevdev_new();
121         ck_assert(dev != NULL);
122
123         libevdev_set_log_function(logfunc, logdata);
124         libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
125
126         libevdev_set_log_function(NULL, NULL);
127         libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
128
129         libevdev_set_log_function(logfunc, logdata);
130         libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
131
132         /* libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL) should
133            trigger a log message. We called it three times, but only twice
134            with the logfunc set, thus, ensure we only called the logfunc
135            twice */
136         ck_assert_int_eq(log_fn_called, 2);
137
138         libevdev_free(dev);
139 }
140 END_TEST
141
142 static char *logdata_1 = "foo";
143 static char *logdata_2 = "bar";
144 static int log_data_fn_called = 0;
145 static void logfunc_data(enum libevdev_log_priority priority,
146                          void *data,
147                          const char *file, int line, const char *func,
148                          const char *f, va_list args) {
149         switch(log_data_fn_called) {
150                 case 0: ck_assert(data == logdata_1); break;
151                 case 1: ck_assert(data == logdata_2); break;
152                 case 2: ck_assert(data == NULL); break;
153                 default:
154                         ck_abort();
155         }
156         log_data_fn_called++;
157 }
158
159 START_TEST(test_log_data)
160 {
161         struct libevdev *dev = NULL;
162
163         dev = libevdev_new();
164         ck_assert(dev != NULL);
165
166         libevdev_set_log_function(logfunc_data, logdata_1);
167         libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
168
169         libevdev_set_log_function(logfunc_data, logdata_2);
170         libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
171
172         libevdev_set_log_function(logfunc_data, NULL);
173         libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
174
175         libevdev_free(dev);
176 }
177 END_TEST
178
179 START_TEST(test_device_init)
180 {
181         struct uinput_device* uidev;
182         struct libevdev *dev;
183         int rc;
184
185         rc = uinput_device_new_with_events(&uidev,
186                                            TEST_DEVICE_NAME, DEFAULT_IDS,
187                                            EV_SYN, SYN_REPORT,
188                                            EV_REL, REL_X,
189                                            EV_REL, REL_Y,
190                                            EV_REL, REL_WHEEL,
191                                            EV_KEY, BTN_LEFT,
192                                            EV_KEY, BTN_MIDDLE,
193                                            EV_KEY, BTN_RIGHT,
194                                            -1);
195         ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
196
197         dev = libevdev_new();
198         ck_assert(dev != NULL);
199         rc = libevdev_set_fd(dev, uinput_device_get_fd(uidev));
200         ck_assert_msg(rc == 0, "Failed to init device: %s", strerror(-rc));;
201
202         uinput_device_free(uidev);
203         libevdev_free(dev);
204 }
205 END_TEST
206
207 START_TEST(test_device_init_from_fd)
208 {
209         struct uinput_device* uidev;
210         struct libevdev *dev;
211         int rc;
212
213         rc = uinput_device_new_with_events(&uidev,
214                                            TEST_DEVICE_NAME, DEFAULT_IDS,
215                                            EV_SYN, SYN_REPORT,
216                                            EV_REL, REL_X,
217                                            EV_REL, REL_Y,
218                                            EV_REL, REL_WHEEL,
219                                            EV_KEY, BTN_LEFT,
220                                            EV_KEY, BTN_MIDDLE,
221                                            EV_KEY, BTN_RIGHT,
222                                            -1);
223         ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
224
225         rc = libevdev_new_from_fd(uinput_device_get_fd(uidev), &dev);
226         ck_assert_msg(rc == 0, "Failed to init device: %s", strerror(-rc));;
227
228         uinput_device_free(uidev);
229         libevdev_free(dev);
230 }
231 END_TEST
232
233 START_TEST(test_device_grab)
234 {
235         struct uinput_device* uidev;
236         struct libevdev *dev;
237         int rc;
238
239         rc = test_create_device(&uidev, &dev,
240                                 EV_SYN, SYN_REPORT,
241                                 EV_REL, REL_X,
242                                 EV_REL, REL_Y,
243                                 EV_REL, REL_WHEEL,
244                                 EV_KEY, BTN_LEFT,
245                                 EV_KEY, BTN_MIDDLE,
246                                 EV_KEY, BTN_RIGHT,
247                                 -1);
248         ck_assert_msg(rc == 0, "Failed to create device: %s", strerror(-rc));
249
250         rc = libevdev_grab(dev, 0);
251         ck_assert_int_eq(rc, -EINVAL);
252         rc = libevdev_grab(dev, 1);
253         ck_assert_int_eq(rc, -EINVAL);
254
255         rc = libevdev_grab(dev, LIBEVDEV_UNGRAB);
256         ck_assert_int_eq(rc, 0);
257         rc = libevdev_grab(dev, LIBEVDEV_GRAB);
258         ck_assert_int_eq(rc, 0);
259         rc = libevdev_grab(dev, LIBEVDEV_GRAB);
260         ck_assert_int_eq(rc, 0);
261         rc = libevdev_grab(dev, LIBEVDEV_UNGRAB);
262         ck_assert_int_eq(rc, 0);
263
264         uinput_device_free(uidev);
265         libevdev_free(dev);
266 }
267 END_TEST
268
269 START_TEST(test_set_clock_id)
270 {
271         struct uinput_device* uidev;
272         struct libevdev *dev;
273         int rc;
274
275         rc = test_create_device(&uidev, &dev,
276                                 EV_SYN, SYN_REPORT,
277                                 EV_REL, REL_X,
278                                 EV_REL, REL_Y,
279                                 EV_REL, REL_WHEEL,
280                                 EV_KEY, BTN_LEFT,
281                                 EV_KEY, BTN_MIDDLE,
282                                 EV_KEY, BTN_RIGHT,
283                                 -1);
284         ck_assert_msg(rc == 0, "Failed to create device: %s", strerror(-rc));
285
286         rc = libevdev_set_clock_id(dev, CLOCK_REALTIME);
287         ck_assert_int_eq(rc, 0);
288
289         rc = libevdev_set_clock_id(dev, CLOCK_MONOTONIC);
290         ck_assert_int_eq(rc, 0);
291
292         rc = libevdev_set_clock_id(dev, CLOCK_MONOTONIC_RAW);
293         ck_assert_int_eq(rc, -EINVAL);
294
295         uinput_device_free(uidev);
296         libevdev_free(dev);
297 }
298 END_TEST
299
300 START_TEST(test_clock_id_events)
301 {
302         struct uinput_device* uidev;
303         struct libevdev *dev, *dev2;
304         int rc, fd;
305         struct input_event ev1, ev2;
306         struct timespec t1_real, t2_real;
307         struct timespec t1_mono, t2_mono;
308         int64_t t1, t2;
309
310         rc = test_create_device(&uidev, &dev,
311                                 EV_SYN, SYN_REPORT,
312                                 EV_REL, REL_X,
313                                 EV_REL, REL_Y,
314                                 EV_REL, REL_WHEEL,
315                                 EV_KEY, BTN_LEFT,
316                                 EV_KEY, BTN_MIDDLE,
317                                 EV_KEY, BTN_RIGHT,
318                                 -1);
319         ck_assert_msg(rc == 0, "Failed to create device: %s", strerror(-rc));
320
321         fd = open(uinput_device_get_devnode(uidev), O_RDONLY);
322         ck_assert_int_gt(fd, -1);
323
324         rc = libevdev_new_from_fd(fd, &dev2);
325         ck_assert_msg(rc == 0, "Failed to create second device: %s", strerror(-rc));
326
327         rc = libevdev_set_clock_id(dev2, CLOCK_MONOTONIC);
328         ck_assert_int_eq(rc, 0);
329
330         clock_gettime(CLOCK_REALTIME, &t1_real);
331         clock_gettime(CLOCK_MONOTONIC, &t1_mono);
332         uinput_device_event(uidev, EV_REL, REL_X, 1);
333         uinput_device_event(uidev, EV_SYN, SYN_REPORT, 0);
334         clock_gettime(CLOCK_REALTIME, &t2_real);
335         clock_gettime(CLOCK_MONOTONIC, &t2_mono);
336
337         rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev1);
338         ck_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS);
339
340         rc = libevdev_next_event(dev2, LIBEVDEV_READ_FLAG_NORMAL, &ev2);
341         ck_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS);
342
343         ck_assert_int_eq(ev1.type, ev2.type);
344         ck_assert_int_eq(ev1.code, ev2.code);
345         ck_assert_int_eq(ev1.value, ev2.value);
346
347         t1 = ev1.time.tv_sec * 1000000LL + ev1.time.tv_usec;
348         t2 = ev2.time.tv_sec * 1000000LL + ev2.time.tv_usec;
349         ck_assert_int_ne(t1, t2);
350
351         ck_assert_int_ge(ev1.time.tv_sec, t1_real.tv_sec);
352         ck_assert_int_ge(ev1.time.tv_usec, t1_real.tv_nsec/1000);
353         ck_assert_int_le(ev1.time.tv_sec, t2_real.tv_sec);
354         ck_assert_int_le(ev1.time.tv_usec, t2_real.tv_nsec/1000);
355
356         ck_assert_int_ge(ev2.time.tv_sec, t1_mono.tv_sec);
357         ck_assert_int_ge(ev2.time.tv_usec, t1_mono.tv_nsec/1000);
358         ck_assert_int_le(ev2.time.tv_sec, t2_mono.tv_sec);
359         ck_assert_int_le(ev2.time.tv_usec, t2_mono.tv_nsec/1000);
360
361         uinput_device_free(uidev);
362         libevdev_free(dev);
363         libevdev_free(dev2);
364         close(fd);
365 }
366 END_TEST
367
368
369 Suite *
370 libevdev_init_test(void)
371 {
372         Suite *s = suite_create("libevdev init tests");
373
374         TCase *tc = tcase_create("device init");
375         tcase_add_test(tc, test_new_device);
376         tcase_add_test(tc, test_free_device);
377         tcase_add_test(tc, test_init_from_invalid_fd);
378         tcase_add_test(tc, test_init_and_change_fd);
379         suite_add_tcase(s, tc);
380
381         tc = tcase_create("log init");
382         tcase_add_test(tc, test_log_init);
383         tcase_add_test(tc, test_log_data);
384         suite_add_tcase(s, tc);
385
386         tc = tcase_create("device fd init");
387         tcase_add_test(tc, test_device_init);
388         tcase_add_test(tc, test_device_init_from_fd);
389         suite_add_tcase(s, tc);
390
391         tc = tcase_create("device grab");
392         tcase_add_test(tc, test_device_grab);
393         suite_add_tcase(s, tc);
394
395         tc = tcase_create("clock id");
396         tcase_add_test(tc, test_set_clock_id);
397         tcase_add_test(tc, test_clock_id_events);
398         suite_add_tcase(s, tc);
399
400         return s;
401 }