id_test.go 4.06 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
4
5
6
package identify_test

import (
	"testing"
	"time"

7
	ic "github.com/ipfs/go-libp2p-crypto"
Jeromy's avatar
Jeromy committed
8
	peer "github.com/ipfs/go-libp2p-peer"
9
10
11
	host "github.com/libp2p/go-libp2p/p2p/host"
	identify "github.com/libp2p/go-libp2p/p2p/protocol/identify"
	testutil "github.com/libp2p/go-libp2p/p2p/test/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12

13
	"context"
Jeromy's avatar
Jeromy committed
14
	ma "github.com/jbenet/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
)

func subtestIDService(t *testing.T, postDialWait time.Duration) {

	// the generated networks should have the id service wired in.
	ctx := context.Background()
	h1 := testutil.GenHostSwarm(t, ctx)
	h2 := testutil.GenHostSwarm(t, ctx)

	h1p := h1.ID()
	h2p := h2.ID()

	testKnowsAddrs(t, h1, h2p, []ma.Multiaddr{}) // nothing
	testKnowsAddrs(t, h2, h1p, []ma.Multiaddr{}) // nothing

	h2pi := h2.Peerstore().PeerInfo(h2p)
	if err := h1.Connect(ctx, h2pi); err != nil {
		t.Fatal(err)
	}

	// we need to wait here if Dial returns before ID service is finished.
	if postDialWait > 0 {
		<-time.After(postDialWait)
	}

	// the IDService should be opened automatically, by the network.
	// what we should see now is that both peers know about each others listen addresses.
42
	t.Log("test peer1 has peer2 addrs correctly")
43
	testKnowsAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) // has them
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44
	testHasProtocolVersions(t, h1, h2p)
45
	testHasPublicKey(t, h1, h2p, h2.Peerstore().PubKey(h2p)) // h1 should have h2's public key
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46
47
48
49
50
51
52
53
54

	// now, this wait we do have to do. it's the wait for the Listening side
	// to be done identifying the connection.
	c := h2.Network().ConnsToPeer(h1.ID())
	if len(c) < 1 {
		t.Fatal("should have connection by now at least.")
	}
	<-h2.IDService().IdentifyWait(c[0])

55
56
57
	addrs := h1.Peerstore().Addrs(h1p)
	addrs = append(addrs, c[0].RemoteMultiaddr())

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58
	// and the protocol versions.
59
60
	t.Log("test peer2 has peer1 addrs correctly")
	testKnowsAddrs(t, h2, h1p, addrs) // has them
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61
	testHasProtocolVersions(t, h2, h1p)
62
	testHasPublicKey(t, h2, h1p, h1.Peerstore().PubKey(h1p)) // h1 should have h2's public key
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
63
64
65
}

func testKnowsAddrs(t *testing.T, h host.Host, p peer.ID, expected []ma.Multiaddr) {
66
	actual := h.Peerstore().Addrs(p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67
68

	if len(actual) != len(expected) {
69
70
71
		t.Errorf("expected: %s", expected)
		t.Errorf("actual: %s", actual)
		t.Fatal("dont have the same addresses")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
	}

	have := map[string]struct{}{}
	for _, addr := range actual {
		have[addr.String()] = struct{}{}
	}
	for _, addr := range expected {
		if _, found := have[addr.String()]; !found {
			t.Errorf("%s did not have addr for %s: %s", h.ID(), p, addr)
			// panic("ahhhhhhh")
		}
	}
}

func testHasProtocolVersions(t *testing.T, h host.Host, p peer.ID) {
	v, err := h.Peerstore().Get(p, "ProtocolVersion")
	if v == nil {
		t.Error("no protocol version")
		return
	}
Jeromy's avatar
Jeromy committed
92
	if v.(string) != identify.LibP2PVersion {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
93
94
95
96
97
98
99
100
		t.Error("protocol mismatch", err)
	}
	v, err = h.Peerstore().Get(p, "AgentVersion")
	if v.(string) != identify.ClientVersion {
		t.Error("agent version mismatch", err)
	}
}

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
func testHasPublicKey(t *testing.T, h host.Host, p peer.ID, shouldBe ic.PubKey) {
	k := h.Peerstore().PubKey(p)
	if k == nil {
		t.Error("no public key")
		return
	}
	if !k.Equals(shouldBe) {
		t.Error("key mismatch")
		return
	}

	p2, err := peer.IDFromPublicKey(k)
	if err != nil {
		t.Error("could not make key")
	} else if p != p2 {
		t.Error("key does not match peerid")
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// TestIDServiceWait gives the ID service 100ms to finish after dialing
// this is becasue it used to be concurrent. Now, Dial wait till the
// id service is done.
func TestIDServiceWait(t *testing.T) {
	N := 3
	for i := 0; i < N; i++ {
		subtestIDService(t, 100*time.Millisecond)
	}
}

func TestIDServiceNoWait(t *testing.T) {
	N := 3
	for i := 0; i < N; i++ {
		subtestIDService(t, 0)
	}
}
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

func TestProtoMatching(t *testing.T) {
	tcp1, _ := ma.NewMultiaddr("/ip4/1.2.3.4/tcp/1234")
	tcp2, _ := ma.NewMultiaddr("/ip4/1.2.3.4/tcp/2345")
	tcp3, _ := ma.NewMultiaddr("/ip4/1.2.3.4/tcp/4567")
	utp, _ := ma.NewMultiaddr("/ip4/1.2.3.4/udp/1234/utp")

	if !identify.HasConsistentTransport(tcp1, []ma.Multiaddr{tcp2, tcp3, utp}) {
		t.Fatal("expected match")
	}

	if identify.HasConsistentTransport(utp, []ma.Multiaddr{tcp2, tcp3}) {
		t.Fatal("expected mismatch")
	}
}