]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/tests.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / shared / tests.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
d2120590 2
f853c6ef
MP
3#include <alloc-util.h>
4#include <fs-util.h>
5#include <libgen.h>
d2120590
LP
6#include <stdlib.h>
7#include <util.h>
8
9#include "tests.h"
1f35a3b2 10#include "path-util.h"
d2120590
LP
11
12char* setup_fake_runtime_dir(void) {
13 char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p;
14
15 assert_se(mkdtemp(t));
16 assert_se(setenv("XDG_RUNTIME_DIR", t, 1) >= 0);
17 assert_se(p = strdup(t));
18
19 return p;
20}
f853c6ef 21
cc100a5a 22const char* get_testdata_dir(const char *suffix) {
c60b6dda 23 const char *env;
f853c6ef
MP
24 /* convenience: caller does not need to free result */
25 static char testdir[PATH_MAX];
26
c60b6dda
MP
27 /* if the env var is set, use that */
28 env = getenv("SYSTEMD_TEST_DATA");
cc100a5a 29 testdir[sizeof(testdir) - 1] = '\0';
c60b6dda 30 if (env) {
cc100a5a
MP
31 if (access(env, F_OK) < 0) {
32 fputs("ERROR: $SYSTEMD_TEST_DATA directory does not exist\n", stderr);
a45d7127 33 exit(EXIT_FAILURE);
cc100a5a
MP
34 }
35 strncpy(testdir, env, sizeof(testdir) - 1);
36 } else {
37 _cleanup_free_ char *exedir = NULL;
38 assert_se(readlink_and_make_absolute("/proc/self/exe", &exedir) >= 0);
39
40 /* Check if we're running from the builddir. If so, use the compiled in path. */
41 if (path_startswith(exedir, ABS_BUILD_DIR))
42 assert_se(snprintf(testdir, sizeof(testdir), "%s/test", ABS_SRC_DIR) > 0);
43 else
44 /* Try relative path, according to the install-test layout */
45 assert_se(snprintf(testdir, sizeof(testdir), "%s/testdata", dirname(exedir)) > 0);
46
47 /* test this without the suffix, as it may contain a glob */
48 if (access(testdir, F_OK) < 0) {
49 fputs("ERROR: Cannot find testdata directory, set $SYSTEMD_TEST_DATA\n", stderr);
a45d7127 50 exit(EXIT_FAILURE);
cc100a5a 51 }
c60b6dda
MP
52 }
53
cc100a5a
MP
54 strncpy(testdir + strlen(testdir), suffix, sizeof(testdir) - strlen(testdir) - 1);
55 return testdir;
f853c6ef 56}