Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Jan 18, 2018
1 parent 3ba8585 commit 606e8f7
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 3 deletions.
130 changes: 130 additions & 0 deletions modules/gateway/gateway_test.go
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"sync"
"testing"
"time"

"github.com/NebulousLabs/Sia/build"
"github.com/NebulousLabs/Sia/modules"
Expand Down Expand Up @@ -209,3 +210,132 @@ func TestParallelClose(t *testing.T) {
}
wg.Wait()
}

// TestManualConnectDisconnect checks if a user initiated connect and
// disconnect works as expected.
func TestManualConnectDisconnect(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
t.Parallel()
g1 := newNamedTestingGateway(t, "1")
defer g1.Close()
g2 := newNamedTestingGateway(t, "2")
defer g2.Close()

// g1 should be able to connect to g2
err := g1.Connect(g2.Address())
if err != nil {
t.Fatal("failed to connect:", err)
}

// g2 manually disconnects from g1 and therefore blacklists it
err = g2.DisconnectManual(g1.Address())
if err != nil {
t.Fatal("failed to disconnect:", err)
}
time.Sleep(time.Second)

// Neither g1 nor g2 can connect after g1 being blacklisted
err = g1.Connect(g2.Address())
if err == nil {
t.Fatal("shouldn't be able to connect")
}
err = g1.ConnectManual(g2.Address())
if err == nil {
t.Fatal("shouldn't be able to connect")
}
err = g2.Connect(g1.Address())
if err == nil {
t.Fatal("shouldn't be able to connect")
}

// g2 manually connects and therefore removes g1 from the blacklist again
err = g2.ConnectManual(g1.Address())
if err != nil {
t.Fatal("failed to connect:", err)
}

// g2 disconnects and lets g1 connect which should also be possible now
err = g2.Disconnect(g1.Address())
if err != nil {
t.Fatal("failed to disconnect:", err)
}
time.Sleep(time.Second)
err = g1.Connect(g2.Address())
if err != nil {
t.Fatal("failed to connect:", err)
}

// same thing again but the other way round
err = g2.Disconnect(g1.Address())
if err != nil {
t.Fatal("failed to disconnect:", err)
}
time.Sleep(time.Second)
err = g2.Connect(g1.Address())
if err != nil {
t.Fatal("failed to connect:", err)
}
}

// TestManualConnectDisconnectPersist checks if the blacklist is persistet on
// disk
func TestManualConnectDisconnectPersist(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
t.Parallel()
g1 := newNamedTestingGateway(t, "1")
defer g1.Close()
g2 := newNamedTestingGateway(t, "2")

// g1 should be able to connect to g2
err := g1.Connect(g2.Address())
if err != nil {
t.Fatal("failed to connect:", err)
}

// g2 manually disconnects from g1 and therefore blacklists it
err = g2.DisconnectManual(g1.Address())
if err != nil {
t.Fatal("failed to disconnect:", err)
}
time.Sleep(time.Second)

// Neither g1 nor g2 can connect after g1 being blacklisted
err = g1.Connect(g2.Address())
if err == nil {
t.Fatal("shouldn't be able to connect")
}
err = g1.ConnectManual(g2.Address())
if err == nil {
t.Fatal("shouldn't be able to connect")
}
err = g2.Connect(g1.Address())
if err == nil {
t.Fatal("shouldn't be able to connect")
}

// Restart g2 without deleting the tmp dir
g2.Close()
g2, err = New("localhost:0", false, g2.persistDir)
if err != nil {
t.Fatal(err)
}
defer g2.Close()

// Neither g1 nor g2 can connect. Since g1 is still blacklisted
err = g1.Connect(g2.Address())
if err == nil {
t.Fatal("shouldn't be able to connect")
}
err = g1.ConnectManual(g2.Address())
if err == nil {
t.Fatal("shouldn't be able to connect")
}
err = g2.Connect(g1.Address())
if err == nil {
t.Fatal("shouldn't be able to connect")
}
}
14 changes: 11 additions & 3 deletions modules/gateway/peers.go
Expand Up @@ -128,7 +128,10 @@ func (g *Gateway) threadedAcceptConn(conn net.Conn) {
addr := modules.NetAddress(conn.RemoteAddr().String())
g.log.Debugf("INFO: %v wants to connect", addr)

if _, exists := g.blacklist[addr.Host()]; exists {
g.mu.RLock()
_, exists := g.blacklist[addr.Host()]
g.mu.RUnlock()
if exists {
g.log.Debugf("INFO: %v was rejected. (blacklisted)")
conn.Close()
return
Expand Down Expand Up @@ -518,9 +521,13 @@ func (g *Gateway) Disconnect(addr modules.NetAddress) error {
// from the blacklist.
func (g *Gateway) ConnectManual(addr modules.NetAddress) error {
g.mu.Lock()
delete(g.blacklist, addr.Host())
var err error
if _, exists := g.blacklist[addr.Host()]; exists {
delete(g.blacklist, addr.Host())
err = g.saveBlacklist()
}
g.mu.Unlock()
return g.Connect(addr)
return build.ComposeErrors(err, g.Connect(addr))
}

// DisconnectManual is a wrapper for the Disconnect function. It is
Expand All @@ -531,6 +538,7 @@ func (g *Gateway) DisconnectManual(addr modules.NetAddress) error {
if err == nil {
g.mu.Lock()
g.blacklist[addr.Host()] = struct{}{}
err = g.saveBlacklist()
g.mu.Unlock()
}
return err
Expand Down

0 comments on commit 606e8f7

Please sign in to comment.