test/context: use a more portable directory-exists check
[platform/upstream/libxkbcommon.git] / test / context.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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.
22  *
23  * Author: Daniel Stone <daniel@fooishbar.org>
24  */
25
26 #include "config.h"
27
28 #include "test.h"
29 #include "context.h"
30
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34
35 /* keeps a cache of all makedir/maketmpdir directories so we can free and
36  * rmdir them in one go, see unmakedirs() */
37 char *dirnames[64];
38 int ndirs;
39
40 /* keeps a cache of all buffered env vars so we can restore
41  * them in one go, see restore_env() */
42 struct env {
43     char *key;
44     char *value;
45 } environment[64];
46 int nenviron;
47
48 static void buffer_env(const char *key)
49 {
50     char *v = getenv(key);
51
52     environment[nenviron].key = strdup(key);
53     environment[nenviron].value = v ? strdup(v) : NULL;
54     nenviron++;
55 }
56
57 static void restore_env(void)
58 {
59     for (int i = 0; i < nenviron; i++) {
60         char *key = environment[i].key,
61              *value = environment[i].value;
62
63         if (value)
64             setenv(key, value, 1);
65         else
66             unsetenv(key);
67
68         free(key);
69         free(value);
70     }
71     nenviron = 0;
72     memset(environment, 0, sizeof(environment));
73 }
74
75 static const char *makedir(const char *parent, const char *path)
76 {
77     char *dirname;
78     int err;
79
80     err = asprintf(&dirname, "%s/%s", parent, path);
81     assert(err >= 0);
82     err = mkdir(dirname, 0777);
83     assert(err == 0);
84
85     dirnames[ndirs++] = dirname;
86
87     return dirname;
88 }
89
90 static const char *maketmpdir(void)
91 {
92     const char *template = "/tmp/xkbcommon-test.XXXXXX";
93     char *tmpdir = strdup(template);
94
95     tmpdir = mkdtemp(tmpdir);
96     assert(tmpdir != NULL);
97
98     dirnames[ndirs++] = tmpdir;
99
100     return tmpdir;
101 }
102
103 static void unmakedirs(void)
104 {
105     /* backwards order for rmdir to work */
106     for (int i = ndirs - 1; i >= 0; i--) {
107         char *dir = dirnames[i];
108         if (!dir)
109             break;
110         rmdir(dir);
111         free(dir);
112     }
113     ndirs = 0;
114     memset(dirnames, 0, sizeof(dirnames));
115 }
116
117 static void
118 test_config_root_include_path(void)
119 {
120     struct xkb_context *ctx;
121     const char *tmpdir;
122     const char *context_path;
123     int nincludes;
124
125     buffer_env("XKB_CONFIG_ROOT");
126     buffer_env("HOME");
127     buffer_env("XDG_CONFIG_HOME");
128
129     tmpdir = maketmpdir();
130     setenv("XKB_CONFIG_ROOT", tmpdir, 1);
131     unsetenv("HOME");
132     unsetenv("XDG_CONFIG_HOME");
133
134     /* built-in path is last */
135     ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
136     nincludes = xkb_context_num_include_paths(ctx);
137     assert(nincludes >= 1);
138     context_path = xkb_context_include_path_get(ctx, nincludes - 1);
139     assert(strcmp(context_path, tmpdir) == 0);
140     xkb_context_unref(ctx);
141
142     unmakedirs();
143     restore_env();
144 }
145
146 static void
147 test_config_root_include_path_fallback(void)
148 {
149     struct xkb_context *ctx;
150     const char *xkbdir = DFLT_XKB_CONFIG_ROOT;
151     const char *context_path;
152     int nincludes;
153
154     /* quick and dirty check that the default directory exists.
155      * It may not on a vanilla test box if we just run the test
156      * suite, so where it's not there just skip this test. */
157     struct stat stat_buf;
158     int err = stat(xkbdir, &stat_buf);
159     if (err != 0)
160         return;
161     if (!S_ISDIR(stat_buf.st_mode))
162         return;
163
164     buffer_env("XKB_CONFIG_ROOT");
165     buffer_env("HOME");
166     buffer_env("XDG_CONFIG_HOME");
167
168     unsetenv("XKB_CONFIG_ROOT");
169     unsetenv("HOME");
170     unsetenv("XDG_CONFIG_HOME");
171
172     /* built-in path is last */
173     ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
174     nincludes = xkb_context_num_include_paths(ctx);
175     assert(nincludes >= 1);
176     context_path = xkb_context_include_path_get(ctx, nincludes - 1);
177     assert(strcmp(context_path, xkbdir) == 0);
178     xkb_context_unref(ctx);
179
180     unmakedirs();
181     restore_env();
182 }
183
184 static void
185 test_xkbdir_include_path(void)
186 {
187     struct xkb_context *ctx;
188     const char *tmpdir;
189     const char *xkb_path;
190     const char *context_path;
191
192     buffer_env("HOME");
193     buffer_env("XDG_CONFIG_HOME");
194
195     tmpdir = maketmpdir();
196     xkb_path = makedir(tmpdir, ".xkb");
197     setenv("HOME", tmpdir, 1);
198     setenv("XDG_CONFIG_HOME", tmpdir, 1);
199
200     /* No XDG directory in our tmpdir, so we expect
201      * the $HOME/.xkb to be the first include path */
202     ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
203     assert(xkb_context_num_include_paths(ctx) >= 1);
204     context_path = xkb_context_include_path_get(ctx, 0);
205     assert(strcmp(context_path, xkb_path) == 0);
206     xkb_context_unref(ctx);
207
208     unmakedirs();
209     restore_env();
210 }
211
212 static void
213 test_xdg_include_path(void)
214 {
215     struct xkb_context *ctx;
216     const char *tmpdir;
217     const char *xdg_path;
218     const char *context_path;
219
220     buffer_env("XDG_CONFIG_HOME");
221
222     tmpdir = maketmpdir();
223     xdg_path = makedir(tmpdir, "xkb");
224     setenv("XDG_CONFIG_HOME", tmpdir, 1);
225
226     /* XDG path is always first */
227     ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
228     assert(xkb_context_num_include_paths(ctx) >= 1);
229     context_path = xkb_context_include_path_get(ctx, 0);
230     assert(strcmp(context_path, xdg_path) == 0);
231     xkb_context_unref(ctx);
232
233     unmakedirs();
234     restore_env();
235 }
236
237 static void
238 test_xdg_include_path_fallback(void)
239 {
240     struct xkb_context *ctx;
241     const char *tmpdir;
242     const char *xdg_root, *xdg_path;
243     const char *context_path;
244
245     buffer_env("XDG_CONFIG_HOME");
246     buffer_env("HOME");
247
248     tmpdir = maketmpdir();
249     xdg_root = makedir(tmpdir, ".config");
250     xdg_path = makedir(xdg_root, "xkb");
251     setenv("HOME", tmpdir, 1);
252     unsetenv("XDG_CONFIG_HOME");
253
254     /* XDG path is always first, even if fallback */
255     ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
256     assert(xkb_context_num_include_paths(ctx) >= 1);
257     context_path = xkb_context_include_path_get(ctx, 0);
258     assert(strcmp(context_path, xdg_path) == 0);
259     xkb_context_unref(ctx);
260
261     unmakedirs();
262     restore_env();
263 }
264
265 static void
266 test_include_order(void)
267 {
268     struct xkb_context *ctx;
269     const char *tmpdir;
270     const char *xdg_path;
271     const char *xkb_home_path;
272     const char *xkb_root_path;
273     const char *context_path;
274
275     buffer_env("XKB_CONFIG_ROOT");
276     buffer_env("XDG_CONFIG_HOME");
277     buffer_env("HOME");
278
279     tmpdir = maketmpdir();
280     xdg_path = makedir(tmpdir, "xkb");
281     xkb_home_path = makedir(tmpdir, ".xkb");
282     xkb_root_path = makedir(tmpdir, "xkbroot");
283     setenv("HOME", tmpdir, 1);
284     setenv("XDG_CONFIG_HOME", tmpdir, 1);
285     setenv("XKB_CONFIG_ROOT", xkb_root_path, 1);
286
287     ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
288     assert(xkb_context_num_include_paths(ctx) >= 3);
289     /* XDG is first */
290     context_path = xkb_context_include_path_get(ctx, 0);
291     assert(strcmp(context_path, xdg_path) == 0);
292     /* $HOME/.xkb is second */
293     context_path = xkb_context_include_path_get(ctx, 1);
294     assert(strcmp(context_path, xkb_home_path) == 0);
295     /* CONFIG_ROOT is last */
296     context_path = xkb_context_include_path_get(ctx, 2);
297     assert(strcmp(context_path, xkb_root_path) == 0);
298
299     xkb_context_unref(ctx);
300
301     unmakedirs();
302     restore_env();
303 }
304
305 int
306 main(void)
307 {
308     struct xkb_context *context = test_get_context(0);
309     xkb_atom_t atom;
310
311     assert(context);
312
313     assert(xkb_context_num_include_paths(context) == 1);
314     assert(!xkb_context_include_path_append(context, "¡NONSENSE!"));
315     assert(xkb_context_num_include_paths(context) == 1);
316
317     atom = xkb_atom_intern(context, "HELLOjunkjunkjunk", 5);
318     assert(atom != XKB_ATOM_NONE);
319     assert(streq(xkb_atom_text(context, atom), "HELLO"));
320
321     atom = xkb_atom_intern_literal(context, "HELLOjunkjunkjunk");
322     assert(atom != XKB_ATOM_NONE);
323     assert(streq(xkb_atom_text(context, atom), "HELLOjunkjunkjunk"));
324
325     xkb_context_unref(context);
326
327     test_config_root_include_path();
328     test_config_root_include_path_fallback();
329     test_xkbdir_include_path();
330     test_xdg_include_path();
331     test_xdg_include_path_fallback();
332     test_include_order();
333
334     return 0;
335 }