mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-09-29 17:38:03 +00:00
* implement resolving 2LD names by labelhash * tiny test addition * simplify language * report expiry and availability correctly * compare block instead of wall clock * simplify language * simplify language * map resolver 410 to NAME NOT_FOUND (a lapsed name is an answer, not a failure) * split error into a machine-readable code and a human message * resolver: reduce comments * resolver: reduce comments, remove REVIEW.html * extend SMP protocol to support name availability queries with accurate and meaningful replies * review fixes * more review fixes * docs shortening and other review fixes * add catch all for furture variants * adversarial review (against simplex-chat) fix * next iteration fixes * adapt house style * eth_call guard and cache constants * revert NAVL command and wrap it all into RSLV * doc fixes * Update src/Simplex/Messaging/Server/Names.hs Co-authored-by: Evgeny <evgeny@poberezkin.com> * Update src/Simplex/Messaging/Protocol.hs Co-authored-by: Evgeny <evgeny@poberezkin.com> * Update src/Simplex/Messaging/Protocol.hs Co-authored-by: Evgeny <evgeny@poberezkin.com> * Update src/Simplex/Messaging/Protocol.hs Co-authored-by: Evgeny <evgeny@poberezkin.com> * Update src/Simplex/Messaging/Protocol.hs Co-authored-by: Evgeny <evgeny@poberezkin.com> * protocol types refactoring * next iteration on types only * rentPrices map * claude answering ep review. to be continued... * separate labelhash and plaintext names cleanly * align implementation with latest type changes to test against client * fix adversarial review findings * trim diff * align resolver with contract changes for names v2 * fix reverse compatibility with .testing mainnet * fix more robustly * NameQuery simplification * revert drive-by refactoring * implement full type change and introduce resolver endpoint versioning * fix stale docs * derive NameRegistration JSON the same way on every build sumTypeJSON switches to the _owsf form on swift builds, but this JSON is the RNAME payload and the resolver's HTTP contract, so a swift client and a Linux relay would disagree on every field. taggedObjectJSON is what it already resolves to everywhere else. The label modifier keeps the reservedReason_ collision escape out of the API, as AgentWorkersDetails does: both arms now say reservedReason. * fix resolver boundary: status before body, cover /v2, drop dead field httpGet read the response body before checking the status, so an oversized error page surfaced as a transient "response too large" instead of the authoritative status. Reverting it to its previous shape restores that and removes the status test both callers had been re-deriving. registration() had no tests at all, though it is the endpoint SMP v22 consumes. RegistrationV2Tests covers the three answer shapes, the error paths and the exact key set of each, which is the wire contract. auctionUntil was always None with no consumer. The spec claimed a reason word travels unchanged; a resolver can only send a word it has, and SNRC's registry records a number. * fix review findings * resolver errors say what went wrong, not "no such name" /v2/resolve answers 200, 400 or 502, and an unregistered name is NRAvailable, so no status means "not registered". Mapping 400/404/410 to NOT_FOUND made a misconfigured relay deny every name, and hid a relay upgraded ahead of its resolver. All three now surface as RESOLVER. rslvNotFound would have gone dead, so it counts what its name says: an availability answer the encoder downgrades for a session below v22. The wire is unchanged. A hashed query the registrar cannot name is refused with 502 rather than answered with a record named "unknown", which the client rejects anyway. NRRUnknown is capped to 32 printable characters again, as the spec says. A registered name that is also reserved no longer offers a date it will never free up on. rentPrices is registrationPrices throughout, and yearPriceUSD is USDCents rather than a bare Int64. * fix regressions found reviewing the last two commits resolveNameMsg read the version off thParams', which on the PFWD path is the proxy's session, not the client's. It takes the version as an argument now, so each call site passes its own — the forwarded one uses fwdVersion. reservedReasonOf matched the reason words before capping, so "internal review" became NRRUnknown "internal", which encodes back as NRRInternal. Capping precedes the match, so what is kept encodes to what it decoded. Four agent tests still pinned NAME NOT_FOUND from a 404 stub, and two spec statements still described the old mapping. A registrar that does not record labels cannot answer a hashed query, which the resolver README now says. * docs sweep * simplify encoding * drop resolver caching (#1866) * rename * remove trailing_ filter * fix resolver v2 for subnames (#1867) * fix resolver v2 for subnames * simplify doc * resolver should report response truth freshness (#1868) * first shot at reporting freshness * fix review findings * renaming --------- Co-authored-by: sh <github.shum@liber.li> Co-authored-by: Evgeny <evgeny@poberezkin.com>
164 lines
7.7 KiB
Haskell
164 lines
7.7 KiB
Haskell
{-# LANGUAGE DataKinds #-}
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
|
{-# LANGUAGE GADTs #-}
|
|
{-# LANGUAGE LambdaCase #-}
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE PatternSynonyms #-}
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
{-# LANGUAGE TypeApplications #-}
|
|
{-# OPTIONS_GHC -fno-warn-ambiguous-fields #-}
|
|
|
|
module AgentTests.ResolveNameTests (resolveNameTests) where
|
|
|
|
import AgentTests.FunctionalAPITests (withAgent)
|
|
import Control.Monad.Except (runExceptT)
|
|
import qualified Data.ByteString.Lazy as LB
|
|
import Data.List (isInfixOf)
|
|
import Network.HTTP.Types (Status, status200, status404, status502)
|
|
import NamesResolverServer (memCfg, memCfg2, memProxyCfg, withNames)
|
|
import qualified NamesResolverServer as NRS
|
|
import SMPAgentClient
|
|
import SMPClient
|
|
import SMPNamesTests (availableBody, registeredBody, testNameRecord)
|
|
import Simplex.Messaging.Agent (resolveSimplexName)
|
|
import Simplex.Messaging.Agent.Client (AgentClient)
|
|
import Simplex.Messaging.Agent.Env.SQLite (InitialAgentServers (..), ServerCfg, ServerRoles (..), presetServerCfg)
|
|
import Simplex.Messaging.Agent.Protocol (AgentErrorType (..))
|
|
import Simplex.Messaging.Client (SMPProxyFallback (..), SMPProxyMode (..), pattern NRMInteractive)
|
|
import Simplex.Messaging.Protocol (SMPServer)
|
|
import qualified Simplex.Messaging.Protocol as SMP
|
|
import Simplex.Messaging.SimplexName (SimplexDomain (..), SimplexTLD (..))
|
|
import Simplex.Messaging.Transport
|
|
import Test.Hspec hiding (fit, it)
|
|
import Util (it)
|
|
|
|
nameSrvCfg :: SMPServer -> ServerCfg 'SMP.PSMP
|
|
nameSrvCfg = presetServerCfg True ServerRoles {storage = True, proxy = False, names = True} (Just 1) . SMP.noAuthSrv
|
|
|
|
proxySrvCfg :: SMPServer -> ServerCfg 'SMP.PSMP
|
|
proxySrvCfg = presetServerCfg True ServerRoles {storage = True, proxy = True, names = False} (Just 1) . SMP.noAuthSrv
|
|
|
|
oneSrv :: ServerCfg 'SMP.PSMP -> InitialAgentServers
|
|
oneSrv cfg_ = (initAgentServersProxy_ SPMNever SPFProhibit) {smp = [(1, [cfg_])]}
|
|
|
|
withDirectResolver :: (Status, LB.ByteString) -> (AgentClient -> IO a) -> IO a
|
|
withDirectResolver (st, body) k =
|
|
NRS.withResolverServer (NRS.resolveResp st body) $ \port _ ->
|
|
withSmpServerConfigOn (transport @TLS) (withNames port memCfg) testPort $ \_ ->
|
|
withAgent 1 agentCfg (oneSrv (nameSrvCfg testSMPServer)) testDB k
|
|
|
|
withProxyAndResolver :: (Status, LB.ByteString) -> (AgentClient -> IO a) -> IO a
|
|
withProxyAndResolver (st, body) k =
|
|
NRS.withResolverServer (NRS.resolveResp st body) $ \port _ ->
|
|
withSmpServerConfigOn (transport @TLS) memProxyCfg testPort $ \_ ->
|
|
withSmpServerConfigOn (transport @TLS) (withNames port memCfg2) testPort2 $ \_ ->
|
|
withAgent 1 agentCfg proxyServers testDB k
|
|
where
|
|
-- only testSMPServer2 (the resolver) has the names role; testSMPServer is the proxy
|
|
proxyServers = (initAgentServersProxy_ SPMAlways SPFProhibit) {smp = [(1, [proxySrvCfg testSMPServer, nameSrvCfg testSMPServer2])]}
|
|
|
|
withNoResolver :: (AgentClient -> IO a) -> IO a
|
|
withNoResolver k =
|
|
withSmpServerConfigOn (transport @TLS) memCfg testPort $ \_ ->
|
|
withAgent 1 agentCfg (oneSrv (nameSrvCfg testSMPServer)) testDB k
|
|
|
|
withNoNameServers :: (AgentClient -> IO a) -> IO a
|
|
withNoNameServers k = withAgent 1 agentCfg (oneSrv (proxySrvCfg testSMPServer)) testDB k
|
|
|
|
resolveNameTests :: Spec
|
|
resolveNameTests = do
|
|
describe "direct path (SPMNever)" $
|
|
it "a resolver error propagates as SMP host (NAME RESOLVER)" testDirectResolverErr
|
|
describe "proxy path (SPMAlways)" $
|
|
it "a resolver error propagates via proxy as SMP <proxyHost> (NAME RESOLVER)" testProxyResolverErr
|
|
describe "TLDTesting path" $
|
|
it "NAME RESOLVER for TLDTesting too" testTestingTldResolverErr
|
|
describe "TLDWeb path" $
|
|
it "NAME RESOLVER for TLDWeb too" testWebTldResolverErr
|
|
describe "no resolver configured" $
|
|
it "answers NAME NO_RESOLVER" testNoResolver
|
|
describe "no names servers (names role off everywhere)" $
|
|
it "fails agent-side with NO_NAME_SERVERS" testNoNameServers
|
|
describe "backing resolver failure" $
|
|
it "surfaces as SMP host (NAME (RESOLVER ..))" testBackendError
|
|
describe "success path" $
|
|
it "returns NameRecord" testDirectSuccess
|
|
describe "name availability" $
|
|
it "an unregistered name answers as available" testAvailSuccess
|
|
|
|
testAvailSuccess :: HasCallStack => IO ()
|
|
testAvailSuccess =
|
|
withDirectResolver (status200, availableBody) $ \c -> do
|
|
r <- runExceptT $ resolveSimplexName c NRMInteractive 1 (SimplexDomain TLDSimplex "alice" [])
|
|
case r of
|
|
Right SMP.NameResponse {registration = SMP.NRAvailable {}} -> pure ()
|
|
_ -> expectationFailure $ "expected Right NRAvailable, got: " <> show r
|
|
|
|
-- | 404 is a resolver that predates /v2/resolve: no status from that endpoint
|
|
-- means "not registered", since an unregistered name answers NRAvailable.
|
|
testDirectResolverErr :: HasCallStack => IO ()
|
|
testDirectResolverErr =
|
|
withDirectResolver (status404, "{}") $ \c -> do
|
|
r <- runExceptT $ resolveSimplexName c NRMInteractive 1 (SimplexDomain TLDSimplex "alice" [])
|
|
case r of
|
|
Left (SMP _ (SMP.NAME (SMP.RESOLVER _))) -> pure ()
|
|
_ -> expectationFailure $ "expected Left (SMP _ (NAME (RESOLVER _))), got: " <> show r
|
|
|
|
testProxyResolverErr :: HasCallStack => IO ()
|
|
testProxyResolverErr =
|
|
withProxyAndResolver (status404, "{}") $ \c -> do
|
|
r <- runExceptT $ resolveSimplexName c NRMInteractive 1 (SimplexDomain TLDSimplex "alice" [])
|
|
case r of
|
|
Left (SMP host (SMP.NAME (SMP.RESOLVER _))) | testPort `isInfixOf` host -> pure ()
|
|
_ -> expectationFailure $ "expected Left (SMP <proxyHost:" <> testPort <> "> (NAME (RESOLVER _))), got: " <> show r
|
|
|
|
testTestingTldResolverErr :: HasCallStack => IO ()
|
|
testTestingTldResolverErr =
|
|
withDirectResolver (status404, "{}") $ \c -> do
|
|
r <- runExceptT $ resolveSimplexName c NRMInteractive 1 (SimplexDomain TLDTesting "bob" [])
|
|
case r of
|
|
Left (SMP _ (SMP.NAME (SMP.RESOLVER _))) -> pure ()
|
|
_ -> expectationFailure $ "expected Left (SMP _ (NAME (RESOLVER _))), got: " <> show r
|
|
|
|
testWebTldResolverErr :: HasCallStack => IO ()
|
|
testWebTldResolverErr =
|
|
withDirectResolver (status404, "{}") $ \c -> do
|
|
r <- runExceptT $ resolveSimplexName c NRMInteractive 1 (SimplexDomain TLDWeb "example.com" [])
|
|
case r of
|
|
Left (SMP _ (SMP.NAME (SMP.RESOLVER _))) -> pure ()
|
|
_ -> expectationFailure $ "expected Left (SMP _ (NAME (RESOLVER _))), got: " <> show r
|
|
|
|
testNoResolver :: HasCallStack => IO ()
|
|
testNoResolver =
|
|
withNoResolver $ \c -> do
|
|
r <- runExceptT $ resolveSimplexName c NRMInteractive 1 (SimplexDomain TLDSimplex "alice" [])
|
|
case r of
|
|
Left (SMP _ (SMP.NAME SMP.NO_RESOLVER)) -> pure ()
|
|
_ -> expectationFailure $ "expected Left (SMP _ (NAME NO_RESOLVER)), got: " <> show r
|
|
|
|
testNoNameServers :: HasCallStack => IO ()
|
|
testNoNameServers =
|
|
withNoNameServers $ \c -> do
|
|
r <- runExceptT $ resolveSimplexName c NRMInteractive 1 (SimplexDomain TLDSimplex "alice" [])
|
|
case r of
|
|
Left NO_NAME_SERVERS -> pure ()
|
|
_ -> expectationFailure $ "expected Left NO_NAME_SERVERS, got: " <> show r
|
|
|
|
testBackendError :: HasCallStack => IO ()
|
|
testBackendError =
|
|
withDirectResolver (status502, "{}") $ \c -> do
|
|
r <- runExceptT $ resolveSimplexName c NRMInteractive 1 (SimplexDomain TLDSimplex "alice" [])
|
|
case r of
|
|
Left (SMP _ (SMP.NAME (SMP.RESOLVER _))) -> pure ()
|
|
_ -> expectationFailure $ "expected Left (SMP _ (NAME (RESOLVER ..))), got: " <> show r
|
|
|
|
testDirectSuccess :: HasCallStack => IO ()
|
|
testDirectSuccess =
|
|
withDirectResolver (status200, registeredBody testNameRecord) $ \c -> do
|
|
r <- runExceptT $ resolveSimplexName c NRMInteractive 1 (SimplexDomain TLDSimplex "alice" [])
|
|
case r of
|
|
Right SMP.NameResponse {registration = SMP.NRRegistered {nameRecord}} -> nameRecord `shouldBe` testNameRecord
|
|
_ -> expectationFailure $ "expected Right NRRegistered, got: " <> show r
|