Test: Use a xvfb wrapper for x11 test The x11 test is currently silently skipped in CI, because it requires a running X server. Create a xvfb wrapper to run the test. We do not use `xvfb-run`, because it is a shell script and it causes valgrind to detect unrelated memory issues in the shell (dash, bash). Improve wrapper using a special ELF section TODO: The wrapper is intended to be used with the x11comp test as well.
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
diff --git a/meson.build b/meson.build
index 6a79093..0769919 100644
--- a/meson.build
+++ b/meson.build
@@ -573,9 +573,11 @@ test_dep = declare_dependency(
link_with: libxkbcommon_test_internal,
)
if get_option('enable-x11')
- libxkbcommon_x11_internal = static_library(
+ libxkbcommon_x11_test_internal = static_library(
'xkbcommon-x11-internal',
libxkbcommon_x11_sources,
+ 'test/xvfb-wrapper.c',
+ 'test/xvfb-wrapper.h',
include_directories: include_directories('src', 'include'),
link_with: libxkbcommon_test_internal,
dependencies: [
@@ -584,7 +586,7 @@ if get_option('enable-x11')
],
)
x11_test_dep = declare_dependency(
- link_with: libxkbcommon_x11_internal,
+ link_with: libxkbcommon_x11_test_internal,
dependencies: [
test_dep,
xcb_dep,
diff --git a/test/x11.c b/test/x11.c
index 00f3a96..3742a0b 100644
--- a/test/x11.c
+++ b/test/x11.c
@@ -24,10 +24,10 @@
#include "config.h"
#include "test.h"
+#include "xvfb-wrapper.h"
#include "xkbcommon/xkbcommon-x11.h"
-int
-main(void)
+X11_TEST(test_basic)
{
struct xkb_context *ctx = test_get_context(0);
xcb_connection_t *conn;
@@ -43,7 +43,7 @@ main(void)
* If it fails, it's not necessarily an actual problem with the code.
* So we don't want a FAIL here.
*/
- conn = xcb_connect(NULL, NULL);
+ conn = xcb_connect(display, NULL);
if (!conn || xcb_connection_has_error(conn)) {
exit_code = SKIP_TEST;
goto err_conn;
@@ -84,3 +84,7 @@ err_conn:
return exit_code;
}
+
+int main(void) {
+ return x11_tests_run();
+}
diff --git a/test/xvfb-wrapper.c b/test/xvfb-wrapper.c
new file mode 100644
index 0000000..d9fa0a7
--- /dev/null
+++ b/test/xvfb-wrapper.c
@@ -0,0 +1,128 @@
+/*
+ * Copyright © 2014 Ran Benita <ran234@gmail.com>
+ * Copyright © 2023 Pierre Le Marre <dev@wismill.eu>
+ *
+ * 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 <stdio.h>
+#include <spawn.h>
+#include <assert.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include "test.h"
+#include "xvfb-wrapper.h"
+#include "xkbcommon/xkbcommon-x11.h"
+
+int
+xvfb_wrapper(int (*test_func)(char* display))
+{
+ int ret = 0;
+ FILE * display_fd;
+ char display_fd_string[32];
+ char *xvfb_argv[] = {
+ (char *) "Xvfb", (char *) "-displayfd", display_fd_string, NULL
+ };
+ char *envp[] = { NULL };
+ pid_t xvfb_pid = 0;
+ char display[32] = ":";
+ size_t length;
+
+ /* File descriptor to retrieve the display number */
+ display_fd = tmpfile();
+ if (display_fd == NULL){
+ fprintf(stderr, "Unable to create temporary file.\n");
+ goto err_display_fd;
+ }
+ snprintf(display_fd_string, sizeof(display_fd_string), "%d", fileno(display_fd));
+
+ /*
+ * Xvfb command: let the server find an available display.
+ *
+ * Note that it may generate multiple times the following output in stderr:
+ * _XSERVTransSocketUNIXCreateListener: ...SocketCreateListener() failed
+ * It is expected: this is the server trying the ports until it finds one
+ * that works.
+ */
+ ret = posix_spawnp(&xvfb_pid, "Xvfb", NULL, NULL, xvfb_argv, envp);
+ if (ret != 0) {
+ ret = SKIP_TEST;
+ goto err_xvfd;
+ }
+
+ /* Wait for Xvfb fully waking up to accept a connection from a client. */
+ sleep(1);
+
+ /* Retrieve the display number: Xvfd writes the display number as a newline-
+ * terminated string; copy this number to form a proper display string. */
+ rewind(display_fd);
+ length = fread(&display[1], 1, sizeof(display) - 1, display_fd);
+ if (length <= 0) {
+ ret = SKIP_TEST;
+ goto err_xvfd;
+ } else {
+ /* Drop the newline character */
+ display[length] = '\0';
+ }
+
+ /* Run the function requiring a running X server */
+ ret = test_func(display);
+
+err_xvfd:
+ if (xvfb_pid > 0)
+ kill(xvfb_pid, SIGTERM);
+ fclose(display_fd);
+err_display_fd:
+ return ret;
+}
+
+/* All X11_TEST functions are in the test_functions_section ELF section.
+ * __start and __stop point to the start and end of that section. See the
+ * __attribute__(section) documentation.
+ */
+extern const struct test_function __start_test_functions_section, __stop_test_functions_section;
+
+int
+x11_tests_run()
+{
+ size_t count = 1; /* For NULL-terminated entry */
+
+ for (const struct test_function *t = &__start_test_functions_section;
+ t < &__stop_test_functions_section;
+ t++)
+ count++;
+
+ int rc;
+ for (const struct test_function *t = &__start_test_functions_section;
+ t < &__stop_test_functions_section;
+ t++) {
+ fprintf(stderr, "Running test: %s from %s\n", t->name, t->file);
+ rc = xvfb_wrapper(t->func);
+ if (rc != 0) {
+ break;
+ }
+ }
+
+ return rc;
+}
diff --git a/test/xvfb-wrapper.h b/test/xvfb-wrapper.h
new file mode 100644
index 0000000..222fa3e
--- /dev/null
+++ b/test/xvfb-wrapper.h
@@ -0,0 +1,44 @@
+/* This is a wrapper around X11 tests to make it faster to use for the simple
+ * type of test cases.
+ *
+ * Use with the X11_TEST macro like this:
+ *
+ * X11_TEST(some_test) {
+ * return 0;
+ * }
+ *
+ * int main(void) {
+ * return x11_tests_run(void);
+ * }
+ *
+ */
+
+#pragma once
+
+typedef int (* x11_test_func_t)(char* display);
+
+struct test_function {
+ const char *name; /* function name */
+ const char *file; /* file name */
+ x11_test_func_t func; /* test function */
+} __attribute__((aligned(16)));
+
+/**
+ * Defines a struct test_function in a custom ELF section that we can then
+ * loop over in x11_tests_run() to extract the tests. This removes the
+ * need of manually adding the tests to a suite or listing them somewhere.
+ */
+#define X11_TEST(_func) \
+static int _func(char* display); \
+static const struct test_function _test_##_func \
+__attribute__((used)) \
+__attribute__((section("test_functions_section"))) = { \
+ .name = #_func, \
+ .func = _func, \
+ .file = __FILE__, \
+}; \
+static int _func(char* display)
+
+int xvfb_wrapper(int (*f)(char* display));
+
+int x11_tests_run(void);