From e671ab2f42dd61d79bd3beb3f9c182ade7960448 Mon Sep 17 00:00:00 2001 From: rht Date: Wed, 3 Jun 2015 18:12:34 +0700 Subject: [PATCH] Swap all 'crypto/rand' rng in tests with 'math/rand' --- fuse/ipns/ipns_test.go | 4 ++-- importer/balanced/balanced_test.go | 3 +-- importer/chunk/splitting_test.go | 5 +++-- importer/trickle/trickle_test.go | 3 +-- p2p/test/backpressure/backpressure_test.go | 4 ++-- p2p/test/reconnects/reconnect_test.go | 4 ++-- util/testutil/gen.go | 3 +-- 7 files changed, 12 insertions(+), 14 deletions(-) diff --git a/fuse/ipns/ipns_test.go b/fuse/ipns/ipns_test.go index 6d51ec46e..25ba7c23f 100644 --- a/fuse/ipns/ipns_test.go +++ b/fuse/ipns/ipns_test.go @@ -4,7 +4,6 @@ package ipns import ( "bytes" - "crypto/rand" "fmt" "io/ioutil" mrand "math/rand" @@ -19,6 +18,7 @@ import ( core "github.com/ipfs/go-ipfs/core" coremock "github.com/ipfs/go-ipfs/core/mock" nsfs "github.com/ipfs/go-ipfs/ipnsfs" + u "github.com/ipfs/go-ipfs/util" ci "github.com/ipfs/go-ipfs/util/testutil/ci" ) @@ -30,7 +30,7 @@ func maybeSkipFuseTests(t *testing.T) { func randBytes(size int) []byte { b := make([]byte, size) - rand.Read(b) + u.NewTimeSeededRand().Read(b) return b } diff --git a/importer/balanced/balanced_test.go b/importer/balanced/balanced_test.go index d0ffe786d..ee36383e7 100644 --- a/importer/balanced/balanced_test.go +++ b/importer/balanced/balanced_test.go @@ -2,7 +2,6 @@ package balanced import ( "bytes" - "crypto/rand" "fmt" "io" "io/ioutil" @@ -130,7 +129,7 @@ func TestRabinBlockSize(t *testing.T) { } buf := new(bytes.Buffer) nbytes := 1024 * 1024 - io.CopyN(buf, rand.Reader, int64(nbytes)) + io.CopyN(buf, u.NewTimeSeededRand(), int64(nbytes)) rab := chunk.NewMaybeRabin(4096) blkch := rab.Split(buf) diff --git a/importer/chunk/splitting_test.go b/importer/chunk/splitting_test.go index 1385b9069..232b4fde9 100644 --- a/importer/chunk/splitting_test.go +++ b/importer/chunk/splitting_test.go @@ -2,14 +2,15 @@ package chunk import ( "bytes" - "crypto/rand" "io" "testing" + + u "github.com/ipfs/go-ipfs/util" ) func randBuf(t *testing.T, size int) []byte { buf := make([]byte, size) - if _, err := rand.Read(buf); err != nil { + if _, err := u.NewTimeSeededRand().Read(buf); err != nil { t.Fatal("failed to read enough randomness") } return buf diff --git a/importer/trickle/trickle_test.go b/importer/trickle/trickle_test.go index 69d1b12cd..1af5d222f 100644 --- a/importer/trickle/trickle_test.go +++ b/importer/trickle/trickle_test.go @@ -2,7 +2,6 @@ package trickle import ( "bytes" - "crypto/rand" "fmt" "io" "io/ioutil" @@ -136,7 +135,7 @@ func TestRabinBlockSize(t *testing.T) { } buf := new(bytes.Buffer) nbytes := 1024 * 1024 - io.CopyN(buf, rand.Reader, int64(nbytes)) + io.CopyN(buf, u.NewTimeSeededRand(), int64(nbytes)) rab := chunk.NewMaybeRabin(4096) blkch := rab.Split(buf) diff --git a/p2p/test/backpressure/backpressure_test.go b/p2p/test/backpressure/backpressure_test.go index 1fe5506ee..77975567e 100644 --- a/p2p/test/backpressure/backpressure_test.go +++ b/p2p/test/backpressure/backpressure_test.go @@ -1,7 +1,6 @@ package backpressure_tests import ( - crand "crypto/rand" "io" "math/rand" "testing" @@ -15,6 +14,7 @@ import ( eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" + u "github.com/ipfs/go-ipfs/util" ) var log = eventlog.Logger("backpressure") @@ -236,7 +236,7 @@ func TestStBackpressureStreamWrite(t *testing.T) { // ready a buffer of random data buf := make([]byte, 65536) - crand.Read(buf) + u.NewTimeSeededRand().Read(buf) for { // send a randomly sized subchunk diff --git a/p2p/test/reconnects/reconnect_test.go b/p2p/test/reconnects/reconnect_test.go index 700921cc9..ee9275307 100644 --- a/p2p/test/reconnects/reconnect_test.go +++ b/p2p/test/reconnects/reconnect_test.go @@ -1,7 +1,6 @@ package reconnect import ( - crand "crypto/rand" "io" "math/rand" "sync" @@ -14,6 +13,7 @@ import ( protocol "github.com/ipfs/go-ipfs/p2p/protocol" testutil "github.com/ipfs/go-ipfs/p2p/test/util" eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog" + u "github.com/ipfs/go-ipfs/util" ps "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" @@ -66,7 +66,7 @@ func newSender() (chan sendChans, func(s inet.Stream)) { buf := make([]byte, 65536) buf2 := make([]byte, 65536) - crand.Read(buf) + u.NewTimeSeededRand().Read(buf) for { select { diff --git a/util/testutil/gen.go b/util/testutil/gen.go index 461a4c8c7..bfc73723a 100644 --- a/util/testutil/gen.go +++ b/util/testutil/gen.go @@ -2,7 +2,6 @@ package testutil import ( "bytes" - crand "crypto/rand" "errors" "fmt" "io" @@ -44,7 +43,7 @@ func SeededTestKeyPair(seed int64) (ci.PrivKey, ci.PubKey, error) { // id, _ := peer.IDFromPublicKey(pk) func RandPeerID() (peer.ID, error) { buf := make([]byte, 16) - if _, err := io.ReadFull(crand.Reader, buf); err != nil { + if _, err := io.ReadFull(u.NewTimeSeededRand(), buf); err != nil { return "", err } h := u.Hash(buf)