// Hostname resolution: which surface a `Host` names and whose project it is. // // This is a TENANCY BOUNDARY, so almost every test below is about what must NOT // resolve. A test suite for this that only checked the happy path would pass // just as well against a registry that answered "the first project it could // find" for every unknown name - which is the single failure that matters. #include #include "hostnames.hpp" using svapi::HostBinding; using svapi::HostEntry; using svapi::HostKind; using svapi::HostRegistry; using svapi::normaliseHost; using svapi::PathStyle; using svapi::validHostname; namespace { HostRegistry twoTenants(std::vector* dropped = nullptr) { return HostRegistry::build( "admin.smartbotics.ai", {"global-api-01.smartbotics.ai"}, {{"novitax", {{"nt.kb.smartbotics.ai", PathStyle::Short}}}, {"justice-pa", {{"pa.smartbotics.ai", PathStyle::Full}, {"assistant.smartbotics.ai", PathStyle::Short}}}}, dropped); } } // namespace // --- normalisation: every arm is a spelling a caller controls ---------------- TEST(NormaliseHost, LowercasesBecauseDnsIsCaseInsensitive) { EXPECT_EQ(normaliseHost("NT.KB.Smartbotics.AI"), "nt.kb.smartbotics.ai"); } TEST(NormaliseHost, StripsThePort) { EXPECT_EQ(normaliseHost("example.com:8443"), "example.com"); EXPECT_EQ(normaliseHost("example.com:443"), "example.com"); } TEST(NormaliseHost, AnIPv6LiteralKeepsItsAddressAndLosesOnlyThePort) { // The trap: truncating at the FIRST colon turns `[::1]:8080` into `[`, // which then matches nothing - or worse, matches a registry entry somebody // created by pasting a bracket. EXPECT_EQ(normaliseHost("[::1]:8080"), "[::1]"); EXPECT_EQ(normaliseHost("[2001:db8::5]"), "[2001:db8::5]"); EXPECT_EQ(normaliseHost("[2001:DB8::5]:443"), "[2001:db8::5]"); } TEST(NormaliseHost, StripsExactlyOneTrailingDot) { // `example.com.` is the fully-qualified spelling and resolves identically, // so a client using it must not miss. EXPECT_EQ(normaliseHost("example.com."), "example.com"); // But only one: `example.com..` is not a name anybody registered, and // normalising it would invent a match. EXPECT_EQ(normaliseHost("example.com.."), "example.com."); } TEST(NormaliseHost, TrimsTheOptionalWhitespaceAHeaderMayCarry) { EXPECT_EQ(normaliseHost(" example.com\t"), "example.com"); } // --- what a project may register -------------------------------------------- TEST(ValidHostname, RefusesAWildcardOutright) { // THE one that matters. `*.smartbotics.ai` would catch every subdomain // nobody had claimed YET, so a tenancy boundary would move whenever an // unrelated DNS record appeared. Wildcards belong in certificate selection, // where the wrong match fails a handshake rather than serving data. EXPECT_FALSE(validHostname("*.smartbotics.ai")); EXPECT_FALSE(validHostname("*")); EXPECT_FALSE(validHostname("nt.*.smartbotics.ai")); } TEST(ValidHostname, RefusesAnythingThatIsNotJustAName) { EXPECT_FALSE(validHostname("https://nt.kb.smartbotics.ai")); EXPECT_FALSE(validHostname("nt.kb.smartbotics.ai/admin")); EXPECT_FALSE(validHostname("nt.kb.smartbotics.ai:8443")); EXPECT_FALSE(validHostname("user@nt.kb.smartbotics.ai")); EXPECT_FALSE(validHostname("nt kb.smartbotics.ai")); } TEST(ValidHostname, RefusesASingleLabelSoAnIntranetAliasCannotBecomeATenant) { EXPECT_FALSE(validHostname("localhost")); EXPECT_FALSE(validHostname("api")); EXPECT_TRUE(validHostname("api.example.com")); } TEST(ValidHostname, RefusesMalformedLabels) { EXPECT_FALSE(validHostname("")); EXPECT_FALSE(validHostname(".example.com")); EXPECT_FALSE(validHostname("example.com.")); // normalise first, then validate EXPECT_FALSE(validHostname("a..b.com")); EXPECT_FALSE(validHostname("-bad.example.com")); EXPECT_FALSE(validHostname("bad-.example.com")); EXPECT_FALSE(validHostname(std::string(254, 'a') + ".com")); } // --- resolution ------------------------------------------------------------- TEST(HostRegistry, ResolvesEachKind) { const HostRegistry r = twoTenants(); EXPECT_EQ(r.resolve("admin.smartbotics.ai").kind, HostKind::Admin); EXPECT_EQ(r.resolve("global-api-01.smartbotics.ai").kind, HostKind::Global); const HostBinding nt = r.resolve("nt.kb.smartbotics.ai"); EXPECT_EQ(nt.kind, HostKind::Project); EXPECT_EQ(nt.project, "novitax"); EXPECT_EQ(nt.pathStyle, PathStyle::Short) << "short is the default shape"; const HostBinding pa = r.resolve("pa.smartbotics.ai"); EXPECT_EQ(pa.project, "justice-pa"); EXPECT_EQ(pa.pathStyle, PathStyle::Full) << "the per-hostname setting is honoured"; } TEST(HostRegistry, OneProjectMayHoldSeveralHostnames) { const HostRegistry r = twoTenants(); EXPECT_EQ(r.resolve("pa.smartbotics.ai").project, "justice-pa"); EXPECT_EQ(r.resolve("assistant.smartbotics.ai").project, "justice-pa"); } TEST(HostRegistry, AnUnknownHostIsUNKNOWNAndNEVERAProject) { // The failure this file exists to prevent. Note the second and third cases: // a SUFFIX of a registered name and a PARENT of one must both miss, because // a closest-parent match would hand every unclaimed subdomain to whoever // owns something above it. const HostRegistry r = twoTenants(); for (const char* h : {"nobody.example.com", "kb.smartbotics.ai", "smartbotics.ai", "evil.nt.kb.smartbotics.ai", "nt.kb.smartbotics.ai.evil.com", ""}) { const HostBinding b = r.resolve(h); EXPECT_EQ(b.kind, HostKind::Unknown) << h; EXPECT_TRUE(b.project.empty()) << h << " resolved to project '" << b.project << "'"; } } TEST(HostRegistry, ResolutionIsCaseAndPortInsensitive) { const HostRegistry r = twoTenants(); EXPECT_EQ(r.resolve("NT.KB.SMARTBOTICS.AI:8443").project, "novitax"); EXPECT_EQ(r.resolve("nt.kb.smartbotics.ai.").project, "novitax"); } // --- collisions: the two directions, and they resolve differently ----------- TEST(HostRegistry, ADeploymentNameCANNOTBeTakenByAProject) { // A project claiming the admin hostname would hand it the web UI and the // docs catalogue. The deployment keeps its name; the claim is dropped. std::vector dropped; const HostRegistry r = HostRegistry::build( "admin.smartbotics.ai", {"global.smartbotics.ai"}, {{"evil", {{"admin.smartbotics.ai", PathStyle::Short}, {"global.smartbotics.ai", PathStyle::Short}}}}, &dropped); EXPECT_EQ(r.resolve("admin.smartbotics.ai").kind, HostKind::Admin); EXPECT_EQ(r.resolve("global.smartbotics.ai").kind, HostKind::Global); EXPECT_TRUE(r.resolve("admin.smartbotics.ai").project.empty()); EXPECT_EQ(dropped.size(), 2u) << "both claims reported, so the operator can see them"; } TEST(HostRegistry, AHostClaimedByTWOProjectsGoesToNEITHER) { // Last-writer-wins would move a tenancy boundary silently. Refusing both // makes it a 404 on a name nobody can reach - visible, and recoverable. std::vector dropped; const HostRegistry r = HostRegistry::build( "admin.example.com", {}, {{"alpha", {{"shared.example.com", PathStyle::Short}}}, {"beta", {{"shared.example.com", PathStyle::Short}}}}, &dropped); const HostBinding b = r.resolve("shared.example.com"); EXPECT_EQ(b.kind, HostKind::Unknown); EXPECT_TRUE(b.project.empty()) << "contested host served '" << b.project << "'"; EXPECT_EQ(dropped.size(), 1u); } TEST(HostRegistry, OneProjectListingTheSameHostTwiceIsHarmless) { const HostRegistry r = HostRegistry::build( "admin.example.com", {}, {{"alpha", {{"a.example.com", PathStyle::Short}, {"A.example.com.", PathStyle::Full}}}}, nullptr); // Kept, and the FIRST entry's style wins - stated by a test so a later // change to "last wins" is a deliberate decision rather than a surprise. const HostBinding b = r.resolve("a.example.com"); EXPECT_EQ(b.project, "alpha"); EXPECT_EQ(b.pathStyle, PathStyle::Short); } TEST(HostRegistry, AnInvalidRegisteredHostIsDroppedRatherThanStored) { std::vector dropped; const HostRegistry r = HostRegistry::build( "admin.example.com", {}, {{"alpha", {{"*.example.com", PathStyle::Short}, {"localhost", PathStyle::Short}}}}, &dropped); EXPECT_EQ(r.resolve("*.example.com").kind, HostKind::Unknown); EXPECT_EQ(r.resolve("anything.example.com").kind, HostKind::Unknown) << "a stored wildcard must not have become a live wildcard"; EXPECT_EQ(dropped.size(), 2u); } // --- empty: the ordinary case for a deployment behind someone else's proxy --- TEST(HostRegistry, NoHostnamesConfiguredMeansEmptyNotEverythingRefused) { // The signal to skip host gating entirely. Without this, adding the feature // would 404 every request on every existing deployment - an outage caused // by shipping a control, which this codebase has done once already // (network_egress defaulting to deny would have done exactly that). const HostRegistry r = HostRegistry::build("", {}, {}, nullptr); EXPECT_TRUE(r.empty()); EXPECT_EQ(r.resolve("anything.example.com").kind, HostKind::Unknown); } TEST(HostRegistry, SizeCountsEveryRegisteredName) { const HostRegistry r = twoTenants(); EXPECT_EQ(r.size(), 5u) << "admin + global + 3 project hostnames"; EXPECT_FALSE(r.empty()); }