mirror of
https://github.com/libp2p/go-libp2p-core.git
synced 2025-04-09 15:00:11 +08:00
move peer and crypto test utils to go-libp2p-testing.
This commit is contained in:
parent
ed42958fbb
commit
757c750aa1
@ -7,7 +7,7 @@ import (
|
||||
|
||||
. "github.com/libp2p/go-libp2p-core/crypto"
|
||||
pb "github.com/libp2p/go-libp2p-core/crypto/pb"
|
||||
tu "github.com/libp2p/go-libp2p-core/crypto/test"
|
||||
"github.com/libp2p/go-libp2p-testing/crypto"
|
||||
)
|
||||
|
||||
func TestKeys(t *testing.T) {
|
||||
@ -17,7 +17,7 @@ func TestKeys(t *testing.T) {
|
||||
}
|
||||
|
||||
func testKeyType(typ int, t *testing.T) {
|
||||
sk, pk, err := tu.RandTestKeyPair(typ, 512)
|
||||
sk, pk, err := tcrypto.RandTestKeyPair(typ, 512)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -114,7 +114,7 @@ func testKeyEquals(t *testing.T, k Key) {
|
||||
t.Fatal("Key not equal to key with same bytes.")
|
||||
}
|
||||
|
||||
sk, pk, err := tu.RandTestKeyPair(RSA, 512)
|
||||
sk, pk, err := tcrypto.RandTestKeyPair(RSA, 512)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -1,25 +0,0 @@
|
||||
package tcrypto
|
||||
|
||||
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)
|
||||
}
|
1
go.mod
1
go.mod
@ -7,6 +7,7 @@ require (
|
||||
github.com/ipfs/go-cid v0.0.1
|
||||
github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8
|
||||
github.com/libp2p/go-flow-metrics v0.0.1
|
||||
github.com/libp2p/go-libp2p-testing v0.0.0-20190508172549-1a0da3de1915
|
||||
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16
|
||||
github.com/mr-tron/base58 v1.1.1
|
||||
github.com/multiformats/go-multiaddr v0.0.2
|
||||
|
3
go.sum
3
go.sum
@ -32,6 +32,9 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
|
||||
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
|
||||
github.com/libp2p/go-flow-metrics v0.0.1 h1:0gxuFd2GuK7IIP5pKljLwps6TvcuYgvG7Atqi3INF5s=
|
||||
github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8=
|
||||
github.com/libp2p/go-libp2p-core v0.0.0-20190508144953-ed42958fbb3a/go.mod h1:U1aaQsPt97g1BVcfdLEnzHdSNbaSg3Gh5eKgJ/KunKg=
|
||||
github.com/libp2p/go-libp2p-testing v0.0.0-20190508172549-1a0da3de1915 h1:ut3tAnP5cOMPZbUHEEhKnvJaJGorIUOdqz+UBC45T/g=
|
||||
github.com/libp2p/go-libp2p-testing v0.0.0-20190508172549-1a0da3de1915/go.mod h1:1FFj6xmoClrgHPBxgUgSSYWYxJ6CdkoP+s+sFf8nVgo=
|
||||
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
|
||||
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
|
||||
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16 h1:5W7KhL8HVF3XCFOweFD3BNESdnO8ewyYTFT2R+/b8FQ=
|
||||
|
@ -4,11 +4,11 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/libp2p/go-libp2p-core/peer"
|
||||
"github.com/libp2p/go-libp2p-core/peer/test"
|
||||
. "github.com/libp2p/go-libp2p-testing/peer"
|
||||
)
|
||||
|
||||
func TestPeerSerdePB(t *testing.T) {
|
||||
id, err := testutil.RandPeerID()
|
||||
id, err := RandPeerID()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -27,7 +27,7 @@ func TestPeerSerdePB(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPeerSerdeJSON(t *testing.T) {
|
||||
id, err := testutil.RandPeerID()
|
||||
id, err := RandPeerID()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -45,7 +45,7 @@ func TestPeerSerdeJSON(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBinaryMarshaler(t *testing.T) {
|
||||
id, err := testutil.RandPeerID()
|
||||
id, err := RandPeerID()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -64,7 +64,7 @@ func TestBinaryMarshaler(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTextMarshaler(t *testing.T) {
|
||||
id, err := testutil.RandPeerID()
|
||||
id, err := RandPeerID()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -7,11 +7,11 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/libp2p/go-libp2p-core/peer"
|
||||
tpeer "github.com/libp2p/go-libp2p-core/peer/test"
|
||||
|
||||
ic "github.com/libp2p/go-libp2p-core/crypto"
|
||||
tcrypto "github.com/libp2p/go-libp2p-core/crypto/test"
|
||||
"github.com/libp2p/go-libp2p-testing/crypto"
|
||||
|
||||
. "github.com/libp2p/go-libp2p-core/peer"
|
||||
"github.com/libp2p/go-libp2p-testing/peer"
|
||||
|
||||
b58 "github.com/mr-tron/base58/base58"
|
||||
mh "github.com/multiformats/go-multihash"
|
||||
@ -92,7 +92,7 @@ func (ks *keyset) load(hpkp, skBytesStr string) error {
|
||||
func TestIDMatchesPublicKey(t *testing.T) {
|
||||
|
||||
test := func(ks keyset) {
|
||||
p1, err := peer.IDB58Decode(ks.hpkp)
|
||||
p1, err := IDB58Decode(ks.hpkp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -105,7 +105,7 @@ func TestIDMatchesPublicKey(t *testing.T) {
|
||||
t.Fatal("p1 does not match pk")
|
||||
}
|
||||
|
||||
p2, err := peer.IDFromPublicKey(ks.pk)
|
||||
p2, err := IDFromPublicKey(ks.pk)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -127,7 +127,7 @@ func TestIDMatchesPublicKey(t *testing.T) {
|
||||
func TestIDMatchesPrivateKey(t *testing.T) {
|
||||
|
||||
test := func(ks keyset) {
|
||||
p1, err := peer.IDB58Decode(ks.hpkp)
|
||||
p1, err := IDB58Decode(ks.hpkp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -140,7 +140,7 @@ func TestIDMatchesPrivateKey(t *testing.T) {
|
||||
t.Fatal("p1 does not match sk")
|
||||
}
|
||||
|
||||
p2, err := peer.IDFromPrivateKey(ks.sk)
|
||||
p2, err := IDFromPrivateKey(ks.sk)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -163,7 +163,7 @@ func TestPublicKeyExtraction(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
id, err := peer.IDFromPublicKey(originalPub)
|
||||
id, err := IDFromPublicKey(originalPub)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -180,7 +180,7 @@ func TestPublicKeyExtraction(t *testing.T) {
|
||||
}
|
||||
|
||||
// Test invalid multihash (invariant of the type of public key)
|
||||
pk, err := peer.ID("").ExtractPublicKey()
|
||||
pk, err := ID("").ExtractPublicKey()
|
||||
if err == nil {
|
||||
t.Fatal("expected an error")
|
||||
}
|
||||
@ -194,12 +194,12 @@ func TestPublicKeyExtraction(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rsaId, err := peer.IDFromPublicKey(rsaPub)
|
||||
rsaId, err := IDFromPublicKey(rsaPub)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
extractedRsaPub, err := rsaId.ExtractPublicKey()
|
||||
if err != peer.ErrNoPublicKey {
|
||||
if err != ErrNoPublicKey {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if extractedRsaPub != nil {
|
||||
@ -209,11 +209,11 @@ func TestPublicKeyExtraction(t *testing.T) {
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
// Empty peer ID invalidates
|
||||
err := peer.ID("").Validate()
|
||||
err := ID("").Validate()
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
} else if err != peer.ErrEmptyPeerID {
|
||||
t.Error("expected error message: " + peer.ErrEmptyPeerID.Error())
|
||||
} else if err != ErrEmptyPeerID {
|
||||
t.Error("expected error message: " + ErrEmptyPeerID.Error())
|
||||
}
|
||||
|
||||
// Non-empty peer ID validates
|
||||
|
@ -1,30 +0,0 @@
|
||||
package tpeer
|
||||
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue
Block a user