2018-03-03 13:20:54 +00:00
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-12-19 11:39:48 +00:00
|
|
|
"crypto/md5"
|
|
|
|
"hash"
|
2018-03-03 13:20:54 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestByteReader(t *testing.T) {
|
|
|
|
buf := []byte("foobar")
|
2020-12-19 11:39:48 +00:00
|
|
|
for _, hasher := range []hash.Hash{nil, md5.New()} {
|
|
|
|
fn := func() RewindReader {
|
|
|
|
return NewByteReader(buf, hasher)
|
|
|
|
}
|
|
|
|
testRewindReader(t, fn, buf)
|
2018-03-03 13:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileReader(t *testing.T) {
|
|
|
|
buf := []byte("foobar")
|
|
|
|
|
|
|
|
d, cleanup := test.TempDir(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
filename := filepath.Join(d, "file-reader-test")
|
2020-12-19 11:39:48 +00:00
|
|
|
err := ioutil.WriteFile(filename, buf, 0600)
|
2018-03-03 13:20:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-12-19 11:39:48 +00:00
|
|
|
for _, hasher := range []hash.Hash{nil, md5.New()} {
|
|
|
|
fn := func() RewindReader {
|
|
|
|
var hash []byte
|
|
|
|
if hasher != nil {
|
|
|
|
// must never fail according to interface
|
2021-01-29 21:12:51 +00:00
|
|
|
_, err := hasher.Write(buf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-12-19 11:39:48 +00:00
|
|
|
hash = hasher.Sum(nil)
|
|
|
|
}
|
|
|
|
rd, err := NewFileReader(f, hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return rd
|
2018-03-03 13:20:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 11:39:48 +00:00
|
|
|
testRewindReader(t, fn, buf)
|
|
|
|
}
|
2018-03-03 13:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testRewindReader(t *testing.T, fn func() RewindReader, data []byte) {
|
2018-03-04 09:40:42 +00:00
|
|
|
seed := time.Now().UnixNano()
|
2018-03-03 13:20:54 +00:00
|
|
|
t.Logf("seed is %d", seed)
|
|
|
|
rnd := rand.New(rand.NewSource(seed))
|
|
|
|
|
|
|
|
type ReaderTestFunc func(t testing.TB, r RewindReader, data []byte)
|
|
|
|
var tests = []ReaderTestFunc{
|
|
|
|
func(t testing.TB, rd RewindReader, data []byte) {
|
2018-03-04 09:40:42 +00:00
|
|
|
if rd.Length() != int64(len(data)) {
|
|
|
|
t.Fatalf("wrong length returned, want %d, got %d", int64(len(data)), rd.Length())
|
2018-03-03 13:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, len(data))
|
|
|
|
_, err := io.ReadFull(rd, buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(buf, data) {
|
|
|
|
t.Fatalf("wrong data returned")
|
|
|
|
}
|
|
|
|
|
2018-03-04 09:40:42 +00:00
|
|
|
if rd.Length() != int64(len(data)) {
|
|
|
|
t.Fatalf("wrong length returned, want %d, got %d", int64(len(data)), rd.Length())
|
2018-03-03 13:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = rd.Rewind()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-04 09:40:42 +00:00
|
|
|
if rd.Length() != int64(len(data)) {
|
|
|
|
t.Fatalf("wrong length returned, want %d, got %d", int64(len(data)), rd.Length())
|
2018-03-03 13:20:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 09:40:42 +00:00
|
|
|
buf2 := make([]byte, int64(len(data)))
|
2018-03-03 13:20:54 +00:00
|
|
|
_, err = io.ReadFull(rd, buf2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(buf2, data) {
|
|
|
|
t.Fatalf("wrong data returned")
|
|
|
|
}
|
|
|
|
|
2018-03-04 09:40:42 +00:00
|
|
|
if rd.Length() != int64(len(data)) {
|
|
|
|
t.Fatalf("wrong length returned, want %d, got %d", int64(len(data)), rd.Length())
|
2018-03-03 13:20:54 +00:00
|
|
|
}
|
2020-12-19 11:39:48 +00:00
|
|
|
|
|
|
|
if rd.Hash() != nil {
|
|
|
|
hasher := md5.New()
|
|
|
|
// must never fail according to interface
|
|
|
|
_, _ = hasher.Write(buf2)
|
|
|
|
if !bytes.Equal(rd.Hash(), hasher.Sum(nil)) {
|
|
|
|
t.Fatal("hash does not match data")
|
|
|
|
}
|
|
|
|
}
|
2018-03-03 13:20:54 +00:00
|
|
|
},
|
|
|
|
func(t testing.TB, rd RewindReader, data []byte) {
|
|
|
|
// read first bytes
|
|
|
|
buf := make([]byte, rnd.Intn(len(data)))
|
|
|
|
_, err := io.ReadFull(rd, buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(buf, data[:len(buf)]) {
|
|
|
|
t.Fatalf("wrong data returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rd.Rewind()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf2 := make([]byte, rnd.Intn(len(data)))
|
|
|
|
_, err = io.ReadFull(rd, buf2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(buf2, data[:len(buf2)]) {
|
|
|
|
t.Fatalf("wrong data returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
// read remainder
|
|
|
|
buf3 := make([]byte, len(data)-len(buf2))
|
|
|
|
_, err = io.ReadFull(rd, buf3)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(buf3, data[len(buf2):]) {
|
|
|
|
t.Fatalf("wrong data returned")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
rd := fn()
|
|
|
|
test(t, rd, data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|