2 This file is part of systemd.
4 Copyright 2016 Lennart Poettering
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.
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.
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/>.
20 #include <alloc-util.h>
27 #include "path-util.h"
29 char* setup_fake_runtime_dir(void) {
30 char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p;
32 assert_se(mkdtemp(t));
33 assert_se(setenv("XDG_RUNTIME_DIR", t, 1) >= 0);
34 assert_se(p = strdup(t));
39 const char* get_testdata_dir(const char *suffix) {
41 /* convenience: caller does not need to free result */
42 static char testdir[PATH_MAX];
44 /* if the env var is set, use that */
45 env = getenv("SYSTEMD_TEST_DATA");
46 testdir[sizeof(testdir) - 1] = '\0';
48 if (access(env, F_OK) < 0) {
49 fputs("ERROR: $SYSTEMD_TEST_DATA directory does not exist\n", stderr);
52 strncpy(testdir, env, sizeof(testdir) - 1);
54 _cleanup_free_ char *exedir = NULL;
55 assert_se(readlink_and_make_absolute("/proc/self/exe", &exedir) >= 0);
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);
61 /* Try relative path, according to the install-test layout */
62 assert_se(snprintf(testdir, sizeof(testdir), "%s/testdata", dirname(exedir)) > 0);
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);
71 strncpy(testdir + strlen(testdir), suffix, sizeof(testdir) - strlen(testdir) - 1);