@cryptotaxi247 / kubo / commits / b31f72818

feat(testutil) add testutil.Peer shim

Brian Tiger Chow committed Dec 23, 2014 at 15:05 UTC b31f7281889e96a4952cc610522890e98b15d128
1 file changed +57
util/testutil/peer.go new
+57
@@ -0,0 +1,57 @@
1 +package testutil
2 +
3 +import (
4 + "testing"
5 +
6 + ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
7 + ci "github.com/jbenet/go-ipfs/crypto"
8 + ipfspeer "github.com/jbenet/go-ipfs/peer"
9 +)
10 +
11 +type Peer interface {
12 + Address() ma.Multiaddr
13 + ID() ipfspeer.ID
14 + PrivateKey() ci.PrivKey
15 + PublicKey() ci.PubKey
16 +}
17 +
18 +func RandPeer(t *testing.T) Peer {
19 + p := RandPeerNetParams(t)
20 + var err error
21 + p.Addr = RandLocalTCPAddress()
22 + p.PrivKey, p.PubKey, err = ci.GenerateKeyPair(ci.RSA, 512)
23 + if err != nil {
24 + t.Fatal(err)
25 + }
26 +
27 + p.ID, err = ipfspeer.IDFromPublicKey(p.PubKey)
28 + if err != nil {
29 + t.Fatal(err)
30 + }
31 +
32 + if err := p.checkKeys(); err != nil {
33 + t.Fatal(err)
34 + }
35 + return &testpeer{p}
36 +}
37 +
38 +// peer is a temporary shim to delay binding of PeerNetParams.
39 +type testpeer struct {
40 + PeerNetParams
41 +}
42 +
43 +func (p *testpeer) ID() ipfspeer.ID {
44 + return p.PeerNetParams.ID
45 +}
46 +
47 +func (p *testpeer) Address() ma.Multiaddr {
48 + return p.Addr
49 +}
50 +
51 +func (p *testpeer) PrivateKey() ci.PrivKey {
52 + return p.PrivKey
53 +}
54 +
55 +func (p *testpeer) PublicKey() ci.PubKey {
56 + return p.PubKey
57 +}