-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiaddr_test.go
80 lines (77 loc) · 2.24 KB
/
multiaddr_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
package caskadht
import (
"testing"
"github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
)
func Test_IsPubliclyDialableAddr(t *testing.T) {
tests := []struct {
name string
given []string
want []string
}{
{
name: "nil",
},
{
name: "empty",
given: []string{},
},
{
name: "bind addr",
given: []string{"/ip4/0.0.0.0/"},
},
{
name: "dns4",
given: []string{
"/dns4/example.invalid",
"/ip4/0.0.0.0/",
},
want: []string{"/dns4/example.invalid"},
},
{
name: "mix",
given: []string{
"/ip6/2604:1380:1000:6000::1/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/sjc-1.bootstrap.libp2p.io/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/localhost/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/ip4/147.75.83.83/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
"/ip4/127.0.0.1/tcp/4001",
"/ip4/127.0.0.1/udp/4001",
},
want: []string{
"/ip6/2604:1380:1000:6000::1/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/sjc-1.bootstrap.libp2p.io/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/ip4/147.75.83.83/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
},
},
{
name: "dns localhost",
given: []string{
"/dns/localhost/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dns4/localhost/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dns6/localhost/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/localhost/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
givenAddrs := make([]multiaddr.Multiaddr, 0, len(tt.given))
for _, a := range tt.given {
addr, err := multiaddr.NewMultiaddr(a)
require.NoError(t, err)
givenAddrs = append(givenAddrs, addr)
}
gotAddrs := multiaddr.FilterAddrs(givenAddrs, IsPubliclyDialableAddr)
if tt.want == nil {
require.Empty(t, gotAddrs)
} else {
require.Equal(t, len(tt.want), len(gotAddrs))
for i, addr := range gotAddrs {
require.Equal(t, tt.want[i], addr.String())
}
}
})
}
}