From 84289d1d69ebffe724b62b29eb5005b2adcf897f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=B6ller?= Date: Mon, 8 Oct 2018 16:25:57 +0200 Subject: [PATCH] readers: add NewPatternReader --- .../jottacloud/jottacloud_internal_test.go | 31 ++----------------- lib/readers/pattern_reader.go | 29 +++++++++++++++++ lib/readers/pattern_reader_test.go | 30 ++++++++++++++++++ 3 files changed, 62 insertions(+), 28 deletions(-) create mode 100644 lib/readers/pattern_reader.go create mode 100644 lib/readers/pattern_reader_test.go diff --git a/backend/jottacloud/jottacloud_internal_test.go b/backend/jottacloud/jottacloud_internal_test.go index f5fe7b2bd..545804aec 100644 --- a/backend/jottacloud/jottacloud_internal_test.go +++ b/backend/jottacloud/jottacloud_internal_test.go @@ -4,50 +4,25 @@ import ( "crypto/md5" "fmt" "io" - "io/ioutil" "testing" + "github.com/ncw/rclone/lib/readers" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) -// A test reader to return a test pattern of size -type testReader struct { - size int64 - c byte -} - -// Reader is the interface that wraps the basic Read method. -func (r *testReader) Read(p []byte) (n int, err error) { - for i := range p { - if r.size <= 0 { - return n, io.EOF - } - p[i] = r.c - r.c = (r.c + 1) % 253 - r.size-- - n++ - } - return -} - func TestReadMD5(t *testing.T) { - // smoke test the reader - b, err := ioutil.ReadAll(&testReader{size: 10}) - require.NoError(t, err) - assert.Equal(t, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, b) - // Check readMD5 for different size and threshold for _, size := range []int64{0, 1024, 10 * 1024, 100 * 1024} { t.Run(fmt.Sprintf("%d", size), func(t *testing.T) { hasher := md5.New() - n, err := io.Copy(hasher, &testReader{size: size}) + n, err := io.Copy(hasher, readers.NewPatternReader(size)) require.NoError(t, err) assert.Equal(t, n, size) wantMD5 := fmt.Sprintf("%x", hasher.Sum(nil)) for _, threshold := range []int64{512, 1024, 10 * 1024, 20 * 1024} { t.Run(fmt.Sprintf("%d", threshold), func(t *testing.T) { - in := &testReader{size: size} + in := readers.NewPatternReader(size) gotMD5, out, cleanup, err := readMD5(in, size, threshold) defer cleanup() require.NoError(t, err) diff --git a/lib/readers/pattern_reader.go b/lib/readers/pattern_reader.go new file mode 100644 index 000000000..a4e534baa --- /dev/null +++ b/lib/readers/pattern_reader.go @@ -0,0 +1,29 @@ +package readers + +import "io" + +// NewPatternReader creates a reader, that returns a deterministic byte pattern. +// After length bytes are read +func NewPatternReader(length int64) io.Reader { + return &patternReader{ + length: length, + } +} + +type patternReader struct { + length int64 + c byte +} + +func (r *patternReader) Read(p []byte) (n int, err error) { + for i := range p { + if r.length <= 0 { + return n, io.EOF + } + p[i] = r.c + r.c = (r.c + 1) % 253 + r.length-- + n++ + } + return +} diff --git a/lib/readers/pattern_reader_test.go b/lib/readers/pattern_reader_test.go new file mode 100644 index 000000000..ed10887cc --- /dev/null +++ b/lib/readers/pattern_reader_test.go @@ -0,0 +1,30 @@ +package readers + +import ( + "io" + "io/ioutil" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestPatternReader(t *testing.T) { + b2 := make([]byte, 1) + + r := NewPatternReader(0) + b, err := ioutil.ReadAll(r) + require.NoError(t, err) + assert.Equal(t, []byte{}, b) + n, err := r.Read(b2) + require.Equal(t, io.EOF, err) + require.Equal(t, 0, n) + + r = NewPatternReader(10) + b, err = ioutil.ReadAll(r) + require.NoError(t, err) + assert.Equal(t, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, b) + n, err = r.Read(b2) + require.Equal(t, io.EOF, err) + require.Equal(t, 0, n) +}