Path: blob/main/contrib/libarchive/tar/test/test_option_P_upper.c
39507 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2024 Mostyn Bramley-Moore <[email protected]>4*/56#include "test.h"78#include <stdlib.h>9#include <string.h>1011#if defined(_WIN32) && !defined(__CYGWIN__)12#define UNLINK _unlink13#else14#define UNLINK unlink15#endif1617DEFINE_TEST(test_extract_tar_absolute_paths)18{19int r;2021// Create an absolute path for a test file inside testworkdir.22char *entry_suffix = "/tar-noabs";23size_t entry_suffix_length = strlen(entry_suffix);24size_t testworkdir_length = strlen(testworkdir);25size_t temp_absolute_file_name_length = testworkdir_length + entry_suffix_length;26char *temp_absolute_file_name = calloc(1, temp_absolute_file_name_length + 1); // +1 for null character.27assertEqualInt(snprintf(temp_absolute_file_name, temp_absolute_file_name_length + 1, "%s%s", testworkdir, entry_suffix),28temp_absolute_file_name_length);2930#if defined(_WIN32) && !defined(__CYGWIN__)31// I'm unsure how to specify paths with spaces for the test invocation on windows.32// Adding quotes doesn't seem to work. We should find a way to escape these paths,33// but for now let's fail in a place that's obviously related to the test setup if34// testworkdir contains spaces.35for (char *p = temp_absolute_file_name; *p != '\0'; p++)36{37assert(*p != ' ');38if (*p == ' ') break;39}40#endif4142// Create the file.43const char *sample_data = "test file from test_extract_tar_absolute_paths";44assertMakeFile(temp_absolute_file_name, 0644, sample_data);4546// Create an archive with the test file, using an absolute path.47#if defined(_WIN32) && !defined(__CYGWIN__)48r = systemf("%s --absolute-paths -cf test.tar %s", testprog, temp_absolute_file_name);49#else50r = systemf("%s --absolute-paths -cf test.tar \"%s\"", testprog, temp_absolute_file_name);51#endif52assertEqualInt(r, 0);5354UNLINK(temp_absolute_file_name);5556// Extracting the archive without -P / --absolute-paths should strip leading drive letter or slash57r = systemf("%s -xf test.tar 2>test.err", testprog);58assertEqualInt(r, 0);59assertFileNotExists(temp_absolute_file_name);6061// Check that the mangled path exists.62#if defined(_WIN32) && !defined(__CYGWIN__)63assertFileExists(temp_absolute_file_name + 3); // Skip the drive letter, colon and slash.64UNLINK(temp_absolute_file_name + 3);65#else66assertFileExists(temp_absolute_file_name + 1); // Skip the slash.67UNLINK(temp_absolute_file_name + 1);68#endif6970// Extracting the archive with -P / --absolute-paths should create the file.71r = systemf("%s --absolute-paths -xf test.tar", testprog);72assertEqualInt(r, 0);73assertFileExists(temp_absolute_file_name);7475// Check that the mangled path wasn't created.76#if defined(_WIN32) && !defined(__CYGWIN__)77assertFileNotExists(temp_absolute_file_name + 3); // Skip the drive letter, colon and slash.78#else79assertFileNotExists(temp_absolute_file_name + 1); // Skip the slash.80#endif81}828384