Add our own version of linux/input.h
[platform/upstream/libinput.git] / test / litest.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 #if HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <check.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <getopt.h>
31 #include <poll.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <time.h>
36 #include <unistd.h>
37 #include "linux/input.h"
38 #include <sys/ptrace.h>
39 #include <sys/timerfd.h>
40 #include <sys/wait.h>
41
42 #include "litest.h"
43 #include "litest-int.h"
44 #include "libinput-util.h"
45
46 static int in_debugger = -1;
47
48 struct test {
49         struct list node;
50         char *name;
51         TCase *tc;
52         enum litest_device_type devices;
53 };
54
55 struct suite {
56         struct list node;
57         struct list tests;
58         char *name;
59         Suite *suite;
60 };
61
62 static struct litest_device *current_device;
63
64 struct litest_device *litest_current_device(void) {
65         return current_device;
66 }
67
68 void litest_set_current_device(struct litest_device *device) {
69         current_device = device;
70 }
71
72 void litest_generic_device_teardown(void)
73 {
74         litest_delete_device(current_device);
75         current_device = NULL;
76 }
77
78 extern struct litest_test_device litest_keyboard_device;
79 extern struct litest_test_device litest_synaptics_clickpad_device;
80 extern struct litest_test_device litest_synaptics_touchpad_device;
81 extern struct litest_test_device litest_trackpoint_device;
82 extern struct litest_test_device litest_bcm5974_device;
83 extern struct litest_test_device litest_mouse_device;
84 extern struct litest_test_device litest_wacom_touch_device;
85
86 struct litest_test_device* devices[] = {
87         &litest_synaptics_clickpad_device,
88         &litest_synaptics_touchpad_device,
89         &litest_keyboard_device,
90         &litest_trackpoint_device,
91         &litest_bcm5974_device,
92         &litest_mouse_device,
93         &litest_wacom_touch_device,
94         NULL,
95 };
96
97
98 static struct list all_tests;
99
100 static void
101 litest_add_tcase_for_device(struct suite *suite,
102                             void *func,
103                             const struct litest_test_device *dev)
104 {
105         struct test *t;
106         const char *test_name = dev->shortname;
107
108         list_for_each(t, &suite->tests, node) {
109                 if (strcmp(t->name, test_name) != 0)
110                         continue;
111
112                 tcase_add_test(t->tc, func);
113                 return;
114         }
115
116         t = zalloc(sizeof(*t));
117         t->name = strdup(test_name);
118         t->tc = tcase_create(test_name);
119         list_insert(&suite->tests, &t->node);
120         tcase_add_checked_fixture(t->tc, dev->setup,
121                                   dev->teardown ? dev->teardown : litest_generic_device_teardown);
122         tcase_add_test(t->tc, func);
123         suite_add_tcase(suite->suite, t->tc);
124 }
125
126 static void
127 litest_add_tcase_no_device(struct suite *suite, void *func)
128 {
129         struct test *t;
130         const char *test_name = "no device";
131
132         list_for_each(t, &suite->tests, node) {
133                 if (strcmp(t->name, test_name) != 0)
134                         continue;
135
136                 tcase_add_test(t->tc, func);
137                 return;
138         }
139
140         t = zalloc(sizeof(*t));
141         t->name = strdup(test_name);
142         t->tc = tcase_create(test_name);
143         list_insert(&suite->tests, &t->node);
144         tcase_add_test(t->tc, func);
145         suite_add_tcase(suite->suite, t->tc);
146 }
147
148 static void
149 litest_add_tcase(struct suite *suite, void *func,
150                  enum litest_device_feature required,
151                  enum litest_device_feature excluded)
152 {
153         struct litest_test_device **dev = devices;
154
155         if (required == LITEST_DISABLE_DEVICE &&
156             excluded == LITEST_DISABLE_DEVICE) {
157                 litest_add_tcase_no_device(suite, func);
158         } else if (required != LITEST_ANY || excluded != LITEST_ANY) {
159                 while (*dev) {
160                         if (((*dev)->features & required) == required &&
161                             ((*dev)->features & excluded) == 0)
162                                 litest_add_tcase_for_device(suite, func, *dev);
163                         dev++;
164                 }
165         } else {
166                 while (*dev) {
167                         litest_add_tcase_for_device(suite, func, *dev);
168                         dev++;
169                 }
170         }
171 }
172
173 void
174 litest_add_no_device(const char *name, void *func)
175 {
176         litest_add(name, func, LITEST_DISABLE_DEVICE, LITEST_DISABLE_DEVICE);
177 }
178
179 void
180 litest_add(const char *name,
181            void *func,
182            enum litest_device_feature required,
183            enum litest_device_feature excluded)
184 {
185         struct suite *s;
186
187         if (all_tests.next == NULL && all_tests.prev == NULL)
188                 list_init(&all_tests);
189
190         list_for_each(s, &all_tests, node) {
191                 if (strcmp(s->name, name) == 0) {
192                         litest_add_tcase(s, func, required, excluded);
193                         return;
194                 }
195         }
196
197         s = zalloc(sizeof(*s));
198         s->name = strdup(name);
199         s->suite = suite_create(s->name);
200
201         list_init(&s->tests);
202         list_insert(&all_tests, &s->node);
203         litest_add_tcase(s, func, required, excluded);
204 }
205
206 int is_debugger_attached()
207 {
208         int status;
209         int rc;
210         int pid = fork();
211
212         if (pid == -1)
213                 return 0;
214
215         if (pid == 0) {
216                 int ppid = getppid();
217                 if (ptrace(PTRACE_ATTACH, ppid, NULL, NULL) == 0) {
218                         waitpid(ppid, NULL, 0);
219                         ptrace(PTRACE_CONT, NULL, NULL);
220                         ptrace(PTRACE_DETACH, ppid, NULL, NULL);
221                         rc = 0;
222                 } else {
223                         rc = 1;
224                 }
225                 _exit(rc);
226         } else {
227                 waitpid(pid, &status, 0);
228                 rc = WEXITSTATUS(status);
229         }
230
231         return rc;
232 }
233
234
235 static void
236 litest_list_tests(struct list *tests)
237 {
238         struct suite *s;
239
240         list_for_each(s, tests, node) {
241                 struct test *t;
242                 printf("%s:\n", s->name);
243                 list_for_each(t, &s->tests, node) {
244                         printf("        %s\n", t->name);
245                 }
246         }
247 }
248
249 static const struct option opts[] = {
250         { "list", 0, 0, 'l' },
251         { 0, 0, 0, 0}
252 };
253
254 int
255 litest_run(int argc, char **argv) {
256         struct suite *s, *snext;
257         int failed;
258         SRunner *sr = NULL;
259
260         if (in_debugger == -1) {
261                 in_debugger = is_debugger_attached();
262                 if (in_debugger)
263                         setenv("CK_FORK", "no", 0);
264         }
265
266         list_for_each(s, &all_tests, node) {
267                 if (!sr)
268                         sr = srunner_create(s->suite);
269                 else
270                         srunner_add_suite(sr, s->suite);
271         }
272
273         while(1) {
274                 int c;
275                 int option_index = 0;
276
277                 c = getopt_long(argc, argv, "", opts, &option_index);
278                 if (c == -1)
279                         break;
280                 switch(c) {
281                         case 'l':
282                                 litest_list_tests(&all_tests);
283                                 return 0;
284                         default:
285                                 fprintf(stderr, "usage: %s [--list]\n", argv[0]);
286                                 return 1;
287
288                 }
289         }
290
291         srunner_run_all(sr, CK_NORMAL);
292         failed = srunner_ntests_failed(sr);
293         srunner_free(sr);
294
295         list_for_each_safe(s, snext, &all_tests, node) {
296                 struct test *t, *tnext;
297
298                 list_for_each_safe(t, tnext, &s->tests, node) {
299                         free(t->name);
300                         list_remove(&t->node);
301                         free(t);
302                 }
303
304                 list_remove(&s->node);
305                 free(s->name);
306                 free(s);
307         }
308
309         return failed;
310 }
311
312 static int
313 open_restricted(const char *path, int flags, void *userdata)
314 {
315         return open(path, flags);
316 }
317
318 static void
319 close_restricted(int fd, void *userdata)
320 {
321         close(fd);
322 }
323
324 const struct libinput_interface interface = {
325         .open_restricted = open_restricted,
326         .close_restricted = close_restricted,
327 };
328
329
330 static struct input_absinfo *
331 merge_absinfo(const struct input_absinfo *orig,
332               const struct input_absinfo *override)
333 {
334         struct input_absinfo *abs;
335         int nelem, i;
336         size_t sz = ABS_MAX + 1;
337
338         if (!orig)
339                 return NULL;
340
341         abs = calloc(sz, sizeof(*abs));
342         ck_assert(abs != NULL);
343
344         nelem = 0;
345         while (orig[nelem].value != -1) {
346                 abs[nelem] = orig[nelem];
347                 nelem++;
348                 ck_assert_int_lt(nelem, sz);
349         }
350
351         /* just append, if the same axis is present twice, libevdev will
352            only use the last value anyway */
353         i = 0;
354         while (override && override[i].value != -1) {
355                 abs[nelem++] = override[i++];
356                 ck_assert_int_lt(nelem, sz);
357         }
358
359         ck_assert_int_lt(nelem, sz);
360         abs[nelem].value = -1;
361
362         return abs;
363 }
364
365 static int*
366 merge_events(const int *orig, const int *override)
367 {
368         int *events;
369         int nelem, i;
370         size_t sz = KEY_MAX * 3;
371
372         if (!orig)
373                 return NULL;
374
375         events = calloc(sz, sizeof(int));
376         ck_assert(events != NULL);
377
378         nelem = 0;
379         while (orig[nelem] != -1) {
380                 events[nelem] = orig[nelem];
381                 nelem++;
382                 ck_assert_int_lt(nelem, sz);
383         }
384
385         /* just append, if the same axis is present twice, libevdev will
386          * ignore the double definition anyway */
387         i = 0;
388         while (override && override[i] != -1) {
389                 events[nelem++] = override[i++];
390                 ck_assert_int_le(nelem, sz);
391         }
392
393         ck_assert_int_lt(nelem, sz);
394         events[nelem] = -1;
395
396         return events;
397 }
398
399
400 static struct litest_device *
401 litest_create(enum litest_device_type which,
402               const char *name_override,
403               struct input_id *id_override,
404               const struct input_absinfo *abs_override,
405               const int *events_override)
406 {
407         struct litest_device *d = NULL;
408         struct litest_test_device **dev;
409         const char *name;
410         const struct input_id *id;
411         struct input_absinfo *abs;
412         int *events;
413
414         dev = devices;
415         while (*dev) {
416                 if ((*dev)->type == which)
417                         break;
418                 dev++;
419         }
420
421         if (!*dev)
422                 ck_abort_msg("Invalid device type %d\n", which);
423
424         d = zalloc(sizeof(*d));
425         ck_assert(d != NULL);
426
427         /* device has custom create method */
428         if ((*dev)->create) {
429                 (*dev)->create(d);
430                 if (abs_override || events_override)
431                         ck_abort_msg("Custom create cannot"
432                                      "be overridden");
433
434                 return d;
435         }
436
437         abs = merge_absinfo((*dev)->absinfo, abs_override);
438         events = merge_events((*dev)->events, events_override);
439         name = name_override ? name_override : (*dev)->name;
440         id = id_override ? id_override : (*dev)->id;
441
442         d->uinput = litest_create_uinput_device_from_description(name,
443                                                                  id,
444                                                                  abs,
445                                                                  events);
446         d->interface = (*dev)->interface;
447         free(abs);
448         free(events);
449
450         return d;
451
452 }
453
454 struct libinput *
455 litest_create_context(void)
456 {
457         struct libinput *libinput =
458                 libinput_path_create_context(&interface, NULL);
459         ck_assert_notnull(libinput);
460         return libinput;
461 }
462
463 struct litest_device *
464 litest_add_device_with_overrides(struct libinput *libinput,
465                                  enum litest_device_type which,
466                                  const char *name_override,
467                                  struct input_id *id_override,
468                                  const struct input_absinfo *abs_override,
469                                  const int *events_override)
470 {
471         struct litest_device *d;
472         int fd;
473         int rc;
474         const char *path;
475
476         d = litest_create(which,
477                           name_override,
478                           id_override,
479                           abs_override,
480                           events_override);
481
482         path = libevdev_uinput_get_devnode(d->uinput);
483         ck_assert(path != NULL);
484         fd = open(path, O_RDWR|O_NONBLOCK);
485         ck_assert_int_ne(fd, -1);
486
487         rc = libevdev_new_from_fd(fd, &d->evdev);
488         ck_assert_int_eq(rc, 0);
489
490         d->libinput = libinput;
491         d->libinput_device = libinput_path_add_device(d->libinput, path);
492         ck_assert(d->libinput_device != NULL);
493         libinput_device_ref(d->libinput_device);
494
495         if (d->interface) {
496                 d->interface->min[ABS_X] = libevdev_get_abs_minimum(d->evdev, ABS_X);
497                 d->interface->max[ABS_X] = libevdev_get_abs_maximum(d->evdev, ABS_X);
498                 d->interface->min[ABS_Y] = libevdev_get_abs_minimum(d->evdev, ABS_Y);
499                 d->interface->max[ABS_Y] = libevdev_get_abs_maximum(d->evdev, ABS_Y);
500         }
501         return d;
502 }
503
504 struct litest_device *
505 litest_create_device_with_overrides(enum litest_device_type which,
506                                     const char *name_override,
507                                     struct input_id *id_override,
508                                     const struct input_absinfo *abs_override,
509                                     const int *events_override)
510 {
511         struct litest_device *dev =
512                 litest_add_device_with_overrides(litest_create_context(),
513                                                  which,
514                                                  name_override,
515                                                  id_override,
516                                                  abs_override,
517                                                  events_override);
518         dev->owns_context = true;
519         return dev;
520 }
521
522 struct litest_device *
523 litest_create_device(enum litest_device_type which)
524 {
525         return litest_create_device_with_overrides(which, NULL, NULL, NULL, NULL);
526 }
527
528 int
529 litest_handle_events(struct litest_device *d)
530 {
531         struct pollfd fd;
532
533         fd.fd = libinput_get_fd(d->libinput);
534         fd.events = POLLIN;
535
536         while (poll(&fd, 1, 1))
537                 libinput_dispatch(d->libinput);
538
539         return 0;
540 }
541
542 void
543 litest_delete_device(struct litest_device *d)
544 {
545         if (!d)
546                 return;
547
548         libinput_device_unref(d->libinput_device);
549         if (d->owns_context)
550                 libinput_destroy(d->libinput);
551         libevdev_free(d->evdev);
552         libevdev_uinput_destroy(d->uinput);
553         memset(d,0, sizeof(*d));
554         free(d);
555 }
556
557 void
558 litest_event(struct litest_device *d, unsigned int type,
559              unsigned int code, int value)
560 {
561         libevdev_uinput_write_event(d->uinput, type, code, value);
562 }
563
564 static int
565 auto_assign_value(struct litest_device *d,
566                   const struct input_event *ev,
567                   int slot, int x, int y)
568 {
569         static int tracking_id;
570         int value = ev->value;
571
572         if (value != LITEST_AUTO_ASSIGN || ev->type != EV_ABS)
573                 return value;
574
575         switch (ev->code) {
576         case ABS_X:
577         case ABS_MT_POSITION_X:
578                 value = litest_scale(d, ABS_X, x);
579                 break;
580         case ABS_Y:
581         case ABS_MT_POSITION_Y:
582                 value = litest_scale(d, ABS_Y, y);
583                 break;
584         case ABS_MT_TRACKING_ID:
585                 value = ++tracking_id;
586                 break;
587         case ABS_MT_SLOT:
588                 value = slot;
589                 break;
590         }
591
592         return value;
593 }
594
595
596 void
597 litest_touch_down(struct litest_device *d, unsigned int slot, int x, int y)
598 {
599         struct input_event *ev;
600
601         if (d->interface->touch_down) {
602                 d->interface->touch_down(d, slot, x, y);
603                 return;
604         }
605
606         ev = d->interface->touch_down_events;
607         while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
608                 int value = auto_assign_value(d, ev, slot, x, y);
609                 litest_event(d, ev->type, ev->code, value);
610                 ev++;
611         }
612 }
613
614 void
615 litest_touch_up(struct litest_device *d, unsigned int slot)
616 {
617         struct input_event *ev;
618         struct input_event up[] = {
619                 { .type = EV_ABS, .code = ABS_MT_SLOT, .value = LITEST_AUTO_ASSIGN },
620                 { .type = EV_ABS, .code = ABS_MT_TRACKING_ID, .value = -1 },
621                 { .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
622                 { .type = -1, .code = -1 }
623         };
624
625         if (d->interface->touch_up) {
626                 d->interface->touch_up(d, slot);
627                 return;
628         } else if (d->interface->touch_up_events) {
629                 ev = d->interface->touch_up_events;
630         } else
631                 ev = up;
632
633         while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
634                 int value = auto_assign_value(d, ev, slot, 0, 0);
635                 litest_event(d, ev->type, ev->code, value);
636                 ev++;
637         }
638 }
639
640 void
641 litest_touch_move(struct litest_device *d, unsigned int slot, int x, int y)
642 {
643         struct input_event *ev;
644
645         if (d->interface->touch_move) {
646                 d->interface->touch_move(d, slot, x, y);
647                 return;
648         }
649
650         ev = d->interface->touch_move_events;
651         while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
652                 int value = auto_assign_value(d, ev, slot, x, y);
653                 litest_event(d, ev->type, ev->code, value);
654                 ev++;
655         }
656 }
657
658 void
659 litest_touch_move_to(struct litest_device *d,
660                      unsigned int slot,
661                      int x_from, int y_from,
662                      int x_to, int y_to,
663                      int steps)
664 {
665         for (int i = 0; i < steps - 1; i++)
666                 litest_touch_move(d, slot,
667                                   x_from + (x_to - x_from)/steps * i,
668                                   y_from + (y_to - y_from)/steps * i);
669         litest_touch_move(d, slot, x_to, y_to);
670 }
671
672 void
673 litest_button_click(struct litest_device *d, unsigned int button, bool is_press)
674 {
675
676         struct input_event *ev;
677         struct input_event click[] = {
678                 { .type = EV_KEY, .code = button, .value = is_press ? 1 : 0 },
679                 { .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
680         };
681
682         ARRAY_FOR_EACH(click, ev)
683                 litest_event(d, ev->type, ev->code, ev->value);
684 }
685
686 void
687 litest_keyboard_key(struct litest_device *d, unsigned int key, bool is_press)
688 {
689         litest_button_click(d, key, is_press);
690 }
691
692 int litest_scale(const struct litest_device *d, unsigned int axis, int val)
693 {
694         int min, max;
695         ck_assert_int_ge(val, 0);
696         ck_assert_int_le(val, 100);
697         ck_assert_int_le(axis, ABS_Y);
698
699         min = d->interface->min[axis];
700         max = d->interface->max[axis];
701         return (max - min) * val/100.0 + min;
702 }
703
704 void
705 litest_drain_events(struct libinput *li)
706 {
707         struct libinput_event *event;
708
709         libinput_dispatch(li);
710         while ((event = libinput_get_event(li))) {
711                 libinput_event_destroy(event);
712                 libinput_dispatch(li);
713         }
714 }
715
716 struct libevdev_uinput *
717 litest_create_uinput_device_from_description(const char *name,
718                                              const struct input_id *id,
719                                              const struct input_absinfo *abs,
720                                              const int *events)
721 {
722         struct libevdev_uinput *uinput;
723         struct libevdev *dev;
724         int type, code;
725         int rc;
726         const struct input_absinfo default_abs = {
727                 .value = 0,
728                 .minimum = 0,
729                 .maximum = 0xffff,
730                 .fuzz = 0,
731                 .flat = 0,
732                 .resolution = 100
733         };
734
735         dev = libevdev_new();
736         ck_assert(dev != NULL);
737
738         libevdev_set_name(dev, name);
739         if (id) {
740                 libevdev_set_id_bustype(dev, id->bustype);
741                 libevdev_set_id_vendor(dev, id->vendor);
742                 libevdev_set_id_product(dev, id->product);
743         }
744
745         while (abs && abs->value != -1) {
746                 rc = libevdev_enable_event_code(dev, EV_ABS,
747                                                 abs->value, abs);
748                 ck_assert_int_eq(rc, 0);
749                 abs++;
750         }
751
752         while (events &&
753                (type = *events++) != -1 &&
754                (code = *events++) != -1) {
755                 if (type == INPUT_PROP_MAX) {
756                         rc = libevdev_enable_property(dev, code);
757                 } else {
758                         if (type != EV_SYN)
759                                 ck_assert(!libevdev_has_event_code(dev, type, code));
760                         rc = libevdev_enable_event_code(dev, type, code,
761                                                         type == EV_ABS ? &default_abs : NULL);
762                 }
763                 ck_assert_int_eq(rc, 0);
764         }
765
766         rc = libevdev_uinput_create_from_device(dev,
767                                                 LIBEVDEV_UINPUT_OPEN_MANAGED,
768                                                 &uinput);
769         ck_assert_int_eq(rc, 0);
770
771         libevdev_free(dev);
772
773         return uinput;
774 }
775
776 static struct libevdev_uinput *
777 litest_create_uinput_abs_device_v(const char *name,
778                                   struct input_id *id,
779                                   const struct input_absinfo *abs,
780                                   va_list args)
781 {
782         int events[KEY_MAX * 2 + 2]; /* increase this if not sufficient */
783         int *event = events;
784         int type, code;
785
786         while ((type = va_arg(args, int)) != -1 &&
787                (code = va_arg(args, int)) != -1) {
788                 *event++ = type;
789                 *event++ = code;
790                 ck_assert(event < &events[ARRAY_LENGTH(events) - 2]);
791         }
792
793         *event++ = -1;
794         *event++ = -1;
795
796         return litest_create_uinput_device_from_description(name, id,
797                                                             abs, events);
798 }
799
800 struct libevdev_uinput *
801 litest_create_uinput_abs_device(const char *name,
802                                 struct input_id *id,
803                                 const struct input_absinfo *abs,
804                                 ...)
805 {
806         struct libevdev_uinput *uinput;
807         va_list args;
808
809         va_start(args, abs);
810         uinput = litest_create_uinput_abs_device_v(name, id, abs, args);
811         va_end(args);
812
813         return uinput;
814 }
815
816 struct libevdev_uinput *
817 litest_create_uinput_device(const char *name, struct input_id *id, ...)
818 {
819         struct libevdev_uinput *uinput;
820         va_list args;
821
822         va_start(args, id);
823         uinput = litest_create_uinput_abs_device_v(name, id, NULL, args);
824         va_end(args);
825
826         return uinput;
827 }