From 4de12bf59312ce47315f8c769fab9cfcd4c83b0b Mon Sep 17 00:00:00 2001 From: greatroar <@> Date: Mon, 9 Mar 2020 09:54:24 +0100 Subject: [PATCH] Remove restic.RandReader math/rand.Rand has implemented Reader since Go 1.6. The repacking tests are not deterministic, but they weren't before, either. --- internal/repository/repack_test.go | 15 +----- internal/restic/rand_reader.go | 81 ------------------------------ internal/restic/testing.go | 2 +- 3 files changed, 3 insertions(+), 95 deletions(-) delete mode 100644 internal/restic/rand_reader.go diff --git a/internal/repository/repack_test.go b/internal/repository/repack_test.go index 286962f49..84955ec30 100644 --- a/internal/repository/repack_test.go +++ b/internal/repository/repack_test.go @@ -2,7 +2,6 @@ package repository_test import ( "context" - "io" "math/rand" "testing" @@ -15,17 +14,6 @@ func randomSize(min, max int) int { return rand.Intn(max-min) + min } -func random(t testing.TB, length int) []byte { - rd := restic.NewRandReader(rand.New(rand.NewSource(rand.Int63()))) - buf := make([]byte, length) - _, err := io.ReadFull(rd, buf) - if err != nil { - t.Fatalf("unable to read %d random bytes: %v", length, err) - } - - return buf -} - func createRandomBlobs(t testing.TB, repo restic.Repository, blobs int, pData float32) { for i := 0; i < blobs; i++ { var ( @@ -41,7 +29,8 @@ func createRandomBlobs(t testing.TB, repo restic.Repository, blobs int, pData fl length = randomSize(1*1024, 20*1024) // 1KiB to 20KiB } - buf := random(t, length) + buf := make([]byte, length) + rand.Read(buf) id := restic.Hash(buf) if repo.Index().Has(id, restic.DataBlob) { diff --git a/internal/restic/rand_reader.go b/internal/restic/rand_reader.go deleted file mode 100644 index b5bc83839..000000000 --- a/internal/restic/rand_reader.go +++ /dev/null @@ -1,81 +0,0 @@ -package restic - -import ( - "io" - "math/rand" - - "github.com/restic/restic/internal/errors" -) - -// RandReader allows reading from a rand.Rand. -type RandReader struct { - rnd *rand.Rand - buf []byte -} - -// NewRandReader creates a new Reader from a random source. -func NewRandReader(rnd *rand.Rand) io.Reader { - return &RandReader{rnd: rnd, buf: make([]byte, 0, 7)} -} - -func (rd *RandReader) read(p []byte) (n int, err error) { - if len(p)%7 != 0 { - panic("invalid buffer length, not multiple of 7") - } - - rnd := rd.rnd - for i := 0; i < len(p); i += 7 { - val := rnd.Int63() - - p[i+0] = byte(val >> 0) - p[i+1] = byte(val >> 8) - p[i+2] = byte(val >> 16) - p[i+3] = byte(val >> 24) - p[i+4] = byte(val >> 32) - p[i+5] = byte(val >> 40) - p[i+6] = byte(val >> 48) - } - - return len(p), nil -} - -func (rd *RandReader) Read(p []byte) (int, error) { - // first, copy buffer to p - pos := copy(p, rd.buf) - copy(rd.buf, rd.buf[pos:]) - - // shorten buf and p accordingly - rd.buf = rd.buf[:len(rd.buf)-pos] - p = p[pos:] - - // if this is enough to fill p, return - if len(p) == 0 { - return pos, nil - } - - // load multiple of 7 byte - l := (len(p) / 7) * 7 - n, err := rd.read(p[:l]) - pos += n - if err != nil { - return pos, errors.Wrap(err, "Read") - } - p = p[n:] - - // load 7 byte to temp buffer - rd.buf = rd.buf[:7] - n, err = rd.read(rd.buf) - if err != nil { - return pos, errors.Wrap(err, "Read") - } - - // copy the remaining bytes from the buffer to p - n = copy(p, rd.buf) - pos += n - - // save the remaining bytes in rd.buf - n = copy(rd.buf, rd.buf[n:]) - rd.buf = rd.buf[:n] - - return pos, nil -} diff --git a/internal/restic/testing.go b/internal/restic/testing.go index eb10919e7..0cf7d171a 100644 --- a/internal/restic/testing.go +++ b/internal/restic/testing.go @@ -16,7 +16,7 @@ import ( // fakeFile returns a reader which yields deterministic pseudo-random data. func fakeFile(t testing.TB, seed, size int64) io.Reader { - return io.LimitReader(NewRandReader(rand.New(rand.NewSource(seed))), size) + return io.LimitReader(rand.New(rand.NewSource(seed)), size) } type fakeFileSystem struct {