]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Simplify tests added in #2363, use testworkdir (#2369)
authorMostyn Bramley-Moore <mostyn@antipode.se>
Mon, 7 Oct 2024 03:34:58 +0000 (05:34 +0200)
committerGitHub <noreply@github.com>
Mon, 7 Oct 2024 03:34:58 +0000 (20:34 -0700)
cpio/test/test_extract_cpio_absolute_paths.c
libarchive/test/test_write_disk_secure_noabsolutepaths.c

index e7a3a8a5a6c4749e487572dc7697d6d2c4e9516e..3c2a6a63dd64d7b377bf88317d4de516914e3711 100644 (file)
@@ -7,47 +7,34 @@
 #include "test.h"
 
 #include <stdlib.h>
+#include <string.h>
 
 #if defined(_WIN32) && !defined(__CYGWIN__)
-
-#include <fileapi.h>
-
 #define UNLINK _unlink
-
 #else
-
 #define UNLINK unlink
-
 #endif
 
 DEFINE_TEST(test_extract_cpio_absolute_paths)
 {
        int r;
 
-#if defined(_WIN32) && !defined(__CYGWIN__)
-       char temp_dir[MAX_PATH + 1];
-       assert(GetTempPathA(MAX_PATH + 1, temp_dir) != 0);
-
-       char temp_file_name[MAX_PATH + 1];
-       char temp_absolute_file_name[MAX_PATH + 1];
-       assert(GetTempFileNameA(temp_dir, "abs", 0, temp_file_name) != 0);
-
-       assert(_fullpath(temp_absolute_file_name, temp_file_name, MAX_PATH) != NULL);
-#else
-       char temp_absolute_file_name[] = "/tmp/cpio-noabs.testXXXXXX";
-       mkstemp(temp_absolute_file_name);
-#endif
-
-       UNLINK(temp_absolute_file_name);
+       // Create an absolute path for a test file inside testworkdir.
+       char *entry_suffix = "/cpio-noabs";
+       size_t entry_suffix_length = strlen(entry_suffix);
+       size_t testworkdir_length = strlen(testworkdir);
+       size_t temp_absolute_file_name_length = testworkdir_length + entry_suffix_length;
+       char *temp_absolute_file_name = calloc(1, temp_absolute_file_name_length + 1); // +1 for null character.
+       assertEqualInt(snprintf(temp_absolute_file_name, temp_absolute_file_name_length + 1, "%s%s", testworkdir, entry_suffix),
+               temp_absolute_file_name_length);
 
+       // Create the file.
        const char *sample_data = "test file from test_extract_cpio_absolute_paths";
-
-       assertMakeFile("filelist", 0644, temp_absolute_file_name);
        assertMakeFile(temp_absolute_file_name, 0644, sample_data);
 
-       // Create an archive with the absolute path.
+       // Create an archive with the test file, using an absolute path.
+       assertMakeFile("filelist", 0644, temp_absolute_file_name);
        r = systemf("%s -o < filelist > archive.cpio 2> stderr1.txt", testprog);
-       UNLINK("filelist");
        assertEqualInt(r, 0);
 
        // Ensure that the temp file does not exist.
@@ -63,5 +50,4 @@ DEFINE_TEST(test_extract_cpio_absolute_paths)
        r = systemf("%s -i --insecure < archive.cpio 2> stderr3.txt", testprog);
        assert(r == 0);
        assertFileExists(temp_absolute_file_name);
-       UNLINK(temp_absolute_file_name);
 }
index 5782d21eab69488c67a6434ddea6ec3a7d5b25a1..df244727fe441bc52e0de6c79f551e3de337fa1f 100644 (file)
@@ -6,19 +6,13 @@
 
 #include "test.h"
 
-#if defined(_WIN32) && !defined(__CYGWIN__)
-
-#include <fileapi.h>
+#include <stdlib.h>
 
+#if defined(_WIN32) && !defined(__CYGWIN__)
 #define UNLINK _unlink
-
 #else
-
-#include <stdlib.h>
 #include <unistd.h>
-
 #define UNLINK unlink
-
 #endif
 
 /*
@@ -40,28 +34,20 @@ DEFINE_TEST(test_write_disk_secure_noabsolutepaths)
        assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
        assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used));
 
-#if defined(_WIN32) && !defined(__CYGWIN__)
-       char temp_dir[MAX_PATH + 1];
-       assert(GetTempPathA(MAX_PATH + 1, temp_dir) != 0);
-
-       char temp_file_name[MAX_PATH + 1];
-       char temp_absolute_file_name[MAX_PATH + 1];
-       assert(GetTempFileNameA(temp_dir, "abs", 0, temp_file_name) != 0);
-
-       assert(_fullpath(temp_absolute_file_name, temp_file_name, MAX_PATH) != NULL);
-
-       // Convert to a unix-style path.
+       // Create an absolute path for a test file inside testworkdir.
+       char *entry_suffix = "/badfile";
+       size_t entry_suffix_length = strlen(entry_suffix);
+       size_t testworkdir_length = strlen(testworkdir);
+       size_t temp_absolute_file_name_length = testworkdir_length + entry_suffix_length;
+       char *temp_absolute_file_name = calloc(1, temp_absolute_file_name_length + 1); // +1 for null character.
+       assertEqualInt(snprintf(temp_absolute_file_name, temp_absolute_file_name_length + 1, "%s%s", testworkdir, entry_suffix),
+               temp_absolute_file_name_length);
+
+       // Convert to a unix-style path, so we can compare it to the entry
+       // path when reading back the archive.
        for (char *p = temp_absolute_file_name; *p != '\0'; p++)
                if (*p == '\\') *p = '/';
 
-#else
-       char temp_absolute_file_name[] = "/tmp/noabs.testXXXXXX";
-       mkstemp(temp_absolute_file_name);
-#endif
-
-       // Ensure that the target file does not exist.
-       UNLINK(temp_absolute_file_name);
-
        // Add a regular file entry with an absolute path.
        assert((ae = archive_entry_new()) != NULL);
        archive_entry_copy_pathname(ae, temp_absolute_file_name);