From 0dd2b375c770854f73fa64b8e43c741e074de7e6 Mon Sep 17 00:00:00 2001 From: Anderson Toshiyuki Sasaki Date: Tue, 20 Nov 2018 14:01:57 +0100 Subject: [PATCH] tests: Introduce functions to change directories This introduces torture_get_current_working_dir() and torture_change_dir() to allow changing directories in tests. Signed-off-by: Anderson Toshiyuki Sasaki Reviewed-by: Andreas Schneider --- tests/torture.c | 60 ++++++++++++++++++++ tests/torture.h | 3 + tests/unittests/CMakeLists.txt | 1 + tests/unittests/torture_push_pop_dir.c | 78 ++++++++++++++++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 tests/unittests/torture_push_pop_dir.c diff --git a/tests/torture.c b/tests/torture.c index f7ed775e..88387edb 100644 --- a/tests/torture.c +++ b/tests/torture.c @@ -39,11 +39,13 @@ #ifdef HAVE_UNISTD_H #include #elif (defined _WIN32) || (defined _WIN64) +#include #include #define read _read #define open _open #define write _write #define close _close +#define chdir _chdir #endif #include "torture.h" @@ -842,6 +844,28 @@ end: return new_file; } +char *torture_get_current_working_dir(void) +{ + + char *cwd = NULL; + char *result = NULL; + + cwd = (char *)malloc(PATH_MAX + 1); + if (cwd == NULL) { + goto end; + } + + result = getcwd(cwd, PATH_MAX); + + if (result == NULL) { + SAFE_FREE(cwd); + goto end; + } + +end: + return cwd; +} + #else /* _WIN32 */ char *torture_make_temp_dir(const char *template) @@ -1072,8 +1096,44 @@ end: return path; } +char *torture_get_current_working_dir(void) +{ + char *cwd = NULL; + char *result = NULL; + + cwd = (char *)malloc(_MAX_PATH + 1); + if (cwd == NULL) { + goto end; + } + + result = _getcwd(cwd, _MAX_PATH); + + if (result == NULL) { + SAFE_FREE(cwd); + goto end; + } + +end: + return cwd; +} + #endif /* _WIN32 */ +int torture_change_dir(char *path) +{ + int rc = 0; + + if (path == NULL) { + rc = -1; + goto end; + } + + rc = chdir(path); + +end: + return rc; +} + int torture_libssh_verbosity(void){ return verbosity; } diff --git a/tests/torture.h b/tests/torture.h index 0367653b..b47c7cbb 100644 --- a/tests/torture.h +++ b/tests/torture.h @@ -128,4 +128,7 @@ int torture_run_tests(void); char *torture_make_temp_dir(const char *template); char *torture_create_temp_file(const char *template); +char *torture_get_current_working_dir(void); +int torture_change_dir(char *path); + #endif /* _TORTURE_H */ diff --git a/tests/unittests/CMakeLists.txt b/tests/unittests/CMakeLists.txt index 730da341..d2dbdd72 100644 --- a/tests/unittests/CMakeLists.txt +++ b/tests/unittests/CMakeLists.txt @@ -18,6 +18,7 @@ set(LIBSSH_UNIT_TESTS torture_packet_filter torture_temp_dir torture_temp_file + torture_push_pop_dir ) set(LIBSSH_THREAD_UNIT_TESTS diff --git a/tests/unittests/torture_push_pop_dir.c b/tests/unittests/torture_push_pop_dir.c new file mode 100644 index 00000000..faf10866 --- /dev/null +++ b/tests/unittests/torture_push_pop_dir.c @@ -0,0 +1,78 @@ +#include "config.h" + +#include "torture.h" +#define LIBSSH_STATIC + +const char template[] = "temp_dir_XXXXXX"; + +static int setup(void **state) +{ + char *temp_dir = NULL; + + temp_dir = torture_make_temp_dir(template); + assert_non_null(temp_dir); + + *state = (void *)temp_dir; + + return 0; +} + +static int teardown(void **state) +{ + char *temp_dir = *((char **)state); + + torture_rmdirs((const char *)temp_dir); + + free(temp_dir); + + return 0; +} + +static void torture_back_and_forth(void **state) +{ + char *temp_dir = *((char **)state); + char *cwd = NULL; + char *after_change = NULL; + char *after_changing_back = NULL; + int rc = 0; + + cwd = torture_get_current_working_dir(); + assert_non_null(cwd); + + printf("Current dir: %s\n", cwd); + + rc = torture_change_dir(temp_dir); + assert_int_equal(rc, 0); + + after_change = torture_get_current_working_dir(); + assert_non_null(after_change); + + printf("Current dir after change: %s\n", after_change); + + rc = torture_change_dir(cwd); + assert_int_equal(rc, 0); + + after_changing_back = torture_get_current_working_dir(); + assert_non_null(after_changing_back); + + printf("Back to dir: %s\n", after_changing_back); + + SAFE_FREE(cwd); + SAFE_FREE(after_change); + SAFE_FREE(after_changing_back); +} + +int torture_run_tests(void) +{ + int rc; + struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(torture_back_and_forth, + setup, teardown), + }; + + torture_filter_tests(tests); + rc = cmocka_run_group_tests(tests, NULL, NULL); + + return rc; +} +