@@ -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}
0 commit comments