hashing: support WriteTo in the reader

This commit is contained in:
Michael Eischer 2021-01-01 12:42:33 +01:00
parent aa0faa8c7d
commit bcb852a8d0
2 changed files with 87 additions and 25 deletions

View file

@ -5,25 +5,47 @@ import (
"io" "io"
) )
// Reader hashes all data read from the underlying reader. // ReadSumer hashes all data read from the underlying reader.
type Reader struct { type ReadSumer interface {
r io.Reader io.Reader
// Sum returns the hash of the data read so far.
Sum(d []byte) []byte
}
type reader struct {
io.Reader
h hash.Hash h hash.Hash
} }
// NewReader returns a new Reader that uses the hash h. type readWriterTo struct {
func NewReader(r io.Reader, h hash.Hash) *Reader { reader
return &Reader{ writerTo io.WriterTo
h: h,
r: io.TeeReader(r, h),
}
} }
func (h *Reader) Read(p []byte) (int, error) { // NewReader returns a new ReadSummer that uses the hash h. If the underlying
return h.r.Read(p) // reader supports WriteTo then the returned reader will do so too.
func NewReader(r io.Reader, h hash.Hash) ReadSumer {
rs := reader{
Reader: io.TeeReader(r, h),
h: h,
}
if _, ok := r.(io.WriterTo); ok {
return &readWriterTo{
reader: rs,
writerTo: r.(io.WriterTo),
}
}
return &rs
} }
// Sum returns the hash of the data read so far. // Sum returns the hash of the data read so far.
func (h *Reader) Sum(d []byte) []byte { func (h *reader) Sum(d []byte) []byte {
return h.h.Sum(d) return h.h.Sum(d)
} }
// WriteTo reads all data into the passed writer
func (h *readWriterTo) WriteTo(w io.Writer) (int64, error) {
return h.writerTo.WriteTo(NewWriter(w, h.h))
}

View file

@ -7,8 +7,26 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"testing" "testing"
rtest "github.com/restic/restic/internal/test"
) )
// only expose Read method
type onlyReader struct {
io.Reader
}
type traceWriterTo struct {
io.Reader
writerTo io.WriterTo
Traced bool
}
func (r *traceWriterTo) WriteTo(w io.Writer) (n int64, err error) {
r.Traced = true
return r.writerTo.WriteTo(w)
}
func TestReader(t *testing.T) { func TestReader(t *testing.T) {
tests := []int{5, 23, 2<<18 + 23, 1 << 20} tests := []int{5, 23, 2<<18 + 23, 1 << 20}
@ -21,22 +39,44 @@ func TestReader(t *testing.T) {
expectedHash := sha256.Sum256(data) expectedHash := sha256.Sum256(data)
rd := NewReader(bytes.NewReader(data), sha256.New()) for _, test := range []struct {
n, err := io.Copy(ioutil.Discard, rd) innerWriteTo, outerWriteTo bool
if err != nil { }{{false, false}, {false, true}, {true, false}, {true, true}} {
t.Fatal(err) // test both code paths in WriteTo
} src := bytes.NewReader(data)
rawSrc := &traceWriterTo{Reader: src, writerTo: src}
innerSrc := io.Reader(rawSrc)
if !test.innerWriteTo {
innerSrc = &onlyReader{Reader: rawSrc}
}
if n != int64(size) { rd := NewReader(innerSrc, sha256.New())
t.Errorf("Reader: invalid number of bytes written: got %d, expected %d", // test both Read and WriteTo
n, size) outerSrc := io.Reader(rd)
} if !test.outerWriteTo {
outerSrc = &onlyReader{Reader: outerSrc}
}
resultingHash := rd.Sum(nil) n, err := io.Copy(ioutil.Discard, outerSrc)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(expectedHash[:], resultingHash) { if n != int64(size) {
t.Errorf("Reader: hashes do not match: expected %02x, got %02x", t.Errorf("Reader: invalid number of bytes written: got %d, expected %d",
expectedHash, resultingHash) n, size)
}
resultingHash := rd.Sum(nil)
if !bytes.Equal(expectedHash[:], resultingHash) {
t.Errorf("Reader: hashes do not match: expected %02x, got %02x",
expectedHash, resultingHash)
}
rtest.Assert(t, rawSrc.Traced == (test.innerWriteTo && test.outerWriteTo),
"unexpected/missing writeTo call innerWriteTo %v outerWriteTo %v",
test.innerWriteTo, test.outerWriteTo)
} }
} }
} }