110 lines
3.8 KiB
C++
110 lines
3.8 KiB
C++
#include <Void/ShortUrlStorage/StorageRepository.hpp>
|
|
#include <Void/Storage/Engine.hpp>
|
|
#include <Void/System/TempDir.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
#include <Void/Time/Clock.hpp>
|
|
|
|
#include <chrono>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
Void::Testing::Test shorturl_storage_durable_persistence_tombstones_and_quota()
|
|
{
|
|
using namespace Void::ShortUrl::V2;
|
|
|
|
Void::FileSystem::TempDir tmp{"void_shorturl_storage_"};
|
|
Void::Time::ManualCivilClock clock{
|
|
Void::Time::CivilClock::time_point{std::chrono::seconds{1'700'000'000}}};
|
|
|
|
std::string generated_key;
|
|
{
|
|
Void::Storage::OpenOptions sopt;
|
|
sopt.directory = tmp.path() / "db";
|
|
auto eng = Void::Storage::Engine::open(sopt);
|
|
Void::Testing::require(eng.has_value());
|
|
auto shared_eng =
|
|
std::make_shared<Void::Storage::Engine>(std::move(*eng));
|
|
|
|
auto repo = std::make_shared<StorageLinkRepository>(shared_eng);
|
|
Void::Testing::expect(repo->is_durable());
|
|
auto rng = std::make_shared<SecurityRandomBytesPort>();
|
|
|
|
LocalProvider::Options lopt;
|
|
lopt.public_base_url = "https://sho.rt";
|
|
LocalProvider provider{repo, lopt, clock, rng};
|
|
|
|
auto a = provider.shorten({.target_url = "https://example.com/durable"},
|
|
ShortenOptions{.operation_id = "dur-op-1"});
|
|
Void::Testing::require(a.has_value());
|
|
generated_key = a->key;
|
|
Void::Testing::expect_eq(generated_key.size(), std::size_t{16});
|
|
|
|
auto b = provider.shorten({
|
|
.target_url = "https://example.com/killme",
|
|
.custom_key = "revokeme",
|
|
});
|
|
Void::Testing::require(b.has_value());
|
|
Void::Testing::require(provider.revoke("revokeme").has_value());
|
|
Void::Testing::require(shared_eng->close().has_value());
|
|
}
|
|
|
|
// Reopen Storage::Engine from disk and verify durable link, idempotency
|
|
// replay, and persisted tombstone non-reuse.
|
|
{
|
|
Void::Storage::OpenOptions sopt;
|
|
sopt.directory = tmp.path() / "db";
|
|
auto eng = Void::Storage::Engine::open(sopt);
|
|
Void::Testing::require(eng.has_value());
|
|
auto shared_eng =
|
|
std::make_shared<Void::Storage::Engine>(std::move(*eng));
|
|
|
|
// Tight disk quota test on separate small repo instance check below,
|
|
// first verify normal recovery.
|
|
auto repo = std::make_shared<StorageLinkRepository>(shared_eng);
|
|
LocalProvider::Options lopt;
|
|
lopt.public_base_url = "https://sho.rt";
|
|
LocalProvider provider{repo, lopt, clock,
|
|
std::make_shared<SecurityRandomBytesPort>()};
|
|
|
|
auto res = provider.resolve(generated_key);
|
|
Void::Testing::require(res.has_value());
|
|
Void::Testing::expect_eq(res->target_url,
|
|
std::string{"https://example.com/durable"});
|
|
|
|
// V2 idempotency replay survives restart!
|
|
auto replay = provider.shorten(
|
|
{.target_url = "https://example.com/durable"},
|
|
ShortenOptions{.operation_id = "dur-op-1"});
|
|
Void::Testing::require(replay.has_value());
|
|
Void::Testing::expect_eq(replay->key, generated_key);
|
|
|
|
// Tombstone survives restart and blocks reuse!
|
|
auto rev = provider.resolve("revokeme");
|
|
Void::Testing::expect(!rev.has_value());
|
|
Void::Testing::expect(v2_error_code_from(rev.error()) ==
|
|
V2ErrorCode::Revoked);
|
|
auto reuse = provider.shorten({
|
|
.target_url = "https://example.com/new",
|
|
.custom_key = "revokeme",
|
|
});
|
|
Void::Testing::expect(!reuse.has_value());
|
|
Void::Testing::expect(v2_error_code_from(reuse.error()) ==
|
|
V2ErrorCode::Conflict);
|
|
|
|
// Quota enforcement rejects insert once quota is exhausted.
|
|
auto tiny_repo =
|
|
std::make_shared<StorageLinkRepository>(shared_eng, 32ULL);
|
|
LocalProvider tight_provider{
|
|
tiny_repo, lopt, clock,
|
|
std::make_shared<SecurityRandomBytesPort>()};
|
|
auto over = tight_provider.shorten(
|
|
{.target_url = "https://example.com/too-big",
|
|
.custom_key = "quota1"});
|
|
Void::Testing::expect(!over.has_value());
|
|
Void::Testing::expect(v2_error_code_from(over.error()) ==
|
|
V2ErrorCode::QuotaExceeded);
|
|
Void::Testing::require(shared_eng->close().has_value());
|
|
}
|
|
}
|