1
0

test-udev.c 16 KB

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