tests: use torture_create_temp_file() in torture_knownhosts_parsing

Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Anderson Toshiyuki Sasaki
2018-11-19 13:45:36 +01:00
committed by Andreas Schneider
parent 78b1f0ead3
commit 77be4ce905
3 changed files with 15 additions and 21 deletions

View File

@@ -1058,6 +1058,7 @@ char *torture_create_temp_file(const char *template)
goto free_prefix; goto free_prefix;
} }
/* Remark: this function creates the file */
rc = GetTempFileNameA(tmp_dir_path, TEXT(prefix), 0, tmp_file_name); rc = GetTempFileNameA(tmp_dir_path, TEXT(prefix), 0, tmp_file_name);
if (rc == 0) { if (rc == 0) {
goto free_prefix; goto free_prefix;

View File

@@ -31,37 +31,29 @@ static int setup_knownhosts_file(void **state)
char *tmp_file = NULL; char *tmp_file = NULL;
size_t nwritten; size_t nwritten;
FILE *fp = NULL; FILE *fp = NULL;
mode_t mask; int rc = 0;
int fd;
tmp_file = strdup(TMP_FILE_NAME); tmp_file = torture_create_temp_file(TMP_FILE_NAME);
assert_non_null(tmp_file); assert_non_null(tmp_file);
*state = tmp_file; *state = tmp_file;
mask = umask(S_IRWXO | S_IRWXG); fp = fopen(tmp_file, "w");
fd = mkstemp(tmp_file); assert_non_null(fp);
umask(mask);
assert_return_code(fd, errno);
fp = fdopen(fd, "w");
if (fp == NULL) {
close(fd);
return -1;
}
nwritten = fwrite(LOCALHOST_PATTERN_ED25519, nwritten = fwrite(LOCALHOST_PATTERN_ED25519,
sizeof(char), sizeof(char),
strlen(LOCALHOST_PATTERN_ED25519), strlen(LOCALHOST_PATTERN_ED25519),
fp); fp);
if (nwritten != strlen(LOCALHOST_PATTERN_ED25519)) { if (nwritten != strlen(LOCALHOST_PATTERN_ED25519)) {
fclose(fp); rc = -1;
return -1; goto close_fp;
} }
nwritten = fwrite("\n", sizeof(char), 1, fp); nwritten = fwrite("\n", sizeof(char), 1, fp);
if (nwritten != 1) { if (nwritten != 1) {
fclose(fp); rc = -1;
return -1; goto close_fp;
} }
nwritten = fwrite(LOCALHOST_RSA_LINE, nwritten = fwrite(LOCALHOST_RSA_LINE,
@@ -69,13 +61,14 @@ static int setup_knownhosts_file(void **state)
strlen(LOCALHOST_RSA_LINE), strlen(LOCALHOST_RSA_LINE),
fp); fp);
if (nwritten != strlen(LOCALHOST_RSA_LINE)) { if (nwritten != strlen(LOCALHOST_RSA_LINE)) {
fclose(fp); rc = -1;
return -1; goto close_fp;
} }
close_fp:
fclose(fp); fclose(fp);
return 0; return rc;
} }
static int teardown_knownhosts_file(void **state) static int teardown_knownhosts_file(void **state)

View File

@@ -3,7 +3,7 @@
#include "torture.h" #include "torture.h"
#define LIBSSH_STATIC #define LIBSSH_STATIC
const char template[] = "/tmp/temp_file_XXXXXX"; const char template[] = "temp_file_XXXXXX";
static int setup(void **state) static int setup(void **state)
{ {