github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/p2p/protocol/identify/id_test.go (about) 1 package identify_test 2 3 import ( 4 "testing" 5 "time" 6 7 host "github.com/ipfs/go-ipfs/p2p/host" 8 peer "github.com/ipfs/go-ipfs/p2p/peer" 9 identify "github.com/ipfs/go-ipfs/p2p/protocol/identify" 10 testutil "github.com/ipfs/go-ipfs/p2p/test/util" 11 12 ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" 13 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" 14 ) 15 16 func subtestIDService(t *testing.T, postDialWait time.Duration) { 17 18 // the generated networks should have the id service wired in. 19 ctx := context.Background() 20 h1 := testutil.GenHostSwarm(t, ctx) 21 h2 := testutil.GenHostSwarm(t, ctx) 22 23 h1p := h1.ID() 24 h2p := h2.ID() 25 26 testKnowsAddrs(t, h1, h2p, []ma.Multiaddr{}) // nothing 27 testKnowsAddrs(t, h2, h1p, []ma.Multiaddr{}) // nothing 28 29 h2pi := h2.Peerstore().PeerInfo(h2p) 30 if err := h1.Connect(ctx, h2pi); err != nil { 31 t.Fatal(err) 32 } 33 34 // we need to wait here if Dial returns before ID service is finished. 35 if postDialWait > 0 { 36 <-time.After(postDialWait) 37 } 38 39 // the IDService should be opened automatically, by the network. 40 // what we should see now is that both peers know about each others listen addresses. 41 testKnowsAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) // has them 42 testHasProtocolVersions(t, h1, h2p) 43 44 // now, this wait we do have to do. it's the wait for the Listening side 45 // to be done identifying the connection. 46 c := h2.Network().ConnsToPeer(h1.ID()) 47 if len(c) < 1 { 48 t.Fatal("should have connection by now at least.") 49 } 50 <-h2.IDService().IdentifyWait(c[0]) 51 52 // and the protocol versions. 53 testKnowsAddrs(t, h2, h1p, h1.Peerstore().Addrs(h1p)) // has them 54 testHasProtocolVersions(t, h2, h1p) 55 } 56 57 func testKnowsAddrs(t *testing.T, h host.Host, p peer.ID, expected []ma.Multiaddr) { 58 actual := h.Peerstore().Addrs(p) 59 60 if len(actual) != len(expected) { 61 t.Error("dont have the same addresses") 62 } 63 64 have := map[string]struct{}{} 65 for _, addr := range actual { 66 have[addr.String()] = struct{}{} 67 } 68 for _, addr := range expected { 69 if _, found := have[addr.String()]; !found { 70 t.Errorf("%s did not have addr for %s: %s", h.ID(), p, addr) 71 // panic("ahhhhhhh") 72 } 73 } 74 } 75 76 func testHasProtocolVersions(t *testing.T, h host.Host, p peer.ID) { 77 v, err := h.Peerstore().Get(p, "ProtocolVersion") 78 if v == nil { 79 t.Error("no protocol version") 80 return 81 } 82 if v.(string) != identify.IpfsVersion { 83 t.Error("protocol mismatch", err) 84 } 85 v, err = h.Peerstore().Get(p, "AgentVersion") 86 if v.(string) != identify.ClientVersion { 87 t.Error("agent version mismatch", err) 88 } 89 } 90 91 // TestIDServiceWait gives the ID service 100ms to finish after dialing 92 // this is becasue it used to be concurrent. Now, Dial wait till the 93 // id service is done. 94 func TestIDServiceWait(t *testing.T) { 95 N := 3 96 for i := 0; i < N; i++ { 97 subtestIDService(t, 100*time.Millisecond) 98 } 99 } 100 101 func TestIDServiceNoWait(t *testing.T) { 102 N := 3 103 for i := 0; i < N; i++ { 104 subtestIDService(t, 0) 105 } 106 }