public async Task Random_packets_sent_only_before_first_packet_to_first_destination() { var settings = new RandomFirstSessionPrefixInterceptor.Settings { CountMin = 2, CountMax = 2 }; var interceptor = new RandomFirstSessionPrefixInterceptor(this._bufferPool, settings, new NullLoggerFactory()); var client = new FakeUdpClient(); var destinationEndPoint = FakeEndPoints.CreateRemote(); var payload = new ArraySegment <byte>(new byte[] { 1, 2, 3, 4 }); var isSendingHandledByInterceptor = await interceptor .TryInterceptOutgoingAsync(payload, destinationEndPoint, client) .ConfigureAwait(true); isSendingHandledByInterceptor.Should().BeFalse(); payload = new ArraySegment <byte>(new byte[] { 5, 6, 7, 8 }); isSendingHandledByInterceptor = await interceptor .TryInterceptOutgoingAsync(payload, destinationEndPoint, client) .ConfigureAwait(true); isSendingHandledByInterceptor.Should().BeFalse(); var packets = client.WaitForSentPackets(2); packets[0].Packet.Length.Should().BeInRange(settings.BytesMin, settings.BytesMax); packets[1].Packet.Length.Should().BeInRange(settings.BytesMin, settings.BytesMax); }
public void Proxy_reassembles_incoming_datagrams() { var client = FakeEndPoints.CreateLocal(); var remote = FakeEndPoints.CreateRemote(); var boundUdpClientStagedReceivedDatagram1 = new Datagram( new DatagramHeader(1, false, remote), new ArraySegment <byte>(new byte[] { 1, 2, 3, 4 })); var boundUdpClientStagedReceivedDatagram2 = new Datagram( new DatagramHeader(2, true, remote), new ArraySegment <byte>(new byte[] { 5, 6, 7, 8 })); this._context.BoundUdpClient.StageReceivedPacket(client, boundUdpClientStagedReceivedDatagram1.ToBytes()); this._context.BoundUdpClient.StageReceivedPacket(client, boundUdpClientStagedReceivedDatagram2.ToBytes()); this._context.RunProxy( () => { var relayClientSentPackets = this._context.RelayingUdpClient.WaitForSentPackets(1) .Select(x => x.Packet).ToList(); relayClientSentPackets.Should().HaveCount(1); relayClientSentPackets[0].Should().Equal(1, 2, 3, 4, 5, 6, 7, 8); A.CallTo( () => this._context.Interceptor.TryInterceptOutgoingAsync( A <ArraySegment <byte> > .That.Matches( x => new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }.SequenceEqual(x)), remote, this._context.RelayingUdpClient)) .MustHaveHappenedOnceExactly(); }); }
public void Proxy_does_not_relay_if_interceptor_returns_true() { var client = FakeEndPoints.CreateLocal(); var remote = FakeEndPoints.CreateRemote(); int incomingCounter = 0; int outgoingCounter = 0; A.CallTo(() => this._context.Interceptor.TryInterceptIncomingAsync(A <Datagram> ._, A <IUdpClient> ._)) .ReturnsLazily(() => Task.FromResult(Interlocked.Increment(ref incomingCounter) % 2 == 1)); A.CallTo( () => this._context.Interceptor.TryInterceptOutgoingAsync( A <ArraySegment <byte> > ._, A <EndPoint> ._, A <IUdpClient> ._)) .ReturnsLazily(() => Task.FromResult(Interlocked.Increment(ref outgoingCounter) % 2 == 1)); this._context.BoundUdpClient.StageReceivedDatagram(client, remote, 10, 9, 8, 7); this._context.BoundUdpClient.StageReceivedDatagram(client, remote, 110, 99, 88, 77); this._context.RelayingUdpClient.StageReceivedPacket(remote, 1, 2, 3, 4); this._context.RelayingUdpClient.StageReceivedPacket(remote, 11, 22, 33, 44); this._context.RunProxy( () => { var boundClientSentPackets = this._context.BoundUdpClient.WaitForSentPackets(1); boundClientSentPackets.Should().HaveCount(1); boundClientSentPackets[0].Packet.Should().Equal(Datagram.Create(remote, 11, 22, 33, 44).ToBytes()); var relayingClientSentPackets = this._context.RelayingUdpClient.WaitForSentPackets(1); relayingClientSentPackets[0].Packet.Should().Equal(110, 99, 88, 77); }); }
public void Proxy_does_NOT_relay_max_UDP_size_incoming_packets() { var client = FakeEndPoints.CreateLocal(); var remote = FakeEndPoints.CreateRemote(); // Must send at least 1 packet to learn the client address and unblock the proxy's receive loop. this._context.BoundUdpClient.StageReceivedDatagram(client, remote, 0); this._context.RelayingUdpClient.StageReceivedPacket(remote, SecureRandom.GetBytes(DatagramHeader.MaxUdpSize)); this._context.RelayingUdpClient.StageReceivedPacket(remote, 1, 2, 3, 4); this._context.RunProxy( () => { var boundClientSentPackets = this._context.BoundUdpClient .WaitForSentPackets(1).Select(x => x.Packet).ToList(); boundClientSentPackets.Should().HaveCount(1); boundClientSentPackets[0].Should().Equal(Datagram.Create(remote, 1, 2, 3, 4).ToBytes()); A.CallTo( () => this._context.Interceptor.TryInterceptIncomingAsync( A <Datagram> .That.Matches(x => new byte[] { 1, 2, 3, 4 }.SequenceEqual(x.Payload)), this._context.BoundUdpClient)) .MustHaveHappenedOnceExactly(); A.CallTo( () => this._context.Interceptor.TryInterceptIncomingAsync( A <Datagram> ._, this._context.BoundUdpClient)) .MustHaveHappenedOnceExactly(); }); }
public void Dispose_aborts_pending_receive() { var client = new FakeUdpClient(); var readTask = client.ReceiveAsync(new byte[1], 0, FakeEndPoints.CreateLocal()); client.Dispose(); readTask.Awaiting(t => t).Should().Throw <Exception>(); }
public async Task Written_packet_can_be_retrieved() { using (var client = new FakeUdpClient()) { var destination = FakeEndPoints.CreateRemote(); var packet = new byte[] { 1, 2, 3, 4, 5 }; await client.SendAsync(packet, 0, packet.Length, destination).ConfigureAwait(true); var received = client.WaitForSentPackets(1).First(); received.Destination.Should().Be(destination); received.Packet.Should().Equal(1, 2, 3, 4, 5); } }
public async Task Staging_packet_makes_it_available_in_receive() { using (var client = new FakeUdpClient()) { var remote = FakeEndPoints.CreateRemote(); client.StageReceivedPacket(remote, 1, 2, 3); var buffer = new byte[10]; var result = await client.ReceiveAsync(buffer, 0, remote).ConfigureAwait(true); result.ReceivedBytes.Should().Be(3); result.RemoteEndPoint.Should().Be(remote); buffer.Take(3).Should().Equal(1, 2, 3); } }
public void Proxy_sends_non_fragmented_packets_in_both_directions() { var client = FakeEndPoints.CreateLocal(); var remote = FakeEndPoints.CreateRemote(); this._context.BoundUdpClient.StageReceivedDatagram(client, remote, 1, 2, 3, 4); var boundUdpClientSentFirstDatagram = this._context.BoundUdpClient.SendDatagram(client, 200, 199, 198, 197, 196); this._context.RelayingUdpClient.StageReceivedPacket(remote, 10, 9, 8, 7); var boundClientExpectedSentSecondDatagram = Datagram.Create(remote, 10, 9, 8, 7); this._context.RelayingUdpClient.Send(remote, 100, 99, 98, 97, 96); this._context.RunProxy( () => { var boundClientSentPackets = this._context.BoundUdpClient.WaitForSentPackets(2) .Select(x => x.Packet).ToList(); boundClientSentPackets.Should().HaveCount(2); boundClientSentPackets[0].Should().Equal(boundUdpClientSentFirstDatagram.ToBytes()); boundClientSentPackets[1].Should().Equal(boundClientExpectedSentSecondDatagram.ToBytes()); A.CallTo( () => this._context.Interceptor.TryInterceptIncomingAsync( A <Datagram> .That.Matches( x => boundClientExpectedSentSecondDatagram.Header == x.Header && boundClientExpectedSentSecondDatagram.Payload.SequenceEqual(x.Payload)), this._context.BoundUdpClient)) .MustHaveHappenedOnceExactly(); var relayClientSentPackets = this._context.RelayingUdpClient.WaitForSentPackets(2) .Select(x => x.Packet).ToList(); relayClientSentPackets.Should().HaveCount(2); relayClientSentPackets[0].Should().Equal(100, 99, 98, 97, 96); relayClientSentPackets[1].Should().Equal(1, 2, 3, 4); A.CallTo( () => this._context.Interceptor.TryInterceptOutgoingAsync( A <ArraySegment <byte> > .That.Matches( x => new byte[] { 1, 2, 3, 4 }.SequenceEqual(x)), remote, this._context.RelayingUdpClient)) .MustHaveHappenedOnceExactly(); }); }
public async Task Proxy_streams_in_both_directions() { using (var clientStream = new LoopbackTcpStream(1, 2, 3, 4, 5)) using (var remoteStream = new LoopbackTcpStream(10, 9, 8, 7, 6)) { var connector = remoteStream.GetConnector(); clientStream.Write(200, 199, 198, 197, 196); remoteStream.Write(100, 99, 98, 97, 96); clientStream.CloseStagedBytesSender(); remoteStream.CloseStagedBytesSender(); using (var proxy = new TcpProxy( clientStream, FakeEndPoints.CreateRemote(), connector, this._bufferPool)) { await proxy.RunAsync(CancellationToken.None).ConfigureAwait(true); } clientStream.GetAllWrittenBytes().Should().Equal(200, 199, 198, 197, 196, 10, 9, 8, 7, 6); remoteStream.GetAllWrittenBytes().Should().Equal(100, 99, 98, 97, 96, 1, 2, 3, 4, 5); } }
public void Trying_to_receive_without_send_or_bind_should_throw() { using (var udpClient = new UdpClient(this._bufferPool)) { udpClient .Awaiting(x => x.ReceiveAsync(new byte[DatagramHeader.MaxUdpSize], 0, FakeEndPoints.CreateRemote())) .Should() .Throw <InvalidOperationException>(); } }