Update
CI / GCC 16 / C++26 / werror (push) Failing after 3m9s
CI / GCC 16 / C++26 / sanitizers (push) Failing after 10m15s
CI / GCC 16 / C++26 / default (push) Failing after 13m1s

Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-07-20 01:18:53 +03:00
1 parent 8583a479dd
commit 3851aeb703
42 files changed
+6948 -759

No files matched your search

+13
View File
@@ -81,6 +81,19 @@ if(_all_generated)
endforeach()
add_test(NAME VoidTests COMMAND VoidTests)
add_test(NAME VoidInstalledComponentConsumers
COMMAND ${CMAKE_COMMAND}
-DVOID_BUILD_DIR=${CMAKE_BINARY_DIR}
-DVOID_TEST_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}/package
-DVOID_TEST_ROOT=${CMAKE_CURRENT_BINARY_DIR}/package-consumer
-DVOID_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DVOID_CTEST_COMMAND=${CMAKE_CTEST_COMMAND}
-DVOID_BUILD_CONFIG=$<CONFIG>
-P ${CMAKE_CURRENT_SOURCE_DIR}/package/InstalledComponents.cmake
)
set_tests_properties(VoidInstalledComponentConsumers PROPERTIES
DEPENDS VoidTests)
endif()
# ── Test-support libraries ────────────────────────────────────────────────────
+24
View File
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 4.0)
project(VoidAuditedConsumer LANGUAGES CXX)
find_package(Void CONFIG REQUIRED COMPONENTS
Archive
Binary
Crypto
Plugin
System
Update
)
add_executable(void_audited_consumer main.cpp)
target_link_libraries(void_audited_consumer PRIVATE
Void::Archive
Void::Binary
Void::Crypto
Void::Plugin
Void::System
Void::Update
)
enable_testing()
add_test(NAME VoidAuditedConsumer COMMAND void_audited_consumer)
+28
View File
@@ -0,0 +1,28 @@
#include <Void/Archive/Archive.hpp>
#include <Void/Binary/Reader.hpp>
#include <Void/Crypto/Crypto.hpp>
#include <Void/Plugin/Loader.hpp>
#include <Void/Plugin/Manager.hpp>
#include <Void/System/SignalWatcher.hpp>
#include <Void/Update/Update.hpp>
#include <array>
#include <bit>
#include <cstddef>
#include <cstdint>
int main()
{
const std::array bytes{std::byte{0x01}, std::byte{0x02}};
Void::Binary::Reader reader{bytes};
const auto value = reader.read<std::uint16_t>(std::endian::big);
const auto digest = Void::Crypto::sha256(bytes);
const Void::Archive::ExtractionOptions limits{};
const Void::Update::VerificationPolicy policy{};
const Void::Plugin::PluginManager::Options plugins{};
return value && *value == 0x0102 && digest && !digest->empty() &&
limits.maxEntries > 0 && policy.maxReleases > 0 &&
plugins.debounce_polls > 0
? 0
: 1;
}
+10
View File
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 4.0)
project(VoidBinaryConsumer LANGUAGES CXX)
find_package(Void CONFIG REQUIRED COMPONENTS Binary)
add_executable(void_binary_consumer main.cpp)
target_link_libraries(void_binary_consumer PRIVATE Void::Binary)
enable_testing()
add_test(NAME VoidBinaryConsumer COMMAND void_binary_consumer)
+14
View File
@@ -0,0 +1,14 @@
#include <Void/Binary/Reader.hpp>
#include <array>
#include <bit>
#include <cstddef>
#include <cstdint>
int main()
{
const std::array bytes{std::byte{0x01}, std::byte{0x02}};
Void::Binary::Reader reader{bytes};
const auto value = reader.read<std::uint16_t>(std::endian::big);
return value && *value == 0x0102 ? 0 : 1;
}
+56
View File
@@ -0,0 +1,56 @@
cmake_minimum_required(VERSION 4.0)
foreach(required IN ITEMS VOID_BUILD_DIR VOID_TEST_SOURCE_DIR VOID_TEST_ROOT
VOID_CXX_COMPILER VOID_CTEST_COMMAND)
if(NOT DEFINED ${required} OR "${${required}}" STREQUAL "")
message(FATAL_ERROR "${required} is required")
endif()
endforeach()
file(REMOVE_RECURSE "${VOID_TEST_ROOT}")
set(install_prefix "${VOID_TEST_ROOT}/install")
set(install_command "${CMAKE_COMMAND}" --install "${VOID_BUILD_DIR}"
--prefix "${install_prefix}")
if(DEFINED VOID_BUILD_CONFIG AND NOT VOID_BUILD_CONFIG STREQUAL "")
list(APPEND install_command --config "${VOID_BUILD_CONFIG}")
endif()
execute_process(COMMAND ${install_command} COMMAND_ERROR_IS_FATAL ANY)
function(run_consumer name)
set(source_dir "${VOID_TEST_SOURCE_DIR}/${name}")
set(build_dir "${VOID_TEST_ROOT}/${name}")
execute_process(
COMMAND "${CMAKE_COMMAND}" -S "${source_dir}" -B "${build_dir}"
"-DCMAKE_PREFIX_PATH=${install_prefix}"
"-DCMAKE_CXX_COMPILER=${VOID_CXX_COMPILER}"
${ARGN}
COMMAND_ERROR_IS_FATAL ANY)
set(build_command "${CMAKE_COMMAND}" --build "${build_dir}")
set(test_command "${VOID_CTEST_COMMAND}" --test-dir "${build_dir}"
--output-on-failure)
if(DEFINED VOID_BUILD_CONFIG AND NOT VOID_BUILD_CONFIG STREQUAL "")
list(APPEND build_command --config "${VOID_BUILD_CONFIG}")
list(APPEND test_command -C "${VOID_BUILD_CONFIG}")
endif()
execute_process(COMMAND ${build_command} COMMAND_ERROR_IS_FATAL ANY)
execute_process(COMMAND ${test_command} COMMAND_ERROR_IS_FATAL ANY)
endfunction()
# A header-only component must remain independently consumable even when every
# optional external package is explicitly unavailable.
run_consumer(Binary
-DCMAKE_DISABLE_FIND_PACKAGE_Boost=TRUE
-DCMAKE_DISABLE_FIND_PACKAGE_LibArchive=TRUE
-DCMAKE_DISABLE_FIND_PACKAGE_OpenSSL=TRUE
-DCMAKE_DISABLE_FIND_PACKAGE_PkgConfig=TRUE
-DCMAKE_DISABLE_FIND_PACKAGE_SQLite3=TRUE
-DCMAKE_DISABLE_FIND_PACKAGE_Threads=TRUE)
# Exercise the transitive dependency closure for every newly extracted runtime
# module while proving unrelated Sodium and SQLite dependencies are not loaded.
run_consumer(Audited
-DCMAKE_DISABLE_FIND_PACKAGE_Boost=TRUE
-DCMAKE_DISABLE_FIND_PACKAGE_PkgConfig=TRUE
-DCMAKE_DISABLE_FIND_PACKAGE_SQLite3=TRUE)
+253
View File
@@ -0,0 +1,253 @@
#include <Void/Archive/Archive.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <array>
#include <atomic>
#include <filesystem>
#include <fstream>
#include <memory>
#include <span>
#include <string>
#include <string_view>
#include <system_error>
#include <thread>
#include <archive.h>
#include <archive_entry.h>
namespace
{
struct ArchiveEntry {
std::string_view path;
std::string_view contents;
unsigned int type{AE_IFREG};
std::string_view link;
};
void write_archive(const std::filesystem::path &path,
std::span<const ArchiveEntry>
entries)
{
using Writer = std::unique_ptr<archive, decltype(&archive_write_free)>;
using Entry =
std::unique_ptr<archive_entry, decltype(&archive_entry_free)>;
Writer 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);
#ifdef _WIN32
Void::Testing::require(archive_write_open_filename_w(
writer.get(), path.c_str()) == ARCHIVE_OK);
#else
Void::Testing::require(archive_write_open_filename(
writer.get(), path.c_str()) == ARCHIVE_OK);
#endif
for (const auto &specification : entries) {
Entry entry{archive_entry_new(), &archive_entry_free};
const std::string entryPath{specification.path};
archive_entry_set_pathname(entry.get(), entryPath.c_str());
archive_entry_set_filetype(entry.get(), specification.type);
archive_entry_set_perm(
entry.get(), specification.type == AE_IFDIR ? 0755 : 0644);
if (specification.type == AE_IFREG) {
archive_entry_set_size(
entry.get(),
static_cast<la_int64_t>(specification.contents.size()));
}
if (!specification.link.empty()) {
const std::string link{specification.link};
archive_entry_set_symlink(entry.get(), link.c_str());
}
Void::Testing::require(
archive_write_header(writer.get(), entry.get()) == ARCHIVE_OK);
std::size_t offset = 0;
while (offset < specification.contents.size()) {
const auto count = archive_write_data(
writer.get(), specification.contents.data() + offset,
specification.contents.size() - offset);
Void::Testing::require(count > 0);
offset += static_cast<std::size_t>(count);
}
Void::Testing::require(archive_write_finish_entry(writer.get()) ==
ARCHIVE_OK);
}
Void::Testing::require(archive_write_close(writer.get()) == ARCHIVE_OK);
}
void write_archive(const std::filesystem::path &path,
std::string_view entryPath, std::string_view contents,
unsigned int type = AE_IFREG, std::string_view link = {})
{
const std::array entries{ArchiveEntry{entryPath, contents, type, link}};
write_archive(path, entries);
}
[[nodiscard]] bool
extraction_file_visible(const std::filesystem::path &destination)
{
std::error_code error;
if (std::filesystem::exists(destination / "anchor", error)) {
return true;
}
error.clear();
for (std::filesystem::directory_iterator iterator{destination, error};
!error && iterator != std::filesystem::directory_iterator{};
iterator.increment(error)) {
if (iterator->path().filename().string().starts_with(
".void-archive-")) {
return true;
}
}
return false;
}
} // namespace
Void::Testing::Test archive_extracts_bounded_tar_zstd()
{
Void::FileSystem::TempDir temporary{"void_archive_"};
const auto source = temporary / "package.tar.zst";
const auto destination = temporary / "output";
write_archive(source, "bin/tool", "payload");
auto extracted = Void::Archive::extract(source, destination);
Void::Testing::require(extracted.has_value());
Void::Testing::expect_eq(extracted->entries, std::size_t{1});
Void::Testing::expect_eq(extracted->bytes, std::uint64_t{7});
std::ifstream input{destination / "bin/tool"};
std::string contents;
input >> contents;
Void::Testing::expect_eq(contents, std::string{"payload"});
}
Void::Testing::Test archive_accepts_explicit_directory_entries()
{
Void::FileSystem::TempDir temporary{"void_archive_"};
const auto source = temporary / "directory.tar.zst";
const auto destination = temporary / "output";
const std::array entries{ArchiveEntry{"bin/", {}, AE_IFDIR},
ArchiveEntry{"bin/tool", "payload"}};
write_archive(source, entries);
auto extracted = Void::Archive::extract(source, destination);
Void::Testing::require(extracted.has_value());
Void::Testing::expect_eq(extracted->entries, std::size_t{2});
Void::Testing::expect_eq(extracted->bytes, std::uint64_t{7});
Void::Testing::expect(std::filesystem::is_directory(destination / "bin"));
std::ifstream input{destination / "bin/tool"};
std::string contents;
input >> contents;
Void::Testing::expect_eq(contents, std::string{"payload"});
}
Void::Testing::Test archive_rejects_traversal_and_links()
{
Void::FileSystem::TempDir temporary{"void_archive_"};
const auto traversal = temporary / "traversal.tar.zst";
write_archive(traversal, "../escape", "bad");
Void::Testing::expect(
!Void::Archive::extract(traversal, temporary / "out1").has_value());
Void::Testing::expect(
!std::filesystem::exists(temporary.path() / "escape"));
const auto symlink = temporary / "symlink.tar.zst";
write_archive(symlink, "link", {}, AE_IFLNK, "/tmp/escape");
Void::Testing::expect(
!Void::Archive::extract(symlink, temporary / "out2").has_value());
const auto trailingFile = temporary / "trailing-file.tar.zst";
write_archive(trailingFile, "file/", "bad");
Void::Testing::expect(
!Void::Archive::extract(trailingFile, temporary / "out3").has_value());
}
Void::Testing::Test archive_rejects_symlink_parent()
{
#ifndef _WIN32
Void::FileSystem::TempDir temporary{"void_archive_"};
const auto source = temporary / "parent-link.tar.zst";
const auto destination = temporary / "output";
const auto outside = temporary / "outside";
std::filesystem::create_directories(destination);
std::filesystem::create_directories(outside);
std::filesystem::create_directory_symlink(outside, destination / "parent");
write_archive(source, "parent/escape", "bad");
Void::Testing::expect(
!Void::Archive::extract(source, destination).has_value());
Void::Testing::expect(!std::filesystem::exists(outside / "escape"));
#else
Void::Testing::expect(true);
#endif
}
Void::Testing::Test archive_pins_destination_during_path_swap()
{
#ifndef _WIN32
Void::FileSystem::TempDir temporary{"void_archive_"};
const auto source = temporary / "swap.tar.zst";
const auto destination = temporary / "output";
const auto displaced = temporary / "displaced";
const auto outside = temporary / "outside";
std::filesystem::create_directories(destination);
std::filesystem::create_directories(outside);
const std::string largePayload(32U * 1024U * 1024U, 'x');
const std::array entries{ArchiveEntry{"anchor", largePayload},
ArchiveEntry{"second", "safe"}};
write_archive(source, entries);
std::atomic_bool extractionFinished{false};
std::atomic_bool swapped{false};
std::jthread attacker{[&] {
while (!extractionFinished.load(std::memory_order_acquire)) {
if (!extraction_file_visible(destination)) {
std::this_thread::yield();
continue;
}
std::error_code error;
std::filesystem::rename(destination, displaced, error);
if (error) {
return;
}
std::filesystem::create_directory_symlink(outside, destination,
error);
if (error) {
std::error_code restoreError;
std::filesystem::rename(displaced, destination, restoreError);
return;
}
swapped.store(true, std::memory_order_release);
return;
}
}};
auto extracted = Void::Archive::extract(source, destination);
extractionFinished.store(true, std::memory_order_release);
attacker.join();
Void::Testing::require(swapped.load(std::memory_order_acquire));
Void::Testing::require(extracted.has_value());
Void::Testing::expect(!std::filesystem::exists(outside / "second"));
Void::Testing::expect(std::filesystem::exists(displaced / "second"));
#else
Void::Testing::expect(true);
#endif
}
Void::Testing::Test archive_enforces_actual_size_limit()
{
Void::FileSystem::TempDir temporary{"void_archive_"};
const auto source = temporary / "large.tar.zst";
write_archive(source, "file", "12345678");
Void::Archive::ExtractionOptions options;
options.maxFileBytes = 4;
Void::Testing::expect(
!Void::Archive::extract(source, temporary / "out", options)
.has_value());
Void::Testing::expect(
!std::filesystem::exists(temporary.path() / "out/file"));
}
+31
View File
@@ -0,0 +1,31 @@
#include <Void/Binary/Reader.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <array>
Void::Testing::Test binary_reader_checks_bounds_and_endian()
{
const std::array bytes{std::byte{0x12}, std::byte{0x34}, std::byte{0x56}};
Void::Binary::Reader reader{bytes};
auto value = reader.read<std::uint16_t>(std::endian::big);
Void::Testing::require(value.has_value());
Void::Testing::expect_eq(*value, static_cast<std::uint16_t>(0x1234));
Void::Testing::expect_eq(reader.position(), static_cast<std::size_t>(2));
auto overflow = reader.read<std::uint16_t>(std::endian::big);
Void::Testing::expect(!overflow.has_value());
Void::Testing::expect_eq(reader.position(), static_cast<std::size_t>(2));
}
Void::Testing::Test binary_reader_subreader_is_bounded()
{
const std::array bytes{std::byte{'a'}, std::byte{'b'}, std::byte{'c'}};
Void::Binary::Reader reader{bytes};
auto child = reader.subreader(2);
Void::Testing::require(child.has_value());
auto value = child->readString(2);
Void::Testing::require(value.has_value());
Void::Testing::expect_eq(*value, std::string{"ab"});
Void::Testing::expect(!child->readBytes(1).has_value());
Void::Testing::expect_eq(reader.remaining(), static_cast<std::size_t>(1));
}
+77
View File
@@ -0,0 +1,77 @@
#include <Void/Crypto/Crypto.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <Void/Text/Hex.hpp>
#include <array>
#include <fstream>
namespace
{
std::string hex(const Void::Crypto::Digest& digest)
{
return Void::Encoding::Hex::encode(digest);
}
} // namespace
Void::Testing::Test crypto_known_hash_vectors()
{
auto sha1 = Void::Crypto::sha1("abc");
auto sha256 = Void::Crypto::sha256("abc");
auto sha512 = Void::Crypto::sha512("abc");
Void::Testing::require(sha1.has_value());
Void::Testing::require(sha256.has_value());
Void::Testing::require(sha512.has_value());
Void::Testing::expect_eq(
hex(*sha1), std::string{"a9993e364706816aba3e25717850c26c9cd0d89d"});
Void::Testing::expect_eq(hex(*sha256),
std::string{"ba7816bf8f01cfea414140de5dae2223b0036"
"1a396177a9cb410ff61f20015ad"});
Void::Testing::expect_eq(
hex(*sha512),
std::string{
"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a21"
"92992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"});
}
Void::Testing::Test crypto_streaming_and_file_hash_match()
{
auto hasher =
Void::Crypto::Hasher::create(Void::Crypto::HashAlgorithm::Sha256);
Void::Testing::require(hasher.has_value());
Void::Testing::require(hasher->update("ab").has_value());
Void::Testing::require(hasher->update("c").has_value());
auto streamed = hasher->finalize();
Void::Testing::require(streamed.has_value());
Void::Testing::expect(!hasher->update("again").has_value());
Void::FileSystem::TempDir temporary{"void_crypto_"};
const auto path = temporary / "data";
std::ofstream{path, std::ios::binary} << "abc";
auto file = Void::Crypto::sha256File(path);
Void::Testing::require(file.has_value());
Void::Testing::expect_eq(*file, *streamed);
}
Void::Testing::Test crypto_ed25519_rfc8032_vector()
{
const auto publicText = Void::Encoding::Hex::decode(
"d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a");
const auto signatureText = Void::Encoding::Hex::decode(
"e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e06522490155"
"5fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b");
Void::Testing::require(publicText.has_value());
Void::Testing::require(signatureText.has_value());
const auto publicKey =
std::as_bytes(std::span{publicText->data(), publicText->size()});
const auto signature =
std::as_bytes(std::span{signatureText->data(), signatureText->size()});
auto valid = Void::Crypto::verifyEd25519({}, signature, publicKey);
Void::Testing::require(valid.has_value());
Void::Testing::expect(*valid);
const std::array message{std::byte{1}};
auto invalid = Void::Crypto::verifyEd25519(message, signature, publicKey);
Void::Testing::require(invalid.has_value());
Void::Testing::expect(!*invalid);
}
+34
View File
@@ -90,6 +90,40 @@ Void::Testing::Test manager_reload_migrates_v1_state_to_v2()
Void::Testing::expect_eq(stateful(h)->counter(), 3); // migrated
}
Void::Testing::Test manager_replace_migrates_from_immutable_new_path()
{
PluginManager manager;
Void::Testing::require(manager.load(VOID_STATEFUL_V1_PATH).has_value());
auto pinned = manager.acquire("StatefulPlugin");
stateful(pinned)->bump();
stateful(pinned)->bump();
auto replaced = manager.replace("StatefulPlugin", VOID_STATEFUL_V2_PATH);
Void::Testing::require(replaced.has_value());
Void::Testing::expect_eq(stateful(pinned)->schema_version(), 1);
auto current = manager.acquire("StatefulPlugin");
Void::Testing::expect_eq(stateful(current)->schema_version(), 2);
Void::Testing::expect_eq(stateful(current)->counter(), 2);
}
Void::Testing::Test manager_replace_failure_keeps_current_generation()
{
PluginManager manager;
Void::Testing::require(manager.load(VOID_STATEFUL_V1_PATH).has_value());
auto before = manager.acquire("StatefulPlugin");
stateful(before)->bump();
#ifdef VOID_TEST_PLUGIN_PATH
auto mismatch = manager.replace("StatefulPlugin", VOID_TEST_PLUGIN_PATH);
Void::Testing::expect(!mismatch.has_value());
Void::Testing::expect(mismatch.error() == LoadError::NameMismatch);
#endif
auto missing = manager.replace("StatefulPlugin", "/no/such/plugin.so");
Void::Testing::expect(!missing.has_value());
Void::Testing::expect(missing.error() == LoadError::FileNotFound);
auto after = manager.acquire("StatefulPlugin");
Void::Testing::expect_eq(stateful(after)->schema_version(), 1);
Void::Testing::expect_eq(stateful(after)->counter(), 1);
}
Void::Testing::Test manager_pinned_handle_survives_reload()
{
Void::FileSystem::TempDir tmp{"void_mgr"};
+22
View File
@@ -0,0 +1,22 @@
#include <Void/System/SignalWatcher.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <chrono>
#include <csignal>
Void::Testing::Test signal_watcher_polls_and_receives_blocked_signal()
{
#ifdef __linux__
auto watcher = Void::Process::SignalWatcher::create({SIGUSR1});
Void::Testing::require(watcher.has_value());
auto empty = watcher->poll();
Void::Testing::require(empty.has_value());
Void::Testing::expect(!empty->has_value());
Void::Testing::require(::raise(SIGUSR1) == 0);
auto received = watcher->wait(std::chrono::milliseconds{100});
Void::Testing::require(received.has_value());
Void::Testing::require(received->has_value());
Void::Testing::expect_eq(**received, SIGUSR1);
#endif
}
+145 -95
View File
@@ -1,240 +1,290 @@
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <Void/Text/Base64.hpp>
#include <Void/Text/Hex.hpp>
#include <Void/Text/Html.hpp>
#include <Void/Text/Url.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
// ── Base64 encode ─────────────────────────────────────────────────────────────
#include <array>
#include <cstddef>
#include <cstdint>
#include <span>
// ── Base64 encode
// ─────────────────────────────────────────────────────────────
Void::Testing::Test base64_encode_empty()
{
Void::Testing::expect(Void::Encoding::Base64::encode("").empty());
Void::Testing::expect(Void::Encoding::Base64::encode("").empty());
}
Void::Testing::Test base64_encode_man()
{
Void::Testing::expect_eq(Void::Encoding::Base64::encode("Man"),
std::string{"TWFu"});
Void::Testing::expect_eq(Void::Encoding::Base64::encode("Man"),
std::string{"TWFu"});
}
Void::Testing::Test base64_encode_ma()
{
Void::Testing::expect_eq(Void::Encoding::Base64::encode("Ma"),
std::string{"TWE="});
Void::Testing::expect_eq(Void::Encoding::Base64::encode("Ma"),
std::string{"TWE="});
}
Void::Testing::Test base64_encode_m()
{
Void::Testing::expect_eq(Void::Encoding::Base64::encode("M"),
std::string{"TQ=="});
Void::Testing::expect_eq(Void::Encoding::Base64::encode("M"),
std::string{"TQ=="});
}
Void::Testing::Test base64_encode_hello_world()
{
Void::Testing::expect_eq(Void::Encoding::Base64::encode("Hello, World!"),
std::string{"SGVsbG8sIFdvcmxkIQ=="});
Void::Testing::expect_eq(Void::Encoding::Base64::encode("Hello, World!"),
std::string{"SGVsbG8sIFdvcmxkIQ=="});
}
// ── Base64 decode ─────────────────────────────────────────────────────────────
// ── Base64 decode
// ─────────────────────────────────────────────────────────────
Void::Testing::Test base64_decode_empty()
{
const auto r = Void::Encoding::Base64::decode("");
Void::Testing::expect(r.has_value());
Void::Testing::expect(r->empty());
const auto r = Void::Encoding::Base64::decode("");
Void::Testing::expect(r.has_value());
Void::Testing::expect(r->empty());
}
Void::Testing::Test base64_decode_man()
{
const auto r = Void::Encoding::Base64::decode("TWFu");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"Man"});
const auto r = Void::Encoding::Base64::decode("TWFu");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"Man"});
}
Void::Testing::Test base64_decode_with_padding()
{
const auto r = Void::Encoding::Base64::decode("SGVsbG8sIFdvcmxkIQ==");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"Hello, World!"});
const auto r = Void::Encoding::Base64::decode("SGVsbG8sIFdvcmxkIQ==");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"Hello, World!"});
}
Void::Testing::Test base64_decode_invalid_returns_nullopt()
{
const auto r = Void::Encoding::Base64::decode("!!");
Void::Testing::expect(!r.has_value());
const auto r = Void::Encoding::Base64::decode("!!");
Void::Testing::expect(!r.has_value());
}
Void::Testing::Test base64_decode_rejects_malformed_padding()
{
Void::Testing::expect(!Void::Encoding::Base64::decode("T=WE").has_value());
Void::Testing::expect(!Void::Encoding::Base64::decode("TWFu=").has_value());
Void::Testing::expect(!Void::Encoding::Base64::decode("TQ===").has_value());
}
Void::Testing::Test base64_decode_rejects_nonzero_trailing_bits()
{
Void::Testing::expect(!Void::Encoding::Base64::decode("TR==").has_value());
}
Void::Testing::Test base64_round_trip()
{
const std::string original = "The quick brown fox jumps over the lazy dog";
const auto encoded = Void::Encoding::Base64::encode(original);
const auto decoded = Void::Encoding::Base64::decode(encoded);
Void::Testing::expect(decoded.has_value());
Void::Testing::expect_eq(*decoded, original);
const std::string original = "The quick brown fox jumps over the lazy dog";
const auto encoded = Void::Encoding::Base64::encode(original);
const auto decoded = Void::Encoding::Base64::decode(encoded);
Void::Testing::expect(decoded.has_value());
Void::Testing::expect_eq(*decoded, original);
}
// ── Base64 URL ────────────────────────────────────────────────────────────────
Void::Testing::Test base64_byte_buffer_round_trip()
{
const std::array<std::uint8_t, 5> original{0x00, 0x7F, 0x80, 0xFE, 0xFF};
const auto encoded =
Void::Encoding::Base64::encode(std::span<const std::uint8_t>{original});
const auto decoded = Void::Encoding::Base64::decode_bytes(encoded);
Void::Testing::expect(decoded.has_value());
Void::Testing::expect_eq(decoded->size(), original.size());
for (std::size_t i = 0; i < original.size(); ++i) {
Void::Testing::expect_eq(std::to_integer<std::uint8_t>((*decoded)[i]),
original[i]);
}
}
// ── Base64 URL
// ────────────────────────────────────────────────────────────────
Void::Testing::Test base64url_no_padding()
{
const auto s = Void::Encoding::Base64::encode_url("Man");
Void::Testing::expect_eq(s, std::string{"TWFu"});
Void::Testing::expect(s.find('=') == std::string::npos);
const auto s = Void::Encoding::Base64::encode_url("Man");
Void::Testing::expect_eq(s, std::string{"TWFu"});
Void::Testing::expect(s.find('=') == std::string::npos);
}
Void::Testing::Test base64url_no_plus_or_slash()
{
// encode something that would produce + or / in standard base64
const std::string tricky = "\xfb\xff\xfe";
const auto s = Void::Encoding::Base64::encode_url(tricky);
Void::Testing::expect(s.find('+') == std::string::npos);
Void::Testing::expect(s.find('/') == std::string::npos);
// encode something that would produce + or / in standard base64
const std::string tricky = "\xfb\xff\xfe";
const auto s = Void::Encoding::Base64::encode_url(tricky);
Void::Testing::expect(s.find('+') == std::string::npos);
Void::Testing::expect(s.find('/') == std::string::npos);
}
// ── Hex encode ───────────────────────────────────────────────────────────────
Void::Testing::Test hex_encode_empty()
{
Void::Testing::expect(Void::Encoding::Hex::encode("").empty());
Void::Testing::expect(Void::Encoding::Hex::encode("").empty());
}
Void::Testing::Test hex_encode_hello()
{
Void::Testing::expect_eq(Void::Encoding::Hex::encode("Hi"),
std::string{"4869"});
Void::Testing::expect_eq(Void::Encoding::Hex::encode("Hi"),
std::string{"4869"});
}
Void::Testing::Test hex_encode_binary()
{
const std::string bin{'\x00', '\xff', '\xab'};
Void::Testing::expect_eq(Void::Encoding::Hex::encode(bin),
std::string{"00ffab"});
const std::string bin{'\x00', '\xff', '\xab'};
Void::Testing::expect_eq(Void::Encoding::Hex::encode(bin),
std::string{"00ffab"});
}
Void::Testing::Test hex_encode_upper()
{
Void::Testing::expect_eq(Void::Encoding::Hex::encode_upper("Hi"),
std::string{"4869"});
Void::Testing::expect_eq(Void::Encoding::Hex::encode_upper("Hi"),
std::string{"4869"});
}
// ── Hex decode ───────────────────────────────────────────────────────────────
Void::Testing::Test hex_decode_empty()
{
const auto r = Void::Encoding::Hex::decode("");
Void::Testing::expect(r.has_value());
Void::Testing::expect(r->empty());
const auto r = Void::Encoding::Hex::decode("");
Void::Testing::expect(r.has_value());
Void::Testing::expect(r->empty());
}
Void::Testing::Test hex_decode_hello()
{
const auto r = Void::Encoding::Hex::decode("4869");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"Hi"});
const auto r = Void::Encoding::Hex::decode("4869");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"Hi"});
}
Void::Testing::Test hex_decode_uppercase()
{
const auto r = Void::Encoding::Hex::decode("4869");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"Hi"});
const auto r = Void::Encoding::Hex::decode("4869");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"Hi"});
}
Void::Testing::Test hex_decode_odd_length_fails()
{
Void::Testing::expect(!Void::Encoding::Hex::decode("abc").has_value());
Void::Testing::expect(!Void::Encoding::Hex::decode("abc").has_value());
}
Void::Testing::Test hex_decode_invalid_char_fails()
{
Void::Testing::expect(!Void::Encoding::Hex::decode("zz").has_value());
Void::Testing::expect(!Void::Encoding::Hex::decode("zz").has_value());
}
Void::Testing::Test hex_round_trip()
{
const std::string original = "binary\x00\x01\x02\xff";
const auto encoded = Void::Encoding::Hex::encode(original);
const auto decoded = Void::Encoding::Hex::decode(encoded);
Void::Testing::expect(decoded.has_value());
Void::Testing::expect_eq(*decoded, original);
const std::string original = "binary\x00\x01\x02\xff";
const auto encoded = Void::Encoding::Hex::encode(original);
const auto decoded = Void::Encoding::Hex::decode(encoded);
Void::Testing::expect(decoded.has_value());
Void::Testing::expect_eq(*decoded, original);
}
// ── URL / percent encoding ─────────────────────────────────────────────────────
Void::Testing::Test hex_byte_buffer_round_trip()
{
const std::array<std::uint8_t, 5> original{0x00, 0x7F, 0x80, 0xFE, 0xFF};
const auto encoded =
Void::Encoding::Hex::encode(std::span<const std::uint8_t>{original});
const auto decoded = Void::Encoding::Hex::decode_bytes(encoded);
Void::Testing::expect(decoded.has_value());
Void::Testing::expect_eq(decoded->size(), original.size());
for (std::size_t i = 0; i < original.size(); ++i) {
Void::Testing::expect_eq(std::to_integer<std::uint8_t>((*decoded)[i]),
original[i]);
}
}
// ── URL / percent encoding
// ─────────────────────────────────────────────────────
Void::Testing::Test url_encode_unreserved_passthrough()
{
Void::Testing::expect_eq(Void::Encoding::Url::encode("AZaz09-._~"),
std::string{"AZaz09-._~"});
Void::Testing::expect_eq(Void::Encoding::Url::encode("AZaz09-._~"),
std::string{"AZaz09-._~"});
}
Void::Testing::Test url_encode_reserved()
{
Void::Testing::expect_eq(Void::Encoding::Url::encode("a b/c?d=e&f"),
std::string{"a%20b%2Fc%3Fd%3De%26f"});
Void::Testing::expect_eq(Void::Encoding::Url::encode("a b/c?d=e&f"),
std::string{"a%20b%2Fc%3Fd%3De%26f"});
}
Void::Testing::Test url_query_encode_space_as_plus()
{
Void::Testing::expect_eq(Void::Encoding::Url::query_encode("a b c"),
std::string{"a+b+c"});
Void::Testing::expect_eq(Void::Encoding::Url::query_encode("a b c"),
std::string{"a+b+c"});
}
Void::Testing::Test url_decode_basic()
{
const auto r = Void::Encoding::Url::decode("a%20b%2Fc");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"a b/c"});
const auto r = Void::Encoding::Url::decode("a%20b%2Fc");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"a b/c"});
}
Void::Testing::Test url_decode_plus_as_space()
{
const auto r = Void::Encoding::Url::decode("a+b", /*plus_as_space=*/true);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"a b"});
const auto r = Void::Encoding::Url::decode("a+b", /*plus_as_space=*/true);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"a b"});
}
Void::Testing::Test url_decode_malformed_fails()
{
Void::Testing::expect(!Void::Encoding::Url::decode("a%2").has_value());
Void::Testing::expect(!Void::Encoding::Url::decode("a%zz").has_value());
Void::Testing::expect(!Void::Encoding::Url::decode("a%2").has_value());
Void::Testing::expect(!Void::Encoding::Url::decode("a%zz").has_value());
}
Void::Testing::Test url_round_trip_utf8()
{
const std::string original = "café/naïve?x=1 2";
const auto enc = Void::Encoding::Url::encode(original);
const auto dec = Void::Encoding::Url::decode(enc);
Void::Testing::expect(dec.has_value());
Void::Testing::expect_eq(*dec, original);
const std::string original = "café/naïve?x=1 2";
const auto enc = Void::Encoding::Url::encode(original);
const auto dec = Void::Encoding::Url::decode(enc);
Void::Testing::expect(dec.has_value());
Void::Testing::expect_eq(*dec, original);
}
// ── HTML entity decoding ───────────────────────────────────────────────────────
// ── HTML entity decoding
// ───────────────────────────────────────────────────────
Void::Testing::Test html_named_entities()
{
Void::Testing::expect_eq(
Void::Text::html_unescape("a &amp; b &lt; c &gt; d &quot;e&quot;"),
std::string{"a & b < c > d \"e\""});
Void::Testing::expect_eq(
Void::Text::html_unescape("a &amp; b &lt; c &gt; d &quot;e&quot;"),
std::string{"a & b < c > d \"e\""});
}
Void::Testing::Test html_numeric_decimal()
{
Void::Testing::expect_eq(Void::Text::html_unescape("&#65;&#66;&#67;"),
std::string{"ABC"});
Void::Testing::expect_eq(Void::Text::html_unescape("&#65;&#66;&#67;"),
std::string{"ABC"});
}
Void::Testing::Test html_numeric_hex_utf8()
{
// U+1F600 grinning face -> 4-byte UTF-8
const auto r = Void::Text::html_unescape("&#x1F600;");
Void::Testing::expect_eq(r.size(), std::size_t{4});
// U+1F600 grinning face -> 4-byte UTF-8
const auto r = Void::Text::html_unescape("&#x1F600;");
Void::Testing::expect_eq(r.size(), std::size_t{4});
}
Void::Testing::Test html_unknown_left_verbatim()
{
Void::Testing::expect_eq(Void::Text::html_unescape("a &bogus; b"),
std::string{"a &bogus; b"});
Void::Testing::expect_eq(Void::Text::html_unescape("plain & text"),
std::string{"plain & text"});
Void::Testing::expect_eq(Void::Text::html_unescape("a &bogus; b"),
std::string{"a &bogus; b"});
Void::Testing::expect_eq(Void::Text::html_unescape("plain & text"),
std::string{"plain & text"});
}
+114 -69
View File
@@ -4,158 +4,203 @@
#include <string>
// ── parse ─────────────────────────────────────────────────────────────────────
// ── parse
// ─────────────────────────────────────────────────────────────────────
Void::Testing::Test semver_parse_simple()
{
const auto v = Void::Semver::parse("1.2.3");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->major, 1u);
Void::Testing::expect_eq(v->minor, 2u);
Void::Testing::expect_eq(v->patch, 3u);
const auto v = Void::Semver::parse("1.2.3");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->major, 1u);
Void::Testing::expect_eq(v->minor, 2u);
Void::Testing::expect_eq(v->patch, 3u);
}
Void::Testing::Test semver_parse_with_v_prefix()
{
const auto v = Void::Semver::parse("v2.0.0");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->major, 2u);
const auto v = Void::Semver::parse("v2.0.0");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->major, 2u);
}
Void::Testing::Test semver_parse_pre_release()
{
const auto v = Void::Semver::parse("1.0.0-alpha.1");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->pre_release, std::string{"alpha.1"});
const auto v = Void::Semver::parse("1.0.0-alpha.1");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->pre_release, std::string{"alpha.1"});
}
Void::Testing::Test semver_parse_build_metadata()
{
const auto v = Void::Semver::parse("1.0.0+build.123");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->build, std::string{"build.123"});
const auto v = Void::Semver::parse("1.0.0+build.123");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->build, std::string{"build.123"});
}
Void::Testing::Test semver_parse_full()
{
const auto v = Void::Semver::parse("2.3.4-beta.2+20240101");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->major, 2u);
Void::Testing::expect_eq(v->pre_release, std::string{"beta.2"});
Void::Testing::expect_eq(v->build, std::string{"20240101"});
const auto v = Void::Semver::parse("2.3.4-beta.2+20240101");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->major, 2u);
Void::Testing::expect_eq(v->pre_release, std::string{"beta.2"});
Void::Testing::expect_eq(v->build, std::string{"20240101"});
}
Void::Testing::Test semver_parse_invalid()
{
Void::Testing::expect(!Void::Semver::parse("not-a-version").has_value());
Void::Testing::expect(!Void::Semver::parse("1.2").has_value());
Void::Testing::expect(!Void::Semver::parse("not-a-version").has_value());
Void::Testing::expect(!Void::Semver::parse("1.2").has_value());
Void::Testing::expect(!Void::Semver::parse_strict("v1.2.3").has_value());
Void::Testing::expect(!Void::Semver::parse("01.2.3").has_value());
Void::Testing::expect(!Void::Semver::parse("1.2.3-alpha.01").has_value());
Void::Testing::expect(!Void::Semver::parse("1.2.3-alpha..1").has_value());
}
// ── to_string ─────────────────────────────────────────────────────────────────
// ── to_string
// ─────────────────────────────────────────────────────────────────
Void::Testing::Test semver_to_string_simple()
{
Void::Semver::Version v{1, 2, 3};
Void::Testing::expect_eq(v.to_string(), std::string{"1.2.3"});
Void::Semver::Version v{1, 2, 3};
Void::Testing::expect_eq(v.to_string(), std::string{"1.2.3"});
}
Void::Testing::Test semver_to_string_with_pre_release()
{
Void::Semver::Version v{1, 0, 0};
v.pre_release = "alpha";
Void::Testing::expect_eq(v.to_string(), std::string{"1.0.0-alpha"});
Void::Semver::Version v{1, 0, 0};
v.pre_release = "alpha";
Void::Testing::expect_eq(v.to_string(), std::string{"1.0.0-alpha"});
}
Void::Testing::Test semver_round_trip()
{
const auto v = Void::Semver::parse("3.1.4-rc.1+build1");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->to_string(), std::string{"3.1.4-rc.1+build1"});
const auto v = Void::Semver::parse("3.1.4-rc.1+build1");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->to_string(), std::string{"3.1.4-rc.1+build1"});
}
// ── comparison ───────────────────────────────────────────────────────────────
Void::Testing::Test semver_compare_major()
{
const auto a = *Void::Semver::parse("2.0.0");
const auto b = *Void::Semver::parse("1.9.9");
Void::Testing::expect(a > b);
const auto a = *Void::Semver::parse("2.0.0");
const auto b = *Void::Semver::parse("1.9.9");
Void::Testing::expect(a > b);
}
Void::Testing::Test semver_compare_minor()
{
const auto a = *Void::Semver::parse("1.2.0");
const auto b = *Void::Semver::parse("1.1.9");
Void::Testing::expect(a > b);
const auto a = *Void::Semver::parse("1.2.0");
const auto b = *Void::Semver::parse("1.1.9");
Void::Testing::expect(a > b);
}
Void::Testing::Test semver_compare_patch()
{
const auto a = *Void::Semver::parse("1.0.1");
const auto b = *Void::Semver::parse("1.0.0");
Void::Testing::expect(a > b);
const auto a = *Void::Semver::parse("1.0.1");
const auto b = *Void::Semver::parse("1.0.0");
Void::Testing::expect(a > b);
}
Void::Testing::Test semver_release_gt_pre_release()
{
const auto a = *Void::Semver::parse("1.0.0");
const auto b = *Void::Semver::parse("1.0.0-alpha");
Void::Testing::expect(a > b);
const auto a = *Void::Semver::parse("1.0.0");
const auto b = *Void::Semver::parse("1.0.0-alpha");
Void::Testing::expect(a > b);
}
Void::Testing::Test semver_equal()
{
const auto a = *Void::Semver::parse("1.2.3");
const auto b = *Void::Semver::parse("1.2.3");
Void::Testing::expect(a == b);
const auto a = *Void::Semver::parse("1.2.3");
const auto b = *Void::Semver::parse("1.2.3");
Void::Testing::expect(a == b);
}
Void::Testing::Test semver_prerelease_precedence_is_spec_compliant()
{
const auto rc2 = *Void::Semver::parse("1.0.0-rc.2");
const auto rc10 = *Void::Semver::parse("1.0.0-rc.10");
const auto numeric = *Void::Semver::parse("1.0.0-1");
const auto alpha = *Void::Semver::parse("1.0.0-alpha");
Void::Testing::expect(rc10 > rc2);
Void::Testing::expect(numeric < alpha);
Void::Testing::expect(*Void::Semver::parse("1.0.0-alpha") <
*Void::Semver::parse("1.0.0-alpha.1"));
}
Void::Testing::Test semver_version_ranges()
{
auto bounded = Void::Semver::parse_range(">=1.2.0 <2.0.0");
auto caret = Void::Semver::parse_range("^1.2.3");
auto wildcard = Void::Semver::parse_range("1.4.x || 2.0.0");
Void::Testing::require(bounded.has_value());
Void::Testing::require(caret.has_value());
Void::Testing::require(wildcard.has_value());
Void::Testing::expect(bounded->contains(*Void::Semver::parse("1.9.9")));
Void::Testing::expect(!bounded->contains(*Void::Semver::parse("2.0.0")));
Void::Testing::expect(caret->contains(*Void::Semver::parse("1.8.0")));
Void::Testing::expect(!caret->contains(*Void::Semver::parse("2.0.0")));
Void::Testing::expect(wildcard->contains(*Void::Semver::parse("1.4.9")));
Void::Testing::expect(wildcard->contains(*Void::Semver::parse("2.0.0")));
}
Void::Testing::Test semver_version_ranges_reject_overflowing_upper_bounds()
{
Void::Testing::expect(
!Void::Semver::parse_range("^4294967295.0.0").has_value());
Void::Testing::expect(
!Void::Semver::parse_range("4294967295.x").has_value());
Void::Testing::expect(
!Void::Semver::parse_range("1.4294967295.x").has_value());
}
// ── bump ─────────────────────────────────────────────────────────────────────
Void::Testing::Test semver_bump_major()
{
const auto v = Void::Semver::Version{1, 2, 3};
const auto b = v.bumped_major();
Void::Testing::expect_eq(b.major, 2u);
Void::Testing::expect_eq(b.minor, 0u);
Void::Testing::expect_eq(b.patch, 0u);
const auto v = Void::Semver::Version{1, 2, 3};
const auto b = v.bumped_major();
Void::Testing::expect_eq(b.major, 2u);
Void::Testing::expect_eq(b.minor, 0u);
Void::Testing::expect_eq(b.patch, 0u);
}
Void::Testing::Test semver_bump_minor()
{
const auto v = Void::Semver::Version{1, 2, 3};
const auto b = v.bumped_minor();
Void::Testing::expect_eq(b.minor, 3u);
Void::Testing::expect_eq(b.patch, 0u);
const auto v = Void::Semver::Version{1, 2, 3};
const auto b = v.bumped_minor();
Void::Testing::expect_eq(b.minor, 3u);
Void::Testing::expect_eq(b.patch, 0u);
}
Void::Testing::Test semver_bump_patch()
{
const auto v = Void::Semver::Version{1, 2, 3};
Void::Testing::expect_eq(v.bumped_patch().patch, 4u);
const auto v = Void::Semver::Version{1, 2, 3};
Void::Testing::expect_eq(v.bumped_patch().patch, 4u);
}
// ── compatible_with ───────────────────────────────────────────────────────────
// ── compatible_with
// ───────────────────────────────────────────────────────────
Void::Testing::Test semver_compatible_same_major()
{
const auto a = *Void::Semver::parse("1.3.0");
const auto req = *Void::Semver::parse("1.2.0");
Void::Testing::expect(a.compatible_with(req));
const auto a = *Void::Semver::parse("1.3.0");
const auto req = *Void::Semver::parse("1.2.0");
Void::Testing::expect(a.compatible_with(req));
}
Void::Testing::Test semver_incompatible_different_major()
{
const auto a = *Void::Semver::parse("2.0.0");
const auto req = *Void::Semver::parse("1.9.9");
Void::Testing::expect(!a.compatible_with(req));
const auto a = *Void::Semver::parse("2.0.0");
const auto req = *Void::Semver::parse("1.9.9");
Void::Testing::expect(!a.compatible_with(req));
}
Void::Testing::Test semver_is_stable()
{
Void::Testing::expect(Void::Semver::Version{1,0,0}.is_stable());
Void::Testing::expect(!Void::Semver::Version{0,9,0}.is_stable());
Void::Semver::Version pre{1,0,0};
pre.pre_release = "alpha";
Void::Testing::expect(!pre.is_stable());
Void::Testing::expect(Void::Semver::Version{1, 0, 0}.is_stable());
Void::Testing::expect(!Void::Semver::Version{0, 9, 0}.is_stable());
Void::Semver::Version pre{1, 0, 0};
pre.pre_release = "alpha";
Void::Testing::expect(!pre.is_stable());
}
+830
View File
@@ -0,0 +1,830 @@
#include <Void/Update/Update.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <Void/Text/Base64.hpp>
#include <Void/Text/Hex.hpp>
#include <array>
#include <cstring>
#include <filesystem>
#include <format>
#include <fstream>
#include <iterator>
#include <memory>
#include <openssl/evp.h>
namespace
{
constexpr std::string_view payload = R"json({
"schemaVersion": 1,
"feedId": "vendor-main",
"sequence": 7,
"generatedAt": "2026-01-01T00:00:00Z",
"expiresAt": "2099-01-01T00:00:00Z",
"releases": [{
"component": "service",
"artifactId": "main",
"channel": "stable",
"version": "1.2.0",
"cohort": "linux-abi-2",
"platform": "linux",
"arch": "x86_64",
"url": "https://updates.example.invalid/service-1.2.0.tar.zst",
"size": 1234,
"sha256": "1111111111111111111111111111111111111111111111111111111111111111",
"packageType": "tar.zst",
"compatibility": {
"versionContracts": {"host.api": ">=1.0.0 <2.0.0"},
"exactContracts": {"plugin.abi": "2"}
},
"releaseManifestSha256": "2222222222222222222222222222222222222222222222222222222222222222"
}]
})json";
std::string sign(std::string_view message)
{
const auto seedText = Void::Encoding::Hex::decode(
"9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60");
Void::Testing::require(seedText.has_value());
using Key = std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>;
using Context = std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)>;
Key key{EVP_PKEY_new_raw_private_key(
EVP_PKEY_ED25519, nullptr,
reinterpret_cast<const unsigned char*>(seedText->data()),
seedText->size()),
&EVP_PKEY_free};
Void::Testing::require(static_cast<bool>(key));
Context context{EVP_MD_CTX_new(), &EVP_MD_CTX_free};
Void::Testing::require(static_cast<bool>(context));
Void::Testing::require(EVP_DigestSignInit(context.get(), nullptr,
nullptr, nullptr,
key.get()) == 1);
std::string signature(64, '\0');
std::size_t size = signature.size();
Void::Testing::require(
EVP_DigestSign(
context.get(),
reinterpret_cast<unsigned char*>(signature.data()), &size,
reinterpret_cast<const unsigned char*>(message.data()),
message.size()) == 1);
signature.resize(size);
return signature;
}
Void::Update::Keyring keyring()
{
const auto publicText = Void::Encoding::Hex::decode(
"d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a");
Void::Testing::require(publicText.has_value());
Void::Update::Ed25519PublicKey key{};
std::memcpy(key.data(), publicText->data(), key.size());
return {{"test-key", key}};
}
std::string envelope(std::string signature)
{
return std::format("{{\"schemaVersion\":1,\"keyId\":\"test-key\","
"\"payload\":\"{}\",\"signature\":\"{}\"}}",
Void::Encoding::Base64::encode_url(payload),
Void::Encoding::Base64::encode_url(signature));
}
} // namespace
Void::Testing::Test update_feed_verifies_before_parsing()
{
auto verified =
Void::Update::verifyFeedEnvelope(envelope(sign(payload)), keyring());
Void::Testing::require(verified.has_value());
Void::Testing::expect_eq(verified->feed.feedId, std::string{"vendor-main"});
Void::Testing::expect_eq(verified->feed.sequence, std::uint64_t{7});
Void::Testing::expect_eq(verified->feed.releases.size(), std::size_t{1});
Void::Testing::expect_eq(verified->signedPayload, std::string{payload});
auto badSignature = sign(payload);
badSignature.front() ^= 1;
Void::Testing::expect(!Void::Update::verifyFeedEnvelope(
envelope(std::move(badSignature)), keyring())
.has_value());
}
Void::Testing::Test update_feed_unsigned_parse_replay_and_selection()
{
auto parsed = Void::Update::parseFeedPayload(payload);
Void::Testing::require(parsed.has_value());
Void::Update::VerificationPolicy replay;
replay.minimumSequence = 7;
Void::Testing::expect(
!Void::Update::parseFeedPayload(payload, replay).has_value());
auto newer = parsed->releases.front();
newer.version = *Void::Semver::parse("1.3.0");
parsed->releases.push_back(newer);
Void::Update::SelectionRequest request{
.component = "service",
.artifactId = "main",
.channel = "stable",
.environment = {.versionContracts = {{"host.api",
*Void::Semver::parse("1.5.0")}},
.exactContracts = {{"plugin.abi", "2"}},
.platform = "linux",
.architecture = "x86_64"},
.currentVersion = *Void::Semver::parse("1.1.0")};
auto selected = Void::Update::selectRelease(*parsed, request);
Void::Testing::require(selected.has_value());
Void::Testing::expect_eq(selected->version.to_string(),
std::string{"1.3.0"});
request.environment.exactContracts["plugin.abi"] = "3";
Void::Testing::expect(
!Void::Update::selectRelease(*parsed, request).has_value());
}
Void::Testing::Test update_feed_rejects_empty_fractional_timestamp()
{
std::string malformed{payload};
const auto timestamp = malformed.find("2026-01-01T00:00:00Z");
Void::Testing::require(timestamp != std::string::npos);
malformed.replace(timestamp,
std::string_view{"2026-01-01T00:00:00Z"}.size(),
"2026-01-01T00:00:00.Z");
Void::Testing::expect(
!Void::Update::parseFeedPayload(malformed).has_value());
}
Void::Testing::Test update_feed_rejects_noncanonical_sha256()
{
std::string uppercaseArtifactHash{payload};
const auto artifactHash = uppercaseArtifactHash.find(std::string(64, '1'));
Void::Testing::require(artifactHash != std::string::npos);
uppercaseArtifactHash[artifactHash] = 'A';
Void::Testing::expect(
!Void::Update::parseFeedPayload(uppercaseArtifactHash).has_value());
std::string uppercaseManifestHash{payload};
const auto manifestHash = uppercaseManifestHash.find(std::string(64, '2'));
Void::Testing::require(manifestHash != std::string::npos);
uppercaseManifestHash[manifestHash] = 'B';
Void::Testing::expect(
!Void::Update::parseFeedPayload(uppercaseManifestHash).has_value());
}
Void::Testing::Test update_feed_accepts_generic_artifact_kinds()
{
std::string sharedLibrary{payload};
constexpr std::string_view packageField{"\"packageType\": \"tar.zst\""};
const auto package = sharedLibrary.find(packageField);
Void::Testing::require(package != std::string::npos);
sharedLibrary.replace(package, packageField.size(),
"\"packageType\": \"shared-library\"");
const auto manifest = sharedLibrary.find(
",\n \"releaseManifestSha256\": "
"\"2222222222222222222222222222222222222222222222222222222222222222\"");
Void::Testing::require(manifest != std::string::npos);
sharedLibrary.erase(manifest,
std::string_view{",\n \"releaseManifestSha256\": "
"\"22222222222222222222222222222222222"
"22222222222222222222222222222\""}
.size());
auto parsed = Void::Update::parseFeedPayload(sharedLibrary);
Void::Testing::require(parsed.has_value());
Void::Testing::require(parsed->releases.size() == 1);
Void::Testing::expect_eq(parsed->releases.front().packageType,
std::string{"shared-library"});
Void::Testing::expect(
parsed->releases.front().releaseManifestSha256.empty());
const Void::Update::SelectionRequest request{
.component = "service",
.artifactId = "main",
.channel = "stable",
.environment = {.versionContracts = {{"host.api",
*Void::Semver::parse("1.5.0")}},
.exactContracts = {{"plugin.abi", "2"}},
.platform = "linux",
.architecture = "x86_64"},
.currentVersion = std::nullopt,
.requiredCohort = std::nullopt,
.allowDowngrade = false,
.includeCurrent = false};
const auto selected = Void::Update::selectRelease(*parsed, request);
Void::Testing::require(selected.has_value());
Void::Testing::expect_eq(selected->packageType,
std::string{"shared-library"});
const auto packageType = sharedLibrary.find("shared-library");
Void::Testing::require(packageType != std::string::npos);
sharedLibrary.replace(packageType,
std::string_view{"shared-library"}.size(), "file");
const auto fileFeed = Void::Update::parseFeedPayload(sharedLibrary);
Void::Testing::require(fileFeed.has_value());
Void::Testing::expect_eq(fileFeed->releases.front().packageType,
std::string{"file"});
const auto filePackageType = sharedLibrary.find("file");
Void::Testing::require(filePackageType != std::string::npos);
sharedLibrary.replace(filePackageType, std::string_view{"file"}.size(),
"wasm-module");
const auto wasmFeed = Void::Update::parseFeedPayload(sharedLibrary);
Void::Testing::require(wasmFeed.has_value());
Void::Testing::expect_eq(wasmFeed->releases.front().packageType,
std::string{"wasm-module"});
}
Void::Testing::Test version_store_commits_and_reconciles_activation()
{
Void::FileSystem::TempDir temporary{"void_versions_"};
Void::Update::VersionStore store{temporary.path() / "store"};
Void::Testing::require(store.initialize().has_value());
const auto version1 = *Void::Semver::parse("1.0.0");
const auto version2 = *Void::Semver::parse("2.0.0");
auto staging1 = store.prepare(version1);
Void::Testing::require(staging1.has_value());
std::ofstream{*staging1 / "marker"} << "one";
Void::Testing::require(store.publish(version1, *staging1).has_value());
Void::Testing::require(store.activate(version1).has_value());
Void::Testing::require(store.commit().has_value());
auto staging2 = store.prepare(version2);
Void::Testing::require(staging2.has_value());
std::ofstream{*staging2 / "marker"} << "two";
Void::Testing::require(store.publish(version2, *staging2).has_value());
Void::Testing::require(store.activate(version2).has_value());
Void::Testing::expect(!store.recordHealthy(version2).has_value());
auto current = store.currentVersion();
Void::Testing::require(current.has_value() && current->has_value());
Void::Testing::expect_eq((*current)->to_string(), std::string{"2.0.0"});
Void::Testing::require(store.reconcile().has_value());
current = store.currentVersion();
Void::Testing::require(current.has_value() && current->has_value());
Void::Testing::expect_eq((*current)->to_string(), std::string{"1.0.0"});
Void::Testing::expect(!store.pendingActivation()->has_value());
}
Void::Testing::Test version_store_commit_point_is_unambiguous_and_idempotent()
{
Void::FileSystem::TempDir temporary{"void_versions_commit_point_"};
Void::Update::VersionStore store{temporary.path() / "store"};
Void::Testing::require(store.initialize().has_value());
const auto version = *Void::Semver::parse("2.1.0");
auto staging = store.prepare(version);
Void::Testing::require(staging.has_value());
std::ofstream{*staging / "marker"} << "candidate";
Void::Testing::require(store.publish(version, *staging).has_value());
Void::Testing::require(store.activate(version).has_value());
Void::Testing::require(store.commit().has_value());
Void::Testing::expect(
!std::filesystem::exists(store.root() / "activation.json"));
Void::Testing::require(store.commit().has_value());
std::ifstream healthInput{store.root() / "health.log"};
const std::string healthHistory{std::istreambuf_iterator<char>{healthInput},
std::istreambuf_iterator<char>{}};
const auto healthyRecord = healthHistory.find("H 2.1.0\n");
Void::Testing::require(healthyRecord != std::string::npos);
Void::Testing::expect(healthHistory.find("H 2.1.0\n", healthyRecord + 1) ==
std::string::npos);
Void::Testing::require(store.rollback().has_value());
const auto current = store.currentVersion();
Void::Testing::require(current.has_value() && current->has_value());
Void::Testing::expect_eq((*current)->to_string(), std::string{"2.1.0"});
}
Void::Testing::Test version_store_reconciles_durable_commit_marker()
{
#ifndef _WIN32
Void::FileSystem::TempDir temporary{"void_versions_commit_marker_"};
Void::Update::VersionStore store{temporary.path() / "store"};
Void::Testing::require(store.initialize().has_value());
const auto version = *Void::Semver::parse("2.2.0");
auto staging = store.prepare(version);
Void::Testing::require(staging.has_value());
std::ofstream{*staging / "marker"} << "candidate";
Void::Testing::require(store.publish(version, *staging).has_value());
Void::Testing::require(store.activate(version).has_value());
std::ifstream healthInput{store.root() / "health.log"};
std::string healthHistory{std::istreambuf_iterator<char>{healthInput},
std::istreambuf_iterator<char>{}};
healthHistory += "H 2.2.0\n";
const auto replacement = store.root() / "health.replacement";
std::ofstream{replacement, std::ios::binary | std::ios::trunc}
<< healthHistory;
std::filesystem::rename(replacement, store.root() / "health.log");
Void::Testing::expect(
std::filesystem::exists(store.root() / "activation.json"));
const auto healthyRecord = healthHistory.find("H 2.2.0\n");
Void::Testing::require(healthyRecord != std::string::npos);
Void::Testing::expect(healthHistory.find("H 2.2.0\n", healthyRecord + 1) ==
std::string::npos);
Void::Testing::require(store.rollback().has_value());
const auto committedCurrent = store.currentVersion();
Void::Testing::require(committedCurrent.has_value() &&
committedCurrent->has_value());
Void::Testing::expect_eq((*committedCurrent)->to_string(),
std::string{"2.2.0"});
Void::Testing::require(store.reconcile().has_value());
Void::Testing::expect(
!std::filesystem::exists(store.root() / "activation.json"));
const auto reconciledCurrent = store.currentVersion();
Void::Testing::require(reconciledCurrent.has_value() &&
reconciledCurrent->has_value());
Void::Testing::expect_eq((*reconciledCurrent)->to_string(),
std::string{"2.2.0"});
#endif
}
Void::Testing::Test version_store_reactivation_refreshes_health_order()
{
Void::FileSystem::TempDir temporary{"void_versions_reactivation_"};
Void::Update::VersionStore store{temporary.path() / "store"};
Void::Testing::require(store.initialize().has_value());
for (const std::string_view text : {"1.0.0", "2.0.0", "3.0.0"}) {
const auto version = *Void::Semver::parse(text);
auto staging = store.prepare(version);
Void::Testing::require(staging.has_value());
std::ofstream{*staging / "marker"} << text;
Void::Testing::require(store.publish(version, *staging).has_value());
Void::Testing::require(store.activate(version).has_value());
Void::Testing::require(store.commit().has_value());
}
const auto reactivated = *Void::Semver::parse("1.0.0");
Void::Testing::require(store.activate(reactivated).has_value());
Void::Testing::require(store.commit().has_value());
const auto removed = store.prune(1);
Void::Testing::require(removed.has_value());
Void::Testing::expect_eq(removed->size(), std::size_t{1});
Void::Testing::expect_eq(removed->front().to_string(),
std::string{"2.0.0"});
Void::Testing::expect(
std::filesystem::is_directory(store.versionPath(reactivated)));
Void::Testing::expect(std::filesystem::is_directory(
store.versionPath(*Void::Semver::parse("3.0.0"))));
}
Void::Testing::Test version_store_records_external_lifecycle_without_staging()
{
Void::FileSystem::TempDir temporary{"void_versions_external_lifecycle_"};
Void::Update::VersionStore store{temporary.path() / "store"};
std::filesystem::create_directories(store.root() / "versions");
for (const std::string_view text :
{"1.0.0", "2.0.0", "3.0.0", "4.0.0", "5.0.0"}) {
std::filesystem::create_directories(
store.versionPath(*Void::Semver::parse(text)));
}
const auto version1 = *Void::Semver::parse("1.0.0");
const auto version2 = *Void::Semver::parse("2.0.0");
const auto version3 = *Void::Semver::parse("3.0.0");
const auto version4 = *Void::Semver::parse("4.0.0");
const auto version5 = *Void::Semver::parse("5.0.0");
Void::Testing::require(store.recordHealthy(version1).has_value());
Void::Testing::require(store.recordHealthy(version2).has_value());
Void::Testing::require(store.recordHealthy(version3).has_value());
Void::Testing::require(store.recordHealthy(version1).has_value());
Void::Testing::require(store.recordFailed(version2).has_value());
Void::Testing::require(store.recordFailed(version4).has_value());
Void::Testing::expect(!std::filesystem::exists(store.root() / "staging"));
std::ifstream healthInput{store.root() / "health.log"};
const std::string healthHistory{std::istreambuf_iterator<char>{healthInput},
std::istreambuf_iterator<char>{}};
Void::Testing::expect(healthHistory.find("F 2.0.0\n") == std::string::npos);
Void::Testing::expect(healthHistory.find("F 4.0.0\n") != std::string::npos);
#ifndef _WIN32
const auto external = temporary.path() / "external";
std::filesystem::create_directories(external);
const auto linkedVersion = *Void::Semver::parse("6.0.0");
std::filesystem::create_directory_symlink(external,
store.versionPath(linkedVersion));
Void::Testing::expect(!store.recordHealthy(linkedVersion).has_value());
std::filesystem::create_directory_symlink(
std::filesystem::path{"versions"} / version1.to_string(),
store.root() / "current");
const auto removed = store.prune(1);
Void::Testing::require(removed.has_value());
Void::Testing::expect_eq(removed->size(), std::size_t{2});
Void::Testing::expect(
!std::filesystem::exists(store.versionPath(version2)));
Void::Testing::expect(
!std::filesystem::exists(store.versionPath(version4)));
Void::Testing::expect(
std::filesystem::is_directory(store.versionPath(version1)));
Void::Testing::expect(
std::filesystem::is_directory(store.versionPath(version3)));
Void::Testing::expect(
std::filesystem::is_directory(store.versionPath(version5)));
Void::Testing::expect(std::filesystem::is_directory(external));
Void::Testing::expect(
std::filesystem::is_symlink(store.versionPath(linkedVersion)));
Void::Testing::expect(!std::filesystem::exists(store.root() / "staging"));
#else
(void)version5;
#endif
}
Void::Testing::Test version_store_prunes_without_an_active_version()
{
Void::FileSystem::TempDir temporary{"void_versions_no_current_"};
Void::Update::VersionStore store{temporary.path() / "store"};
std::filesystem::create_directories(store.root() / "versions");
for (const std::string_view text : {"1.0.0", "2.0.0", "3.0.0", "4.0.0"}) {
std::filesystem::create_directories(
store.versionPath(*Void::Semver::parse(text)));
}
const auto version1 = *Void::Semver::parse("1.0.0");
const auto version2 = *Void::Semver::parse("2.0.0");
const auto version3 = *Void::Semver::parse("3.0.0");
const auto version4 = *Void::Semver::parse("4.0.0");
Void::Testing::require(store.recordHealthy(version1).has_value());
Void::Testing::require(store.recordHealthy(version2).has_value());
Void::Testing::require(store.recordFailed(version3).has_value());
auto removed = store.prune(1);
Void::Testing::require(removed.has_value());
Void::Testing::expect_eq(removed->size(), std::size_t{2});
Void::Testing::expect(
!std::filesystem::exists(store.versionPath(version1)));
Void::Testing::expect(
std::filesystem::is_directory(store.versionPath(version2)));
Void::Testing::expect(
!std::filesystem::exists(store.versionPath(version3)));
Void::Testing::expect(
std::filesystem::is_directory(store.versionPath(version4)));
// A retired SemVer may be republished by an external lifecycle owner. Its
// earlier healthy event belongs to the retired publication, so a failure in
// the new publication must still be recorded and prunable.
std::filesystem::create_directories(store.versionPath(version1));
Void::Testing::require(store.recordFailed(version1).has_value());
std::ifstream healthInput{store.root() / "health.log"};
const std::string healthHistory{std::istreambuf_iterator<char>{healthInput},
std::istreambuf_iterator<char>{}};
const auto retired = healthHistory.find("D 1.0.0\n");
Void::Testing::require(retired != std::string::npos);
Void::Testing::expect(healthHistory.find("F 1.0.0\n", retired) !=
std::string::npos);
removed = store.prune(1);
Void::Testing::require(removed.has_value());
Void::Testing::expect_eq(removed->size(), std::size_t{1});
Void::Testing::expect(
!std::filesystem::exists(store.versionPath(version1)));
}
Void::Testing::Test version_store_reports_healthy_publications_in_event_order()
{
Void::FileSystem::TempDir temporary{"void_versions_healthy_query_"};
Void::Update::VersionStore store{temporary.path() / "store"};
std::filesystem::create_directories(store.root() / "versions");
const auto version1 = *Void::Semver::parse("1.0.0");
const auto version2 = *Void::Semver::parse("2.0.0");
const auto version3 = *Void::Semver::parse("3.0.0");
const auto version4 = *Void::Semver::parse("4.0.0");
const auto version5 = *Void::Semver::parse("5.0.0");
for (const auto& version :
{version1, version2, version3, version4, version5}) {
std::filesystem::create_directories(store.versionPath(version));
}
Void::Testing::require(store.recordHealthy(version1).has_value());
Void::Testing::require(store.recordHealthy(version2).has_value());
Void::Testing::require(store.recordHealthy(version3).has_value());
Void::Testing::require(store.recordHealthy(version1).has_value());
Void::Testing::require(store.recordFailed(version4).has_value());
#ifndef _WIN32
const auto readable = std::filesystem::perms::owner_read |
std::filesystem::perms::group_read |
std::filesystem::perms::others_read;
Void::Testing::expect(
(std::filesystem::status(store.root() / ".update.lock").permissions() &
readable) == readable);
Void::Testing::expect(
(std::filesystem::status(store.root() / "health.log").permissions() &
readable) == readable);
#endif
auto healthy = store.healthyPublishedVersions();
Void::Testing::require(healthy.has_value());
Void::Testing::expect_eq(healthy->size(), std::size_t{3});
Void::Testing::expect_eq((*healthy)[0].to_string(), std::string{"1.0.0"});
Void::Testing::expect_eq((*healthy)[1].to_string(), std::string{"3.0.0"});
Void::Testing::expect_eq((*healthy)[2].to_string(), std::string{"2.0.0"});
// Pruning retires an older healthy lifecycle and excludes failed and
// retired publications. Unknown publications remain on disk but are not
// reported as rollback candidates.
Void::Testing::require(store.prune(2).has_value());
healthy = store.healthyPublishedVersions();
Void::Testing::require(healthy.has_value());
Void::Testing::expect_eq(healthy->size(), std::size_t{2});
Void::Testing::expect_eq((*healthy)[0].to_string(), std::string{"1.0.0"});
Void::Testing::expect_eq((*healthy)[1].to_string(), std::string{"3.0.0"});
Void::Testing::expect(
std::filesystem::is_directory(store.versionPath(version5)));
std::filesystem::create_directories(store.versionPath(version2));
Void::Testing::require(store.recordFailed(version2).has_value());
healthy = store.healthyPublishedVersions();
Void::Testing::require(healthy.has_value());
Void::Testing::expect_eq(healthy->size(), std::size_t{2});
Void::Testing::require(store.recordHealthy(version2).has_value());
healthy = store.healthyPublishedVersions();
Void::Testing::require(healthy.has_value());
Void::Testing::expect_eq(healthy->size(), std::size_t{3});
Void::Testing::expect_eq((*healthy)[0].to_string(), std::string{"2.0.0"});
std::filesystem::remove_all(store.versionPath(version1));
healthy = store.healthyPublishedVersions();
Void::Testing::require(healthy.has_value());
Void::Testing::expect_eq(healthy->size(), std::size_t{2});
Void::Testing::expect_eq((*healthy)[0].to_string(), std::string{"2.0.0"});
Void::Testing::expect_eq((*healthy)[1].to_string(), std::string{"3.0.0"});
}
Void::Testing::Test version_store_prune_never_synthesizes_health()
{
#ifndef _WIN32
Void::FileSystem::TempDir temporary{"void_versions_prune_health_"};
Void::Update::VersionStore store{temporary.path() / "store"};
std::filesystem::create_directories(store.root() / "versions");
const auto failed = *Void::Semver::parse("1.0.0");
const auto healthy = *Void::Semver::parse("2.0.0");
std::filesystem::create_directories(store.versionPath(failed));
std::filesystem::create_directories(store.versionPath(healthy));
Void::Testing::require(store.recordFailed(failed).has_value());
Void::Testing::require(store.recordHealthy(healthy).has_value());
std::filesystem::create_directory_symlink(
std::filesystem::path{"versions"} / failed.to_string(),
store.root() / "current");
Void::Testing::require(store.prune(1).has_value());
std::ifstream healthInput{store.root() / "health.log"};
const std::string healthHistory{std::istreambuf_iterator<char>{healthInput},
std::istreambuf_iterator<char>{}};
Void::Testing::expect(healthHistory.find("H 1.0.0\n") == std::string::npos);
std::filesystem::remove(store.root() / "current");
std::filesystem::create_directory_symlink(
std::filesystem::path{"versions"} / healthy.to_string(),
store.root() / "current");
const auto removed = store.prune(0);
Void::Testing::require(removed.has_value());
Void::Testing::expect_eq(removed->size(), std::size_t{1});
Void::Testing::expect(!std::filesystem::exists(store.versionPath(failed)));
#endif
}
Void::Testing::Test version_store_compacts_health_history_atomically()
{
Void::FileSystem::TempDir temporary{"void_versions_health_compaction_"};
Void::Update::VersionStore store{temporary.path() / "store"};
std::filesystem::create_directories(store.root() / "versions");
const auto version1 = *Void::Semver::parse("1.0.0");
const auto version2 = *Void::Semver::parse("2.0.0");
std::filesystem::create_directories(store.versionPath(version1));
std::filesystem::create_directories(store.versionPath(version2));
{
std::ofstream history{store.root() / "health.log",
std::ios::binary | std::ios::trunc};
for (std::size_t index = 0; index < 100'000; ++index) {
history << "H 1.0.0\n";
}
}
Void::Testing::require(store.recordHealthy(version2).has_value());
Void::Testing::expect(
std::filesystem::file_size(store.root() / "health.log") < 1024);
const auto removed = store.prune(1);
Void::Testing::require(removed.has_value());
Void::Testing::expect_eq(removed->size(), std::size_t{1});
Void::Testing::expect_eq(removed->front().to_string(),
std::string{"1.0.0"});
Void::Testing::expect(
std::filesystem::is_directory(store.versionPath(version2)));
}
Void::Testing::Test version_store_rejects_unsafe_activation_journal()
{
#ifndef _WIN32
Void::FileSystem::TempDir temporary{"void_versions_journal_safety_"};
Void::Update::VersionStore store{temporary.path() / "store"};
Void::Testing::require(store.initialize().has_value());
const auto external = temporary.path() / "external-activation.json";
std::ofstream{external}
<< R"({"schemaVersion":1,"previousVersion":null,"candidateVersion":"1.0.0"})";
std::filesystem::create_symlink(external, store.root() / "activation.json");
Void::Testing::expect(!store.pendingActivation().has_value());
Void::Testing::expect(std::filesystem::is_regular_file(external));
std::filesystem::remove(store.root() / "activation.json");
{
std::ofstream oversized{store.root() / "activation.json",
std::ios::binary | std::ios::trunc};
oversized.seekp(128 * 1024);
oversized.put('\0');
}
Void::Testing::expect(!store.pendingActivation().has_value());
std::filesystem::remove(store.root() / "activation.json");
std::ofstream{store.root() / "activation.json"}
<< R"({"schemaVersion":1,"previousVersion":null,"candidateVersion":"1.0.0"})";
Void::Testing::expect(!store.pendingActivation().has_value());
#endif
}
Void::Testing::Test version_store_prunes_only_unprotected_real_versions()
{
Void::FileSystem::TempDir temporary{"void_versions_prune_"};
Void::Update::VersionStore store{temporary.path() / "store"};
Void::Testing::require(store.initialize().has_value());
const std::array versionTexts{"1.0.0", "2.0.0", "3.0.0", "4.0.0"};
for (const std::string_view text : versionTexts) {
const auto version = *Void::Semver::parse(text);
auto staging = store.prepare(version);
Void::Testing::require(staging.has_value());
std::ofstream{*staging / "marker"} << text;
Void::Testing::require(store.publish(version, *staging).has_value());
Void::Testing::require(store.activate(version).has_value());
Void::Testing::require(store.commit().has_value());
}
const auto active = *Void::Semver::parse("4.0.0");
const auto staged = store.prepare(*Void::Semver::parse("8.0.0"));
Void::Testing::require(staged.has_value());
std::ofstream{*staged / "keep"} << "staging";
const auto uncommitted = *Void::Semver::parse("7.0.0");
auto uncommittedStaging = store.prepare(uncommitted);
Void::Testing::require(uncommittedStaging.has_value());
std::ofstream{*uncommittedStaging / "marker"} << "uncommitted";
Void::Testing::require(
store.publish(uncommitted, *uncommittedStaging).has_value());
#ifndef _WIN32
const auto external = temporary.path() / "external";
std::filesystem::create_directories(external);
std::ofstream{external / "keep"} << "outside";
std::filesystem::create_directory_symlink(
external, store.versionPath(*Void::Semver::parse("1.0.0")) / "linked");
std::filesystem::create_directory_symlink(
external, store.versionPath(*Void::Semver::parse("9.0.0")));
const auto orphanedQuarantine = store.root() / "versions" / ".prune.orphan";
std::filesystem::create_directories(orphanedQuarantine);
std::filesystem::create_directory_symlink(external,
orphanedQuarantine / "linked");
#endif
auto removed = store.prune(2);
Void::Testing::require(removed.has_value());
Void::Testing::expect_eq(removed->size(), std::size_t{1});
Void::Testing::expect_eq(removed->front().to_string(),
std::string{"1.0.0"});
Void::Testing::expect(!std::filesystem::exists(
store.versionPath(*Void::Semver::parse("1.0.0"))));
for (const std::string_view text : {"2.0.0", "3.0.0", "4.0.0"}) {
Void::Testing::expect(std::filesystem::is_directory(
store.versionPath(*Void::Semver::parse(text))));
}
Void::Testing::expect(std::filesystem::exists(*staged / "keep"));
Void::Testing::expect(
std::filesystem::is_directory(store.versionPath(uncommitted)));
#ifndef _WIN32
Void::Testing::expect(std::filesystem::exists(external / "keep"));
Void::Testing::expect(!std::filesystem::exists(orphanedQuarantine));
Void::Testing::expect(std::filesystem::is_symlink(
store.versionPath(*Void::Semver::parse("9.0.0"))));
#endif
const auto candidate = *Void::Semver::parse("5.0.0");
auto candidateStaging = store.prepare(candidate);
Void::Testing::require(candidateStaging.has_value());
std::ofstream{*candidateStaging / "marker"} << "candidate";
Void::Testing::require(
store.publish(candidate, *candidateStaging).has_value());
Void::Testing::require(store.activate(candidate).has_value());
removed = store.prune(0);
Void::Testing::require(removed.has_value());
Void::Testing::expect_eq(removed->size(), std::size_t{2});
Void::Testing::expect(
std::filesystem::is_directory(store.versionPath(candidate)));
Void::Testing::expect(
std::filesystem::is_directory(store.versionPath(active)));
Void::Testing::expect(std::filesystem::exists(*staged / "keep"));
Void::Testing::expect(
std::filesystem::is_directory(store.versionPath(uncommitted)));
Void::Testing::require(store.rollback().has_value());
const auto current = store.currentVersion();
Void::Testing::require(current.has_value() && current->has_value());
Void::Testing::expect_eq((*current)->to_string(), std::string{"4.0.0"});
removed = store.prune(0);
Void::Testing::require(removed.has_value());
Void::Testing::expect_eq(removed->size(), std::size_t{1});
Void::Testing::expect_eq(removed->front().to_string(),
std::string{"5.0.0"});
Void::Testing::expect(
!std::filesystem::exists(store.versionPath(candidate)));
Void::Testing::expect(
std::filesystem::is_directory(store.versionPath(uncommitted)));
}
Void::Testing::Test version_store_rejects_symlink_layout()
{
#ifndef _WIN32
Void::FileSystem::TempDir temporary{"void_versions_links_"};
const auto target = temporary.path() / "target";
std::filesystem::create_directories(target);
const auto linkedRoot = temporary.path() / "linked-store";
std::filesystem::create_directory_symlink(target, linkedRoot);
Void::Update::VersionStore rootStore{linkedRoot};
Void::Testing::expect(!rootStore.initialize().has_value());
const auto versionsRoot = temporary.path() / "versions-store";
std::filesystem::create_directories(versionsRoot / "staging");
std::filesystem::create_directory_symlink(target,
versionsRoot / "versions");
Void::Update::VersionStore versionsStore{versionsRoot};
Void::Testing::expect(!versionsStore.initialize().has_value());
const auto stagingRoot = temporary.path() / "staging-store";
std::filesystem::create_directories(stagingRoot / "versions");
std::filesystem::create_directory_symlink(target, stagingRoot / "staging");
Void::Update::VersionStore stagingStore{stagingRoot};
Void::Testing::expect(!stagingStore.initialize().has_value());
#endif
}
Void::Testing::Test version_store_publish_requires_immediate_real_child()
{
Void::FileSystem::TempDir temporary{"void_versions_publish_"};
Void::Update::VersionStore store{temporary.path() / "store"};
Void::Testing::require(store.initialize().has_value());
const auto nested = store.root() / "staging" / "nested" / "candidate";
std::filesystem::create_directories(nested);
const auto version1 = *Void::Semver::parse("3.0.0");
Void::Testing::expect(!store.publish(version1, nested).has_value());
#ifndef _WIN32
const auto external = temporary.path() / "external-candidate";
std::filesystem::create_directories(external);
const auto linked = store.root() / "staging" / "linked-candidate";
std::filesystem::create_directory_symlink(external, linked);
const auto version2 = *Void::Semver::parse("3.1.0");
Void::Testing::expect(!store.publish(version2, linked).has_value());
#endif
}
Void::Testing::Test version_store_activate_rejects_symlink_version()
{
#ifndef _WIN32
Void::FileSystem::TempDir temporary{"void_versions_activate_"};
Void::Update::VersionStore store{temporary.path() / "store"};
Void::Testing::require(store.initialize().has_value());
const auto external = temporary.path() / "external-version";
std::filesystem::create_directories(external);
const auto version = *Void::Semver::parse("4.0.0");
std::filesystem::create_directory_symlink(external,
store.versionPath(version));
Void::Testing::expect(!store.activate(version).has_value());
Void::Testing::expect(
!std::filesystem::exists(store.root() / "activation.json"));
#endif
}
Void::Testing::Test version_store_rollback_revalidates_previous_version()
{
#ifndef _WIN32
Void::FileSystem::TempDir temporary{"void_versions_rollback_links_"};
Void::Update::VersionStore store{temporary.path() / "store"};
Void::Testing::require(store.initialize().has_value());
const auto version1 = *Void::Semver::parse("1.0.0");
const auto version2 = *Void::Semver::parse("2.0.0");
for (const auto& version : {version1, version2}) {
auto staging = store.prepare(version);
Void::Testing::require(staging.has_value());
std::ofstream{*staging / "marker"} << version.to_string();
Void::Testing::require(store.publish(version, *staging).has_value());
}
Void::Testing::require(store.activate(version1).has_value());
Void::Testing::require(store.commit().has_value());
Void::Testing::require(store.activate(version2).has_value());
const auto external = temporary.path() / "external-previous";
std::filesystem::create_directories(external);
std::filesystem::remove_all(store.versionPath(version1));
std::filesystem::create_directory_symlink(external,
store.versionPath(version1));
Void::Testing::expect(!store.rollback().has_value());
Void::Testing::expect(
std::filesystem::exists(store.root() / "activation.json"));
const auto current = store.currentVersion();
Void::Testing::require(current.has_value() && current->has_value());
Void::Testing::expect_eq((*current)->to_string(), std::string{"2.0.0"});
Void::Testing::expect(std::filesystem::is_directory(external));
#endif
}