private bool ConnectToPeer(PeerAddressManager peerAddressManager, Mock <INetworkPeerFactory> networkPeerFactoryExisting, ConnectionManagerSettings connectionManagerSettingsExisting, PeerConnectorConnectNode peerConnector, IPEndPoint endpointNode, Mock <IConnectionManager> mockConnectionManager) { peerAddressManager.AddPeer(endpointNode, IPAddress.Loopback); var networkPeer = new Mock <INetworkPeer>(); networkPeer.SetupGet(np => np.PeerEndPoint).Returns(new IPEndPoint(endpointNode.Address, endpointNode.Port)); networkPeer.SetupGet(np => np.RemoteSocketAddress).Returns(endpointNode.Address); networkPeer.SetupGet(np => np.RemoteSocketPort).Returns(endpointNode.Port); networkPeer.SetupGet(np => np.State).Returns(NetworkPeerState.HandShaked); networkPeerFactoryExisting.Setup(npf => npf.CreateConnectedNetworkPeerAsync(It.Is <IPEndPoint>(x => Equals(x, endpointNode)), It.IsAny <NetworkPeerConnectionParameters>(), It.IsAny <NetworkPeerDisposer>())).Returns(Task.FromResult(networkPeer.Object)); connectionManagerSettingsExisting.AddNode.Add(endpointNode); connectionManagerSettingsExisting.Connect.Add(endpointNode); NetworkPeerCollection connectedPeers = (NetworkPeerCollection)mockConnectionManager.Object.ConnectedPeers; foreach (INetworkPeer peer in peerConnector.ConnectorPeers) { if (!connectedPeers.Contains(peer)) { connectedPeers.Add(peer); } } mockConnectionManager.SetupGet(np => np.ConnectedPeers).Returns(connectedPeers); peerConnector.Initialize(mockConnectionManager.Object); peerConnector.OnConnectAsync().GetAwaiter().GetResult(); return(peerConnector.ConnectorPeers.Select(p => p.PeerEndPoint).Contains(endpointNode)); }
private PeerConnectorConnectNode CreatePeerConnectorConnectNode(NodeSettings nodeSettings, ConnectionManagerSettings connectionSettings, IPeerAddressManager peerAddressManager) { var peerConnector = new PeerConnectorConnectNode(this.asyncLoopFactory, DateTimeProvider.Default, this.extendedLoggerFactory, this.network, this.networkPeerFactory, this.nodeLifetime, nodeSettings, connectionSettings, peerAddressManager); var connectionManager = CreateConnectionManager(nodeSettings, connectionSettings, peerAddressManager, peerConnector); peerConnector.Initialize(connectionManager); return(peerConnector); }
public void PeerConnectorDiscovery_ConnectsTo_DiscoveredPeers() { var nodeSettings = new NodeSettings(this.Network, args: new[] { "-IpRangeFiltering=false" }); var connectionManagerSettings = new ConnectionManagerSettings(nodeSettings); DataFolder peerFolder = CreateDataFolder(this); var peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.extendedLoggerFactory, new SelfEndpointTracker(this.extendedLoggerFactory, connectionManagerSettings)); IPAddress ipAddressAdd = IPAddress.Parse("::ffff:192.168.0.1"); var endpointAddNode = new IPEndPoint(ipAddressAdd, 80); IPAddress ipAddressConnect = IPAddress.Parse("::ffff:192.168.0.2"); var endpointConnectNode = new IPEndPoint(ipAddressConnect, 80); IPAddress ipAddressDiscovered = IPAddress.Parse("::ffff:192.168.0.3"); var endpointDiscoveredNode = new IPEndPoint(ipAddressDiscovered, 80); peerAddressManager.AddPeer(endpointAddNode, IPAddress.Loopback); peerAddressManager.AddPeer(endpointConnectNode, IPAddress.Loopback); peerAddressManager.AddPeer(endpointDiscoveredNode, IPAddress.Loopback); connectionManagerSettings.AddAddNode(endpointAddNode); connectionManagerSettings.Connect.Add(endpointConnectNode); var networkPeer = new Mock <INetworkPeer>(); networkPeer.SetupGet(np => np.PeerEndPoint).Returns(new IPEndPoint(ipAddressDiscovered, 80)); networkPeer.SetupGet(np => np.RemoteSocketAddress).Returns(ipAddressDiscovered); networkPeer.SetupGet(np => np.RemoteSocketPort).Returns(80); networkPeer.SetupGet(np => np.State).Returns(NetworkPeerState.HandShaked); var networkPeerFactory = new Mock <INetworkPeerFactory>(); networkPeerFactory.Setup(npf => npf.CreateConnectedNetworkPeerAsync(It.IsAny <IPEndPoint>(), It.IsAny <NetworkPeerConnectionParameters>(), It.IsAny <NetworkPeerDisposer>())).Returns(Task.FromResult(networkPeer.Object)); var peerConnector = new PeerConnectorConnectNode(this.asyncProvider, DateTimeProvider.Default, this.extendedLoggerFactory, this.Network, networkPeerFactory.Object, this.nodeLifetime, nodeSettings, connectionManagerSettings, peerAddressManager, new SelfEndpointTracker(this.extendedLoggerFactory, connectionManagerSettings)); var selfEndpointTracker = new Mock <ISelfEndpointTracker>(); IConnectionManager connectionManager = CreateConnectionManager(nodeSettings, connectionManagerSettings, peerAddressManager, peerConnector, selfEndpointTracker.Object); peerConnector.Initialize(connectionManager); peerConnector.OnConnectAsync().GetAwaiter().GetResult(); Assert.DoesNotContain(endpointAddNode, peerConnector.ConnectorPeers.Select(p => p.PeerEndPoint)); Assert.DoesNotContain(endpointConnectNode, peerConnector.ConnectorPeers.Select(p => p.PeerEndPoint)); Assert.Contains(endpointDiscoveredNode, peerConnector.ConnectorPeers.Select(p => p.PeerEndPoint)); }
public void PeerConnectorDiscovery_ConnectsTo_DiscoveredPeers() { var peerFolder = AssureEmptyDirAsDataFolder(Path.Combine(AppContext.BaseDirectory, "PeerConnectorTests")); var peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.extendedLoggerFactory); var ipAddressAdd = IPAddress.Parse("::ffff:192.168.0.1"); var endpointAddNode = new IPEndPoint(ipAddressAdd, 80); var ipAddressConnect = IPAddress.Parse("::ffff:192.168.0.2"); var endpointConnectNode = new IPEndPoint(ipAddressConnect, 80); var ipAddressDiscovered = IPAddress.Parse("::ffff:192.168.0.3"); var endpointDiscoveredNode = new IPEndPoint(ipAddressDiscovered, 80); peerAddressManager.AddPeer(endpointAddNode, IPAddress.Loopback); peerAddressManager.AddPeer(endpointConnectNode, IPAddress.Loopback); peerAddressManager.AddPeer(endpointDiscoveredNode, IPAddress.Loopback); var nodeSettings = new NodeSettings(); var connectionManagerSettings = new ConnectionManagerSettings(); connectionManagerSettings.Load(nodeSettings); connectionManagerSettings.AddNode.Add(endpointAddNode); connectionManagerSettings.Connect.Add(endpointConnectNode); var networkPeer = new Mock <INetworkPeer>(); networkPeer.SetupGet(np => np.PeerEndPoint).Returns(new IPEndPoint(ipAddressDiscovered, 80)); networkPeer.SetupGet(np => np.RemoteSocketAddress).Returns(ipAddressDiscovered); networkPeer.SetupGet(np => np.RemoteSocketPort).Returns(80); networkPeer.SetupGet(np => np.State).Returns(NetworkPeerState.HandShaked); var networkPeerFactory = new Mock <INetworkPeerFactory>(); networkPeerFactory.Setup(npf => npf.CreateConnectedNetworkPeerAsync(It.IsAny <Network>(), It.IsAny <IPEndPoint>(), It.IsAny <NetworkPeerConnectionParameters>())).Returns(Task.FromResult(networkPeer.Object)); var peerConnector = new PeerConnectorConnectNode(this.asyncLoopFactory, DateTimeProvider.Default, this.extendedLoggerFactory, this.network, networkPeerFactory.Object, this.nodeLifetime, nodeSettings, connectionManagerSettings, peerAddressManager); var connectionManager = CreateConnectionManager(nodeSettings, connectionManagerSettings, peerAddressManager, peerConnector); peerConnector.Initialize(connectionManager); peerConnector.OnConnectAsync().GetAwaiter().GetResult(); Assert.DoesNotContain(endpointAddNode, peerConnector.ConnectedPeers.Select(p => p.PeerEndPoint)); Assert.DoesNotContain(endpointConnectNode, peerConnector.ConnectedPeers.Select(p => p.PeerEndPoint)); Assert.Contains(endpointDiscoveredNode, peerConnector.ConnectedPeers.Select(p => p.PeerEndPoint)); }
public void PeerConnectorDiscovery_ConnectsTo_LocalNodes_IpRangeFilteringEnabled() { DataFolder peerFolder = CreateDataFolder(this); var peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.extendedLoggerFactory, new SelfEndpointTracker(this.extendedLoggerFactory)); var nodeSettings = new NodeSettings(this.Network, args: new[] { string.Empty }); // Ip range filtering enabled by default. var connectionManagerSettingsExisting = new ConnectionManagerSettings(nodeSettings); Mock <INetworkPeerFactory> networkPeerFactoryExisting = new Mock <INetworkPeerFactory>(); Mock <IConnectionManager> connectionManagerExisting = new Mock <IConnectionManager>(); var networkPeerParameters = new NetworkPeerConnectionParameters(); networkPeerParameters.TemplateBehaviors.Add(new ConnectionManagerBehavior(connectionManagerExisting.Object, this.extendedLoggerFactory)); connectionManagerExisting.SetupGet(np => np.Parameters).Returns(networkPeerParameters); connectionManagerExisting.SetupGet(np => np.ConnectedPeers).Returns(new NetworkPeerCollection()); var peerConnector = new PeerConnectorConnectNode(this.asyncLoopFactory, DateTimeProvider.Default, this.extendedLoggerFactory, this.Network, networkPeerFactoryExisting.Object, this.nodeLifetime, nodeSettings, connectionManagerSettingsExisting, peerAddressManager, new SelfEndpointTracker(this.extendedLoggerFactory)); peerConnector.Initialize(connectionManagerExisting.Object); // Peer 1 is a local address. IPAddress originalAddressPeer1 = IPAddress.Parse("::ffff:192.168.0.1"); // ipv4 var endpointPeer1 = new IPEndPoint(originalAddressPeer1, 80); bool connectedToThisPeer = ConnectToPeer(peerAddressManager, networkPeerFactoryExisting, connectionManagerSettingsExisting, peerConnector, endpointPeer1, connectionManagerExisting); Assert.True(connectedToThisPeer); // Peer 2 is a local address in a different group. IPAddress addressInDifferentNetworkGroupPeer2 = IPAddress.Parse("::ffff:192.168.1.1"); // ipv4 var endpointPeer2 = new IPEndPoint(addressInDifferentNetworkGroupPeer2, 80); connectedToThisPeer = ConnectToPeer(peerAddressManager, networkPeerFactoryExisting, connectionManagerSettingsExisting, peerConnector, endpointPeer2, connectionManagerExisting); Assert.True(connectedToThisPeer); // Peer 3 is a local address in a different group. IPAddress addressInDifferentNetworkGroupPeer3 = IPAddress.Parse("0:0:0:0:0:ffff:c0a8:101"); // ipv6 var endpointPeer3 = new IPEndPoint(addressInDifferentNetworkGroupPeer3, 80); connectedToThisPeer = ConnectToPeer(peerAddressManager, networkPeerFactoryExisting, connectionManagerSettingsExisting, peerConnector, endpointPeer3, connectionManagerExisting); Assert.True(connectedToThisPeer); }
public void PeerConnectorDiscovery_DoesNotConnectTo_NodeInSameNetworkGroup_WithIpRangeFilteringEnabled() { DataFolder peerFolder = CreateDataFolder(this); var peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.extendedLoggerFactory, new SelfEndpointTracker(this.extendedLoggerFactory)); // IpRangeFiltering enabled by default. var nodeSettings = new NodeSettings(this.Network, args: new[] { "-IpRangeFiltering" }); var connectionManagerSettingsExisting = new ConnectionManagerSettings(nodeSettings); Mock <INetworkPeerFactory> networkPeerFactoryExisting = new Mock <INetworkPeerFactory>(); Mock <IConnectionManager> connectionManagerExisting = new Mock <IConnectionManager>(); var networkPeerParameters = new NetworkPeerConnectionParameters(); networkPeerParameters.TemplateBehaviors.Add(new ConnectionManagerBehavior(connectionManagerExisting.Object, this.extendedLoggerFactory)); connectionManagerExisting.SetupGet(np => np.Parameters).Returns(networkPeerParameters); connectionManagerExisting.SetupGet(np => np.ConnectedPeers).Returns(new NetworkPeerCollection()); var peerConnector = new PeerConnectorConnectNode(this.asyncLoopFactory, DateTimeProvider.Default, this.extendedLoggerFactory, this.Network, networkPeerFactoryExisting.Object, this.nodeLifetime, nodeSettings, connectionManagerSettingsExisting, peerAddressManager, new SelfEndpointTracker(this.extendedLoggerFactory)); peerConnector.Initialize(connectionManagerExisting.Object); // Peer 1. IPAddress originalAddressPeer1 = IPAddress.Parse("::ffff:57.48.183.81"); // ipv4 var endpointPeer1 = new IPEndPoint(originalAddressPeer1, 80); peerAddressManager.AddPeer(endpointPeer1, IPAddress.Loopback); bool connectedToThisPeer = ConnectToPeer(peerAddressManager, networkPeerFactoryExisting, connectionManagerSettingsExisting, peerConnector, endpointPeer1, connectionManagerExisting); Assert.True(connectedToThisPeer); // Peer 2 has different network group to Peer 1. IPAddress addressInDifferentNetworkGroupPeer2 = IPAddress.Parse("99be:f5c5:adc2:525c:f6d7:7b30:5336:5a0f"); // ipv6 var endpointPeer2 = new IPEndPoint(addressInDifferentNetworkGroupPeer2, 80); peerAddressManager.AddPeer(endpointPeer2, IPAddress.Loopback); connectedToThisPeer = ConnectToPeer(peerAddressManager, networkPeerFactoryExisting, connectionManagerSettingsExisting, peerConnector, endpointPeer2, connectionManagerExisting); Assert.True(connectedToThisPeer); // Different network group: connects. // Peer 3 in same network group as Peer 2. IPAddress addressInSameNetworkGroupPeer3 = IPAddress.Parse("99be:f5c5:adc2:525c:db45:d36e:ce01:a394"); // ipv6 var endpointPeer3 = new IPEndPoint(addressInSameNetworkGroupPeer3, 80); peerAddressManager.AddPeer(endpointPeer3, IPAddress.Loopback); connectedToThisPeer = ConnectToPeer(peerAddressManager, networkPeerFactoryExisting, connectionManagerSettingsExisting, peerConnector, endpointPeer3, connectionManagerExisting); Assert.False(connectedToThisPeer); // Same network group: does not connect. // Peer 4 has different network group to Peer 1. IPAddress addressInDifferentNetworkGroupPeer4 = IPAddress.Parse("::ffff:58.48.183.81"); // ipv4 var endpointPeer4 = new IPEndPoint(addressInDifferentNetworkGroupPeer4, 80); peerAddressManager.AddPeer(endpointPeer4, IPAddress.Loopback); connectedToThisPeer = ConnectToPeer(peerAddressManager, networkPeerFactoryExisting, connectionManagerSettingsExisting, peerConnector, endpointPeer4, connectionManagerExisting); Assert.True(connectedToThisPeer); // Different network group: connects. // Peer 5 has same network group as Peer 1. IPAddress addressInSameNetworkGroupPeer5 = IPAddress.Parse("::ffff:57.48.183.82"); // ipv4 var endpointPeer5 = new IPEndPoint(addressInSameNetworkGroupPeer5, 80); peerAddressManager.AddPeer(endpointPeer5, IPAddress.Loopback); connectedToThisPeer = ConnectToPeer(peerAddressManager, networkPeerFactoryExisting, connectionManagerSettingsExisting, peerConnector, endpointPeer5, connectionManagerExisting); Assert.False(connectedToThisPeer); // Same network group: does not connect. }