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