Esempio n. 1
0
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var services       = context.Services;
            var netTestContext = new NetworkTestContext();

            services.AddTransient(provider =>
            {
                var mockService = new Mock <IConnectionService>();
                mockService.Setup(m => m.DoHandshakeAsync(It.IsAny <DnsEndPoint>(), It.IsAny <Handshake>()))
                .Returns <DnsEndPoint, Handshake>((pe, hsk) =>
                {
                    var handshake = NetworkTestHelper.CreateValidHandshake(CryptoHelper.GenerateKeyPair(), 10, hsk.HandshakeData.ChainId);
                    netTestContext.GeneratedHandshakes[pe.Host.ToString()] = handshake;

                    var handShakeReply = Task.FromResult(new HandshakeReply
                    {
                        Error     = HandshakeError.HandshakeOk,
                        Handshake = handshake
                    });
                    return(handShakeReply);
                });

                return(mockService.Object);
            });

            services.AddSingleton <NetworkTestContext>(netTestContext);
        }
Esempio n. 2
0
        public async Task DoHandshake_Test()
        {
            var request = new HandshakeRequest {
                Handshake = new Handshake {
                    HandshakeData = new HandshakeData()
                }
            };
            var context = BuildServerCallContext(null, "ipv4:127.0.0.1:7878");

            //invalid handshake
            var result = await _serverService.DoHandshake(request, context);

            result.Error.ShouldBe(HandshakeError.ChainMismatch);

            request.Handshake.HandshakeData.ChainId = _blockchainService.GetChainId();
            result = await _serverService.DoHandshake(request, context);

            result.Error.ShouldBe(HandshakeError.ProtocolMismatch);

            request.Handshake.HandshakeData.Version = KernelConstants.ProtocolVersion;
            request.Handshake.HandshakeData.Time    = TimestampHelper.GetUtcNow();
            result = await _serverService.DoHandshake(request, context);

            result.Error.ShouldBe(HandshakeError.WrongSignature);

            var peerKeyPair = CryptoHelper.GenerateKeyPair();
            var handshake   = NetworkTestHelper.CreateValidHandshake(peerKeyPair, 10, ChainHelper.ConvertBase58ToChainId("AELF"), 2000);

            request = new HandshakeRequest {
                Handshake = handshake
            };
            result = await _serverService.DoHandshake(request, context);

            result.Error.ShouldBe(HandshakeError.HandshakeOk);
        }
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var netTestHelper = new NetworkTestContext();

            context.Services.AddSingleton(netTestHelper);

            Configure <NetworkOptions>(o => {
                o.ListeningPort = 2001;
                o.MaxPeers      = 2;
            });

            context.Services.AddTransient(o =>
            {
                var mockBlockchainService = new Mock <IBlockchainService>();
                var keyPair = CryptoHelper.GenerateKeyPair();

                mockBlockchainService.Setup(b => b.GetChainAsync()).ReturnsAsync(new Chain
                {
                    Id = NetworkTestConstants.DefaultChainId
                });

                mockBlockchainService.Setup(b => b.GetBlockHeaderByHashAsync(It.IsAny <Hash>())).ReturnsAsync(
                    NetworkTestHelper.CreateFakeBlockHeader(NetworkTestConstants.DefaultChainId, 1, keyPair));

                return(mockBlockchainService.Object);
            });

            context.Services.AddTransient(sp =>
            {
                var mockDialer = new Mock <IPeerDialer>();

                mockDialer.Setup(d => d.DialPeerAsync(It.Is <IPEndPoint>(ip => ip.ToString() == NetworkTestConstants.FakeIpEndpoint)))
                .Returns <IPEndPoint>(s =>
                {
                    var peer = GrpcTestPeerHelpers.CreateBasicPeer(NetworkTestConstants.FakeIpEndpoint, NetworkTestConstants.FakePubkey);
                    netTestHelper.AddDialedPeer(peer);
                    return(Task.FromResult(peer));
                });

                mockDialer.Setup(d => d.DialPeerAsync(It.Is <IPEndPoint>(ip => ip.ToString() == NetworkTestConstants.FakeIpEndpoint2)))
                .Returns <IPEndPoint>(s =>
                {
                    var peer = GrpcTestPeerHelpers.CreateBasicPeer(NetworkTestConstants.FakeIpEndpoint2, NetworkTestConstants.FakePubkey);
                    netTestHelper.AddDialedPeer(peer);
                    return(Task.FromResult(peer));
                });

                mockDialer.Setup(d => d.DialPeerAsync(It.Is <IPEndPoint>(ip => ip.ToString() == NetworkTestConstants
                                                                         .DialExceptionIpEndpoint)))
                .Throws <Exception>();

                mockDialer.Setup(d => d.DialPeerAsync(It.Is <IPEndPoint>(ip => ip.ToString() == NetworkTestConstants.HandshakeWithNetExceptionIp)))
                .Returns <IPEndPoint>(s =>
                {
                    var mockClient = new Mock <PeerService.PeerServiceClient>();
                    mockClient.Setup(m => m.DoHandshakeAsync(It.IsAny <HandshakeRequest>(), It.IsAny <Metadata>(), It.IsAny <DateTime?>(), CancellationToken.None))
                    .Throws(new AggregateException());

                    var peer = GrpcTestPeerHelpers.CreatePeerWithClient(NetworkTestConstants.FakeIpEndpoint2, NetworkTestConstants.FakePubkey,
                                                                        mockClient.Object);

                    netTestHelper.AddDialedPeer(peer);

                    return(Task.FromResult(peer));
                });

                // Incorrect handshake
                mockDialer.Setup(d => d.DialPeerAsync(It.Is <IPEndPoint>(ip => ip.ToString() == NetworkTestConstants.BadHandshakeIp)))
                .Returns <IPEndPoint>((s) =>
                {
                    var handshakeReply = new HandshakeReply();

                    var handshakeCall = TestCalls.AsyncUnaryCall(Task.FromResult(handshakeReply),
                                                                 Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { });

                    var mockClient = new Mock <PeerService.PeerServiceClient>();
                    mockClient.Setup(m => m.DoHandshakeAsync(It.IsAny <HandshakeRequest>(), It.IsAny <Metadata>(), It.IsAny <DateTime?>(),
                                                             CancellationToken.None)).Returns(handshakeCall);
                    mockClient.Setup(m => m.ConfirmHandshakeAsync(It.IsAny <ConfirmHandshakeRequest>(), It.IsAny <Metadata>(), It.IsAny <DateTime?>(),
                                                                  CancellationToken.None)).Throws(new AggregateException());

                    var peer = GrpcTestPeerHelpers.CreatePeerWithClient(NetworkTestConstants.GoodPeerEndpoint,
                                                                        NetworkTestConstants.FakePubkey, mockClient.Object);

                    netTestHelper.AddDialedPeer(peer);

                    return(Task.FromResult(peer));
                });

                // Incorrect handshake signature
                mockDialer.Setup(d => d.DialPeerAsync(It.Is <IPEndPoint>(ip => ip.ToString() == NetworkTestConstants
                                                                         .HandshakeWithDataExceptionIp)))
                .Returns <string>(async(s) =>
                {
                    var handshakeProvider        = context.Services.GetServiceLazy <IHandshakeProvider>().Value;
                    var handshake                = await handshakeProvider.GetHandshakeAsync();
                    handshake.HandshakeData.Time = null;
                    var handshakeReply           = new HandshakeReply {
                        Handshake = handshake
                    };

                    var handshakeCall = TestCalls.AsyncUnaryCall(Task.FromResult(handshakeReply),
                                                                 Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { });

                    var mockClient = new Mock <PeerService.PeerServiceClient>();
                    mockClient.Setup(m => m.DoHandshakeAsync(It.IsAny <HandshakeRequest>(), It.IsAny <Metadata>(), It.IsAny <DateTime?>(),
                                                             CancellationToken.None)).Returns(handshakeCall);

                    var peer = GrpcTestPeerHelpers.CreatePeerWithClient(NetworkTestConstants.GoodPeerEndpoint,
                                                                        NetworkTestConstants.FakePubkey, mockClient.Object);

                    netTestHelper.AddDialedPeer(peer);

                    return(peer);
                });

                // This peer will pass all checks with success.
                mockDialer.Setup(d => d.DialPeerAsync(It.Is <IPEndPoint>(ip => ip.ToString() == NetworkTestConstants.GoodPeerEndpoint)))
                .Returns <IPEndPoint>(s =>
                {
                    var keyPair        = CryptoHelper.GenerateKeyPair();
                    var handshakeReply = new HandshakeReply {
                        Handshake = NetworkTestHelper.CreateValidHandshake(keyPair, 10)
                    };
                    var handshakeCall = TestCalls.AsyncUnaryCall(Task.FromResult(handshakeReply),
                                                                 Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { });
                    var ConfirmHandshakeCall = TestCalls.AsyncUnaryCall(Task.FromResult(new VoidReply()),
                                                                        Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { });

                    var mockClient = new Mock <PeerService.PeerServiceClient>();
                    mockClient.Setup(m => m.DoHandshakeAsync(It.IsAny <HandshakeRequest>(), It.IsAny <Metadata>(), It.IsAny <DateTime?>(),
                                                             CancellationToken.None)).Returns(handshakeCall);

                    mockClient.Setup(m => m.ConfirmHandshakeAsync(It.IsAny <ConfirmHandshakeRequest>(), It.IsAny <Metadata>(), It.IsAny <DateTime?>(),
                                                                  CancellationToken.None)).Returns(ConfirmHandshakeCall);

                    var peer = GrpcTestPeerHelpers.CreatePeerWithClient(NetworkTestConstants.GoodPeerEndpoint,
                                                                        keyPair.PublicKey.ToHex(), mockClient.Object);

                    netTestHelper.AddDialedPeer(peer);

                    return(Task.FromResult(peer));
                });

                return(mockDialer.Object);
            });
        }