Merge pull request #8294 from gilles-peskine-arm/test-datax-chdir

Support running unit tests from another directory
This commit is contained in:
Gilles Peskine 2023-10-26 15:53:10 +00:00 committed by GitHub
commit 4f144093ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 0 deletions

View file

@ -432,6 +432,50 @@ static void write_outcome_result(FILE *outcome_file,
fflush(outcome_file);
}
#if defined(__unix__) || \
(defined(__APPLE__) && defined(__MACH__))
#define MBEDTLS_HAVE_CHDIR
#endif
#if defined(MBEDTLS_HAVE_CHDIR)
/** Try chdir to the directory containing argv0.
*
* Failures are silent.
*/
static void try_chdir_if_supported(const char *argv0)
{
/* We might want to allow backslash as well, for Windows. But then we also
* need to consider chdir() vs _chdir(), and different conventions
* regarding paths in argv[0] (naively enabling this code with
* backslash support on Windows leads to chdir into the wrong directory
* on the CI). */
const char *slash = strrchr(argv0, '/');
if (slash == NULL) {
return;
}
size_t path_size = slash - argv0 + 1;
char *path = mbedtls_calloc(1, path_size);
if (path == NULL) {
return;
}
memcpy(path, argv0, path_size - 1);
path[path_size - 1] = 0;
int ret = chdir(path);
if (ret != 0) {
mbedtls_fprintf(stderr, "%s: note: chdir(\"%s\") failed.\n",
__func__, path);
}
mbedtls_free(path);
}
#else /* MBEDTLS_HAVE_CHDIR */
/* No chdir() or no support for parsing argv[0] on this platform. */
static void try_chdir_if_supported(const char *argv0)
{
(void) argv0;
return;
}
#endif /* MBEDTLS_HAVE_CHDIR */
/**
* \brief Desktop implementation of execute_tests().
* Parses command line and executes tests from

View file

@ -237,6 +237,21 @@ int main(int argc, const char *argv[])
#endif
#endif
/* Try changing to the directory containing the executable, if
* using the default data file. This allows running the executable
* from another directory (e.g. the project root) and still access
* the .datax file as well as data files used by test cases
* (typically from tests/data_files).
*
* Note that we do this before the platform setup (which may access
* files such as a random seed). We also do this before accessing
* test-specific files such as the outcome file, which is arguably
* not desirable and should be fixed later.
*/
if (argc == 1) {
try_chdir_if_supported(argv[0]);
}
int ret = mbedtls_test_platform_setup();
if (ret != 0) {
mbedtls_fprintf(stderr,