From: Mostyn Bramley-Moore Date: Mon, 7 Oct 2024 03:34:58 +0000 (+0200) Subject: Simplify tests added in #2363, use testworkdir (#2369) X-Git-Tag: v3.8.0~158 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=62ce582d1c22c8affa9453bbd549776b60c669fa;p=thirdparty%2Flibarchive.git Simplify tests added in #2363, use testworkdir (#2369) --- diff --git a/cpio/test/test_extract_cpio_absolute_paths.c b/cpio/test/test_extract_cpio_absolute_paths.c index e7a3a8a5a..3c2a6a63d 100644 --- a/cpio/test/test_extract_cpio_absolute_paths.c +++ b/cpio/test/test_extract_cpio_absolute_paths.c @@ -7,47 +7,34 @@ #include "test.h" #include +#include #if defined(_WIN32) && !defined(__CYGWIN__) - -#include - #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); } diff --git a/libarchive/test/test_write_disk_secure_noabsolutepaths.c b/libarchive/test/test_write_disk_secure_noabsolutepaths.c index 5782d21ea..df244727f 100644 --- a/libarchive/test/test_write_disk_secure_noabsolutepaths.c +++ b/libarchive/test/test_write_disk_secure_noabsolutepaths.c @@ -6,19 +6,13 @@ #include "test.h" -#if defined(_WIN32) && !defined(__CYGWIN__) - -#include +#include +#if defined(_WIN32) && !defined(__CYGWIN__) #define UNLINK _unlink - #else - -#include #include - #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);