Authenticate CultMesh authority routes and replay operations
This commit is contained in:
parent
6835ea4c10
commit
f622ee1b24
30 changed files with 1432 additions and 142 deletions
241
tests/GameCult.Mesh.Tests/CultMeshAuthorityProofTests.cs
Normal file
241
tests/GameCult.Mesh.Tests/CultMeshAuthorityProofTests.cs
Normal file
|
|
@ -0,0 +1,241 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using GameCult.Networking;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace GameCult.Mesh.Tests;
|
||||
|
||||
public sealed class CultMeshAuthorityProofTests
|
||||
{
|
||||
private static readonly DateTimeOffset Now = DateTimeOffset.FromUnixTimeMilliseconds(1_800_000_000_000);
|
||||
|
||||
[Test]
|
||||
public void SignedRouteBindsEveryAuthorityFieldToConsumerTrust()
|
||||
{
|
||||
using var odin = ECDsa.Create(ECCurve.NamedCurves.nistP256);
|
||||
using var provider = ECDsa.Create(ECCurve.NamedCurves.nistP256);
|
||||
var route = SignedRoute(odin, provider);
|
||||
var trust = Trust(odin);
|
||||
|
||||
trust.Validate("aetheria", route, Now);
|
||||
var wireRoundTrip = new CultMeshVerseDescriptor(
|
||||
"aetheria",
|
||||
"Aetheria",
|
||||
CultMeshVerseAuthorityModel.OperatorCluster,
|
||||
new CultMeshVerseCompatibility("cultmesh.v0", "rules"),
|
||||
authorityRoutes: new[] { route })
|
||||
.ToMessage()
|
||||
.ToVerseDescriptor()
|
||||
.AuthorityRoutes[0];
|
||||
trust.Validate("aetheria", wireRoundTrip, Now);
|
||||
wireRoundTrip.Certificate!.ProviderKey.KeyId.Should().Be("provider-1");
|
||||
|
||||
Action mutatedEndpoint = () => trust.Validate("aetheria", new CultMeshAuthorityRoute(
|
||||
route.AuthorityRuntimeId,
|
||||
"wss://evil.example/mesh",
|
||||
route.ProtocolIds,
|
||||
route.Priority,
|
||||
route.Generation,
|
||||
route.Certificate), Now);
|
||||
mutatedEndpoint.Should().Throw<CultMeshSessionException>()
|
||||
.Which.Failure.Reason.Should().Be(CultMeshSessionFailureReason.Authentication);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoteTrustRejectsUnknownExpiredAndUnsignedRoutes()
|
||||
{
|
||||
using var odin = ECDsa.Create(ECCurve.NamedCurves.nistP256);
|
||||
using var provider = ECDsa.Create(ECCurve.NamedCurves.nistP256);
|
||||
using var stranger = ECDsa.Create(ECCurve.NamedCurves.nistP256);
|
||||
var route = SignedRoute(odin, provider);
|
||||
|
||||
Action unknownRoot = () => Trust(stranger).Validate("aetheria", route, Now);
|
||||
Action expired = () => Trust(odin).Validate("aetheria", route, Now.AddHours(2));
|
||||
Action unsigned = () => Trust(odin).Validate("aetheria", new CultMeshAuthorityRoute(
|
||||
"aetheria-daemon", "wss://provider.example/mesh",
|
||||
new[] { CultMeshProtocols.Documents.Value }, generation: "generation-1"), Now);
|
||||
|
||||
unknownRoot.Should().Throw<CultMeshSessionException>();
|
||||
expired.Should().Throw<CultMeshSessionException>();
|
||||
unsigned.Should().Throw<CultMeshSessionException>();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UnsignedRouteRequiresExplicitLoopbackDevelopmentPolicy()
|
||||
{
|
||||
var local = new CultMeshAuthorityRoute(
|
||||
"aetheria-daemon", "rudp://127.0.0.1:3076",
|
||||
new[] { CultMeshProtocols.Documents.Value }, generation: "local");
|
||||
var remote = new CultMeshAuthorityRoute(
|
||||
"aetheria-daemon", "rudp://192.0.2.10:3076",
|
||||
new[] { CultMeshProtocols.Documents.Value }, generation: "remote");
|
||||
var policy = new CultMeshAuthorityTrustPolicy(CultMeshAuthorityTrustMode.LocalDevelopment);
|
||||
|
||||
policy.Validate("aetheria", local, Now);
|
||||
Action remoteAttempt = () => policy.Validate("aetheria", remote, Now);
|
||||
remoteAttempt.Should().Throw<CultMeshSessionException>();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProviderProofRejectsCredentialFreeEchoAndNonceReplay()
|
||||
{
|
||||
using var odin = ECDsa.Create(ECCurve.NamedCurves.nistP256);
|
||||
using var provider = ECDsa.Create(ECCurve.NamedCurves.nistP256);
|
||||
var route = SignedRoute(odin, provider);
|
||||
var trust = Trust(odin);
|
||||
var signer = new CultMeshSessionProofSigner(route, provider);
|
||||
var request = Request("nonce-a");
|
||||
var signed = Accepted(request, signer.Sign(request));
|
||||
|
||||
CultMeshAuthorityProof.VerifySessionProof(request, signed, "aetheria", route, trust, Now).Should().BeTrue();
|
||||
|
||||
var echo = Accepted(request, string.Empty);
|
||||
CultMeshAuthorityProof.VerifySessionProof(request, echo, "aetheria", route, trust, Now).Should().BeFalse();
|
||||
|
||||
var replayedRequest = Request("nonce-b");
|
||||
CultMeshAuthorityProof.VerifySessionProof(replayedRequest, signed, "aetheria", route, trust, Now).Should().BeFalse();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SessionManagerRejectsPeerThatOnlyEchoesCertifiedIdentityStrings()
|
||||
{
|
||||
using var odin = ECDsa.Create(ECCurve.NamedCurves.nistP256);
|
||||
using var provider = ECDsa.Create(ECCurve.NamedCurves.nistP256);
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var route = CultMeshAuthorityProof.CreateSignedRoute(
|
||||
"aetheria", "aetheria-daemon", "wss://provider.example/mesh",
|
||||
new[] { CultMeshProtocols.Documents.Value }, 0, "generation-live",
|
||||
CultMeshEcdsaP256PublicKey.From("provider-live", provider), "odin-live",
|
||||
now.AddMinutes(-1), now.AddMinutes(5), odin);
|
||||
var descriptor = new CultMeshVerseDescriptor(
|
||||
"aetheria", "Aetheria", CultMeshVerseAuthorityModel.OperatorCluster,
|
||||
new CultMeshVerseCompatibility("cultmesh.v0", "rules"),
|
||||
authorityRoutes: new[] { route });
|
||||
using var discovery = new CultMeshDiscoveryService(new[] { new SignedRouteSource(descriptor) });
|
||||
using var manager = new CultMeshSessionManager(
|
||||
discovery,
|
||||
new[] { new EchoConnector() },
|
||||
new CultMeshSessionManagerOptions
|
||||
{
|
||||
Trust = new CultMeshAuthorityTrustPolicy(
|
||||
CultMeshAuthorityTrustMode.AuthenticatedRemote,
|
||||
new[] { CultMeshEcdsaP256PublicKey.From("odin-live", odin) })
|
||||
});
|
||||
|
||||
Func<Task> connect = async () => await manager.ConnectAsync(
|
||||
new CultMeshSessionTarget("aetheria", "aetheria-daemon"),
|
||||
CultMeshProtocols.Documents);
|
||||
|
||||
(await connect.Should().ThrowAsync<CultMeshSessionException>())
|
||||
.Which.Failure.Reason.Should().Be(CultMeshSessionFailureReason.Authority);
|
||||
}
|
||||
|
||||
private static CultMeshAuthorityRoute SignedRoute(ECDsa odin, ECDsa provider) =>
|
||||
CultMeshAuthorityProof.CreateSignedRoute(
|
||||
"aetheria",
|
||||
"aetheria-daemon",
|
||||
"wss://provider.example/mesh",
|
||||
new[] { CultMeshProtocols.Documents.Value },
|
||||
0,
|
||||
"generation-1",
|
||||
CultMeshEcdsaP256PublicKey.From("provider-1", provider),
|
||||
"odin-root-1",
|
||||
Now.AddMinutes(-1),
|
||||
Now.AddHours(1),
|
||||
odin);
|
||||
|
||||
private static CultMeshAuthorityTrustPolicy Trust(ECDsa odin) => new(
|
||||
CultMeshAuthorityTrustMode.AuthenticatedRemote,
|
||||
new[] { CultMeshEcdsaP256PublicKey.From("odin-root-1", odin) });
|
||||
|
||||
private static GameCult.Networking.CultMeshSessionOpenMessage Request(string nonceLabel)
|
||||
{
|
||||
using var hash = SHA256.Create();
|
||||
return new GameCult.Networking.CultMeshSessionOpenMessage
|
||||
{
|
||||
MessageId = "message-1",
|
||||
SourceRuntimeId = "browser-1",
|
||||
VerseId = "aetheria",
|
||||
AuthorityRuntimeId = "aetheria-daemon",
|
||||
ProtocolId = CultMeshProtocols.Documents.Value,
|
||||
RouteGeneration = "generation-1",
|
||||
ClientNonce = Convert.ToBase64String(hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(nonceLabel)))
|
||||
};
|
||||
}
|
||||
|
||||
private static GameCult.Networking.CultMeshSessionAcceptedMessage Accepted(
|
||||
GameCult.Networking.CultMeshSessionOpenMessage request,
|
||||
string signature) => new()
|
||||
{
|
||||
MessageId = request.MessageId,
|
||||
Accepted = true,
|
||||
VerseId = request.VerseId,
|
||||
AuthorityRuntimeId = request.AuthorityRuntimeId,
|
||||
ProtocolId = request.ProtocolId,
|
||||
RouteGeneration = request.RouteGeneration,
|
||||
ClientNonce = request.ClientNonce,
|
||||
ProviderKeyId = "provider-1",
|
||||
ProviderSignature = signature
|
||||
};
|
||||
|
||||
private sealed class SignedRouteSource : ICultMeshLookupSource
|
||||
{
|
||||
private readonly CultMeshVerseDescriptor _descriptor;
|
||||
public SignedRouteSource(CultMeshVerseDescriptor descriptor) => _descriptor = descriptor;
|
||||
public string SourceId => "signed-odin";
|
||||
public Task<IReadOnlyList<CultMeshDiscoveryObservation>> LookupAsync(
|
||||
CultMeshDiscoveryQuery query,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
Task.FromResult<IReadOnlyList<CultMeshDiscoveryObservation>>(new[]
|
||||
{
|
||||
new CultMeshDiscoveryObservation(
|
||||
_descriptor, SourceId, DateTimeOffset.UtcNow,
|
||||
DateTimeOffset.UtcNow.AddMinutes(1), CultMeshDiscoveryTrust.Signed)
|
||||
});
|
||||
}
|
||||
|
||||
private sealed class EchoConnector : ICultMeshTransportConnector
|
||||
{
|
||||
public string ConnectorId => "credential-free-echo";
|
||||
public int Priority => 0;
|
||||
public bool CanConnect(CultMeshTransportCandidate candidate) => true;
|
||||
public Task<ICultNetSchemaClient> ConnectAsync(
|
||||
CultMeshTransportCandidate candidate,
|
||||
CultMeshProtocolId protocol,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
Task.FromResult<ICultNetSchemaClient>(new EchoClient());
|
||||
}
|
||||
|
||||
private sealed class EchoClient : ICultNetSchemaClient
|
||||
{
|
||||
private Action<CultMeshSessionAcceptedMessage>? _accepted;
|
||||
public bool Connected => true;
|
||||
public void Connect(string host, int port) { }
|
||||
public void SendCultNet<T>(T message) where T : ICultNetSchemaMessage
|
||||
{
|
||||
if (message is not CultMeshSessionOpenMessage request) return;
|
||||
_accepted?.Invoke(new CultMeshSessionAcceptedMessage
|
||||
{
|
||||
MessageId = request.MessageId,
|
||||
Accepted = true,
|
||||
VerseId = request.VerseId,
|
||||
AuthorityRuntimeId = request.AuthorityRuntimeId,
|
||||
ProtocolId = request.ProtocolId,
|
||||
RouteGeneration = request.RouteGeneration,
|
||||
ClientNonce = request.ClientNonce,
|
||||
ProviderKeyId = "provider-live",
|
||||
ProviderSignature = string.Empty
|
||||
});
|
||||
}
|
||||
public void OnCultNet<T>(Action<T> callback) where T : ICultNetSchemaMessage
|
||||
{
|
||||
if (typeof(T) == typeof(CultMeshSessionAcceptedMessage))
|
||||
_accepted = response => callback((T)(object)response);
|
||||
}
|
||||
public void Dispose() { }
|
||||
}
|
||||
}
|
||||
|
|
@ -26,7 +26,7 @@ public sealed class CultMeshBodySessionTests
|
|||
var client = new LoopbackClient(server);
|
||||
var connector = new LoopbackConnector(client);
|
||||
using var discovery = new CultMeshDiscoveryService(new[] { new RouteSource() });
|
||||
using var sessions = new CultMeshSessionManager(discovery, new[] { connector });
|
||||
using var sessions = new CultMeshSessionManager(discovery, new[] { connector }, CultMeshTestTrust.LocalSessions);
|
||||
var provider = new CultMeshSessionBodyProvider(
|
||||
"aetheria.daemon", sessions, new CultMeshSessionTarget("aetheria", "aetheria.daemon"));
|
||||
|
||||
|
|
@ -73,7 +73,7 @@ public sealed class CultMeshBodySessionTests
|
|||
});
|
||||
using var discovery = new CultMeshDiscoveryService(new[] { new RouteSource(endpoint) });
|
||||
using var sessions = new CultMeshSessionManager(discovery,
|
||||
new ICultMeshTransportConnector[] { new CultMeshSchemaTransportConnector() });
|
||||
new ICultMeshTransportConnector[] { new CultMeshSchemaTransportConnector() }, CultMeshTestTrust.LocalSessions);
|
||||
var provider = new CultMeshSessionBodyProvider("aetheria.daemon", sessions,
|
||||
new CultMeshSessionTarget("aetheria", "aetheria.daemon"),
|
||||
new CultMeshSessionBodyProviderOptions { ResponseTimeout = TimeSpan.FromSeconds(10) });
|
||||
|
|
@ -142,7 +142,7 @@ public sealed class CultMeshBodySessionTests
|
|||
private sealed class RouteSource : ICultMeshLookupSource
|
||||
{
|
||||
private readonly string _endpoint;
|
||||
public RouteSource(string endpoint = "rudp://body.test:3076") => _endpoint = endpoint;
|
||||
public RouteSource(string endpoint = "rudp://127.0.0.13:3076") => _endpoint = endpoint;
|
||||
public string SourceId => "test";
|
||||
public Task<IReadOnlyList<CultMeshDiscoveryObservation>> LookupAsync(CultMeshDiscoveryQuery query,
|
||||
CancellationToken cancellationToken = default)
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ public sealed class CultMeshClientTests
|
|||
return new RendezvousClient();
|
||||
}
|
||||
},
|
||||
Sessions = CultMeshTestTrust.LocalSessions,
|
||||
Connectors = new[] { connector }
|
||||
});
|
||||
|
||||
|
|
@ -48,7 +49,7 @@ public sealed class CultMeshClientTests
|
|||
|
||||
first.Should().BeSameAs(second);
|
||||
first.Target.Should().Be(Target);
|
||||
first.State.Path!.Endpoint.Should().Be("rudp://aetheria:3081");
|
||||
first.State.Path!.Endpoint.Should().Be("rudp://127.0.0.1:3081");
|
||||
discoveryClients.Should().Be(1);
|
||||
connector.ConnectCount.Should().Be(1);
|
||||
}
|
||||
|
|
@ -61,6 +62,7 @@ public sealed class CultMeshClientTests
|
|||
{
|
||||
RendezvousEndpoints = new[] { "rudp://odin:3076" },
|
||||
Discovery = new CultMeshVerseDiscoveryClientOptions { CreateClient = () => new RendezvousClient() },
|
||||
Sessions = CultMeshTestTrust.LocalSessions,
|
||||
Connectors = new[] { connector }
|
||||
});
|
||||
|
||||
|
|
@ -82,6 +84,7 @@ public sealed class CultMeshClientTests
|
|||
{
|
||||
RendezvousEndpoints = new[] { "rudp://odin:3076" },
|
||||
Discovery = new CultMeshVerseDiscoveryClientOptions { CreateClient = () => new RendezvousClient() },
|
||||
Sessions = CultMeshTestTrust.LocalSessions,
|
||||
Connectors = new[] { connector }
|
||||
});
|
||||
|
||||
|
|
@ -103,6 +106,7 @@ public sealed class CultMeshClientTests
|
|||
{
|
||||
RendezvousEndpoints = new[] { "rudp://odin:3076" },
|
||||
Discovery = new CultMeshVerseDiscoveryClientOptions { CreateClient = () => new RendezvousClient() },
|
||||
Sessions = CultMeshTestTrust.LocalSessions,
|
||||
Connectors = new[] { connector }
|
||||
});
|
||||
using var lease = await mesh.LeaseDocumentAsync<ClientTestDocument>(Target, "surface:pilot");
|
||||
|
|
@ -125,6 +129,7 @@ public sealed class CultMeshClientTests
|
|||
{
|
||||
RendezvousEndpoints = new[] { "rudp://odin:3076" },
|
||||
Discovery = new CultMeshVerseDiscoveryClientOptions { CreateClient = () => new RendezvousClient() },
|
||||
Sessions = CultMeshTestTrust.LocalSessions,
|
||||
Connectors = new[] { connector }
|
||||
});
|
||||
|
||||
|
|
@ -148,6 +153,7 @@ public sealed class CultMeshClientTests
|
|||
{
|
||||
RendezvousEndpoints = new[] { "rudp://odin:3076" },
|
||||
Discovery = new CultMeshVerseDiscoveryClientOptions { CreateClient = () => new RendezvousClient() },
|
||||
Sessions = CultMeshTestTrust.LocalSessions,
|
||||
Connectors = new[] { connector },
|
||||
SubscriptionResponseTimeout = TimeSpan.FromMilliseconds(20)
|
||||
});
|
||||
|
|
@ -168,6 +174,7 @@ public sealed class CultMeshClientTests
|
|||
{
|
||||
RendezvousEndpoints = new[] { "rudp://odin:3076" },
|
||||
Discovery = new CultMeshVerseDiscoveryClientOptions { CreateClient = () => new RendezvousClient() },
|
||||
Sessions = CultMeshTestTrust.LocalSessions,
|
||||
Connectors = new[] { connector }
|
||||
});
|
||||
|
||||
|
|
@ -187,6 +194,7 @@ public sealed class CultMeshClientTests
|
|||
{
|
||||
RendezvousEndpoints = new[] { "rudp://odin:3076" },
|
||||
Discovery = new CultMeshVerseDiscoveryClientOptions { CreateClient = () => new RendezvousClient() },
|
||||
Sessions = CultMeshTestTrust.LocalSessions,
|
||||
Connectors = new[] { connector }
|
||||
});
|
||||
|
||||
|
|
@ -211,6 +219,7 @@ public sealed class CultMeshClientTests
|
|||
{
|
||||
RendezvousEndpoints = new[] { "rudp://odin:3076" },
|
||||
Discovery = new CultMeshVerseDiscoveryClientOptions { CreateClient = () => new RendezvousClient() },
|
||||
Sessions = CultMeshTestTrust.LocalSessions,
|
||||
Connectors = new[] { connector }
|
||||
});
|
||||
|
||||
|
|
@ -237,6 +246,7 @@ public sealed class CultMeshClientTests
|
|||
{
|
||||
RendezvousEndpoints = new[] { "rudp://odin:3076" },
|
||||
Discovery = new CultMeshVerseDiscoveryClientOptions { CreateClient = () => new RendezvousClient() },
|
||||
Sessions = CultMeshTestTrust.LocalSessions,
|
||||
Connectors = new[] { connector }
|
||||
});
|
||||
|
||||
|
|
@ -262,6 +272,7 @@ public sealed class CultMeshClientTests
|
|||
{
|
||||
RendezvousEndpoints = new[] { "rudp://odin:3076" },
|
||||
Discovery = new CultMeshVerseDiscoveryClientOptions { CreateClient = () => new RendezvousClient() },
|
||||
Sessions = CultMeshTestTrust.LocalSessions,
|
||||
Connectors = new[] { connector }
|
||||
});
|
||||
var request = new ClientTestDocument { Id = "command:1", Text = "move" };
|
||||
|
|
@ -288,6 +299,7 @@ public sealed class CultMeshClientTests
|
|||
{
|
||||
RendezvousEndpoints = new[] { "rudp://odin:3076" },
|
||||
Discovery = new CultMeshVerseDiscoveryClientOptions { CreateClient = () => new RendezvousClient() },
|
||||
Sessions = CultMeshTestTrust.LocalSessions,
|
||||
Connectors = new[] { connector }
|
||||
});
|
||||
|
||||
|
|
@ -319,6 +331,7 @@ public sealed class CultMeshClientTests
|
|||
{
|
||||
RendezvousEndpoints = new[] { "rudp://odin:3076" },
|
||||
Discovery = new CultMeshVerseDiscoveryClientOptions { CreateClient = () => new RendezvousClient() },
|
||||
Sessions = CultMeshTestTrust.LocalSessions,
|
||||
Connectors = new[] { connector },
|
||||
OperationResponseTimeout = TimeSpan.FromSeconds(2)
|
||||
});
|
||||
|
|
@ -348,6 +361,7 @@ public sealed class CultMeshClientTests
|
|||
{
|
||||
RendezvousEndpoints = new[] { "rudp://odin:3076" },
|
||||
Discovery = new CultMeshVerseDiscoveryClientOptions { CreateClient = () => new RendezvousClient() },
|
||||
Sessions = CultMeshTestTrust.LocalSessions,
|
||||
Connectors = new[] { connector },
|
||||
OperationResponseTimeout = TimeSpan.FromSeconds(5)
|
||||
});
|
||||
|
|
@ -378,6 +392,7 @@ public sealed class CultMeshClientTests
|
|||
{
|
||||
RendezvousEndpoints = new[] { "rudp://odin:3076" },
|
||||
Discovery = new CultMeshVerseDiscoveryClientOptions { CreateClient = () => new RendezvousClient() },
|
||||
Sessions = CultMeshTestTrust.LocalSessions,
|
||||
Connectors = new[] { connector }
|
||||
});
|
||||
|
||||
|
|
@ -403,6 +418,7 @@ public sealed class CultMeshClientTests
|
|||
{
|
||||
RendezvousEndpoints = new[] { "rudp://odin:3076" },
|
||||
Discovery = new CultMeshVerseDiscoveryClientOptions { CreateClient = () => new RendezvousClient() },
|
||||
Sessions = CultMeshTestTrust.LocalSessions,
|
||||
Connectors = new[] { connector }
|
||||
});
|
||||
var opening = mesh.LeaseDocumentAsync<ClientTestDocument>(Target, "surface:pilot");
|
||||
|
|
@ -432,7 +448,7 @@ public sealed class CultMeshClientTests
|
|||
"Aetheria",
|
||||
CultMeshVerseAuthorityModel.OperatorCluster,
|
||||
new CultMeshVerseCompatibility("cultmesh.v0", "rules"),
|
||||
new[] { "rudp://aetheria:3081" },
|
||||
new[] { "rudp://127.0.0.1:3081" },
|
||||
new[] { "aetheria-daemon" }).ToMessage()
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ public sealed class CultMeshContentSessionTests
|
|||
using var sessions = new CultMeshSessionManager(
|
||||
discovery,
|
||||
Array.Empty<ICultMeshTransportConnector>(),
|
||||
new ICultMeshContentTransportConnector[] { connector });
|
||||
new ICultMeshContentTransportConnector[] { connector }, CultMeshTestTrust.LocalSessions);
|
||||
var provider = new CultMeshSessionContentProvider(
|
||||
"aetheria.daemon",
|
||||
sessions,
|
||||
|
|
@ -99,7 +99,7 @@ public sealed class CultMeshContentSessionTests
|
|||
using var sessions = new CultMeshSessionManager(
|
||||
discovery,
|
||||
Array.Empty<ICultMeshTransportConnector>(),
|
||||
new ICultMeshContentTransportConnector[] { new CultMeshTcpContentTransportConnector() });
|
||||
new ICultMeshContentTransportConnector[] { new CultMeshTcpContentTransportConnector() }, CultMeshTestTrust.LocalSessions);
|
||||
|
||||
Func<Task> connect = async () => await sessions.ConnectContentAsync(
|
||||
new CultMeshSessionTarget("aetheria", "aetheria.daemon"));
|
||||
|
|
@ -151,7 +151,7 @@ public sealed class CultMeshContentSessionTests
|
|||
using var sessions = new CultMeshSessionManager(
|
||||
discovery,
|
||||
Array.Empty<ICultMeshTransportConnector>(),
|
||||
new ICultMeshContentTransportConnector[] { connector });
|
||||
new ICultMeshContentTransportConnector[] { connector }, CultMeshTestTrust.LocalSessions);
|
||||
var provider = new CultMeshSessionContentProvider(
|
||||
"aetheria.daemon",
|
||||
sessions,
|
||||
|
|
@ -220,7 +220,7 @@ public sealed class CultMeshContentSessionTests
|
|||
{
|
||||
private readonly string _endpoint;
|
||||
|
||||
public RouteSource(string endpoint = "rudp://content.test:3076") => _endpoint = endpoint;
|
||||
public RouteSource(string endpoint = "rudp://127.0.0.14:3076") => _endpoint = endpoint;
|
||||
|
||||
public string SourceId => "test-rendezvous";
|
||||
|
||||
|
|
|
|||
|
|
@ -28,22 +28,22 @@ public sealed class CultMeshSessionManagerTests
|
|||
connected = candidate;
|
||||
return Task.FromResult<ICultNetSchemaClient>(new FakeSchemaClient());
|
||||
});
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, new CultMeshSessionManagerOptions { Clock = clock });
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, CultMeshTestTrust.LocalSessionsWithClock(clock));
|
||||
|
||||
await manager.ConnectAsync(Target, CultMeshProtocols.Documents);
|
||||
|
||||
connected.Should().NotBeNull();
|
||||
connected!.Endpoint.Should().Be("rudp://aetheria:3076");
|
||||
connected!.Endpoint.Should().Be("rudp://127.0.0.1:3076");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Connect_ReusesInflightAndEstablishedSession()
|
||||
{
|
||||
var clock = new ManualSessionClock();
|
||||
using var discovery = Discovery(clock, () => new[] { "rudp://direct:3076" });
|
||||
using var discovery = Discovery(clock, () => new[] { "rudp://127.0.0.2:3076" });
|
||||
var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var connector = new FakeConnector(async (_, _) => { await release.Task; return new FakeSchemaClient(); });
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, new CultMeshSessionManagerOptions { Clock = clock });
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, CultMeshTestTrust.LocalSessionsWithClock(clock));
|
||||
var target = Target;
|
||||
|
||||
var first = manager.ConnectAsync(target, CultMeshProtocols.Documents);
|
||||
|
|
@ -61,19 +61,19 @@ public sealed class CultMeshSessionManagerTests
|
|||
public async Task Connect_RacesPathsAndDisposesSuccessfulLoser()
|
||||
{
|
||||
var clock = new ManualSessionClock();
|
||||
using var discovery = Discovery(clock, () => new[] { "rudp://slow:3076", "rudp://fast:3076" });
|
||||
using var discovery = Discovery(clock, () => new[] { "rudp://127.0.0.3:3076", "rudp://127.0.0.4:3076" });
|
||||
var slowRelease = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var slowClient = new FakeSchemaClient();
|
||||
var fastClient = new FakeSchemaClient();
|
||||
var connector = new FakeConnector(async (candidate, _) =>
|
||||
{
|
||||
if (candidate.Endpoint.Contains("slow")) { await slowRelease.Task; return slowClient; }
|
||||
if (candidate.Endpoint.Contains("127.0.0.3")) { await slowRelease.Task; return slowClient; }
|
||||
return fastClient;
|
||||
});
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, new CultMeshSessionManagerOptions { Clock = clock });
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, CultMeshTestTrust.LocalSessionsWithClock(clock));
|
||||
|
||||
var session = await manager.ConnectAsync(Target, CultMeshProtocols.Documents);
|
||||
session.State.Path!.Endpoint.Should().Be("rudp://fast:3076");
|
||||
session.State.Path!.Endpoint.Should().Be("rudp://127.0.0.4:3076");
|
||||
slowRelease.TrySetResult();
|
||||
await WaitUntilAsync(() => slowClient.DisposeCount == 1);
|
||||
fastClient.DisposeCount.Should().Be(0);
|
||||
|
|
@ -90,8 +90,8 @@ public sealed class CultMeshSessionManagerTests
|
|||
new CultMeshVerseCompatibility("cultmesh.v0", "rules"),
|
||||
authorityRoutes: new[]
|
||||
{
|
||||
new CultMeshAuthorityRoute("decoy-daemon", "rudp://decoy:3076"),
|
||||
new CultMeshAuthorityRoute("aetheria-daemon", "rudp://authority:3076")
|
||||
new CultMeshAuthorityRoute("decoy-daemon", "rudp://127.0.0.5:3076"),
|
||||
new CultMeshAuthorityRoute("aetheria-daemon", "rudp://127.0.0.6:3076")
|
||||
});
|
||||
using var discovery = new CultMeshDiscoveryService(
|
||||
new[] { new StaticRouteSource(clock, descriptor) },
|
||||
|
|
@ -102,22 +102,22 @@ public sealed class CultMeshSessionManagerTests
|
|||
attempts.Add(candidate.Endpoint);
|
||||
return Task.FromResult<ICultNetSchemaClient>(new FakeSchemaClient());
|
||||
});
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector });
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, CultMeshTestTrust.LocalSessions);
|
||||
|
||||
var session = await manager.ConnectAsync(Target, CultMeshProtocols.Documents);
|
||||
|
||||
session.State.Path!.Endpoint.Should().Be("rudp://authority:3076");
|
||||
attempts.Should().Equal("rudp://authority:3076");
|
||||
session.State.Path!.Endpoint.Should().Be("rudp://127.0.0.6:3076");
|
||||
attempts.Should().Equal("rudp://127.0.0.6:3076");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Connect_RejectsRouteWhoseNativeIdentityDoesNotMatchTarget()
|
||||
{
|
||||
var clock = new ManualSessionClock();
|
||||
using var discovery = Discovery(clock, () => new[] { "rudp://decoy:3076" });
|
||||
using var discovery = Discovery(clock, () => new[] { "rudp://127.0.0.5:3076" });
|
||||
var connector = new FakeConnector((_, _) =>
|
||||
Task.FromResult<ICultNetSchemaClient>(new NativeMismatchClient()));
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector });
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, CultMeshTestTrust.LocalSessions);
|
||||
|
||||
var error = Assert.ThrowsAsync<CultMeshSessionException>(() =>
|
||||
manager.ConnectAsync(Target, CultMeshProtocols.Documents));
|
||||
|
|
@ -129,10 +129,10 @@ public sealed class CultMeshSessionManagerTests
|
|||
public void Connect_RejectsPortableHandshakeFromWrongRuntime()
|
||||
{
|
||||
var clock = new ManualSessionClock();
|
||||
using var discovery = Discovery(clock, () => new[] { "rudp://decoy:3076" });
|
||||
using var discovery = Discovery(clock, () => new[] { "rudp://127.0.0.5:3076" });
|
||||
var connector = new FakeConnector((_, _) =>
|
||||
Task.FromResult<ICultNetSchemaClient>(new HandshakeSchemaClient("decoy-daemon")));
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector });
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, CultMeshTestTrust.LocalSessions);
|
||||
|
||||
var error = Assert.ThrowsAsync<CultMeshSessionException>(() =>
|
||||
manager.ConnectAsync(Target, CultMeshProtocols.Documents));
|
||||
|
|
@ -148,7 +148,7 @@ public sealed class CultMeshSessionManagerTests
|
|||
"Aetheria",
|
||||
CultMeshVerseAuthorityModel.OperatorCluster,
|
||||
new CultMeshVerseCompatibility("cultmesh.v0", "rules"),
|
||||
discoveryEndpoints: new[] { "rudp://shared:3076" },
|
||||
discoveryEndpoints: new[] { "rudp://127.0.0.7:3076" },
|
||||
authorityRuntimeIds: new[] { "authority-a", "authority-b" });
|
||||
|
||||
action.Should().Throw<InvalidOperationException>().WithMessage("*ambiguous*");
|
||||
|
|
@ -158,7 +158,7 @@ public sealed class CultMeshSessionManagerTests
|
|||
public async Task Connect_CallerCancellationDoesNotCancelSharedAttempt()
|
||||
{
|
||||
var clock = new ManualSessionClock();
|
||||
using var discovery = Discovery(clock, () => new[] { "rudp://direct:3076" });
|
||||
using var discovery = Discovery(clock, () => new[] { "rudp://127.0.0.2:3076" });
|
||||
var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var connector = new FakeConnector(async (_, token) =>
|
||||
{
|
||||
|
|
@ -166,7 +166,7 @@ public sealed class CultMeshSessionManagerTests
|
|||
await release.Task;
|
||||
return new FakeSchemaClient();
|
||||
});
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, new CultMeshSessionManagerOptions { Clock = clock });
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, CultMeshTestTrust.LocalSessionsWithClock(clock));
|
||||
using var cancellation = new CancellationTokenSource();
|
||||
var target = Target;
|
||||
|
||||
|
|
@ -185,7 +185,7 @@ public sealed class CultMeshSessionManagerTests
|
|||
public async Task BackgroundFailureMigratesSameSessionAcrossEndpointRotation()
|
||||
{
|
||||
var clock = new ManualSessionClock();
|
||||
var route = "rudp://first:3076";
|
||||
var route = "rudp://127.0.0.8:3076";
|
||||
using var discovery = Discovery(clock, () => new[] { route }, TimeSpan.FromSeconds(5));
|
||||
var clients = new List<FakeSchemaClient>();
|
||||
var connector = new FakeConnector((_, _) =>
|
||||
|
|
@ -194,7 +194,7 @@ public sealed class CultMeshSessionManagerTests
|
|||
clients.Add(client);
|
||||
return Task.FromResult<ICultNetSchemaClient>(client);
|
||||
});
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, new CultMeshSessionManagerOptions { Clock = clock });
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, CultMeshTestTrust.LocalSessionsWithClock(clock));
|
||||
var target = Target;
|
||||
var first = await manager.ConnectAsync(target, CultMeshProtocols.Documents);
|
||||
var states = new List<CultMeshSessionStatus>();
|
||||
|
|
@ -204,14 +204,14 @@ public sealed class CultMeshSessionManagerTests
|
|||
schemaLease.OnCultNet<CultNetErrorMessage>(_ => deliveries++);
|
||||
clients[0].Emit(new CultNetErrorMessage { Error = "before rotation" });
|
||||
|
||||
route = "rudp://second:3076";
|
||||
route = "rudp://127.0.0.9:3076";
|
||||
clients[0].Fail(new IOException("partition"));
|
||||
await WaitUntilAsync(() => first.State.Status == CultMeshSessionStatus.Online && connector.ConnectCount == 2);
|
||||
var second = await manager.ConnectAsync(target, CultMeshProtocols.Documents);
|
||||
clients[1].Emit(new CultNetErrorMessage { Error = "after rotation" });
|
||||
|
||||
second.Should().BeSameAs(first);
|
||||
second.State.Path!.Endpoint.Should().Be("rudp://second:3076");
|
||||
second.State.Path!.Endpoint.Should().Be("rudp://127.0.0.9:3076");
|
||||
states.Should().ContainInOrder(CultMeshSessionStatus.Reconnecting, CultMeshSessionStatus.Online);
|
||||
connector.ConnectCount.Should().Be(2);
|
||||
clients[0].DisposeCount.Should().Be(1);
|
||||
|
|
@ -238,8 +238,8 @@ public sealed class CultMeshSessionManagerTests
|
|||
public void Connect_ReportsUnsupportedPathPrecisely()
|
||||
{
|
||||
var clock = new ManualSessionClock();
|
||||
using var discovery = Discovery(clock, () => new[] { "quic://unimplemented:443" });
|
||||
using var manager = new CultMeshSessionManager(discovery, Array.Empty<ICultMeshTransportConnector>(), new CultMeshSessionManagerOptions { Clock = clock });
|
||||
using var discovery = Discovery(clock, () => new[] { "quic://127.0.0.10:443" });
|
||||
using var manager = new CultMeshSessionManager(discovery, Array.Empty<ICultMeshTransportConnector>(), CultMeshTestTrust.LocalSessionsWithClock(clock));
|
||||
|
||||
var error = Assert.ThrowsAsync<CultMeshSessionException>(() =>
|
||||
manager.ConnectAsync(Target, CultMeshProtocols.Documents));
|
||||
|
|
@ -253,10 +253,10 @@ public sealed class CultMeshSessionManagerTests
|
|||
public async Task SnapshotSessionsBorrowOneManagedChannelAndDisposeRegistrationsIndependently()
|
||||
{
|
||||
var clock = new ManualSessionClock();
|
||||
using var discovery = Discovery(clock, () => new[] { "rudp://direct:3076" });
|
||||
using var discovery = Discovery(clock, () => new[] { "rudp://127.0.0.2:3076" });
|
||||
var client = new RespondingSchemaClient();
|
||||
var connector = new FakeConnector((_, _) => Task.FromResult<ICultNetSchemaClient>(client));
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, new CultMeshSessionManagerOptions { Clock = clock });
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, CultMeshTestTrust.LocalSessionsWithClock(clock));
|
||||
var target = Target;
|
||||
using var first = await CultMeshSnapshotSession.ConnectAsync(manager, target, new CultMeshSnapshotRequestOptions());
|
||||
using var second = await CultMeshSnapshotSession.ConnectAsync(manager, target, new CultMeshSnapshotRequestOptions());
|
||||
|
|
@ -274,10 +274,10 @@ public sealed class CultMeshSessionManagerTests
|
|||
public async Task PeerExchangeBorrowsManagedProtocolSessionAcrossRequests()
|
||||
{
|
||||
var clock = new ManualSessionClock();
|
||||
using var discovery = Discovery(clock, () => new[] { "rudp://direct:3076" });
|
||||
using var discovery = Discovery(clock, () => new[] { "rudp://127.0.0.2:3076" });
|
||||
var client = new PeerExchangeSchemaClient();
|
||||
var connector = new FakeConnector((_, _) => Task.FromResult<ICultNetSchemaClient>(client));
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, new CultMeshSessionManagerOptions { Clock = clock });
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, CultMeshTestTrust.LocalSessionsWithClock(clock));
|
||||
var exchange = new CultMeshPeerExchangeClient(new CultMeshPeerExchangeClientOptions { Sessions = manager, Clock = clock });
|
||||
var target = Target;
|
||||
|
||||
|
|
@ -294,10 +294,10 @@ public sealed class CultMeshSessionManagerTests
|
|||
public async Task SchemaClientLeasesDisposeHandlersWithoutClosingSession()
|
||||
{
|
||||
var clock = new ManualSessionClock();
|
||||
using var discovery = Discovery(clock, () => new[] { "rudp://direct:3076" });
|
||||
using var discovery = Discovery(clock, () => new[] { "rudp://127.0.0.2:3076" });
|
||||
var client = new LeaseSchemaClient();
|
||||
var connector = new FakeConnector((_, _) => Task.FromResult<ICultNetSchemaClient>(client));
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, new CultMeshSessionManagerOptions { Clock = clock });
|
||||
using var manager = new CultMeshSessionManager(discovery, new[] { connector }, CultMeshTestTrust.LocalSessionsWithClock(clock));
|
||||
var session = await manager.ConnectAsync(Target, CultMeshProtocols.Subscriptions);
|
||||
using var first = session.OpenSchemaClient();
|
||||
using var second = session.OpenSchemaClient();
|
||||
|
|
@ -363,8 +363,8 @@ public sealed class CultMeshSessionManagerTests
|
|||
var expires = _clock.UtcNow.AddMinutes(1);
|
||||
return Task.FromResult<IReadOnlyList<CultMeshDiscoveryObservation>>(new[]
|
||||
{
|
||||
Observation("norn", "rudp://norn:3076", expires),
|
||||
Observation("aetheria", "rudp://aetheria:3076", expires)
|
||||
Observation("norn", "rudp://127.0.0.11:3076", expires),
|
||||
Observation("aetheria", "rudp://127.0.0.1:3076", expires)
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -544,7 +544,7 @@ public sealed class CultMeshSessionManagerTests
|
|||
MessageId = request.MessageId,
|
||||
Peers = new[]
|
||||
{
|
||||
new CultMeshPeerCard("peer-a", request.VerseId, new[] { "rudp://peer-a:3076" }).ToMessage()
|
||||
new CultMeshPeerCard("peer-a", request.VerseId, new[] { "rudp://127.0.0.12:3076" }).ToMessage()
|
||||
}
|
||||
};
|
||||
foreach (var handler in _handlers.ToArray()) handler(response);
|
||||
|
|
|
|||
18
tests/GameCult.Mesh.Tests/CultMeshTestTrust.cs
Normal file
18
tests/GameCult.Mesh.Tests/CultMeshTestTrust.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
namespace GameCult.Mesh.Tests;
|
||||
|
||||
internal static class CultMeshTestTrust
|
||||
{
|
||||
internal static CultMeshAuthorityTrustPolicy LocalPolicy { get; } =
|
||||
new(CultMeshAuthorityTrustMode.LocalDevelopment);
|
||||
|
||||
internal static CultMeshSessionManagerOptions LocalSessions => new()
|
||||
{
|
||||
Trust = LocalPolicy
|
||||
};
|
||||
|
||||
internal static CultMeshSessionManagerOptions LocalSessionsWithClock(ICultMeshClock clock) => new()
|
||||
{
|
||||
Clock = clock,
|
||||
Trust = LocalPolicy
|
||||
};
|
||||
}
|
||||
|
|
@ -44,7 +44,7 @@ public sealed class CultMeshTransportModularityTests
|
|||
{
|
||||
legacy,
|
||||
new CultMeshTcpSchemaTransportConnector()
|
||||
});
|
||||
}, CultMeshTestTrust.LocalSessions);
|
||||
|
||||
var session = await sessions.ConnectAsync(
|
||||
new CultMeshSessionTarget("aetheria", "aetheria.daemon"),
|
||||
|
|
@ -91,7 +91,7 @@ public sealed class CultMeshTransportModularityTests
|
|||
{
|
||||
legacy,
|
||||
new CultMeshTcpContentTransportConnector()
|
||||
});
|
||||
}, CultMeshTestTrust.LocalSessions);
|
||||
|
||||
var session = await sessions.ConnectContentAsync(new CultMeshSessionTarget("aetheria", "aetheria.daemon"));
|
||||
using var destination = new MemoryStream();
|
||||
|
|
@ -111,7 +111,7 @@ public sealed class CultMeshTransportModularityTests
|
|||
});
|
||||
using var sessions = new CultMeshSessionManager(
|
||||
discovery,
|
||||
Array.Empty<ICultMeshTransportConnector>());
|
||||
Array.Empty<ICultMeshTransportConnector>(), CultMeshTestTrust.LocalSessions);
|
||||
|
||||
Func<Task> connect = async () => await sessions.ConnectContentAsync(
|
||||
new CultMeshSessionTarget("aetheria", "aetheria.daemon"));
|
||||
|
|
@ -132,7 +132,7 @@ public sealed class CultMeshTransportModularityTests
|
|||
discovery,
|
||||
Array.Empty<ICultMeshTransportConnector>(),
|
||||
Array.Empty<ICultMeshContentTransportConnector>(),
|
||||
new ICultMeshRealtimeTransportConnector[] { connector });
|
||||
new ICultMeshRealtimeTransportConnector[] { connector }, CultMeshTestTrust.LocalSessions);
|
||||
var session = await sessions.ConnectRealtimeAsync(
|
||||
new CultMeshSessionTarget("aetheria", "aetheria.daemon"));
|
||||
var sent = new CultMeshRealtimeFrame
|
||||
|
|
@ -163,7 +163,7 @@ public sealed class CultMeshTransportModularityTests
|
|||
});
|
||||
using var sessions = new CultMeshSessionManager(
|
||||
discovery,
|
||||
Array.Empty<ICultMeshTransportConnector>());
|
||||
Array.Empty<ICultMeshTransportConnector>(), CultMeshTestTrust.LocalSessions);
|
||||
|
||||
Func<Task> connect = async () => await sessions.ConnectRealtimeAsync(
|
||||
new CultMeshSessionTarget("aetheria", "aetheria.daemon"));
|
||||
|
|
@ -183,7 +183,7 @@ public sealed class CultMeshTransportModularityTests
|
|||
discovery,
|
||||
Array.Empty<ICultMeshTransportConnector>(),
|
||||
Array.Empty<ICultMeshContentTransportConnector>(),
|
||||
new ICultMeshRealtimeTransportConnector[] { new UnverifiedRealtimeConnector() });
|
||||
new ICultMeshRealtimeTransportConnector[] { new UnverifiedRealtimeConnector() }, CultMeshTestTrust.LocalSessions);
|
||||
|
||||
Func<Task> connect = async () => await sessions.ConnectRealtimeAsync(
|
||||
new CultMeshSessionTarget("aetheria", "aetheria.daemon"));
|
||||
|
|
|
|||
Loading…
Reference in a new issue