Debian Codegraph @

test/context.c

raw
1 /*
2 * Copyright © 2012 Intel Corporation
3 * SPDX-License-Identifier: MIT
4 *
5 * Author: Daniel Stone <[email protected]>
6 */
7
8 #include "config.h"
9
10 #include <stdlib.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13
14 #include "test.h"
15 #include "xkbcommon/xkbcommon.h"
16 #include "context.h"
17
18 /* keeps a cache of all makedir/maketmpdir directories so we can free and
19 * rmdir them in one go, see unmakedirs() */
20 char *dirnames[64];
21 int ndirs;
22
23 /* keeps a cache of all buffered env vars so we can restore
24 * them in one go, see restore_env() */
25 struct env {
26 char *key;
27 char *value;
28 } environment[64];
29 int nenviron;
30
31 static void
32 buffer_env(const char *key)
33 {
34 char *v = getenv(key);
35
36 environment[nenviron].key = strdup(key);
37 environment[nenviron].value = v ? strdup(v) : NULL;
38 nenviron++;
39 }
40
41 static void
42 restore_env(void)
43 {
44 for (int i = 0; i < nenviron; i++) {
45 char *key = environment[i].key,
46 *value = environment[i].value;
47
48 if (value)
49 setenv(key, value, 1);
50 else
51 unsetenv(key);
52
53 free(key);
54 free(value);
55 }
56 nenviron = 0;
57 memset(environment, 0, sizeof(environment));
58 }
59
60 static const char *
61 makedir(const char *parent, const char *path)
62 {
63 char *dirname = test_makedir(parent, path);
64 dirnames[ndirs++] = dirname;
65 return dirname;
66 }
67
68 static const char *maketmpdir(void)
69 {
70 char *tmpdir = test_maketempdir("xkbcommon-test.XXXXXX");
71 dirnames[ndirs++] = tmpdir;
72 return tmpdir;
73 }
74
75 static void
76 unmakedirs(void)
77 {
78 /* backwards order for rmdir to work */
79 for (int i = ndirs - 1; i >= 0; i--) {
80 char *dir = dirnames[i];
81 if (!dir)
82 break;
83 rmdir(dir);
84 free(dir);
85 }
86 ndirs = 0;
87 memset(dirnames, 0, sizeof(dirnames));
88 }
89
90 static void
91 test_config_root_include_path(void)
92 {
93 struct xkb_context *ctx;
94 const char *tmpdir;
95 const char *context_path;
96
97 buffer_env("XKB_CONFIG_ROOT");
98 buffer_env("HOME");
99 buffer_env("XDG_CONFIG_HOME");
100
101 tmpdir = maketmpdir();
102 setenv("XKB_CONFIG_ROOT", tmpdir, 1);
103 unsetenv("HOME");
104 unsetenv("XDG_CONFIG_HOME");
105
106 /* Valid built-in path is last */
107 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
108 const unsigned int nincludes = xkb_context_num_include_paths(ctx);
109 assert(nincludes >= 1);
110 context_path = xkb_context_include_path_get(ctx, nincludes - 1);
111 assert(strcmp(context_path, tmpdir) == 0);
112 xkb_context_unref(ctx);
113
114 unmakedirs();
115 restore_env();
116 }
117
118 static void
119 test_config_root_include_path_fallback(void)
120 {
121 struct xkb_context *ctx;
122 const char *xkbdir = DFLT_XKB_CONFIG_ROOT;
123 const char *context_path;
124
125 /* quick and dirty check that the default directory exists.
126 * It may not on a vanilla test box if we just run the test
127 * suite, so where it's not there just skip this test. */
128 struct stat stat_buf;
129 int err = stat(xkbdir, &stat_buf);
130 if (err != 0)
131 return;
132 if (!S_ISDIR(stat_buf.st_mode))
133 return;
134
135 buffer_env("XKB_CONFIG_ROOT");
136 buffer_env("XKB_CONFIG_UNVERSIONED_EXTENSIONS_PATH");
137 buffer_env("XKB_CONFIG_VERSIONED_EXTENSIONS_PATH");
138 buffer_env("XKB_CONFIG_EXTRA_PATH");
139 buffer_env("HOME");
140 buffer_env("XDG_CONFIG_HOME");
141
142 unsetenv("XKB_CONFIG_ROOT");
143 unsetenv("HOME");
144 unsetenv("XDG_CONFIG_HOME");
145
146 /* Valid built-in path is last */
147 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
148 unsigned int nincludes = xkb_context_num_include_paths(ctx);
149 assert(nincludes >= 1);
150 context_path = xkb_context_include_path_get(ctx, nincludes - 1);
151 assert(strcmp(context_path, xkbdir) == 0);
152 xkb_context_unref(ctx);
153
154 /* Invalid built-in path is replaced with legacy X11 path */
155 setenv("XKB_CONFIG_ROOT", "¡NONSENSE!", 1);
156 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
157 nincludes = xkb_context_num_include_paths(ctx);
158 assert(nincludes >= 1);
159 context_path = xkb_context_include_path_get(ctx, nincludes - 1);
160 assert(strcmp(context_path, DFLT_XKB_LEGACY_ROOT) == 0);
161 xkb_context_unref(ctx);
162
163 setenv("XKB_CONFIG_UNVERSIONED_EXTENSIONS_PATH", "", 1);
164 setenv("XKB_CONFIG_VERSIONED_EXTENSIONS_PATH", "", 1);
165
166 /* Ensure some path is available */
167 const char *tmpdir = maketmpdir();
168 setenv("XKB_CONFIG_EXTRA_PATH", tmpdir, 1);
169
170 /* Invalid built-in path and deliberately skipped legacy X11 path */
171 setenv("XKB_CONFIG_ROOT", "", 1);
172 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
173 nincludes = xkb_context_num_include_paths(ctx);
174 assert(nincludes >= 1);
175 context_path = xkb_context_include_path_get(ctx, nincludes - 1);
176 assert(strcmp(context_path, tmpdir) == 0);
177 xkb_context_unref(ctx);
178
179 /* No valid path */
180 setenv("XKB_CONFIG_EXTRA_PATH", "", 1);
181 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
182 nincludes = xkb_context_num_include_paths(ctx);
183 assert(nincludes == 0);
184 xkb_context_unref(ctx);
185
186 unmakedirs();
187 restore_env();
188 }
189
190 static void
191 test_xkbdir_include_path(void)
192 {
193 struct xkb_context *ctx;
194 const char *tmpdir;
195 const char *xkb_path;
196 const char *context_path;
197
198 buffer_env("HOME");
199 buffer_env("XDG_CONFIG_HOME");
200
201 tmpdir = maketmpdir();
202 xkb_path = makedir(tmpdir, ".xkb");
203 setenv("HOME", tmpdir, 1);
204 setenv("XDG_CONFIG_HOME", tmpdir, 1);
205
206 /* No XDG directory in our tmpdir, so we expect
207 * the $HOME/.xkb to be the first include path */
208 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
209 assert(xkb_context_num_include_paths(ctx) >= 1);
210 context_path = xkb_context_include_path_get(ctx, 0);
211 assert(strcmp(context_path, xkb_path) == 0);
212 xkb_context_unref(ctx);
213
214 unmakedirs();
215 restore_env();
216 }
217
218 static void
219 test_xdg_include_path(void)
220 {
221 struct xkb_context *ctx;
222 const char *tmpdir;
223 const char *xdg_path;
224 const char *context_path;
225
226 buffer_env("XDG_CONFIG_HOME");
227
228 tmpdir = maketmpdir();
229 xdg_path = makedir(tmpdir, "xkb");
230 setenv("XDG_CONFIG_HOME", tmpdir, 1);
231
232 /* XDG path is always first */
233 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
234 assert(xkb_context_num_include_paths(ctx) >= 1);
235 context_path = xkb_context_include_path_get(ctx, 0);
236 assert(strcmp(context_path, xdg_path) == 0);
237 xkb_context_unref(ctx);
238
239 unmakedirs();
240 restore_env();
241 }
242
243 static void
244 test_xdg_include_path_fallback(void)
245 {
246 struct xkb_context *ctx;
247 const char *tmpdir;
248 const char *xdg_root, *xdg_path;
249 const char *context_path;
250
251 buffer_env("XDG_CONFIG_HOME");
252 buffer_env("HOME");
253
254 tmpdir = maketmpdir();
255 xdg_root = makedir(tmpdir, ".config");
256 xdg_path = makedir(xdg_root, "xkb");
257 setenv("HOME", tmpdir, 1);
258 unsetenv("XDG_CONFIG_HOME");
259
260 /* XDG path is always first, even if fallback */
261 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
262 assert(xkb_context_num_include_paths(ctx) >= 1);
263 context_path = xkb_context_include_path_get(ctx, 0);
264 assert(strcmp(context_path, xdg_path) == 0);
265 xkb_context_unref(ctx);
266
267 unmakedirs();
268 restore_env();
269 }
270
271 static void
272 test_include_order(void)
273 {
274 struct xkb_context *ctx;
275 const char *tmpdir;
276 const char *xdg_path;
277 const char *xkb_home_path;
278 const char *xkb_root_path;
279 const char *context_path;
280
281 buffer_env("XKB_CONFIG_ROOT");
282 buffer_env("XKB_CONFIG_UNVERSIONED_EXTENSIONS_PATH");
283 buffer_env("XKB_CONFIG_VERSIONED_EXTENSIONS_PATH");
284 buffer_env("XDG_CONFIG_HOME");
285 buffer_env("HOME");
286
287 tmpdir = maketmpdir();
288 xdg_path = makedir(tmpdir, "xkb");
289 xkb_home_path = makedir(tmpdir, ".xkb");
290 xkb_root_path = makedir(tmpdir, "xkbroot");
291 setenv("HOME", tmpdir, 1);
292 setenv("XDG_CONFIG_HOME", tmpdir, 1);
293 setenv("XKB_CONFIG_ROOT", xkb_root_path, 1);
294 setenv("XKB_CONFIG_UNVERSIONED_EXTENSIONS_PATH", "", 1);
295 setenv("XKB_CONFIG_VERSIONED_EXTENSIONS_PATH", "", 1);
296
297 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
298 assert(xkb_context_num_include_paths(ctx) >= 3);
299 /* XDG is first */
300 context_path = xkb_context_include_path_get(ctx, 0);
301 assert(strcmp(context_path, xdg_path) == 0);
302 /* $HOME/.xkb is second */
303 context_path = xkb_context_include_path_get(ctx, 1);
304 assert(strcmp(context_path, xkb_home_path) == 0);
305 /* CONFIG_ROOT is last */
306 context_path = xkb_context_include_path_get(ctx, 2);
307 assert(strcmp(context_path, xkb_root_path) == 0);
308
309 xkb_context_unref(ctx);
310
311 unmakedirs();
312 restore_env();
313 }
314
315 static void
316 test_delayed_includes(void)
317 {
318 buffer_env("XKB_CONFIG_ROOT");
319 buffer_env("XKB_CONFIG_UNVERSIONED_EXTENSIONS_PATH");
320 buffer_env("XKB_CONFIG_VERSIONED_EXTENSIONS_PATH");
321 buffer_env("XKB_CONFIG_EXTRA_PATH");
322 buffer_env("XDG_CONFIG_HOME");
323 buffer_env("HOME");
324
325 const char * const xkb_root_path = maketmpdir();
326
327 /* Empty paths, so that currently a call to
328 * xkb_context_include_path_append_default() would fail */
329 setenv("XKB_CONFIG_ROOT", "", 1);
330 setenv("XKB_CONFIG_UNVERSIONED_EXTENSIONS_PATH", "", 1);
331 setenv("XKB_CONFIG_VERSIONED_EXTENSIONS_PATH", "", 1);
332 setenv("XKB_CONFIG_EXTRA_PATH", "", 1);
333 unsetenv("XDG_CONFIG_HOME");
334 unsetenv("HOME");
335
336 struct xkb_context *ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
337 /* Context should initialize correctly, despite no include path is
338 * currently available */
339 assert(ctx);
340 assert(xkb_context_num_include_paths(ctx) == 0);
341 /* Setting XKB_CONFIG_ROOT correctly is too late, because we used API that
342 * requires to initialize the include paths */
343 setenv("XKB_CONFIG_ROOT", xkb_root_path, 1);
344 assert(xkb_context_num_include_paths(ctx) == 0);
345 /* We cannot add further paths, since the default paths failed */
346 assert(!xkb_context_include_path_append(ctx, xkb_root_path));
347 xkb_context_unref(ctx);
348
349 setenv("XKB_CONFIG_ROOT", "", 1);
350 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
351 /* Context should initialize correctly, despite no include path is
352 * currently available */
353 assert(ctx);
354 /* Set XKB_CONFIG_ROOT correctly before using any API that requires the
355 * to initialize the include paths */
356 setenv("XKB_CONFIG_ROOT", xkb_root_path, 1);
357 assert(xkb_context_num_include_paths(ctx) == 1);
358 const char * const context_path = xkb_context_include_path_get(ctx, 0);
359 assert(strcmp(context_path, xkb_root_path) == 0);
360 assert(xkb_context_include_path_append(ctx, xkb_root_path) == 1);
361 assert(xkb_context_num_include_paths(ctx) == 2);
362 xkb_context_unref(ctx);
363
364 unmakedirs();
365 restore_env();
366 }
367
368 int
369 main(void)
370 {
371 test_init();
372
373 struct xkb_context *context = test_get_context(0);
374 xkb_atom_t atom;
375
376 assert(context);
377
378 assert(xkb_context_num_include_paths(context) == 1);
379 assert(!xkb_context_include_path_append(context, "¡NONSENSE!"));
380 assert(xkb_context_num_include_paths(context) == 1);
381
382 atom = xkb_atom_intern(context, "HELLOjunkjunkjunk", 5);
383 assert(atom != XKB_ATOM_NONE);
384 assert(streq(xkb_atom_text(context, atom), "HELLO"));
385
386 atom = xkb_atom_intern_literal(context, "HELLOjunkjunkjunk");
387 assert(atom != XKB_ATOM_NONE);
388 assert(streq(xkb_atom_text(context, atom), "HELLOjunkjunkjunk"));
389
390 xkb_context_unref(context);
391
392 test_config_root_include_path();
393 test_config_root_include_path_fallback();
394 test_xkbdir_include_path();
395 test_xdg_include_path();
396 test_xdg_include_path_fallback();
397 test_include_order();
398 test_delayed_includes();
399
400 return EXIT_SUCCESS;
401 }