Exemplo n.º 1
0
        public void IsValueExpired_Exp()
        {
            KadId ownerNodeId = KadId.GenerateRandom();

            for (int i = 2; i < ownerNodeId.Value.Length; i++)
            {
                ownerNodeId.Value[i] = 255;
            }
            ownerNodeId.Value[0] = 0;
            ownerNodeId.Value[1] = 0;
            KadNode node1 = new KadNode(new System.Net.IPEndPoint(IPAddress.Loopback, 55555), ownerNodeId);

            byte[] baseId = new byte[20];

            for (int i = 1; i < 21; i++)  //fill up random bucket
            {
                baseId[19] = (byte)i;
                KadContactNode contact = new KadContactNode(new KadId(baseId), new IPEndPoint(IPAddress.Loopback, 20000));
                node1.BucketList.Put(contact);
            }

            //baseId[0] = 255;
            //baseId[1] = 255;
            //baseId[19] = 0;
            //for(int i = 1; i < 20; i++) {
            //    baseId[3] = (byte)i;
            //    ContactNode contact = new ContactNode(new KadId(baseId), new IPEndPoint(IPAddress.Loopback, 20000));
            //    node1.BucketList.Put(contact);
            //}

            KadValue nonExpiredVal = new KadValue(node1.BucketList.Buckets.Aggregate((o, n) => o.Id < n.Id ? n : o).GetNodes()
                                                  .Aggregate((o, n) => o.Id > n.Id ? n : o).Id, DateTime.UtcNow.AddSeconds(-(node1.ValueExpirationInSec + 1)), new byte[20]);

            Assert.IsFalse(node1.IsValueExpired(nonExpiredVal));

            KadValue expiredVal = new KadValue(nonExpiredVal.Id, DateTime.UtcNow.AddSeconds(-(((node1.ValueExpirationInSec + 1) * Math.Exp(20 / 19)))), new byte[20]);

            Assert.IsTrue(node1.IsValueExpired(expiredVal));

            node1.Shutdown();
        }
Exemplo n.º 2
0
        public void IsValueExpired_GoodAmountOfCloseContacts()
        {
            KadId ownerNodeId = KadId.GenerateRandom();

            for (int i = 2; i < ownerNodeId.Value.Length; i++)
            {
                ownerNodeId.Value[i] = 255;
            }
            ownerNodeId.Value[0] = 0;
            ownerNodeId.Value[1] = 0;
            KadNode node1 = new KadNode(new System.Net.IPEndPoint(IPAddress.Loopback, 55555), ownerNodeId);


            byte[] baseId = new byte[20];
            baseId[0] = 255;
            for (int i = 0; i < 20; i++)  //fill up random bucket
            {
                baseId[19] = (byte)i;
                KadContactNode contact = new KadContactNode(new KadId(baseId), new IPEndPoint(IPAddress.Loopback, 20000));
                node1.BucketList.Put(contact);
            }

            baseId[1] = 255;
            for (int i = 0; i < 20; i++)
            {
                baseId[19] = (byte)i;
                KadContactNode contact = new KadContactNode(new KadId(baseId), new IPEndPoint(IPAddress.Loopback, 20000));
                node1.BucketList.Put(contact);
            }

            KadValue value = new KadValue(node1.BucketList.Buckets.Aggregate((o, n) => o.Id < n.Id ? n : o).GetNodes().Aggregate((o, n) => o.Id > n.Id ? n : o).Id, DateTime.UtcNow, new byte[20]);

            Assert.IsFalse(node1.IsValueExpired(value));

            KadValue expiredVal = new KadValue(value.Id, value.Timestamp.AddSeconds(-(node1.ValueExpirationInSec + 1)), new byte[20]);

            Assert.IsTrue(node1.IsValueExpired(expiredVal));

            node1.Shutdown();
        }