Skip to content

Commit

Permalink
p2p/test: bogus key pair for faster tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbenet committed Jan 5, 2015
1 parent 41af4f4 commit 1ab9588
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 14 deletions.
2 changes: 1 addition & 1 deletion blockservice/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// Mocks returns |n| connected mock Blockservices
func Mocks(t *testing.T, n int) []*BlockService {
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(0))
sg := bitswap.NewSessionGenerator(net)
sg := bitswap.NewTestSessionGenerator(net)

instances := sg.Instances(n)

Expand Down
18 changes: 9 additions & 9 deletions exchange/bitswap/bitswap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
blocks "github.com/jbenet/go-ipfs/blocks"
blocksutil "github.com/jbenet/go-ipfs/blocks/blocksutil"
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
p2ptestutil "github.com/jbenet/go-ipfs/p2p/test/util"
mockrouting "github.com/jbenet/go-ipfs/routing/mock"
u "github.com/jbenet/go-ipfs/util"
delay "github.com/jbenet/go-ipfs/util/delay"
"github.com/jbenet/go-ipfs/util/testutil"
)

// FIXME the tests are really sensitive to the network delay. fix them to work
Expand All @@ -25,7 +25,7 @@ func TestClose(t *testing.T) {
// TODO
t.Skip("TODO Bitswap's Close implementation is a WIP")
vnet := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
sesgen := NewSessionGenerator(vnet)
sesgen := NewTestSessionGenerator(vnet)
defer sesgen.Close()
bgen := blocksutil.NewBlockGenerator()

Expand All @@ -39,7 +39,7 @@ func TestClose(t *testing.T) {
func TestGetBlockTimeout(t *testing.T) {

net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
g := NewSessionGenerator(net)
g := NewTestSessionGenerator(net)
defer g.Close()

self := g.Next()
Expand All @@ -57,11 +57,11 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) { // TODO revisit this

rs := mockrouting.NewServer()
net := tn.VirtualNetwork(rs, delay.Fixed(kNetworkDelay))
g := NewSessionGenerator(net)
g := NewTestSessionGenerator(net)
defer g.Close()

block := blocks.NewBlock([]byte("block"))
pinfo := testutil.RandIdentityOrFatal(t)
pinfo := p2ptestutil.RandTestBogusIdentityOrFatal(t)
rs.Client(pinfo).Provide(context.Background(), block.Key()) // but not on network

solo := g.Next()
Expand All @@ -81,7 +81,7 @@ func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {

net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
block := blocks.NewBlock([]byte("block"))
g := NewSessionGenerator(net)
g := NewTestSessionGenerator(net)
defer g.Close()

hasBlock := g.Next()
Expand Down Expand Up @@ -134,7 +134,7 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
t.SkipNow()
}
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
sg := NewSessionGenerator(net)
sg := NewTestSessionGenerator(net)
defer sg.Close()
bg := blocksutil.NewBlockGenerator()

Expand Down Expand Up @@ -198,7 +198,7 @@ func TestSendToWantingPeer(t *testing.T) {
}

net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
sg := NewSessionGenerator(net)
sg := NewTestSessionGenerator(net)
defer sg.Close()
bg := blocksutil.NewBlockGenerator()

Expand Down Expand Up @@ -243,7 +243,7 @@ func TestSendToWantingPeer(t *testing.T) {

func TestBasicBitswap(t *testing.T) {
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
sg := NewSessionGenerator(net)
sg := NewTestSessionGenerator(net)
bg := blocksutil.NewBlockGenerator()

t.Log("Test a few nodes trying to get one file with a lot of blocks")
Expand Down
6 changes: 4 additions & 2 deletions exchange/bitswap/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (
exchange "github.com/jbenet/go-ipfs/exchange"
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
peer "github.com/jbenet/go-ipfs/p2p/peer"
p2ptestutil "github.com/jbenet/go-ipfs/p2p/test/util"
datastore2 "github.com/jbenet/go-ipfs/util/datastore2"
delay "github.com/jbenet/go-ipfs/util/delay"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)

func NewSessionGenerator(
// WARNING: this uses RandTestBogusIdentity DO NOT USE for NON TESTS!
func NewTestSessionGenerator(
net tn.Network) SessionGenerator {
ctx, cancel := context.WithCancel(context.TODO())
return SessionGenerator{
Expand All @@ -41,7 +43,7 @@ func (g *SessionGenerator) Close() error {

func (g *SessionGenerator) Next() Instance {
g.seq++
p, err := testutil.RandIdentity()
p, err := p2ptestutil.RandTestBogusIdentity()
if err != nil {
panic("FIXME") // TODO change signature
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/net/mock/mock_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"fmt"
"sort"
"sync"
"time"

ic "github.com/jbenet/go-ipfs/p2p/crypto"
host "github.com/jbenet/go-ipfs/p2p/host"
bhost "github.com/jbenet/go-ipfs/p2p/host/basic"
inet "github.com/jbenet/go-ipfs/p2p/net"
peer "github.com/jbenet/go-ipfs/p2p/peer"
p2putil "github.com/jbenet/go-ipfs/p2p/test/util"
testutil "github.com/jbenet/go-ipfs/util/testutil"

context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
Expand Down Expand Up @@ -45,7 +45,7 @@ func New(ctx context.Context) Mocknet {
}

func (mn *mocknet) GenPeer() (host.Host, error) {
sk, _, err := testutil.SeededTestKeyPair(time.Now().UnixNano())
sk, err := p2putil.RandTestBogusPrivateKey()
if err != nil {
return nil, err
}
Expand Down
165 changes: 165 additions & 0 deletions p2p/test/util/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package testutil

import (
"bytes"
"io"
"testing"

u "github.com/jbenet/go-ipfs/util"
eventlog "github.com/jbenet/go-ipfs/util/eventlog"
testutil "github.com/jbenet/go-ipfs/util/testutil"

ic "github.com/jbenet/go-ipfs/p2p/crypto"
peer "github.com/jbenet/go-ipfs/p2p/peer"

ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
)

var log = eventlog.Logger("boguskey")

// TestBogusPrivateKey is a key used for testing (to avoid expensive keygen)
type TestBogusPrivateKey []byte

// TestBogusPublicKey is a key used for testing (to avoid expensive keygen)
type TestBogusPublicKey []byte

func (pk TestBogusPublicKey) Verify(data, sig []byte) (bool, error) {
log.Criticalf("TestBogusPublicKey.Verify -- this better be a test!")
return bytes.Equal(data, reverse(sig)), nil
}

func (pk TestBogusPublicKey) Bytes() ([]byte, error) {
return []byte(pk), nil
}

func (pk TestBogusPublicKey) Encrypt(b []byte) ([]byte, error) {
log.Criticalf("TestBogusPublicKey.Encrypt -- this better be a test!")
return reverse(b), nil
}

// Equals checks whether this key is equal to another
func (pk TestBogusPublicKey) Equals(k ic.Key) bool {
return ic.KeyEqual(pk, k)
}

func (pk TestBogusPublicKey) Hash() ([]byte, error) {
return ic.KeyHash(pk)
}

func (sk TestBogusPrivateKey) GenSecret() []byte {
return []byte(sk)
}

func (sk TestBogusPrivateKey) Sign(message []byte) ([]byte, error) {
log.Criticalf("TestBogusPrivateKey.Sign -- this better be a test!")
return reverse(message), nil
}

func (sk TestBogusPrivateKey) GetPublic() ic.PubKey {
return TestBogusPublicKey(sk)
}

func (sk TestBogusPrivateKey) Decrypt(b []byte) ([]byte, error) {
log.Criticalf("TestBogusPrivateKey.Decrypt -- this better be a test!")
return reverse(b), nil
}

func (sk TestBogusPrivateKey) Bytes() ([]byte, error) {
return []byte(sk), nil
}

// Equals checks whether this key is equal to another
func (sk TestBogusPrivateKey) Equals(k ic.Key) bool {
return ic.KeyEqual(sk, k)
}

func (sk TestBogusPrivateKey) Hash() ([]byte, error) {
return ic.KeyHash(sk)
}

func RandTestBogusPrivateKey() (TestBogusPrivateKey, error) {
r := u.NewTimeSeededRand()
k := make([]byte, 5)
if _, err := io.ReadFull(r, k); err != nil {
return nil, err
}
return TestBogusPrivateKey(k), nil
}

func RandTestBogusPublicKey() (TestBogusPublicKey, error) {
k, err := RandTestBogusPrivateKey()
return TestBogusPublicKey(k), err
}

func RandTestBogusPrivateKeyOrFatal(t *testing.T) TestBogusPrivateKey {
k, err := RandTestBogusPrivateKey()
if err != nil {
t.Fatal(err)
}
return k
}

func RandTestBogusPublicKeyOrFatal(t *testing.T) TestBogusPublicKey {
k, err := RandTestBogusPublicKey()
if err != nil {
t.Fatal(err)
}
return k
}

func RandTestBogusIdentity() (testutil.Identity, error) {
k, err := RandTestBogusPrivateKey()
if err != nil {
return nil, err
}

id, err := peer.IDFromPrivateKey(k)
if err != nil {
return nil, err
}

return &identity{
k: k,
id: id,
a: testutil.RandLocalTCPAddress(),
}, nil
}

func RandTestBogusIdentityOrFatal(t *testing.T) testutil.Identity {
k, err := RandTestBogusIdentity()
if err != nil {
t.Fatal(err)
}
return k
}

// identity is a temporary shim to delay binding of PeerNetParams.
type identity struct {
k TestBogusPrivateKey
id peer.ID
a ma.Multiaddr
}

func (p *identity) ID() peer.ID {
return p.id
}

func (p *identity) Address() ma.Multiaddr {
return p.a
}

func (p *identity) PrivateKey() ic.PrivKey {
return p.k
}

func (p *identity) PublicKey() ic.PubKey {
return p.k.GetPublic()
}

func reverse(a []byte) []byte {
b := make([]byte, len(a))
for i := 0; i < len(a); i++ {
b[i] = a[len(a)-1-i]
}
return b
}

1 comment on commit 1ab9588

@jbenet
Copy link
Member Author

@jbenet jbenet commented on 1ab9588 Jan 5, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@briantigerchow:

ok      github.com/jbenet/go-ipfs/exchange/bitswap  0.540s

😄

Please sign in to comment.