]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Fix absolute path test when run as root
authorsubotac <73706465+subotac@users.noreply.github.com>
Thu, 30 Jul 2026 15:40:17 +0000 (18:40 +0300)
committersubotac <73706465+subotac@users.noreply.github.com>
Thu, 30 Jul 2026 15:40:17 +0000 (18:40 +0300)
tar/test/test_option_P_upper.c

index 69810a304318d74b8eefac293cce6fc556a8cf1d..2ed31b59d9ee9a72f5c23e16788fab8f79c383c0 100644 (file)
@@ -20,7 +20,7 @@ DEFINE_TEST(test_extract_tar_absolute_paths)
        int r;
 
        // Create an absolute path for a test file inside testworkdir.
-       char *entry_suffix = "/tar-noabs";
+       char *entry_suffix = "/work/tar-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;
@@ -28,6 +28,10 @@ DEFINE_TEST(test_extract_tar_absolute_paths)
        assertEqualInt(snprintf(temp_absolute_file_name, temp_absolute_file_name_length + 1, "%s%s", testworkdir, entry_suffix),
                temp_absolute_file_name_length);
 
+       // Use a directory owned by the unprivileged test user.
+       assertMakeDir("work", 0755);
+       assertChdir("work");
+
 #if defined(_WIN32) && !defined(__CYGWIN__)
        // I'm unsure how to specify paths with spaces for the test invocation on windows.
        // Adding quotes doesn't seem to work. We should find a way to escape these paths,
@@ -62,14 +66,15 @@ DEFINE_TEST(test_extract_tar_absolute_paths)
        // Check that the mangled path exists.
 #if defined(_WIN32) && !defined(__CYGWIN__)
        assertFileExists(temp_absolute_file_name + 3); // Skip the drive letter, colon and slash.
-       UNLINK(temp_absolute_file_name + 3);
 #else
        assertFileExists(temp_absolute_file_name + 1); // Skip the slash.
-       UNLINK(temp_absolute_file_name + 1);
 #endif
 
-       // Extracting the archive with -P / --absolute-paths should create the file.
-       r = systemf("%s --absolute-paths -xf test.tar", testprog);
+       // Extracting the archive with -P / --absolute-paths in a new directory should
+       // create the file without creating a mangled path.
+       assertMakeDir("with-P", 0755);
+       assertChdir("with-P");
+       r = systemf("%s --absolute-paths -xf ../test.tar", testprog);
        assertEqualInt(r, 0);
        assertFileExists(temp_absolute_file_name);