Skip to content

Commit cc100a5

Browse files
committed
test: drop TEST_DATA_DIR, fold into get_testdata_dir()
Drop the TEST_DATA_DIR macro as this was using alloca() within a function call which is allegedly unsafe. So add a "suffix" argument to get_testdata_dir() instead and call that directly.
1 parent c60b6dd commit cc100a5

File tree

10 files changed

+37
-37
lines changed

10 files changed

+37
-37
lines changed

src/resolve/test-dns-packet.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@
2929
#include "resolved-dns-rr.h"
3030
#include "string-util.h"
3131
#include "strv.h"
32+
#include "tests.h"
3233
#include "unaligned.h"
3334

34-
#include "test-helper.h"
35-
3635
#define HASH_KEY SD_ID128_MAKE(d3,1e,48,90,4b,fa,4c,fe,af,9d,d5,a1,d7,2e,8a,b1)
3736

3837
static void verify_rr_copy(DnsResourceRecord *rr) {
@@ -117,7 +116,7 @@ int main(int argc, char **argv) {
117116
N = argc - 1;
118117
fnames = argv + 1;
119118
} else {
120-
assert_se(glob(TEST_DATA_DIR("/test-resolve/*.pkts"), GLOB_NOSORT, NULL, &g) == 0);
119+
assert_se(glob(get_testdata_dir("/test-resolve/*.pkts"), GLOB_NOSORT, NULL, &g) == 0);
121120
N = g.gl_pathc;
122121
fnames = g.gl_pathv;
123122
}

src/shared/tests.c

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,38 @@ char* setup_fake_runtime_dir(void) {
3636
return p;
3737
}
3838

39-
const char* get_testdata_dir(void) {
39+
const char* get_testdata_dir(const char *suffix) {
4040
const char *env;
41-
_cleanup_free_ char *exedir = NULL;
4241
/* convenience: caller does not need to free result */
4342
static char testdir[PATH_MAX];
4443

4544
/* if the env var is set, use that */
4645
env = getenv("SYSTEMD_TEST_DATA");
46+
testdir[sizeof(testdir) - 1] = '\0';
4747
if (env) {
48-
if (access(env, F_OK) >= 0)
49-
return env;
50-
51-
fputs("ERROR: $SYSTEMD_TEST_DATA directory does not exist\n", stderr);
52-
exit(1);
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+
}
5369
}
5470

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-
return ABS_SRC_DIR "/test";
60-
61-
/* Try relative path, according to the install-test layout */
62-
assert_se(snprintf(testdir, sizeof(testdir), "%s/testdata", dirname(exedir)) > 0);
63-
if (access(testdir, F_OK) >= 0)
64-
return testdir;
65-
66-
fputs("ERROR: Cannot find testdata directory, set $SYSTEMD_TEST_DATA\n", stderr);
67-
exit(1);
71+
strncpy(testdir + strlen(testdir), suffix, sizeof(testdir) - strlen(testdir) - 1);
72+
return testdir;
6873
}

src/shared/tests.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
***/
2121

2222
char* setup_fake_runtime_dir(void);
23-
const char* get_testdata_dir(void);
23+
const char* get_testdata_dir(const char *suffix);

src/test/test-cgroup-mask.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static int test_cgroup_mask(void) {
3535
int r;
3636

3737
/* Prepare the manager. */
38-
assert_se(set_unit_path(TEST_DATA_DIR("")) >= 0);
38+
assert_se(set_unit_path(get_testdata_dir("")) >= 0);
3939
assert_se(runtime_dir = setup_fake_runtime_dir());
4040
r = manager_new(UNIT_FILE_USER, true, &m);
4141
if (r == -EPERM || r == -EACCES) {

src/test/test-engine.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int main(int argc, char *argv[]) {
3838
int r;
3939

4040
/* prepare the test */
41-
assert_se(set_unit_path(TEST_DATA_DIR("")) >= 0);
41+
assert_se(set_unit_path(get_testdata_dir("")) >= 0);
4242
assert_se(runtime_dir = setup_fake_runtime_dir());
4343
r = manager_new(UNIT_FILE_USER, true, &m);
4444
if (MANAGER_SKIP_TEST(r)) {

src/test/test-execute.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#endif
3636
#include "stat-util.h"
3737
#include "test-helper.h"
38+
#include "tests.h"
3839
#include "unit.h"
3940
#include "util.h"
4041
#include "virt.h"
@@ -516,7 +517,7 @@ int main(int argc, char *argv[]) {
516517
}
517518

518519
assert_se(setenv("XDG_RUNTIME_DIR", "/tmp/", 1) == 0);
519-
assert_se(set_unit_path(TEST_DATA_DIR("/test-execute/")) >= 0);
520+
assert_se(set_unit_path(get_testdata_dir("/test-execute")) >= 0);
520521

521522
/* Unset VAR1, VAR2 and VAR3 which are used in the PassEnvironment test
522523
* cases, otherwise (and if they are present in the environment),

src/test/test-helper.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
***/
2121

2222
#include "sd-daemon.h"
23-
#include "string-util.h"
24-
#include "tests.h"
2523

2624
#include "macro.h"
2725

@@ -41,6 +39,3 @@
4139
-ENOENT, \
4240
-ENOMEDIUM /* cannot determine cgroup */ \
4341
)
44-
45-
#define TEST_DATA_DIR(subdir) \
46-
strjoina(get_testdata_dir(), subdir)

src/test/test-journal-importer.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "log.h"
2525
#include "journal-importer.h"
2626
#include "string-util.h"
27-
#include "test-helper.h"
27+
#include "tests.h"
2828

2929
static void assert_iovec_entry(const struct iovec *iovec, const char* content) {
3030
assert_se(strlen(content) == iovec->iov_len);
@@ -39,7 +39,7 @@ static void test_basic_parsing(void) {
3939
_cleanup_(journal_importer_cleanup) JournalImporter imp = {};
4040
int r;
4141

42-
imp.fd = open(TEST_DATA_DIR("/journal-data/journal-1.txt"), O_RDONLY|O_CLOEXEC);
42+
imp.fd = open(get_testdata_dir("/journal-data/journal-1.txt"), O_RDONLY|O_CLOEXEC);
4343
assert_se(imp.fd >= 0);
4444

4545
do
@@ -68,7 +68,7 @@ static void test_bad_input(void) {
6868
_cleanup_(journal_importer_cleanup) JournalImporter imp = {};
6969
int r;
7070

71-
imp.fd = open(TEST_DATA_DIR("/journal-data/journal-2.txt"), O_RDONLY|O_CLOEXEC);
71+
imp.fd = open(get_testdata_dir("/journal-data/journal-2.txt"), O_RDONLY|O_CLOEXEC);
7272
assert_se(imp.fd >= 0);
7373

7474
do

src/test/test-path.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ int main(int argc, char *argv[]) {
262262
log_parse_environment();
263263
log_open();
264264

265-
assert_se(set_unit_path(TEST_DATA_DIR("/test-path/")) >= 0);
265+
assert_se(set_unit_path(get_testdata_dir("/test-path")) >= 0);
266266
assert_se(runtime_dir = setup_fake_runtime_dir());
267267

268268
for (test = tests; test && *test; test++) {

src/test/test-sched-prio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ int main(int argc, char *argv[]) {
3535
int r;
3636

3737
/* prepare the test */
38-
assert_se(set_unit_path(TEST_DATA_DIR("")) >= 0);
38+
assert_se(set_unit_path(get_testdata_dir("")) >= 0);
3939
assert_se(runtime_dir = setup_fake_runtime_dir());
4040
r = manager_new(UNIT_FILE_USER, true, &m);
4141
if (MANAGER_SKIP_TEST(r)) {

0 commit comments

Comments
 (0)