diff options
Diffstat (limited to 'p2p/discover/database_test.go')
-rw-r--r-- | p2p/discover/database_test.go | 74 |
1 files changed, 43 insertions, 31 deletions
diff --git a/p2p/discover/database_test.go b/p2p/discover/database_test.go index f327cf73b..3ed84a099 100644 --- a/p2p/discover/database_test.go +++ b/p2p/discover/database_test.go @@ -6,6 +6,7 @@ import ( "net" "os" "path/filepath" + "reflect" "testing" "time" ) @@ -85,11 +86,12 @@ func TestNodeDBInt64(t *testing.T) { } func TestNodeDBFetchStore(t *testing.T) { - node := &Node{ - ID: MustHexID("0x1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), - IP: net.IP([]byte{192, 168, 0, 1}), - TCPPort: 30303, - } + node := newNode( + MustHexID("0x1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), + net.IP{192, 168, 0, 1}, + 30303, + 30303, + ) inst := time.Now() db, _ := newNodeDB("", Version) @@ -124,34 +126,40 @@ func TestNodeDBFetchStore(t *testing.T) { } if stored := db.node(node.ID); stored == nil { t.Errorf("node: not found") - } else if !bytes.Equal(stored.ID[:], node.ID[:]) || !stored.IP.Equal(node.IP) || stored.TCPPort != node.TCPPort { + } else if !reflect.DeepEqual(stored, node) { t.Errorf("node: data mismatch: have %v, want %v", stored, node) } } var nodeDBSeedQueryNodes = []struct { - node Node + node *Node pong time.Time }{ { - node: Node{ - ID: MustHexID("0x01d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), - IP: []byte{127, 0, 0, 1}, - }, + node: newNode( + MustHexID("0x01d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), + net.IP{127, 0, 0, 1}, + 30303, + 30303, + ), pong: time.Now().Add(-2 * time.Second), }, { - node: Node{ - ID: MustHexID("0x02d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), - IP: []byte{127, 0, 0, 2}, - }, + node: newNode( + MustHexID("0x02d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), + net.IP{127, 0, 0, 2}, + 30303, + 30303, + ), pong: time.Now().Add(-3 * time.Second), }, { - node: Node{ - ID: MustHexID("0x03d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), - IP: []byte{127, 0, 0, 3}, - }, + node: newNode( + MustHexID("0x03d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), + net.IP{127, 0, 0, 3}, + 30303, + 30303, + ), pong: time.Now().Add(-1 * time.Second), }, } @@ -162,7 +170,7 @@ func TestNodeDBSeedQuery(t *testing.T) { // Insert a batch of nodes for querying for i, seed := range nodeDBSeedQueryNodes { - if err := db.updateNode(&seed.node); err != nil { + if err := db.updateNode(seed.node); err != nil { t.Fatalf("node %d: failed to insert: %v", i, err) } } @@ -202,7 +210,7 @@ func TestNodeDBSeedQueryContinuation(t *testing.T) { // Insert a batch of nodes for querying for i, seed := range nodeDBSeedQueryNodes { - if err := db.updateNode(&seed.node); err != nil { + if err := db.updateNode(seed.node); err != nil { t.Fatalf("node %d: failed to insert: %v", i, err) } } @@ -266,22 +274,26 @@ func TestNodeDBPersistency(t *testing.T) { } var nodeDBExpirationNodes = []struct { - node Node + node *Node pong time.Time exp bool }{ { - node: Node{ - ID: MustHexID("0x01d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), - IP: []byte{127, 0, 0, 1}, - }, + node: newNode( + MustHexID("0x01d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), + net.IP{127, 0, 0, 1}, + 30303, + 30303, + ), pong: time.Now().Add(-nodeDBNodeExpiration + time.Minute), exp: false, }, { - node: Node{ - ID: MustHexID("0x02d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), - IP: []byte{127, 0, 0, 2}, - }, + node: newNode( + MustHexID("0x02d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), + net.IP{127, 0, 0, 2}, + 30303, + 30303, + ), pong: time.Now().Add(-nodeDBNodeExpiration - time.Minute), exp: true, }, @@ -293,7 +305,7 @@ func TestNodeDBExpiration(t *testing.T) { // Add all the test nodes and set their last pong time for i, seed := range nodeDBExpirationNodes { - if err := db.updateNode(&seed.node); err != nil { + if err := db.updateNode(seed.node); err != nil { t.Fatalf("node %d: failed to insert: %v", i, err) } if err := db.updateLastPong(seed.node.ID, seed.pong); err != nil { |