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());
        }
Ejemplo n.º 2
0
        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_IgnoringBannedPeer()
        {
            string discoveredpeer = "::ffff:192.168.0.2";

            IPAddress ipAddress   = IPAddress.Parse("::ffff:192.168.0.1");
            var       endPointOne = new IPEndPoint(ipAddress, 80);

            ipAddress = IPAddress.Parse(discoveredpeer);
            var endPointTwo = new IPEndPoint(ipAddress, 80);

            DataFolder peerFolder = CreateDataFolder(this);

            var peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.extendedLoggerFactory, new SelfEndpointTracker());

            peerAddressManager.AddPeer(endPointOne, IPAddress.Loopback);
            peerAddressManager.AddPeer(endPointTwo, IPAddress.Loopback);

            // Discovered peer.
            ipAddress = IPAddress.Parse(discoveredpeer);
            peerAddressManager.PeerDiscoveredFrom(new IPEndPoint(ipAddress, 80), DateTime.UtcNow.AddHours(-25));

            // Banned peer.
            peerAddressManager.FindPeer(endPointOne).BanUntil = DateTime.UtcNow.AddMinutes(1);

            IEnumerable <PeerAddress> peers = peerAddressManager.PeerSelector.SelectPeersForDiscovery(2);

            Assert.Single(peers);
            Assert.Contains(peers, p => p.Endpoint.Match(endPointTwo));
        }