Ejemplo n.º 1
0
        public Datagram SendDatagram(EndPoint destinationEndPoint, params byte[] buffer)
        {
            var datagram = Datagram.Create(destinationEndPoint, buffer);

            this.Send(destinationEndPoint, datagram.ToBytes());
            return(datagram);
        }
Ejemplo n.º 2
0
        public Datagram StageReceivedDatagram(EndPoint source, EndPoint relayToDestination, params byte[] packet)
        {
            var datagram = Datagram.Create(relayToDestination, packet);

            this.StageReceivedPacket(source, datagram.ToBytes());
            return(datagram);
        }
Ejemplo n.º 3
0
        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);
            });
        }
Ejemplo n.º 4
0
        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();
            });
        }
Ejemplo n.º 5
0
        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();
            });
        }