Files

151 lines
5.6 KiB
C++

#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);
}