diff options
-rw-r--r-- | tests/torture.c | 80 | ||||
-rw-r--r-- | tests/torture.h | 1 | ||||
-rw-r--r-- | tests/unittests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/unittests/torture_temp_file.c | 63 |
4 files changed, 145 insertions, 0 deletions
diff --git a/tests/torture.c b/tests/torture.c index f83ab4cb..fbf2463f 100644 --- a/tests/torture.c +++ b/tests/torture.c @@ -810,6 +810,38 @@ end: return new_dir; } +char *torture_create_temp_file(const char *template) +{ + char *new_file = NULL; + FILE *fp = NULL; + mode_t mask; + int fd; + + new_file = strdup(template); + if (new_file == NULL) { + goto end; + } + + mask = umask(S_IRWXO | S_IRWXG); + fd = mkstemp(new_file); + umask(mask); + if (fd == -1) { + goto end; + } + + fp = fdopen(fd, "w"); + if (fp == NULL) { + SAFE_FREE(new_file); + close(fd); + goto end; + } + + fclose(fp); + +end: + return new_file; +} + #else /* _WIN32 */ char *torture_make_temp_dir(const char *template) @@ -991,6 +1023,54 @@ int torture_isdir(const char *path) return 0; } +char *torture_create_temp_file(const char *template) +{ + DWORD rc = 0; + char tmp_dir_path[MAX_PATH]; + char tmp_file_name[MAX_PATH]; + char *prefix = NULL; + char *path = NULL; + char *prefix_end = NULL; + char *slash = NULL; + + if (template == NULL) { + goto end; + } + + prefix = strdup(template); + if (prefix == NULL) { + goto end; + } + + /* Replace slashes with backslashes */ + slash = strchr(prefix, '/'); + for (; slash != NULL; slash = strchr(prefix, '/')) { + *slash = '\\'; + } + + prefix_end = strstr(prefix, "XXXXXX"); + if (prefix_end != NULL) { + *prefix_end = '\0'; + } + + rc = GetTempPathA(MAX_PATH, tmp_dir_path); + if ((rc > MAX_PATH) || (rc == 0)) { + goto free_prefix; + } + + rc = GetTempFileNameA(tmp_dir_path, TEXT(prefix), 0, tmp_file_name); + if (rc == 0) { + goto free_prefix; + } + + path = strdup(tmp_file_name); + +free_prefix: + SAFE_FREE(prefix); +end: + return path; +} + #endif /* _WIN32 */ int torture_libssh_verbosity(void){ diff --git a/tests/torture.h b/tests/torture.h index 661f92f0..0367653b 100644 --- a/tests/torture.h +++ b/tests/torture.h @@ -126,5 +126,6 @@ void torture_teardown_sshd_server(void **state); int torture_run_tests(void); char *torture_make_temp_dir(const char *template); +char *torture_create_temp_file(const char *template); #endif /* _TORTURE_H */ diff --git a/tests/unittests/CMakeLists.txt b/tests/unittests/CMakeLists.txt index 04e905ca..730da341 100644 --- a/tests/unittests/CMakeLists.txt +++ b/tests/unittests/CMakeLists.txt @@ -17,6 +17,7 @@ set(LIBSSH_UNIT_TESTS torture_hashes torture_packet_filter torture_temp_dir + torture_temp_file ) set(LIBSSH_THREAD_UNIT_TESTS diff --git a/tests/unittests/torture_temp_file.c b/tests/unittests/torture_temp_file.c new file mode 100644 index 00000000..8f97bc9d --- /dev/null +++ b/tests/unittests/torture_temp_file.c @@ -0,0 +1,63 @@ +#include "config.h" + +#include "torture.h" +#define LIBSSH_STATIC + +const char template[] = "/tmp/temp_file_XXXXXX"; + +static int setup(void **state) +{ + char *file_name = NULL; + + file_name = torture_create_temp_file(template); + assert_non_null(file_name); + + *state = (void *)file_name; + + return 0; +} + +static int teardown(void **state) +{ + int rc; + char *file_name = *((char **)state); + + assert_non_null(file_name); + + rc = unlink(file_name); + assert_int_equal(rc, 0); + + SAFE_FREE(file_name); + + return 0; +} + + +static void torture_temp_file(void **state) +{ + char *file_name = *((char **)state); + FILE *fp = NULL; + + assert_non_null(file_name); + + fp = fopen(file_name, "r"); + assert_non_null(fp); + + fclose(fp); + + printf("Created temp file: %s\n", file_name); +} + +int torture_run_tests(void) +{ + int rc; + struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(torture_temp_file, setup, teardown), + }; + + torture_filter_tests(tests); + rc = cmocka_run_group_tests(tests, NULL, NULL); + + return rc; +} + |