public void Listening_MissingTransport() { var peer = new Peer { Id = self.Id, PublicKey = self.PublicKey, AgentVersion = self.AgentVersion }; var swarm = new Swarm { LocalPeer = peer }; ExceptionAssert.Throws <ArgumentException>(() => { var _ = swarm.StartListeningAsync("/ip4/127.0.0.1").Result; }); Assert.AreEqual(0, peer.Addresses.Count()); }
public void NewPeerAddress_Self() { var swarm = new Swarm { LocalPeer = self }; var selfAddress = "/ip4/178.62.158.247/tcp/4001/ipfs/" + self.Id; ExceptionAssert.Throws <Exception>(() => { var _ = swarm.RegisterPeerAsync(selfAddress).Result; }); selfAddress = "/ip4/178.62.158.247/tcp/4001/p2p/" + self.Id; ExceptionAssert.Throws <Exception>(() => { var _ = swarm.RegisterPeerAsync(selfAddress).Result; }); }
public async Task Connect_WithSomeUnreachableAddresses() { var bid = "QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"; var peerB = new Peer { AgentVersion = "peerB", Id = bid, PublicKey = "CAASXjBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQDlTSgVLprWaXfmxDr92DJE1FP0wOexhulPqXSTsNh5ot6j+UiuMgwb0shSPKzLx9AuTolCGhnwpTBYHVhFoBErAgMBAAE=", Addresses = new MultiAddress[] { $"/ip4/127.0.0.2/tcp/2/ipfs/{bid}", $"/ip4/127.0.0.3/tcp/3/ipfs/{bid}" } }; var swarmB = new Swarm { LocalPeer = peerB }; await swarmB.StartAsync(); var peerBAddress = await swarmB.StartListeningAsync("/ip4/127.0.0.1/tcp/0"); Assert.IsTrue(peerB.Addresses.Count() > 0); var swarm = new Swarm { LocalPeer = self }; await swarm.StartAsync(); try { var remotePeer = await swarm.ConnectAsync(peerB); Assert.IsNotNull(remotePeer.ConnectedAddress); Assert.AreEqual(peerB.PublicKey, remotePeer.PublicKey); Assert.IsTrue(remotePeer.IsValid()); Assert.IsTrue(swarm.KnownPeers.Contains(peerB)); } finally { await swarm.StopAsync(); await swarmB.StopAsync(); } }
public void Connecting_To_Self() { var swarm = new Swarm { LocalPeer = self }; swarm.StartAsync().Wait(); try { ExceptionAssert.Throws <Exception>(() => { var _ = swarm.ConnectAsync(earth).Result; }); } finally { swarm.StopAsync().Wait(); } }
public void Connecting_To_Blacklisted_Address() { var swarm = new Swarm { LocalPeer = self }; swarm.BlackList.Add(mars); swarm.StartAsync().Wait(); try { ExceptionAssert.Throws <Exception>(() => { var _ = swarm.ConnectAsync(mars).Result; }); } finally { swarm.StopAsync().Wait(); } }
public async Task Connects_OnPeerDiscovered_When_Below_MinConnections() { var swarmA = new Swarm { LocalPeer = peerA }; await swarmA.StartAsync(); var peerAAddress = await swarmA.StartListeningAsync("/ip4/127.0.0.1/tcp/0"); var swarmB = new Swarm { LocalPeer = peerB }; await swarmB.StartAsync(); var peerBAddress = await swarmB.StartListeningAsync("/ip4/127.0.0.1/tcp/0"); try { using (var dialer = new AutoDialer(swarmA)) { var other = swarmA.RegisterPeerAddress(peerBAddress); // wait for the connection. var endTime = DateTime.Now.AddSeconds(3); while (other.ConnectedAddress == null) { if (DateTime.Now > endTime) { Assert.Fail("Did not do autodial"); } await Task.Delay(100); } } } finally { await swarmA?.StopAsync(); await swarmB?.StopAsync(); } }
public void KnownPeers() { var swarm = new Swarm { LocalPeer = self }; Assert.AreEqual(0, swarm.KnownPeers.Count()); Assert.AreEqual(0, swarm.KnownPeerAddresses.Count()); swarm.RegisterPeerAddress("/ip4/10.1.10.10/tcp/29087/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3"); Assert.AreEqual(1, swarm.KnownPeers.Count()); Assert.AreEqual(1, swarm.KnownPeerAddresses.Count()); swarm.RegisterPeerAddress("/ip4/10.1.10.11/tcp/29087/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3"); Assert.AreEqual(1, swarm.KnownPeers.Count()); Assert.AreEqual(2, swarm.KnownPeerAddresses.Count()); swarm.RegisterPeerAddress(venus); Assert.AreEqual(2, swarm.KnownPeers.Count()); Assert.AreEqual(3, swarm.KnownPeerAddresses.Count()); }
public void Connect_Not_Peer() { var remoteId = "QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb"; MultiAddress remoteAddress = $"/dns/npmjs.com/tcp/80/ipfs/{remoteId}"; var swarm = new Swarm { LocalPeer = self }; swarm.StartAsync().Wait(); try { ExceptionAssert.Throws <Exception>(() => { var _ = swarm.ConnectAsync(remoteAddress).Result; }); } finally { swarm.StopAsync().Wait(); } }
public void IsAllowed_Peer() { var swarm = new Swarm(); var peer = new Peer { Id = "QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h", Addresses = new MultiAddress[] { "/ip4/127.0.0.1/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h" } }; Assert.IsTrue(swarm.IsAllowed(peer)); swarm.BlackList.Add(peer.Addresses.First()); Assert.IsFalse(swarm.IsAllowed(peer)); swarm.BlackList.Clear(); swarm.BlackList.Add("/p2p/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); Assert.IsFalse(swarm.IsAllowed(peer)); }
public async Task Stop_Closes_Listeners() { var peer = new Peer { Id = self.Id, PublicKey = self.PublicKey, AgentVersion = self.AgentVersion }; MultiAddress addr = "/ip4/0.0.0.0/tcp/0"; var swarm = new Swarm { LocalPeer = peer }; try { await swarm.StartAsync(); await swarm.StartListeningAsync(addr); Assert.IsTrue(peer.Addresses.Count() > 0); await swarm.StopAsync(); Assert.AreEqual(0, peer.Addresses.Count()); await swarm.StartAsync(); await swarm.StartListeningAsync(addr); Assert.IsTrue(peer.Addresses.Count() > 0); await swarm.StopAsync(); Assert.AreEqual(0, peer.Addresses.Count()); } catch (Exception) { await swarm.StopAsync(); throw; } }
public async Task Connecting_To_Self_Indirect() { var swarm = new Swarm { LocalPeer = self }; await swarm.StartAsync(); try { var listen = await swarm.StartListeningAsync("/ip4/127.0.0.1/tcp/0"); var bad = listen.Clone(); bad.Protocols[2].Value = "QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb"; ExceptionAssert.Throws <Exception>(() => { swarm.ConnectAsync(bad).Wait(); }); } finally { await swarm.StopAsync(); } }
public async Task Listening_Start_Stop() { var peer = new Peer { Id = self.Id, PublicKey = self.PublicKey, AgentVersion = self.AgentVersion }; MultiAddress addr = "/ip4/0.0.0.0/tcp/0"; var swarm = new Swarm { LocalPeer = peer }; await swarm.StartAsync(); try { await swarm.StartListeningAsync(addr); Assert.IsTrue(peer.Addresses.Count() > 0); await swarm.StopListeningAsync(addr); Assert.AreEqual(0, peer.Addresses.Count()); await swarm.StartListeningAsync(addr); Assert.IsTrue(peer.Addresses.Count() > 0); await swarm.StopListeningAsync(addr); Assert.AreEqual(0, peer.Addresses.Count()); } finally { await swarm.StopAsync(); } }
public void LocalPeer() { var swarm = new Swarm { LocalPeer = self }; Assert.AreEqual(self, swarm.LocalPeer) ; ExceptionAssert.Throws <ArgumentNullException>(() => { swarm.LocalPeer = null; }); ExceptionAssert.Throws <ArgumentNullException>(() => { swarm.LocalPeer = new Peer { Id = self.Id }; }); ExceptionAssert.Throws <ArgumentNullException>(() => { swarm.LocalPeer = new Peer { PublicKey = self.PublicKey }; }); ExceptionAssert.Throws <ArgumentException>(() => { swarm.LocalPeer = new Peer { Id = self.Id, PublicKey = other.PublicKey }; }); swarm.LocalPeer = new Peer { Id = other.Id, PublicKey = other.PublicKey }; Assert.AreEqual(other, swarm.LocalPeer); }
public void Connect_Cancelled() { var cs = new CancellationTokenSource(); cs.Cancel(); var remoteId = "QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb"; MultiAddress remoteAddress = $"/ip4/127.0.0.1/tcp/4002/ipfs/{remoteId}"; var swarm = new Swarm { LocalPeer = self }; swarm.StartAsync().Wait(); try { ExceptionAssert.Throws <Exception>(() => { var _ = swarm.ConnectAsync(remoteAddress, cs.Token).Result; }); } finally { swarm.StopAsync().Wait(); } }
public async Task Dial_Peer_No_Address() { var peer = new Peer { Id = mars.PeerId }; var swarm = new Swarm { LocalPeer = self }; await swarm.StartAsync(); try { ExceptionAssert.Throws <Exception>(() => { swarm.DialAsync(peer, "/foo/0.42.0").Wait(); }); } finally { await swarm.StopAsync(); } }
public async Task Dial_Peer() { var peerB = new Peer { AgentVersion = "peerB", Id = "QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h", PublicKey = "CAASXjBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQDlTSgVLprWaXfmxDr92DJE1FP0wOexhulPqXSTsNh5ot6j+UiuMgwb0shSPKzLx9AuTolCGhnwpTBYHVhFoBErAgMBAAE=" }; var swarmB = new Swarm { LocalPeer = peerB }; await swarmB.StartAsync(); var peerBAddress = await swarmB.StartListeningAsync("/ip4/127.0.0.1/tcp/0"); var swarm = new Swarm { LocalPeer = self }; await swarm.StartAsync(); try { using (var stream = await swarm.DialAsync(peerB, "/ipfs/id/1.0.0")) { Assert.IsNotNull(stream); Assert.IsTrue(stream.CanRead); Assert.IsTrue(stream.CanWrite); } } finally { await swarm.StopAsync(); await swarmB.StopAsync(); } }
/// <summary> /// Creates a new instance of the <see cref="AutoDialer"/> class. /// </summary> /// <param name="swarm"> /// Provides access to other peers. /// </param> public AutoDialer(Swarm swarm) { this.swarm = swarm; swarm.PeerDiscovered += OnPeerDiscovered; swarm.PeerDisconnected += OnPeerDisconnected; }
public async Task Connects_OnPeerDisconnected_When_Below_MinConnections() { var swarmA = new Swarm { LocalPeer = peerA }; await swarmA.StartAsync(); var peerAAddress = await swarmA.StartListeningAsync("/ip4/127.0.0.1/tcp/0"); var swarmB = new Swarm { LocalPeer = peerB }; await swarmB.StartAsync(); var peerBAddress = await swarmB.StartListeningAsync("/ip4/127.0.0.1/tcp/0"); var swarmC = new Swarm { LocalPeer = peerC }; await swarmC.StartAsync(); var peerCAddress = await swarmC.StartListeningAsync("/ip4/127.0.0.1/tcp/0"); bool isBConnected = false; swarmA.ConnectionEstablished += (s, conn) => { if (conn.RemotePeer == peerB) { isBConnected = true; } }; try { using (var dialer = new AutoDialer(swarmA) { MinConnections = 1 }) { var b = swarmA.RegisterPeerAddress(peerBAddress); var c = swarmA.RegisterPeerAddress(peerCAddress); // wait for the peer B connection. var endTime = DateTime.Now.AddSeconds(3); while (!isBConnected) { if (DateTime.Now > endTime) { Assert.Fail("Did not do autodial on peer discovered"); } await Task.Delay(100); } Assert.IsNull(c.ConnectedAddress); await swarmA.DisconnectAsync(peerBAddress); // wait for the peer C connection. endTime = DateTime.Now.AddSeconds(3); while (c.ConnectedAddress == null) { if (DateTime.Now > endTime) { Assert.Fail("Did not do autodial on peer disconnected"); } await Task.Delay(100); } } } finally { await swarmA?.StopAsync(); await swarmB?.StopAsync(); await swarmC?.StopAsync(); } }
public async Task Connect_Disconnect_Reconnect() { var peerB = new Peer { AgentVersion = "peerB", Id = "QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h", PublicKey = "CAASXjBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQDlTSgVLprWaXfmxDr92DJE1FP0wOexhulPqXSTsNh5ot6j+UiuMgwb0shSPKzLx9AuTolCGhnwpTBYHVhFoBErAgMBAAE=" }; var swarmB = new Swarm { LocalPeer = peerB }; await swarmB.StartAsync(); var peerBAddress = await swarmB.StartListeningAsync("/ip4/127.0.0.1/tcp/0"); Assert.IsTrue(peerB.Addresses.Count() > 0); var swarm = new Swarm { LocalPeer = self }; await swarm.StartAsync(); await swarm.StartListeningAsync("/ip4/127.0.0.1/tcp/0"); try { var remotePeer = (await swarm.ConnectAsync(peerBAddress)).RemotePeer; Assert.IsNotNull(remotePeer.ConnectedAddress); Assert.AreEqual(peerB.PublicKey, remotePeer.PublicKey); Assert.IsTrue(remotePeer.IsValid()); Assert.IsTrue(swarm.KnownPeers.Contains(peerB)); // wait for swarmB to settle var endTime = DateTime.Now.AddSeconds(3); while (true) { if (DateTime.Now > endTime) { Assert.Fail("swarmB does not know about self"); } if (swarmB.KnownPeers.Contains(self)) { break; } await Task.Delay(100); } var me = swarmB.KnownPeers.First(p => p == self); Assert.AreEqual(self.Id, me.Id); Assert.AreEqual(self.PublicKey, me.PublicKey); Assert.IsNotNull(me.ConnectedAddress); // Check disconnect await swarm.DisconnectAsync(peerBAddress); Assert.IsNull(remotePeer.ConnectedAddress); Assert.IsTrue(swarm.KnownPeers.Contains(peerB)); Assert.IsTrue(swarmB.KnownPeers.Contains(self)); // wait for swarmB to settle endTime = DateTime.Now.AddSeconds(3); while (true) { if (DateTime.Now > endTime) { Assert.Fail("swarmB did not close connection."); } if (me.ConnectedAddress == null) { break; } await Task.Delay(100); } // Reconnect remotePeer = (await swarm.ConnectAsync(peerBAddress)).RemotePeer; Assert.IsNotNull(remotePeer.ConnectedAddress); Assert.AreEqual(peerB.PublicKey, remotePeer.PublicKey); Assert.IsTrue(remotePeer.IsValid()); Assert.IsTrue(swarm.KnownPeers.Contains(peerB)); } finally { await swarm.StopAsync(); await swarmB.StopAsync(); } }