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 PeerFile_CanSaveAndLoadPeers_PeerSeen() { IPAddress ipAddress = IPAddress.Parse("::ffff:192.168.0.1"); var endpoint = new IPEndPoint(ipAddress, 80); DataFolder peerFolder = CreateDataFolder(this); var addressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.LoggerFactory.Object, new SelfEndpointTracker(this.LoggerFactory.Object)); addressManager.AddPeer(endpoint, IPAddress.Loopback); DateTime applicableDate = DateTime.UtcNow.Date; addressManager.PeerAttempted(endpoint, applicableDate); addressManager.PeerConnected(endpoint, applicableDate); addressManager.PeerHandshaked(endpoint, applicableDate); addressManager.PeerSeen(endpoint, applicableDate); addressManager.SavePeers(); addressManager.LoadPeers(); PeerAddress savedPeer = addressManager.FindPeer(endpoint); Assert.Equal("::ffff:192.168.0.1", savedPeer.Endpoint.Address.ToString()); Assert.Equal(80, savedPeer.Endpoint.Port); Assert.Equal(0, savedPeer.ConnectionAttempts); Assert.Equal(applicableDate, savedPeer.LastConnectionSuccess.Value.Date); Assert.Equal(applicableDate, savedPeer.LastConnectionHandshake.Value.Date); Assert.Equal(applicableDate, savedPeer.LastSeen.Value.Date); Assert.Equal("127.0.0.1", savedPeer.Loopback.ToString()); }
public void CanSaveAndLoadPeerAddressFile_PeerHandshaked() { var ipAddress = IPAddress.Parse("::ffff:192.168.0.1"); var networkAddress = new NetworkAddress(ipAddress, 80); var peerFolder = AssureEmptyDirAsDataFolder(Path.Combine(AppContext.BaseDirectory, "PeerAddressManager")); var addressManager = new PeerAddressManager(peerFolder); addressManager.AddPeer(networkAddress, IPAddress.Loopback); var applicableDate = DateTime.UtcNow.Date; addressManager.PeerAttempted(networkAddress.Endpoint, applicableDate); addressManager.PeerConnected(networkAddress.Endpoint, applicableDate); addressManager.PeerHandshaked(networkAddress.Endpoint, applicableDate); addressManager.SavePeers(); addressManager.LoadPeers(); var savedPeer = addressManager.FindPeer(networkAddress.Endpoint); Assert.Equal("::ffff:192.168.0.1", savedPeer.NetworkAddress.Endpoint.Address.ToString()); Assert.Equal(applicableDate, savedPeer.NetworkAddress.Time); Assert.Equal(80, savedPeer.NetworkAddress.Endpoint.Port); Assert.Equal(0, savedPeer.ConnectionAttempts); Assert.Equal(applicableDate, savedPeer.LastConnectionSuccess.Value.Date); Assert.Equal(applicableDate, savedPeer.LastConnectionHandshake.Value.Date); Assert.Equal("127.0.0.1", savedPeer.Loopback.ToString()); }
public void PeerCanBeReturnedAsPreferred_HasBeenConnectedTo_Scenario3() { var ipAddress = IPAddress.Parse("::ffff:192.168.0.1"); var addressOne = new NetworkAddress(ipAddress, 80); ipAddress = IPAddress.Parse("::ffff:192.168.0.2"); var addressTwo = new NetworkAddress(ipAddress, 80); ipAddress = IPAddress.Parse("::ffff:192.168.0.3"); var addressThree = new NetworkAddress(ipAddress, 80); var peerFolder = AssureEmptyDirAsDataFolder(Path.Combine(AppContext.BaseDirectory, "PeerAddressManager")); var addressManager = new PeerAddressManager(peerFolder); addressManager.AddPeer(addressOne, IPAddress.Loopback); addressManager.AddPeer(addressTwo, IPAddress.Loopback); addressManager.AddPeer(addressThree, IPAddress.Loopback); addressManager.PeerConnected(addressOne.Endpoint, DateTime.UtcNow.AddDays(-5)); for (int i = 0; i < 11; i++) { addressManager.PeerAttempted(addressOne.Endpoint, DateTime.UtcNow); } var networkAddresses = addressManager.SelectPeersToConnectTo(); Assert.Equal(2, networkAddresses.Count()); Assert.Null(networkAddresses.FirstOrDefault(n => n.Endpoint.Address.ToString() == addressOne.Endpoint.Address.ToString())); }
public void PeerFile_CanSaveAndLoadPeers_PeerSeen() { var ipAddress = IPAddress.Parse("::ffff:192.168.0.1"); var endpoint = new IPEndPoint(ipAddress, 80); var peerFolder = AssureEmptyDirAsDataFolder(Path.Combine(AppContext.BaseDirectory, "PeerAddressManager")); var addressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.loggerFactory, new SelfEndpointTracker()); addressManager.AddPeer(endpoint, IPAddress.Loopback); var applicableDate = DateTime.UtcNow.Date; addressManager.PeerAttempted(endpoint, applicableDate); addressManager.PeerConnected(endpoint, applicableDate); addressManager.PeerHandshaked(endpoint, applicableDate); addressManager.PeerSeen(endpoint, applicableDate); addressManager.SavePeers(); addressManager.LoadPeers(); var savedPeer = addressManager.FindPeer(endpoint); Assert.Equal("::ffff:192.168.0.1", savedPeer.Endpoint.Address.ToString()); Assert.Equal(80, savedPeer.Endpoint.Port); Assert.Equal(0, savedPeer.ConnectionAttempts); Assert.Equal(applicableDate, savedPeer.LastConnectionSuccess.Value.Date); Assert.Equal(applicableDate, savedPeer.LastConnectionHandshake.Value.Date); Assert.Equal(applicableDate, savedPeer.LastSeen.Value.Date); Assert.Equal("127.0.0.1", savedPeer.Loopback.ToString()); }
public void PeerState_AllConnectionDataGetsReset() { IPAddress ipAddress = IPAddress.Parse("::ffff:192.168.0.1"); var endpoint = new IPEndPoint(ipAddress, 80); PeerAddressManager peerAddressManager = this.CreatePeerAddressManager(); peerAddressManager.AddPeer(endpoint, IPAddress.Loopback); peerAddressManager.PeerConnected(endpoint, DateTime.UtcNow); peerAddressManager.PeerAttempted(endpoint, DateTime.UtcNow); peerAddressManager.PeerAttempted(endpoint, DateTime.UtcNow); peerAddressManager.PeerAttempted(endpoint, DateTime.UtcNow); peerAddressManager.PeerConnected(endpoint, DateTime.UtcNow); PeerAddress peerOne = peerAddressManager.FindPeer(endpoint); Assert.Equal(0, peerOne.ConnectionAttempts); Assert.Null(peerOne.LastAttempt); Assert.NotNull(peerOne.LastConnectionSuccess); }
public void PeerState_AllConnectionDataGetsReset() { var ipAddress = IPAddress.Parse("::ffff:192.168.0.1"); var networkAddress = new NetworkAddress(ipAddress, 80); var peerFolder = AssureEmptyDirAsDataFolder(Path.Combine(AppContext.BaseDirectory, "PeerAddressManager")); var addressManager = new PeerAddressManager(peerFolder, this.extendedLoggerFactory); addressManager.AddPeer(networkAddress, IPAddress.Loopback); addressManager.PeerConnected(networkAddress.Endpoint, DateTime.UtcNow); addressManager.PeerAttempted(networkAddress.Endpoint, DateTime.UtcNow); addressManager.PeerAttempted(networkAddress.Endpoint, DateTime.UtcNow); addressManager.PeerAttempted(networkAddress.Endpoint, DateTime.UtcNow); addressManager.PeerConnected(networkAddress.Endpoint, DateTime.UtcNow); var peerOne = addressManager.FindPeer(networkAddress.Endpoint); Assert.Equal(0, peerOne.ConnectionAttempts); Assert.Null(peerOne.LastConnectionAttempt); Assert.NotNull(peerOne.LastConnectionSuccess); }
public void PeerState_TestReturnFromPeerConnectedSet_IgnoringBannedPeer() { var ipAddress = IPAddress.Parse("::ffff:192.168.0.1"); var endPointOne = new IPEndPoint(ipAddress, 80); ipAddress = IPAddress.Parse("::ffff:192.168.0.2"); var endPointTwo = new IPEndPoint(ipAddress, 80); var peerFolder = AssureEmptyDirAsDataFolder(Path.Combine(AppContext.BaseDirectory, "PeerAddressManager")); var peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.extendedLoggerFactory, new SelfEndpointTracker()); peerAddressManager.AddPeer(endPointOne, IPAddress.Loopback); peerAddressManager.AddPeer(endPointTwo, IPAddress.Loopback); peerAddressManager.FindPeer(endPointTwo).BanUntil = DateTime.UtcNow.AddMinutes(1); peerAddressManager.PeerConnected(endPointOne, DateTime.UtcNow.AddSeconds(-80)); peerAddressManager.PeerConnected(endPointTwo, DateTime.UtcNow.AddSeconds(-80)); IEnumerable<PeerAddress> peers = peerAddressManager.PeerSelector.Connected(); Assert.Single(peers); Assert.Contains(peers, p => p.Endpoint.Match(endPointOne)); }
public void PeerCanBeReturnedAsPreferred_HasBeenConnectedTo_Scenario1() { var ipAddress = IPAddress.Parse("::ffff:192.168.0.1"); var addressOne = new NetworkAddress(ipAddress, 80); ipAddress = IPAddress.Parse("::ffff:192.168.0.2"); var addressTwo = new NetworkAddress(ipAddress, 80); ipAddress = IPAddress.Parse("::ffff:192.168.0.3"); var addressThree = new NetworkAddress(ipAddress, 80); var peerFolder = AssureEmptyDirAsDataFolder(Path.Combine(AppContext.BaseDirectory, "PeerAddressManager")); var addressManager = new PeerAddressManager(peerFolder); addressManager.AddPeer(addressOne, IPAddress.Loopback, PeerIntroductionType.Add); addressManager.AddPeer(addressTwo, IPAddress.Loopback, PeerIntroductionType.Add); addressManager.AddPeer(addressThree, IPAddress.Loopback, PeerIntroductionType.Add); addressManager.PeerConnected(addressThree.Endpoint, DateTimeOffset.Now); var networkAddresses = addressManager.SelectPeersToConnectTo(); Assert.Equal(3, networkAddresses.Count()); }