2015-02-11 18:37:48 +00:00
|
|
|
package backend_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/restic/restic/backend"
|
2015-04-09 19:15:48 +00:00
|
|
|
. "github.com/restic/restic/test"
|
2015-02-11 18:37:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-02-11 18:37:48 +00:00
|
|
|
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, n == int64(size)+int64(len(expectedHash)),
|
2015-02-11 18:37:48 +00:00
|
|
|
"HashAppendReader: invalid number of bytes read: got %d, expected %d",
|
|
|
|
n, size+len(expectedHash))
|
|
|
|
|
|
|
|
r := target.Bytes()
|
|
|
|
resultingHash := r[len(r)-len(expectedHash):]
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, bytes.Equal(expectedHash[:], resultingHash),
|
2015-02-11 18:37:48 +00:00
|
|
|
"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))
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, n2 == 0, "HashAppendReader returned %d additional bytes", n)
|
|
|
|
Assert(t, err == io.EOF, "HashAppendReader returned %v instead of EOF", err)
|
2015-02-11 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-02-11 18:37:48 +00:00
|
|
|
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, n == int64(size),
|
2015-02-11 18:37:48 +00:00
|
|
|
"HashAppendReader: invalid number of bytes read: got %d, expected %d",
|
2015-02-15 22:21:35 +00:00
|
|
|
n, size)
|
2015-02-11 18:37:48 +00:00
|
|
|
|
|
|
|
resultingHash := rd.Sum(nil)
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, bytes.Equal(expectedHash[:], resultingHash),
|
2015-02-11 18:37:48 +00:00
|
|
|
"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))
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, n2 == 0, "HashAppendReader returned %d additional bytes", n)
|
|
|
|
Assert(t, err == io.EOF, "HashAppendReader returned %v instead of EOF", err)
|
2015-02-11 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|