forked from libp2p/go-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_test.go
116 lines (90 loc) · 2.85 KB
/
options_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package libp2p
import (
"context"
"net"
"testing"
"time"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/test"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/stretchr/testify/require"
)
func TestDeprecatedFiltersOptionsOutbound(t *testing.T) {
require := require.New(t)
f := ma.NewFilters()
_, ipnet, _ := net.ParseCIDR("127.0.0.0/24")
f.AddFilter(*ipnet, ma.ActionDeny)
host0, err := New(context.TODO(), Filters(f))
require.NoError(err)
require.NotNil(host0)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
id, _ := test.RandPeerID()
addr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0/p2p/" + id.Pretty())
ai, _ := peer.AddrInfoFromP2pAddr(addr)
err = host0.Connect(ctx, *ai)
require.Error(err)
require.Contains(err.Error(), "no good addresses")
}
var (
ip4FullMask = net.IPMask{255, 255, 255, 255}
ip6FullMask = net.IPMask{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}
)
func TestDeprecatedFiltersOptionsInbound(t *testing.T) {
require := require.New(t)
host0, err := New(context.TODO())
require.NoError(err)
require.NotNil(host0)
f := ma.NewFilters()
for _, addr := range host0.Addrs() {
ip, err := manet.ToIP(addr)
require.NoError(err)
require.NotNil(t, ip)
var mask net.IPMask
if ip.To4() != nil {
mask = ip4FullMask
} else {
mask = ip6FullMask
}
ipnet := net.IPNet{IP: ip, Mask: mask}
f.AddFilter(ipnet, ma.ActionDeny)
}
host1, err := New(context.TODO(), Filters(f))
require.NoError(err)
require.NotNil(host1)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
peerInfo := peer.AddrInfo{
ID: host1.ID(),
Addrs: host1.Addrs(),
}
err = host0.Connect(ctx, peerInfo)
require.Error(err)
}
func TestDeprecatedFiltersAndAddressesOptions(t *testing.T) {
require := require.New(t)
f := ma.NewFilters()
_, ipnet1, _ := net.ParseCIDR("127.0.0.0/24")
_, ipnet2, _ := net.ParseCIDR("128.0.0.0/24")
f.AddFilter(*ipnet1, ma.ActionDeny)
host, err := New(context.TODO(), Filters(f), FilterAddresses(ipnet2))
require.NoError(err)
require.NotNil(host)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
id, _ := test.RandPeerID()
addr1, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0/p2p/" + id.Pretty())
addr2, _ := ma.NewMultiaddr("/ip4/128.0.0.1/tcp/0/p2p/" + id.Pretty())
ai, _ := peer.AddrInfosFromP2pAddrs(addr1, addr2)
err = host.Connect(ctx, ai[0])
require.Error(err)
require.Contains(err.Error(), "no good addresses")
}
func TestCannotSetFiltersAndConnGater(t *testing.T) {
require := require.New(t)
f := ma.NewFilters()
_, err := New(context.TODO(), Filters(f), ConnectionGater(nil))
require.Error(err)
require.Contains(err.Error(), "cannot configure multiple connection gaters")
}