From 1a95e48389e16a34b95ed269c1d1dcbc0ca0c839 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 24 Jan 2016 18:58:15 +0100 Subject: [PATCH] Remove unneeded special readers --- backend/reader.go | 73 ------------------------------------- backend/reader_test.go | 81 ------------------------------------------ 2 files changed, 154 deletions(-) delete mode 100644 backend/reader.go delete mode 100644 backend/reader_test.go diff --git a/backend/reader.go b/backend/reader.go deleted file mode 100644 index eabe9527b..000000000 --- a/backend/reader.go +++ /dev/null @@ -1,73 +0,0 @@ -package backend - -import ( - "hash" - "io" -) - -type HashAppendReader struct { - r io.Reader - h hash.Hash - sum []byte - closed bool -} - -func NewHashAppendReader(r io.Reader, h hash.Hash) *HashAppendReader { - return &HashAppendReader{ - h: h, - r: io.TeeReader(r, h), - sum: make([]byte, 0, h.Size()), - } -} - -func (h *HashAppendReader) Read(p []byte) (n int, err error) { - if !h.closed { - n, err = h.r.Read(p) - - if err == io.EOF { - h.closed = true - h.sum = h.h.Sum(h.sum) - } else if err != nil { - return - } - } - - if h.closed { - // output hash - r := len(p) - n - - if r > 0 { - c := copy(p[n:], h.sum) - h.sum = h.sum[c:] - - n += c - err = nil - } - - if len(h.sum) == 0 { - err = io.EOF - } - } - - return -} - -type HashingReader struct { - r io.Reader - h hash.Hash -} - -func NewHashingReader(r io.Reader, h hash.Hash) *HashingReader { - return &HashingReader{ - h: h, - r: io.TeeReader(r, h), - } -} - -func (h *HashingReader) Read(p []byte) (int, error) { - return h.r.Read(p) -} - -func (h *HashingReader) Sum(d []byte) []byte { - return h.h.Sum(d) -} diff --git a/backend/reader_test.go b/backend/reader_test.go deleted file mode 100644 index b4a23eaea..000000000 --- a/backend/reader_test.go +++ /dev/null @@ -1,81 +0,0 @@ -package backend_test - -import ( - "bytes" - "crypto/rand" - "crypto/sha256" - "io" - "io/ioutil" - "testing" - - "github.com/restic/restic/backend" - . "github.com/restic/restic/test" -) - -func TestHashAppendReader(t *testing.T) { - tests := []int{5, 23, 2<<18 + 23, 1 << 20} - - for _, size := range tests { - data := make([]byte, size) - _, err := io.ReadFull(rand.Reader, data) - if err != nil { - t.Fatalf("ReadFull: %v", err) - } - - expectedHash := sha256.Sum256(data) - - rd := backend.NewHashAppendReader(bytes.NewReader(data), sha256.New()) - - target := bytes.NewBuffer(nil) - n, err := io.Copy(target, rd) - OK(t, err) - - Assert(t, n == int64(size)+int64(len(expectedHash)), - "HashAppendReader: invalid number of bytes read: got %d, expected %d", - n, size+len(expectedHash)) - - r := target.Bytes() - resultingHash := r[len(r)-len(expectedHash):] - Assert(t, bytes.Equal(expectedHash[:], resultingHash), - "HashAppendReader: hashes do not match: expected %02x, got %02x", - expectedHash, resultingHash) - - // try to read again, must return io.EOF - n2, err := rd.Read(make([]byte, 100)) - Assert(t, n2 == 0, "HashAppendReader returned %d additional bytes", n) - Assert(t, err == io.EOF, "HashAppendReader returned %v instead of EOF", err) - } -} - -func TestHashingReader(t *testing.T) { - tests := []int{5, 23, 2<<18 + 23, 1 << 20} - - for _, size := range tests { - data := make([]byte, size) - _, err := io.ReadFull(rand.Reader, data) - if err != nil { - t.Fatalf("ReadFull: %v", err) - } - - expectedHash := sha256.Sum256(data) - - rd := backend.NewHashingReader(bytes.NewReader(data), sha256.New()) - - n, err := io.Copy(ioutil.Discard, rd) - OK(t, err) - - Assert(t, n == int64(size), - "HashAppendReader: invalid number of bytes read: got %d, expected %d", - n, size) - - resultingHash := rd.Sum(nil) - Assert(t, bytes.Equal(expectedHash[:], resultingHash), - "HashAppendReader: hashes do not match: expected %02x, got %02x", - expectedHash, resultingHash) - - // try to read again, must return io.EOF - n2, err := rd.Read(make([]byte, 100)) - Assert(t, n2 == 0, "HashAppendReader returned %d additional bytes", n) - Assert(t, err == io.EOF, "HashAppendReader returned %v instead of EOF", err) - } -}