Fix tests

This commit is contained in:
Alexander Neumann 2016-09-03 14:03:43 +02:00
parent ffbe05af9b
commit 2054e3c026
2 changed files with 21 additions and 16 deletions

View File

@ -7,17 +7,15 @@ import (
"restic"
"restic/repository"
"testing"
"github.com/restic/chunker"
)
func loadBlob(t *testing.T, repo restic.Repository, id restic.ID, buf []byte) []byte {
func loadBlob(t *testing.T, repo restic.Repository, id restic.ID, buf []byte) int {
n, err := repo.LoadDataBlob(id, buf)
if err != nil {
t.Fatalf("LoadBlob(%v) returned error %v", id, err)
}
return buf[:n]
return n
}
func checkSavedFile(t *testing.T, repo restic.Repository, treeID restic.ID, name string, rd io.Reader) {
@ -40,12 +38,19 @@ func checkSavedFile(t *testing.T, repo restic.Repository, treeID restic.ID, name
}
// check blobs
buf := make([]byte, chunker.MaxSize)
buf2 := make([]byte, chunker.MaxSize)
for i, id := range node.Content {
buf = loadBlob(t, repo, id, buf)
size, err := repo.LookupBlobSize(id, restic.DataBlob)
if err != nil {
t.Fatal(err)
}
buf2 = buf2[:len(buf)]
buf := make([]byte, int(size))
n := loadBlob(t, repo, id, buf)
if n != len(buf) {
t.Errorf("wrong number of bytes read, want %d, got %d", len(buf), n)
}
buf2 := make([]byte, int(size))
_, err = io.ReadFull(rd, buf2)
if err != nil {
t.Fatal(err)

View File

@ -40,7 +40,7 @@ func (m *MockRepo) LoadDataBlob(id restic.ID, buf []byte) (int, error) {
return 0, err
}
if uint(cap(buf)) < size {
if uint(len(buf)) < size {
return 0, errors.New("buffer too small")
}
@ -81,7 +81,7 @@ func genTestContent() map[restic.ID][]byte {
const maxBufSize = 20 * 1024 * 1024
func testRead(t *testing.T, f *file, offset, length int, data []byte) []byte {
func testRead(t *testing.T, f *file, offset, length int, data []byte) {
ctx := MockContext{}
req := &fuse.ReadRequest{
@ -92,8 +92,6 @@ func testRead(t *testing.T, f *file, offset, length int, data []byte) []byte {
Data: make([]byte, length),
}
OK(t, f.Read(ctx, req, resp))
return resp.Data
}
var offsetReadsTests = []struct {
@ -135,8 +133,9 @@ func TestFuseFile(t *testing.T) {
for i, test := range offsetReadsTests {
b := memfile[test.offset : test.offset+test.length]
res := testRead(t, f, test.offset, test.length, b)
if !bytes.Equal(b, res) {
buf := make([]byte, test.length)
testRead(t, f, test.offset, test.length, buf)
if !bytes.Equal(b, buf) {
t.Errorf("test %d failed, wrong data returned", i)
}
}
@ -150,8 +149,9 @@ func TestFuseFile(t *testing.T) {
}
b := memfile[offset : offset+length]
res := testRead(t, f, offset, length, b)
if !bytes.Equal(b, res) {
buf := make([]byte, length)
testRead(t, f, offset, length, buf)
if !bytes.Equal(b, buf) {
t.Errorf("test %d failed (offset %d, length %d), wrong data returned", i, offset, length)
}
}