604 lines
22 KiB
C++
604 lines
22 KiB
C++
#include <Void/Archive/Archive.hpp>
|
|
#include <Void/System/TempDir.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#if defined(VOID_ARCHIVE_HAS_LIBARCHIVE) && VOID_ARCHIVE_HAS_LIBARCHIVE
|
|
#include <archive.h>
|
|
#include <archive_entry.h>
|
|
#endif
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstring>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <memory>
|
|
#include <stop_token>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <sys/stat.h>
|
|
#include <thread>
|
|
#include <tuple>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
using namespace Void::Archive;
|
|
|
|
std::vector<std::byte> to_bytes(std::string_view s)
|
|
{
|
|
std::vector<std::byte> out(s.size());
|
|
for (std::size_t i = 0; i < s.size(); ++i) {
|
|
out[i] = static_cast<std::byte>(static_cast<unsigned char>(s[i]));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::string read_file_str(const std::filesystem::path& p)
|
|
{
|
|
std::ifstream in{p, std::ios::binary};
|
|
Void::Testing::require(in.is_open());
|
|
return std::string{std::istreambuf_iterator<char>{in},
|
|
std::istreambuf_iterator<char>{}};
|
|
}
|
|
|
|
void write_octal(std::span<std::byte> dst, std::uint64_t val)
|
|
{
|
|
dst.back() = std::byte{0};
|
|
std::size_t pos = dst.size() - 1;
|
|
while (pos > 0) {
|
|
--pos;
|
|
dst[pos] = static_cast<std::byte>('0' + (val & 7ULL));
|
|
val >>= 3ULL;
|
|
}
|
|
}
|
|
|
|
std::vector<std::byte> make_raw_tar_ustar(
|
|
std::span<const std::tuple<std::string_view, std::string_view, char,
|
|
std::string_view>>
|
|
entries)
|
|
{
|
|
std::vector<std::byte> out;
|
|
for (const auto& [path, content, typeflag, link] : entries) {
|
|
std::array<std::byte, 512> hdr{};
|
|
std::memcpy(hdr.data(), path.data(),
|
|
std::min<std::size_t>(path.size(), 100));
|
|
write_octal(std::span{hdr}.subspan(100, 8),
|
|
typeflag == '5' ? 0755ULL : 0644ULL);
|
|
write_octal(std::span{hdr}.subspan(108, 8), 0);
|
|
write_octal(std::span{hdr}.subspan(116, 8), 0);
|
|
const std::uint64_t sz = (typeflag == '0') ? content.size() : 0ULL;
|
|
write_octal(std::span{hdr}.subspan(124, 12), sz);
|
|
write_octal(std::span{hdr}.subspan(136, 12), 0);
|
|
std::fill(hdr.begin() + 148, hdr.begin() + 156, std::byte{' '});
|
|
hdr[156] = static_cast<std::byte>(typeflag);
|
|
if (!link.empty()) {
|
|
std::memcpy(hdr.data() + 157, link.data(),
|
|
std::min<std::size_t>(link.size(), 100));
|
|
}
|
|
std::memcpy(hdr.data() + 257, "ustar\0", 6);
|
|
hdr[263] = std::byte{'0'};
|
|
hdr[264] = std::byte{'0'};
|
|
std::uint32_t sum = 0;
|
|
for (const std::byte b : hdr) {
|
|
sum += static_cast<std::uint8_t>(b);
|
|
}
|
|
write_octal(std::span{hdr}.subspan(148, 7), sum);
|
|
hdr[155] = std::byte{' '};
|
|
|
|
out.insert(out.end(), hdr.begin(), hdr.end());
|
|
if (typeflag == '0' && !content.empty()) {
|
|
const auto cb =
|
|
std::as_bytes(std::span{content.data(), content.size()});
|
|
out.insert(out.end(), cb.begin(), cb.end());
|
|
const std::size_t rem = content.size() % 512U;
|
|
if (rem != 0) {
|
|
out.resize(out.size() + (512U - rem), std::byte{0});
|
|
}
|
|
}
|
|
}
|
|
out.resize(out.size() + 1024U, std::byte{0});
|
|
return out;
|
|
}
|
|
|
|
ArchiveCompression default_test_compression() noexcept
|
|
{
|
|
return has_libarchive_backend() ? ArchiveCompression::Zstd
|
|
: ArchiveCompression::None;
|
|
}
|
|
|
|
#if defined(VOID_ARCHIVE_HAS_LIBARCHIVE) && VOID_ARCHIVE_HAS_LIBARCHIVE
|
|
std::vector<std::byte> make_raw_tar(
|
|
std::span<const std::tuple<std::string_view, std::string_view,
|
|
unsigned int, std::string_view>>
|
|
entries)
|
|
{
|
|
using W = std::unique_ptr<archive, decltype(&archive_write_free)>;
|
|
using E =
|
|
std::unique_ptr<archive_entry, decltype(&archive_entry_free)>;
|
|
std::vector<std::byte> out;
|
|
W writer{archive_write_new(), &archive_write_free};
|
|
Void::Testing::require(static_cast<bool>(writer));
|
|
Void::Testing::require(archive_write_add_filter_zstd(writer.get()) ==
|
|
ARCHIVE_OK);
|
|
Void::Testing::require(archive_write_set_format_pax_restricted(
|
|
writer.get()) == ARCHIVE_OK);
|
|
Void::Testing::require(
|
|
archive_write_set_bytes_in_last_block(writer.get(), 1) ==
|
|
ARCHIVE_OK);
|
|
auto cb = [](archive*, void* data, const void* buf,
|
|
std::size_t len) -> la_ssize_t {
|
|
auto* vec = static_cast<std::vector<std::byte>*>(data);
|
|
const auto* p = static_cast<const std::byte*>(buf);
|
|
vec->insert(vec->end(), p, p + len);
|
|
return static_cast<la_ssize_t>(len);
|
|
};
|
|
Void::Testing::require(archive_write_open2(writer.get(), &out, nullptr,
|
|
cb, nullptr, nullptr) ==
|
|
ARCHIVE_OK);
|
|
for (const auto& [path, content, type, link] : entries) {
|
|
E ae{archive_entry_new(), &archive_entry_free};
|
|
const std::string p{path};
|
|
archive_entry_set_pathname(ae.get(), p.c_str());
|
|
archive_entry_set_filetype(ae.get(), type);
|
|
archive_entry_set_perm(ae.get(), type == AE_IFDIR ? 0755 : 0644);
|
|
if (type == AE_IFREG) {
|
|
archive_entry_set_size(ae.get(),
|
|
static_cast<la_int64_t>(content.size()));
|
|
}
|
|
if (!link.empty()) {
|
|
const std::string l{link};
|
|
archive_entry_set_symlink(ae.get(), l.c_str());
|
|
}
|
|
Void::Testing::require(archive_write_header(writer.get(), ae.get()) ==
|
|
ARCHIVE_OK);
|
|
if (!content.empty()) {
|
|
Void::Testing::require(
|
|
archive_write_data(writer.get(), content.data(),
|
|
content.size()) ==
|
|
static_cast<la_ssize_t>(content.size()));
|
|
}
|
|
Void::Testing::require(archive_write_finish_entry(writer.get()) ==
|
|
ARCHIVE_OK);
|
|
}
|
|
Void::Testing::require(archive_write_close(writer.get()) == ARCHIVE_OK);
|
|
return out;
|
|
}
|
|
#endif
|
|
} // namespace
|
|
|
|
Void::Testing::Test archive_reader_reproducible_skip_and_forensic_listing()
|
|
{
|
|
// 1. Reproducible creation produces identical bytes regardless of entry
|
|
// ordering.
|
|
const std::array<CreateEntry, 3> order_a{{
|
|
CreateEntry{.path = "bin/run",
|
|
.type = EntryType::RegularFile,
|
|
.executable = true,
|
|
.content = to_bytes("#!/bin/sh\necho hi\n")},
|
|
CreateEntry{.path = "docs/readme.txt",
|
|
.type = EntryType::RegularFile,
|
|
.executable = false,
|
|
.content = to_bytes("hello archive")},
|
|
CreateEntry{.path = "bin/", .type = EntryType::Directory},
|
|
}};
|
|
const std::array<CreateEntry, 3> order_b{order_a[2], order_a[1], order_a[0]};
|
|
|
|
VectorByteSink sink_a;
|
|
VectorByteSink sink_b;
|
|
CreationOptions copts{.compression = default_test_compression(),
|
|
.reproducible = true};
|
|
Void::Testing::require(create_archive(sink_a, order_a, copts).has_value());
|
|
Void::Testing::require(create_archive(sink_b, order_b, copts).has_value());
|
|
Void::Testing::expect_eq(sink_a.bytes(), sink_b.bytes());
|
|
|
|
// 2. Caller-driven Reader enforces drain/skip before next_entry and
|
|
// accounts skipped decompressed bytes against max_total_bytes.
|
|
{
|
|
SpanByteSource src{sink_a.bytes()};
|
|
auto reader = Reader::open(src);
|
|
Void::Testing::require(reader.has_value());
|
|
|
|
auto e1 = reader->next_entry();
|
|
Void::Testing::require(e1.has_value() && e1->has_value());
|
|
Void::Testing::expect_eq((*e1)->path, std::string{"bin"});
|
|
Void::Testing::expect_eq(static_cast<int>((*e1)->type),
|
|
static_cast<int>(EntryType::Directory));
|
|
|
|
auto e2 = reader->next_entry();
|
|
Void::Testing::require(e2.has_value() && e2->has_value());
|
|
Void::Testing::expect_eq((*e2)->path, std::string{"bin/run"});
|
|
Void::Testing::expect((*e2)->executable);
|
|
|
|
// Calling next_entry before draining/skipping active file fails.
|
|
auto premature = reader->next_entry();
|
|
Void::Testing::require(!premature.has_value());
|
|
}
|
|
|
|
// Skip accounting catches total limit even when caller skips data!
|
|
{
|
|
SpanByteSource src{sink_a.bytes()};
|
|
ReadLimits tight{};
|
|
tight.max_total_bytes = 20; // bin/run (18B) + readme (13B) = 31B > 20B
|
|
auto reader = Reader::open(src, tight);
|
|
Void::Testing::require(reader.has_value());
|
|
Void::Testing::require(reader->next_entry().has_value()); // bin/
|
|
Void::Testing::require(reader->next_entry().has_value()); // bin/run
|
|
auto skipped = reader->skip_entry();
|
|
Void::Testing::require(skipped.has_value());
|
|
Void::Testing::expect_eq(*skipped, 18ULL);
|
|
auto e3 = reader->next_entry(); // docs/readme.txt declared 13B > 2B rem
|
|
Void::Testing::require(!e3.has_value());
|
|
Void::Testing::expect_eq(
|
|
e3.error().code().value,
|
|
static_cast<std::int32_t>(ArchiveErrorCode::limit_exceeded));
|
|
}
|
|
|
|
// 3. Zero limits rejected unless allow_zero_entries is explicitly set.
|
|
{
|
|
ReadLimits zero_entries{};
|
|
zero_entries.max_entries = 0;
|
|
Void::Testing::expect(!zero_entries.validate().has_value());
|
|
zero_entries.allow_zero_entries = true;
|
|
Void::Testing::expect(zero_entries.validate().has_value());
|
|
}
|
|
|
|
// 4. File-as-parent conflict rejected cleanly (pure USTAR works with and
|
|
// without LibArchive; additionally exercise zstd when LibArchive exists).
|
|
{
|
|
const std::array<
|
|
std::tuple<std::string_view, std::string_view, char,
|
|
std::string_view>,
|
|
2>
|
|
conflict_ustar{{
|
|
{"foo", "file-bytes", '0', ""},
|
|
{"foo/child", "nested", '0', ""},
|
|
}};
|
|
const auto raw_ustar = make_raw_tar_ustar(conflict_ustar);
|
|
SpanByteSource src{raw_ustar};
|
|
auto listed = list_entries(src, {}, false);
|
|
Void::Testing::require(!listed.has_value());
|
|
Void::Testing::expect_eq(
|
|
listed.error().code().value,
|
|
static_cast<std::int32_t>(ArchiveErrorCode::unsafe_entry));
|
|
|
|
#if defined(VOID_ARCHIVE_HAS_LIBARCHIVE) && VOID_ARCHIVE_HAS_LIBARCHIVE
|
|
const std::array<
|
|
std::tuple<std::string_view, std::string_view, unsigned int,
|
|
std::string_view>,
|
|
2>
|
|
conflict_entries{{
|
|
{"foo", "file-bytes", AE_IFREG, ""},
|
|
{"foo/child", "nested", AE_IFREG, ""},
|
|
}};
|
|
const auto raw = make_raw_tar(conflict_entries);
|
|
SpanByteSource src_la{raw};
|
|
auto listed_la = list_entries(src_la, {}, false);
|
|
Void::Testing::require(!listed_la.has_value());
|
|
Void::Testing::expect_eq(
|
|
listed_la.error().code().value,
|
|
static_cast<std::int32_t>(ArchiveErrorCode::unsafe_entry));
|
|
#endif
|
|
}
|
|
|
|
// 5. Normal vs forensic listing + sanitized diagnostics.
|
|
{
|
|
const std::array<
|
|
std::tuple<std::string_view, std::string_view, char,
|
|
std::string_view>,
|
|
3>
|
|
mixed_ustar{{
|
|
{"safe.txt", "ok", '0', ""},
|
|
{"bad\nlink", "", '2', "/etc/passwd"},
|
|
{"../escape", "nope", '0', ""},
|
|
}};
|
|
const auto raw = make_raw_tar_ustar(mixed_ustar);
|
|
|
|
SpanByteSource src1{raw};
|
|
Void::Testing::expect(!list_entries(src1, {}, false).has_value());
|
|
|
|
SpanByteSource src2{raw};
|
|
auto forensic = list_entries(src2, {}, true);
|
|
Void::Testing::require(forensic.has_value());
|
|
Void::Testing::expect_eq(forensic->size(), 3U);
|
|
Void::Testing::expect((*forensic)[0].accepted);
|
|
Void::Testing::expect(!(*forensic)[1].accepted);
|
|
Void::Testing::expect((*forensic)[1].path.find('\n') ==
|
|
std::string::npos);
|
|
Void::Testing::expect(!(*forensic)[2].accepted);
|
|
}
|
|
|
|
// Diagnostic sanitizer caps at 4 KiB and escapes newlines.
|
|
const std::string huge_diag(6000, 'a');
|
|
Void::Testing::expect_eq(sanitize_diagnostic_path(huge_diag).size(), 4096U);
|
|
Void::Testing::expect_eq(sanitize_diagnostic_path("a\nb\rc"),
|
|
std::string{"a\\nb\\rc"});
|
|
}
|
|
|
|
Void::Testing::Test archive_create_formats_builtin_ustar_pax_and_gzip()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_archive_formats_"};
|
|
|
|
// Built-in pure-C++ USTAR/PAX creation works regardless of LibArchive,
|
|
// handles long PAX paths (>120 bytes), custom canonical metadata, and
|
|
// produces byte-identical output across runs and entry permutations.
|
|
const std::string long_path =
|
|
"very/deep/nested/package/hierarchy/with/many/subdirectories/"
|
|
"extending/well/past/one/hundred/bytes/component/artifact.txt";
|
|
const std::array<CreateEntry, 3> entries_a{{
|
|
CreateEntry{.path = long_path,
|
|
.type = EntryType::RegularFile,
|
|
.executable = false,
|
|
.content = to_bytes("pax-long-path-content")},
|
|
CreateEntry{.path = "bin/tool",
|
|
.type = EntryType::RegularFile,
|
|
.executable = false,
|
|
.content = to_bytes("tool-bytes"),
|
|
.mode = 0755U},
|
|
CreateEntry{.path = "bin/", .type = EntryType::Directory},
|
|
}};
|
|
const std::array<CreateEntry, 3> entries_b{
|
|
entries_a[2], entries_a[0], entries_a[1]};
|
|
|
|
CreationOptions builtin_opts{
|
|
.compression = ArchiveCompression::None,
|
|
.reproducible = true,
|
|
.canonical_mtime = 1700000000,
|
|
.canonical_uid = 1000,
|
|
.canonical_gid = 1000,
|
|
.prefer_builtin_tar = true,
|
|
};
|
|
|
|
VectorByteSink raw1;
|
|
VectorByteSink raw2;
|
|
Void::Testing::require(create(raw1, entries_a, builtin_opts).has_value());
|
|
Void::Testing::require(create(raw2, entries_b, builtin_opts).has_value());
|
|
Void::Testing::expect_eq(raw1.bytes(), raw2.bytes());
|
|
|
|
const auto tar_file = tmp / "builtin.tar";
|
|
Void::Testing::require(create(tar_file, entries_a, builtin_opts).has_value());
|
|
auto extracted = extract(tar_file, tmp / "unpacked_tar");
|
|
Void::Testing::require(extracted.has_value());
|
|
Void::Testing::expect_eq(read_file_str(tmp / "unpacked_tar" / long_path),
|
|
std::string{"pax-long-path-content"});
|
|
Void::Testing::expect_eq(read_file_str(tmp / "unpacked_tar/bin/tool"),
|
|
std::string{"tool-bytes"});
|
|
|
|
struct stat st_tool{};
|
|
Void::Testing::require(::stat((tmp / "unpacked_tar/bin/tool").c_str(),
|
|
&st_tool) == 0);
|
|
Void::Testing::expect_eq(st_tool.st_mode & 0777, mode_t{0755});
|
|
|
|
// Staged extraction over built-in tar also succeeds (exercising
|
|
// LibArchive interop when LibArchive is linked, or built-in Reader when
|
|
// LibArchive is absent).
|
|
auto staged_tar = extract_staged(tar_file, tmp / "staged_tar");
|
|
Void::Testing::require(staged_tar.has_value());
|
|
auto pub_tar = staged_tar->publish("v1");
|
|
Void::Testing::require(pub_tar.has_value());
|
|
Void::Testing::expect_eq(
|
|
read_file_str(tmp / "staged_tar/v1" / long_path),
|
|
std::string{"pax-long-path-content"});
|
|
|
|
if (has_libarchive_backend()) {
|
|
CreationOptions gzip_opts{
|
|
.compression = ArchiveCompression::Gzip,
|
|
.reproducible = true,
|
|
.canonical_mtime = 1700000000,
|
|
};
|
|
VectorByteSink gz1;
|
|
VectorByteSink gz2;
|
|
Void::Testing::require(create(gz1, entries_a, gzip_opts).has_value());
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{15});
|
|
Void::Testing::require(create(gz2, entries_b, gzip_opts).has_value());
|
|
Void::Testing::expect_eq(gz1.bytes(), gz2.bytes());
|
|
|
|
const auto gz_path = tmp / "bundle.tar.gz";
|
|
Void::Testing::require(
|
|
create(gz_path, entries_a, gzip_opts).has_value());
|
|
auto gz_ext = extract(gz_path, tmp / "unpacked_gz");
|
|
Void::Testing::require(gz_ext.has_value());
|
|
Void::Testing::expect_eq(read_file_str(tmp / "unpacked_gz" / long_path),
|
|
std::string{"pax-long-path-content"});
|
|
|
|
auto gz_staged = extract_staged(gz_path, tmp / "staged_gz");
|
|
Void::Testing::require(gz_staged.has_value());
|
|
Void::Testing::require(gz_staged->publish("v1").has_value());
|
|
Void::Testing::expect_eq(
|
|
read_file_str(tmp / "staged_gz/v1/bin/tool"),
|
|
std::string{"tool-bytes"});
|
|
} else {
|
|
VectorByteSink unused;
|
|
auto zst_err =
|
|
create(unused, entries_a,
|
|
CreationOptions{.compression = ArchiveCompression::Zstd});
|
|
Void::Testing::require(!zst_err.has_value());
|
|
Void::Testing::expect_eq(
|
|
zst_err.error().code().value,
|
|
static_cast<std::int32_t>(ArchiveErrorCode::unsupported_format));
|
|
|
|
auto gz_err =
|
|
create(unused, entries_a,
|
|
CreationOptions{.compression = ArchiveCompression::Gzip});
|
|
Void::Testing::require(!gz_err.has_value());
|
|
Void::Testing::expect_eq(
|
|
gz_err.error().code().value,
|
|
static_cast<std::int32_t>(ArchiveErrorCode::unsupported_format));
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test archive_staged_extraction_atomic_publish_unknown_and_cleanup()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_archive_staged_"};
|
|
const auto archive_path =
|
|
tmp / (has_libarchive_backend() ? "pkg.tar.zst" : "pkg.tar");
|
|
const auto parent_dir = tmp / "versions";
|
|
std::filesystem::create_directories(parent_dir);
|
|
|
|
const std::array<CreateEntry, 2> entries{{
|
|
CreateEntry{.path = "bin/app",
|
|
.type = EntryType::RegularFile,
|
|
.executable = true,
|
|
.content = to_bytes("binary-payload")},
|
|
CreateEntry{.path = "etc/config.txt",
|
|
.type = EntryType::RegularFile,
|
|
.executable = false,
|
|
.content = to_bytes("key=val")},
|
|
}};
|
|
const CreationOptions create_opts{.compression = default_test_compression()};
|
|
Void::Testing::require(
|
|
create_archive(archive_path, entries, create_opts).has_value());
|
|
|
|
// 1. Staged extraction + atomic no-replace publication + safe modes.
|
|
std::size_t progress_calls = 0;
|
|
StagedExtractionOptions sopts{};
|
|
sopts.on_progress = [&](const ProgressEvent&) { ++progress_calls; };
|
|
auto staged = extract_staged(archive_path, parent_dir, sopts);
|
|
Void::Testing::require(staged.has_value());
|
|
Void::Testing::expect(staged->valid());
|
|
Void::Testing::expect(!staged->published());
|
|
Void::Testing::expect_eq(staged->entries(), 2U);
|
|
Void::Testing::expect(progress_calls >= 1U);
|
|
|
|
// Unsupported renameat2 ENOSYS returns unsupported_capability without
|
|
// fallback or consuming the staging tree.
|
|
{
|
|
ScopedFaultGuard fault{FaultPoint::rename_enosys};
|
|
auto unsupported = staged->publish("v1");
|
|
Void::Testing::require(!unsupported.has_value());
|
|
Void::Testing::expect_eq(
|
|
unsupported.error().code().value,
|
|
static_cast<std::int32_t>(ArchiveErrorCode::unsupported_capability));
|
|
Void::Testing::expect(!staged->published());
|
|
Void::Testing::expect(!std::filesystem::exists(parent_dir / "v1"));
|
|
}
|
|
|
|
// Atomic publish succeeds.
|
|
auto receipt = staged->publish("v1");
|
|
Void::Testing::require(receipt.has_value());
|
|
Void::Testing::expect_eq(static_cast<int>(receipt->outcome),
|
|
static_cast<int>(PublicationOutcome::Published));
|
|
Void::Testing::expect(staged->published());
|
|
Void::Testing::expect_eq(read_file_str(parent_dir / "v1/bin/app"),
|
|
std::string{"binary-payload"});
|
|
Void::Testing::expect_eq(read_file_str(parent_dir / "v1/etc/config.txt"),
|
|
std::string{"key=val"});
|
|
|
|
struct stat st_exec{};
|
|
struct stat st_reg{};
|
|
Void::Testing::require(::stat((parent_dir / "v1/bin/app").c_str(), &st_exec) ==
|
|
0);
|
|
Void::Testing::require(
|
|
::stat((parent_dir / "v1/etc/config.txt").c_str(), &st_reg) == 0);
|
|
Void::Testing::expect_eq(st_exec.st_mode & 0777, mode_t{0755});
|
|
Void::Testing::expect_eq(st_reg.st_mode & 0777, mode_t{0644});
|
|
|
|
// 2. Never overwrites an existing child directory.
|
|
auto staged2 = extract_staged(archive_path, parent_dir);
|
|
Void::Testing::require(staged2.has_value());
|
|
auto conflict = staged2->publish("v1");
|
|
Void::Testing::require(!conflict.has_value());
|
|
Void::Testing::expect_eq(
|
|
conflict.error().code().value,
|
|
static_cast<std::int32_t>(ArchiveErrorCode::already_exists));
|
|
const auto stage2_root = staged2->staging_root();
|
|
Void::Testing::expect(std::filesystem::exists(stage2_root));
|
|
// Destroying unpublished StagedTree cleans up its staging directory.
|
|
staged2 = std::unexpected{make_archive_error(ArchiveErrorCode::none)};
|
|
Void::Testing::expect(!std::filesystem::exists(stage2_root));
|
|
|
|
// 3. Post-rename parent fsync failure reports Unknown outcome with identity
|
|
// and does NOT claim rollback.
|
|
auto staged3 = extract_staged(archive_path, parent_dir);
|
|
Void::Testing::require(staged3.has_value());
|
|
{
|
|
ScopedFaultGuard fault{FaultPoint::post_rename_parent_sync};
|
|
auto unknown = staged3->publish("v2");
|
|
Void::Testing::require(unknown.has_value());
|
|
Void::Testing::expect_eq(static_cast<int>(unknown->outcome),
|
|
static_cast<int>(PublicationOutcome::Unknown));
|
|
Void::Testing::expect_eq(unknown->destination_path, parent_dir / "v2");
|
|
Void::Testing::expect(!unknown->staging_id.empty());
|
|
Void::Testing::expect(std::filesystem::exists(parent_dir / "v2/bin/app"));
|
|
}
|
|
|
|
// 4. Failed extraction with cleanup fault preserves primary error +
|
|
// retryable staging_id; cleanup_stale_staging cleans only valid marked dirs.
|
|
const auto foreign_dir = parent_dir / ".void-stage-foreign";
|
|
std::filesystem::create_directories(foreign_dir);
|
|
{
|
|
ScopedFaultGuard fault{FaultPoint::staging_cleanup_fail};
|
|
StagedExtractionOptions tight{};
|
|
tight.limits.max_file_bytes = 4;
|
|
auto failed = extract_staged(archive_path, parent_dir, tight);
|
|
Void::Testing::require(!failed.has_value());
|
|
Void::Testing::expect_eq(
|
|
failed.error().code().value,
|
|
static_cast<std::int32_t>(ArchiveErrorCode::limit_exceeded));
|
|
Void::Testing::expect(failed.error().message().find("staging_id=") !=
|
|
std::string::npos);
|
|
}
|
|
auto cleaned = cleanup_stale_staging(parent_dir, std::chrono::seconds{0});
|
|
Void::Testing::require(cleaned.has_value());
|
|
Void::Testing::expect_eq(*cleaned, 1U);
|
|
Void::Testing::expect(std::filesystem::exists(foreign_dir));
|
|
}
|
|
|
|
Void::Testing::Test archive_isolated_worker_extracts_verifies_and_publishes()
|
|
{
|
|
#ifdef VOID_ARCHIVE_WORKER_PATH
|
|
Void::FileSystem::TempDir tmp{"void_archive_iso_"};
|
|
const auto archive_path =
|
|
tmp / (has_libarchive_backend() ? "untrusted.tar.zst" : "untrusted.tar");
|
|
const auto parent_dir = tmp / "out";
|
|
std::filesystem::create_directories(parent_dir);
|
|
|
|
const std::array<CreateEntry, 2> entries{{
|
|
CreateEntry{.path = "bin/tool",
|
|
.type = EntryType::RegularFile,
|
|
.executable = true,
|
|
.content = to_bytes("isolated-ok")},
|
|
CreateEntry{.path = "data/info.txt",
|
|
.type = EntryType::RegularFile,
|
|
.executable = false,
|
|
.content = to_bytes("verified-by-parent")},
|
|
}};
|
|
Void::Testing::require(
|
|
create_archive(archive_path, entries,
|
|
CreationOptions{.compression = default_test_compression()})
|
|
.has_value());
|
|
|
|
IsolationOptions iso{};
|
|
iso.worker_executable = VOID_ARCHIVE_WORKER_PATH;
|
|
iso.timeout = std::chrono::seconds{30};
|
|
iso.max_rss_bytes = 256ULL * 1024 * 1024;
|
|
|
|
auto staged = extract_staged_isolated(archive_path, parent_dir, iso);
|
|
Void::Testing::require(staged.has_value());
|
|
Void::Testing::expect_eq(staged->entries(), 2U);
|
|
|
|
auto published = staged->publish("rel-1");
|
|
Void::Testing::require(published.has_value());
|
|
Void::Testing::expect_eq(static_cast<int>(published->outcome),
|
|
static_cast<int>(PublicationOutcome::Published));
|
|
Void::Testing::expect_eq(read_file_str(parent_dir / "rel-1/bin/tool"),
|
|
std::string{"isolated-ok"});
|
|
Void::Testing::expect_eq(read_file_str(parent_dir / "rel-1/data/info.txt"),
|
|
std::string{"verified-by-parent"});
|
|
|
|
// Worker limit violation returns limit_exceeded and cleans staging.
|
|
IsolationOptions tight = iso;
|
|
tight.extraction.limits.max_file_bytes = 4;
|
|
auto failed = extract_staged_isolated(archive_path, parent_dir, tight);
|
|
Void::Testing::require(!failed.has_value());
|
|
Void::Testing::expect_eq(
|
|
failed.error().code().value,
|
|
static_cast<std::int32_t>(ArchiveErrorCode::limit_exceeded));
|
|
#else
|
|
Void::Testing::expect(true);
|
|
#endif
|
|
}
|