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>
37 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
43 /* keeps a cache of all makedir/maketmpdir directories so we can free and
44 * rmdir them in one go, see unmakedirs() */
48 /* keeps a cache of all buffered env vars so we can restore
49 * them in one go, see restore_env() */
56 static void buffer_env(const char *key)
58 char *v = getenv(key);
60 environment[nenviron].key = strdup(key);
61 environment[nenviron].value = v ? strdup(v) : NULL;
65 static void restore_env(void)
67 for (int i = 0; i < nenviron; i++) {
68 char *key = environment[i].key,
69 *value = environment[i].value;
72 setenv(key, value, 1);
80 memset(environment, 0, sizeof(environment));
83 static const char *makedir(const char *parent, const char *path)
88 err = asprintf(&dirname, "%s/%s", parent, path);
90 err = mkdir(dirname, 0777);
93 dirnames[ndirs++] = dirname;
98 static const char *maketmpdir(void)
100 const char *template = "/tmp/xkbcommon-test.XXXXXX";
101 char *tmpdir = strdup(template);
103 tmpdir = mkdtemp(tmpdir);
104 assert(tmpdir != NULL);
106 dirnames[ndirs++] = tmpdir;
111 static void unmakedirs(void)
113 /* backwards order for rmdir to work */
114 for (int i = ndirs - 1; i >= 0; i--) {
115 char *dir = dirnames[i];
122 memset(dirnames, 0, sizeof(dirnames));
126 test_config_root_include_path(void)
128 struct xkb_context *ctx;
130 const char *context_path;
133 buffer_env("XKB_CONFIG_ROOT");
135 buffer_env("XDG_CONFIG_HOME");
137 tmpdir = maketmpdir();
138 setenv("XKB_CONFIG_ROOT", tmpdir, 1);
140 unsetenv("XDG_CONFIG_HOME");
142 /* built-in path is last */
143 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
144 nincludes = xkb_context_num_include_paths(ctx);
145 assert(nincludes >= 1);
146 context_path = xkb_context_include_path_get(ctx, nincludes - 1);
147 assert(strcmp(context_path, tmpdir) == 0);
148 xkb_context_unref(ctx);
155 test_config_root_include_path_fallback(void)
157 struct xkb_context *ctx;
158 const char *xkbdir = DFLT_XKB_CONFIG_ROOT;
159 const char *context_path;
162 /* quick and dirty check that the default directory exists.
163 * It may not on a vanilla test box if we just run the test
164 * suite, so where it's not there just skip this test. */
165 struct stat stat_buf;
166 int err = stat(xkbdir, &stat_buf);
169 if (!S_ISDIR(stat_buf.st_mode))
172 buffer_env("XKB_CONFIG_ROOT");
174 buffer_env("XDG_CONFIG_HOME");
176 unsetenv("XKB_CONFIG_ROOT");
178 unsetenv("XDG_CONFIG_HOME");
180 /* built-in path is last */
181 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
182 nincludes = xkb_context_num_include_paths(ctx);
183 assert(nincludes >= 1);
184 context_path = xkb_context_include_path_get(ctx, nincludes - 1);
185 assert(strcmp(context_path, xkbdir) == 0);
186 xkb_context_unref(ctx);
193 test_xkbdir_include_path(void)
195 struct xkb_context *ctx;
197 const char *xkb_path;
198 const char *context_path;
201 buffer_env("XDG_CONFIG_HOME");
203 tmpdir = maketmpdir();
204 xkb_path = makedir(tmpdir, ".xkb");
205 setenv("HOME", tmpdir, 1);
206 setenv("XDG_CONFIG_HOME", tmpdir, 1);
208 /* No XDG directory in our tmpdir, so we expect
209 * the $HOME/.xkb to be the first include path */
210 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
211 assert(xkb_context_num_include_paths(ctx) >= 1);
212 context_path = xkb_context_include_path_get(ctx, 0);
213 assert(strcmp(context_path, xkb_path) == 0);
214 xkb_context_unref(ctx);
221 test_xdg_include_path(void)
223 struct xkb_context *ctx;
225 const char *xdg_path;
226 const char *context_path;
228 buffer_env("XDG_CONFIG_HOME");
230 tmpdir = maketmpdir();
231 xdg_path = makedir(tmpdir, "xkb");
232 setenv("XDG_CONFIG_HOME", tmpdir, 1);
234 /* XDG path is always first */
235 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
236 assert(xkb_context_num_include_paths(ctx) >= 1);
237 context_path = xkb_context_include_path_get(ctx, 0);
238 assert(strcmp(context_path, xdg_path) == 0);
239 xkb_context_unref(ctx);
246 test_xdg_include_path_fallback(void)
248 struct xkb_context *ctx;
250 const char *xdg_root, *xdg_path;
251 const char *context_path;
253 buffer_env("XDG_CONFIG_HOME");
256 tmpdir = maketmpdir();
257 xdg_root = makedir(tmpdir, ".config");
258 xdg_path = makedir(xdg_root, "xkb");
259 setenv("HOME", tmpdir, 1);
260 unsetenv("XDG_CONFIG_HOME");
262 /* XDG path is always first, even if fallback */
263 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
264 assert(xkb_context_num_include_paths(ctx) >= 1);
265 context_path = xkb_context_include_path_get(ctx, 0);
266 assert(strcmp(context_path, xdg_path) == 0);
267 xkb_context_unref(ctx);
274 test_include_order(void)
276 struct xkb_context *ctx;
278 const char *xdg_path;
279 const char *xkb_home_path;
280 const char *xkb_root_path;
281 const char *context_path;
283 buffer_env("XKB_CONFIG_ROOT");
284 buffer_env("XDG_CONFIG_HOME");
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);
295 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
296 assert(xkb_context_num_include_paths(ctx) >= 3);
298 context_path = xkb_context_include_path_get(ctx, 0);
299 assert(strcmp(context_path, xdg_path) == 0);
300 /* $HOME/.xkb is second */
301 context_path = xkb_context_include_path_get(ctx, 1);
302 assert(strcmp(context_path, xkb_home_path) == 0);
303 /* CONFIG_ROOT is last */
304 context_path = xkb_context_include_path_get(ctx, 2);
305 assert(strcmp(context_path, xkb_root_path) == 0);
307 xkb_context_unref(ctx);
316 struct xkb_context *context = test_get_context(0);
321 assert(xkb_context_num_include_paths(context) == 1);
322 assert(!xkb_context_include_path_append(context, "¡NONSENSE!"));
323 assert(xkb_context_num_include_paths(context) == 1);
325 atom = xkb_atom_intern(context, "HELLOjunkjunkjunk", 5);
326 assert(atom != XKB_ATOM_NONE);
327 assert(streq(xkb_atom_text(context, atom), "HELLO"));
329 atom = xkb_atom_intern_literal(context, "HELLOjunkjunkjunk");
330 assert(atom != XKB_ATOM_NONE);
331 assert(streq(xkb_atom_text(context, atom), "HELLOjunkjunkjunk"));
333 xkb_context_unref(context);
335 test_config_root_include_path();
336 test_config_root_include_path_fallback();
337 test_xkbdir_include_path();
338 test_xdg_include_path();
339 test_xdg_include_path_fallback();
340 test_include_order();