public void Setup() { counter = 0; listener = new TestListener(); engine = new DhtEngine(listener); node = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, 4)); }
public void NodeReplaceTest() { int nodeCount = 0; Bucket b = new Bucket(); for (int i = 0; i < Bucket.MaxCapacity; i++) { Node n = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, i)); n.Seen(); b.Add(n); } b.Nodes[3].Seen(TimeSpan.FromDays(5)); b.Nodes[1].Seen(TimeSpan.FromDays(4)); b.Nodes[5].Seen(TimeSpan.FromDays(3)); listener.MessageSent += (message, endpoint) => { b.Nodes.Sort((l, r) => l.LastSeen.CompareTo(r.LastSeen)); if ((endpoint.Port == 3 && nodeCount == 0) || (endpoint.Port == 1 && nodeCount == 1) || (endpoint.Port == 5 && nodeCount == 2)) { Node n = b.Nodes.Find(delegate(Node no) { return(no.EndPoint.Port == endpoint.Port); }); n.Seen(); PingResponse response = new PingResponse(n.Id, message.TransactionId); listener.RaiseMessageReceived(response, node.EndPoint); nodeCount++; } }; ReplaceNodeTask task = new ReplaceNodeTask(engine, b, null); Assert.IsTrue(task.Execute().Wait(4000), "#10"); }
internal static IEnumerable <Node> FromCompactNode(BEncodedList nodes) { foreach (BEncodedValue node in nodes) { //bad format! if (!(node is BEncodedList)) { continue; } string host = string.Empty; long port = 0; foreach (BEncodedValue val in (BEncodedList)node) { if (val is BEncodedString) { host = ((BEncodedString)val).Text; } else if (val is BEncodedNumber) { port = ((BEncodedNumber)val).Number; } } IPAddress address; IPAddress.TryParse(host, out address); //REM: bad design from bitcomet we do not have node id so create it... //or use torrent infohash? // Will messages from this node be discarded later on if the NodeId doesn't match? if (address != null) { yield return(new Node(NodeId.Create(), new IPEndPoint(address, (int)port))); } } }
public async Task NodeReplaceTest() { int nodeCount = 0; Bucket b = new Bucket(); for (int i = 0; i < Bucket.MaxCapacity; i++) { Node n = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, i)); n.Seen(); b.Add(n); } b.Nodes[3].Seen(TimeSpan.FromDays(5)); b.Nodes[1].Seen(TimeSpan.FromDays(4)); b.Nodes[5].Seen(TimeSpan.FromDays(3)); listener.MessageSent += (data, endpoint) => { engine.MessageLoop.DhtMessageFactory.TryDecodeMessage(BEncodedValue.Decode <BEncodedDictionary> (data), out DhtMessage message); b.Nodes.Sort((l, r) => l.LastSeen.CompareTo(r.LastSeen)); if ((endpoint.Port == 3 && nodeCount == 0) || (endpoint.Port == 1 && nodeCount == 1) || (endpoint.Port == 5 && nodeCount == 2)) { Node n = b.Nodes.Find(no => no.EndPoint.Port == endpoint.Port); n.Seen(); PingResponse response = new PingResponse(n.Id, message.TransactionId); listener.RaiseMessageReceived(response, node.EndPoint); nodeCount++; } }; ReplaceNodeTask task = new ReplaceNodeTask(engine, b, null); await task.Execute().WithTimeout(4000); }
public void Setup() { listener = new TestListener(); node = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, 0)); engine = new DhtEngine(listener); //engine.Add(node); }
public async Task Setup() { counter = 0; listener = new TestListener(); engine = new DhtEngine(); await engine.SetListenerAsync(listener); node = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, 4)); }
public void AnnouncePeerEncode() { Node n = new MonoTorrent.Dht.Node(NodeId.Create(), null); n.Token = token; AnnouncePeer m = new AnnouncePeer(id, infohash, 6881, token); m.TransactionId = transactionId; Compare(m, "d1:ad2:id20:abcdefghij01234567899:info_hash20:mnopqrstuvwxyz1234564:porti6881e5:token8:aoeusnthe1:q13:announce_peer1:t2:aa1:y1:qe"); }
public void CompactPort() { Node n = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Parse("1.21.121.3"), 511)); BEncodedString port = n.CompactPort(); Assert.AreEqual(1, port.Span[0], "#1"); Assert.AreEqual(21, port.Span[1], "#1"); Assert.AreEqual(121, port.Span[2], "#1"); Assert.AreEqual(3, port.Span[3], "#1"); Assert.AreEqual(1, port.Span[4], "#1"); Assert.AreEqual(255, port.Span[5], "#1"); }
public async Task BucketRefreshTest() { List <Node> nodes = new List <Node> (); for (int i = 0; i < 5; i++) { nodes.Add(new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, i))); } listener.MessageSent += (data, endpoint) => { engine.MessageLoop.DhtMessageFactory.TryDecodeMessage(BEncodedValue.Decode <BEncodedDictionary> (data), out DhtMessage message); Node current = nodes.Find(n => n.EndPoint.Port.Equals(endpoint.Port)); if (current == null) { return; } if (message is Ping) { PingResponse r = new PingResponse(current.Id, message.TransactionId); listener.RaiseMessageReceived(r, current.EndPoint); } else if (message is FindNode) { FindNodeResponse response = new FindNodeResponse(current.Id, message.TransactionId); response.Nodes = ""; listener.RaiseMessageReceived(response, current.EndPoint); } }; foreach (var n in nodes) { engine.RoutingTable.Add(n); } foreach (Bucket b in engine.RoutingTable.Buckets) { b.Changed(TimeSpan.FromDays(1)); foreach (var n in b.Nodes) { n.Seen(TimeSpan.FromDays(1)); } } await engine.RefreshBuckets(); foreach (Bucket b in engine.RoutingTable.Buckets) { Assert.IsTrue(b.LastChanged < TimeSpan.FromHours(1)); Assert.IsTrue(b.Nodes.Exists(n => n.LastSeen < TimeSpan.FromHours(1))); } }
public void CompactNode() { Node n = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Parse("1.21.121.3"), 511)); BEncodedString port = n.CompactNode(); Assert.IsTrue(Toolbox.ByteMatch(n.Id.Bytes, 0, port.TextBytes, 0, 20), "#A"); Assert.AreEqual(1, port.TextBytes[20], "#1"); Assert.AreEqual(21, port.TextBytes[21], "#1"); Assert.AreEqual(121, port.TextBytes[22], "#1"); Assert.AreEqual(3, port.TextBytes[23], "#1"); Assert.AreEqual(1, port.TextBytes[24], "#1"); Assert.AreEqual(255, port.TextBytes[25], "#1"); }
public void CompactNode() { Node n = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Parse("1.21.121.3"), 511)); BEncodedString port = n.CompactNode(); Assert.IsTrue(n.Id.Span.SequenceEqual(port.Span.Slice(0, 20)), "#A"); Assert.AreEqual(1, port.Span[20], "#1"); Assert.AreEqual(21, port.Span[21], "#1"); Assert.AreEqual(121, port.Span[22], "#1"); Assert.AreEqual(3, port.Span[23], "#1"); Assert.AreEqual(1, port.Span[24], "#1"); Assert.AreEqual(255, port.Span[25], "#1"); }
public async System.Threading.Tasks.Task BucketRefreshTestAsync() { List <Node> nodes = new List <Node>(); for (int i = 0; i < 5; i++) { nodes.Add(new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, i))); } engine.TimeOut = TimeSpan.FromMilliseconds(25); engine.BucketRefreshTimeout = TimeSpan.FromMilliseconds(75); engine.MessageLoop.QuerySent += delegate(object o, SendQueryEventArgs e) { DhtEngine.MainLoop.Queue(delegate { if (!e.TimedOut) { return; } Node current = nodes.Find(delegate(Node n) { return(n.EndPoint.Port.Equals(e.EndPoint.Port)); }); if (current == null) { return; } if (e.Query is Ping) { PingResponse r = new PingResponse(current.Id, e.Query.TransactionId); listener.RaiseMessageReceived(r, current.EndPoint); } else if (e.Query is FindNode) { FindNodeResponse response = new FindNodeResponse(current.Id, e.Query.TransactionId); response.Nodes = ""; listener.RaiseMessageReceived(response, current.EndPoint); } }); }; await engine.AddAsync(nodes); engine.Start(); System.Threading.Thread.Sleep(500); foreach (Bucket b in engine.RoutingTable.Buckets) { Assert.IsTrue(b.LastChanged > DateTime.UtcNow.AddSeconds(-2)); Assert.IsTrue(b.Nodes.Exists(delegate(Node n) { return(n.LastSeen > DateTime.UtcNow.AddMilliseconds(-900)); })); } }
public void NodeReplaceTest() { engine.TimeOut = TimeSpan.FromMilliseconds(25); ManualResetEvent handle = new ManualResetEvent(false); Bucket b = new Bucket(); for (int i = 0; i < Bucket.MaxCapacity; i++) { Node n = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, i)); n.LastSeen = DateTime.UtcNow; b.Add(n); } b.Nodes[3].LastSeen = DateTime.UtcNow.AddDays(-5); b.Nodes[1].LastSeen = DateTime.UtcNow.AddDays(-4); b.Nodes[5].LastSeen = DateTime.UtcNow.AddDays(-3); engine.MessageLoop.QuerySent += delegate(object o, SendQueryEventArgs e) { if (!e.TimedOut) { return; } b.Nodes.Sort(); if ((e.EndPoint.Port == 3 && nodeCount == 0) || (e.EndPoint.Port == 1 && nodeCount == 1) || (e.EndPoint.Port == 5 && nodeCount == 2)) { Node n = b.Nodes.Find(delegate(Node no) { return(no.EndPoint.Port == e.EndPoint.Port); }); n.Seen(); PingResponse response = new PingResponse(n.Id, e.Query.TransactionId); DhtEngine.MainLoop.Queue(delegate { //System.Threading.Thread.Sleep(100); Console.WriteLine("Faking the receive"); listener.RaiseMessageReceived(response, node.EndPoint); }); nodeCount++; } }; ReplaceNodeTask task = new ReplaceNodeTask(engine, b, null); // FIXME: Need to assert that node 0.0.0.0:0 is the one which failed - i.e. it should be replaced task.Completed += delegate(object o, TaskCompleteEventArgs e) { handle.Set(); }; task.Execute(); Assert.IsTrue(handle.WaitOne(4000, false), "#10"); }
public async Task BucketRefreshTest() { List <Node> nodes = new List <Node>(); for (int i = 0; i < 5; i++) { nodes.Add(new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, i))); } engine.MessageLoop.Timeout = TimeSpan.FromMilliseconds(25); engine.BucketRefreshTimeout = TimeSpan.FromMilliseconds(75); listener.MessageSent += (message, endpoint) => { Node current = nodes.Find(delegate(Node n) { return(n.EndPoint.Port.Equals(endpoint.Port)); }); if (current == null) { return; } if (message is Ping) { PingResponse r = new PingResponse(current.Id, message.TransactionId); listener.RaiseMessageReceived(r, current.EndPoint); } else if (message is FindNode) { FindNodeResponse response = new FindNodeResponse(current.Id, message.TransactionId); response.Nodes = ""; listener.RaiseMessageReceived(response, current.EndPoint); } }; engine.Add(nodes); foreach (Bucket b in engine.RoutingTable.Buckets) { b.Changed(TimeSpan.FromDays(-1)); } await engine.StartAsync(); await engine.WaitForState(DhtState.Ready); foreach (Bucket b in engine.RoutingTable.Buckets) { Assert.IsTrue(b.LastChanged < TimeSpan.FromMinutes(1)); Assert.IsTrue(b.Nodes.Exists(delegate(Node n) { return(n.LastSeen < TimeSpan.FromMilliseconds(900)); })); } }
public void SortByLastSeen() { List <Node> nodes = new List <Node>(); DateTime start = DateTime.Now; for (int i = 0; i < 5; i++) { Node n = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, 0)); n.LastSeen = start.AddDays(-i); nodes.Add(n); } nodes.Sort(); Assert.AreEqual(start.AddDays(-4), nodes[0].LastSeen); Assert.AreEqual(start, nodes[4].LastSeen); }
public void SortBySeen() { var oldNode = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, 0)); var newNode = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, 1)); var bucket = new Bucket { oldNode, newNode }; oldNode.Seen(TimeSpan.FromDays(1)); bucket.SortBySeen(); Assert.AreEqual(oldNode, bucket.Nodes[0], "#1"); newNode.Seen(TimeSpan.FromDays(2)); bucket.SortBySeen(); Assert.AreEqual(newNode, bucket.Nodes[0], "#2"); }
public void ReplaceNodeTest() { engine.TimeOut = TimeSpan.FromMilliseconds(25); Node replacement = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Loopback, 1337)); for (int i = 0; i < 4; i++) { Node node = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, i)); node.LastSeen = DateTime.UtcNow.AddMinutes(-i); engine.RoutingTable.Add(node); } Node nodeToReplace = engine.RoutingTable.Buckets[0].Nodes[3]; ReplaceNodeTask task = new ReplaceNodeTask(engine, engine.RoutingTable.Buckets[0], replacement); Assert.IsTrue(task.Execute().Wait(1000), "#a"); Assert.IsFalse(engine.RoutingTable.Buckets[0].Nodes.Contains(nodeToReplace), "#1"); Assert.IsTrue(engine.RoutingTable.Buckets[0].Nodes.Contains(replacement), "#2"); }
public void ReplaceNodeTest() { engine.MessageLoop.Timeout = TimeSpan.FromMilliseconds(0); Node replacement = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Loopback, 1337)); for (int i = 0; i < 4; i++) { var n = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, i)); n.Seen(TimeSpan.FromDays(i)); engine.RoutingTable.Add(n); } Node nodeToReplace = engine.RoutingTable.Buckets[0].Nodes[3]; ReplaceNodeTask task = new ReplaceNodeTask(engine, engine.RoutingTable.Buckets[0], replacement); Assert.IsTrue(task.Execute().Wait(1000), "#a"); Assert.IsFalse(engine.RoutingTable.Buckets[0].Nodes.Contains(nodeToReplace), "#1"); Assert.IsTrue(engine.RoutingTable.Buckets[0].Nodes.Contains(replacement), "#2"); }
public void CheckTokenGenerator() { TokenManager m = new TokenManager(); m.Timeout = TimeSpan.FromMilliseconds(75); // 1 second timeout for testing purposes Node n = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 25)); Node n2 = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Parse("127.0.0.2"), 25)); BEncodedString s = m.GenerateToken(n); BEncodedString s2 = m.GenerateToken(n); Assert.AreEqual(s, s2, "#1"); Assert.IsTrue(m.VerifyToken(n, s), "#2"); Assert.IsFalse(m.VerifyToken(n2, s), "#3"); System.Threading.Thread.Sleep(100); Assert.IsTrue(m.VerifyToken(n, s), "#4"); System.Threading.Thread.Sleep(100); Assert.IsFalse(m.VerifyToken(n, s), "#5"); }
public void Setup() { manager = new TokenManager(SHA1.Create()); node = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 25)); token = manager.GenerateToken(node); }
public RoutingTable() : this(new Node(NodeId.Create(), new System.Net.IPEndPoint(IPAddress.Any, 0))) { }