Path: blob/main/contrib/libarchive/cpio/test/test_extract_cpio_absolute_paths.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_cpio_absolute_paths)18{19int r;2021// Create an absolute path for a test file inside testworkdir.22const char *entry_suffix = "/cpio-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// Create the file.31const char *sample_data = "test file from test_extract_cpio_absolute_paths";32assertMakeFile(temp_absolute_file_name, 0644, sample_data);3334// Create an archive with the test file, using an absolute path.35assertMakeFile("filelist", 0644, temp_absolute_file_name);36r = systemf("%s -o < filelist > archive.cpio 2> stderr1.txt", testprog);37assertEqualInt(r, 0);3839// Ensure that the temp file does not exist.40UNLINK(temp_absolute_file_name);4142// We should refuse to create the absolute path without --insecure.43r = systemf("%s -i < archive.cpio 2> stderr2.txt", testprog);44assert(r != 0);45assertFileNotExists(temp_absolute_file_name);46UNLINK(temp_absolute_file_name); // Cleanup just in case.4748// But if we specify --insecure then the absolute path should be created.49r = systemf("%s -i --insecure < archive.cpio 2> stderr3.txt", testprog);50assert(r == 0);51assertFileExists(temp_absolute_file_name);52}535455