Increase log verbosity in tests
[platform/upstream/libxkbcommon.git] / test / common.c
1 /*
2  * Copyright 2009 Dan Nicholson
3  * Copyright © 2012 Daniel Stone
4  * Copyright © 2012 Ran Benita
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
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/syslog.h>
39
40 #include "xkbcommon/xkbcommon.h"
41 #include "test.h"
42
43 const char *
44 test_get_path(const char *path_rel)
45 {
46     static char path[PATH_MAX];
47     const char *srcdir = getenv("srcdir");
48
49     snprintf(path, PATH_MAX - 1,
50              "%s/test/data/%s", srcdir ? srcdir : ".",
51              path_rel ? path_rel : "");
52
53     return path;
54 }
55
56 char *
57 test_read_file(const char *path_rel)
58 {
59     struct stat info;
60     char *ret, *tmp;
61     int fd, count, remaining;
62
63     fd = open(test_get_path(path_rel), O_RDONLY);
64     if (fd < 0)
65         return NULL;
66
67     if (fstat(fd, &info) != 0) {
68         close(fd);
69         return NULL;
70     }
71
72     ret = malloc(info.st_size + 1);
73     if (!ret) {
74         close(fd);
75         return NULL;
76     }
77
78     remaining = info.st_size;
79     tmp = ret;
80     while ((count = read(fd, tmp, remaining))) {
81         remaining -= count;
82         tmp += count;
83     }
84     ret[info.st_size] = '\0';
85     close(fd);
86
87     if (remaining != 0) {
88         free(ret);
89         return NULL;
90     }
91
92     return ret;
93 }
94
95 struct xkb_context *
96 test_get_context(void)
97 {
98     struct xkb_context *ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
99
100     if (!ctx)
101         return NULL;
102
103     xkb_context_include_path_append(ctx, test_get_path(""));
104
105     xkb_set_log_priority(ctx, LOG_DEBUG);
106     xkb_set_log_verbosity(ctx, 101);
107
108     return ctx;
109 }
110
111 struct xkb_keymap *
112 test_compile_file(struct xkb_context *context, const char *path_rel)
113 {
114     struct xkb_keymap *keymap;
115     FILE *file;
116     const char *path = test_get_path(path_rel);
117
118     file = fopen(path, "r");
119     if (!file) {
120         fprintf(stderr, "Failed to open path: %s\n", path);
121         return NULL;
122     }
123     assert(file != NULL);
124
125     keymap = xkb_map_new_from_file(context, file,
126                                    XKB_KEYMAP_FORMAT_TEXT_V1, 0);
127     fclose(file);
128
129     if (!keymap) {
130         fprintf(stderr, "Failed to compile path: %s\n", path);
131         return NULL;
132     }
133
134     fprintf(stderr, "Successfully compiled path: %s\n", path);
135
136     return keymap;
137 }
138
139 struct xkb_keymap *
140 test_compile_string(struct xkb_context *context, const char *string)
141 {
142     struct xkb_keymap *keymap;
143
144     keymap = xkb_map_new_from_string(context, string,
145                                      XKB_KEYMAP_FORMAT_TEXT_V1, 0);
146     if (!keymap) {
147         fprintf(stderr, "Failed to compile string\n");
148         return NULL;
149     }
150
151     return keymap;
152 }
153
154 struct xkb_keymap *
155 test_compile_rules(struct xkb_context *context, const char *rules,
156                    const char *model, const char *layout,
157                    const char *variant, const char *options)
158 {
159     struct xkb_keymap *keymap;
160     struct xkb_rule_names rmlvo = {
161         .rules = rules,
162         .model = model,
163         .layout = layout,
164         .variant = variant,
165         .options = options
166     };
167
168     keymap = xkb_map_new_from_names(context, &rmlvo, 0);
169     if (!keymap) {
170         fprintf(stderr,
171                 "Failed to compile RMLVO: '%s', '%s', '%s', '%s', '%s'\n",
172                 rules, model, layout, variant, options);
173         return NULL;
174     }
175
176     return keymap;
177 }