aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/enode/localnode_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'p2p/enode/localnode_test.go')
-rw-r--r--p2p/enode/localnode_test.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/p2p/enode/localnode_test.go b/p2p/enode/localnode_test.go
index f5e3496d6..00746a8d2 100644
--- a/p2p/enode/localnode_test.go
+++ b/p2p/enode/localnode_test.go
@@ -17,10 +17,13 @@
package enode
import (
+ "math/rand"
+ "net"
"testing"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/enr"
+ "github.com/stretchr/testify/assert"
)
func newLocalNodeForTesting() (*LocalNode, *DB) {
@@ -74,3 +77,46 @@ func TestLocalNodeSeqPersist(t *testing.T) {
t.Fatalf("wrong seq %d on instance with changed key, want 1", s)
}
}
+
+// This test checks behavior of the endpoint predictor.
+func TestLocalNodeEndpoint(t *testing.T) {
+ var (
+ fallback = &net.UDPAddr{IP: net.IP{127, 0, 0, 1}, Port: 80}
+ predicted = &net.UDPAddr{IP: net.IP{127, 0, 1, 2}, Port: 81}
+ staticIP = net.IP{127, 0, 1, 2}
+ )
+ ln, db := newLocalNodeForTesting()
+ defer db.Close()
+
+ // Nothing is set initially.
+ assert.Equal(t, net.IP(nil), ln.Node().IP())
+ assert.Equal(t, 0, ln.Node().UDP())
+ assert.Equal(t, uint64(1), ln.Node().Seq())
+
+ // Set up fallback address.
+ ln.SetFallbackIP(fallback.IP)
+ ln.SetFallbackUDP(fallback.Port)
+ assert.Equal(t, fallback.IP, ln.Node().IP())
+ assert.Equal(t, fallback.Port, ln.Node().UDP())
+ assert.Equal(t, uint64(2), ln.Node().Seq())
+
+ // Add endpoint statements from random hosts.
+ for i := 0; i < iptrackMinStatements; i++ {
+ assert.Equal(t, fallback.IP, ln.Node().IP())
+ assert.Equal(t, fallback.Port, ln.Node().UDP())
+ assert.Equal(t, uint64(2), ln.Node().Seq())
+
+ from := &net.UDPAddr{IP: make(net.IP, 4), Port: 90}
+ rand.Read(from.IP)
+ ln.UDPEndpointStatement(from, predicted)
+ }
+ assert.Equal(t, predicted.IP, ln.Node().IP())
+ assert.Equal(t, predicted.Port, ln.Node().UDP())
+ assert.Equal(t, uint64(3), ln.Node().Seq())
+
+ // Static IP overrides prediction.
+ ln.SetStaticIP(staticIP)
+ assert.Equal(t, staticIP, ln.Node().IP())
+ assert.Equal(t, fallback.Port, ln.Node().UDP())
+ assert.Equal(t, uint64(4), ln.Node().Seq())
+}