github.com/turingchain2020/turingchain@v1.1.21/system/p2p/dht/manage/connectionGater_test.go (about) 1 package manage 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/assert" 10 11 "github.com/libp2p/go-libp2p" 12 core "github.com/libp2p/go-libp2p-core" 13 "github.com/libp2p/go-libp2p-core/host" 14 "github.com/libp2p/go-libp2p-core/network" 15 "github.com/libp2p/go-libp2p-core/peer" 16 "github.com/multiformats/go-multiaddr" 17 "github.com/stretchr/testify/require" 18 ) 19 20 func newTestHost(port int) (core.Host, error) { 21 m, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)) 22 if err != nil { 23 return nil, err 24 } 25 26 return libp2p.New(context.Background(), 27 libp2p.ListenAddrs(m), 28 ) 29 30 } 31 32 func Test_MaxLimit(t *testing.T) { 33 m, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 12345)) 34 require.Nil(t, err) 35 36 var host1 host.Host 37 CacheLimit = 0 38 gater := NewConnGater(&host1, 1, nil, nil) 39 host1, err = libp2p.New(context.Background(), 40 libp2p.ListenAddrs(m), 41 libp2p.ConnectionGater(gater), 42 ) 43 require.Nil(t, err) 44 45 host2, err := newTestHost(12346) 46 require.Nil(t, err) 47 h1info := peer.AddrInfo{ 48 ID: host1.ID(), 49 Addrs: host1.Addrs(), 50 } 51 err = host2.Connect(context.Background(), h1info) 52 require.Nil(t, err) 53 54 host3, err := newTestHost(12347) 55 require.Nil(t, err) 56 //超过上限,会拒绝连接,所以host3连接host1会被拒绝,连接失败 57 err = host3.Connect(context.Background(), h1info) 58 assert.NotNil(t, err) 59 } 60 61 func Test_InterceptAccept(t *testing.T) { 62 var host1 host.Host 63 gater := NewConnGater(&host1, 0, nil, nil) 64 65 var ip = "47.97.223.101" 66 multiAddress, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, 3000)) 67 require.NoError(t, err) 68 for i := 0; i < ipBurst; i++ { 69 valid := gater.validateDial(multiAddress) 70 require.True(t, valid) 71 } 72 valid := gater.validateDial(multiAddress) 73 require.False(t, valid) 74 //test whiteList 75 whitePeer1, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%v", "192.168.102.123", 3001, "16Uiu2HAmK9PAPYoTzHnobzB5nQFnY7p9ZVcJYQ1BgzKCr7izAhbJ")) 76 peerInfo, err := peer.AddrInfoFromP2pAddr(whitePeer1) 77 assert.Nil(t, err) 78 gater2 := NewConnGater(&host1, 0, nil, []*peer.AddrInfo{peerInfo}) 79 whitePeer2, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", "192.168.102.123", 3002)) 80 //在白名单内部,校验通过 81 assert.True(t, gater2.checkWhitAddr(whitePeer2)) 82 //通过peerID 校验白名单 83 assert.True(t, gater2.checkWhitePeerList(peerInfo.ID)) 84 85 } 86 87 func Test_InterceptAddrDial(t *testing.T) { 88 var host1 host.Host 89 gater := NewConnGater(&host1, 0, nil, nil) 90 var ip = "47.97.223.101" 91 multiAddress, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, 3000)) 92 require.NoError(t, err) 93 require.True(t, gater.InterceptAddrDial("", multiAddress)) 94 } 95 96 func Test_InterceptPeerDial(t *testing.T) { 97 var host1 host.Host 98 ctx := context.Background() 99 defer ctx.Done() 100 whitePeer1, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%v", "192.168.105.123", 3001, "16Uiu2HAmK9PAPYoTzHnobzB5nQFnY7p9ZVcJYQ1BgzKCr7izAhbJ")) 101 peerInfo, err := peer.AddrInfoFromP2pAddr(whitePeer1) 102 require.Nil(t, err) 103 gater := NewConnGater(&host1, 1, NewTimeCache(context.Background(), time.Second), nil) 104 var pid = "16Uiu2HAmCyJhBvE1vn62MQWhhaPph1cxeU9nNZJoZQ1Pe1xASZUg" 105 106 gater.blacklist.Add(pid, 0) 107 id, err := peer.Decode(pid) 108 require.NoError(t, err) 109 ok := gater.InterceptPeerDial(id) 110 require.False(t, ok) 111 time.Sleep(time.Second * 2) 112 ok = gater.InterceptPeerDial(id) 113 require.True(t, ok) 114 //白名单校验 115 gater = NewConnGater(&host1, 1, NewTimeCache(context.Background(), time.Second), []*peer.AddrInfo{peerInfo}) 116 ok = gater.InterceptPeerDial(id) 117 //因为ID不在白名单内部,所有会被拦截 118 require.False(t, ok) 119 120 } 121 122 func Test_otherInterface(t *testing.T) { 123 var host1 host.Host 124 ctx := context.Background() 125 defer ctx.Done() 126 gater := NewConnGater(&host1, 1, NewTimeCache(context.Background(), time.Second), nil) 127 allow, _ := gater.InterceptUpgraded(nil) 128 require.True(t, allow) 129 require.True(t, gater.InterceptSecured(network.DirInbound, "", nil)) 130 131 } 132 133 func Test_timecache(t *testing.T) { 134 ctx, cancel := context.WithCancel(context.Background()) 135 cache := NewTimeCache(ctx, time.Second) 136 cache.Add("one", 0) 137 cache.Add("two", time.Second*3) 138 cache.Add("three", time.Second*5) 139 time.Sleep(time.Second * 2) 140 require.False(t, cache.Has("one")) 141 require.True(t, cache.Has("two")) 142 require.True(t, cache.Has("three")) 143 time.Sleep(time.Second * 2) 144 require.False(t, cache.Has("two")) 145 require.True(t, cache.Has("three")) 146 cancel() 147 time.Sleep(time.Second * 2) 148 require.True(t, cache.Has("three")) 149 150 }