Commit 17d39398 authored by jbenet's avatar jbenet
Browse files

identify: tests verify we have public keys

parent 83eddcae
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"testing" "testing"
"time" "time"
ic "github.com/ipfs/go-libp2p-crypto"
peer "github.com/ipfs/go-libp2p-peer" peer "github.com/ipfs/go-libp2p-peer"
host "github.com/libp2p/go-libp2p/p2p/host" host "github.com/libp2p/go-libp2p/p2p/host"
identify "github.com/libp2p/go-libp2p/p2p/protocol/identify" identify "github.com/libp2p/go-libp2p/p2p/protocol/identify"
...@@ -41,6 +42,7 @@ func subtestIDService(t *testing.T, postDialWait time.Duration) { ...@@ -41,6 +42,7 @@ func subtestIDService(t *testing.T, postDialWait time.Duration) {
t.Log("test peer1 has peer2 addrs correctly") t.Log("test peer1 has peer2 addrs correctly")
testKnowsAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) // has them testKnowsAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) // has them
testHasProtocolVersions(t, h1, h2p) 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 // now, this wait we do have to do. it's the wait for the Listening side
// to be done identifying the connection. // to be done identifying the connection.
...@@ -57,6 +59,7 @@ func subtestIDService(t *testing.T, postDialWait time.Duration) { ...@@ -57,6 +59,7 @@ func subtestIDService(t *testing.T, postDialWait time.Duration) {
t.Log("test peer2 has peer1 addrs correctly") t.Log("test peer2 has peer1 addrs correctly")
testKnowsAddrs(t, h2, h1p, addrs) // has them testKnowsAddrs(t, h2, h1p, addrs) // has them
testHasProtocolVersions(t, h2, h1p) 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) { 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) { ...@@ -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 // TestIDServiceWait gives the ID service 100ms to finish after dialing
// this is becasue it used to be concurrent. Now, Dial wait till the // this is becasue it used to be concurrent. Now, Dial wait till the
// id service is done. // id service is done.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment