public void PeerSelector_ReturnPeersForGetAddrPayload_Scenario3() { var peersToAdd = new List <IPEndPoint>(); for (int i = 1; i <= 15; i++) { IPAddress ipAddress = IPAddress.Parse(string.Format("::ffff:192.168.0.{0}", i)); peersToAdd.Add(new IPEndPoint(ipAddress, 80)); } DataFolder peerFolder = CreateDataFolder(this); PeerAddressManager peerAddressManager = this.CreatePeerAddressManager(peerFolder); peerAddressManager.AddPeers(peersToAdd.ToArray(), IPAddress.Loopback); for (int i = 1; i <= 7; i++) { IPAddress ipAddress = IPAddress.Parse(string.Format("::ffff:192.168.0.{0}", i)); peerAddressManager.PeerAttempted(new IPEndPoint(ipAddress, 80), DateTime.UtcNow.AddHours(-2)); } IEnumerable <PeerAddress> peers = peerAddressManager.PeerSelector.SelectPeersForGetAddrPayload(15); Assert.Equal(15, peers.Count()); Assert.Equal(7, peers.Count(p => p.Attempted)); Assert.Equal(8, peers.Count(p => p.Fresh)); }
public void PeerSelector_ReturnPeersForDiscovery_Scenario1() { var peersToAdd = new List <IPEndPoint>(); for (int i = 1; i <= 5; i++) { var ipAddress = IPAddress.Parse(string.Format("::ffff:192.168.0.{0}", i)); peersToAdd.Add(new IPEndPoint(ipAddress, 80)); } var peerFolder = AssureEmptyDirAsDataFolder(Path.Combine(AppContext.BaseDirectory, "PeerAddressManager")); var peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.extendedLoggerFactory); peerAddressManager.AddPeers(peersToAdd.ToArray(), IPAddress.Loopback); //These peers were all discovered from in the last 24 hours for (int i = 1; i <= 3; i++) { var ipAddress = IPAddress.Parse(string.Format("::ffff:192.168.0.{0}", i)); peerAddressManager.PeerDiscoveredFrom(new IPEndPoint(ipAddress, 80), DateTime.UtcNow.AddHours(-5)); } //These peers were all discovered from more than 24 hours ago for (int i = 4; i <= 5; i++) { var ipAddress = IPAddress.Parse(string.Format("::ffff:192.168.0.{0}", i)); peerAddressManager.PeerDiscoveredFrom(new IPEndPoint(ipAddress, 80), DateTime.UtcNow.AddHours(-25)); } var peers = peerAddressManager.PeerSelector.SelectPeersForDiscovery(5); Assert.Equal(2, peers.Count()); }
public void PeerSelector_ReturnPeersForDiscovery_Scenario1() { var peersToAdd = new List <IPEndPoint>(); for (int i = 1; i <= 5; i++) { IPAddress ipAddress = IPAddress.Parse(string.Format("::ffff:192.168.0.{0}", i)); peersToAdd.Add(new IPEndPoint(ipAddress, 80)); } DataFolder peerFolder = CreateDataFolder(this); PeerAddressManager peerAddressManager = this.CreatePeerAddressManager(peerFolder); peerAddressManager.AddPeers(peersToAdd.ToArray(), IPAddress.Loopback); //These peers were all discovered from in the last 24 hours for (int i = 1; i <= 3; i++) { IPAddress ipAddress = IPAddress.Parse(string.Format("::ffff:192.168.0.{0}", i)); peerAddressManager.PeerDiscoveredFrom(new IPEndPoint(ipAddress, 80), DateTime.UtcNow.AddHours(-5)); } //These peers were all discovered from more than 24 hours ago for (int i = 4; i <= 5; i++) { IPAddress ipAddress = IPAddress.Parse(string.Format("::ffff:192.168.0.{0}", i)); peerAddressManager.PeerDiscoveredFrom(new IPEndPoint(ipAddress, 80), DateTime.UtcNow.AddHours(-25)); } IEnumerable <PeerAddress> peers = peerAddressManager.PeerSelector.SelectPeersForDiscovery(5); Assert.Equal(2, peers.Count()); }
public void PeerSelector_ReturnPeersForGetAddrPayload_Scenario4() { var peersToAdd = new List <IPEndPoint>(); for (int i = 1; i <= 15; i++) { var ipAddress = IPAddress.Parse(string.Format("::ffff:192.168.0.{0}", i)); peersToAdd.Add(new IPEndPoint(ipAddress, 80)); } var peerFolder = AssureEmptyDirAsDataFolder(Path.Combine(AppContext.BaseDirectory, "PeerAddressManager")); var peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.extendedLoggerFactory); peerAddressManager.AddPeers(peersToAdd.ToArray(), IPAddress.Loopback); for (int i = 1; i <= 2; i++) { var ipAddress = IPAddress.Parse(string.Format("::ffff:192.168.0.{0}", i)); peerAddressManager.PeerConnected(new IPEndPoint(ipAddress, 80), DateTime.UtcNow.AddSeconds(-80)); } var peers = peerAddressManager.PeerSelector.SelectPeersForGetAddrPayload(15); Assert.Equal(15, peers.Count()); Assert.Equal(2, peers.Count(p => p.Connected)); Assert.Equal(13, peers.Count(p => p.Fresh)); }
public void PeerAddressManager_AddPeer_then_RemovePeer_works_with_IPV4_and_IPV6() { DataFolder peerFolder = CreateDataFolder(this); var source = new IPEndPoint(IPAddress.Parse("124.54.54.2"), 22); var ipV4Addresses = new[] { IPAddress.Parse("21.23.0.1"), IPAddress.Parse("143.12.0.1"), IPAddress.Parse("99.87.44.1"), IPAddress.Parse("192.168.0.1"), }; var ipV4Endpoints = ipV4Addresses.Select((a, i) => new IPEndPoint(a, i)).ToArray(); var addressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.LoggerFactory.Object, new SelfEndpointTracker(this.LoggerFactory.Object, this.connectionManagerSettings)); addressManager.AddPeers(ipV4Endpoints, source.Address); addressManager.Peers.Select(a => a.Endpoint.Address.MapToIPv6()) .Distinct().Count().Should().Be(4); var ipV6Addresses = new[] { IPAddress.Parse("1050:1:0:0:5:600:300c:326b"), IPAddress.Parse("23d1:1:34b5:0:5:600:300c:326b"), ipV4Addresses[3].MapToIPv6(), }; var ipV6Endpoints = ipV6Addresses.Select((a, i) => new IPEndPoint(a, i)).ToArray(); addressManager.AddPeers(ipV6Endpoints, source.Address); addressManager.Peers.Select(a => a.Endpoint.Address.MapToIPv6()) .Distinct().Count().Should().Be(6); addressManager.RemovePeer(ipV4Endpoints[0]); addressManager.Peers.Select(a => a.Endpoint.Address.MapToIPv6()) .Distinct().Count().Should().Be(5); addressManager.RemovePeer(ipV6Endpoints[1]); addressManager.Peers.Select(a => a.Endpoint.Address.MapToIPv6()) .Distinct().Count().Should().Be(4); }