test: add test for the various default include paths All tests create a temporary directory, set up the environment for that directory and then check the include paths for the presence of that directory, ideally in the right position of the list. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
diff --git a/test/context.c b/test/context.c
index dc60888..8cb0a47 100644
--- a/test/context.c
+++ b/test/context.c
@@ -26,6 +26,279 @@
#include "test.h"
#include "context.h"
+#include <unistd.h>
+#include <sys/stat.h>
+#include <dirent.h>
+
+/* keeps a cache of all makedir/maketmpdir directories so we can free and
+ * rmdir them in one go, see unmakedirs() */
+char *dirnames[64];
+int ndirs;
+
+/* keeps a cache of all buffered env vars so we can restore
+ * them in one go, see restore_env() */
+struct env {
+ char *key;
+ char *value;
+} environment[64];
+int nenviron;
+
+static void buffer_env(const char *key)
+{
+ char *v = getenv(key);
+
+ environment[nenviron].key = strdup(key);
+ environment[nenviron].value = v ? strdup(v) : NULL;
+ nenviron++;
+}
+
+static void restore_env(void)
+{
+ for (int i = 0; i < nenviron; i++) {
+ char *key = environment[i].key,
+ *value = environment[i].value;
+
+ if (value)
+ setenv(key, value, 1);
+ else
+ unsetenv(key);
+
+ free(key);
+ free(value);
+ }
+ nenviron = 0;
+ memset(environment, 0, sizeof(environment));
+}
+
+static const char *makedir(const char *parent, const char *path)
+{
+ char *dirname;
+ int err;
+
+ err = asprintf(&dirname, "%s/%s", parent, path);
+ assert(err >= 0);
+ err = mkdir(dirname, 0777);
+ assert(err == 0);
+
+ dirnames[ndirs++] = dirname;
+
+ return dirname;
+}
+
+static const char *maketmpdir(void)
+{
+ const char *template = "/tmp/xkbcommon-test.XXXXXX";
+ char *tmpdir = strdup(template);
+
+ tmpdir = mkdtemp(tmpdir);
+ assert(tmpdir != NULL);
+
+ dirnames[ndirs++] = tmpdir;
+
+ return tmpdir;
+}
+
+static void unmakedirs(void)
+{
+ /* backwards order for rmdir to work */
+ for (int i = ndirs - 1; i >= 0; i--) {
+ char *dir = dirnames[i];
+ if (!dir)
+ break;
+ rmdir(dir);
+ free(dir);
+ }
+ ndirs = 0;
+ memset(dirnames, 0, sizeof(dirnames));
+}
+
+static void
+test_config_root_include_path(void)
+{
+ struct xkb_context *ctx;
+ const char *tmpdir;
+ const char *context_path;
+ int nincludes;
+
+ buffer_env("XKB_CONFIG_ROOT");
+ buffer_env("HOME");
+ buffer_env("XDG_CONFIG_HOME");
+
+ tmpdir = maketmpdir();
+ setenv("XKB_CONFIG_ROOT", tmpdir, 1);
+ unsetenv("HOME");
+ unsetenv("XDG_CONFIG_HOME");
+
+ /* built-in path is last */
+ ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
+ nincludes = xkb_context_num_include_paths(ctx);
+ assert(nincludes >= 1);
+ context_path = xkb_context_include_path_get(ctx, nincludes - 1);
+ assert(strcmp(context_path, tmpdir) == 0);
+ xkb_context_unref(ctx);
+
+ unmakedirs();
+ restore_env();
+}
+
+static void
+test_config_root_include_path_fallback(void)
+{
+ struct xkb_context *ctx;
+ const char *xkbdir = DFLT_XKB_CONFIG_ROOT;
+ const char *context_path;
+ int nincludes;
+ DIR *dir;
+
+ /* quick and dirty check that the default directory exists.
+ * It may not on a vanilla test box if we just run the test
+ * suite, so where it's not there just skip this test. */
+ dir = opendir(xkbdir);
+ if (!dir)
+ return;
+ closedir(dir);
+
+ buffer_env("XKB_CONFIG_ROOT");
+ buffer_env("HOME");
+ buffer_env("XDG_CONFIG_HOME");
+
+ unsetenv("XKB_CONFIG_ROOT");
+ unsetenv("HOME");
+ unsetenv("XDG_CONFIG_HOME");
+
+ /* built-in path is last */
+ ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
+ nincludes = xkb_context_num_include_paths(ctx);
+ assert(nincludes >= 1);
+ context_path = xkb_context_include_path_get(ctx, nincludes - 1);
+ assert(strcmp(context_path, xkbdir) == 0);
+ xkb_context_unref(ctx);
+
+ unmakedirs();
+ restore_env();
+}
+
+static void
+test_xkbdir_include_path(void)
+{
+ struct xkb_context *ctx;
+ const char *tmpdir;
+ const char *xkb_path;
+ const char *context_path;
+
+ buffer_env("HOME");
+ buffer_env("XDG_CONFIG_HOME");
+
+ tmpdir = maketmpdir();
+ xkb_path = makedir(tmpdir, ".xkb");
+ setenv("HOME", tmpdir, 1);
+ setenv("XDG_CONFIG_HOME", tmpdir, 1);
+
+ /* No XDG directory in our tmpdir, so we expect
+ * the $HOME/.xkb to be the first include path */
+ ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
+ assert(xkb_context_num_include_paths(ctx) >= 1);
+ context_path = xkb_context_include_path_get(ctx, 0);
+ assert(strcmp(context_path, xkb_path) == 0);
+ xkb_context_unref(ctx);
+
+ unmakedirs();
+ restore_env();
+}
+
+static void
+test_xdg_include_path(void)
+{
+ struct xkb_context *ctx;
+ const char *tmpdir;
+ const char *xdg_path;
+ const char *context_path;
+
+ buffer_env("XDG_CONFIG_HOME");
+
+ tmpdir = maketmpdir();
+ xdg_path = makedir(tmpdir, "xkb");
+ setenv("XDG_CONFIG_HOME", tmpdir, 1);
+
+ /* XDG path is always first */
+ ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
+ assert(xkb_context_num_include_paths(ctx) >= 1);
+ context_path = xkb_context_include_path_get(ctx, 0);
+ assert(strcmp(context_path, xdg_path) == 0);
+ xkb_context_unref(ctx);
+
+ unmakedirs();
+ restore_env();
+}
+
+static void
+test_xdg_include_path_fallback(void)
+{
+ struct xkb_context *ctx;
+ const char *tmpdir;
+ const char *xdg_root, *xdg_path;
+ const char *context_path;
+
+ buffer_env("XDG_CONFIG_HOME");
+ buffer_env("HOME");
+
+ tmpdir = maketmpdir();
+ xdg_root = makedir(tmpdir, ".config");
+ xdg_path = makedir(xdg_root, "xkb");
+ setenv("HOME", tmpdir, 1);
+ unsetenv("XDG_CONFIG_HOME");
+
+ /* XDG path is always first, even if fallback */
+ ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
+ assert(xkb_context_num_include_paths(ctx) >= 1);
+ context_path = xkb_context_include_path_get(ctx, 0);
+ assert(strcmp(context_path, xdg_path) == 0);
+ xkb_context_unref(ctx);
+
+ unmakedirs();
+ restore_env();
+}
+
+static void
+test_include_order(void)
+{
+ struct xkb_context *ctx;
+ const char *tmpdir;
+ const char *xdg_path;
+ const char *xkb_home_path;
+ const char *xkb_root_path;
+ const char *context_path;
+
+ buffer_env("XKB_CONFIG_ROOT");
+ buffer_env("XDG_CONFIG_HOME");
+ buffer_env("HOME");
+
+ tmpdir = maketmpdir();
+ xdg_path = makedir(tmpdir, "xkb");
+ xkb_home_path = makedir(tmpdir, ".xkb");
+ xkb_root_path = makedir(tmpdir, "xkbroot");
+ setenv("HOME", tmpdir, 1);
+ setenv("XDG_CONFIG_HOME", tmpdir, 1);
+ setenv("XKB_CONFIG_ROOT", xkb_root_path, 1);
+
+ ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
+ assert(xkb_context_num_include_paths(ctx) >= 3);
+ /* XDG is first */
+ context_path = xkb_context_include_path_get(ctx, 0);
+ assert(strcmp(context_path, xdg_path) == 0);
+ /* $HOME/.xkb is second */
+ context_path = xkb_context_include_path_get(ctx, 1);
+ assert(strcmp(context_path, xkb_home_path) == 0);
+ /* CONFIG_ROOT is last */
+ context_path = xkb_context_include_path_get(ctx, 2);
+ assert(strcmp(context_path, xkb_root_path) == 0);
+
+ xkb_context_unref(ctx);
+
+ unmakedirs();
+ restore_env();
+}
+
int
main(void)
{
@@ -48,5 +321,12 @@ main(void)
xkb_context_unref(context);
+ test_config_root_include_path();
+ test_config_root_include_path_fallback();
+ test_xkbdir_include_path();
+ test_xdg_include_path();
+ test_xdg_include_path_fallback();
+ test_include_order();
+
return 0;
}