forked from wayland-tablet/libinput
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
litest.c
4267 lines (3610 loc) · 96.6 KB
/
litest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2013 Red Hat, Inc.
* Copyright © 2013 Marcin Slusarz <marcin.slusarz@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include <check.h>
#include <dirent.h>
#include <errno.h>
#include <libgen.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <getopt.h>
#include <poll.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <unistd.h>
#include "linux/input.h"
#include <sys/ptrace.h>
#include <sys/resource.h>
#include <sys/timerfd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <libudev.h>
#if HAVE_LIBSYSTEMD
#include <systemd/sd-bus.h>
#endif
#ifdef __FreeBSD__
#include <termios.h>
#endif
#include <valgrind/valgrind.h>
#include "litest.h"
#include "litest-int.h"
#include "libinput-util.h"
#include "quirks.h"
#include "builddir.h"
#include <linux/kd.h>
#define UDEV_RULES_D "/run/udev/rules.d"
#define UDEV_RULE_PREFIX "99-litest-"
#define UDEV_MODEL_QUIRKS_RULE_FILE UDEV_RULES_D \
"/91-litest-model-quirks-REMOVEME-XXXXXX.rules"
#define UDEV_TEST_DEVICE_RULE_FILE UDEV_RULES_D \
"/91-litest-test-device-REMOVEME-XXXXXXX.rules"
#define UDEV_DEVICE_GROUPS_FILE UDEV_RULES_D \
"/80-libinput-device-groups-litest-XXXXXX.rules"
static int jobs = 8;
static bool in_debugger = false;
static bool verbose = false;
static bool run_deviceless = false;
static bool use_system_rules_quirks = false;
const char *filter_test = NULL;
const char *filter_device = NULL;
const char *filter_group = NULL;
static struct quirks_context *quirks_context;
struct created_file {
struct list link;
char *path;
};
struct list created_files_list; /* list of all files to remove at the end of
the test run */
static void litest_init_udev_rules(struct list *created_files_list);
static void litest_remove_udev_rules(struct list *created_files_list);
enum quirks_setup_mode {
QUIRKS_SETUP_USE_SRCDIR,
QUIRKS_SETUP_ONLY_DEVICE,
QUIRKS_SETUP_FULL,
};
static void litest_setup_quirks(struct list *created_files_list,
enum quirks_setup_mode mode);
/* defined for the litest selftest */
#ifndef LITEST_DISABLE_BACKTRACE_LOGGING
#define litest_log(...) fprintf(stderr, __VA_ARGS__)
#define litest_vlog(format_, args_) vfprintf(stderr, format_, args_)
#else
#define litest_log(...) { /* __VA_ARGS__ */ }
#define litest_vlog(...) { /* __VA_ARGS__ */ }
#endif
static void
litest_backtrace(void)
{
#if HAVE_GSTACK
pid_t parent, child;
int pipefd[2];
if (RUNNING_ON_VALGRIND) {
litest_log(" Using valgrind, omitting backtrace\n");
return;
}
if (pipe(pipefd) == -1)
return;
parent = getpid();
child = fork();
if (child == 0) {
char pid[8];
close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
sprintf(pid, "%d", parent);
execlp("gstack", "gstack", pid, NULL);
exit(errno);
}
/* parent */
char buf[1024];
int status, nread;
close(pipefd[1]);
waitpid(child, &status, 0);
status = WEXITSTATUS(status);
if (status != 0) {
litest_log("ERROR: gstack failed, no backtrace available: %s\n",
strerror(status));
} else {
litest_log("\nBacktrace:\n");
while ((nread = read(pipefd[0], buf, sizeof(buf) - 1)) > 0) {
buf[nread] = '\0';
litest_log("%s", buf);
}
litest_log("\n");
}
close(pipefd[0]);
#endif
}
LIBINPUT_ATTRIBUTE_PRINTF(5, 6)
__attribute__((noreturn))
void
litest_fail_condition(const char *file,
int line,
const char *func,
const char *condition,
const char *message,
...)
{
litest_log("FAILED: %s\n", condition);
if (message) {
va_list args;
va_start(args, message);
litest_vlog(message, args);
va_end(args);
}
litest_log("in %s() (%s:%d)\n", func, file, line);
litest_backtrace();
abort();
}
__attribute__((noreturn))
void
litest_fail_comparison_int(const char *file,
int line,
const char *func,
const char *operator,
int a,
int b,
const char *astr,
const char *bstr)
{
litest_log("FAILED COMPARISON: %s %s %s\n", astr, operator, bstr);
litest_log("Resolved to: %d %s %d\n", a, operator, b);
litest_log("in %s() (%s:%d)\n", func, file, line);
litest_backtrace();
abort();
}
__attribute__((noreturn))
void
litest_fail_comparison_ptr(const char *file,
int line,
const char *func,
const char *comparison)
{
litest_log("FAILED COMPARISON: %s\n", comparison);
litest_log("in %s() (%s:%d)\n", func, file, line);
litest_backtrace();
abort();
}
struct test {
struct list node;
char *name;
char *devname;
void *func;
void *setup;
void *teardown;
struct range range;
bool deviceless;
};
struct suite {
struct list node;
struct list tests;
char *name;
};
static struct litest_device *current_device;
struct litest_device *litest_current_device(void)
{
return current_device;
}
void litest_set_current_device(struct litest_device *device)
{
current_device = device;
}
void litest_generic_device_teardown(void)
{
litest_delete_device(current_device);
current_device = NULL;
}
struct list devices;
static struct list all_tests;
static inline void
litest_system(const char *command)
{
int ret;
ret = system(command);
if (ret == -1) {
litest_abort_msg("Failed to execute: %s", command);
} else if (WIFEXITED(ret)) {
if (WEXITSTATUS(ret))
litest_abort_msg("'%s' failed with %d",
command,
WEXITSTATUS(ret));
} else if (WIFSIGNALED(ret)) {
litest_abort_msg("'%s' terminated with signal %d",
command,
WTERMSIG(ret));
}
}
static void
litest_reload_udev_rules(void)
{
litest_system("udevadm control --reload-rules");
}
static void
litest_add_tcase_for_device(struct suite *suite,
const char *funcname,
void *func,
const struct litest_test_device *dev,
const struct range *range)
{
struct test *t;
t = zalloc(sizeof(*t));
t->name = safe_strdup(funcname);
t->devname = safe_strdup(dev->shortname);
t->func = func;
t->setup = dev->setup;
t->teardown = dev->teardown ?
dev->teardown : litest_generic_device_teardown;
if (range)
t->range = *range;
list_insert(&suite->tests, &t->node);
}
static void
litest_add_tcase_no_device(struct suite *suite,
void *func,
const char *funcname,
const struct range *range)
{
struct test *t;
const char *test_name = funcname;
if (filter_device &&
fnmatch(filter_device, test_name, 0) != 0)
return;
t = zalloc(sizeof(*t));
t->name = safe_strdup(test_name);
t->devname = safe_strdup("no device");
t->func = func;
if (range)
t->range = *range;
t->setup = NULL;
t->teardown = NULL;
list_insert(&suite->tests, &t->node);
}
static void
litest_add_tcase_deviceless(struct suite *suite,
void *func,
const char *funcname,
const struct range *range)
{
struct test *t;
const char *test_name = funcname;
if (filter_device &&
fnmatch(filter_device, test_name, 0) != 0)
return;
t = zalloc(sizeof(*t));
t->deviceless = true;
t->name = safe_strdup(test_name);
t->devname = safe_strdup("deviceless");
t->func = func;
if (range)
t->range = *range;
t->setup = NULL;
t->teardown = NULL;
list_insert(&suite->tests, &t->node);
}
static struct suite *
get_suite(const char *name)
{
struct suite *s;
list_for_each(s, &all_tests, node) {
if (streq(s->name, name))
return s;
}
s = zalloc(sizeof(*s));
s->name = safe_strdup(name);
list_init(&s->tests);
list_insert(&all_tests, &s->node);
return s;
}
static void
litest_add_tcase(const char *suite_name,
const char *funcname,
void *func,
enum litest_device_feature required,
enum litest_device_feature excluded,
const struct range *range)
{
struct suite *suite;
bool added = false;
litest_assert(required >= LITEST_DEVICELESS);
litest_assert(excluded >= LITEST_DEVICELESS);
if (filter_test &&
fnmatch(filter_test, funcname, 0) != 0)
return;
if (filter_group &&
fnmatch(filter_group, suite_name, 0) != 0)
return;
suite = get_suite(suite_name);
if (required == LITEST_DEVICELESS &&
excluded == LITEST_DEVICELESS) {
litest_add_tcase_deviceless(suite, func, funcname, range);
added = true;
} else if (required == LITEST_DISABLE_DEVICE &&
excluded == LITEST_DISABLE_DEVICE) {
litest_add_tcase_no_device(suite, func, funcname, range);
added = true;
} else if (required != LITEST_ANY || excluded != LITEST_ANY) {
struct litest_test_device *dev;
list_for_each(dev, &devices, node) {
if (dev->features & LITEST_IGNORED)
continue;
if (filter_device &&
fnmatch(filter_device, dev->shortname, 0) != 0)
continue;
if ((dev->features & required) != required ||
(dev->features & excluded) != 0)
continue;
litest_add_tcase_for_device(suite,
funcname,
func,
dev,
range);
added = true;
}
} else {
struct litest_test_device *dev;
list_for_each(dev, &devices, node) {
if (dev->features & LITEST_IGNORED)
continue;
if (filter_device &&
fnmatch(filter_device, dev->shortname, 0) != 0)
continue;
litest_add_tcase_for_device(suite,
funcname,
func,
dev,
range);
added = true;
}
}
if (!added &&
filter_test == NULL &&
filter_device == NULL &&
filter_group == NULL) {
fprintf(stderr, "Test '%s' does not match any devices. Aborting.\n", funcname);
abort();
}
}
void
_litest_add_no_device(const char *name, const char *funcname, void *func)
{
_litest_add(name, funcname, func, LITEST_DISABLE_DEVICE, LITEST_DISABLE_DEVICE);
}
void
_litest_add_ranged_no_device(const char *name,
const char *funcname,
void *func,
const struct range *range)
{
_litest_add_ranged(name,
funcname,
func,
LITEST_DISABLE_DEVICE,
LITEST_DISABLE_DEVICE,
range);
}
void
_litest_add_deviceless(const char *name,
const char *funcname,
void *func)
{
_litest_add_ranged(name,
funcname,
func,
LITEST_DEVICELESS,
LITEST_DEVICELESS,
NULL);
}
void
_litest_add(const char *name,
const char *funcname,
void *func,
enum litest_device_feature required,
enum litest_device_feature excluded)
{
_litest_add_ranged(name,
funcname,
func,
required,
excluded,
NULL);
}
void
_litest_add_ranged(const char *name,
const char *funcname,
void *func,
enum litest_device_feature required,
enum litest_device_feature excluded,
const struct range *range)
{
litest_add_tcase(name, funcname, func, required, excluded, range);
}
void
_litest_add_for_device(const char *name,
const char *funcname,
void *func,
enum litest_device_type type)
{
_litest_add_ranged_for_device(name, funcname, func, type, NULL);
}
void
_litest_add_ranged_for_device(const char *name,
const char *funcname,
void *func,
enum litest_device_type type,
const struct range *range)
{
struct suite *s;
struct litest_test_device *dev;
bool device_filtered = false;
litest_assert(type < LITEST_NO_DEVICE);
if (filter_test &&
fnmatch(filter_test, funcname, 0) != 0)
return;
if (filter_group &&
fnmatch(filter_group, name, 0) != 0)
return;
s = get_suite(name);
list_for_each(dev, &devices, node) {
if (filter_device &&
fnmatch(filter_device, dev->shortname, 0) != 0) {
device_filtered = true;
continue;
}
if (dev->type == type) {
litest_add_tcase_for_device(s,
funcname,
func,
dev,
range);
return;
}
}
/* only abort if no filter was set, that's a bug */
if (!device_filtered)
litest_abort_msg("Invalid test device type\n");
}
LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
static void
litest_log_handler(struct libinput *libinput,
enum libinput_log_priority pri,
const char *format,
va_list args)
{
static int is_tty = -1;
const char *priority = NULL;
const char *color;
if (is_tty == -1)
is_tty = isatty(STDERR_FILENO);
switch(pri) {
case LIBINPUT_LOG_PRIORITY_INFO:
priority = "info ";
color = ANSI_HIGHLIGHT;
break;
case LIBINPUT_LOG_PRIORITY_ERROR:
priority = "error";
color = ANSI_BRIGHT_RED;
break;
case LIBINPUT_LOG_PRIORITY_DEBUG:
priority = "debug";
color = ANSI_NORMAL;
break;
default:
abort();
}
if (!is_tty)
color = "";
else if (strstr(format, "tap:"))
color = ANSI_BLUE;
else if (strstr(format, "thumb state:"))
color = ANSI_YELLOW;
else if (strstr(format, "button state:"))
color = ANSI_MAGENTA;
else if (strstr(format, "touch-size:") ||
strstr(format, "pressure:"))
color = ANSI_GREEN;
else if (strstr(format, "palm:") ||
strstr(format, "thumb:"))
color = ANSI_CYAN;
else if (strstr(format, "edge-scroll:"))
color = ANSI_BRIGHT_GREEN;
fprintf(stderr, "%slitest %s ", color, priority);
vfprintf(stderr, format, args);
if (is_tty)
fprintf(stderr, ANSI_NORMAL);
if (strstr(format, "client bug: ") ||
strstr(format, "libinput bug: ")) {
/* valgrind is too slow and some of our offsets are too
* short, don't abort if during a valgrind run we get a
* negative offset */
if (!RUNNING_ON_VALGRIND ||
!strstr(format, "offset negative"))
litest_abort_msg("libinput bug triggered, aborting.\n");
}
if (strstr(format, "Touch jump detected and discarded")) {
litest_abort_msg("libinput touch jump triggered, aborting.\n");
}
}
static char *
litest_init_device_udev_rules(struct litest_test_device *dev);
static void
litest_init_all_device_udev_rules(struct list *created_files)
{
struct litest_test_device *dev;
list_for_each(dev, &devices, node) {
char *udev_file;
udev_file = litest_init_device_udev_rules(dev);
if (udev_file) {
struct created_file *file = zalloc(sizeof(*file));
file->path = udev_file;
list_insert(created_files, &file->link);
}
}
}
static int
open_restricted(const char *path, int flags, void *userdata)
{
int fd = open(path, flags);
return fd < 0 ? -errno : fd;
}
static void
close_restricted(int fd, void *userdata)
{
close(fd);
}
struct libinput_interface interface = {
.open_restricted = open_restricted,
.close_restricted = close_restricted,
};
static void
litest_signal(int sig)
{
struct created_file *f, *tmp;
list_for_each_safe(f, tmp, &created_files_list, link) {
list_remove(&f->link);
unlink(f->path);
rmdir(f->path);
/* in the sighandler, we can't free */
}
if (fork() == 0) {
/* child, we can run system() */
litest_reload_udev_rules();
exit(0);
}
exit(1);
}
static inline void
litest_setup_sighandler(int sig)
{
struct sigaction act, oact;
int rc;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, sig);
act.sa_flags = 0;
act.sa_handler = litest_signal;
rc = sigaction(sig, &act, &oact);
litest_assert_int_ne(rc, -1);
}
static void
litest_free_test_list(struct list *tests)
{
struct suite *s, *snext;
list_for_each_safe(s, snext, tests, node) {
struct test *t, *tnext;
list_for_each_safe(t, tnext, &s->tests, node) {
free(t->name);
free(t->devname);
list_remove(&t->node);
free(t);
}
list_remove(&s->node);
free(s->name);
free(s);
}
}
LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
static inline void
quirk_log_handler(struct libinput *unused,
enum libinput_log_priority priority,
const char *format,
va_list args)
{
if (priority < LIBINPUT_LOG_PRIORITY_ERROR)
return;
vfprintf(stderr, format, args);
}
static int
litest_run_suite(struct list *tests, int which, int max, int error_fd)
{
int failed = 0;
SRunner *sr = NULL;
struct suite *s;
struct test *t;
int count = -1;
struct name {
struct list node;
char *name;
};
struct name *n, *tmp;
struct list testnames;
const char *data_path;
data_path = getenv("LIBINPUT_QUIRKS_DIR");
if (!data_path)
data_path = LIBINPUT_QUIRKS_DIR;
quirks_context = quirks_init_subsystem(data_path,
NULL,
quirk_log_handler,
NULL,
QLOG_LIBINPUT_LOGGING);
/* Check just takes the suite/test name pointers but doesn't strdup
* them - we have to keep them around */
list_init(&testnames);
/* For each test, create one test suite with one test case, then
add it to the test runner. The only benefit suites give us in
check is that we can filter them, but our test runner has a
--filter-group anyway. */
list_for_each(s, tests, node) {
list_for_each(t, &s->tests, node) {
Suite *suite;
TCase *tc;
char *sname, *tname;
/* We run deviceless tests as part of the normal
* test suite runner, just in case. Filtering
* all the other ones out just for the case where
* we can't run the full runner.
*/
if (run_deviceless && !t->deviceless)
continue;
count = (count + 1) % max;
if (max != 1 && (count % max) != which)
continue;
xasprintf(&sname,
"%s:%s:%s",
s->name,
t->name,
t->devname);
litest_assert_ptr_notnull(sname);
n = zalloc(sizeof(*n));
n->name = sname;
list_insert(&testnames, &n->node);
xasprintf(&tname,
"%s:%s",
t->name,
t->devname);
litest_assert_ptr_notnull(tname);
n = zalloc(sizeof(*n));
n->name = tname;
list_insert(&testnames, &n->node);
tc = tcase_create(tname);
tcase_add_checked_fixture(tc,
t->setup,
t->teardown);
if (t->range.upper != t->range.lower)
tcase_add_loop_test(tc,
t->func,
t->range.lower,
t->range.upper);
else
tcase_add_test(tc, t->func);
suite = suite_create(sname);
suite_add_tcase(suite, tc);
if (!sr)
sr = srunner_create(suite);
else
srunner_add_suite(sr, suite);
}
}
if (!sr)
goto out;
srunner_run_all(sr, CK_ENV);
failed = srunner_ntests_failed(sr);
if (failed) {
TestResult **trs;
trs = srunner_failures(sr);
for (int i = 0; i < failed; i++) {
char tname[256];
char *c = tname;
/* tr_tcname is in the form "suite:testcase", let's
* convert this to "suite(testcase)" to make
* double-click selection in the terminal a bit
* easier. */
snprintf(tname, sizeof(tname), "%s)", tr_tcname(trs[i]));
if ((c = index(c, ':')))
*c = '(';
dprintf(error_fd,
":: Failure: %s:%d: %s\n",
tr_lfile(trs[i]),
tr_lno(trs[i]),
tname);
}
free(trs);
}
srunner_free(sr);
out:
list_for_each_safe(n, tmp, &testnames, node) {
free(n->name);
free(n);
}
quirks_context_unref(quirks_context);
return failed;
}
static int
litest_fork_subtests(struct list *tests, int max_forks)
{
int failed = 0;
int status;
pid_t pid;
int f;
int pipes[max_forks];
for (f = 0; f < max_forks; f++) {
int rc;
int pipefd[2];
rc = pipe2(pipefd, O_NONBLOCK|O_NONBLOCK);
assert(rc != -1);
pid = fork();
if (pid == 0) {
close(pipefd[0]);
failed = litest_run_suite(tests,
f,
max_forks,
pipefd[1]);
litest_free_test_list(&all_tests);
exit(failed);
/* child always exits here */
} else {
pipes[f] = pipefd[0];
close(pipefd[1]);
}
}
/* parent process only */
while (wait(&status) != -1 && errno != ECHILD) {
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
failed = 1;
}
for (f = 0; f < max_forks; f++) {
char buf[1024] = {0};
int rc;
while ((rc = read(pipes[f], buf, sizeof(buf) - 1)) > 0) {
buf[rc] = '\0';
fprintf(stderr, "%s", buf);
}
close(pipes[f]);
}
return failed;
}
static inline int
inhibit(void)
{
int lock_fd = -1;
#if HAVE_LIBSYSTEMD
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_message *m = NULL;
sd_bus *bus = NULL;
int rc;
if (run_deviceless)
return -1;
rc = sd_bus_open_system(&bus);
if (rc != 0) {
fprintf(stderr, "Warning: inhibit failed: %s\n", strerror(-rc));
goto out;
}
rc = sd_bus_call_method(bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"Inhibit",
&error,
&m,
"ssss",
"handle-lid-switch:handle-power-key:handle-suspend-key:handle-hibernate-key",
"libinput test-suite runner",
"testing in progress",
"block");
if (rc < 0) {
fprintf(stderr, "Warning: inhibit failed: %s\n", error.message);
goto out;
}
rc = sd_bus_message_read(m, "h", &lock_fd);
if (rc < 0) {
fprintf(stderr, "Warning: inhibit failed: %s\n", strerror(-rc));
goto out;
}
lock_fd = dup(lock_fd);
out:
sd_bus_error_free(&error);
sd_bus_message_unref(m);
sd_bus_close(bus);
sd_bus_unref(bus);
#endif
return lock_fd;
}
static inline int
litest_run(int argc, char **argv)
{
int failed = 0;
int inhibit_lock_fd;
list_init(&created_files_list);
if (list_empty(&all_tests)) {
fprintf(stderr,
"Error: filters are too strict, no tests to run.\n");