d9fa0a7934058a23f8bb31a6779d71abe8aca421
[platform/upstream/libxkbcommon.git] / test / xvfb-wrapper.c
1 /*
2  * Copyright © 2014 Ran Benita <ran234@gmail.com>
3  * Copyright © 2023 Pierre Le Marre <dev@wismill.eu>
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 <stdio.h>
28 #include <spawn.h>
29 #include <assert.h>
30 #include <signal.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33
34 #include "test.h"
35 #include "xvfb-wrapper.h"
36 #include "xkbcommon/xkbcommon-x11.h"
37
38 int
39 xvfb_wrapper(int (*test_func)(char* display))
40 {
41     int ret = 0;
42     FILE * display_fd;
43     char display_fd_string[32];
44     char *xvfb_argv[] = {
45         (char *) "Xvfb", (char *) "-displayfd", display_fd_string, NULL
46     };
47     char *envp[] = { NULL };
48     pid_t xvfb_pid = 0;
49     char display[32] = ":";
50     size_t length;
51
52     /* File descriptor to retrieve the display number */
53     display_fd = tmpfile();
54     if (display_fd == NULL){
55         fprintf(stderr, "Unable to create temporary file.\n");
56         goto err_display_fd;
57     }
58     snprintf(display_fd_string, sizeof(display_fd_string), "%d", fileno(display_fd));
59
60     /*
61      * Xvfb command: let the server find an available display.
62      *
63      * Note that it may generate multiple times the following output in stderr:
64      *    _XSERVTransSocketUNIXCreateListener: ...SocketCreateListener() failed
65      * It is expected: this is the server trying the ports until it finds one
66      * that works.
67      */
68     ret = posix_spawnp(&xvfb_pid, "Xvfb", NULL, NULL, xvfb_argv, envp);
69     if (ret != 0) {
70         ret = SKIP_TEST;
71         goto err_xvfd;
72     }
73
74     /* Wait for Xvfb fully waking up to accept a connection from a client. */
75     sleep(1);
76
77     /* Retrieve the display number: Xvfd writes the display number as a newline-
78      * terminated string; copy this number to form a proper display string. */
79     rewind(display_fd);
80     length = fread(&display[1], 1, sizeof(display) - 1, display_fd);
81     if (length <= 0) {
82         ret = SKIP_TEST;
83         goto err_xvfd;
84     } else {
85         /* Drop the newline character */
86         display[length] = '\0';
87     }
88
89     /* Run the function requiring a running X server */
90     ret = test_func(display);
91
92 err_xvfd:
93     if (xvfb_pid > 0)
94         kill(xvfb_pid, SIGTERM);
95     fclose(display_fd);
96 err_display_fd:
97     return ret;
98 }
99
100 /* All X11_TEST functions are in the test_functions_section ELF section.
101  * __start and __stop point to the start and end of that section. See the
102  * __attribute__(section) documentation.
103  */
104 extern const struct test_function __start_test_functions_section, __stop_test_functions_section;
105
106 int
107 x11_tests_run()
108 {
109     size_t count = 1; /* For NULL-terminated entry */
110
111     for (const struct test_function *t = &__start_test_functions_section;
112          t < &__stop_test_functions_section;
113          t++)
114         count++;
115
116     int rc;
117     for (const struct test_function *t = &__start_test_functions_section;
118          t < &__stop_test_functions_section;
119          t++) {
120         fprintf(stderr, "Running test: %s from %s\n", t->name, t->file);
121         rc = xvfb_wrapper(t->func);
122         if (rc != 0) {
123             break;
124         }
125     }
126
127     return rc;
128 }