From 77be4ce9056e86f13b06fe15f410d663f5e7b771 Mon Sep 17 00:00:00 2001 From: Anderson Toshiyuki Sasaki Date: Mon, 19 Nov 2018 13:45:36 +0100 Subject: [PATCH] tests: use torture_create_temp_file() in torture_knownhosts_parsing Signed-off-by: Anderson Toshiyuki Sasaki Reviewed-by: Andreas Schneider --- tests/torture.c | 1 + tests/unittests/torture_knownhosts_parsing.c | 33 ++++++++------------ tests/unittests/torture_temp_file.c | 2 +- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/tests/torture.c b/tests/torture.c index fbf2463f..f7ed775e 100644 --- a/tests/torture.c +++ b/tests/torture.c @@ -1058,6 +1058,7 @@ char *torture_create_temp_file(const char *template) goto free_prefix; } + /* Remark: this function creates the file */ rc = GetTempFileNameA(tmp_dir_path, TEXT(prefix), 0, tmp_file_name); if (rc == 0) { goto free_prefix; diff --git a/tests/unittests/torture_knownhosts_parsing.c b/tests/unittests/torture_knownhosts_parsing.c index cb3cea8c..fa8eb048 100644 --- a/tests/unittests/torture_knownhosts_parsing.c +++ b/tests/unittests/torture_knownhosts_parsing.c @@ -31,37 +31,29 @@ static int setup_knownhosts_file(void **state) char *tmp_file = NULL; size_t nwritten; FILE *fp = NULL; - mode_t mask; - int fd; + int rc = 0; - tmp_file = strdup(TMP_FILE_NAME); + tmp_file = torture_create_temp_file(TMP_FILE_NAME); assert_non_null(tmp_file); + *state = tmp_file; - mask = umask(S_IRWXO | S_IRWXG); - fd = mkstemp(tmp_file); - umask(mask); - assert_return_code(fd, errno); - - fp = fdopen(fd, "w"); - if (fp == NULL) { - close(fd); - return -1; - } + fp = fopen(tmp_file, "w"); + assert_non_null(fp); nwritten = fwrite(LOCALHOST_PATTERN_ED25519, sizeof(char), strlen(LOCALHOST_PATTERN_ED25519), fp); if (nwritten != strlen(LOCALHOST_PATTERN_ED25519)) { - fclose(fp); - return -1; + rc = -1; + goto close_fp; } nwritten = fwrite("\n", sizeof(char), 1, fp); if (nwritten != 1) { - fclose(fp); - return -1; + rc = -1; + goto close_fp; } nwritten = fwrite(LOCALHOST_RSA_LINE, @@ -69,13 +61,14 @@ static int setup_knownhosts_file(void **state) strlen(LOCALHOST_RSA_LINE), fp); if (nwritten != strlen(LOCALHOST_RSA_LINE)) { - fclose(fp); - return -1; + rc = -1; + goto close_fp; } +close_fp: fclose(fp); - return 0; + return rc; } static int teardown_knownhosts_file(void **state) diff --git a/tests/unittests/torture_temp_file.c b/tests/unittests/torture_temp_file.c index 8f97bc9d..793d6c12 100644 --- a/tests/unittests/torture_temp_file.c +++ b/tests/unittests/torture_temp_file.c @@ -3,7 +3,7 @@ #include "torture.h" #define LIBSSH_STATIC -const char template[] = "/tmp/temp_file_XXXXXX"; +const char template[] = "temp_file_XXXXXX"; static int setup(void **state) {