Add NoDefaultBootstrap and use it and testify in a few tests

This commit is contained in:
Matt Joiner 2015-12-16 15:13:32 +11:00
parent 86b0c02eac
commit 4798907da4
3 changed files with 22 additions and 16 deletions

View File

@ -38,12 +38,19 @@ type transactionKey struct {
// ServerConfig allows to set up a configuration of the `Server` instance
// to be created with NewServer
type ServerConfig struct {
Addr string // Listen address. Used if Conn is nil.
// Listen address. Used if Conn is nil.
Addr string
Conn net.PacketConn
// Don't respond to queries from other nodes.
Passive bool
// DHT Bootstrap nodes
BootstrapNodes []string
// Disable bootstrapping from global servers even if given no BootstrapNodes.
// This creates a solitary node that awaits other nodes; it's only useful if
// you're creating your own DHT and want to avoid accidental crossover, without
// spoofing a bootstrap node and filling your logs with connection errors.
NoDefaultBootstrap bool
// Disable the DHT security extension:
// http://www.libtorrent.org/dht_sec.html.
NoSecurity bool
@ -171,6 +178,7 @@ func (n *node) DefinitelyGood() bool {
}
return true
}
func jitterDuration(average time.Duration, plusMinus time.Duration) time.Duration {
return average - plusMinus/2 + time.Duration(rand.Int63n(int64(plusMinus)))
}

View File

@ -107,23 +107,23 @@ func TestDHTDefaultConfig(t *testing.T) {
}
func TestPing(t *testing.T) {
srv, err := NewServer(nil)
if err != nil {
t.Fatal(err)
}
srv, err := NewServer(&ServerConfig{
Addr: "127.0.0.1:5680",
NoDefaultBootstrap: true,
})
require.NoError(t, err)
defer srv.Close()
srv0, err := NewServer(nil)
if err != nil {
t.Fatal(err)
}
srv0, err := NewServer(&ServerConfig{
Addr: "127.0.0.1:5681",
BootstrapNodes: []string{"127.0.0.1:5680"},
})
require.NoError(t, err)
defer srv0.Close()
tn, err := srv.Ping(&net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: srv0.Addr().(*net.UDPAddr).Port,
})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer tn.Close()
ok := make(chan bool)
tn.SetResponseHandler(func(msg Msg, msgOk bool) {
@ -159,9 +159,7 @@ func TestDHTSec(t *testing.T) {
} {
ip := net.ParseIP(case_.ipStr)
id, err := hex.DecodeString(case_.nodeIDHex)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
secure := NodeIdSecure(string(id), ip)
if secure != case_.valid {
t.Fatalf("case failed: %v", case_)

View File

@ -541,7 +541,7 @@ func (s *Server) addRootNodes() error {
func (s *Server) bootstrap() (err error) {
s.mu.Lock()
defer s.mu.Unlock()
if len(s.nodes) == 0 {
if len(s.nodes) == 0 && !s.config.NoDefaultBootstrap {
err = s.addRootNodes()
}
if err != nil {