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