]> git.scottworley.com Git - overonion/blobdiff - reverse_test.c
Use make_temporary_file in tests
[overonion] / reverse_test.c
index 30aff32d4d4153231ce510eadd83e5c9105d755c..0b82f19af270ad0150a5f51794f0d00ccea0d46d 100644 (file)
@@ -1,4 +1,5 @@
 #include "reverse_lib.h"
+#include "temp_file.h"
 
 #include <err.h>
 #include <stdio.h>
@@ -12,11 +13,9 @@ static const char test_file[] = "reverse.c";
 /* Reverse input_file.  Dump the result in a temporary file.  Return the temp
  * file's name.  The caller must free the filename. */
 char* reverse_to_temp_file(const char* input_file) {
-  char* temp_filename = strdup("/tmp/reverse_test.XXXXXX");
-  int fd = mkstemp(temp_filename);
-  if (fd == -1) err(EX_IOERR, "Couldn't make a temporary file");
-  FILE* f = fdopen(fd, "w");
-  if (f == NULL) err(EX_IOERR, "Couldn't open temporary file");
+  char* temp_filename;
+  FILE* f;
+  make_temporary_file(&temp_filename, &f);
   reverse_file(input_file, f);
   if (fclose(f) == EOF) err(EX_IOERR, "Couldn't close temporary file");
   return temp_filename;
@@ -74,11 +73,9 @@ void test_reverse_from_file_then_from_stream_is_the_same() {
   if (close(pipefd[1]) == -1) err(EX_OSERR, "Couldn't close unneeded pipe descriptor");
   FILE* from_first = fdopen(pipefd[0], "r");
   if (from_first == NULL) err(EX_IOERR, "Couldn't open pipe for reading");
-  char* out_temp_filename = strdup("/tmp/reverse_test.XXXXXX");
-  int out_fd = mkstemp(out_temp_filename);
-  if (out_fd == -1) err(EX_IOERR, "Couldn't make a temporary file");
-  FILE* out_file = fdopen(out_fd, "w");
-  if (out_file == NULL) err(EX_IOERR, "Couldn't open temporary file");
+  char* out_temp_filename;
+  FILE* out_file;
+  make_temporary_file(&out_temp_filename, &out_file);
   reverse_stream(from_first, out_file);
   if (fclose(out_file) == EOF) err(EX_IOERR, "Couldn't close temporary file");