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

import (
	"testing"

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

11
12
	ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/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
29
)

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/utp"),       // utp is broken
		m("/ip4/1.2.3.4/udp/1234/udt"),       // udt is broken on arm
30
		m("/ip6/fe80::1/tcp/0"),              // link local
31
		m("/ip6/fe80::100/tcp/1234"),         // link local
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32
33
34
	}

	good := []ma.Multiaddr{
35
36
		m("/ip4/127.0.0.1/tcp/0"),
		m("/ip6/::1/tcp/0"),
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, true) {
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, true) {
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
66
67
68

	// now test it with swarm

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

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

	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
}