Debian Codegraph @

test/test-udev.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-util.h>
29 #include <libinput.h>
30 #include <libudev.h>
31 #include <unistd.h>
32
33 #include "litest.h"
34
35 static int
36 open_restricted(const char *path, int flags, void *data)
37 {
38 int fd;
39 fd = open(path, flags);
40 return fd < 0 ? -errno : fd;
41 }
42 static void
43 close_restricted(int fd, void *data)
44 {
45 close(fd);
46 }
47
48 static const struct libinput_interface simple_interface = {
49 .open_restricted = open_restricted,
50 .close_restricted = close_restricted,
51 };
52
53 START_TEST(udev_create_NULL)
54 {
55 _unref_(udev) *udev = udev_new();
56
57 _unref_(libinput) *li = libinput_udev_create_context(NULL, NULL, NULL);
58 litest_assert(li == NULL);
59
60 li = libinput_udev_create_context(&simple_interface, NULL, NULL);
61 litest_assert(li == NULL);
62
63 li = libinput_udev_create_context(NULL, NULL, udev);
64 litest_assert(li == NULL);
65
66 li = libinput_udev_create_context(&simple_interface, NULL, udev);
67 litest_assert_notnull(li);
68 litest_assert_int_eq(libinput_udev_assign_seat(li, NULL), -1);
69 }
70 END_TEST
71
72 START_TEST(udev_create_seat0)
73 {
74 struct libinput_event *event;
75 int fd;
76
77 _unref_(udev) *udev = udev_new();
78 litest_assert_notnull(udev);
79
80 _unref_(libinput) *li =
81 libinput_udev_create_context(&simple_interface, NULL, udev);
82 litest_assert_notnull(li);
83 litest_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
84
85 fd = libinput_get_fd(li);
86 litest_assert_int_ge(fd, 0);
87
88 /* expect at least one event */
89 litest_dispatch(li);
90 event = libinput_get_event(li);
91 litest_assert_notnull(event);
92
93 libinput_event_destroy(event);
94 }
95 END_TEST
96
97 START_TEST(udev_create_empty_seat)
98 {
99 struct libinput_event *event;
100 int fd;
101
102 _unref_(udev) *udev = udev_new();
103 litest_assert_notnull(udev);
104
105 /* expect a libinput reference, but no events */
106 _unref_(libinput) *li =
107 libinput_udev_create_context(&simple_interface, NULL, udev);
108 litest_assert_notnull(li);
109 litest_assert_int_eq(libinput_udev_assign_seat(li, "seatdoesntexist"), 0);
110
111 fd = libinput_get_fd(li);
112 litest_assert_int_ge(fd, 0);
113
114 litest_dispatch(li);
115 event = libinput_get_event(li);
116 litest_assert(event == NULL);
117
118 libinput_event_destroy(event);
119 }
120 END_TEST
121
122 START_TEST(udev_create_seat_too_long)
123 {
124 char seatname[258];
125
126 memset(seatname, 'a', sizeof(seatname) - 1);
127 seatname[sizeof(seatname) - 1] = '\0';
128
129 _unref_(udev) *udev = udev_new();
130 litest_assert_notnull(udev);
131
132 _unref_(libinput) *li =
133 libinput_udev_create_context(&simple_interface, NULL, udev);
134 litest_assert_notnull(li);
135 litest_set_log_handler_bug(li);
136
137 litest_assert_int_eq(libinput_udev_assign_seat(li, seatname), -1);
138
139 litest_assert_empty_queue(li);
140 }
141 END_TEST
142
143 START_TEST(udev_set_user_data)
144 {
145 int data1, data2;
146
147 _unref_(udev) *udev = udev_new();
148 litest_assert_notnull(udev);
149
150 _unref_(libinput) *li =
151 libinput_udev_create_context(&simple_interface, &data1, udev);
152 litest_assert_notnull(li);
153 litest_assert(libinput_get_user_data(li) == &data1);
154 libinput_set_user_data(li, &data2);
155 litest_assert(libinput_get_user_data(li) == &data2);
156 }
157 END_TEST
158
159 START_TEST(udev_added_seat_default)
160 {
161 struct libinput_event *event;
162 struct libinput_device *device;
163 struct libinput_seat *seat;
164 const char *seat_name;
165
166 _unref_(udev) *udev = udev_new();
167 litest_assert_notnull(udev);
168
169 _unref_(libinput) *li =
170 libinput_udev_create_context(&simple_interface, NULL, udev);
171 litest_assert_notnull(li);
172 litest_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
173 litest_dispatch(li);
174
175 /* Drop any events from other devices */
176 litest_drain_events(li);
177
178 /* Now create our own device, it should be in the "default"
179 * logical seat. This test may fail if there is a local rule changing
180 * that, but it'll be fine for the 99% case. */
181 _unused_ _destroy_(litest_device) *dev =
182 litest_create(LITEST_MOUSE, NULL, NULL, NULL, NULL);
183 litest_wait_for_event_of_type(li, LIBINPUT_EVENT_DEVICE_ADDED);
184 event = libinput_get_event(li);
185 device = libinput_event_get_device(event);
186 seat = libinput_device_get_seat(device);
187 litest_assert_notnull(seat);
188
189 seat_name = libinput_seat_get_logical_name(seat);
190 litest_assert_str_eq(seat_name, "default");
191 libinput_event_destroy(event);
192 }
193 END_TEST
194
195 START_TEST(udev_change_seat)
196 {
197 const char *seat2_name = "new seat";
198
199 _unref_(udev) *udev = udev_new();
200 litest_assert_notnull(udev);
201
202 _unref_(libinput) *li =
203 libinput_udev_create_context(&simple_interface, NULL, udev);
204 litest_assert_notnull(li);
205 litest_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
206 litest_dispatch(li);
207
208 /* Drop any events from other devices */
209 litest_drain_events(li);
210
211 const char *devname = "udev-change-seat device";
212
213 /* Now create our own device, it should be in the "default"
214 * logical seat. This test may fail if there is a local rule changing
215 * that, but it'll be fine for the 99% case. */
216 _unused_ _destroy_(litest_device) *dev =
217 litest_create(LITEST_MOUSE, devname, NULL, NULL, NULL);
218
219 _unref_(libinput_device) *device = NULL;
220 _autofree_ char *seat1_name = NULL;
221 while (true) {
222 litest_wait_for_event_of_type(li, LIBINPUT_EVENT_DEVICE_ADDED);
223
224 _destroy_(libinput_event) *event = libinput_get_event(li);
225 struct libinput_device *d = libinput_event_get_device(event);
226 const char *name = libinput_device_get_name(d);
227 if (strendswith(name, devname)) {
228 device = libinput_device_ref(d);
229 struct libinput_seat *seat = libinput_device_get_seat(device);
230 seat1_name = safe_strdup(libinput_seat_get_logical_name(seat));
231 break;
232 }
233 }
234 litest_drain_events(li);
235
236 /* Changing the logical seat name will remove and re-add the device */
237 int rc = libinput_device_set_seat_logical_name(device, seat2_name);
238 litest_assert_int_eq(rc, 0);
239
240 while (true) {
241 litest_wait_for_event_of_type(li, LIBINPUT_EVENT_DEVICE_REMOVED);
242 _destroy_(libinput_event) *event = libinput_get_event(li);
243 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_REMOVED);
244 if (libinput_event_get_device(event) == device)
245 break;
246 }
247
248 _unref_(libinput_seat) *seat2 = NULL;
249 while (true) {
250 litest_wait_for_event_of_type(li, LIBINPUT_EVENT_DEVICE_ADDED);
251
252 _destroy_(libinput_event) *event = libinput_get_event(li);
253 litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_ADDED);
254 struct libinput_device *d = libinput_event_get_device(event);
255 const char *name = libinput_device_get_name(d);
256 if (strendswith(name, devname)) {
257 seat2 = libinput_device_get_seat(d);
258 libinput_seat_ref(seat2);
259 break;
260 }
261 }
262
263 litest_assert_str_ne(libinput_seat_get_logical_name(seat2), seat1_name);
264 litest_assert_str_eq(libinput_seat_get_logical_name(seat2), seat2_name);
265 }
266 END_TEST
267
268 START_TEST(udev_double_suspend)
269 {
270 struct libinput_event *event;
271 int fd;
272
273 _unref_(udev) *udev = udev_new();
274 litest_assert_notnull(udev);
275
276 _unref_(libinput) *li =
277 libinput_udev_create_context(&simple_interface, NULL, udev);
278 litest_assert_notnull(li);
279 litest_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
280
281 fd = libinput_get_fd(li);
282 litest_assert_int_ge(fd, 0);
283
284 /* expect at least one event */
285 litest_assert_int_ge(litest_dispatch(li), 0);
286 event = libinput_get_event(li);
287 litest_assert_notnull(event);
288
289 libinput_suspend(li);
290 libinput_suspend(li);
291 libinput_resume(li);
292
293 libinput_event_destroy(event);
294 }
295 END_TEST
296
297 START_TEST(udev_double_resume)
298 {
299 struct libinput_event *event;
300 int fd;
301
302 _unref_(udev) *udev = udev_new();
303 litest_assert_notnull(udev);
304
305 _unref_(libinput) *li =
306 libinput_udev_create_context(&simple_interface, NULL, udev);
307 litest_assert_notnull(li);
308 litest_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
309
310 fd = libinput_get_fd(li);
311 litest_assert_int_ge(fd, 0);
312
313 /* expect at least one event */
314 litest_assert_int_ge(litest_dispatch(li), 0);
315 event = libinput_get_event(li);
316 litest_assert_notnull(event);
317
318 libinput_suspend(li);
319 libinput_resume(li);
320 libinput_resume(li);
321
322 libinput_event_destroy(event);
323 }
324 END_TEST
325
326 static void
327 process_events_count_devices(struct libinput *li, int *device_count)
328 {
329 struct libinput_event *event;
330
331 while ((event = libinput_get_event(li))) {
332 switch (libinput_event_get_type(event)) {
333 case LIBINPUT_EVENT_DEVICE_ADDED:
334 (*device_count)++;
335 break;
336 case LIBINPUT_EVENT_DEVICE_REMOVED:
337 (*device_count)--;
338 break;
339 default:
340 break;
341 }
342 libinput_event_destroy(event);
343 }
344 }
345
346 START_TEST(udev_suspend_resume)
347 {
348 int fd;
349 int num_devices = 0;
350
351 _unref_(udev) *udev = udev_new();
352 litest_assert_notnull(udev);
353
354 _unref_(libinput) *li =
355 libinput_udev_create_context(&simple_interface, NULL, udev);
356 litest_assert_notnull(li);
357 litest_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
358
359 fd = libinput_get_fd(li);
360 litest_assert_int_ge(fd, 0);
361
362 /* Check that at least one device was discovered after creation. */
363 litest_assert_int_ge(litest_dispatch(li), 0);
364 process_events_count_devices(li, &num_devices);
365 litest_assert_int_gt(num_devices, 0);
366
367 /* Check that after a suspend, no devices are left. */
368 libinput_suspend(li);
369 litest_assert_int_ge(litest_dispatch(li), 0);
370 process_events_count_devices(li, &num_devices);
371 litest_assert_int_eq(num_devices, 0);
372
373 /* Check that after a resume, at least one device is discovered. */
374 libinput_resume(li);
375 litest_assert_int_ge(litest_dispatch(li), 0);
376 process_events_count_devices(li, &num_devices);
377 litest_assert_int_gt(num_devices, 0);
378 }
379 END_TEST
380
381 START_TEST(udev_resume_before_seat)
382 {
383 _unref_(udev) *udev = udev_new();
384 litest_assert_notnull(udev);
385
386 _unref_(libinput) *li =
387 libinput_udev_create_context(&simple_interface, NULL, udev);
388 litest_assert_notnull(li);
389
390 int rc = libinput_resume(li);
391 litest_assert_int_eq(rc, 0);
392 }
393 END_TEST
394
395 START_TEST(udev_suspend_resume_before_seat)
396 {
397 _unref_(udev) *udev = udev_new();
398 litest_assert_notnull(udev);
399
400 _unref_(libinput) *li =
401 libinput_udev_create_context(&simple_interface, NULL, udev);
402 litest_assert_notnull(li);
403
404 libinput_suspend(li);
405 int rc = libinput_resume(li);
406 litest_assert_int_eq(rc, 0);
407 }
408 END_TEST
409
410 START_TEST(udev_device_sysname)
411 {
412 struct libinput_event *ev;
413 struct libinput_device *device;
414 const char *sysname;
415
416 _unref_(udev) *udev = udev_new();
417 litest_assert_notnull(udev);
418
419 _unref_(libinput) *li =
420 libinput_udev_create_context(&simple_interface, NULL, udev);
421 litest_assert_notnull(li);
422 litest_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
423
424 litest_dispatch(li);
425
426 while ((ev = libinput_get_event(li))) {
427 if (libinput_event_get_type(ev) != LIBINPUT_EVENT_DEVICE_ADDED) {
428 libinput_event_destroy(ev);
429 continue;
430 }
431
432 device = libinput_event_get_device(ev);
433 sysname = libinput_device_get_sysname(device);
434 litest_assert_notnull(sysname);
435 litest_assert_int_gt(strlen(sysname), 1U);
436 litest_assert(strchr(sysname, '/') == NULL);
437 litest_assert(strstartswith(sysname, "event"));
438 libinput_event_destroy(ev);
439 }
440 }
441 END_TEST
442
443 START_TEST(udev_seat_recycle)
444 {
445 struct libinput_event *ev;
446 struct libinput_device *device;
447 struct libinput_seat *saved_seat = NULL;
448 struct libinput_seat *seat;
449 int data = 0;
450 int found = 0;
451 void *user_data;
452
453 _unref_(udev) *udev = udev_new();
454 litest_assert_notnull(udev);
455
456 _unref_(libinput) *li =
457 libinput_udev_create_context(&simple_interface, NULL, udev);
458 litest_assert_notnull(li);
459 litest_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
460
461 litest_dispatch(li);
462 while ((ev = libinput_get_event(li))) {
463 switch (libinput_event_get_type(ev)) {
464 case LIBINPUT_EVENT_DEVICE_ADDED:
465 if (saved_seat)
466 break;
467
468 device = libinput_event_get_device(ev);
469 litest_assert_notnull(device);
470 saved_seat = libinput_device_get_seat(device);
471 libinput_seat_set_user_data(saved_seat, &data);
472 libinput_seat_ref(saved_seat);
473 break;
474 default:
475 break;
476 }
477
478 libinput_event_destroy(ev);
479 }
480
481 litest_assert_notnull(saved_seat);
482
483 libinput_suspend(li);
484
485 litest_drain_events(li);
486
487 libinput_resume(li);
488
489 litest_dispatch(li);
490 while ((ev = libinput_get_event(li))) {
491 switch (libinput_event_get_type(ev)) {
492 case LIBINPUT_EVENT_DEVICE_ADDED:
493 device = libinput_event_get_device(ev);
494 litest_assert_notnull(device);
495
496 seat = libinput_device_get_seat(device);
497 user_data = libinput_seat_get_user_data(seat);
498 if (user_data == &data) {
499 found = 1;
500 litest_assert(seat == saved_seat);
501 }
502 break;
503 default:
504 break;
505 }
506
507 libinput_event_destroy(ev);
508 }
509
510 litest_assert(found == 1);
511 }
512 END_TEST
513
514 START_TEST(udev_path_add_device)
515 {
516 struct libinput_device *device;
517
518 _unref_(udev) *udev = udev_new();
519 litest_assert_notnull(udev);
520
521 _unref_(libinput) *li =
522 libinput_udev_create_context(&simple_interface, NULL, udev);
523 litest_assert_notnull(li);
524 litest_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
525
526 litest_set_log_handler_bug(li);
527 device = libinput_path_add_device(li, "/dev/input/event0");
528 litest_assert(device == NULL);
529 litest_restore_log_handler(li);
530 }
531 END_TEST
532
533 START_TEST(udev_path_remove_device)
534 {
535 struct libinput_device *device;
536 struct libinput_event *event;
537
538 _unref_(udev) *udev = udev_new();
539 litest_assert_notnull(udev);
540
541 _unref_(libinput) *li =
542 libinput_udev_create_context(&simple_interface, NULL, udev);
543 litest_assert_notnull(li);
544 litest_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
545 litest_dispatch(li);
546
547 litest_wait_for_event_of_type(li, LIBINPUT_EVENT_DEVICE_ADDED);
548 event = libinput_get_event(li);
549 device = libinput_event_get_device(event);
550 litest_assert_notnull(device);
551
552 /* no effect bug a bug log msg */
553 litest_set_log_handler_bug(li);
554 libinput_path_remove_device(device);
555 litest_restore_log_handler(li);
556
557 libinput_event_destroy(event);
558 }
559 END_TEST
560
561 START_TEST(udev_ignore_device)
562 {
563 struct libinput_device *device;
564 struct libinput_event *event;
565 const char *devname;
566
567 _destroy_(litest_device) *dev =
568 litest_create(LITEST_IGNORED_MOUSE, NULL, NULL, NULL, NULL);
569 devname = libevdev_get_name(dev->evdev);
570
571 _unref_(udev) *udev = udev_new();
572 litest_assert_notnull(udev);
573
574 _unref_(libinput) *li =
575 libinput_udev_create_context(&simple_interface, NULL, udev);
576 litest_assert_notnull(li);
577 litest_restore_log_handler(li);
578
579 litest_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
580 litest_dispatch(li);
581
582 event = libinput_get_event(li);
583 litest_assert_notnull(event);
584 while (event) {
585 if (libinput_event_get_type(event) == LIBINPUT_EVENT_DEVICE_ADDED) {
586 const char *name;
587
588 device = libinput_event_get_device(event);
589 name = libinput_device_get_name(device);
590 litest_assert_str_ne(devname, name);
591 }
592 libinput_event_destroy(event);
593 litest_dispatch(li);
594 event = libinput_get_event(li);
595 }
596 }
597 END_TEST
598
599 TEST_COLLECTION(udev)
600 {
601 /* clang-format off */
602 litest_add_no_device(udev_create_NULL);
603 litest_add_no_device(udev_create_seat0);
604 litest_add_no_device(udev_create_empty_seat);
605 litest_add_no_device(udev_create_seat_too_long);
606 litest_add_no_device(udev_set_user_data);
607
608 litest_add_no_device(udev_added_seat_default);
609 litest_add_no_device(udev_change_seat);
610
611 litest_add_for_device(udev_double_suspend, LITEST_SYNAPTICS_CLICKPAD_X220);
612 litest_add_for_device(udev_double_resume, LITEST_SYNAPTICS_CLICKPAD_X220);
613 litest_add_for_device(udev_suspend_resume, LITEST_SYNAPTICS_CLICKPAD_X220);
614 litest_add_for_device(udev_resume_before_seat, LITEST_SYNAPTICS_CLICKPAD_X220);
615 litest_add_for_device(udev_suspend_resume_before_seat, LITEST_SYNAPTICS_CLICKPAD_X220);
616 litest_add_for_device(udev_device_sysname, LITEST_SYNAPTICS_CLICKPAD_X220);
617 litest_add_for_device(udev_seat_recycle, LITEST_SYNAPTICS_CLICKPAD_X220);
618
619 litest_add_no_device(udev_path_add_device);
620 litest_add_for_device(udev_path_remove_device, LITEST_SYNAPTICS_CLICKPAD_X220);
621
622 litest_add_no_device(udev_ignore_device);
623 /* clang-format on */
624 }