diff --git a/blockservice/mock.go b/blockservice/mock.go index 57432178ee7..73fcdf2fcae 100644 --- a/blockservice/mock.go +++ b/blockservice/mock.go @@ -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) diff --git a/exchange/bitswap/bitswap_test.go b/exchange/bitswap/bitswap_test.go index af6cb138cec..64d5ead52c1 100644 --- a/exchange/bitswap/bitswap_test.go +++ b/exchange/bitswap/bitswap_test.go @@ -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 @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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") diff --git a/exchange/bitswap/testutils.go b/exchange/bitswap/testutils.go index dd96e5f463a..95019f297cd 100644 --- a/exchange/bitswap/testutils.go +++ b/exchange/bitswap/testutils.go @@ -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{ @@ -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 } diff --git a/p2p/net/mock/mock_net.go b/p2p/net/mock/mock_net.go index 664ef1a2bf5..6146c4e52e2 100644 --- a/p2p/net/mock/mock_net.go +++ b/p2p/net/mock/mock_net.go @@ -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" @@ -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 } diff --git a/p2p/test/util/key.go b/p2p/test/util/key.go new file mode 100644 index 00000000000..fc4f3af4e5a --- /dev/null +++ b/p2p/test/util/key.go @@ -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 +}