summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@redhat.com>2017-02-22 17:10:28 +0100
committerNikos Mavrogiannopoulos <nmav@redhat.com>2017-02-22 17:15:52 +0100
commit92134798358d054062e97e14b39d1a9bf60428b5 (patch)
treeffca8ef0d8c55e37cadf26f396ffc44ed04394bd
parent18ad1b8fb6ba705575882e5425e86bd27fcaea41 (diff)
downloadgnutls-92134798358d054062e97e14b39d1a9bf60428b5.tar.gz
tests: utils: added ability to use tmpfiles
Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
-rw-r--r--tests/utils.c74
-rw-r--r--tests/utils.h5
2 files changed, 79 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;
+ }
+}
diff --git a/tests/utils.h b/tests/utils.h
index 8f3ac3fc97..5c0afe70ad 100644
--- a/tests/utils.h
+++ b/tests/utils.h
@@ -61,4 +61,9 @@ extern void binprint(const void *str, size_t len);
extern void doit(void);
void sec_sleep(int sec);
+#define TMPNAME_SIZE 128
+char *get_tmpname(char s[TMPNAME_SIZE]);
+void track_temp_files(void);
+void delete_temp_files(void);
+
#endif /* UTILS_H */