From 17d39398e9c83959c65860478f802ccd14edc192 Mon Sep 17 00:00:00 2001 From: jbenet Date: Sun, 11 Sep 2016 18:36:49 -0400 Subject: [PATCH] identify: tests verify we have public keys --- p2p/protocol/identify/id_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index d0c147c..cf7f814 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -4,6 +4,7 @@ import ( "testing" "time" + ic "github.com/ipfs/go-libp2p-crypto" peer "github.com/ipfs/go-libp2p-peer" host "github.com/libp2p/go-libp2p/p2p/host" identify "github.com/libp2p/go-libp2p/p2p/protocol/identify" @@ -41,6 +42,7 @@ func subtestIDService(t *testing.T, postDialWait time.Duration) { t.Log("test peer1 has peer2 addrs correctly") testKnowsAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) // has them testHasProtocolVersions(t, h1, h2p) + testHasPublicKey(t, h1, h2p, h2.Peerstore().PubKey(h2p)) // h1 should have h2's public key // now, this wait we do have to do. it's the wait for the Listening side // to be done identifying the connection. @@ -57,6 +59,7 @@ func subtestIDService(t *testing.T, postDialWait time.Duration) { t.Log("test peer2 has peer1 addrs correctly") testKnowsAddrs(t, h2, h1p, addrs) // has them testHasProtocolVersions(t, h2, h1p) + testHasPublicKey(t, h2, h1p, h1.Peerstore().PubKey(h1p)) // h1 should have h2's public key } func testKnowsAddrs(t *testing.T, h host.Host, p peer.ID, expected []ma.Multiaddr) { @@ -95,6 +98,25 @@ func testHasProtocolVersions(t *testing.T, h host.Host, p peer.ID) { } } +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") + } +} + // 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. -- GitLab