swarm_addr_test.go 2.73 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
4
5
package swarm

import (
	"testing"

6
7
8
	metrics "github.com/libp2p/go-libp2p/p2p/metrics"
	addrutil "github.com/libp2p/go-libp2p/p2p/net/swarm/addr"
	testutil "github.com/libp2p/go-libp2p/testutil"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9

Jeromy's avatar
Jeromy committed
10
	pstore "github.com/ipfs/go-libp2p-peerstore"
Jeromy's avatar
Jeromy committed
11
12
	ma "github.com/jbenet/go-multiaddr"
	context "golang.org/x/net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
)

func TestFilterAddrs(t *testing.T) {

	m := func(s string) ma.Multiaddr {
		maddr, err := ma.NewMultiaddr(s)
		if err != nil {
			t.Fatal(err)
		}
		return maddr
	}

	bad := []ma.Multiaddr{
		m("/ip4/1.2.3.4/udp/1234"),           // unreliable
		m("/ip4/1.2.3.4/udp/1234/sctp/1234"), // not in manet
		m("/ip4/1.2.3.4/udp/1234/udt"),       // udt is broken on arm
29
		m("/ip6/fe80::1/tcp/0"),              // link local
30
		m("/ip6/fe80::100/tcp/1234"),         // link local
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31
32
33
	}

	good := []ma.Multiaddr{
34
35
		m("/ip4/127.0.0.1/tcp/0"),
		m("/ip6/::1/tcp/0"),
36
		m("/ip4/1.2.3.4/udp/1234/utp"),
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37
38
39
40
41
42
43
	}

	goodAndBad := append(good, bad...)

	// test filters

	for _, a := range bad {
44
		if addrutil.AddrUsable(a, false) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45
46
47
48
49
			t.Errorf("addr %s should be unusable", a)
		}
	}

	for _, a := range good {
50
		if !addrutil.AddrUsable(a, false) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51
52
53
54
			t.Errorf("addr %s should be usable", a)
		}
	}

55
56
57
	subtestAddrsEqual(t, addrutil.FilterUsableAddrs(bad), []ma.Multiaddr{})
	subtestAddrsEqual(t, addrutil.FilterUsableAddrs(good), good)
	subtestAddrsEqual(t, addrutil.FilterUsableAddrs(goodAndBad), good)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58
59
60
61
62
63
64
65

	// now test it with swarm

	id, err := testutil.RandPeerID()
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
66
	ps := pstore.NewPeerstore()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67
68
	ctx := context.Background()

Jeromy's avatar
Jeromy committed
69
	if _, err := NewNetwork(ctx, bad, id, ps, metrics.NewBandwidthCounter()); err == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70
71
72
		t.Fatal("should have failed to create swarm")
	}

Jeromy's avatar
Jeromy committed
73
	if _, err := NewNetwork(ctx, goodAndBad, id, ps, metrics.NewBandwidthCounter()); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
		t.Fatal("should have succeeded in creating swarm", err)
	}
}

func subtestAddrsEqual(t *testing.T, a, b []ma.Multiaddr) {
	if len(a) != len(b) {
		t.Error(t)
	}

	in := func(addr ma.Multiaddr, l []ma.Multiaddr) bool {
		for _, addr2 := range l {
			if addr.Equal(addr2) {
				return true
			}
		}
		return false
	}

	for _, aa := range a {
		if !in(aa, b) {
			t.Errorf("%s not in %s", aa, b)
		}
	}
}
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

func TestDialBadAddrs(t *testing.T) {

	m := func(s string) ma.Multiaddr {
		maddr, err := ma.NewMultiaddr(s)
		if err != nil {
			t.Fatal(err)
		}
		return maddr
	}

	ctx := context.Background()
	s := makeSwarms(ctx, t, 1)[0]

	test := func(a ma.Multiaddr) {
		p := testutil.RandPeerIDFatal(t)
Jeromy's avatar
Jeromy committed
114
		s.peers.AddAddr(p, a, pstore.PermanentAddrTTL)
115
		if _, err := s.Dial(ctx, p); err == nil {
116
			t.Errorf("swarm should not dial: %s", p)
117
118
119
120
121
122
123
		}
	}

	test(m("/ip6/fe80::1"))                // link local
	test(m("/ip6/fe80::100"))              // link local
	test(m("/ip4/127.0.0.1/udp/1234/utp")) // utp
}