tests: Introduce torture_create_temp_file()
Introduced torture_create_temp_file() and torture_close_fp() to allow creating temprary files transparently in Unix and Windows environment. This also adds a unit test for the added functions. Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Этот коммит содержится в:
родитель
31527d4105
Коммит
78b1f0ead3
@ -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){
|
||||
|
@ -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 */
|
||||
|
@ -17,6 +17,7 @@ set(LIBSSH_UNIT_TESTS
|
||||
torture_hashes
|
||||
torture_packet_filter
|
||||
torture_temp_dir
|
||||
torture_temp_file
|
||||
)
|
||||
|
||||
set(LIBSSH_THREAD_UNIT_TESTS
|
||||
|
63
tests/unittests/torture_temp_file.c
Обычный файл
63
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;
|
||||
}
|
||||
|
Загрузка…
Ссылка в новой задаче
Block a user