Debian Codegraph @

test/test-path.c

raw
1 /*
2 * Copyright © 2013 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <config.h>
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <libinput.h>
29 #include <stdio.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32
33 #include "libinput-util.h"
34 #include "litest.h"
35
36 struct counter {
37 int open_func_count;
38 int close_func_count;
39 };
40
41 static int
42 open_restricted_count(const char *path, int flags, void *data)
43 {
44 struct counter *c = data;
45 int fd;
46
47 c->open_func_count++;
48
49 fd = open(path, flags);
50 return fd < 0 ? -errno : fd;
51 }
52
53 static void
54 close_restricted_count(int fd, void *data)
55 {
56 struct counter *c = data;
57
58 c->close_func_count++;
59 close(fd);
60 }
61
62 static const struct libinput_interface counting_interface = {
63 .open_restricted = open_restricted_count,
64 .close_restricted = close_restricted_count,
65 };
66
67 static int
68 open_restricted(const char *path, int flags, void *data)
69 {
70 int fd = open(path, flags);
71 return fd < 0 ? -errno : fd;
72 }
73
74 static void
75 close_restricted(int fd, void *data)
76 {
77 close(fd);
78 }
79
80 static const struct libinput_interface simple_interface = {
81 .open_restricted = open_restricted,
82 .close_restricted = close_restricted,
83 };
84
85 START_TEST(path_create_NULL)
86 {
87 struct libinput *li;
88 struct counter counter;
89
90 counter.open_func_count = 0;
91 counter.close_func_count = 0;
92
93 li = libinput_path_create_context(NULL, NULL);
94 litest_assert(li == NULL);
95 li = libinput_path_create_context(&counting_interface, &counter);
96 litest_assert_notnull(li);
97 libinput_unref(li);
98
99 litest_assert_int_eq(counter.open_func_count, 0);
100 litest_assert_int_eq(counter.close_func_count, 0);
101 }
102 END_TEST
103
104 START_TEST(path_create_invalid)
105 {
106 struct libinput *li;
107 struct libinput_device *device;
108 const char *path = "/tmp";
109 struct counter counter;
110
111 counter.open_func_count = 0;
112 counter.close_func_count = 0;
113
114 li = libinput_path_create_context(&counting_interface, &counter);
115 litest_assert_notnull(li);
116
117 litest_disable_log_handler(li);
118
119 device = libinput_path_add_device(li, path);
120 litest_assert(device == NULL);
121
122 litest_assert_int_eq(counter.open_func_count, 0);
123 litest_assert_int_eq(counter.close_func_count, 0);
124
125 litest_restore_log_handler(li);
126 libinput_unref(li);
127 litest_assert_int_eq(counter.close_func_count, 0);
128 }
129 END_TEST
130
131 START_TEST(path_create_invalid_kerneldev)
132 {
133 struct libinput *li;
134 struct libinput_device *device;
135 const char *path = "/dev/uinput";
136 struct counter counter;
137
138 counter.open_func_count = 0;
139 counter.close_func_count = 0;
140
141 li = libinput_path_create_context(&counting_interface, &counter);
142 litest_assert_notnull(li);
143
144 litest_disable_log_handler(li);
145
146 device = libinput_path_add_device(li, path);
147 litest_assert(device == NULL);
148
149 litest_assert_int_eq(counter.open_func_count, 1);
150 litest_assert_int_eq(counter.close_func_count, 1);
151
152 litest_restore_log_handler(li);
153 libinput_unref(li);
154 litest_assert_int_eq(counter.close_func_count, 1);
155 }
156 END_TEST
157
158 START_TEST(path_create_invalid_file)
159 {
160 struct libinput *li;
161 struct libinput_device *device;
162 char path[] = "/tmp/litest_path_XXXXXX";
163 int fd;
164 struct counter counter;
165
166 umask(002);
167 fd = mkstemp(path);
168 litest_assert_int_ge(fd, 0);
169 close(fd);
170
171 counter.open_func_count = 0;
172 counter.close_func_count = 0;
173
174 li = libinput_path_create_context(&counting_interface, &counter);
175 unlink(path);
176
177 litest_disable_log_handler(li);
178
179 litest_assert_notnull(li);
180 device = libinput_path_add_device(li, path);
181 litest_assert(device == NULL);
182
183 litest_assert_int_eq(counter.open_func_count, 0);
184 litest_assert_int_eq(counter.close_func_count, 0);
185
186 litest_restore_log_handler(li);
187 libinput_unref(li);
188 litest_assert_int_eq(counter.close_func_count, 0);
189 }
190 END_TEST
191
192 START_TEST(path_create_pathmax_file)
193 {
194 struct libinput *li;
195 struct libinput_device *device;
196 struct counter counter;
197
198 _autofree_ char *path = zalloc(PATH_MAX * 2);
199 memset(path, 'a', PATH_MAX * 2 - 1);
200
201 counter.open_func_count = 0;
202 counter.close_func_count = 0;
203
204 li = libinput_path_create_context(&counting_interface, &counter);
205
206 litest_set_log_handler_bug(li);
207 litest_assert_notnull(li);
208 device = libinput_path_add_device(li, path);
209 litest_assert(device == NULL);
210
211 litest_assert_int_eq(counter.open_func_count, 0);
212 litest_assert_int_eq(counter.close_func_count, 0);
213
214 litest_restore_log_handler(li);
215 libinput_unref(li);
216 litest_assert_int_eq(counter.close_func_count, 0);
217 }
218 END_TEST
219
220 START_TEST(path_create_destroy)
221 {
222 struct libinput *li;
223 struct libinput_device *device;
224 struct libevdev_uinput *uinput;
225 struct counter counter;
226
227 counter.open_func_count = 0;
228 counter.close_func_count = 0;
229
230 /* clang-format off */
231 uinput = litest_create_uinput_device("test device", NULL,
232 EV_KEY, BTN_LEFT,
233 EV_KEY, BTN_RIGHT,
234 EV_REL, REL_X,
235 EV_REL, REL_Y,
236 -1);
237 /* clang-format on */
238
239 li = libinput_path_create_context(&counting_interface, &counter);
240 litest_assert_notnull(li);
241
242 litest_disable_log_handler(li);
243
244 litest_assert(libinput_get_user_data(li) == &counter);
245
246 device = libinput_path_add_device(li, libevdev_uinput_get_devnode(uinput));
247 litest_assert_notnull(device);
248
249 litest_assert_int_eq(counter.open_func_count, 1);
250
251 libevdev_uinput_destroy(uinput);
252 libinput_unref(li);
253 litest_assert_int_eq(counter.close_func_count, 1);
254 }
255 END_TEST
256
257 START_TEST(path_force_destroy)
258 {
259 struct litest_device *dev = litest_current_device();
260 struct libinput *li;
261 struct libinput_device *device;
262
263 li = libinput_path_create_context(&simple_interface, NULL);
264 litest_assert_notnull(li);
265 libinput_ref(li);
266 device = libinput_path_add_device(li, libevdev_uinput_get_devnode(dev->uinput));
267 litest_assert_notnull(device);
268
269 while (libinput_unref(li) != NULL)
270 ;
271 }
272 END_TEST
273
274 START_TEST(path_set_user_data)
275 {
276 struct libinput *li;
277 int data1, data2;
278
279 li = libinput_path_create_context(&simple_interface, &data1);
280 litest_assert_notnull(li);
281 litest_assert(libinput_get_user_data(li) == &data1);
282 libinput_set_user_data(li, &data2);
283 litest_assert(libinput_get_user_data(li) == &data2);
284
285 libinput_unref(li);
286 }
287 END_TEST
288
289 START_TEST(path_added_seat)
290 {
291 struct litest_device *dev = litest_current_device();
292 struct libinput *li = dev->libinput;
293 struct libinput_event *event;
294 struct libinput_device *device;
295 struct libinput_seat *seat;
296 const char *seat_name;
297
298 litest_dispatch(li);
299
300 event = libinput_get_event(li);
301 litest_assert_notnull(event);
302
303 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_ADDED);
304
305 device = libinput_event_get_device(event);
306 seat = libinput_device_get_seat(device);
307 litest_assert_notnull(seat);
308
309 seat_name = libinput_seat_get_logical_name(seat);
310 litest_assert_str_eq(seat_name, "default");
311
312 libinput_event_destroy(event);
313 }
314 END_TEST
315
316 START_TEST(path_seat_change)
317 {
318 struct litest_device *dev = litest_current_device();
319 struct libinput *li = dev->libinput;
320 struct libinput_event *event;
321 struct libinput_device *device;
322 struct libinput_seat *seat1, *seat2;
323 const char *seat1_name;
324 const char *seat2_name = "new seat";
325 int rc;
326
327 litest_dispatch(li);
328
329 event = libinput_get_event(li);
330 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_ADDED);
331
332 device = libinput_event_get_device(event);
333 libinput_device_ref(device);
334
335 seat1 = libinput_device_get_seat(device);
336 libinput_seat_ref(seat1);
337
338 seat1_name = libinput_seat_get_logical_name(seat1);
339 libinput_event_destroy(event);
340
341 litest_drain_events(li);
342
343 rc = libinput_device_set_seat_logical_name(device, seat2_name);
344 litest_assert_int_eq(rc, 0);
345
346 litest_dispatch(li);
347
348 event = libinput_get_event(li);
349 litest_assert_notnull(event);
350
351 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_REMOVED);
352
353 litest_assert(libinput_event_get_device(event) == device);
354 libinput_event_destroy(event);
355
356 event = libinput_get_event(li);
357 litest_assert_notnull(event);
358 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_ADDED);
359 litest_assert(libinput_event_get_device(event) != device);
360 libinput_device_unref(device);
361
362 device = libinput_event_get_device(event);
363 seat2 = libinput_device_get_seat(device);
364
365 litest_assert_str_ne(libinput_seat_get_logical_name(seat2), seat1_name);
366 litest_assert_str_eq(libinput_seat_get_logical_name(seat2), seat2_name);
367 libinput_event_destroy(event);
368
369 libinput_seat_unref(seat1);
370
371 /* litest: swap the new device in, so cleanup works */
372 libinput_device_unref(dev->libinput_device);
373 libinput_device_ref(device);
374 dev->libinput_device = device;
375 }
376 END_TEST
377
378 START_TEST(path_added_device)
379 {
380 struct litest_device *dev = litest_current_device();
381 struct libinput *li = dev->libinput;
382 struct libinput_event *event;
383 struct libinput_device *device;
384
385 litest_dispatch(li);
386
387 event = libinput_get_event(li);
388 litest_assert_notnull(event);
389 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_ADDED);
390 device = libinput_event_get_device(event);
391 litest_assert_notnull(device);
392
393 libinput_event_destroy(event);
394 }
395 END_TEST
396
397 START_TEST(path_add_device)
398 {
399 struct litest_device *dev = litest_current_device();
400 struct libinput *li = dev->libinput;
401 struct libinput_event *event;
402 struct libinput_device *device;
403
404 litest_dispatch(li);
405
406 event = libinput_get_event(li);
407 litest_assert_notnull(event);
408 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_ADDED);
409 device = libinput_event_get_device(event);
410 litest_assert_notnull(device);
411 _autofree_ char *sysname1 = safe_strdup(libinput_device_get_sysname(device));
412 libinput_event_destroy(event);
413
414 litest_assert_empty_queue(li);
415
416 device = libinput_path_add_device(li, libevdev_uinput_get_devnode(dev->uinput));
417 litest_assert_notnull(device);
418
419 litest_dispatch(li);
420
421 event = libinput_get_event(li);
422 litest_assert_notnull(event);
423 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_ADDED);
424 device = libinput_event_get_device(event);
425 litest_assert_notnull(device);
426 _autofree_ char *sysname2 = safe_strdup(libinput_device_get_sysname(device));
427 libinput_event_destroy(event);
428
429 litest_assert_str_eq(sysname1, sysname2);
430 }
431 END_TEST
432
433 START_TEST(path_add_invalid_path)
434 {
435 struct libinput_device *device;
436
437 _litest_context_destroy_ struct libinput *li = litest_create_context();
438
439 litest_disable_log_handler(li);
440 device = libinput_path_add_device(li, "/tmp/");
441 litest_restore_log_handler(li);
442 litest_assert(device == NULL);
443
444 litest_dispatch(li);
445
446 litest_assert_empty_queue(li);
447 }
448 END_TEST
449
450 START_TEST(path_device_sysname)
451 {
452 struct litest_device *dev = litest_current_device();
453 struct libinput_event *ev;
454 struct libinput_device *device;
455 const char *sysname;
456
457 litest_dispatch(dev->libinput);
458
459 ev = libinput_get_event(dev->libinput);
460 litest_assert_notnull(ev);
461 litest_assert_event_type(ev, LIBINPUT_EVENT_DEVICE_ADDED);
462 device = libinput_event_get_device(ev);
463 litest_assert_notnull(device);
464 sysname = libinput_device_get_sysname(device);
465
466 litest_assert_notnull(sysname);
467 litest_assert_int_gt(strlen(sysname), 1U);
468 litest_assert(strchr(sysname, '/') == NULL);
469 litest_assert(strstartswith(sysname, "event"));
470
471 libinput_event_destroy(ev);
472 }
473 END_TEST
474
475 START_TEST(path_remove_device)
476 {
477 struct litest_device *dev = litest_current_device();
478 struct libinput *li = dev->libinput;
479 struct libinput_event *event;
480 struct libinput_device *device;
481 int remove_event = 0;
482
483 device = libinput_path_add_device(li, libevdev_uinput_get_devnode(dev->uinput));
484 litest_assert_notnull(device);
485 litest_drain_events(li);
486
487 libinput_path_remove_device(device);
488 litest_dispatch(li);
489
490 while ((event = libinput_get_event(li))) {
491 enum libinput_event_type type;
492 type = libinput_event_get_type(event);
493
494 if (type == LIBINPUT_EVENT_DEVICE_REMOVED)
495 remove_event++;
496
497 libinput_event_destroy(event);
498 }
499
500 litest_assert_int_eq(remove_event, 1);
501 }
502 END_TEST
503
504 START_TEST(path_double_remove_device)
505 {
506 struct litest_device *dev = litest_current_device();
507 struct libinput *li = dev->libinput;
508 struct libinput_event *event;
509 struct libinput_device *device;
510 int remove_event = 0;
511
512 device = libinput_path_add_device(li, libevdev_uinput_get_devnode(dev->uinput));
513 litest_assert_notnull(device);
514 litest_drain_events(li);
515
516 libinput_path_remove_device(device);
517 libinput_path_remove_device(device);
518 litest_dispatch(li);
519
520 while ((event = libinput_get_event(li))) {
521 enum libinput_event_type type;
522 type = libinput_event_get_type(event);
523
524 if (type == LIBINPUT_EVENT_DEVICE_REMOVED)
525 remove_event++;
526
527 libinput_event_destroy(event);
528 }
529
530 litest_assert_int_eq(remove_event, 1);
531 }
532 END_TEST
533
534 START_TEST(path_suspend)
535 {
536 struct libinput *li;
537 struct libinput_device *device;
538 struct libevdev_uinput *uinput;
539 int rc;
540 void *userdata = &rc;
541
542 /* clang-format off */
543 uinput = litest_create_uinput_device("test device", NULL,
544 EV_KEY, BTN_LEFT,
545 EV_KEY, BTN_RIGHT,
546 EV_REL, REL_X,
547 EV_REL, REL_Y,
548 -1);
549 /* clang-format on */
550
551 li = libinput_path_create_context(&simple_interface, userdata);
552 litest_assert_notnull(li);
553
554 device = libinput_path_add_device(li, libevdev_uinput_get_devnode(uinput));
555 litest_assert_notnull(device);
556
557 libinput_suspend(li);
558 libinput_resume(li);
559
560 libevdev_uinput_destroy(uinput);
561 libinput_unref(li);
562 }
563 END_TEST
564
565 START_TEST(path_double_suspend)
566 {
567 struct libinput *li;
568 struct libinput_device *device;
569 struct libevdev_uinput *uinput;
570 int rc;
571 void *userdata = &rc;
572
573 /* clang-format off */
574 uinput = litest_create_uinput_device("test device", NULL,
575 EV_KEY, BTN_LEFT,
576 EV_KEY, BTN_RIGHT,
577 EV_REL, REL_X,
578 EV_REL, REL_Y,
579 -1);
580 /* clang-format on */
581
582 li = libinput_path_create_context(&simple_interface, userdata);
583 litest_assert_notnull(li);
584
585 device = libinput_path_add_device(li, libevdev_uinput_get_devnode(uinput));
586 litest_assert_notnull(device);
587
588 libinput_suspend(li);
589 libinput_suspend(li);
590 libinput_resume(li);
591
592 libevdev_uinput_destroy(uinput);
593 libinput_unref(li);
594 }
595 END_TEST
596
597 START_TEST(path_double_resume)
598 {
599 struct libinput *li;
600 struct libinput_device *device;
601 struct libevdev_uinput *uinput;
602 int rc;
603 void *userdata = &rc;
604
605 /* clang-format off */
606 uinput = litest_create_uinput_device("test device", NULL,
607 EV_KEY, BTN_LEFT,
608 EV_KEY, BTN_RIGHT,
609 EV_REL, REL_X,
610 EV_REL, REL_Y,
611 -1);
612 /* clang-format on */
613
614 li = libinput_path_create_context(&simple_interface, userdata);
615 litest_assert_notnull(li);
616
617 device = libinput_path_add_device(li, libevdev_uinput_get_devnode(uinput));
618 litest_assert_notnull(device);
619
620 libinput_suspend(li);
621 libinput_resume(li);
622 libinput_resume(li);
623
624 libevdev_uinput_destroy(uinput);
625 libinput_unref(li);
626 }
627 END_TEST
628
629 START_TEST(path_add_device_suspend_resume)
630 {
631 struct libinput *li;
632 struct libinput_device *device;
633 struct libinput_event *event;
634 struct libevdev_uinput *uinput1, *uinput2;
635 int rc;
636 int nevents;
637 void *userdata = &rc;
638
639 /* clang-format off */
640 uinput1 = litest_create_uinput_device("test device", NULL,
641 EV_KEY, BTN_LEFT,
642 EV_KEY, BTN_RIGHT,
643 EV_REL, REL_X,
644 EV_REL, REL_Y,
645 -1);
646 uinput2 = litest_create_uinput_device("test device 2", NULL,
647 EV_KEY, BTN_LEFT,
648 EV_KEY, BTN_RIGHT,
649 EV_REL, REL_X,
650 EV_REL, REL_Y,
651 -1);
652 /* clang-format on */
653
654 li = libinput_path_create_context(&simple_interface, userdata);
655 litest_assert_notnull(li);
656
657 device = libinput_path_add_device(li, libevdev_uinput_get_devnode(uinput1));
658 litest_assert_notnull(device);
659 libinput_path_add_device(li, libevdev_uinput_get_devnode(uinput2));
660
661 litest_dispatch(li);
662
663 nevents = 0;
664 while ((event = libinput_get_event(li))) {
665 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_ADDED);
666 libinput_event_destroy(event);
667 nevents++;
668 }
669
670 litest_assert_int_eq(nevents, 2);
671
672 libinput_suspend(li);
673 litest_dispatch(li);
674
675 nevents = 0;
676 while ((event = libinput_get_event(li))) {
677 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_REMOVED);
678 libinput_event_destroy(event);
679 nevents++;
680 }
681
682 litest_assert_int_eq(nevents, 2);
683
684 libinput_resume(li);
685 litest_dispatch(li);
686
687 nevents = 0;
688 while ((event = libinput_get_event(li))) {
689 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_ADDED);
690 libinput_event_destroy(event);
691 nevents++;
692 }
693
694 litest_assert_int_eq(nevents, 2);
695
696 libevdev_uinput_destroy(uinput1);
697 libevdev_uinput_destroy(uinput2);
698 libinput_unref(li);
699 }
700 END_TEST
701
702 START_TEST(path_add_device_suspend_resume_fail)
703 {
704 struct libinput *li;
705 struct libinput_device *device;
706 struct libinput_event *event;
707 struct libevdev_uinput *uinput1, *uinput2;
708 int rc;
709 int nevents;
710 void *userdata = &rc;
711
712 /* clang-format off */
713 uinput1 = litest_create_uinput_device("test device", NULL,
714 EV_KEY, BTN_LEFT,
715 EV_KEY, BTN_RIGHT,
716 EV_REL, REL_X,
717 EV_REL, REL_Y,
718 -1);
719 uinput2 = litest_create_uinput_device("test device 2", NULL,
720 EV_KEY, BTN_LEFT,
721 EV_KEY, BTN_RIGHT,
722 EV_REL, REL_X,
723 EV_REL, REL_Y,
724 -1);
725 /* clang-format on */
726
727 li = libinput_path_create_context(&simple_interface, userdata);
728 litest_assert_notnull(li);
729
730 device = libinput_path_add_device(li, libevdev_uinput_get_devnode(uinput1));
731 litest_assert_notnull(device);
732 device = libinput_path_add_device(li, libevdev_uinput_get_devnode(uinput2));
733 litest_assert_notnull(device);
734
735 litest_dispatch(li);
736
737 nevents = 0;
738 while ((event = libinput_get_event(li))) {
739 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_ADDED);
740 libinput_event_destroy(event);
741 nevents++;
742 }
743
744 litest_assert_int_eq(nevents, 2);
745
746 libinput_suspend(li);
747 litest_dispatch(li);
748
749 nevents = 0;
750 while ((event = libinput_get_event(li))) {
751 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_REMOVED);
752 libinput_event_destroy(event);
753 nevents++;
754 }
755
756 litest_assert_int_eq(nevents, 2);
757
758 /* now drop one of the devices */
759 libevdev_uinput_destroy(uinput1);
760 rc = libinput_resume(li);
761 litest_assert_int_eq(rc, -1);
762
763 litest_dispatch(li);
764
765 nevents = 0;
766 while ((event = libinput_get_event(li))) {
767 enum libinput_event_type type;
768 type = libinput_event_get_type(event);
769 /* We expect one device being added, second one fails,
770 * causing a removed event for the first one */
771 if (type != LIBINPUT_EVENT_DEVICE_ADDED &&
772 type != LIBINPUT_EVENT_DEVICE_REMOVED)
773 litest_abort_msg("Unexpected event type");
774 libinput_event_destroy(event);
775 nevents++;
776 }
777
778 litest_assert_int_eq(nevents, 2);
779
780 libevdev_uinput_destroy(uinput2);
781 libinput_unref(li);
782 }
783 END_TEST
784
785 START_TEST(path_add_device_suspend_resume_remove_device)
786 {
787 struct libinput *li;
788 struct libinput_device *device;
789 struct libinput_event *event;
790 struct libevdev_uinput *uinput1, *uinput2;
791 int rc;
792 int nevents;
793 void *userdata = &rc;
794
795 /* clang-format off */
796 uinput1 = litest_create_uinput_device("test device", NULL,
797 EV_KEY, BTN_LEFT,
798 EV_KEY, BTN_RIGHT,
799 EV_REL, REL_X,
800 EV_REL, REL_Y,
801 -1);
802 uinput2 = litest_create_uinput_device("test device 2", NULL,
803 EV_KEY, BTN_LEFT,
804 EV_KEY, BTN_RIGHT,
805 EV_REL, REL_X,
806 EV_REL, REL_Y,
807 -1);
808 /* clang-format on */
809
810 li = libinput_path_create_context(&simple_interface, userdata);
811 litest_assert_notnull(li);
812
813 device = libinput_path_add_device(li, libevdev_uinput_get_devnode(uinput1));
814 litest_assert_notnull(device);
815 device = libinput_path_add_device(li, libevdev_uinput_get_devnode(uinput2));
816
817 libinput_device_ref(device);
818 litest_dispatch(li);
819
820 nevents = 0;
821 while ((event = libinput_get_event(li))) {
822 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_ADDED);
823 libinput_event_destroy(event);
824 nevents++;
825 }
826
827 litest_assert_int_eq(nevents, 2);
828
829 libinput_suspend(li);
830 litest_dispatch(li);
831
832 nevents = 0;
833 while ((event = libinput_get_event(li))) {
834 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_REMOVED);
835 libinput_event_destroy(event);
836 nevents++;
837 }
838
839 litest_assert_int_eq(nevents, 2);
840
841 /* now drop and remove one of the devices */
842 libevdev_uinput_destroy(uinput2);
843 libinput_path_remove_device(device);
844 libinput_device_unref(device);
845
846 rc = libinput_resume(li);
847 litest_assert_int_eq(rc, 0);
848
849 litest_dispatch(li);
850
851 nevents = 0;
852 while ((event = libinput_get_event(li))) {
853 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_ADDED);
854 libinput_event_destroy(event);
855 nevents++;
856 }
857
858 litest_assert_int_eq(nevents, 1);
859
860 libevdev_uinput_destroy(uinput1);
861 libinput_unref(li);
862 }
863 END_TEST
864
865 START_TEST(path_device_gone)
866 {
867 struct libinput *li;
868 struct libinput_device *device;
869 struct libevdev_uinput *uinput;
870 struct libinput_event *event;
871
872 /* clang-format off */
873 uinput = litest_create_uinput_device("test device", NULL,
874 EV_KEY, BTN_LEFT,
875 EV_KEY, BTN_RIGHT,
876 EV_REL, REL_X,
877 EV_REL, REL_Y,
878 -1);
879 /* clang-format on */
880
881 li = libinput_path_create_context(&simple_interface, NULL);
882 litest_assert_notnull(li);
883
884 device = libinput_path_add_device(li, libevdev_uinput_get_devnode(uinput));
885 litest_assert_notnull(device);
886
887 litest_drain_events(li);
888
889 libevdev_uinput_destroy(uinput);
890
891 litest_dispatch(li);
892
893 event = libinput_get_event(li);
894 litest_assert_notnull(event);
895 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_REMOVED);
896 libinput_event_destroy(event);
897
898 libinput_unref(li);
899 }
900 END_TEST
901
902 START_TEST(path_seat_recycle)
903 {
904 struct libinput *li;
905 struct libevdev_uinput *uinput;
906 int rc;
907 void *userdata = &rc;
908 struct libinput_event *ev;
909 struct libinput_device *device;
910 struct libinput_seat *saved_seat = NULL;
911 struct libinput_seat *seat;
912 int data = 0;
913 int found = 0;
914 void *user_data;
915
916 /* clang-format off */
917 uinput = litest_create_uinput_device("test device", NULL,
918 EV_KEY, BTN_LEFT,
919 EV_KEY, BTN_RIGHT,
920 EV_REL, REL_X,
921 EV_REL, REL_Y,
922 -1);
923 /* clang-format on */
924
925 li = libinput_path_create_context(&simple_interface, userdata);
926 litest_assert_notnull(li);
927
928 device = libinput_path_add_device(li, libevdev_uinput_get_devnode(uinput));
929 litest_assert_notnull(device);
930
931 litest_dispatch(li);
932
933 ev = libinput_get_event(li);
934 litest_assert_notnull(ev);
935 litest_assert_event_type(ev, LIBINPUT_EVENT_DEVICE_ADDED);
936 device = libinput_event_get_device(ev);
937 litest_assert_notnull(device);
938 saved_seat = libinput_device_get_seat(device);
939 libinput_seat_set_user_data(saved_seat, &data);
940 libinput_seat_ref(saved_seat);
941 libinput_event_destroy(ev);
942 litest_assert_notnull(saved_seat);
943
944 litest_assert_empty_queue(li);
945
946 libinput_suspend(li);
947
948 litest_drain_events(li);
949
950 libinput_resume(li);
951
952 litest_dispatch(li);
953 ev = libinput_get_event(li);
954 litest_assert_notnull(ev);
955 litest_assert_event_type(ev, LIBINPUT_EVENT_DEVICE_ADDED);
956 device = libinput_event_get_device(ev);
957 litest_assert_notnull(device);
958
959 seat = libinput_device_get_seat(device);
960 user_data = libinput_seat_get_user_data(seat);
961 if (user_data == &data) {
962 found = 1;
963 litest_assert(seat == saved_seat);
964 }
965
966 libinput_event_destroy(ev);
967 litest_assert(found == 1);
968
969 libinput_unref(li);
970
971 libevdev_uinput_destroy(uinput);
972 }
973 END_TEST
974
975 START_TEST(path_udev_assign_seat)
976 {
977 struct litest_device *dev = litest_current_device();
978 struct libinput *li = dev->libinput;
979 int rc;
980
981 litest_set_log_handler_bug(li);
982 rc = libinput_udev_assign_seat(li, "foo");
983 litest_assert_int_eq(rc, -1);
984 litest_restore_log_handler(li);
985 }
986 END_TEST
987
988 START_TEST(path_ignore_device)
989 {
990 struct litest_device *dev;
991 struct libinput_device *device;
992 const char *path;
993
994 dev = litest_create(LITEST_IGNORED_MOUSE, NULL, NULL, NULL, NULL);
995 path = libevdev_uinput_get_devnode(dev->uinput);
996 litest_assert_notnull(path);
997
998 _litest_context_destroy_ struct libinput *li = litest_create_context();
999 device = libinput_path_add_device(li, path);
1000 litest_assert(device == NULL);
1001
1002 litest_device_destroy(dev);
1003 }
1004 END_TEST
1005
1006 TEST_COLLECTION(path)
1007 {
1008 /* clang-format off */
1009 litest_add_no_device(path_create_NULL);
1010 litest_add_no_device(path_create_invalid);
1011 litest_add_no_device(path_create_invalid_file);
1012 litest_add_no_device(path_create_invalid_kerneldev);
1013 litest_add_no_device(path_create_pathmax_file);
1014 litest_add_no_device(path_create_destroy);
1015 litest_add(path_force_destroy, LITEST_ANY, LITEST_ANY);
1016 litest_add_no_device(path_set_user_data);
1017 litest_add_no_device(path_suspend);
1018 litest_add_no_device(path_double_suspend);
1019 litest_add_no_device(path_double_resume);
1020 litest_add_no_device(path_add_device_suspend_resume);
1021 litest_add_no_device(path_add_device_suspend_resume_fail);
1022 litest_add_no_device(path_add_device_suspend_resume_remove_device);
1023 litest_add_for_device(path_added_seat, LITEST_SYNAPTICS_CLICKPAD_X220);
1024 litest_add_for_device(path_seat_change, LITEST_SYNAPTICS_CLICKPAD_X220);
1025 litest_add(path_added_device, LITEST_ANY, LITEST_ANY);
1026 litest_add(path_device_sysname, LITEST_ANY, LITEST_ANY);
1027 litest_add_for_device(path_add_device, LITEST_SYNAPTICS_CLICKPAD_X220);
1028 litest_add_no_device(path_add_invalid_path);
1029 litest_add_for_device(path_remove_device, LITEST_SYNAPTICS_CLICKPAD_X220);
1030 litest_add_for_device(path_double_remove_device, LITEST_SYNAPTICS_CLICKPAD_X220);
1031 litest_add_no_device(path_device_gone);
1032 litest_add_no_device(path_seat_recycle);
1033 litest_add_for_device(path_udev_assign_seat, LITEST_SYNAPTICS_CLICKPAD_X220);
1034
1035 litest_add_no_device(path_ignore_device);
1036 /* clang-format on */
1037 }