Ejemplo n.º 1
0
        internal Mock <INetworkPeer> GetNetworkPeerWithConnection()
        {
            var networkPeer = new Mock <INetworkPeer>();

            var connection = new NetworkPeerConnection(this.Network, networkPeer.Object, new TcpClient(), 0, (message, token) => Task.CompletedTask,
                                                       this.dateTimeProvider, this.loggerFactory, new PayloadProvider().DiscoverPayloads());

            networkPeer.Setup(n => n.Connection)
            .Returns(connection);

            networkPeer.Setup(n => n.PeerEndPoint)
            .Returns(new System.Net.IPEndPoint(IPAddress.Loopback, 9999));

            networkPeer.Setup(n => n.RemoteSocketAddress)
            .Returns(IPAddress.Loopback.EnsureIPv6());
            networkPeer.Setup(n => n.RemoteSocketPort)
            .Returns(9999);

            networkPeer.Setup(n => n.RemoteSocketEndpoint)
            .Returns(new System.Net.IPEndPoint(IPAddress.Loopback.EnsureIPv6(), 9999));

            networkPeer.Setup(n => n.State)
            .Returns(NetworkPeerState.Connected);

            var behavior = new Mock <IConnectionManagerBehavior>();

            networkPeer.Setup(n => n.Behavior <IConnectionManagerBehavior>())
            .Returns(behavior.Object);

            this.peerAddressManager.AddPeer(networkPeer.Object.PeerEndPoint, networkPeer.Object.PeerEndPoint.Address);
            this.connectionManager.AddConnectedPeer(networkPeer.Object);

            return(networkPeer);
        }
Ejemplo n.º 2
0
        /// <summary>Creates a peer with extended puller behavior.</summary>
        public INetworkPeer CreatePeer(out ExtendedBlockPullerBehavior mockedBehavior, bool notSupportedVersion = false)
        {
            var peer = new Mock <INetworkPeer>();

            var connection = new NetworkPeerConnection(Network.StratisMain, peer.Object, new TcpClient(), this.currentPeerId, (message, token) => Task.CompletedTask,
                                                       new DateTimeProvider(), this.loggerFactory, new PayloadProvider());

            this.currentPeerId++;
            peer.SetupGet(networkPeer => networkPeer.Connection).Returns(connection);

            var            connectionParameters = new NetworkPeerConnectionParameters();
            VersionPayload version = connectionParameters.CreateVersion(new IPEndPoint(1, 1), Network.StratisMain, new DateTimeProvider().GetTimeOffset());

            if (notSupportedVersion)
            {
                version.Version = ProtocolVersion.NOBLKS_VERSION_START;
            }
            else
            {
                version.Services = NetworkPeerServices.Network;
            }

            peer.SetupGet(x => x.PeerVersion).Returns(version);
            peer.SetupGet(x => x.State).Returns(NetworkPeerState.HandShaked);
            peer.SetupGet(x => x.MessageReceived).Returns(new AsyncExecutionEvent <INetworkPeer, IncomingMessage>());

            ExtendedBlockPullerBehavior behavior = this.CreateBlockPullerBehavior();

            behavior.Attach(peer.Object);
            peer.Setup(x => x.Behavior <IBlockPullerBehavior>()).Returns(() => behavior);

            mockedBehavior = behavior;
            return(peer.Object);
        }
        private Mock <INetworkPeer> CreatePeerMock()
        {
            var peer = new Mock <INetworkPeer>();

            var connection = new NetworkPeerConnection(KnownNetworks.StratisMain, peer.Object, new TcpClient(), 0, (message, token) => Task.CompletedTask,
                                                       new DateTimeProvider(), this.loggerFactory, new PayloadProvider());

            peer.SetupGet(networkPeer => networkPeer.Connection).Returns(connection);

            var            connectionParameters = new NetworkPeerConnectionParameters();
            VersionPayload version = connectionParameters.CreateVersion(new IPEndPoint(1, 1), new IPEndPoint(1, 1), KnownNetworks.StratisMain, new DateTimeProvider().GetTimeOffset());

            version.Services = NetworkPeerServices.Network;

            peer.SetupGet(x => x.PeerVersion).Returns(version);
            peer.SetupGet(x => x.State).Returns(NetworkPeerState.HandShaked);

            this.StateChanged    = new AsyncExecutionEvent <INetworkPeer, NetworkPeerState>();
            this.MessageReceived = new AsyncExecutionEvent <INetworkPeer, IncomingMessage>();

            peer.Setup(x => x.StateChanged).Returns(() => this.StateChanged);
            peer.Setup(x => x.MessageReceived).Returns(() => this.MessageReceived);

            var connectionManagerBehaviorMock = new Mock <IConnectionManagerBehavior>();

            connectionManagerBehaviorMock.Setup(x => x.Whitelisted).Returns(this.IsPeerWhitelisted);

            peer.Setup(x => x.Behavior <IConnectionManagerBehavior>()).Returns(() => connectionManagerBehaviorMock.Object);

            peer.SetupGet(x => x.PeerEndPoint).Returns(new IPEndPoint(1, 1));

            return(peer);
        }