Skip to content

Commit

Permalink
absorb {crypto,peer} test utils. (#8)
Browse files Browse the repository at this point in the history
Avoids circular module dependency.
  • Loading branch information
raulk authored May 23, 2019
1 parent 0a9354a commit d090163
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
6 changes: 3 additions & 3 deletions core/crypto/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

. "github.com/libp2p/go-libp2p-core/crypto"
pb "github.com/libp2p/go-libp2p-core/crypto/pb"
"github.com/libp2p/go-libp2p-testing/crypto"
"github.com/libp2p/go-libp2p-core/test"
)

func TestKeys(t *testing.T) {
Expand All @@ -17,7 +17,7 @@ func TestKeys(t *testing.T) {
}

func testKeyType(typ int, t *testing.T) {
sk, pk, err := tcrypto.RandTestKeyPair(typ, 512)
sk, pk, err := test.RandTestKeyPair(typ, 512)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func testKeyEquals(t *testing.T, k Key) {
t.Fatal("Key not equal to key with same bytes.")
}

sk, pk, err := tcrypto.RandTestKeyPair(RSA, 512)
sk, pk, err := test.RandTestKeyPair(RSA, 512)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion core/peer/peer_serde_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/libp2p/go-libp2p-core/peer"
. "github.com/libp2p/go-libp2p-testing/peer"
. "github.com/libp2p/go-libp2p-core/test"
)

func TestPeerSerdePB(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions core/peer/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"testing"

ic "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-testing/crypto"
"github.com/libp2p/go-libp2p-core/test"

. "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-testing/peer"
"github.com/libp2p/go-libp2p-core/test"

b58 "github.com/mr-tron/base58/base58"
mh "github.com/multiformats/go-multihash"
Expand Down Expand Up @@ -49,7 +49,7 @@ type keyset struct {

func (ks *keyset) generate() error {
var err error
ks.sk, ks.pk, err = tcrypto.RandTestKeyPair(ic.RSA, 512)
ks.sk, ks.pk, err = test.RandTestKeyPair(ic.RSA, 512)
if err != nil {
return err
}
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestValidate(t *testing.T) {
}

// Non-empty peer ID validates
p, err := tpeer.RandPeerID()
p, err := test.RandPeerID()
if err != nil {
t.Fatal(err)
}
Expand Down
25 changes: 25 additions & 0 deletions core/test/crypto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package test

import (
"math/rand"
"sync/atomic"
"time"

ci "github.com/libp2p/go-libp2p-core/crypto"
)

var generatedPairs int64 = 0

func RandTestKeyPair(typ, bits int) (ci.PrivKey, ci.PubKey, error) {
seed := time.Now().UnixNano()

// workaround for low time resolution
seed += atomic.AddInt64(&generatedPairs, 1) << 32

return SeededTestKeyPair(typ, bits, time.Now().UnixNano())
}

func SeededTestKeyPair(typ, bits int, seed int64) (ci.PrivKey, ci.PubKey, error) {
r := rand.New(rand.NewSource(seed))
return ci.GenerateKeyPairWithReader(typ, bits, r)
}
30 changes: 30 additions & 0 deletions core/test/peer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package test

import (
"io"
"math/rand"
"testing"
"time"

"github.com/libp2p/go-libp2p-core/peer"

mh "github.com/multiformats/go-multihash"
)

func RandPeerID() (peer.ID, error) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
buf := make([]byte, 16)
if _, err := io.ReadFull(r, buf); err != nil {
return "", err
}
h, _ := mh.Sum(buf, mh.SHA2_256, -1)
return peer.ID(h), nil
}

func RandPeerIDFatal(t testing.TB) peer.ID {
p, err := RandPeerID()
if err != nil {
t.Fatal(err)
}
return p
}

0 comments on commit d090163

Please sign in to comment.