feat(shorturl): complete P2 M-SHORTURL-01..04 V2 local, storage, and remote providers with frozen V1
This commit is contained in:
1 parent
658c21a2fa
commit
691808eab6
13 files changed
+2317
-1
No files matched your search
@@ -0,0 +1,150 @@
|
||||
#include <Void/ShortUrl/ShortUrl.hpp>
|
||||
#include <Void/ShortUrl/V2.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
// Freeze checks on V1 enum ordinals & version constants.
|
||||
static_assert(Void::ShortUrl::V1::kServiceVersion == 1U);
|
||||
static_assert(static_cast<std::uint32_t>(Void::ShortUrl::V1::ErrorCode::none) == 0U);
|
||||
static_assert(static_cast<std::uint32_t>(Void::ShortUrl::V1::ErrorCode::unsupported) == 2U);
|
||||
static_assert(static_cast<std::uint32_t>(Void::ShortUrl::V1::ErrorCode::revoked) == 13U);
|
||||
static_assert(static_cast<std::uint32_t>(Void::ShortUrl::V1::ErrorCode::response_too_large) == 15U);
|
||||
|
||||
Void::Testing::Test shorturl_v2_local_validation_expiry_revoke_and_idempotency()
|
||||
{
|
||||
using namespace Void::ShortUrl::V2;
|
||||
|
||||
Void::Time::ManualCivilClock clock{
|
||||
Void::Time::CivilClock::time_point{std::chrono::seconds{1'700'000'000}}};
|
||||
auto repo = std::make_shared<InMemoryLinkRepository>();
|
||||
Void::Testing::expect(!repo->is_durable());
|
||||
|
||||
LocalProvider::Options opts;
|
||||
opts.public_base_url = "https://sho.rt/s";
|
||||
opts.limits.max_entries = 10;
|
||||
opts.limits.max_retained_identities = 5;
|
||||
opts.host_policy = [](std::string_view host) {
|
||||
return host != "blocked.internal";
|
||||
};
|
||||
|
||||
auto provider = std::make_shared<LocalProvider>(repo, opts, clock);
|
||||
|
||||
// 1. Validation rejects userinfo, non-http, control chars, denied host,
|
||||
// reserved keys, and past expiry.
|
||||
Void::Testing::expect(
|
||||
!provider->shorten({.target_url = "https://user:pw@example.com/a"})
|
||||
.has_value());
|
||||
Void::Testing::expect(
|
||||
!provider->shorten({.target_url = "ftp://example.com/a"}).has_value());
|
||||
Void::Testing::expect(
|
||||
!provider->shorten({.target_url = "https://blocked.internal/x"})
|
||||
.has_value());
|
||||
Void::Testing::expect(
|
||||
!provider
|
||||
->shorten({.target_url = "https://example.com/a",
|
||||
.custom_key = "health"})
|
||||
.has_value());
|
||||
Void::Testing::expect(
|
||||
!provider
|
||||
->shorten({.target_url = "https://example.com/a",
|
||||
.expires_at_unix_seconds = 1'699'999'999})
|
||||
.has_value());
|
||||
|
||||
// 2. Generated 16-char base62 key + V2 idempotency replay within 24h.
|
||||
auto created = provider->shorten(
|
||||
{.target_url = "https://example.com/page"},
|
||||
ShortenOptions{.operation_id = "op-001"});
|
||||
Void::Testing::require(created.has_value());
|
||||
Void::Testing::expect_eq(created->key.size(), std::size_t{16});
|
||||
Void::Testing::expect_eq(created->short_url,
|
||||
"https://sho.rt/s/" + created->key);
|
||||
|
||||
auto replay = provider->shorten(
|
||||
{.target_url = "https://example.com/page"},
|
||||
ShortenOptions{.operation_id = "op-001"});
|
||||
Void::Testing::require(replay.has_value());
|
||||
Void::Testing::expect_eq(replay->key, created->key);
|
||||
|
||||
// 3. Custom key conflict & tombstone non-reuse after revoke.
|
||||
auto custom = provider->shorten({
|
||||
.target_url = "https://example.com/custom",
|
||||
.custom_key = "my_Link-1",
|
||||
.expires_at_unix_seconds = 1'700'003'600,
|
||||
});
|
||||
Void::Testing::require(custom.has_value());
|
||||
Void::Testing::expect_eq(custom->key, std::string{"my_Link-1"});
|
||||
|
||||
auto conflict = provider->shorten({
|
||||
.target_url = "https://example.com/other",
|
||||
.custom_key = "my_Link-1",
|
||||
});
|
||||
Void::Testing::expect(!conflict.has_value());
|
||||
Void::Testing::expect(v2_error_code_from(conflict.error()) ==
|
||||
V2ErrorCode::Conflict);
|
||||
|
||||
auto resolved = provider->resolve("my_Link-1");
|
||||
Void::Testing::require(resolved.has_value());
|
||||
Void::Testing::expect_eq(resolved->target_url,
|
||||
std::string{"https://example.com/custom"});
|
||||
|
||||
// Revoke creates tombstone; resolve returns Revoked and key cannot be
|
||||
// reused.
|
||||
Void::Testing::require(provider->revoke("my_Link-1").has_value());
|
||||
auto after_revoke = provider->resolve("my_Link-1");
|
||||
Void::Testing::expect(!after_revoke.has_value());
|
||||
Void::Testing::expect(v2_error_code_from(after_revoke.error()) ==
|
||||
V2ErrorCode::Revoked);
|
||||
|
||||
auto reuse = provider->shorten({
|
||||
.target_url = "https://example.com/reused",
|
||||
.custom_key = "my_Link-1",
|
||||
});
|
||||
Void::Testing::expect(!reuse.has_value());
|
||||
Void::Testing::expect(v2_error_code_from(reuse.error()) ==
|
||||
V2ErrorCode::Conflict);
|
||||
|
||||
// 4. Expiry via ManualCivilClock + idempotency retention expiry.
|
||||
auto exp_link = provider->shorten({
|
||||
.target_url = "https://example.com/temp",
|
||||
.custom_key = "temp1",
|
||||
.expires_at_unix_seconds = 1'700'000'100,
|
||||
});
|
||||
Void::Testing::require(exp_link.has_value());
|
||||
Void::Testing::require(clock.advance(std::chrono::seconds{200}).has_value());
|
||||
auto exp_res = provider->resolve("temp1");
|
||||
Void::Testing::expect(!exp_res.has_value());
|
||||
Void::Testing::expect(v2_error_code_from(exp_res.error()) ==
|
||||
V2ErrorCode::Expired);
|
||||
|
||||
Void::Testing::require(clock.advance(std::chrono::hours{25}).has_value());
|
||||
auto expired_idem = provider->shorten(
|
||||
{.target_url = "https://example.com/page"},
|
||||
ShortenOptions{.operation_id = "op-001"});
|
||||
Void::Testing::expect(!expired_idem.has_value());
|
||||
Void::Testing::expect(v2_error_code_from(expired_idem.error()) ==
|
||||
V2ErrorCode::IdentityExpired);
|
||||
|
||||
// 5. V1Adapter bridges V2 provider to frozen V1::IShortener cleanly.
|
||||
V1Adapter v1{provider};
|
||||
Void::Testing::expect_eq(v1.service_name(), Void::ShortUrl::V1::kServiceId);
|
||||
Void::Testing::expect_eq(v1.service_version(), std::uint32_t{1});
|
||||
Void::Testing::expect(v1.supports_resolve());
|
||||
|
||||
auto s_v1 = v1.shorten({.target_url = "https://example.com/v1"});
|
||||
Void::Testing::require(s_v1.ok());
|
||||
auto r_v1 = v1.resolve({.key = s_v1.key});
|
||||
Void::Testing::require(r_v1.ok());
|
||||
Void::Testing::expect_eq(r_v1.target_url,
|
||||
std::string{"https://example.com/v1"});
|
||||
|
||||
auto r_revoked = v1.resolve({.key = "my_Link-1"});
|
||||
Void::Testing::expect(!r_revoked.ok());
|
||||
Void::Testing::expect(r_revoked.error.code ==
|
||||
Void::ShortUrl::V1::ErrorCode::revoked);
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
#include <Void/Data/StrictJson.hpp>
|
||||
#include <Void/Net/HttpServer.hpp>
|
||||
#include <Void/ShortUrlRemote/RemoteProvider.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
Void::Testing::Test shorturl_remote_provider_capabilities_retry_after_and_unknown()
|
||||
{
|
||||
using namespace Void::ShortUrl::V2;
|
||||
|
||||
Void::Net::HttpServer server;
|
||||
std::atomic<int> shorten_calls{0};
|
||||
std::atomic<bool> stall_next{false};
|
||||
std::atomic<bool> release_stall{false};
|
||||
|
||||
Void::Testing::require(
|
||||
server
|
||||
.add_route(
|
||||
Void::Net::Method::Post, "/v2/shorten",
|
||||
[&](const Void::Net::ServerRequest& req)
|
||||
-> Void::Net::RouteResult {
|
||||
const int n =
|
||||
shorten_calls.fetch_add(1, std::memory_order_relaxed);
|
||||
if (stall_next.load(std::memory_order_acquire)) {
|
||||
for (int i = 0; i < 200 &&
|
||||
!release_stall.load(
|
||||
std::memory_order_acquire);
|
||||
++i) {
|
||||
std::this_thread::sleep_for(
|
||||
std::chrono::milliseconds{20});
|
||||
}
|
||||
return Void::Net::ServerResponse{.status = 200,
|
||||
.body = "{}"};
|
||||
}
|
||||
if (n == 1) {
|
||||
return Void::Net::ServerResponse{
|
||||
.status = 429,
|
||||
.headers = {{"Retry-After", "45"}},
|
||||
.body = "slow down"};
|
||||
}
|
||||
auto parsed = Void::Json::Strict::parse(req.body);
|
||||
Void::Testing::require(parsed.has_value());
|
||||
const auto& obj =
|
||||
std::get<Void::Json::Strict::Value::Object>(
|
||||
parsed->value);
|
||||
const std::string target =
|
||||
std::get<std::string>(obj.at("target_url").value);
|
||||
return Void::Net::ServerResponse{
|
||||
.status = 200,
|
||||
.headers = {{"Content-Type", "application/json"}},
|
||||
.body = "{\"key\":\"rem01\",\"short_url\":\"https://"
|
||||
"rem.test/rem01\",\"target_url\":\"" +
|
||||
target + "\"}",
|
||||
};
|
||||
})
|
||||
.has_value());
|
||||
|
||||
Void::Testing::require(
|
||||
server
|
||||
.add_route(
|
||||
Void::Net::Method::Get, "/v2/resolve/:key",
|
||||
[](const Void::Net::ServerRequest& req)
|
||||
-> Void::Net::RouteResult {
|
||||
const auto it = req.parameters.find("key");
|
||||
if (it == req.parameters.end() || it->second != "rem01") {
|
||||
return Void::Net::ServerResponse{.status = 404,
|
||||
.body = "missing"};
|
||||
}
|
||||
return Void::Net::ServerResponse{
|
||||
.status = 200,
|
||||
.headers = {{"Content-Type", "application/json"}},
|
||||
.body =
|
||||
"{\"target_url\":\"https://example.com/remote\","
|
||||
"\"short_url\":\"https://rem.test/rem01\"}",
|
||||
};
|
||||
})
|
||||
.has_value());
|
||||
|
||||
Void::Testing::require(
|
||||
server
|
||||
.add_route(Void::Net::Method::Delete, "/v2/links/:key",
|
||||
[](const Void::Net::ServerRequest&)
|
||||
-> Void::Net::RouteResult {
|
||||
return Void::Net::ServerResponse{.status = 204,
|
||||
.body = ""};
|
||||
})
|
||||
.has_value());
|
||||
|
||||
Void::Testing::require(server.start().has_value());
|
||||
|
||||
// 1. Unsupported capability rejected BEFORE any network call!
|
||||
{
|
||||
RemoteProviderOptions opts;
|
||||
opts.base_url = server.base_url();
|
||||
opts.capabilities.supports_custom_key = false;
|
||||
opts.capabilities.supports_expiry = false;
|
||||
opts.capabilities.supports_resolve = false;
|
||||
RemoteProvider restricted{opts};
|
||||
|
||||
auto no_custom = restricted.shorten({
|
||||
.target_url = "https://example.com/a",
|
||||
.custom_key = "nope",
|
||||
});
|
||||
Void::Testing::expect(!no_custom.has_value());
|
||||
Void::Testing::expect(v2_error_code_from(no_custom.error()) ==
|
||||
V2ErrorCode::Unsupported);
|
||||
Void::Testing::expect_eq(shorten_calls.load(), 0);
|
||||
|
||||
auto no_resolve = restricted.resolve("rem01");
|
||||
Void::Testing::expect(!no_resolve.has_value());
|
||||
Void::Testing::expect(v2_error_code_from(no_resolve.error()) ==
|
||||
V2ErrorCode::Unsupported);
|
||||
}
|
||||
|
||||
// 2. Full round-trip, 429 Retry-After preservation, and timeout ->
|
||||
// UnknownOutcome without blind replay.
|
||||
RemoteProviderOptions opts;
|
||||
opts.base_url = server.base_url();
|
||||
opts.timeout = std::chrono::seconds{1};
|
||||
auto provider = std::make_shared<RemoteProvider>(opts);
|
||||
|
||||
auto ok = provider->shorten({.target_url = "https://example.com/remote"},
|
||||
ShortenOptions{.operation_id = "rem-op-1"});
|
||||
Void::Testing::require(ok.has_value());
|
||||
Void::Testing::expect_eq(ok->key, std::string{"rem01"});
|
||||
|
||||
auto res = provider->resolve("rem01");
|
||||
Void::Testing::require(res.has_value());
|
||||
Void::Testing::expect_eq(res->target_url,
|
||||
std::string{"https://example.com/remote"});
|
||||
|
||||
Void::Testing::require(provider->revoke("rem01").has_value());
|
||||
|
||||
// Second shorten hits 429 with Retry-After: 45.
|
||||
auto rl = provider->shorten({.target_url = "https://example.com/rate"});
|
||||
Void::Testing::expect(!rl.has_value());
|
||||
Void::Testing::expect(v2_error_code_from(rl.error()) ==
|
||||
V2ErrorCode::RateLimited);
|
||||
Void::Testing::expect_eq(provider->last_retry_after_seconds(),
|
||||
std::uint32_t{45});
|
||||
|
||||
// Third shorten stalls until 1s timeout -> UnknownOutcome (and V1Adapter
|
||||
// maps it to provider_error).
|
||||
stall_next.store(true, std::memory_order_release);
|
||||
auto unk = provider->shorten({.target_url = "https://example.com/lost"});
|
||||
Void::Testing::expect(!unk.has_value());
|
||||
Void::Testing::expect(v2_error_code_from(unk.error()) ==
|
||||
V2ErrorCode::UnknownOutcome);
|
||||
|
||||
release_stall.store(true, std::memory_order_release);
|
||||
server.shutdown();
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
#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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user