func TestOnDisconnect(t *testing.T) { gnet.EraseMessages() d := newDefaultDaemon() c := gnetConnection(addr) var mirror uint32 = 100 // Not blacklistable reason := gnet.DisconnectWriteFailed setupTestOnDisconnect(d, c, mirror) assert.NotPanics(t, func() { d.onGnetDisconnect(c, reason) }) // Should not be in blacklist assert.Equal(t, len(d.Peers.Peers.Blacklist), 0) // Should no longer be in OutgoingConnections assert.Equal(t, len(d.OutgoingConnections), 0) // Should no longer be in d.ExpectingIntroductions assert.Equal(t, len(d.ExpectingIntroductions), 0) // Should be removed from the mirror, and the mirror dict for this ip // should be removed assert.Equal(t, len(d.mirrorConnections), 0) assert.Equal(t, len(d.ConnectionMirrors), 0) // Blacklistable reason = DisconnectIntroductionTimeout setupTestOnDisconnect(d, c, mirror) assert.NotPanics(t, func() { d.onGnetDisconnect(c, reason) }) assert.Equal(t, len(d.Peers.Peers.Blacklist), 1) assert.NotNil(t, d.Peers.Peers.Blacklist[addr]) // Should be in blacklist assert.Equal(t, len(d.Peers.Peers.Blacklist), 1) assert.NotNil(t, d.Peers.Peers.Blacklist[addr]) // Should no longer be in OutgoingConnections assert.Equal(t, len(d.OutgoingConnections), 0) // Should no longer be in d.ExpectingIntroductions assert.Equal(t, len(d.ExpectingIntroductions), 0) // Should be removed from the mirror, and the mirror dict for this ip // should be removed assert.Equal(t, len(d.mirrorConnections), 0) assert.Equal(t, len(d.ConnectionMirrors), 0) // Cleanup delete(d.Peers.Peers.Blacklist, addr) // d.mirrorConnections should retain a submap if there are other ports // inside reason = gnet.DisconnectWriteFailed setupTestOnDisconnect(d, c, mirror) d.mirrorConnections[mirror][strings.Split(addrb, ":")[0]] = addrPort assert.NotPanics(t, func() { d.onGnetDisconnect(c, reason) }) // Should not be in blacklist assert.Equal(t, len(d.Peers.Peers.Blacklist), 0) // Should no longer be in OutgoingConnections assert.Equal(t, len(d.OutgoingConnections), 0) // Should no longer be in d.ExpectingIntroductions assert.Equal(t, len(d.ExpectingIntroductions), 0) // Should be removed from the mirror, and the mirror dict for this ip // should be removed assert.Equal(t, len(d.mirrorConnections), 1) assert.Equal(t, len(d.mirrorConnections[mirror]), 1) assert.Equal(t, len(d.ConnectionMirrors), 0) shutdown(d) }
func TestStart(t *testing.T) { gnet.EraseMessages() defer cleanupPeers() d, quit := setupDaemonLoopDHT() defer closeDaemon(d, quit) assert.NotNil(t, d) assert.NotNil(t, d.Pool) assert.NotNil(t, d.DHT) go d.Start(quit) wait() assert.NotEqual(t, len(gnet.MessageIdMap), 0) assert.NotNil(t, d.Pool) assert.NotNil(t, d.Peers) assert.NotNil(t, d.DHT) assert.NotNil(t, d.Messages) assert.NotNil(t, d.Gateway) }
func TestOnConnect(t *testing.T) { d := newDefaultDaemon() // Test a valid connection, unsolicited e := ConnectEvent{addr, false} p, _ := d.Peers.Peers.AddPeer(addr) c := setupExistingPool(d.Pool) go d.Pool.Pool.ConnectionWriteLoop(c) d.pendingConnections[addr] = p assert.NotPanics(t, func() { d.onConnect(e) }) wait() assert.Equal(t, len(d.Pool.Pool.SendResults), 1) if len(d.Pool.Pool.SendResults) == 0 { t.Fatalf("SendResults empty, would block") } sr := <-d.Pool.Pool.SendResults assert.Equal(t, sr.Connection, c) assert.Nil(t, sr.Error) _, ok := sr.Message.(*IntroductionMessage) assert.True(t, ok) // This connection should no longer be pending assert.Equal(t, len(d.pendingConnections), 0) // This is not an outgoing connection, we did not solicit it assert.Equal(t, len(d.OutgoingConnections), 0) // We should be expecting its version assert.Equal(t, len(d.ExpectingIntroductions), 1) _, exists := d.ExpectingIntroductions[addr] assert.True(t, exists) // An introduction should have been sent assert.False(t, c.LastSent.IsZero()) // d.ipCounts should be 1 assert.Equal(t, d.ipCounts[addrIP], 1) // Cleanup delete(d.ipCounts, addrIP) delete(d.ExpectingIntroductions, addr) c.Close() // Test a valid connection, solicited e = ConnectEvent{addr, true} c = gnetConnection(addr) go d.Pool.Pool.ConnectionWriteLoop(c) d.pendingConnections[addr] = p d.Pool.Pool.Addresses[addr] = c d.Pool.Pool.Pool[c.Id] = c assert.NotPanics(t, func() { d.onConnect(e) }) wait() assert.Equal(t, len(d.Pool.Pool.SendResults), 1) if len(d.Pool.Pool.SendResults) == 0 { t.Fatalf("SendResults empty, would block") } sr = <-d.Pool.Pool.SendResults assert.Equal(t, sr.Connection, c) assert.Nil(t, sr.Error) _, ok = sr.Message.(*IntroductionMessage) assert.True(t, ok) // This connection should no longer be pending assert.Equal(t, len(d.pendingConnections), 0) // We should mark this as an outgoing connection since we solicited it assert.Equal(t, len(d.OutgoingConnections), 1) assert.NotNil(t, d.OutgoingConnections[addr]) // We should be expecting its version assert.Equal(t, len(d.ExpectingIntroductions), 1) _, exists = d.ExpectingIntroductions[addr] assert.True(t, exists) // An introduction should have been sent assert.False(t, c.LastSent.IsZero()) // d.ipCounts should be 1 assert.Equal(t, d.ipCounts[addrIP], 1) // Cleanup c.Close() delete(d.ExpectingIntroductions, addr) delete(d.OutgoingConnections, addr) delete(d.ipCounts, addrIP) // Test a connection that is not connected by the time of processing c.LastSent = util.ZeroTime() e = ConnectEvent{addr, true} delete(d.Pool.Pool.Addresses, addr) d.pendingConnections[addr] = p assert.NotPanics(t, func() { d.onConnect(e) }) wait() assert.Equal(t, len(d.Pool.Pool.SendResults), 0) // This connection should no longer be pending assert.Equal(t, len(d.pendingConnections), 0) // No message should have been sent assert.True(t, c.LastSent.IsZero()) // We should not be expecting its version assert.Equal(t, len(d.ExpectingIntroductions), 0) // We should not have recorded it to ipCount assert.Equal(t, d.ipCounts[addrIP], 0) // Test a connection that is blacklisted e = ConnectEvent{addr, true} c = gnetConnection(addr) go d.Pool.Pool.ConnectionWriteLoop(c) d.Peers.Peers.AddBlacklistEntry(addr, time.Hour) d.pendingConnections[addr] = p d.Pool.Pool.Addresses[addr] = c d.Pool.Pool.Pool[c.Id] = c assert.NotPanics(t, func() { d.onConnect(e) }) wait() // No introduction should have been sent assert.Equal(t, len(d.Pool.Pool.SendResults), 0) // This connection should no longer be pending assert.Equal(t, len(d.pendingConnections), 0) // No message should have been sent assert.True(t, c.LastSent.IsZero()) // We should not be expecting its version assert.Equal(t, len(d.ExpectingIntroductions), 0) // We should not have recorded its ipCount assert.Equal(t, d.ipCounts[addrIP], 0) // We should be looking to disconnect this client assert.Equal(t, len(d.Pool.Pool.DisconnectQueue), 1) if len(d.Pool.Pool.DisconnectQueue) == 0 { t.Fatal("pool.Pool.DisconnectQueue is empty, would block") } de := <-d.Pool.Pool.DisconnectQueue assert.Equal(t, de.ConnId, 1) assert.Equal(t, de.Reason, DisconnectIsBlacklisted) // Cleanup c.Close() delete(d.Peers.Peers.Blacklist, addr) // Test a connection that has reached maxed ipCount e = ConnectEvent{addr, true} c = gnetConnection(addr) go d.Pool.Pool.ConnectionWriteLoop(c) d.ipCounts[addrIP] = d.Config.IPCountsMax d.pendingConnections[addr] = p d.Pool.Pool.Addresses[addr] = c d.Pool.Pool.Pool[c.Id] = c assert.NotPanics(t, func() { d.onConnect(e) }) wait() // No introduction should have been sent assert.Equal(t, len(d.Pool.Pool.SendResults), 0) // This connection should no longer be pending assert.Equal(t, len(d.pendingConnections), 0) // No message should have been sent assert.True(t, c.LastSent.IsZero()) // We should not be expecting its version assert.Equal(t, len(d.ExpectingIntroductions), 0) // d.ipCounts should be unchanged assert.Equal(t, d.ipCounts[addrIP], d.Config.IPCountsMax) // We should be looking to disconnect this client assert.Equal(t, len(d.Pool.Pool.DisconnectQueue), 1) if len(d.Pool.Pool.DisconnectQueue) == 0 { t.Fatal("pool.Pool.DisconnectQueue is empty, would block") } de = <-d.Pool.Pool.DisconnectQueue assert.Equal(t, de.ConnId, 1) assert.Equal(t, de.Reason, DisconnectIPLimitReached) // Cleanup c.Close() delete(d.ipCounts, addrIP) gnet.EraseMessages() shutdown(d) }
func shutdown(d *Daemon) { d.Shutdown() wait() cleanupPeers() gnet.EraseMessages() }