Files
go-libp2p-peerstore/test/test_utils.go
Raúl Kripalani eaa2ccbf1e refactor tests into suites.
This enables each implementation to subject itself to a common test
suite.
2018-08-30 13:44:50 +01:00

37 lines
810 B
Go

package test
import (
"io"
"math/rand"
"time"
ci "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
mh "github.com/multiformats/go-multihash"
)
func timeSeededRand() io.Reader {
return rand.New(rand.NewSource(time.Now().UnixNano()))
}
func RandPeerID() (peer.ID, error) {
buf := make([]byte, 16)
if _, err := io.ReadFull(timeSeededRand(), buf); err != nil {
return "", err
}
h, err := mh.Sum(buf, mh.SHA2_256, -1)
if err != nil {
return "", err
}
return peer.ID(h), nil
}
func RandTestKeyPair(bits int) (ci.PrivKey, ci.PubKey, error) {
return ci.GenerateKeyPairWithReader(ci.RSA, bits, timeSeededRand())
}
func SeededTestKeyPair(seed int64) (ci.PrivKey, ci.PubKey, error) {
return ci.GenerateKeyPairWithReader(ci.RSA, 512, rand.New(rand.NewSource(seed)))
}