Fix build error
[platform/upstream/libxkbcommon.git] / test / common.c
1 /*
2  * Copyright © 2009 Dan Nicholson <dbn.lists@gmail.com>
3  * Copyright © 2012 Intel Corporation
4  * Copyright © 2012 Ran Benita <ran234@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Except as contained in this notice, the names of the authors or their
24  * institutions shall not be used in advertising or otherwise to promote the
25  * sale, use or other dealings in this Software without prior written
26  * authorization from the authors.
27  *
28  * Author: Dan Nicholson <dbn.lists@gmail.com>
29  *         Daniel Stone <daniel@fooishbar.org>
30  *         Ran Benita <ran234@gmail.com>
31  */
32
33 #include "config.h"
34
35 #include <limits.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #ifdef _WIN32
41 #include <io.h>
42 #include <windows.h>
43 #else
44 #include <unistd.h>
45 #include <termios.h>
46 #endif
47
48 #include "test.h"
49 #include "utils.h"
50
51 /*
52  * Test a sequence of keysyms, resulting from a sequence of key presses,
53  * against the keysyms they're supposed to generate.
54  *
55  * - Each test runs with a clean state.
56  * - Each line in the test is made up of:
57  *   + A keycode, given as a KEY_* from linux/input.h.
58  *   + A direction - DOWN for press, UP for release, BOTH for
59  *     immediate press + release, REPEAT to just get the syms.
60  *   + A sequence of keysyms that should result from this keypress.
61  *
62  * The vararg format is:
63  * <KEY_*>  <DOWN | UP | BOTH>  <XKB_KEY_* (zero or more)>  <NEXT | FINISH>
64  *
65  * See below for examples.
66  */
67 int
68 test_key_seq_va(struct xkb_keymap *keymap, va_list ap)
69 {
70     struct xkb_state *state;
71
72     xkb_keycode_t kc;
73     int op;
74     xkb_keysym_t keysym;
75
76     const xkb_keysym_t *syms;
77     xkb_keysym_t sym;
78     unsigned int nsyms, i;
79     char ksbuf[64];
80
81     fprintf(stderr, "----\n");
82
83     state = xkb_state_new(keymap);
84     assert(state);
85
86     for (;;) {
87         kc = va_arg(ap, int) + EVDEV_OFFSET;
88         op = va_arg(ap, int);
89
90         nsyms = xkb_state_key_get_syms(state, kc, &syms);
91         if (nsyms == 1) {
92             sym = xkb_state_key_get_one_sym(state, kc);
93             syms = &sym;
94         }
95
96         fprintf(stderr, "got %u syms for keycode %u: [", nsyms, kc);
97
98         if (op == DOWN || op == BOTH)
99             xkb_state_update_key(state, kc, XKB_KEY_DOWN);
100         if (op == UP || op == BOTH)
101             xkb_state_update_key(state, kc, XKB_KEY_UP);
102
103         for (i = 0; i < nsyms; i++) {
104             keysym = va_arg(ap, int);
105             xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
106             fprintf(stderr, "%s%s", (i != 0) ? ", " : "", ksbuf);
107
108             if (keysym == FINISH || keysym == NEXT) {
109                 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
110                 fprintf(stderr, "Did not expect keysym: %s.\n", ksbuf);
111                 goto fail;
112             }
113
114             if (keysym != syms[i]) {
115                 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
116                 fprintf(stderr, "Expected keysym: %s. ", ksbuf);;
117                 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
118                 fprintf(stderr, "Got keysym: %s.\n", ksbuf);;
119                 goto fail;
120             }
121         }
122
123         if (nsyms == 0) {
124             keysym = va_arg(ap, int);
125             if (keysym != XKB_KEY_NoSymbol) {
126                 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
127                 fprintf(stderr, "Expected %s, but got no keysyms.\n", ksbuf);
128                 goto fail;
129             }
130         }
131
132         fprintf(stderr, "]\n");
133
134         keysym = va_arg(ap, int);
135         if (keysym == NEXT)
136             continue;
137         if (keysym == FINISH)
138             break;
139
140         xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
141         fprintf(stderr, "Expected keysym: %s. Didn't get it.\n", ksbuf);
142         goto fail;
143     }
144
145     xkb_state_unref(state);
146     return 1;
147
148 fail:
149     xkb_state_unref(state);
150     return 0;
151 }
152
153 int
154 test_key_seq(struct xkb_keymap *keymap, ...)
155 {
156     va_list ap;
157     int ret;
158
159     va_start(ap, keymap);
160     ret = test_key_seq_va(keymap, ap);
161     va_end(ap);
162
163     return ret;
164 }
165
166 char *
167 test_makedir(const char *parent, const char *path)
168 {
169     char *dirname;
170     int err;
171
172     dirname = asprintf_safe("%s/%s", parent, path);
173     assert(dirname);
174 #ifdef _WIN32
175     err = _mkdir(dirname);
176 #else
177     err = mkdir(dirname, 0777);
178 #endif
179     assert(err == 0);
180
181     return dirname;
182 }
183
184 char *
185 test_maketempdir(const char *template)
186 {
187 #ifdef _WIN32
188     const char *basetmp = getenv("TMP");
189     if (basetmp == NULL) {
190         basetmp = getenv("TEMP");
191     }
192     if (basetmp == NULL) {
193         basetmp = getenv("top_builddir");
194     }
195     assert(basetmp != NULL);
196     char *tmpdir = asprintf_safe("%s/%s", basetmp, template);
197     assert(tmpdir != NULL);
198     char *tmp = _mktemp(tmpdir);
199     assert(tmp == tmpdir);
200     int ret = _mkdir(tmp);
201     assert(ret == 0);
202     return tmpdir;
203 #else
204     char *tmpdir = asprintf_safe("/tmp/%s", template);
205     assert(tmpdir != NULL);
206     char *tmp = mkdtemp(tmpdir);
207     assert(tmp == tmpdir);
208     return tmpdir;
209 #endif
210 }
211
212 char *
213 test_get_path(const char *path_rel)
214 {
215     char *path;
216     const char *srcdir;
217
218     srcdir = getenv("top_srcdir");
219     if (!srcdir)
220         srcdir = ".";
221
222     if (path_rel[0] == '/')
223         return strdup(path_rel);
224
225     path = asprintf_safe("%s/test/data%s%s", srcdir,
226                          path_rel[0] ? "/" : "", path_rel);
227     if (!path) {
228         fprintf(stderr, "Failed to allocate path for %s\n", path_rel);
229         return NULL;
230     }
231     return path;
232 }
233
234 char *
235 test_read_file(const char *path_rel)
236 {
237     struct stat info;
238     char *ret, *tmp, *path;
239     int fd, count, remaining;
240
241     path = test_get_path(path_rel);
242     if (!path)
243         return NULL;
244
245     fd = open(path, O_RDONLY);
246     free(path);
247     if (fd < 0)
248         return NULL;
249
250     if (fstat(fd, &info) != 0) {
251         close(fd);
252         return NULL;
253     }
254
255     ret = malloc(info.st_size + 1);
256     if (!ret) {
257         close(fd);
258         return NULL;
259     }
260
261     remaining = info.st_size;
262     tmp = ret;
263     while ((count = read(fd, tmp, remaining))) {
264         remaining -= count;
265         tmp += count;
266     }
267     ret[info.st_size] = '\0';
268     close(fd);
269
270     if (remaining != 0) {
271         free(ret);
272         return NULL;
273     }
274
275     return ret;
276 }
277
278 struct xkb_context *
279 test_get_context(enum test_context_flags test_flags)
280 {
281     enum xkb_context_flags ctx_flags;
282     struct xkb_context *ctx;
283     char *path;
284
285     ctx_flags = XKB_CONTEXT_NO_DEFAULT_INCLUDES;
286     if (test_flags & CONTEXT_ALLOW_ENVIRONMENT_NAMES) {
287         unsetenv("XKB_DEFAULT_RULES");
288         unsetenv("XKB_DEFAULT_MODEL");
289         unsetenv("XKB_DEFAULT_LAYOUT");
290         unsetenv("XKB_DEFAULT_VARIANT");
291         unsetenv("XKB_DEFAULT_OPTIONS");
292     }
293     else {
294         ctx_flags |= XKB_CONTEXT_NO_ENVIRONMENT_NAMES;
295     }
296
297     ctx = xkb_context_new(ctx_flags);
298     if (!ctx)
299         return NULL;
300
301     path = test_get_path("");
302     if (!path) {
303         xkb_context_unref(ctx);
304         return NULL;
305     }
306
307     xkb_context_include_path_append(ctx, path);
308     free(path);
309
310     return ctx;
311 }
312
313 struct xkb_keymap *
314 test_compile_file(struct xkb_context *context, const char *path_rel)
315 {
316     struct xkb_keymap *keymap;
317     FILE *file;
318     char *path;
319
320     path = test_get_path(path_rel);
321     if (!path)
322         return NULL;
323
324     file = fopen(path, "rb");
325     if (!file) {
326         fprintf(stderr, "Failed to open path: %s\n", path);
327         free(path);
328         return NULL;
329     }
330     assert(file != NULL);
331
332     keymap = xkb_keymap_new_from_file(context, file,
333                                       XKB_KEYMAP_FORMAT_TEXT_V1, 0);
334     fclose(file);
335
336     if (!keymap) {
337         fprintf(stderr, "Failed to compile path: %s\n", path);
338         free(path);
339         return NULL;
340     }
341
342     fprintf(stderr, "Successfully compiled path: %s\n", path);
343     free(path);
344
345     return keymap;
346 }
347
348 struct xkb_keymap *
349 test_compile_string(struct xkb_context *context, const char *string)
350 {
351     struct xkb_keymap *keymap;
352
353     keymap = xkb_keymap_new_from_string(context, string,
354                                         XKB_KEYMAP_FORMAT_TEXT_V1, 0);
355     if (!keymap) {
356         fprintf(stderr, "Failed to compile string\n");
357         return NULL;
358     }
359
360     return keymap;
361 }
362
363 struct xkb_keymap *
364 test_compile_buffer(struct xkb_context *context, const char *buf, size_t len)
365 {
366     struct xkb_keymap *keymap;
367
368     keymap = xkb_keymap_new_from_buffer(context, buf, len,
369                                         XKB_KEYMAP_FORMAT_TEXT_V1, 0);
370     if (!keymap) {
371         fprintf(stderr, "Failed to compile keymap from memory buffer\n");
372         return NULL;
373     }
374
375     return keymap;
376 }
377
378 struct xkb_keymap *
379 test_compile_rules(struct xkb_context *context, const char *rules,
380                    const char *model, const char *layout,
381                    const char *variant, const char *options)
382 {
383     struct xkb_keymap *keymap;
384     struct xkb_rule_names rmlvo = {
385         .rules = isempty(rules) ? NULL : rules,
386         .model = isempty(model) ? NULL : model,
387         .layout = isempty(layout) ? NULL : layout,
388         .variant = isempty(variant) ? NULL : variant,
389         .options = isempty(options) ? NULL : options
390     };
391
392     if (!rules && !model && !layout && !variant && !options)
393         keymap = xkb_keymap_new_from_names(context, NULL, 0);
394     else
395         keymap = xkb_keymap_new_from_names(context, &rmlvo, 0);
396
397     if (!keymap) {
398         fprintf(stderr,
399                 "Failed to compile RMLVO: '%s', '%s', '%s', '%s', '%s'\n",
400                 rules, model, layout, variant, options);
401         return NULL;
402     }
403
404     return keymap;
405 }