diff options
Diffstat (limited to 'tests/utils.c')
-rw-r--r-- | tests/utils.c | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/utils.c b/tests/utils.c index 65ceafd857..37345a62d3 100644 --- a/tests/utils.c +++ b/tests/utils.c @@ -30,6 +30,7 @@ #include <time.h> #include <unistd.h> #include <errno.h> +#include <assert.h> #ifndef _WIN32 # include <netinet/in.h> # include <sys/socket.h> @@ -39,6 +40,8 @@ # include <winbase.h> #endif #endif +#include <gnutls/gnutls.h> +#include <gnutls/crypto.h> #include "utils.h" @@ -183,3 +186,74 @@ int main(int argc, char *argv[]) return error_count ? 1 : 0; } + +struct tmp_file_st { + char file[TMPNAME_SIZE]; + struct tmp_file_st *next; +}; + +static struct tmp_file_st *temp_files = (void*)-1; + +static void append(const char *file) +{ + struct tmp_file_st *p; + + if (temp_files == (void*)-1) + return; + + p = calloc(1, sizeof(*p)); + + assert(p != NULL); + strcpy(p->file, file); + p->next = temp_files; + temp_files = p; +} + +char *get_tmpname(char s[TMPNAME_SIZE]) +{ + unsigned char rnd[6]; + static char _s[TMPNAME_SIZE]; + int ret; + char *p; + const char *path; + + ret = gnutls_rnd(GNUTLS_RND_NONCE, rnd, sizeof(rnd)); + if (ret < 0) + return NULL; + + path = getenv("builddir"); + if (path == NULL) + path = "."; + + if (s == NULL) + p = _s; + else + p = s; + + snprintf(p, TMPNAME_SIZE, "%s/tmpfile-%02x%02x%02x%02x%02x%02x.tmp", path, (unsigned)rnd[0], (unsigned)rnd[1], + (unsigned)rnd[2], (unsigned)rnd[3], (unsigned)rnd[4], (unsigned)rnd[5]); + + append(p); + + return p; +} + +void track_temp_files(void) +{ + temp_files = NULL; +} + +void delete_temp_files(void) +{ + struct tmp_file_st *p = temp_files; + struct tmp_file_st *next; + + if (p == (void*)-1) + return; + + while(p != NULL) { + next = p->next; + free(p); + p = next; + } +} |