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

import (
Jeromy's avatar
Jeromy committed
4
	"context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
6
	"testing"

Jeromy's avatar
Jeromy committed
7
	addrutil "github.com/libp2p/go-addr-util"
Jeromy's avatar
Jeromy committed
8
	metrics "github.com/libp2p/go-libp2p-metrics"
Jeromy's avatar
Jeromy committed
9
	pstore "github.com/libp2p/go-libp2p-peerstore"
Jeromy's avatar
Jeromy committed
10
	testutil "github.com/libp2p/go-testutil"
Jeromy's avatar
Jeromy committed
11
	ma "github.com/multiformats/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
)

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
28
		m("/ip6/fe80::1/tcp/0"),              // link local
29
		m("/ip6/fe80::100/tcp/1234"),         // link local
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30
31
32
	}

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

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

	// test filters

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

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

54
55
56
	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
57
58
59
60
61
62
63
64

	// now test it with swarm

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

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

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

Jeromy's avatar
Jeromy committed
72
	if _, err := NewNetwork(ctx, goodAndBad, id, ps, metrics.NewBandwidthCounter()); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
		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)
		}
	}
}
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

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
113
		s.peers.AddAddr(p, a, pstore.PermanentAddrTTL)
114
		if _, err := s.Dial(ctx, p); err == nil {
115
			t.Errorf("swarm should not dial: %s", p)
116
117
118
119
120
121
122
		}
	}

	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
}