Commit a85aa9b5 authored by Brian Tiger Chow's avatar Brian Tiger Chow

refactor: rename testutil func

parent 81a67338
......@@ -18,7 +18,7 @@ import (
var log = eventlog.Logger("backpressure")
func GenNetwork(t *testing.T, ctx context.Context) (inet.Network, error) {
p := testutil.RandPeerNetParams(t)
p := testutil.RandPeerNetParamsOrFatal(t)
ps := peer.NewPeerstore()
ps.AddAddress(p.ID, p.Addr)
ps.AddPubKey(p.ID, p.PubKey)
......
......@@ -49,8 +49,8 @@ func setupSingleConn(t *testing.T, ctx context.Context) (a, b Conn, p1, p2 tu.Pe
func setupConn(t *testing.T, ctx context.Context, secure bool) (a, b Conn, p1, p2 tu.PeerNetParams) {
p1 = tu.RandPeerNetParams(t)
p2 = tu.RandPeerNetParams(t)
p1 = tu.RandPeerNetParamsOrFatal(t)
p2 = tu.RandPeerNetParamsOrFatal(t)
laddr := p1.Addr
key1 := p1.PrivKey
......@@ -95,8 +95,8 @@ func setupConn(t *testing.T, ctx context.Context, secure bool) (a, b Conn, p1, p
func testDialer(t *testing.T, secure bool) {
// t.Skip("Skipping in favor of another test")
p1 := tu.RandPeerNetParams(t)
p2 := tu.RandPeerNetParams(t)
p1 := tu.RandPeerNetParamsOrFatal(t)
p2 := tu.RandPeerNetParamsOrFatal(t)
key1 := p1.PrivKey
key2 := p2.PrivKey
......
......@@ -14,7 +14,7 @@ import (
)
func GenNetwork(t *testing.T, ctx context.Context) inet.Network {
p := testutil.RandPeerNetParams(t)
p := testutil.RandPeerNetParamsOrFatal(t)
ps := peer.NewPeerstore()
ps.AddAddress(p.ID, p.Addr)
ps.AddPubKey(p.ID, p.PubKey)
......
......@@ -51,7 +51,7 @@ func makeSwarms(ctx context.Context, t *testing.T, num int) ([]*Swarm, []testuti
peersnp := make([]testutil.PeerNetParams, 0, num)
for i := 0; i < num; i++ {
localnp := testutil.RandPeerNetParams(t)
localnp := testutil.RandPeerNetParamsOrFatal(t)
peersnp = append(peersnp, localnp)
peerstore := peer.NewPeerstore()
......
......@@ -107,21 +107,29 @@ func (p *PeerNetParams) checkKeys() error {
return nil // ok. move along.
}
func RandPeerNetParams(t *testing.T) (p PeerNetParams) {
func RandPeerNetParamsOrFatal(t *testing.T) PeerNetParams {
p, err := RandPeerNetParams()
if err != nil {
t.Fatal(err)
return PeerNetParams{} // TODO return nil
}
return *p
}
func RandPeerNetParams() (*PeerNetParams, error) {
var p PeerNetParams
var err error
p.Addr = RandLocalTCPAddress()
p.PrivKey, p.PubKey, err = ci.GenerateKeyPair(ci.RSA, 512)
if err != nil {
t.Fatal(err)
return nil, err
}
p.ID, err = peer.IDFromPublicKey(p.PubKey)
if err != nil {
t.Fatal(err)
return nil, err
}
if err := p.checkKeys(); err != nil {
t.Fatal(err)
return nil, err
}
return p
return &p, nil
}
package testutil
import (
"testing"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
ci "github.com/jbenet/go-ipfs/crypto"
ipfspeer "github.com/jbenet/go-ipfs/peer"
peer "github.com/jbenet/go-ipfs/peer"
)
type Peer interface {
Address() ma.Multiaddr
ID() ipfspeer.ID
ID() peer.ID
PrivateKey() ci.PrivKey
PublicKey() ci.PubKey
}
func RandPeer(t *testing.T) Peer {
p := RandPeerNetParams(t)
var err error
p.Addr = RandLocalTCPAddress()
p.PrivKey, p.PubKey, err = ci.GenerateKeyPair(ci.RSA, 512)
if err != nil {
t.Fatal(err)
}
p.ID, err = ipfspeer.IDFromPublicKey(p.PubKey)
func RandPeer() (Peer, error) {
p, err := RandPeerNetParams()
if err != nil {
t.Fatal(err)
}
if err := p.checkKeys(); err != nil {
t.Fatal(err)
return nil, err
}
return &testpeer{p}
return &testpeer{*p}, nil
}
// peer is a temporary shim to delay binding of PeerNetParams.
......@@ -40,7 +26,7 @@ type testpeer struct {
PeerNetParams
}
func (p *testpeer) ID() ipfspeer.ID {
func (p *testpeer) ID() peer.ID {
return p.PeerNetParams.ID
}
......
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