add tests for keybook; []peer.ID => peer.IDSlice.

This commit is contained in:
Raúl Kripalani 2018-08-31 12:59:46 +01:00
parent 95b7f1677e
commit 171f051aa1
9 changed files with 234 additions and 104 deletions

View File

@ -66,7 +66,7 @@ type Peerstore interface {
SupportsProtocols(peer.ID, ...string) ([]string, error)
// Peers returns all of the peer IDs stored across all inner stores.
Peers() []peer.ID
Peers() peer.IDSlice
}
type PeerMetadata interface {
@ -111,7 +111,7 @@ type AddrBook interface {
ClearAddrs(p peer.ID)
// PeersWithAddrs returns all of the peer IDs stored in the AddrBook
PeersWithAddrs() []peer.ID
PeersWithAddrs() peer.IDSlice
}
// KeyBook tracks the keys of Peers.
@ -130,5 +130,5 @@ type KeyBook interface {
AddPrivKey(peer.ID, ic.PrivKey) error
// PeersWithKeys returns all the peer IDs stored in the KeyBook
PeersWithKeys() []peer.ID
PeersWithKeys() peer.IDSlice
}

View File

@ -31,7 +31,7 @@ func NewPeerstore(kb KeyBook, ab AddrBook, md PeerMetadata) Peerstore {
}
}
func (ps *peerstore) Peers() []peer.ID {
func (ps *peerstore) Peers() peer.IDSlice {
set := map[peer.ID]struct{}{}
for _, p := range ps.PeersWithKeys() {
set[p] = struct{}{}
@ -40,7 +40,7 @@ func (ps *peerstore) Peers() []peer.ID {
set[p] = struct{}{}
}
pps := make([]peer.ID, 0, len(set))
pps := make(peer.IDSlice, 0, len(set))
for p := range set {
pps = append(pps, p)
}
@ -132,7 +132,7 @@ func (ps *peerstore) SupportsProtocols(p peer.ID, protos ...string) ([]string, e
return out, nil
}
func PeerInfos(ps Peerstore, peers []peer.ID) []PeerInfo {
func PeerInfos(ps Peerstore, peers peer.IDSlice) []PeerInfo {
pi := make([]PeerInfo, len(peers))
for i, p := range peers {
pi[i] = ps.PeerInfo(p)
@ -140,8 +140,8 @@ func PeerInfos(ps Peerstore, peers []peer.ID) []PeerInfo {
return pi
}
func PeerInfoIDs(pis []PeerInfo) []peer.ID {
ps := make([]peer.ID, len(pis))
func PeerInfoIDs(pis []PeerInfo) peer.IDSlice {
ps := make(peer.IDSlice, len(pis))
for i, pi := range pis {
ps[i] = pi.ID
}

View File

@ -193,12 +193,12 @@ func (mgr *dsAddrBook) Addrs(p peer.ID) []ma.Multiaddr {
}
// Peers returns all of the peer IDs for which the AddrBook has addresses.
func (mgr *dsAddrBook) PeersWithAddrs() []peer.ID {
func (mgr *dsAddrBook) PeersWithAddrs() peer.IDSlice {
q := query.Query{KeysOnly: true}
results, err := mgr.ds.Query(q)
if err != nil {
log.Error(err)
return []peer.ID{}
return peer.IDSlice{}
}
idset := make(map[peer.ID]struct{})
@ -211,7 +211,7 @@ func (mgr *dsAddrBook) PeersWithAddrs() []peer.ID {
idset[id] = struct{}{}
}
ids := make([]peer.ID, 0, len(idset))
ids := make(peer.IDSlice, 0, len(idset))
for id := range idset {
ids = append(ids, id)
}

View File

@ -45,14 +45,14 @@ func NewAddrBook() pstore.AddrBook {
}
}
func (mab *memoryAddrBook) PeersWithAddrs() []peer.ID {
func (mab *memoryAddrBook) PeersWithAddrs() peer.IDSlice {
mab.addrmu.Lock()
defer mab.addrmu.Unlock()
if mab.addrs == nil {
return nil
}
pids := make([]peer.ID, 0, len(mab.addrs))
pids := make(peer.IDSlice, 0, len(mab.addrs))
for pid := range mab.addrs {
pids = append(pids, pid)
}

View File

@ -19,6 +19,12 @@ func TestInMemoryAddrBook(t *testing.T) {
})
}
func TestInMemoryKeyBook(t *testing.T) {
test.TestKeyBook(t, func() (pstore.KeyBook, func()) {
return NewKeyBook(), nil
})
}
func BenchmarkInMemoryPeerstore(b *testing.B) {
test.BenchmarkPeerstore(b, func() (pstore.Peerstore, func()) {
return NewPeerstore(), nil

View File

@ -26,9 +26,9 @@ func NewKeyBook() pstore.KeyBook {
}
}
func (mkb *memoryKeyBook) PeersWithKeys() []peer.ID {
func (mkb *memoryKeyBook) PeersWithKeys() peer.IDSlice {
mkb.RLock()
ps := make([]peer.ID, 0, len(mkb.pks)+len(mkb.sks))
ps := make(peer.IDSlice, 0, len(mkb.pks)+len(mkb.sks))
for p := range mkb.pks {
ps = append(ps, p)
}

View File

@ -4,10 +4,8 @@ import (
"testing"
"time"
"github.com/libp2p/go-libp2p-peer"
ma "github.com/multiformats/go-multiaddr"
pstore "github.com/libp2p/go-libp2p-peerstore"
ma "github.com/multiformats/go-multiaddr"
)
var addressBookSuite = map[string]func(book pstore.AddrBook) func(*testing.T){
@ -38,27 +36,27 @@ func TestAddrBook(t *testing.T, factory AddrBookFactory) {
func testAddresses(m pstore.AddrBook) func(*testing.T) {
return func(t *testing.T) {
id1 := IDS(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQN")
id2 := IDS(t, "QmRmPL3FDZKE3Qiwv1RosLdwdvbvg17b2hB39QPScgWKKZ")
id3 := IDS(t, "QmPhi7vBsChP7sjRoZGgg7bcKqF6MmCcQwvRbDte8aJ6Kn")
id4 := IDS(t, "QmPhi7vBsChP7sjRoZGgg7bcKqF6MmCcQwvRbDte8aJ5Kn")
id5 := IDS(t, "QmPhi7vBsChP7sjRoZGgg7bcKqF6MmCcQwvRbDte8aJ5Km")
id1 := peerId(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQN")
id2 := peerId(t, "QmRmPL3FDZKE3Qiwv1RosLdwdvbvg17b2hB39QPScgWKKZ")
id3 := peerId(t, "QmPhi7vBsChP7sjRoZGgg7bcKqF6MmCcQwvRbDte8aJ6Kn")
id4 := peerId(t, "QmPhi7vBsChP7sjRoZGgg7bcKqF6MmCcQwvRbDte8aJ5Kn")
id5 := peerId(t, "QmPhi7vBsChP7sjRoZGgg7bcKqF6MmCcQwvRbDte8aJ5Km")
ma11 := MA(t, "/ip4/1.2.3.1/tcp/1111")
ma21 := MA(t, "/ip4/2.2.3.2/tcp/1111")
ma22 := MA(t, "/ip4/2.2.3.2/tcp/2222")
ma31 := MA(t, "/ip4/3.2.3.3/tcp/1111")
ma32 := MA(t, "/ip4/3.2.3.3/tcp/2222")
ma33 := MA(t, "/ip4/3.2.3.3/tcp/3333")
ma41 := MA(t, "/ip4/4.2.3.3/tcp/1111")
ma42 := MA(t, "/ip4/4.2.3.3/tcp/2222")
ma43 := MA(t, "/ip4/4.2.3.3/tcp/3333")
ma44 := MA(t, "/ip4/4.2.3.3/tcp/4444")
ma51 := MA(t, "/ip4/5.2.3.3/tcp/1111")
ma52 := MA(t, "/ip4/5.2.3.3/tcp/2222")
ma53 := MA(t, "/ip4/5.2.3.3/tcp/3333")
ma54 := MA(t, "/ip4/5.2.3.3/tcp/4444")
ma55 := MA(t, "/ip4/5.2.3.3/tcp/5555")
ma11 := multiaddr(t, "/ip4/1.2.3.1/tcp/1111")
ma21 := multiaddr(t, "/ip4/2.2.3.2/tcp/1111")
ma22 := multiaddr(t, "/ip4/2.2.3.2/tcp/2222")
ma31 := multiaddr(t, "/ip4/3.2.3.3/tcp/1111")
ma32 := multiaddr(t, "/ip4/3.2.3.3/tcp/2222")
ma33 := multiaddr(t, "/ip4/3.2.3.3/tcp/3333")
ma41 := multiaddr(t, "/ip4/4.2.3.3/tcp/1111")
ma42 := multiaddr(t, "/ip4/4.2.3.3/tcp/2222")
ma43 := multiaddr(t, "/ip4/4.2.3.3/tcp/3333")
ma44 := multiaddr(t, "/ip4/4.2.3.3/tcp/4444")
ma51 := multiaddr(t, "/ip4/5.2.3.3/tcp/1111")
ma52 := multiaddr(t, "/ip4/5.2.3.3/tcp/2222")
ma53 := multiaddr(t, "/ip4/5.2.3.3/tcp/3333")
ma54 := multiaddr(t, "/ip4/5.2.3.3/tcp/4444")
ma55 := multiaddr(t, "/ip4/5.2.3.3/tcp/5555")
ttl := time.Hour
m.AddAddr(id1, ma11, ttl)
@ -90,13 +88,13 @@ func testAddresses(m pstore.AddrBook) func(*testing.T) {
func testClearWorks(m pstore.AddrBook) func(t *testing.T) {
return func(t *testing.T) {
id1 := IDS(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQN")
id2 := IDS(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQM")
ma11 := MA(t, "/ip4/1.2.3.1/tcp/1111")
ma12 := MA(t, "/ip4/2.2.3.2/tcp/2222")
ma13 := MA(t, "/ip4/3.2.3.3/tcp/3333")
ma24 := MA(t, "/ip4/4.2.3.3/tcp/4444")
ma25 := MA(t, "/ip4/5.2.3.3/tcp/5555")
id1 := peerId(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQN")
id2 := peerId(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQM")
ma11 := multiaddr(t, "/ip4/1.2.3.1/tcp/1111")
ma12 := multiaddr(t, "/ip4/2.2.3.2/tcp/2222")
ma13 := multiaddr(t, "/ip4/3.2.3.3/tcp/3333")
ma24 := multiaddr(t, "/ip4/4.2.3.3/tcp/4444")
ma25 := multiaddr(t, "/ip4/5.2.3.3/tcp/5555")
m.AddAddr(id1, ma11, time.Hour)
m.AddAddr(id1, ma12, time.Hour)
@ -117,8 +115,8 @@ func testClearWorks(m pstore.AddrBook) func(t *testing.T) {
func testSetNegativeTTLClears(m pstore.AddrBook) func(t *testing.T) {
return func(t *testing.T) {
id1 := IDS(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQN")
ma11 := MA(t, "/ip4/1.2.3.1/tcp/1111")
id1 := peerId(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQN")
ma11 := multiaddr(t, "/ip4/1.2.3.1/tcp/1111")
m.SetAddr(id1, ma11, time.Hour)
@ -132,12 +130,12 @@ func testSetNegativeTTLClears(m pstore.AddrBook) func(t *testing.T) {
func testUpdateTTLs(m pstore.AddrBook) func(t *testing.T) {
return func(t *testing.T) {
id1 := IDS(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQN")
id2 := IDS(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQM")
ma11 := MA(t, "/ip4/1.2.3.1/tcp/1111")
ma12 := MA(t, "/ip4/1.2.3.1/tcp/1112")
ma21 := MA(t, "/ip4/1.2.3.1/tcp/1121")
ma22 := MA(t, "/ip4/1.2.3.1/tcp/1122")
id1 := peerId(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQN")
id2 := peerId(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQM")
ma11 := multiaddr(t, "/ip4/1.2.3.1/tcp/1111")
ma12 := multiaddr(t, "/ip4/1.2.3.1/tcp/1112")
ma21 := multiaddr(t, "/ip4/1.2.3.1/tcp/1121")
ma22 := multiaddr(t, "/ip4/1.2.3.1/tcp/1122")
// Shouldn't panic.
m.UpdateAddrs(id1, time.Hour, time.Minute)
@ -178,7 +176,7 @@ func testUpdateTTLs(m pstore.AddrBook) func(t *testing.T) {
func testNilAddrsDontBreak(m pstore.AddrBook) func(t *testing.T) {
return func(t *testing.T) {
id1 := IDS(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQN")
id1 := peerId(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQN")
m.SetAddr(id1, nil, time.Hour)
m.AddAddr(id1, nil, time.Hour)
}
@ -186,13 +184,13 @@ func testNilAddrsDontBreak(m pstore.AddrBook) func(t *testing.T) {
func testAddressesExpire(m pstore.AddrBook) func(t *testing.T) {
return func(t *testing.T) {
id1 := IDS(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQN")
id2 := IDS(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQM")
ma11 := MA(t, "/ip4/1.2.3.1/tcp/1111")
ma12 := MA(t, "/ip4/2.2.3.2/tcp/2222")
ma13 := MA(t, "/ip4/3.2.3.3/tcp/3333")
ma24 := MA(t, "/ip4/4.2.3.3/tcp/4444")
ma25 := MA(t, "/ip4/5.2.3.3/tcp/5555")
id1 := peerId(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQN")
id2 := peerId(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQM")
ma11 := multiaddr(t, "/ip4/1.2.3.1/tcp/1111")
ma12 := multiaddr(t, "/ip4/2.2.3.2/tcp/2222")
ma13 := multiaddr(t, "/ip4/3.2.3.3/tcp/3333")
ma24 := multiaddr(t, "/ip4/4.2.3.3/tcp/4444")
ma25 := multiaddr(t, "/ip4/5.2.3.3/tcp/5555")
m.AddAddr(id1, ma11, time.Hour)
m.AddAddr(id1, ma12, time.Hour)
@ -239,24 +237,6 @@ func testAddressesExpire(m pstore.AddrBook) func(t *testing.T) {
}
}
func IDS(t *testing.T, ids string) peer.ID {
t.Helper()
id, err := peer.IDB58Decode(ids)
if err != nil {
t.Fatalf("id %q is bad: %s", ids, err)
}
return id
}
func MA(t *testing.T, m string) ma.Multiaddr {
t.Helper()
maddr, err := ma.NewMultiaddr(m)
if err != nil {
t.Fatal(err)
}
return maddr
}
func testHas(t *testing.T, exp, act []ma.Multiaddr) {
t.Helper()
if len(exp) != len(act) {

154
test/keybook_suite.go Normal file
View File

@ -0,0 +1,154 @@
package test
import (
"sort"
"testing"
"github.com/libp2p/go-libp2p-peer"
pt "github.com/libp2p/go-libp2p-peer/test"
pstore "github.com/libp2p/go-libp2p-peerstore"
)
var keyBookSuite = map[string]func(kb pstore.KeyBook) func(*testing.T){
"AddGetPrivKey": testKeybookPrivKey,
"AddGetPubKey": testKeyBookPubKey,
"PeersWithKeys": testKeyBookPeers,
"PubKeyAddedOnRetrieve": testInlinedPubKeyAddedOnRetrieve,
}
type KeyBookFactory func() (pstore.KeyBook, func())
func TestKeyBook(t *testing.T, factory KeyBookFactory) {
for name, test := range keyBookSuite {
// Create a new peerstore.
kb, closeFunc := factory()
// Run the test.
t.Run(name, test(kb))
// Cleanup.
if closeFunc != nil {
closeFunc()
}
}
}
func testKeybookPrivKey(kb pstore.KeyBook) func(t *testing.T) {
return func(t *testing.T) {
if peers := kb.PeersWithKeys(); len(peers) > 0 {
t.Error("expected peers to be empty on init")
}
priv, _, err := pt.RandTestKeyPair(512)
if err != nil {
t.Error(err)
}
id, err := peer.IDFromPrivateKey(priv)
if err != nil {
t.Error(err)
}
err = kb.AddPrivKey(id, priv)
if err != nil {
t.Error(err)
}
if res := kb.PrivKey(id); !priv.Equals(res) {
t.Error("retrieved private key did not match stored private key")
}
if peers := kb.PeersWithKeys(); len(peers) != 1 || peers[0] != id {
t.Error("list of peers did not include test peer")
}
}
}
func testKeyBookPubKey(kb pstore.KeyBook) func(t *testing.T) {
return func(t *testing.T) {
if peers := kb.PeersWithKeys(); len(peers) > 0 {
t.Error("expected peers to be empty on init")
}
_, pub, err := pt.RandTestKeyPair(512)
if err != nil {
t.Error(err)
}
id, err := peer.IDFromPublicKey(pub)
if err != nil {
t.Error(err)
}
err = kb.AddPubKey(id, pub)
if err != nil {
t.Error(err)
}
if res := kb.PubKey(id); !pub.Equals(res) {
t.Error("retrieved public key did not match stored public key")
}
if peers := kb.PeersWithKeys(); len(peers) != 1 || peers[0] != id {
t.Error("list of peers did not include test peer")
}
}
}
func testKeyBookPeers(kb pstore.KeyBook) func(t *testing.T) {
return func(t *testing.T) {
if peers := kb.PeersWithKeys(); len(peers) > 0 {
t.Error("expected peers to be empty on init")
}
var peers peer.IDSlice
for i := 0; i < 10; i++ {
// Add a public key.
_, pub, _ := pt.RandTestKeyPair(512)
p1, _ := peer.IDFromPublicKey(pub)
kb.AddPubKey(p1, pub)
// Add a private key.
priv, _, _ := pt.RandTestKeyPair(512)
p2, _ := peer.IDFromPrivateKey(priv)
kb.AddPrivKey(p2, priv)
peers = append(peers, []peer.ID{p1, p2}...)
}
kbPeers := kb.PeersWithKeys()
sort.Sort(kbPeers)
sort.Sort(peers)
for i, p := range kbPeers {
if p != peers[i] {
t.Errorf("mismatch of peer at index %d", i)
}
}
}
}
func testInlinedPubKeyAddedOnRetrieve(kb pstore.KeyBook) func(t *testing.T) {
return func(t *testing.T) {
if peers := kb.PeersWithKeys(); len(peers) > 0 {
t.Error("expected peers to be empty on init")
}
// Key small enough for inlining.
_, pub, err := pt.RandTestKeyPair(32)
if err != nil {
t.Error(err)
}
id, err := peer.IDFromPublicKey(pub)
if err != nil {
t.Error(err)
}
pubKey := kb.PubKey(id)
if !pubKey.Equals(pub) {
t.Error("mismatch between original public key and keybook-calculated one")
}
}
}

View File

@ -1,36 +1,26 @@
package test
import (
"io"
"math/rand"
"time"
"testing"
ci "github.com/libp2p/go-libp2p-crypto"
"github.com/libp2p/go-libp2p-peer"
mh "github.com/multiformats/go-multihash"
ma "github.com/multiformats/go-multiaddr"
)
func timeSeededRand() io.Reader {
return rand.New(rand.NewSource(time.Now().UnixNano()))
}
func RandPeerID() (peer.ID, error) {
buf := make([]byte, 16)
if _, err := io.ReadFull(timeSeededRand(), buf); err != nil {
return "", err
}
h, err := mh.Sum(buf, mh.SHA2_256, -1)
func peerId(t *testing.T, ids string) peer.ID {
t.Helper()
id, err := peer.IDB58Decode(ids)
if err != nil {
return "", err
t.Fatalf("id %q is bad: %s", ids, err)
}
return peer.ID(h), nil
return id
}
func RandTestKeyPair(bits int) (ci.PrivKey, ci.PubKey, error) {
return ci.GenerateKeyPairWithReader(ci.RSA, bits, timeSeededRand())
}
func SeededTestKeyPair(seed int64) (ci.PrivKey, ci.PubKey, error) {
return ci.GenerateKeyPairWithReader(ci.RSA, 512, rand.New(rand.NewSource(seed)))
}
func multiaddr(t *testing.T, m string) ma.Multiaddr {
t.Helper()
maddr, err := ma.NewMultiaddr(m)
if err != nil {
t.Fatal(err)
}
return maddr
}