hashing: Remove io.WriterTo implementation

This functionality has gone unused since
4b3dc415ef changed hashing.Reader's only
client to use ioutil.ReadAll on a bufio.Reader wrapping the hashing
Reader.

Reverts bcb852a8d0.
This commit is contained in:
greatroar 2022-05-10 22:35:57 +02:00
parent ab49c14621
commit 54b8337813
2 changed files with 25 additions and 87 deletions

View file

@ -7,26 +7,8 @@ import (
"io"
"io/ioutil"
"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) {
tests := []int{5, 23, 2<<18 + 23, 1 << 20}
@ -39,44 +21,22 @@ func TestReader(t *testing.T) {
expectedHash := sha256.Sum256(data)
for _, test := range []struct {
innerWriteTo, outerWriteTo bool
}{{false, false}, {false, true}, {true, false}, {true, true}} {
// 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}
}
rd := NewReader(bytes.NewReader(data), sha256.New())
n, err := io.Copy(ioutil.Discard, rd)
if err != nil {
t.Fatal(err)
}
rd := NewReader(innerSrc, sha256.New())
// test both Read and WriteTo
outerSrc := io.Reader(rd)
if !test.outerWriteTo {
outerSrc = &onlyReader{Reader: outerSrc}
}
if n != int64(size) {
t.Errorf("Reader: invalid number of bytes written: got %d, expected %d",
n, size)
}
n, err := io.Copy(ioutil.Discard, outerSrc)
if err != nil {
t.Fatal(err)
}
resultingHash := rd.Sum(nil)
if n != int64(size) {
t.Errorf("Reader: invalid number of bytes written: got %d, expected %d",
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)
if !bytes.Equal(expectedHash[:], resultingHash) {
t.Errorf("Reader: hashes do not match: expected %02x, got %02x",
expectedHash, resultingHash)
}
}
}