using System.Net; using XFEEliteDangerous.NativeFormat.Replication; using XFEEliteDangerous.Core; using Xunit; namespace XFEEliteDangerous.Tests; public sealed class NativeEdServerReliableControlTests { [Theory][InlineData(false,0)][InlineData(true,3)] public void ARouteRequestGetsAReliableDirectoryUpdateAndRetriesReplayTheSameResponse(bool framed,byte alignment) { var lookups=0; using var fixture=new Fixture(framed,alignment,resolveRoute:machine=>{lookups++;return machine==123?new(0x7F000001,47001,1360):null;}); var query=new byte[8];var bit=0;EdServerControlMessageCodec.WriteLetterType(query,ref bit,5,11); for(var index=0;index<48;index++,bit++)if(((123UL>>index)&1)!=0)query[bit/8]|=(byte)(1<<(bit%8)); query[bit/8]|=(byte)(1<<(bit%8)); var packet=fixture.Packet(Seq(102),(query,49));var replies=fixture.Session.Receive(packet,false); Assert.Equal(2,replies.Count);Assert.Equal(1,lookups);Assert.Equal(1,fixture.Session.PendingReliable); bit=0;Assert.Equal(2,EdServerControlMessageCodec.ReadLetterType(replies[0],ref bit,16)); EdServerControlMessageCodec.ReadSequenceBody(replies[0],ref bit); Assert.Equal(5,EdServerControlMessageCodec.ReadLetterType(replies[0],ref bit,16)); var route=EdServerRouteMessageCodec.ReadBody(replies[0],ref bit); Assert.False(route.IsRequest);Assert.Equal(123UL,route.MachineRunId);Assert.Equal(new[]{"IP4"},route.Schemes);Assert.Equal(1,route.EndpointCount); var replay=fixture.Session.Receive(packet,false);Assert.Equal(replies[0],replay[0]);Assert.Equal(1,lookups); var malformed=fixture.Packet(Seq(103),(query,49),(Type(99),0)); Assert.Empty(fixture.Session.Receive(malformed,false));Assert.Equal(1,lookups); } [Fact] public void IdlePrivateConnectionSendsAnUnsequencedAckWithoutAllocatingOrStartingAnApplication() { using var fixture=new Fixture(withApplication:true); fixture.Clock.Advance(TimeSpan.FromMilliseconds(999));Assert.Empty(fixture.Session.ClaimDueRetransmissions()); fixture.Clock.Advance(TimeSpan.FromMilliseconds(1)); var heartbeat=Assert.Single(fixture.Session.ClaimDueRetransmissions());var p=0; Assert.Equal((byte)EdServerBuiltInLetter.Ack,EdServerControlMessageCodec.ReadLetterType(heartbeat,ref p,16)); Assert.Equal(new EdServerAckMessage(101,0,16),EdServerControlMessageCodec.ReadAckBody(heartbeat,ref p)); Assert.True(EdServerControlMessageCodec.IsZeroPadding(heartbeat,p)); Assert.Equal(1,fixture.Session.HeartbeatPackets);Assert.Equal(0,fixture.Session.PendingReliable); Assert.Null(fixture.Session.InitialObjectIdBase);Assert.Equal(1UL,fixture.Allocator.AllocatedBlockCount); Assert.Empty(fixture.Session.ClaimDueRetransmissions()); fixture.Session.Revoke();fixture.Clock.Advance(TimeSpan.FromSeconds(2)); Assert.Empty(fixture.Session.ClaimDueRetransmissions());Assert.Equal(1,fixture.Session.HeartbeatPackets); } [Fact] public void ReliableRepliesSuppressExtraHeartbeatsAndLaterHeartbeatUsesCommittedReceivePosition() { using var fixture=new Fixture(withApplication:true); var first=fixture.Session.Receive(fixture.Packet(Seq(102),Id()),false);Assert.Equal(2,first.Count); fixture.Clock.Advance(TimeSpan.FromSeconds(1)); Assert.Equal(first[0],Assert.Single(fixture.Session.ClaimDueRetransmissions())); Assert.Equal(0,fixture.Session.HeartbeatPackets); var p=16;var replySequence=EdServerControlMessageCodec.ReadSequenceBody(first[0],ref p); Assert.Empty(fixture.Session.Receive(fixture.Packet(Ack(replySequence)),false)); fixture.Clock.Advance(TimeSpan.FromMilliseconds(999));Assert.Empty(fixture.Session.ClaimDueRetransmissions()); fixture.Clock.Advance(TimeSpan.FromMilliseconds(1));var heartbeat=Assert.Single(fixture.Session.ClaimDueRetransmissions());p=16; Assert.Equal((ushort)102,EdServerControlMessageCodec.ReadAckBody(heartbeat,ref p).Sequence); Assert.Equal(1,fixture.Session.HeartbeatPackets);Assert.Equal(0,fixture.Session.PendingReliable); Assert.Equal(2UL,fixture.Allocator.AllocatedBlockCount); } [Fact] public void ReconnectedNativeClientCanStartManagementWithoutRequestingAnotherIdBlock() { using var fixture=new Fixture(withApplication:true); Assert.Equal(2,fixture.Session.Receive(fixture.Packet(Seq(102),MtuReply(),Id()),false).Count); var firstBase=fixture.Session.InitialObjectIdBase; var reply=Assert.Single(fixture.Session.Receive(EdServerControlMessageCodec.EncodeSyncLetter(Fixture.Sync with{Sequence=501}),false)); var p=0;var sync=EdServerControlMessageCodec.ReadSyncLetter(reply,ref p,33837); Assert.Empty(fixture.Session.Receive(fixture.Packet(Ack(sync.Sequence)),false)); // The actual native reconnect sends StartServerAdmin without type10, // before the later reliable group carrying the new MTU reply. var accepted=fixture.Session.Receive(fixture.Packet(Seq(502),Start()),false); Assert.Equal(2,accepted.Count);Assert.Equal("",fixture.Session.LastControlFailure); Assert.Equal(firstBase,fixture.Session.InitialObjectIdBase); Assert.Null(fixture.Session.EstablishedMtu); Assert.Equal(3UL,fixture.Allocator.AllocatedBlockCount); // Original server/client, fresh server. var retransmitted=fixture.Session.Receive(fixture.Packet(Seq(502),Start()),false); Assert.Equal(accepted[0],retransmitted[0]);Assert.Equal(accepted[1],retransmitted[1]); Assert.Equal(3UL,fixture.Allocator.AllocatedBlockCount); Assert.Empty(fixture.Session.Receive(fixture.Packet(Seq(502),Start(wrongAuthentication:true)),false)); Assert.Equal("changed-reliable-duplicate",fixture.Session.LastControlFailure); Assert.Single(fixture.Session.Receive(fixture.Packet(Seq(503),MtuReply()),false)); Assert.Equal((ushort)1360,fixture.Session.EstablishedMtu);Assert.Equal("",fixture.Session.LastControlFailure); } [Fact]public void HardResynchronizationDropsOldWindowsAndRequiresANewAcknowledgedTransport() { using var fixture=new Fixture(); Assert.Equal(2,fixture.Session.Receive(fixture.Packet(Seq(102),MtuReply(),Id()),false).Count); var firstBase=fixture.Session.InitialObjectIdBase;Assert.Equal(1,fixture.Session.PendingReliable); var sync=Fixture.Sync with{Sequence=501}; var reply=Assert.Single(fixture.Session.Receive(EdServerControlMessageCodec.EncodeSyncLetter(sync),false));var p=0; var parsed=EdServerControlMessageCodec.ReadSyncLetter(reply,ref p,33837); Assert.Equal(501,parsed.Acknowledgement);Assert.NotEqual(fixture.ServerSequence,parsed.Sequence); Assert.Equal(1,fixture.Session.TransportRestarts);Assert.Null(fixture.Session.EstablishedMtu); Assert.Equal(firstBase,fixture.Session.InitialObjectIdBase);Assert.Equal(0,fixture.Session.PendingReliable); Assert.Equal(EdServerHandshakeState.AwaitingAcknowledgement,fixture.Session.State); Assert.Single(fixture.Session.Receive(EdServerControlMessageCodec.EncodeSyncLetter(sync),false)); Assert.Equal(1,fixture.Session.TransportRestarts); Assert.Empty(fixture.Session.Receive(fixture.Packet(Ack(fixture.ServerSequence)),false)); Assert.Equal(EdServerHandshakeState.AwaitingAcknowledgement,fixture.Session.State); Assert.Empty(fixture.Session.Receive(fixture.Packet(Ack(parsed.Sequence)),false)); Assert.Equal(EdServerHandshakeState.Established,fixture.Session.State); Assert.Equal(2,fixture.Session.Receive(fixture.Packet(Seq(502),MtuReply(),Id()),false).Count); Assert.Equal(firstBase,fixture.Session.InitialObjectIdBase);Assert.Equal(2UL,fixture.Allocator.AllocatedBlockCount); } [Fact]public void InvalidRestartCannotClearAValidWindowOrConsumeAnObjectBlock() { using var fixture=new Fixture();fixture.Session.Receive(fixture.Packet(Seq(102),MtuReply(),Id()),false); var originalBase=fixture.Session.InitialObjectIdBase;var pending=fixture.Session.PendingReliable; foreach(var changed in new[]{Fixture.Sync with{Sequence=501,BuildRevision=1},Fixture.Sync with{Sequence=501,IsClient=false}, Fixture.Sync with{Sequence=0},Fixture.Sync with{Sequence=501,EncryptedKeyBytes=new byte[]{1}}}) { Assert.Empty(fixture.Session.Receive(EdServerControlMessageCodec.EncodeSyncLetter(changed),false)); Assert.Equal(EdServerHandshakeState.Established,fixture.Session.State); Assert.Equal(originalBase,fixture.Session.InitialObjectIdBase);Assert.Equal(pending,fixture.Session.PendingReliable); Assert.Equal(0,fixture.Session.TransportRestarts);Assert.Equal(1UL,fixture.Allocator.AllocatedBlockCount); } Assert.Equal(2,fixture.Session.Receive(fixture.Packet(Seq(103),Id()),false).Count); } [Theory] [InlineData(false,0)] [InlineData(true,3)] public void OneSequenceIdentifiesWholeReliableGroupAndRetryIsDeduplicated(bool framed,byte alignment) { using var fixture=new Fixture(framed,alignment); var batch=fixture.Packet(Seq(102),MtuReply(),MtuReply(),Id()); var first=fixture.Session.Receive(batch,false);Assert.Equal(2,first.Count);Assert.Equal((ushort)1360,fixture.Session.EstablishedMtu!.Value); Assert.Equal(1UL,fixture.Allocator.AllocatedBlockCount); var p=16;Assert.Equal((ushort)102,EdServerControlMessageCodec.ReadAckBody(first[1],ref p).Sequence); var retry=fixture.Session.Receive(batch,false);Assert.Equal(first[0],retry[0]);Assert.Equal(first[1],retry[1]); Assert.Empty(fixture.Session.Receive(fixture.Packet(Seq(102),MtuReply(),Id()),false)); // A partial replay changes the group. Assert.Equal(1UL,fixture.Allocator.AllocatedBlockCount); Assert.Equal(2,fixture.Session.Receive(fixture.Packet(Seq(103),Id()),false).Count);Assert.Equal(2UL,fixture.Allocator.AllocatedBlockCount); } [Theory] [InlineData(false,0)] [InlineData(true,3)] public void RouteMetadataAndMtuCanSharePrefixAndMalformedTailCannotCommitAllocation(bool framed,byte alignment) { using var fixture=new Fixture(framed,alignment); var route=Route();var replies=fixture.Session.Receive(fixture.Packet(Seq(102),MtuReply(),route),false); Assert.Single(replies);Assert.Equal(1,fixture.Session.AcceptedRoutes);Assert.Equal((ushort)1360,fixture.Session.EstablishedMtu!.Value); Assert.Single(fixture.Session.Receive(fixture.Packet(Seq(102),MtuReply(),route),false));Assert.Equal(1,fixture.Session.AcceptedRoutes); var malformed=fixture.Packet(Seq(103),Id(),route);Array.Resize(ref malformed,malformed.Length-3); Assert.Empty(fixture.Session.Receive(malformed,false));Assert.Equal(0UL,fixture.Allocator.AllocatedBlockCount); Assert.Equal(2,fixture.Session.Receive(fixture.Packet(Seq(103),Id()),false).Count); } [Theory] [InlineData(false,0)] [InlineData(true,3)] public void ExplicitGroupsAdvanceOnceAndCanBeRetriedTogetherOrSeparately(bool framed,byte alignment) { using var fixture=new Fixture(framed,alignment); var original=fixture.Session.Receive(fixture.Packet(Seq(102),MtuReply(),Route(),Seq(103),MtuReply(),MtuReply()),false); Assert.Single(original);var bit=16;Assert.Equal((ushort)103,EdServerControlMessageCodec.ReadAckBody(original[0],ref bit).Sequence); Assert.Single(fixture.Session.Receive(fixture.Packet(Seq(102),MtuReply(),Route()),false)); Assert.Single(fixture.Session.Receive(fixture.Packet(Seq(103),MtuReply(),MtuReply()),false)); Assert.Equal(1,fixture.Session.AcceptedRoutes); var next=fixture.Session.Receive(fixture.Packet(Seq(102),MtuReply(),Route(),Seq(104),Id()),false); Assert.Equal(2,next.Count);Assert.Equal(1UL,fixture.Allocator.AllocatedBlockCount); Assert.Empty(fixture.Session.Receive(fixture.Packet(Seq(105),Id(),Seq(105),Id()),false)); Assert.Equal(1UL,fixture.Allocator.AllocatedBlockCount); } [Fact] public void RouteCodecIsBoundedUnalignedAndTruncationDoesNotAdvanceCaller() { var encoded=Route();var p=11;var metadata=EdServerRouteMessageCodec.ReadBody(encoded.Item1,ref p); Assert.Equal(123UL,metadata.MachineRunId);Assert.Equal(1,metadata.EndpointCount);Assert.Equal(new[]{"Comp","IP4"},metadata.Schemes); Assert.Equal(11+encoded.Item2,p); for(var count=0;count<(p+7)/8;count++) { var prefix=encoded.Item1.AsSpan(0,count).ToArray();var position=11; Assert.Throws(()=>EdServerRouteMessageCodec.ReadBody(prefix,ref position));Assert.Equal(11,position); } } [Fact] public void RelayRouteMarkerHasNoBodyAndLeavesFollowingFieldUntouched() { var bytes=new byte[64];var bit=3; void Bits(ulong value,int width){for(var index=0;index (EdServerControlMessageCodec.EncodeAckLetter(new((ushort)sequence, 0, 16), 11), 32); private static (byte[], int) Seq(ushort sequence) { var data = new byte[4]; var p = 0; EdServerControlMessageCodec.WriteSequenceLetter(data, ref p, sequence, 11); return (data, 16); } private static (byte[], int) Id() => (Type((byte)EdServerBuiltInLetter.IdAllocationRequest), 0); private static (byte[],int) Start(bool wrongAuthentication=false) { using var value=new EdServerStartServerAdmin([],"synthetic-machine"u8, wrongAuthentication?"wrong-auth"u8:"synthetic-auth"u8,[],false); var data=new byte[256];var p=0;EdServerControlMessageCodec.WriteLetterType(data,ref p,26,11); var beginning=p;EdServerAdminMessageCodec.WriteStartBody(data,ref p,value); return(data,p-beginning); } private static byte[] Type(byte type) { var data = new byte[2]; var p = 0; EdServerControlMessageCodec.WriteLetterType(data, ref p, type, 11); return data; } private static (byte[], int) MtuTest() { var data = new byte[6]; var p = 0; EdServerControlMessageCodec.WriteLetterType(data, ref p, (byte)EdServerBuiltInLetter.MtuTest, 11); EdServerMtuMessageCodec.WriteTest(data, ref p, 1360, []); return (data, 32); } private static (byte[], int) MtuReply() { var data = new byte[4]; var p = 0; EdServerControlMessageCodec.WriteLetterType(data, ref p, (byte)EdServerBuiltInLetter.MtuReply, 11); EdServerMtuMessageCodec.WriteReply(data, ref p, 1360); return (data, 16); } private static (byte[],int) Route() { var data=new byte[64];var p=0;EdServerControlMessageCodec.WriteLetterType(data,ref p,(byte)EdServerBuiltInLetter.Route,11); var beginning=p;void Bits(ulong value,int width){for(var index=0;index new(33837, 101, 0, 11, 0x3F800000, false, 0, 0x42A9, "synthetic"u8.ToArray(), 0x7F000001, 0, ReadOnlyMemory.Empty, false, true, 332841, 332841); public ManualClock Clock { get; } = new(); public NativeObjectIdAllocator Allocator { get; } = new(); public LocalReplicationSession Session { get; } public ushort ServerSequence { get; } private readonly bool framed; private readonly byte alignment; public Fixture(bool framed = false, byte alignment = 0,bool withApplication=false,Func? resolveRoute=null) { (this.framed, this.alignment) = (framed, alignment); var app=withApplication?new LocalReplicationApplication(11,12,Allocator.AllocateBlock("management-fixture"), System.Security.Cryptography.SHA256.HashData("synthetic-machine"u8), System.Security.Cryptography.SHA256.HashData("synthetic-auth"u8)):null; Session = new(new(IPAddress.Loopback, 1234), Allocator, "local-control-fixture", Clock,application:app,resolveRoute:resolveRoute); var response = Assert.Single(Session.Receive(EdServerControlMessageCodec.EncodeSyncLetter(Sync with { LengthDelimited = framed, AlignmentMode = alignment }), false)); var p = 0; ServerSequence = EdServerControlMessageCodec.ReadSyncLetter(response, ref p, 33837).Sequence; Assert.Empty(Session.Receive(Packet(Ack(ServerSequence)), false)); Assert.Equal(EdServerHandshakeState.Established, Session.State); } public byte[] Packet(params (byte[] Data, int Bits)[] messages) { var result = new byte[512]; var p = 0; foreach (var (data, bits) in messages) { var source = 0; var type = EdServerControlMessageCodec.ReadLetterType(data, ref source, 11); EdServerControlMessageCodec.WriteLetterType(result, ref p, type, 11); var frame = EdServerLetterFramingCodec.BeginWrite(result, ref p, type, framed, alignment); for (var i = 0; i < bits; i++, source++, p++) if ((data[source / 8] & (1 << (source % 8))) != 0) result[p / 8] |= (byte)(1 << (p % 8)); EdServerLetterFramingCodec.CompleteWrite(result, ref p, frame); } return result[..((p + 7) / 8)]; } public void Dispose() => Session.Dispose(); } private sealed class ManualClock : TimeProvider { private long timestamp; public override long TimestampFrequency => TimeSpan.TicksPerSecond; public override long GetTimestamp() => timestamp; public void Advance(TimeSpan value) => timestamp += value.Ticks; } }