Skip to content

Commit 6400d24

Browse files
committed
test filter caching
1 parent 8c2c236 commit 6400d24

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

dht_filters_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package dht
22

33
import (
4+
"context"
5+
"net"
46
"testing"
57

8+
ic "github.com/libp2p/go-libp2p-core/crypto"
9+
"github.com/libp2p/go-libp2p-core/network"
10+
"github.com/libp2p/go-libp2p-core/peer"
611
"github.com/multiformats/go-multiaddr"
12+
ma "github.com/multiformats/go-multiaddr"
13+
manet "github.com/multiformats/go-multiaddr-net"
714
)
815

916
func TestIsRelay(t *testing.T) {
@@ -21,3 +28,39 @@ func TestIsRelay(t *testing.T) {
2128
}
2229

2330
}
31+
32+
type mockConn struct {
33+
local peer.AddrInfo
34+
remote peer.AddrInfo
35+
}
36+
37+
func (m *mockConn) Close() error { return nil }
38+
func (m *mockConn) NewStream() (network.Stream, error) { return nil, nil }
39+
func (m *mockConn) GetStreams() []network.Stream { return []network.Stream{} }
40+
func (m *mockConn) Stat() network.Stat { return network.Stat{Direction: network.DirOutbound} }
41+
func (m *mockConn) LocalMultiaddr() ma.Multiaddr { return m.local.Addrs[0] }
42+
func (m *mockConn) RemoteMultiaddr() ma.Multiaddr { return m.remote.Addrs[0] }
43+
func (m *mockConn) LocalPeer() peer.ID { return m.local.ID }
44+
func (m *mockConn) LocalPrivateKey() ic.PrivKey { return nil }
45+
func (m *mockConn) RemotePeer() peer.ID { return m.remote.ID }
46+
func (m *mockConn) RemotePublicKey() ic.PubKey { return nil }
47+
48+
func TestFilterCaching(t *testing.T) {
49+
ctx, cancel := context.WithCancel(context.Background())
50+
defer cancel()
51+
d := setupDHT(ctx, t, true)
52+
53+
remote, _ := manet.FromIP(net.IPv4(8, 8, 8, 8))
54+
if PrivateRoutingTableFilter(d, []network.Conn{&mockConn{
55+
local: d.Host().Peerstore().PeerInfo(d.Host().ID()),
56+
remote: peer.AddrInfo{ID: "", Addrs: []ma.Multiaddr{remote}},
57+
}}) {
58+
t.Fatal("filter should prevent public remote peers.")
59+
}
60+
61+
r1 := getCachedRouter()
62+
r2 := getCachedRouter()
63+
if r1 != r2 {
64+
t.Fatal("router should be returned multiple times.")
65+
}
66+
}

0 commit comments

Comments
 (0)