id_test.go 3.15 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package identify_test

import (
	"testing"
	"time"

	inet "github.com/jbenet/go-ipfs/p2p/net"
	handshake "github.com/jbenet/go-ipfs/p2p/net/handshake"
	netutil "github.com/jbenet/go-ipfs/p2p/net/swarmnet/util"
	peer "github.com/jbenet/go-ipfs/p2p/peer"

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
)

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

	// the generated networks should have the id service wired in.
	ctx := context.Background()
	n1 := netutil.GenNetwork(t, ctx)
	n2 := netutil.GenNetwork(t, ctx)

	n1p := n1.LocalPeer()
	n2p := n2.LocalPeer()

	testKnowsAddrs(t, n1, n2p, []ma.Multiaddr{}) // nothing
	testKnowsAddrs(t, n2, n1p, []ma.Multiaddr{}) // nothing

	// have n2 tell n1, so we can dial...
	netutil.DivulgeAddresses(n2, n1)

	testKnowsAddrs(t, n1, n2p, n2.Peerstore().Addresses(n2p)) // has them
	testKnowsAddrs(t, n2, n1p, []ma.Multiaddr{})              // nothing

	if err := n1.DialPeer(ctx, n2p); err != nil {
		t.Fatalf("Failed to dial:", 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.
	testKnowsAddrs(t, n1, n2p, n2.Peerstore().Addresses(n2p)) // has them
	testHasProtocolVersions(t, n1, n2p)

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

	// and the protocol versions.
	testKnowsAddrs(t, n2, n1p, n1.Peerstore().Addresses(n1p)) // has them
	testHasProtocolVersions(t, n2, n1p)
}

func testKnowsAddrs(t *testing.T, n inet.Network, p peer.ID, expected []ma.Multiaddr) {
	actual := n.Peerstore().Addresses(p)

	if len(actual) != len(expected) {
		t.Error("dont have the same addresses")
	}

	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", n.LocalPeer(), p, addr)
			// panic("ahhhhhhh")
		}
	}
}

func testHasProtocolVersions(t *testing.T, n inet.Network, p peer.ID) {
	v, err := n.Peerstore().Get(p, "ProtocolVersion")
	if v == nil {
		t.Error("no protocol version")
		return
	}
	if v.(string) != handshake.IpfsVersion.String() {
		t.Error("protocol mismatch", err)
	}
	v, err = n.Peerstore().Get(p, "AgentVersion")
	if v.(string) != handshake.ClientVersion {
		t.Error("agent version mismatch", err)
	}
}

// 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)
	}
}