Debian Codegraph @

test/litest-main.c

raw
1 /*
2 * Copyright © 2013 Red Hat, Inc.
3 * Copyright © 2013 Marcin Slusarz <[email protected]>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "config.h"
26
27 #include <dirent.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <fnmatch.h>
31 #include <getopt.h>
32 #include <libudev.h>
33 #include <poll.h>
34 #include <signal.h>
35 #include <stdarg.h>
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <sys/ptrace.h>
40 #include <sys/resource.h>
41 #include <sys/stat.h>
42 #include <sys/sysinfo.h>
43 #include <sys/timerfd.h>
44 #include <sys/types.h>
45 #include <sys/wait.h>
46 #include <time.h>
47 #include <unistd.h>
48
49 #include "linux/input.h"
50 #ifdef HAVE_LIBSYSTEMD
51 #include <systemd/sd-bus.h>
52 #endif
53 #ifdef __FreeBSD__
54 #include <termios.h>
55 #endif
56
57 #include <valgrind/valgrind.h>
58
59 #include "util-backtrace.h"
60 #include "util-files.h"
61 #include "util-libinput.h"
62
63 #include "builddir.h"
64 #include "libinput-util.h"
65 #include "litest-int.h"
66 #include "litest-runner.h"
67 #include "litest.h"
68 #include "quirks.h"
69
70 static struct list all_test_suites = LIST_INIT(all_test_suites); /* struct suite */
71
72 static int jobs;
73 extern bool use_colors;
74 extern bool in_debugger;
75 extern bool verbose;
76 extern bool run_deviceless;
77 extern struct suite *current_suite;
78
79 static bool
80 is_debugger_attached(void)
81 {
82 int status;
83 bool rc;
84 int pid = fork();
85
86 if (pid == -1)
87 return 0;
88
89 if (pid == 0) {
90 int ppid = getppid();
91 if (ptrace(PTRACE_ATTACH, ppid, NULL, 0) == 0) {
92 waitpid(ppid, NULL, 0);
93 ptrace(PTRACE_CONT, ppid, NULL, 0);
94 ptrace(PTRACE_DETACH, ppid, NULL, 0);
95 rc = false;
96 } else {
97 rc = true;
98 }
99 _exit(rc);
100 } else {
101 waitpid(pid, &status, 0);
102 rc = WEXITSTATUS(status);
103 }
104
105 return !!rc;
106 }
107
108 static void
109 litest_list_tests(struct list *tests)
110 {
111 struct suite *s;
112 const char *last_test_name = "<invalid>";
113 const char *last_dev_name = "<invalid>";
114
115 printf("groups:\n");
116 list_for_each(s, tests, node) {
117 struct test *t;
118 printf(" - group: \"%s\"\n", s->name);
119 printf(" tests:\n");
120 list_for_each(t, &s->tests, node) {
121 bool same_test = streq(last_test_name, t->name);
122 bool same_dev = streq(last_dev_name, t->devname);
123
124 if (!same_test) {
125 printf(" - name: \"%s\"\n", t->name);
126 printf(" devices:\n");
127 }
128
129 if (!same_test || !same_dev) {
130 last_test_name = t->name;
131 last_dev_name = t->devname;
132 printf(" - name: \"%s\"\n", t->devname);
133 }
134 }
135 }
136 }
137
138 extern const struct test_device __start_test_device_section, __stop_test_device_section;
139
140 static void
141 litest_init_test_devices(void)
142 {
143 const struct test_device *t;
144 const struct test_device *start = &__start_test_device_section;
145 const struct test_device *stop = &__stop_test_device_section;
146
147 for (t = start; t < stop; t++)
148 litest_add_test_device(
149 &t->device->node); /* NOLINT(clang-analyzer-security.ArrayBound)
150 */
151 }
152
153 extern const struct test_collection __start_test_collection_section,
154 __stop_test_collection_section;
155
156 static void
157 setup_tests(void)
158 {
159 /* Iterate through linker-provided section boundaries.
160 * These symbols mark the start and end of the test_collection_section. */
161 const struct test_collection *start = &__start_test_collection_section;
162 const struct test_collection *stop = &__stop_test_collection_section;
163 const struct test_collection *c;
164
165 for (c = start; c < stop;
166 c++) { /* NOLINT(clang-analyzer-security.ArrayBound) */
167 struct suite *s;
168 s = zalloc(sizeof(*s));
169 s->name = safe_strdup(
170 c->name); /* NOLINT(clang-analyzer-security.ArrayBound) */
171
172 list_init(&s->tests);
173 list_append(&all_test_suites, &s->node);
174
175 current_suite = s;
176 c->setup();
177 current_suite = NULL;
178 }
179 }
180
181 static int
182 check_device_access(void)
183 {
184 if (getuid() != 0) {
185 fprintf(stderr,
186 "%s must be run as root.\n",
187 program_invocation_short_name);
188 return 77;
189 }
190
191 if (access("/dev/uinput", F_OK) == -1 &&
192 access("/dev/input/uinput", F_OK) == -1) {
193 fprintf(stderr, "uinput device is missing, skipping tests.\n");
194 return 77;
195 }
196
197 return 0;
198 }
199
200 static void
201 litest_free_test_list(struct list *tests)
202 {
203 struct suite *s;
204
205 list_for_each_safe(s, tests, node) {
206 struct test *t;
207
208 list_for_each_safe(t, &s->tests, node) {
209 litest_test_parameters_unref(t->params);
210 free(t->name);
211 free(t->devname);
212 list_remove(&t->node);
213 free(t);
214 }
215
216 list_remove(&s->node);
217 free(s->name);
218 free(s);
219 }
220 }
221
222 int
223 main(int argc, char **argv)
224 {
225 enum litest_mode mode;
226 int rc;
227 const char *meson_testthreads;
228
229 use_colors = getenv("FORCE_COLOR") || isatty(STDERR_FILENO);
230 if (getenv("NO_COLOR"))
231 use_colors = false;
232
233 in_debugger = is_debugger_attached();
234 if (in_debugger) {
235 jobs = 0;
236 } else if ((meson_testthreads = getenv("MESON_TESTTHREADS")) == NULL ||
237 !safe_atoi(meson_testthreads, &jobs)) {
238 jobs = get_nprocs();
239 if (!RUNNING_ON_VALGRIND)
240 jobs *= 2;
241 }
242
243 if (getenv("LITEST_VERBOSE"))
244 verbose = true;
245
246 mode = litest_parse_argv(argc, argv, &jobs);
247 if (mode == LITEST_MODE_ERROR)
248 return EXIT_FAILURE;
249
250 litest_init_test_devices();
251
252 setup_tests();
253 if (list_empty(&all_test_suites)) {
254 fprintf(stderr, "Error: filters are too strict, no tests to run.\n");
255 return EXIT_FAILURE;
256 }
257
258 if (mode == LITEST_MODE_LIST) {
259 litest_list_tests(&all_test_suites);
260 return EXIT_SUCCESS;
261 }
262
263 if (!run_deviceless && (rc = check_device_access()) != 0)
264 return rc;
265
266 enum litest_runner_result result = litest_run(&all_test_suites, jobs);
267
268 litest_free_test_list(&all_test_suites);
269
270 switch (result) {
271 case LITEST_PASS:
272 return EXIT_SUCCESS;
273 case LITEST_SKIP:
274 return 77;
275 default:
276 return result;
277 }
278 }