2 * Copyright © 2012 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
23 * Author: Daniel Stone <daniel@fooishbar.org>
32 #include <sys/types.h>
34 /* keeps a cache of all makedir/maketmpdir directories so we can free and
35 * rmdir them in one go, see unmakedirs() */
39 /* keeps a cache of all buffered env vars so we can restore
40 * them in one go, see restore_env() */
47 static void buffer_env(const char *key)
49 char *v = getenv(key);
51 environment[nenviron].key = strdup(key);
52 environment[nenviron].value = v ? strdup(v) : NULL;
56 static void restore_env(void)
58 for (int i = 0; i < nenviron; i++) {
59 char *key = environment[i].key,
60 *value = environment[i].value;
63 setenv(key, value, 1);
71 memset(environment, 0, sizeof(environment));
74 static const char *makedir(const char *parent, const char *path)
76 char *dirname = test_makedir(parent, path);
77 dirnames[ndirs++] = dirname;
81 static const char *maketmpdir(void)
83 char *tmpdir = test_maketempdir("xkbcommon-test.XXXXXX");
84 dirnames[ndirs++] = tmpdir;
88 static void unmakedirs(void)
90 /* backwards order for rmdir to work */
91 for (int i = ndirs - 1; i >= 0; i--) {
92 char *dir = dirnames[i];
99 memset(dirnames, 0, sizeof(dirnames));
103 test_config_root_include_path(void)
105 struct xkb_context *ctx;
107 const char *context_path;
110 buffer_env("XKB_CONFIG_ROOT");
112 buffer_env("XDG_CONFIG_HOME");
114 tmpdir = maketmpdir();
115 setenv("XKB_CONFIG_ROOT", tmpdir, 1);
117 unsetenv("XDG_CONFIG_HOME");
119 /* built-in path is last */
120 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
121 nincludes = xkb_context_num_include_paths(ctx);
122 assert(nincludes >= 1);
123 context_path = xkb_context_include_path_get(ctx, nincludes - 1);
124 assert(strcmp(context_path, tmpdir) == 0);
125 xkb_context_unref(ctx);
132 test_config_root_include_path_fallback(void)
134 struct xkb_context *ctx;
135 const char *xkbdir = DFLT_XKB_CONFIG_ROOT;
136 const char *context_path;
139 /* quick and dirty check that the default directory exists.
140 * It may not on a vanilla test box if we just run the test
141 * suite, so where it's not there just skip this test. */
142 struct stat stat_buf;
143 int err = stat(xkbdir, &stat_buf);
146 if (!S_ISDIR(stat_buf.st_mode))
149 buffer_env("XKB_CONFIG_ROOT");
151 buffer_env("XDG_CONFIG_HOME");
153 unsetenv("XKB_CONFIG_ROOT");
155 unsetenv("XDG_CONFIG_HOME");
157 /* built-in path is last */
158 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
159 nincludes = xkb_context_num_include_paths(ctx);
160 assert(nincludes >= 1);
161 context_path = xkb_context_include_path_get(ctx, nincludes - 1);
162 assert(strcmp(context_path, xkbdir) == 0);
163 xkb_context_unref(ctx);
170 test_xkbdir_include_path(void)
172 struct xkb_context *ctx;
174 const char *xkb_path;
175 const char *context_path;
178 buffer_env("XDG_CONFIG_HOME");
180 tmpdir = maketmpdir();
181 xkb_path = makedir(tmpdir, ".xkb");
182 setenv("HOME", tmpdir, 1);
183 setenv("XDG_CONFIG_HOME", tmpdir, 1);
185 /* No XDG directory in our tmpdir, so we expect
186 * the $HOME/.xkb to be the first include path */
187 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
188 assert(xkb_context_num_include_paths(ctx) >= 1);
189 context_path = xkb_context_include_path_get(ctx, 0);
190 assert(strcmp(context_path, xkb_path) == 0);
191 xkb_context_unref(ctx);
198 test_xdg_include_path(void)
200 struct xkb_context *ctx;
202 const char *xdg_path;
203 const char *context_path;
205 buffer_env("XDG_CONFIG_HOME");
207 tmpdir = maketmpdir();
208 xdg_path = makedir(tmpdir, "xkb");
209 setenv("XDG_CONFIG_HOME", tmpdir, 1);
211 /* XDG path is always first */
212 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
213 assert(xkb_context_num_include_paths(ctx) >= 1);
214 context_path = xkb_context_include_path_get(ctx, 0);
215 assert(strcmp(context_path, xdg_path) == 0);
216 xkb_context_unref(ctx);
223 test_xdg_include_path_fallback(void)
225 struct xkb_context *ctx;
227 const char *xdg_root, *xdg_path;
228 const char *context_path;
230 buffer_env("XDG_CONFIG_HOME");
233 tmpdir = maketmpdir();
234 xdg_root = makedir(tmpdir, ".config");
235 xdg_path = makedir(xdg_root, "xkb");
236 setenv("HOME", tmpdir, 1);
237 unsetenv("XDG_CONFIG_HOME");
239 /* XDG path is always first, even if fallback */
240 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
241 assert(xkb_context_num_include_paths(ctx) >= 1);
242 context_path = xkb_context_include_path_get(ctx, 0);
243 assert(strcmp(context_path, xdg_path) == 0);
244 xkb_context_unref(ctx);
251 test_include_order(void)
253 struct xkb_context *ctx;
255 const char *xdg_path;
256 const char *xkb_home_path;
257 const char *xkb_root_path;
258 const char *context_path;
260 buffer_env("XKB_CONFIG_ROOT");
261 buffer_env("XDG_CONFIG_HOME");
264 tmpdir = maketmpdir();
265 xdg_path = makedir(tmpdir, "xkb");
266 xkb_home_path = makedir(tmpdir, ".xkb");
267 xkb_root_path = makedir(tmpdir, "xkbroot");
268 setenv("HOME", tmpdir, 1);
269 setenv("XDG_CONFIG_HOME", tmpdir, 1);
270 setenv("XKB_CONFIG_ROOT", xkb_root_path, 1);
272 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
273 assert(xkb_context_num_include_paths(ctx) >= 3);
275 context_path = xkb_context_include_path_get(ctx, 0);
276 assert(strcmp(context_path, xdg_path) == 0);
277 /* $HOME/.xkb is second */
278 context_path = xkb_context_include_path_get(ctx, 1);
279 assert(strcmp(context_path, xkb_home_path) == 0);
280 /* CONFIG_ROOT is last */
281 context_path = xkb_context_include_path_get(ctx, 2);
282 assert(strcmp(context_path, xkb_root_path) == 0);
284 xkb_context_unref(ctx);
293 struct xkb_context *context = test_get_context(0);
298 assert(xkb_context_num_include_paths(context) == 1);
299 assert(!xkb_context_include_path_append(context, "¡NONSENSE!"));
300 assert(xkb_context_num_include_paths(context) == 1);
302 atom = xkb_atom_intern(context, "HELLOjunkjunkjunk", 5);
303 assert(atom != XKB_ATOM_NONE);
304 assert(streq(xkb_atom_text(context, atom), "HELLO"));
306 atom = xkb_atom_intern_literal(context, "HELLOjunkjunkjunk");
307 assert(atom != XKB_ATOM_NONE);
308 assert(streq(xkb_atom_text(context, atom), "HELLOjunkjunkjunk"));
310 xkb_context_unref(context);
312 test_config_root_include_path();
313 test_config_root_include_path_fallback();
314 test_xkbdir_include_path();
315 test_xdg_include_path();
316 test_xdg_include_path_fallback();
317 test_include_order();