mirror of
https://github.com/cuberite/polarssl.git
synced 2025-09-09 23:26:29 -04:00
Support running unit tests from another directory
When running a test suite, try to change to the directory containing the executable. This allows running a test suite from any directory, and still allow it to access its .datax file as well as data files (generally in tests/data_files) used by individual test cases. Only implemented on Unix-like systems and on Windows. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
parent
37b8478ab8
commit
9cc1255e99
@ -432,6 +432,39 @@ static void write_outcome_result(FILE *outcome_file,
|
|||||||
fflush(outcome_file);
|
fflush(outcome_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(__unix__) || \
|
||||||
|
(defined(__APPLE__) && defined(__MACH__)) || \
|
||||||
|
defined(_WIN32)
|
||||||
|
#define MBEDTLS_HAVE_CHDIR
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_HAVE_CHDIR)
|
||||||
|
/** Try chdir to the directory containing argv0.
|
||||||
|
*
|
||||||
|
* Failures are silent.
|
||||||
|
*/
|
||||||
|
static void try_chdir(const char *argv0)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
#if defined(_WIN32)
|
||||||
|
(void) _chdir(path);
|
||||||
|
#else
|
||||||
|
(void) chdir(path);
|
||||||
|
#endif
|
||||||
|
mbedtls_free(path);
|
||||||
|
}
|
||||||
|
#endif /* MBEDTLS_HAVE_CHDIR */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Desktop implementation of execute_tests().
|
* \brief Desktop implementation of execute_tests().
|
||||||
* Parses command line and executes tests from
|
* Parses command line and executes tests from
|
||||||
|
@ -278,6 +278,21 @@ int main(int argc, const char *argv[])
|
|||||||
mbedtls_test_hook_error_add = &mbedtls_test_err_add_check;
|
mbedtls_test_hook_error_add = &mbedtls_test_err_add_check;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef MBEDTLS_HAVE_CHDIR
|
||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
try_chdir(argv[0]);
|
||||||
|
#endif /* MBEDTLS_HAVE_CHDIR */
|
||||||
|
|
||||||
int ret = mbedtls_test_platform_setup();
|
int ret = mbedtls_test_platform_setup();
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
mbedtls_fprintf(stderr,
|
mbedtls_fprintf(stderr,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user