From 7196971159eec0585cd3ca8da683d6be0a4f4783 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Tue, 26 Jan 2016 22:00:11 +0100 Subject: [PATCH] Remove unneeded HashingReader implementation --- backend/writer.go | 38 ----------------------------------- backend/writer_test.go | 45 ------------------------------------------ 2 files changed, 83 deletions(-) delete mode 100644 backend/writer.go delete mode 100644 backend/writer_test.go diff --git a/backend/writer.go b/backend/writer.go deleted file mode 100644 index 5764b872c..000000000 --- a/backend/writer.go +++ /dev/null @@ -1,38 +0,0 @@ -package backend - -import ( - "hash" - "io" -) - -// HashingWriter wraps an io.Writer to hashes all data that is written to it. -type HashingWriter struct { - w io.Writer - h hash.Hash - size int -} - -// NewHashAppendWriter wraps the writer w and feeds all data written to the hash h. -func NewHashingWriter(w io.Writer, h hash.Hash) *HashingWriter { - return &HashingWriter{ - h: h, - w: io.MultiWriter(w, h), - } -} - -// Write wraps the write method of the underlying writer and also hashes all data. -func (h *HashingWriter) Write(p []byte) (int, error) { - n, err := h.w.Write(p) - h.size += n - return n, err -} - -// Sum returns the hash of all data written so far. -func (h *HashingWriter) Sum(d []byte) []byte { - return h.h.Sum(d) -} - -// Size returns the number of bytes written to the underlying writer. -func (h *HashingWriter) Size() int { - return h.size -} diff --git a/backend/writer_test.go b/backend/writer_test.go deleted file mode 100644 index 9fda2c06f..000000000 --- a/backend/writer_test.go +++ /dev/null @@ -1,45 +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 TestHashingWriter(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) - - wr := backend.NewHashingWriter(ioutil.Discard, sha256.New()) - - n, err := io.Copy(wr, bytes.NewReader(data)) - OK(t, err) - - Assert(t, n == int64(size), - "HashAppendWriter: invalid number of bytes written: got %d, expected %d", - n, size) - - Assert(t, wr.Size() == size, - "HashAppendWriter: invalid number of bytes returned: got %d, expected %d", - wr.Size, size) - - resultingHash := wr.Sum(nil) - Assert(t, bytes.Equal(expectedHash[:], resultingHash), - "HashAppendWriter: hashes do not match: expected %02x, got %02x", - expectedHash, resultingHash) - } -}