2015-08-16 20:27:07 +00:00
|
|
|
// +build !openbsd
|
2015-08-17 17:40:34 +00:00
|
|
|
// +build !windows
|
2015-08-16 20:27:07 +00:00
|
|
|
|
2015-07-26 14:43:42 +00:00
|
|
|
package fuse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-09-01 20:17:37 +00:00
|
|
|
"restic/errors"
|
2016-08-21 15:46:23 +00:00
|
|
|
|
2015-07-26 14:43:42 +00:00
|
|
|
"bazil.org/fuse"
|
|
|
|
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic"
|
|
|
|
. "restic/test"
|
2015-07-26 14:43:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MockRepo struct {
|
2016-08-31 20:51:35 +00:00
|
|
|
blobs map[restic.ID][]byte
|
2015-07-26 14:43:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 20:51:35 +00:00
|
|
|
func NewMockRepo(content map[restic.ID][]byte) *MockRepo {
|
2015-07-26 14:43:42 +00:00
|
|
|
return &MockRepo{blobs: content}
|
|
|
|
}
|
|
|
|
|
2016-08-31 20:51:35 +00:00
|
|
|
func (m *MockRepo) LookupBlobSize(id restic.ID, t restic.BlobType) (uint, error) {
|
2015-07-26 14:43:42 +00:00
|
|
|
buf, ok := m.blobs[id]
|
|
|
|
if !ok {
|
|
|
|
return 0, errors.New("blob not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint(len(buf)), nil
|
|
|
|
}
|
|
|
|
|
2016-09-03 11:34:04 +00:00
|
|
|
func (m *MockRepo) LoadDataBlob(id restic.ID, buf []byte) (int, error) {
|
|
|
|
size, err := m.LookupBlobSize(id, restic.DataBlob)
|
2015-07-26 14:43:42 +00:00
|
|
|
if err != nil {
|
2016-09-03 11:34:04 +00:00
|
|
|
return 0, err
|
2015-07-26 14:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if uint(cap(buf)) < size {
|
2016-09-03 11:34:04 +00:00
|
|
|
return 0, errors.New("buffer too small")
|
2015-07-26 14:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf = buf[:size]
|
|
|
|
copy(buf, m.blobs[id])
|
2016-09-03 11:34:04 +00:00
|
|
|
return int(size), nil
|
2015-07-26 14:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MockContext struct{}
|
|
|
|
|
|
|
|
func (m MockContext) Deadline() (time.Time, bool) { return time.Now(), false }
|
|
|
|
func (m MockContext) Done() <-chan struct{} { return nil }
|
|
|
|
func (m MockContext) Err() error { return nil }
|
|
|
|
func (m MockContext) Value(key interface{}) interface{} { return nil }
|
|
|
|
|
|
|
|
var testContent = genTestContent()
|
|
|
|
var testContentLengths = []uint{
|
|
|
|
4646 * 1024,
|
|
|
|
655 * 1024,
|
|
|
|
378 * 1024,
|
|
|
|
8108 * 1024,
|
|
|
|
558 * 1024,
|
|
|
|
}
|
|
|
|
var testMaxFileSize uint
|
|
|
|
|
2016-08-31 20:51:35 +00:00
|
|
|
func genTestContent() map[restic.ID][]byte {
|
|
|
|
m := make(map[restic.ID][]byte)
|
2015-07-26 14:43:42 +00:00
|
|
|
|
|
|
|
for _, length := range testContentLengths {
|
|
|
|
buf := Random(int(length), int(length))
|
2016-08-31 20:51:35 +00:00
|
|
|
id := restic.Hash(buf)
|
2015-07-26 14:43:42 +00:00
|
|
|
m[id] = buf
|
|
|
|
testMaxFileSize += length
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
const maxBufSize = 20 * 1024 * 1024
|
|
|
|
|
|
|
|
func testRead(t *testing.T, f *file, offset, length int, data []byte) []byte {
|
|
|
|
ctx := MockContext{}
|
|
|
|
|
|
|
|
req := &fuse.ReadRequest{
|
|
|
|
Offset: int64(offset),
|
|
|
|
Size: length,
|
|
|
|
}
|
|
|
|
resp := &fuse.ReadResponse{
|
|
|
|
Data: make([]byte, length),
|
|
|
|
}
|
|
|
|
OK(t, f.Read(ctx, req, resp))
|
|
|
|
|
|
|
|
return resp.Data
|
|
|
|
}
|
|
|
|
|
|
|
|
var offsetReadsTests = []struct {
|
|
|
|
offset, length int
|
|
|
|
}{
|
|
|
|
{0, 5 * 1024 * 1024},
|
|
|
|
{4000 * 1024, 1000 * 1024},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFuseFile(t *testing.T) {
|
|
|
|
repo := NewMockRepo(testContent)
|
|
|
|
ctx := MockContext{}
|
|
|
|
|
|
|
|
memfile := make([]byte, 0, maxBufSize)
|
|
|
|
|
2016-08-31 20:51:35 +00:00
|
|
|
var ids restic.IDs
|
2015-07-26 14:43:42 +00:00
|
|
|
for id, buf := range repo.blobs {
|
|
|
|
ids = append(ids, id)
|
|
|
|
memfile = append(memfile, buf...)
|
|
|
|
}
|
|
|
|
|
|
|
|
node := &restic.Node{
|
|
|
|
Name: "foo",
|
|
|
|
Inode: 23,
|
|
|
|
Mode: 0742,
|
|
|
|
Size: 42,
|
|
|
|
Content: ids,
|
|
|
|
}
|
2015-07-26 18:41:29 +00:00
|
|
|
f, err := newFile(repo, node, false)
|
2015-07-26 14:43:42 +00:00
|
|
|
OK(t, err)
|
|
|
|
|
|
|
|
attr := fuse.Attr{}
|
|
|
|
OK(t, f.Attr(ctx, &attr))
|
|
|
|
|
|
|
|
Equals(t, node.Inode, attr.Inode)
|
|
|
|
Equals(t, node.Mode, attr.Mode)
|
|
|
|
Equals(t, node.Size, attr.Size)
|
2016-02-14 20:24:02 +00:00
|
|
|
Equals(t, (node.Size/uint64(attr.BlockSize))+1, attr.Blocks)
|
2015-07-26 14:43:42 +00:00
|
|
|
|
|
|
|
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) {
|
|
|
|
t.Errorf("test %d failed, wrong data returned", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 200; i++ {
|
|
|
|
length := rand.Intn(int(testMaxFileSize) / 2)
|
|
|
|
offset := rand.Intn(int(testMaxFileSize))
|
|
|
|
if length+offset > int(testMaxFileSize) {
|
|
|
|
diff := length + offset - int(testMaxFileSize)
|
|
|
|
length -= diff
|
|
|
|
}
|
|
|
|
|
|
|
|
b := memfile[offset : offset+length]
|
|
|
|
res := testRead(t, f, offset, length, b)
|
|
|
|
if !bytes.Equal(b, res) {
|
|
|
|
t.Errorf("test %d failed (offset %d, length %d), wrong data returned", i, offset, length)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|