chiark / gitweb /
Prep v234: Apply missing upstream fixes in src/login (3/6)
[elogind.git] / src / shared / tests.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2016 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <alloc-util.h>
21 #include <fs-util.h>
22 #include <libgen.h>
23 #include <stdlib.h>
24 #include <util.h>
25
26 #include "tests.h"
27 #include "path-util.h"
28
29 char* setup_fake_runtime_dir(void) {
30         char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p;
31
32         assert_se(mkdtemp(t));
33         assert_se(setenv("XDG_RUNTIME_DIR", t, 1) >= 0);
34         assert_se(p = strdup(t));
35
36         return p;
37 }
38
39 const char* get_testdata_dir(const char *suffix) {
40         const char *env;
41         /* convenience: caller does not need to free result */
42         static char testdir[PATH_MAX];
43
44         /* if the env var is set, use that */
45         env = getenv("SYSTEMD_TEST_DATA");
46         testdir[sizeof(testdir) - 1] = '\0';
47         if (env) {
48                 if (access(env, F_OK) < 0) {
49                         fputs("ERROR: $SYSTEMD_TEST_DATA directory does not exist\n", stderr);
50                         exit(1);
51                 }
52                 strncpy(testdir, env, sizeof(testdir) - 1);
53         } else {
54                 _cleanup_free_ char *exedir = NULL;
55                 assert_se(readlink_and_make_absolute("/proc/self/exe", &exedir) >= 0);
56
57                 /* Check if we're running from the builddir. If so, use the compiled in path. */
58                 if (path_startswith(exedir, ABS_BUILD_DIR))
59                         assert_se(snprintf(testdir, sizeof(testdir), "%s/test", ABS_SRC_DIR) > 0);
60                 else
61                         /* Try relative path, according to the install-test layout */
62                         assert_se(snprintf(testdir, sizeof(testdir), "%s/testdata", dirname(exedir)) > 0);
63
64                 /* test this without the suffix, as it may contain a glob */
65                 if (access(testdir, F_OK) < 0) {
66                         fputs("ERROR: Cannot find testdata directory, set $SYSTEMD_TEST_DATA\n", stderr);
67                         exit(1);
68                 }
69         }
70
71         strncpy(testdir + strlen(testdir), suffix, sizeof(testdir) - strlen(testdir) - 1);
72         return testdir;
73 }