Debian Codegraph @

test/test-plugins-lua.c

raw
1 /*
2 * Copyright © 2025 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 <fcntl.h>
27 #include <inttypes.h>
28 #include <valgrind/valgrind.h>
29
30 #include "util-files.h"
31 #include "util-strings.h"
32 #include "util-time.h"
33
34 #include "libinput.h"
35 #include "litest.h"
36
37 static char *
38 _litest_write_plugin(const char *tmpdir, const char *filename, const char *content)
39 {
40 static int counter = 0;
41 counter += 10;
42
43 char *path = strdup_printf("%s/%d-%s.lua", tmpdir, counter, filename);
44 _autoclose_ int fd = open(path, O_WRONLY | O_CREAT, 0644);
45 litest_assert_errno_success(fd);
46
47 if (content) {
48 write(fd, content, strlen(content));
49 fsync(fd);
50 }
51
52 return path;
53 }
54
55 #define litest_write_plugin(tmpdir_, content_) \
56 _litest_write_plugin(tmpdir_, __func__, content_)
57
58 START_TEST(lua_load_failure)
59 {
60 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
61 const char *lua = "asfasdk1298'..asdfasdf'123@2;asd"; /* invalid lua */
62 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
63 _litest_context_destroy_ struct libinput *li =
64 litest_create_context_with_plugindir(tmpdir->path);
65
66 litest_with_logcapture(li, capture) {
67 libinput_plugin_system_load_plugins(li,
68 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
69 litest_drain_events(li);
70
71 size_t index = 0;
72 litest_assert(
73 strv_find_substring(capture->errors, "Failed to load", &index));
74 litest_assert_str_in(path, capture->errors[index]);
75 }
76 }
77 END_TEST
78
79 enum content {
80 EMPTY,
81 NOTHING,
82 BASIC,
83 COMMENT,
84 DUPLICATE_CALL,
85 VERSION_NOT_A_TABLE,
86 MISSING_REGISTER,
87 };
88
89 START_TEST(lua_load_success_but_no_register)
90 {
91 enum content content = litest_test_param_get_i32(test_env->params, "content");
92
93 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
94
95 const char *lua = NULL;
96 switch (content) {
97 case DUPLICATE_CALL:
98 lua = "v1 = libinput:register({1})\np2 = libinput:register({2})\n";
99 break;
100 case VERSION_NOT_A_TABLE:
101 lua = "v1 = libinput:register(1)\n";
102 break;
103 case MISSING_REGISTER:
104 lua = "libinput:connect(\"new-evdev-device\", function(device) assert(false) end)\n";
105 break;
106 case BASIC:
107 lua = "a = 1 + 10";
108 break;
109 case COMMENT:
110 lua = "-- near-empty file";
111 break;
112 case NOTHING:
113 lua = "";
114 break;
115 case EMPTY:
116 break;
117 }
118
119 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
120 _litest_context_destroy_ struct libinput *li =
121 litest_create_context_with_plugindir(tmpdir->path);
122
123 litest_with_logcapture(li, capture) {
124 libinput_plugin_system_load_plugins(li,
125 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
126 switch (content) {
127 case VERSION_NOT_A_TABLE:
128 litest_assert_strv_substring(capture->errors,
129 "unloading after error");
130 break;
131 case DUPLICATE_CALL:
132 litest_assert_strv_substring(capture->errors,
133 "plugin already registered");
134 break;
135 default:
136 litest_assert_strv_substring(capture->errors,
137 "plugin never registered");
138 break;
139 }
140 }
141 }
142 END_TEST
143
144 START_TEST(lua_register_noop)
145 {
146 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
147 const char *lua = "libinput:register({1})";
148 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
149 _litest_context_destroy_ struct libinput *li =
150 litest_create_context_with_plugindir(tmpdir->path);
151 litest_with_logcapture(li, capture) {
152 libinput_plugin_system_load_plugins(li,
153 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
154 litest_drain_events(li);
155 litest_assert_logcapture_no_errors(capture);
156 }
157 }
158 END_TEST
159
160 START_TEST(lua_unregister_is_last)
161 {
162 const char *when = litest_test_param_get_string(test_env->params, "when");
163
164 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
165 _autofree_ char *lua = strdup_printf(
166 "libinput:register({1})\n"
167 "libinput:connect(\"new-evdev-device\", function(device)\n %s\n libinput:log_error(\"abort abort\")\nend)\n"
168 "%slibinput:log_error(\"must not happen\")",
169 streq(when, "connect") ? "libinput:unregister()" : "",
170 streq(when, "run") ? "libinput:unregister()\n" : "--");
171 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
172
173 _litest_context_destroy_ struct libinput *li =
174 litest_create_context_with_plugindir(tmpdir->path);
175 litest_with_logcapture(li, capture) {
176 libinput_plugin_system_load_plugins(li,
177 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
178 _destroy_(litest_device) *device = litest_add_device(li, LITEST_MOUSE);
179 litest_drain_events(li);
180 litest_assert_logcapture_no_errors(capture);
181 }
182 }
183 END_TEST
184
185 START_TEST(lua_test_logging)
186 {
187 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
188
189 enum libinput_log_priority priority =
190 litest_test_param_get_i32(test_env->params, "priority");
191
192 const char *lua = NULL;
193 switch (priority) {
194 case LIBINPUT_LOG_PRIORITY_DEBUG:
195 lua = "libinput:log_debug(\"deb-ug\");";
196 break;
197 case LIBINPUT_LOG_PRIORITY_INFO:
198 lua = "libinput:log_info(\"inf-o\");";
199 break;
200 case LIBINPUT_LOG_PRIORITY_ERROR:
201 lua = "libinput:log_error(\"err-or\");";
202 break;
203 default:
204 litest_assert_not_reached();
205 break;
206 }
207
208 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
209 _litest_context_destroy_ struct libinput *li =
210 litest_create_context_with_plugindir(tmpdir->path);
211 libinput_log_set_priority(li, priority);
212
213 litest_with_logcapture(li, capture) {
214 libinput_plugin_system_load_plugins(li,
215 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
216 litest_drain_events(li);
217
218 switch (priority) {
219 case LIBINPUT_LOG_PRIORITY_DEBUG:
220 litest_assert(
221 strv_find_substring(capture->debugs, "deb-ug", NULL));
222 litest_assert(
223 !strv_find_substring(capture->infos, "inf-o", NULL));
224 litest_assert(
225 !strv_find_substring(capture->errors, "err-or", NULL));
226 break;
227 case LIBINPUT_LOG_PRIORITY_INFO:
228 litest_assert(
229 !strv_find_substring(capture->debugs, "deb-ug", NULL));
230 litest_assert(
231 strv_find_substring(capture->infos, "inf-o", NULL));
232 litest_assert(
233 !strv_find_substring(capture->errors, "err-or", NULL));
234 break;
235 case LIBINPUT_LOG_PRIORITY_ERROR:
236 litest_assert(
237 !strv_find_substring(capture->debugs, "deb-ug", NULL));
238 litest_assert(
239 !strv_find_substring(capture->infos, "inf-o", NULL));
240 litest_assert(
241 strv_find_substring(capture->errors, "err-or", NULL));
242 break;
243 }
244 }
245 }
246 END_TEST
247
248 START_TEST(lua_test_evdev_global)
249 {
250 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
251
252 /* This is generated, if a few of them work the
253 * rest should work too */
254 const char *lua =
255 "libinput:register({1}); a = evdev.ABS_X\nb = evdev.REL_X\nc = evdev.KEY_A\nd = evdev.EV_SYN\n";
256 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
257 _litest_context_destroy_ struct libinput *li =
258 litest_create_context_with_plugindir(tmpdir->path);
259 litest_with_logcapture(li, capture) {
260 libinput_plugin_system_load_plugins(li,
261 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
262 litest_drain_events(li);
263
264 litest_assert_logcapture_no_errors(capture);
265 }
266 }
267 END_TEST
268
269 START_TEST(lua_test_libinput_now)
270 {
271 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
272 const char *lua = "libinput:log_error(\">>>\" .. libinput:now())";
273 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
274 _litest_context_destroy_ struct libinput *li =
275 litest_create_context_with_plugindir(tmpdir->path);
276 litest_with_logcapture(li, capture) {
277 libinput_plugin_system_load_plugins(li,
278 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
279 litest_drain_events(li);
280
281 usec_t test_now;
282 int rc = now_in_us(&test_now);
283 litest_assert_neg_errno_success(rc);
284
285 size_t index = 0;
286 litest_assert(strv_find_substring(capture->errors, ">>>", &index));
287 size_t nelem = 0;
288 _autostrvfree_ char **tokens =
289 strv_from_string(capture->errors[index], ">>>", &nelem);
290 litest_assert_int_eq(nelem, 2U);
291
292 uint64_t plugin_now = strtoull(tokens[1], NULL, 10);
293
294 litest_assert_int_le(plugin_now, usec_as_uint64_t(test_now));
295 /* Even a slow test runner hopefully doesn't take >300ms to get to the
296 * log print */
297 litest_assert_int_gt(
298 plugin_now,
299 usec_as_uint64_t(usec_sub(test_now, usec_from_millis(300))));
300 }
301 }
302 END_TEST
303
304 START_TEST(lua_test_libinput_timer)
305 {
306 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
307
308 const char *mode = litest_test_param_get_string(test_env->params, "mode");
309 bool reschedule = litest_test_param_get_bool(test_env->params, "reschedule");
310
311 _autofree_ char *timeout =
312 strdup_printf("%s%" PRIu64,
313 streq(mode, "absolute") ? "libinput:now() + " : "",
314 usec_as_uint64_t(usec_from_millis(100)));
315 _autofree_ char *reschedule_timeout =
316 strdup_printf("libinput:timer_set_%s(%s%" PRIu64 ")\n",
317 mode,
318 streq(mode, "absolute") ? "t + " : "",
319 usec_as_uint64_t(usec_from_millis(100)));
320 _autofree_ char *lua = strdup_printf(
321 "libinput:register({1})\n"
322 "libinput:connect(\"timer-expired\",\n"
323 " function(t)\n"
324 " libinput:log_error(\">>>\" .. t)\n"
325 " %s\n"
326 " end)\n"
327 "libinput:timer_set_%s(%s)\n",
328 reschedule ? reschedule_timeout : "",
329 mode,
330 timeout);
331
332 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
333 _litest_context_destroy_ struct libinput *li =
334 litest_create_context_with_plugindir(tmpdir->path);
335
336 litest_with_logcapture(li, capture) {
337 libinput_plugin_system_load_plugins(li,
338 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
339 litest_drain_events(li);
340
341 litest_assert_logcapture_no_errors(capture);
342
343 size_t nloops = reschedule ? 4 : 1;
344 for (size_t i = 0; i < nloops; i++) {
345 libinput_dispatch(li);
346 msleep(100);
347 libinput_dispatch(li);
348
349 usec_t test_now;
350 int rc = now_in_us(&test_now);
351 litest_assert_neg_errno_success(rc);
352
353 _autostrvfree_ char **msg = steal(&capture->errors);
354 litest_assert_ptr_notnull(msg);
355 size_t index;
356 litest_assert(strv_find_substring(msg, ">>>", &index));
357
358 size_t nelem = 0;
359 _autostrvfree_ char **tokens =
360 strv_from_string(msg[index], ">>>", &nelem);
361 litest_assert_int_eq(nelem, 2U);
362
363 uint64_t plugin_now = strtoull(tokens[1], NULL, 10);
364 litest_assert_int_le(plugin_now, usec_as_uint64_t(test_now));
365 /* Even a slow test runner hopefully doesn't take >300ms between
366 * dispatch and now_in_us */
367 litest_assert_int_gt(
368 plugin_now,
369 usec_as_uint64_t(
370 usec_sub(test_now, usec_from_millis(300))));
371 }
372
373 if (!reschedule) {
374 libinput_dispatch(li);
375 msleep(120);
376 libinput_dispatch(li);
377 }
378
379 litest_assert_logcapture_no_errors(capture);
380 }
381 }
382 END_TEST
383
384 enum connect_error {
385 BAD_TYPE,
386 TOO_FEW_ARGS,
387 TOO_MANY_ARGS,
388 };
389
390 START_TEST(lua_bad_connect)
391 {
392 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
393
394 const char *handler = litest_test_param_get_string(test_env->params, "handler");
395 enum connect_error error = litest_test_param_get_i32(test_env->params, "error");
396
397 const char *func = NULL;
398 switch (error) {
399 case BAD_TYPE:
400 func = "a";
401 break;
402 case TOO_FEW_ARGS:
403 func = "function(p) libinput:log_debug(\"few\"); end";
404 break;
405 case TOO_MANY_ARGS:
406 func = "function(p, a, b) libinput:log_debug(\"many\"); end";
407 break;
408 }
409
410 _autofree_ char *lua = strdup_printf(
411 "libinput:register({1})\n"
412 "a = 10\n"
413 "libinput:connect(\"%s\", %s)\n",
414 handler,
415 func);
416
417 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
418 _litest_context_destroy_ struct libinput *li =
419 litest_create_context_with_plugindir(tmpdir->path);
420 litest_with_logcapture(li, capture) {
421 libinput_plugin_system_load_plugins(li,
422 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
423 litest_drain_events(li);
424
425 switch (error) {
426 /* These don't trigger a lua erro so we just test they don't segfault us
427 */
428 case TOO_FEW_ARGS:
429 case TOO_MANY_ARGS:
430 litest_assert_logcapture_no_errors(capture);
431 break;
432 case BAD_TYPE:
433 litest_assert_strv_substring(capture->errors,
434 "bad argument #2 to 'connect'");
435 break;
436 }
437 }
438 }
439 END_TEST
440
441 START_TEST(lua_register_multiversions)
442 {
443
444 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
445 const char *lua =
446 "v = libinput:register({1, 3, 4, 10, 15})\nlibinput:log_info(\"VERSION:\" .. v)\n";
447 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
448 _litest_context_destroy_ struct libinput *li =
449 litest_create_context_with_plugindir(tmpdir->path);
450 if (libinput_log_get_priority(li) > LIBINPUT_LOG_PRIORITY_INFO)
451 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_INFO);
452
453 litest_with_logcapture(li, capture) {
454 libinput_plugin_system_load_plugins(li,
455 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
456 litest_drain_events(li);
457
458 litest_assert_logcapture_no_errors(capture);
459 litest_assert_strv_substring(capture->infos, "VERSION:1");
460 }
461 }
462 END_TEST
463
464 START_TEST(lua_allowed_functions)
465 {
466 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
467 /* This tests on the assumption that if some of these work,
468 * then the others we allow will work too. */
469 const char *lua =
470 "\n"
471 "libinput:register({1})\n"
472 "a = {10, 20}\n"
473 "for _, v in ipairs(a) do\n"
474 " v = v + 1\n"
475 "end\n"
476 "b = {foo = 1}"
477 "for k, v in pairs(a) do\n"
478 " v = v + 1\n"
479 "end\n"
480 "print(math.maxinteger)\n"
481 "table.sort({10, 2, 4})\n"
482 "assert(true)\n"
483 "";
484 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
485 _litest_context_destroy_ struct libinput *li =
486 litest_create_context_with_plugindir(tmpdir->path);
487 litest_with_logcapture(li, capture) {
488 libinput_plugin_system_load_plugins(li,
489 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
490 litest_drain_events(li);
491
492 litest_assert_logcapture_no_errors(capture);
493 }
494 }
495 END_TEST
496
497 START_TEST(lua_disallowed_functions)
498 {
499 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
500 /* This tests on the assumption that if some of these work,
501 * then the others we allow will work too. */
502 const char *lua =
503 "\n"
504 "libinput:register({1})\n"
505 "assert(io == nil)\n"
506 "assert(require == nil)\n"
507 "assert(rawget == nil)\n"
508 "assert(rawset == nil)\n"
509 "assert(setfenv == nil)\n"
510 "assert(getmetatable == nil)\n"
511 "assert(setmetatable == nil)\n"
512 "assert(package == nil)\n"
513 "assert(os == nil)\n"
514 "assert(debug == nil)\n"
515 "";
516 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
517 _litest_context_destroy_ struct libinput *li =
518 litest_create_context_with_plugindir(tmpdir->path);
519 litest_with_logcapture(li, capture) {
520 libinput_plugin_system_load_plugins(li,
521 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
522 litest_drain_events(li);
523
524 litest_assert_logcapture_no_errors(capture);
525 }
526 }
527 END_TEST
528
529 START_TEST(lua_gc_not_accessible)
530 {
531 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
532 const char *lua =
533 "libinput:register({1})\n"
534 "assert(libinput.__gc == nil)\n"
535 "function check_device_gc(device)\n"
536 " assert(device.__gc == nil)\n"
537 " libinput:log_info(\"gc_not_accessible: ok\")\n"
538 "end\n"
539 "libinput:connect(\"new-evdev-device\", check_device_gc)\n";
540
541 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
542 _litest_context_destroy_ struct libinput *li =
543 litest_create_context_with_plugindir(tmpdir->path);
544 if (libinput_log_get_priority(li) > LIBINPUT_LOG_PRIORITY_INFO)
545 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_INFO);
546
547 litest_with_logcapture(li, capture) {
548 libinput_plugin_system_load_plugins(li,
549 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
550 litest_drain_events(li);
551
552 _destroy_(litest_device) *device = litest_add_device(li, LITEST_MOUSE);
553 litest_drain_events(li);
554
555 litest_assert_logcapture_no_errors(capture);
556 litest_assert_strv_substring(capture->infos, "gc_not_accessible: ok");
557 }
558 }
559 END_TEST
560
561 START_TEST(lua_frame_handler)
562 {
563 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
564 const char *lua =
565 "libinput:register({1})\n"
566 "function frame_handler(_, frame, timestamp)\n"
567 " libinput:log_info(\"T:\" .. timestamp)\n"
568 " for _, e in ipairs(frame) do\n"
569 " libinput:log_info(\"E:\" .. e.usage .. \":\" .. e.value)\n"
570 " end\n"
571 "end\n"
572 "libinput:connect(\"new-evdev-device\", function(device) device:connect(\"evdev-frame\", frame_handler) end)\n";
573
574 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
575 _litest_context_destroy_ struct libinput *li =
576 litest_create_context_with_plugindir(tmpdir->path);
577 if (libinput_log_get_priority(li) > LIBINPUT_LOG_PRIORITY_INFO)
578 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_INFO);
579
580 litest_with_logcapture(li, capture) {
581 libinput_plugin_system_load_plugins(li,
582 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
583 litest_drain_events(li);
584
585 _destroy_(litest_device) *device = litest_add_device(li, LITEST_MOUSE);
586 litest_drain_events(li);
587
588 usec_t before, after;
589 now_in_us(&before);
590 msleep(1);
591 litest_button_click_debounced(device, li, BTN_LEFT, 1);
592 litest_button_click_debounced(device, li, BTN_LEFT, 0);
593 litest_assert_logcapture_no_errors(capture);
594 msleep(1);
595 now_in_us(&after);
596
597 /* EV_KEY << 16 | BTN_LEFT -> 65808 */
598
599 litest_assert_strv_substring(capture->infos, "E:65808:1");
600 litest_assert_strv_substring(capture->infos, "E:65808:0");
601 /* SYN_REPORT shouldn't show up in the frame */
602 litest_assert(!strv_find_substring(capture->infos, "E:0:0", NULL));
603
604 size_t idx;
605 litest_assert(strv_find_substring(capture->infos, "T:", &idx));
606
607 _autofree_ char *str = safe_strdup(capture->infos[idx]);
608 for (size_t i = 0; str[i]; i++) {
609 if (str[i] == '\n') {
610 str[i] = '\0';
611 break;
612 }
613 }
614
615 size_t nelems;
616 _autostrvfree_ char **split = strv_from_string(str, ":", &nelems);
617 litest_assert_int_gt(nelems, 1U);
618 char *strtime = split[nelems - 1];
619 uint64_t timestamp = 0;
620 litest_assert(safe_atou64(strtime, &timestamp));
621 litest_assert_int_gt(timestamp, usec_as_uint64_t(before));
622 litest_assert_int_lt(timestamp, usec_as_uint64_t(after));
623 }
624 }
625 END_TEST
626
627 START_TEST(lua_device_info)
628 {
629 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
630 const char *lua =
631 "libinput:register({1})\n"
632 "function info_printer(device)\n"
633 " local info = device:info()\n"
634 " libinput:log_info(\"BUS:\" .. info.bustype)\n"
635 " libinput:log_info(\"VID:\" .. info.vid)\n"
636 " libinput:log_info(\"PID:\" .. info.pid)\n"
637 "end\n"
638 "libinput:connect(\"new-evdev-device\", info_printer)\n";
639
640 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
641 _litest_context_destroy_ struct libinput *li =
642 litest_create_context_with_plugindir(tmpdir->path);
643 if (libinput_log_get_priority(li) > LIBINPUT_LOG_PRIORITY_INFO)
644 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_INFO);
645
646 litest_with_logcapture(li, capture) {
647 libinput_plugin_system_load_plugins(li,
648 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
649 litest_drain_events(li);
650
651 _destroy_(litest_device) *device = litest_add_device(li, LITEST_MOUSE);
652 litest_drain_events(li);
653
654 litest_assert_strv_substring(capture->infos, "BUS:3");
655 litest_assert_strv_substring(capture->infos, "VID:6127");
656 litest_assert_strv_substring(capture->infos, "PID:24601");
657 }
658 }
659 END_TEST
660
661 START_TEST(lua_set_absinfo)
662 {
663 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
664 const char *lua =
665 "libinput:register({1})\n"
666 "function absinfo_setter(device)\n"
667 " local absinfos = device:absinfos()\n"
668 " for u, a in pairs(absinfos) do\n"
669 " libinput:log_info(\"A:\" .. u .. \":\" .. a.minimum .. \":\" .. a.maximum .. \":\" .. a.resolution .. \":\" .. a.fuzz .. \":\" .. a.flat)\n"
670 " end\n"
671 " device:set_absinfo(evdev.ABS_X, { minimum = 0, maximum = 1000, resolution = 100 })\n"
672 " device:set_absinfo(evdev.ABS_Y, { minimum = 0, maximum = 200, resolution = 10 })\n"
673 " device:set_absinfo(evdev.ABS_MT_POSITION_X, { minimum = 0, maximum = 1000, resolution = 100 })\n"
674 " device:set_absinfo(evdev.ABS_MT_POSITION_Y, { minimum = 0, maximum = 200, resolution = 10 })\n"
675 "end\n"
676 "libinput:connect(\"new-evdev-device\", absinfo_setter)\n";
677
678 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
679 _litest_context_destroy_ struct libinput *li =
680 litest_create_context_with_plugindir(tmpdir->path);
681 if (libinput_log_get_priority(li) > LIBINPUT_LOG_PRIORITY_INFO)
682 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_INFO);
683
684 litest_with_logcapture(li, capture) {
685 libinput_plugin_system_load_plugins(li,
686 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
687 litest_drain_events(li);
688
689 _destroy_(litest_device) *device =
690 litest_add_device(li, LITEST_GENERIC_MULTITOUCH_SCREEN);
691 litest_drain_events(li);
692
693 litest_assert_logcapture_no_errors(capture);
694
695 for (int code = 0; code <= ABS_MAX; code++) {
696 if (!libevdev_has_event_code(device->evdev, EV_ABS, code)) {
697 _autofree_ char *prefix =
698 strdup_printf("A:%u", (EV_ABS << 16) | code);
699 litest_assert(!strv_find_substring(capture->infos,
700 prefix,
701 NULL));
702 continue;
703 }
704
705 const struct input_absinfo *absinfo =
706 libevdev_get_abs_info(device->evdev, code);
707 _autofree_ char *message = strdup_printf("A:%u:%d:%d:%d:%d:%d",
708 (EV_ABS << 16) | code,
709 absinfo->minimum,
710 absinfo->maximum,
711 absinfo->resolution,
712 absinfo->fuzz,
713 absinfo->flat);
714 litest_assert_strv_substring(capture->infos, message);
715 }
716
717 /* If the absinfo worked, our device is 10x20mm big */
718 double w, h;
719 libinput_device_get_size(device->libinput_device, &w, &h);
720 litest_assert_double_eq(w, 10.0);
721 litest_assert_double_eq(h, 20.0);
722 }
723 }
724 END_TEST
725
726 START_TEST(lua_enable_disable_evdev_usage)
727 {
728 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
729 /* We have two plugins here, one that enables codes and one that prints
730 * the frame.
731 *
732 * The first plugin also inserts a REL_Z event into the frame since we
733 * can't send that through the kernel.
734 */
735 const char *lua1 =
736 "libinput:register({1})\n"
737 "function frame_handler(_, frame, timestamp)\n"
738 " table.insert(frame, { usage = evdev.REL_Z, value = 3 })\n"
739 " return frame\n"
740 "end\n"
741 "function enabler(device)\n"
742 " device:enable_evdev_usage(evdev.REL_Z)\n"
743 " device:enable_evdev_usage(evdev.BTN_STYLUS2)\n"
744 " device:disable_evdev_usage(evdev.REL_WHEEL)\n"
745 " device:connect(\"evdev-frame\", frame_handler)\n"
746 "end\n"
747 "libinput:connect(\"new-evdev-device\", enabler)\n";
748
749 const char *lua2 =
750 "libinput:register({1})\n"
751 "function frame_handler(_, frame, timestamp)\n"
752 " libinput:log_info(\"frame\")\n"
753 " for _, e in ipairs(frame) do\n"
754 " libinput:log_info(\"E:\" .. e.usage .. \":\" .. e.value)\n"
755 " end\n"
756 "end\n"
757 "function f(device)\n"
758 " libinput:log_info(\"F: \" .. device:name())\n"
759 " device:connect(\"evdev-frame\", frame_handler)\n"
760 "end\n"
761 "libinput:connect(\"new-evdev-device\", f)\n";
762
763 _autofree_ char *p1 = litest_write_plugin(tmpdir->path, lua1);
764 _autofree_ char *p2 = litest_write_plugin(tmpdir->path, lua2);
765 _litest_context_destroy_ struct libinput *li =
766 litest_create_context_with_plugindir(tmpdir->path);
767 if (libinput_log_get_priority(li) > LIBINPUT_LOG_PRIORITY_INFO)
768 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_INFO);
769
770 litest_with_logcapture(li, capture) {
771 libinput_plugin_system_load_plugins(li,
772 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
773 litest_drain_events(li);
774
775 _destroy_(litest_device) *device = litest_add_device(li, LITEST_MOUSE);
776 litest_drain_events(li);
777
778 /* We enabled that one ourselves */
779 litest_assert(
780 libinput_device_pointer_has_button(device->libinput_device,
781 BTN_STYLUS2));
782
783 litest_assert_logcapture_no_errors(capture);
784
785 litest_event(device, EV_REL, REL_X, 1);
786 litest_event(device, EV_REL, REL_Y, 2);
787 litest_event(device, EV_REL, REL_WHEEL, -1);
788 litest_event(device, EV_SYN, SYN_REPORT, 0);
789 litest_dispatch(li);
790
791 litest_assert_logcapture_no_errors(capture);
792
793 litest_assert_strv_substring(capture->infos, "E:131072:1");
794 litest_assert_strv_substring(capture->infos, "E:131073:2");
795 litest_assert_strv_substring(capture->infos, "E:131074:3");
796 litest_assert(!strv_find_substring(capture->infos, "E:131080", NULL));
797 }
798 }
799 END_TEST
800
801 START_TEST(lua_udev_properties)
802 {
803 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
804 const char *lua =
805 "libinput:register({1})\n"
806 "function prop_printer(device)\n"
807 " local properties = device:udev_properties()\n"
808 " for k, v in pairs(properties) do\n"
809 " libinput:log_info(k .. \"=\" .. v)\n"
810 " end\n"
811 "end\n"
812 "libinput:connect(\"new-evdev-device\", prop_printer)\n";
813
814 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
815 _litest_context_destroy_ struct libinput *li =
816 litest_create_context_with_plugindir(tmpdir->path);
817 if (libinput_log_get_priority(li) > LIBINPUT_LOG_PRIORITY_INFO)
818 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_INFO);
819
820 litest_with_logcapture(li, capture) {
821 libinput_plugin_system_load_plugins(li,
822 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
823 litest_drain_events(li);
824
825 enum litest_device_type which =
826 litest_test_param_get_i32(test_env->params, "which");
827 _destroy_(litest_device) *device = litest_add_device(li, which);
828 litest_drain_events(li);
829
830 litest_assert_logcapture_no_errors(capture);
831
832 switch (which) {
833 case LITEST_TRACKPOINT:
834 litest_assert_strv_substring(capture->infos,
835 "ID_INPUT_POINTINGSTICK=1");
836 _fallthrough_;
837 case LITEST_MOUSE:
838 litest_assert_strv_substring(capture->infos,
839 "ID_INPUT_MOUSE=1");
840 break;
841 case LITEST_GENERIC_MULTITOUCH_SCREEN:
842 litest_assert_strv_substring(capture->infos,
843 "ID_INPUT_TOUCHSCREEN=1");
844 break;
845 default:
846 litest_assert_not_reached();
847 break;
848 }
849 litest_assert(!strv_find_substring(capture->infos,
850 "ID_INPUT_WIDTH_MM",
851 NULL));
852 litest_assert(!strv_find_substring(capture->infos,
853 "ID_INPUT_WIDTH_MM",
854 NULL));
855 }
856 }
857 END_TEST
858
859 START_TEST(lua_append_prepend_frame)
860 {
861 bool append = litest_test_param_get_bool(test_env->params, "append");
862 bool in_timer = litest_test_param_get_bool(test_env->params, "in_timer");
863 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
864 _autofree_ char *lua = strdup_printf(
865 "libinput:register({1})\n"
866 "mydev = nil\n"
867 "function frame_handler(device, frame, timestamp)\n"
868 " device:%s_frame({{ usage = evdev.BTN_LEFT, value = 1}})\n" /* commented
869 out
870 if
871 !in_timer
872 */
873 " return nil\n"
874 "end\n"
875 "libinput:connect(\"new-evdev-device\", function(device)\n"
876 " mydev = device\n"
877 " %sdevice:connect(\"evdev-frame\", frame_handler)\n"
878 " %slibinput:timer_set_relative(200000)\n" /* commented out if
879 !in_timer */
880 "end)\n"
881 "function timer_expired(t)\n"
882 " mydev:%s_frame({{ usage = evdev.BTN_LEFT, value = 1 }})\n"
883 "end\n"
884 "libinput:connect(\"timer-expired\", timer_expired)\n",
885 append ? "append" : "prepend",
886 in_timer ? "-- " : "",
887 in_timer ? "" : "-- ",
888 append ? "append" : "prepend");
889 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
890 _litest_context_destroy_ struct libinput *li =
891 litest_create_context_with_plugindir(tmpdir->path);
892 libinput_plugin_system_load_plugins(li, LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
893 litest_drain_events(li);
894
895 _destroy_(litest_device) *device = litest_add_device(li, LITEST_MOUSE);
896 litest_drain_events(li);
897 msleep(10); /* trigger the timer, if any */
898 litest_dispatch(li);
899
900 if (in_timer) {
901 litest_assert_button_event(li, BTN_LEFT, LIBINPUT_BUTTON_STATE_PRESSED);
902 }
903
904 litest_event(device, EV_REL, REL_X, 1);
905 litest_event(device, EV_REL, REL_Y, 2);
906 litest_event(device, EV_SYN, SYN_REPORT, 0);
907 litest_dispatch(li);
908 litest_timeout_debounce(li);
909 litest_dispatch(li);
910
911 if (!in_timer && !append) {
912 litest_assert_button_event(li, BTN_LEFT, LIBINPUT_BUTTON_STATE_PRESSED);
913 }
914
915 _destroy_(libinput_event) *ev = libinput_get_event(li);
916 litest_is_motion_event(ev);
917
918 if (!in_timer && append) {
919 litest_assert_button_event(li, BTN_LEFT, LIBINPUT_BUTTON_STATE_PRESSED);
920 }
921
922 litest_assert_empty_queue(li);
923 }
924 END_TEST
925
926 START_TEST(lua_ignore_unsupported_codes)
927 {
928 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
929 _autofree_ char *lua = strdup_printf(
930 "libinput:register({1})\n"
931 "function frame_handler(device, frame, timestamp)\n"
932 " local events = {}\n"
933 " for _, v in ipairs(frame) do\n"
934 " table.insert(events, { usage = v.usage, value = v.value })\n"
935 " end\n"
936 " table.insert(events, { usage = evdev.ABS_X, value = 1000 })\n"
937 " table.insert(events, { usage = evdev.ABS_Y, value = 100 })\n"
938 " table.insert(events, { usage = evdev.BTN_BACK, value = 1 })\n"
939 " table.insert(events, { usage = evdev.BTN_LEFT, value = 1 })\n" /* this
940 one actually exists */
941 " return events\n"
942 "end\n"
943 "libinput:connect(\"new-evdev-device\", function(device)\n"
944 " device:connect(\"evdev-frame\", frame_handler)\n"
945 "end)\n");
946 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
947 _litest_context_destroy_ struct libinput *li =
948 litest_create_context_with_plugindir(tmpdir->path);
949 libinput_plugin_system_load_plugins(li, LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
950 litest_drain_events(li);
951
952 _destroy_(litest_device) *device = litest_add_device(li, LITEST_MOUSE);
953 litest_drain_events(li);
954
955 litest_event(device, EV_REL, REL_X, 1);
956 litest_event(device, EV_REL, REL_Y, 2);
957 litest_event(device, EV_SYN, SYN_REPORT, 0);
958 litest_dispatch(li);
959 litest_timeout_debounce(li);
960 litest_dispatch(li);
961
962 _destroy_(libinput_event) *ev = libinput_get_event(li);
963 litest_is_motion_event(ev);
964 litest_assert_button_event(li, BTN_LEFT, LIBINPUT_BUTTON_STATE_PRESSED);
965
966 litest_assert_empty_queue(li);
967 }
968 END_TEST
969
970 enum when {
971 DEVICE_NEW,
972 FIRST_FRAME,
973 };
974
975 START_TEST(lua_disable_button_debounce)
976 {
977 enum when when = litest_test_param_get_i32(test_env->params, "when");
978 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
979 _autofree_ char *lua = strdup_printf(
980 "libinput:register({1})\n"
981 "function frame_handler(device, _, _)\n"
982 " device:disable_feature(\"button-debouncing\")\n"
983 "end\n"
984 "function new_device(device)\n"
985 " %s device:disable_feature(\"button-debouncing\")\n"
986 " %s device:connect(\"evdev-frame\", frame_handler)\n"
987 "end\n"
988 "libinput:connect(\"new-evdev-device\", new_device)\n",
989 when == DEVICE_NEW ? "" : "--",
990 when == FIRST_FRAME ? "" : "--");
991 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
992 _litest_context_destroy_ struct libinput *li =
993 litest_create_context_with_plugindir(tmpdir->path);
994
995 if (libinput_log_get_priority(li) > LIBINPUT_LOG_PRIORITY_DEBUG)
996 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
997
998 litest_with_logcapture(li, capture) {
999 libinput_plugin_system_load_plugins(li,
1000 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
1001 litest_drain_events(li);
1002
1003 _destroy_(litest_device) *dev = litest_add_device(li, LITEST_MOUSE);
1004 litest_drain_events(li);
1005
1006 litest_disable_middleemu(dev);
1007
1008 litest_event(dev, EV_KEY, BTN_LEFT, 1);
1009 litest_event(dev, EV_SYN, SYN_REPORT, 0);
1010 litest_event(dev, EV_KEY, BTN_LEFT, 0);
1011 litest_event(dev, EV_SYN, SYN_REPORT, 0);
1012 litest_event(dev, EV_KEY, BTN_LEFT, 1);
1013 litest_event(dev, EV_SYN, SYN_REPORT, 0);
1014 litest_event(dev, EV_KEY, BTN_LEFT, 0);
1015 litest_event(dev, EV_SYN, SYN_REPORT, 0);
1016 litest_timeout_debounce(li);
1017
1018 litest_assert_button_event(li, BTN_LEFT, LIBINPUT_BUTTON_STATE_PRESSED);
1019 litest_assert_button_event(li,
1020 BTN_LEFT,
1021 LIBINPUT_BUTTON_STATE_RELEASED);
1022 litest_assert_button_event(li, BTN_LEFT, LIBINPUT_BUTTON_STATE_PRESSED);
1023 litest_assert_button_event(li,
1024 BTN_LEFT,
1025 LIBINPUT_BUTTON_STATE_RELEASED);
1026 litest_assert_empty_queue(li);
1027
1028 litest_assert_strv_substring(capture->debugs,
1029 "disabled button debouncing on request");
1030 }
1031 }
1032 END_TEST
1033
1034 START_TEST(lua_disable_touchpad_jump_detection)
1035 {
1036 enum when when = litest_test_param_get_i32(test_env->params, "when");
1037 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
1038 _autofree_ char *lua = strdup_printf(
1039 "libinput:register({1})\n"
1040 "function frame_handler(dev, f, ts)\n"
1041 " dev:disable_feature(\"touchpad-jump-detection\")\n"
1042 "end\n"
1043 "function new_device(device)\n"
1044 " %sdevice:disable_feature(\"touchpad-jump-detection\")\n"
1045 " %sdevice:connect(\"evdev-frame\", frame_handler)\n"
1046 "end\n"
1047 "libinput:connect(\"new-evdev-device\", new_device)\n",
1048 when == DEVICE_NEW ? "" : "-- ",
1049 when == FIRST_FRAME ? "" : "-- ");
1050
1051 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
1052 _litest_context_destroy_ struct libinput *li =
1053 litest_create_context_with_plugindir(tmpdir->path);
1054
1055 if (libinput_log_get_priority(li) > LIBINPUT_LOG_PRIORITY_DEBUG)
1056 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
1057
1058 litest_with_logcapture(li, capture) {
1059 libinput_plugin_system_load_plugins(li,
1060 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
1061 litest_drain_events(li);
1062
1063 _destroy_(litest_device) *dev =
1064 litest_add_device(li, LITEST_SYNAPTICS_RMI4);
1065 litest_drain_events(li);
1066
1067 litest_touch_down(dev, 0, 40, 50);
1068 litest_touch_move(dev, 0, 80, 80);
1069 litest_touch_move(dev, 0, 90, 90);
1070 litest_touch_up(dev, 0);
1071
1072 litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
1073 litest_assert_empty_queue(li);
1074
1075 litest_assert(!strv_find_substring(capture->infos,
1076 "Touch jump detected and discarded",
1077 NULL));
1078 }
1079 }
1080 END_TEST
1081
1082 START_TEST(lua_disable_wheel_debouncing)
1083 {
1084 enum when when = litest_test_param_get_i32(test_env->params, "when");
1085 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
1086 _autofree_ char *lua = strdup_printf(
1087 "libinput:register({1})\n"
1088 "function frame_handler(device, _, _)\n"
1089 " device:disable_feature(\"wheel-debouncing\")\n"
1090 "end\n"
1091 "function new_device(device)\n"
1092 " %s device:disable_feature(\"wheel-debouncing\")\n"
1093 " %s device:connect(\"evdev-frame\", frame_handler)\n"
1094 "end\n"
1095 "libinput:connect(\"new-evdev-device\", new_device)\n",
1096 when == DEVICE_NEW ? "" : "--",
1097 when == FIRST_FRAME ? "" : "--");
1098 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
1099 _litest_context_destroy_ struct libinput *li =
1100 litest_create_context_with_plugindir(tmpdir->path);
1101
1102 if (libinput_log_get_priority(li) > LIBINPUT_LOG_PRIORITY_DEBUG)
1103 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
1104
1105 litest_with_logcapture(li, capture) {
1106 libinput_plugin_system_load_plugins(li,
1107 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
1108
1109 _destroy_(litest_device) *dev = litest_add_device(li, LITEST_MOUSE);
1110 litest_drain_events(li);
1111
1112 for (size_t i = 0; i < 4; i++) {
1113 /* Send a small wheel events - when debouncing is disabled, they
1114 * should all be delivered immediately without delay */
1115 litest_event(dev, EV_REL, REL_WHEEL_HI_RES, 10);
1116 litest_event(dev, EV_SYN, SYN_REPORT, 0);
1117 litest_dispatch(li);
1118
1119 _destroy_(libinput_event) *ev = libinput_get_event(li);
1120 auto ptrev = litest_is_axis_event(
1121 ev,
1122 LIBINPUT_EVENT_POINTER_SCROLL_WHEEL,
1123 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1124 0);
1125 double v120 = libinput_event_pointer_get_scroll_value_v120(
1126 ptrev,
1127 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1128 litest_assert_double_eq(v120, -10);
1129 }
1130 litest_assert_empty_queue(li);
1131 litest_assert_strv_substring(capture->debugs,
1132 "disabled wheel debouncing on request");
1133 }
1134 }
1135 END_TEST
1136
1137 START_TEST(lua_remove_plugin_on_timeout)
1138 {
1139 enum when when = litest_test_param_get_i32(test_env->params, "when");
1140 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
1141 _autofree_ char *lua = strdup_printf(
1142 "libinput:register({1})\n"
1143 "function infinite_loop(device)\n"
1144 " local i = 0\n"
1145 " while true do\n"
1146 " i = i + 1\n"
1147 " end\n"
1148 "end\n"
1149 "function new_device(device)\n"
1150 " device:connect(\"evdev-frame\", infinite_loop)\n"
1151 "end\n"
1152 "%s libinput:connect(\"new-evdev-device\", infinite_loop)\n"
1153 "%s libinput:connect(\"new-evdev-device\", new_device)\n",
1154 when == DEVICE_NEW ? "" : "--",
1155 when == FIRST_FRAME ? "" : "--");
1156
1157 _autofree_ char *path = litest_write_plugin(tmpdir->path, lua);
1158 _litest_context_destroy_ struct libinput *li =
1159 litest_create_context_with_plugindir(tmpdir->path);
1160 _destroy_(litest_device) *dev = NULL;
1161
1162 litest_with_logcapture(li, capture) {
1163 libinput_plugin_system_load_plugins(li,
1164 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
1165 litest_drain_events(li);
1166
1167 usec_t before = usec_from_now();
1168
1169 dev = litest_add_device(li, LITEST_MOUSE);
1170 litest_drain_events(li);
1171
1172 if (when == FIRST_FRAME) {
1173 litest_event(dev, EV_KEY, BTN_LEFT, 1);
1174 litest_event(dev, EV_SYN, SYN_REPORT, 0);
1175 litest_dispatch(li);
1176 litest_event(dev, EV_KEY, BTN_LEFT, 0);
1177 litest_event(dev, EV_SYN, SYN_REPORT, 0);
1178 litest_dispatch(li);
1179 }
1180
1181 usec_t after = usec_from_now();
1182 usec_t elapsed = usec_delta(after, before);
1183
1184 /* verify the timeout kicked in around ~1 second (but less than 2s) */
1185 litest_assert_int_ge(usec_to_millis(elapsed), 1000U);
1186 if (!RUNNING_ON_VALGRIND)
1187 litest_assert_int_lt(usec_to_millis(elapsed), 2000U);
1188
1189 size_t index = 0;
1190 litest_assert(strv_find_substring(capture->errors,
1191 "Plugin execution timeout",
1192 &index));
1193 litest_assert_str_in("exceeded 1 second", capture->errors[index]);
1194
1195 litest_assert(strv_find_substring(capture->errors,
1196 "unloading after error",
1197 NULL));
1198 }
1199 /* device events should go through now */
1200 litest_event(dev, EV_KEY, BTN_LEFT, 1);
1201 litest_event(dev, EV_SYN, SYN_REPORT, 0);
1202 litest_dispatch(li);
1203 litest_assert_button_event(li, BTN_LEFT, LIBINPUT_BUTTON_STATE_PRESSED);
1204 }
1205 END_TEST
1206
1207 /* Pre-compiled Lua 5.4 bytecode for the following source:
1208 *
1209 * libinput:register({1})
1210 * libinput:connect("new-evdev-device", function(device)
1211 * libinput:log_info("loaded from binary lua file")
1212 * end)
1213 *
1214 * To regenerate:
1215 * luac5.4 -o /dev/stdout /tmp/plugin.lua | xxd -i
1216 */
1217 static const unsigned char binary_lua_plugin[] = {
1218 0x1b, 0x4c, 0x75, 0x61, 0x54, 0x00, 0x19, 0x93, 0x0d, 0x0a, 0x1a, 0x0a, 0x04,
1219 0x08, 0x08, 0x78, 0x56, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1220 0x00, 0x00, 0x28, 0x77, 0x40, 0x01, 0x9b, 0x40, 0x74, 0x65, 0x73, 0x74, 0x2f,
1221 0x31, 0x30, 0x2d, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x2d, 0x70, 0x6c, 0x75,
1222 0x67, 0x69, 0x6e, 0x2e, 0x6c, 0x75, 0x61, 0x80, 0x80, 0x00, 0x01, 0x04, 0x8e,
1223 0x51, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x14, 0x80, 0x00, 0x01, 0x13,
1224 0x01, 0x00, 0x01, 0x52, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x80, 0x4e, 0x01,
1225 0x01, 0x00, 0x44, 0x00, 0x03, 0x01, 0x0b, 0x00, 0x00, 0x00, 0x14, 0x80, 0x00,
1226 0x02, 0x03, 0x81, 0x01, 0x00, 0xcf, 0x01, 0x00, 0x00, 0x44, 0x00, 0x04, 0x01,
1227 0x46, 0x00, 0x01, 0x01, 0x84, 0x04, 0x89, 0x6c, 0x69, 0x62, 0x69, 0x6e, 0x70,
1228 0x75, 0x74, 0x04, 0x89, 0x72, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x04,
1229 0x88, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x04, 0x91, 0x6e, 0x65, 0x77,
1230 0x2d, 0x65, 0x76, 0x64, 0x65, 0x76, 0x2d, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65,
1231 0x81, 0x01, 0x00, 0x00, 0x81, 0x80, 0x8d, 0x8f, 0x01, 0x00, 0x04, 0x85, 0x8b,
1232 0x00, 0x00, 0x00, 0x94, 0x80, 0x01, 0x01, 0x83, 0x01, 0x01, 0x00, 0xc4, 0x00,
1233 0x03, 0x01, 0xc7, 0x00, 0x01, 0x00, 0x83, 0x04, 0x89, 0x6c, 0x69, 0x62, 0x69,
1234 0x6e, 0x70, 0x75, 0x74, 0x04, 0x89, 0x6c, 0x6f, 0x67, 0x5f, 0x69, 0x6e, 0x66,
1235 0x6f, 0x04, 0x9c, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x64, 0x20, 0x66, 0x72, 0x6f,
1236 0x6d, 0x20, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x20, 0x6c, 0x75, 0x61, 0x20,
1237 0x66, 0x69, 0x6c, 0x65, 0x81, 0x00, 0x00, 0x00, 0x80, 0x85, 0x01, 0x00, 0x00,
1238 0x00, 0x01, 0x80, 0x81, 0x87, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x80, 0x85,
1239 0x81, 0x85, 0x5f, 0x45, 0x4e, 0x56, 0x8e, 0x01, 0x0b, 0x00, 0x00, 0x00, 0x00,
1240 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0xfe, 0x02, 0x80, 0x80, 0x81, 0x85, 0x5f,
1241 0x45, 0x4e, 0x56,
1242 };
1243
1244 START_TEST(lua_reject_precompiled_files)
1245 {
1246 _destroy_(tmpdir) *tmpdir = tmpdir_create(NULL);
1247
1248 /* Write the binary bytecode to a .lua file in the tmpdir.
1249 * Binary (pre-compiled) Lua files must be rejected by the
1250 * plugin loader for security reasons. */
1251 _autofree_ char *path = strdup_printf("%s/10-binary-plugin.lua", tmpdir->path);
1252 _autoclose_ int fd = open(path, O_WRONLY | O_CREAT, 0644);
1253 litest_assert_errno_success(fd);
1254
1255 ssize_t written = write(fd, binary_lua_plugin, sizeof(binary_lua_plugin));
1256 litest_assert_int_eq((int)written, (int)sizeof(binary_lua_plugin));
1257 fsync(fd);
1258
1259 _litest_context_destroy_ struct libinput *li =
1260 litest_create_context_with_plugindir(tmpdir->path);
1261
1262 litest_with_logcapture(li, capture) {
1263 libinput_plugin_system_load_plugins(li,
1264 LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
1265 litest_drain_events(li);
1266
1267 size_t index = 0;
1268 litest_assert(
1269 strv_find_substring(capture->errors, "Failed to load", &index));
1270 litest_assert_str_in(path, capture->errors[index]);
1271 }
1272 }
1273 END_TEST
1274
1275 TEST_COLLECTION(lua)
1276 {
1277 /* clang-format off */
1278 litest_add_no_device(lua_load_failure);
1279 litest_add_no_device(lua_reject_precompiled_files);
1280 litest_with_parameters(params,
1281 "content", 'I', 6,
1282 litest_named_i32(EMPTY),
1283 litest_named_i32(BASIC),
1284 litest_named_i32(COMMENT),
1285 litest_named_i32(DUPLICATE_CALL),
1286 litest_named_i32(MISSING_REGISTER),
1287 litest_named_i32(VERSION_NOT_A_TABLE)) {
1288 litest_add_parametrized_no_device(lua_load_success_but_no_register,
1289 params);
1290 }
1291 litest_add_no_device(lua_register_noop);
1292 litest_with_parameters(params, "when", 's', 2, "run", "connect") {
1293 litest_add_parametrized_no_device(lua_unregister_is_last, params);
1294 }
1295 litest_add_no_device(lua_test_evdev_global);
1296 litest_add_no_device(lua_test_libinput_now);
1297 litest_with_parameters(params,
1298 "mode", 's', 2, "absolute", "relative",
1299 "reschedule", 'b') {
1300 litest_add_parametrized_no_device(lua_test_libinput_timer, params);
1301 }
1302
1303 litest_with_parameters(params,
1304 "priority", 'I', 3,
1305 litest_named_i32(LIBINPUT_LOG_PRIORITY_DEBUG),
1306 litest_named_i32(LIBINPUT_LOG_PRIORITY_INFO),
1307 litest_named_i32(LIBINPUT_LOG_PRIORITY_ERROR)) {
1308 litest_add_parametrized_no_device(lua_test_logging, params);
1309 }
1310
1311 litest_with_parameters(params,
1312 "handler", 's', 2, "new-evdev-device", "timer-expired",
1313 "error", 'I', 3,
1314 litest_named_i32(BAD_TYPE),
1315 litest_named_i32(TOO_FEW_ARGS),
1316 litest_named_i32(TOO_MANY_ARGS)) {
1317 litest_add_parametrized_no_device(lua_bad_connect, params);
1318 }
1319
1320 litest_add_no_device(lua_register_multiversions);
1321 litest_add_no_device(lua_allowed_functions);
1322 litest_add_no_device(lua_disallowed_functions);
1323 litest_add_no_device(lua_gc_not_accessible);
1324
1325 litest_add_no_device(lua_frame_handler);
1326 litest_add_no_device(lua_device_info);
1327 litest_add_no_device(lua_set_absinfo);
1328 litest_add_no_device(lua_enable_disable_evdev_usage);
1329 litest_add_no_device(lua_ignore_unsupported_codes);
1330
1331 litest_with_parameters(params,
1332 "which", 'I', 3,
1333 litest_named_i32(LITEST_MOUSE),
1334 litest_named_i32(LITEST_TRACKPOINT),
1335 litest_named_i32(LITEST_GENERIC_MULTITOUCH_SCREEN)) {
1336 litest_add_parametrized_no_device(lua_udev_properties, params);
1337 }
1338
1339 litest_with_parameters(params, "append", 'b', "in_timer", 'b') {
1340 litest_add_parametrized_no_device(lua_append_prepend_frame, params);
1341 }
1342
1343 litest_with_parameters(params, "when", 'I', 2,
1344 litest_named_i32(DEVICE_NEW),
1345 litest_named_i32(FIRST_FRAME)) {
1346 litest_add_parametrized_no_device(lua_disable_button_debounce, params);
1347 litest_add_parametrized_no_device(lua_disable_touchpad_jump_detection, params);
1348 litest_add_parametrized_no_device(lua_disable_wheel_debouncing, params);
1349
1350 litest_add_parametrized_no_device(lua_remove_plugin_on_timeout, params);
1351 }
1352 /* clang-format on */
1353 }