1 /*
2 * Copyright 2019 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include "config.h"
27
28 #include <string.h>
29 #include <assert.h>
30 #include <libudev.h>
31 #include <unistd.h>
32 #include <sys/file.h>
33 #include <errno.h>
34
35 #include "shared/helpers.h"
36 #include "weston-test-fixture-compositor.h"
37 #include "weston.h"
38 #include "test-config.h"
39
40 struct prog_args {
41 int argc;
42 char **argv;
43 char **saved;
44 int alloc;
45 };
46
47 static void
prog_args_init(struct prog_args * p)48 prog_args_init(struct prog_args *p)
49 {
50 memset(p, 0, sizeof(*p));
51 }
52
53 static void
prog_args_take(struct prog_args * p,char * arg)54 prog_args_take(struct prog_args *p, char *arg)
55 {
56 assert(arg);
57
58 if (p->argc == p->alloc) {
59 p->alloc += 10;
60 p->argv = realloc(p->argv, sizeof(char *) * p->alloc);
61 assert(p->argv);
62 }
63
64 p->argv[p->argc++] = arg;
65 }
66
67 /*
68 * The program to be executed will trample on argv, hence we need a copy to
69 * be able to free all our args.
70 */
71 static void
prog_args_save(struct prog_args * p)72 prog_args_save(struct prog_args *p)
73 {
74 assert(p->saved == NULL);
75
76 p->saved = calloc(p->argc, sizeof(char *));
77 assert(p->saved);
78
79 memcpy(p->saved, p->argv, sizeof(char *) * p->argc);
80 }
81
82 static void
prog_args_fini(struct prog_args * p)83 prog_args_fini(struct prog_args *p)
84 {
85 int i;
86
87 assert(p->saved);
88
89 for (i = 0; i < p->argc; i++)
90 free(p->saved[i]);
91 free(p->saved);
92 free(p->argv);
93 prog_args_init(p);
94 }
95
96 static char *
get_lock_path(void)97 get_lock_path(void)
98 {
99 const char *env_path, *suffix;
100 char *lock_path;
101
102 suffix = "weston-test-suite-drm-lock";
103 env_path = getenv("XDG_RUNTIME_DIR");
104 if (!env_path) {
105 fprintf(stderr, "Failed to compute lock file path. " \
106 "XDG_RUNTIME_DIR is not set.\n");
107 return NULL;
108 }
109
110 if (asprintf(&lock_path, "%s/%s", env_path, suffix) == -1)
111 return NULL;
112
113 return lock_path;
114 }
115
116 /*
117 * DRM-backend tests need to be run sequentially, since there can only be
118 * one user at a time with master status in a DRM KMS device. Since the
119 * test suite runs the tests in parallel, there's a mechanism to assure
120 * only one DRM-backend test is running at a time: tests of this type keep
121 * waiting until they acquire a lock (which is hold until they end).
122 *
123 * Returns -1 in failure and fd in success.
124 */
125 static int
wait_for_lock(void)126 wait_for_lock(void)
127 {
128 char *lock_path;
129 int fd;
130
131 lock_path = get_lock_path();
132 if (!lock_path)
133 goto err_path;
134
135 fd = open(lock_path, O_RDWR | O_CLOEXEC | O_CREAT, 00700);
136 if (fd == -1) {
137 fprintf(stderr, "Could not open lock file %s: %s\n", lock_path, strerror(errno));
138 goto err_open;
139 }
140 fprintf(stderr, "Waiting for lock on %s...\n", lock_path);
141
142 /* The call is blocking, so we don't need a 'while'. Also, as we
143 * have a timeout for each test, this won't get stuck waiting. */
144 if (flock(fd, LOCK_EX) == -1) {
145 fprintf(stderr, "Could not lock %s: %s\n", lock_path, strerror(errno));
146 goto err_lock;
147 }
148 fprintf(stderr, "Lock %s acquired.\n", lock_path);
149 free(lock_path);
150
151 return fd;
152
153 err_lock:
154 close(fd);
155 err_open:
156 free(lock_path);
157 err_path:
158 return -1;
159 }
160
161 /** Initialize part of compositor setup
162 *
163 * \param setup The variable to initialize.
164 * \param testset_name Value for testset_name member.
165 *
166 * \ingroup testharness_private
167 */
168 void
compositor_setup_defaults_(struct compositor_setup * setup,const char * testset_name)169 compositor_setup_defaults_(struct compositor_setup *setup,
170 const char *testset_name)
171 {
172 *setup = (struct compositor_setup) {
173 .backend = WESTON_BACKEND_HEADLESS,
174 .renderer = RENDERER_NOOP,
175 .shell = SHELL_DESKTOP,
176 .xwayland = false,
177 .width = 320,
178 .height = 240,
179 .scale = 1,
180 .transform = WL_OUTPUT_TRANSFORM_NORMAL,
181 .config_file = NULL,
182 .extra_module = NULL,
183 .logging_scopes = NULL,
184 .testset_name = testset_name,
185 };
186 }
187
188 static const char *
backend_to_str(enum weston_compositor_backend b)189 backend_to_str(enum weston_compositor_backend b)
190 {
191 static const char * const names[] = {
192 [WESTON_BACKEND_DRM] = "drm-backend.so",
193 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
194 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
195 [WESTON_BACKEND_RDP] = "rdp-backend.so",
196 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
197 [WESTON_BACKEND_X11] = "X11-backend.so",
198 };
199 assert(b >= 0 && b < ARRAY_LENGTH(names));
200 return names[b];
201 }
202
203 static const char *
renderer_to_arg(enum weston_compositor_backend b,enum renderer_type r)204 renderer_to_arg(enum weston_compositor_backend b, enum renderer_type r)
205 {
206 static const char * const headless_names[] = {
207 [RENDERER_NOOP] = NULL,
208 [RENDERER_PIXMAN] = "--use-pixman",
209 [RENDERER_GL] = "--use-gl",
210 };
211 static const char * const drm_names[] = {
212 [RENDERER_PIXMAN] = "--use-pixman",
213 [RENDERER_GL] = NULL,
214 };
215
216 switch (b) {
217 case WESTON_BACKEND_HEADLESS:
218 assert(r >= RENDERER_NOOP && r <= RENDERER_GL);
219 return headless_names[r];
220 case WESTON_BACKEND_DRM:
221 assert(r >= RENDERER_PIXMAN && r <= RENDERER_GL);
222 return drm_names[r];
223 default:
224 assert(0 && "renderer_to_str() does not know the backend");
225 }
226
227 return NULL;
228 }
229
230 static const char *
shell_to_str(enum shell_type t)231 shell_to_str(enum shell_type t)
232 {
233 static const char * const names[] = {
234 [SHELL_TEST_DESKTOP] = "weston-test-desktop-shell.so",
235 [SHELL_DESKTOP] = "desktop-shell.so",
236 [SHELL_FULLSCREEN] = "fullscreen-shell.so",
237 [SHELL_IVI] = "ivi-shell.so",
238 };
239 assert(t >= 0 && t < ARRAY_LENGTH(names));
240 return names[t];
241 }
242
243 static const char *
transform_to_str(enum wl_output_transform t)244 transform_to_str(enum wl_output_transform t)
245 {
246 static const char * const names[] = {
247 [WL_OUTPUT_TRANSFORM_NORMAL] = "normal",
248 [WL_OUTPUT_TRANSFORM_90] = "rotate-90",
249 [WL_OUTPUT_TRANSFORM_180] = "rotate-180",
250 [WL_OUTPUT_TRANSFORM_270] = "rotate-270",
251 [WL_OUTPUT_TRANSFORM_FLIPPED] = "flipped",
252 [WL_OUTPUT_TRANSFORM_FLIPPED_90] = "flipped-rotate-90",
253 [WL_OUTPUT_TRANSFORM_FLIPPED_180] = "flipped-rotate-180",
254 [WL_OUTPUT_TRANSFORM_FLIPPED_270] = "flipped-rotate-270",
255 };
256
257 assert(t < ARRAY_LENGTH(names) && names[t]);
258 return names[t];
259 }
260
261 /** Execute compositor
262 *
263 * Manufactures the compositor command line and calls wet_main().
264 *
265 * Returns RESULT_SKIP if the given setup contains features that were disabled
266 * in the build, e.g. GL-renderer or DRM-backend.
267 *
268 * \ingroup testharness_private
269 */
270 int
execute_compositor(const struct compositor_setup * setup,struct wet_testsuite_data * data)271 execute_compositor(const struct compositor_setup *setup,
272 struct wet_testsuite_data *data)
273 {
274 struct prog_args args;
275 char *tmp;
276 const char *ctmp, *drm_device;
277 int ret, lock_fd = -1;
278
279 if (setenv("WESTON_MODULE_MAP", WESTON_MODULE_MAP, 0) < 0 ||
280 setenv("WESTON_DATA_DIR", WESTON_DATA_DIR, 0) < 0) {
281 fprintf(stderr, "Error: environment setup failed.\n");
282 return RESULT_HARD_ERROR;
283 }
284
285 #ifndef BUILD_DRM_COMPOSITOR
286 if (setup->backend == WESTON_BACKEND_DRM) {
287 fprintf(stderr, "DRM-backend required but not built, skipping.\n");
288 return RESULT_SKIP;
289 }
290 #endif
291
292 #ifndef BUILD_FBDEV_COMPOSITOR
293 if (setup->backend == WESTON_BACKEND_FBDEV) {
294 fprintf(stderr, "fbdev-backend required but not built, skipping.\n");
295 return RESULT_SKIP;
296 }
297 #endif
298
299 #ifndef BUILD_RDP_COMPOSITOR
300 if (setup->backend == WESTON_BACKEND_RDP) {
301 fprintf(stderr, "RDP-backend required but not built, skipping.\n");
302 return RESULT_SKIP;
303 }
304 #endif
305
306 #ifndef BUILD_WAYLAND_COMPOSITOR
307 if (setup->backend == WESTON_BACKEND_WAYLAND) {
308 fprintf(stderr, "wayland-backend required but not built, skipping.\n");
309 return RESULT_SKIP;
310 }
311 #endif
312
313 #ifndef BUILD_X11_COMPOSITOR
314 if (setup->backend == WESTON_BACKEND_X11) {
315 fprintf(stderr, "X11-backend required but not built, skipping.\n");
316 return RESULT_SKIP;
317 }
318 #endif
319
320 #ifndef ENABLE_EGL
321 if (setup->renderer == RENDERER_GL) {
322 fprintf(stderr, "GL-renderer required but not built, skipping.\n");
323 return RESULT_SKIP;
324 }
325 #endif
326
327 #if !TEST_GL_RENDERER
328 if (setup->renderer == RENDERER_GL) {
329 fprintf(stderr, "GL-renderer disabled for tests, skipping.\n");
330 return RESULT_SKIP;
331 }
332 #endif
333
334 prog_args_init(&args);
335
336 /* argv[0] */
337 asprintf(&tmp, "weston-%s", setup->testset_name);
338 prog_args_take(&args, tmp);
339
340 asprintf(&tmp, "--backend=%s", backend_to_str(setup->backend));
341 prog_args_take(&args, tmp);
342
343 if (setup->backend == WESTON_BACKEND_DRM) {
344
345 drm_device = getenv("WESTON_TEST_SUITE_DRM_DEVICE");
346 if (!drm_device) {
347 fprintf(stderr, "Skipping DRM-backend tests because " \
348 "WESTON_TEST_SUITE_DRM_DEVICE is not set. " \
349 "See test suite documentation to learn how " \
350 "to run them.\n");
351 return RESULT_SKIP;
352 }
353 asprintf(&tmp, "--drm-device=%s", drm_device);
354 prog_args_take(&args, tmp);
355
356 prog_args_take(&args, strdup("--seat=weston-test-seat"));
357
358 prog_args_take(&args, strdup("--continue-without-input"));
359
360 lock_fd = wait_for_lock();
361 if (lock_fd == -1)
362 return RESULT_FAIL;
363 }
364
365 asprintf(&tmp, "--socket=%s", setup->testset_name);
366 prog_args_take(&args, tmp);
367
368 asprintf(&tmp, "--modules=%s%s%s", TESTSUITE_PLUGIN_PATH,
369 setup->extra_module ? "," : "",
370 setup->extra_module ? setup->extra_module : "");
371 prog_args_take(&args, tmp);
372
373 if (setup->backend != WESTON_BACKEND_DRM &&
374 setup->backend != WESTON_BACKEND_FBDEV) {
375 asprintf(&tmp, "--width=%d", setup->width);
376 prog_args_take(&args, tmp);
377
378 asprintf(&tmp, "--height=%d", setup->height);
379 prog_args_take(&args, tmp);
380 }
381
382 if (setup->scale != 1) {
383 asprintf(&tmp, "--scale=%d", setup->scale);
384 prog_args_take(&args, tmp);
385 }
386
387 if (setup->transform != WL_OUTPUT_TRANSFORM_NORMAL) {
388 asprintf(&tmp, "--transform=%s",
389 transform_to_str(setup->transform));
390 prog_args_take(&args, tmp);
391 }
392
393 if (setup->config_file) {
394 asprintf(&tmp, "--config=%s", setup->config_file);
395 prog_args_take(&args, tmp);
396 } else {
397 prog_args_take(&args, strdup("--no-config"));
398 }
399
400 ctmp = renderer_to_arg(setup->backend, setup->renderer);
401 if (ctmp)
402 prog_args_take(&args, strdup(ctmp));
403
404 asprintf(&tmp, "--shell=%s", shell_to_str(setup->shell));
405 prog_args_take(&args, tmp);
406
407 if (setup->logging_scopes) {
408 asprintf(&tmp, "--logger-scopes=%s", setup->logging_scopes);
409 prog_args_take(&args, tmp);
410 }
411
412 if (setup->xwayland)
413 prog_args_take(&args, strdup("--xwayland"));
414
415 wet_testsuite_data_set(data);
416 prog_args_save(&args);
417 ret = wet_main(args.argc, args.argv);
418
419 prog_args_fini(&args);
420
421 /* We acquired a lock (if this is a DRM-backend test) and now we can
422 * close its fd and release it, as it has already been run. */
423 if (lock_fd != -1)
424 close(lock_fd);
425
426 return ret;
427 }
428