From 040db5179527d7d6cde784d6cd37706748a4fb0e Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Mon, 8 Aug 2016 17:16:23 -0700 Subject: [PATCH] testutil, storage: use math/rand.Read where possible Use the much faster math/rand.Read function where cryptographic guarantees are not required. The unit test suite should speed up a little bit but we've already optimized around this, so it may not matter. Signed-off-by: Stephen J Day --- registry/storage/driver/testsuites/testsuites.go | 5 +---- registry/storage/filereader_test.go | 3 +-- testutil/tarfile.go | 3 +-- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/registry/storage/driver/testsuites/testsuites.go b/registry/storage/driver/testsuites/testsuites.go index de8e3143..87f9c6ac 100644 --- a/registry/storage/driver/testsuites/testsuites.go +++ b/registry/storage/driver/testsuites/testsuites.go @@ -1168,10 +1168,7 @@ func randomFilename(length int64) string { var randomBytes = make([]byte, 128<<20) func init() { - // increase the random bytes to the required maximum - for i := range randomBytes { - randomBytes[i] = byte(rand.Intn(2 << 8)) - } + _, _ = rand.Read(randomBytes) // always returns len(randomBytes) and nil error } func randomContents(length int64) []byte { diff --git a/registry/storage/filereader_test.go b/registry/storage/filereader_test.go index f43873b3..5926020c 100644 --- a/registry/storage/filereader_test.go +++ b/registry/storage/filereader_test.go @@ -2,7 +2,6 @@ package storage import ( "bytes" - "crypto/rand" "io" mrand "math/rand" "os" @@ -16,7 +15,7 @@ import ( func TestSimpleRead(t *testing.T) { ctx := context.Background() content := make([]byte, 1<<20) - n, err := rand.Read(content) + n, err := mrand.Read(content) if err != nil { t.Fatalf("unexpected error building random data: %v", err) } diff --git a/testutil/tarfile.go b/testutil/tarfile.go index baa5ac5a..a8ba0155 100644 --- a/testutil/tarfile.go +++ b/testutil/tarfile.go @@ -3,7 +3,6 @@ package testutil import ( "archive/tar" "bytes" - "crypto/rand" "fmt" "io" mrand "math/rand" @@ -46,7 +45,7 @@ func CreateRandomTarFile() (rs io.ReadSeeker, dgst digest.Digest, err error) { randomData := make([]byte, fileSize) // Fill up the buffer with some random data. - n, err := rand.Read(randomData) + n, err := mrand.Read(randomData) if n != len(randomData) { return nil, "", fmt.Errorf("short read creating random reader: %v bytes != %v bytes", n, len(randomData))