2016-01-23 16:08:03 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/rand"
|
|
|
|
"reflect"
|
2016-08-31 20:39:36 +00:00
|
|
|
"restic"
|
2016-01-23 16:08:03 +00:00
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
2016-09-01 20:17:37 +00:00
|
|
|
"restic/errors"
|
2016-09-04 12:38:18 +00:00
|
|
|
"restic/test"
|
2016-08-29 17:18:57 +00:00
|
|
|
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic/backend"
|
2016-01-23 16:08:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateFn is a function that creates a temporary repository for the tests.
|
2016-08-31 20:51:35 +00:00
|
|
|
var CreateFn func() (restic.Backend, error)
|
2016-01-23 16:08:03 +00:00
|
|
|
|
|
|
|
// OpenFn is a function that opens a previously created temporary repository.
|
2016-08-31 20:51:35 +00:00
|
|
|
var OpenFn func() (restic.Backend, error)
|
2016-01-23 16:08:03 +00:00
|
|
|
|
|
|
|
// CleanupFn removes temporary files and directories created during the tests.
|
|
|
|
var CleanupFn func() error
|
|
|
|
|
2016-08-31 20:51:35 +00:00
|
|
|
var but restic.Backend // backendUnderTest
|
2016-01-23 16:08:03 +00:00
|
|
|
var butInitialized bool
|
|
|
|
|
2016-08-31 20:51:35 +00:00
|
|
|
func open(t testing.TB) restic.Backend {
|
2016-01-23 16:08:03 +00:00
|
|
|
if OpenFn == nil {
|
|
|
|
t.Fatal("OpenFn not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if CreateFn == nil {
|
|
|
|
t.Fatalf("CreateFn not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !butInitialized {
|
|
|
|
be, err := CreateFn()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Create returned unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
but = be
|
|
|
|
butInitialized = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if but == nil {
|
|
|
|
var err error
|
|
|
|
but, err = OpenFn()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Open returned unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return but
|
|
|
|
}
|
|
|
|
|
|
|
|
func close(t testing.TB) {
|
|
|
|
if but == nil {
|
|
|
|
t.Fatalf("trying to close non-existing backend")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := but.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Close returned unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
but = nil
|
|
|
|
}
|
|
|
|
|
2016-01-23 18:12:02 +00:00
|
|
|
// TestCreate creates a backend.
|
|
|
|
func TestCreate(t testing.TB) {
|
2016-01-23 16:08:03 +00:00
|
|
|
if CreateFn == nil {
|
|
|
|
t.Fatalf("CreateFn not set!")
|
|
|
|
}
|
|
|
|
|
|
|
|
be, err := CreateFn()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Create returned error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
butInitialized = true
|
|
|
|
|
|
|
|
err = be.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Close returned error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 18:12:02 +00:00
|
|
|
// TestOpen opens a previously created backend.
|
|
|
|
func TestOpen(t testing.TB) {
|
2016-01-23 16:08:03 +00:00
|
|
|
if OpenFn == nil {
|
|
|
|
t.Fatalf("OpenFn not set!")
|
|
|
|
}
|
|
|
|
|
|
|
|
be, err := OpenFn()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Open returned error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = be.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Close returned error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 18:12:02 +00:00
|
|
|
// TestCreateWithConfig tests that creating a backend in a location which already
|
2016-01-23 17:07:15 +00:00
|
|
|
// has a config file fails.
|
2016-01-23 18:12:02 +00:00
|
|
|
func TestCreateWithConfig(t testing.TB) {
|
2016-01-23 17:07:15 +00:00
|
|
|
if CreateFn == nil {
|
|
|
|
t.Fatalf("CreateFn not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
b := open(t)
|
|
|
|
defer close(t)
|
|
|
|
|
|
|
|
// save a config
|
2016-08-31 20:39:36 +00:00
|
|
|
store(t, b, restic.ConfigFile, []byte("test config"))
|
2016-01-23 17:07:15 +00:00
|
|
|
|
|
|
|
// now create the backend again, this must fail
|
|
|
|
_, err := CreateFn()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected error not found for creating a backend with an existing config file")
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove config
|
2016-08-31 20:39:36 +00:00
|
|
|
err = b.Remove(restic.ConfigFile, "")
|
2016-01-23 17:07:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error removing config: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 18:12:02 +00:00
|
|
|
// TestLocation tests that a location string is returned.
|
|
|
|
func TestLocation(t testing.TB) {
|
2016-01-23 16:08:03 +00:00
|
|
|
b := open(t)
|
|
|
|
defer close(t)
|
|
|
|
|
|
|
|
l := b.Location()
|
|
|
|
if l == "" {
|
|
|
|
t.Fatalf("invalid location string %q", l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 18:12:02 +00:00
|
|
|
// TestConfig saves and loads a config from the backend.
|
|
|
|
func TestConfig(t testing.TB) {
|
2016-01-23 16:08:03 +00:00
|
|
|
b := open(t)
|
|
|
|
defer close(t)
|
|
|
|
|
|
|
|
var testString = "Config"
|
|
|
|
|
|
|
|
// create config and read it back
|
2016-09-01 19:19:30 +00:00
|
|
|
_, err := backend.LoadAll(b, restic.Handle{Type: restic.ConfigFile}, nil)
|
2016-01-23 16:08:03 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("did not get expected error for non-existing config")
|
|
|
|
}
|
|
|
|
|
2016-09-01 19:19:30 +00:00
|
|
|
err = b.Save(restic.Handle{Type: restic.ConfigFile}, []byte(testString))
|
2016-01-23 16:08:03 +00:00
|
|
|
if err != nil {
|
2016-01-24 19:23:50 +00:00
|
|
|
t.Fatalf("Save() error: %v", err)
|
2016-01-23 16:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// try accessing the config with different names, should all return the
|
|
|
|
// same config
|
|
|
|
for _, name := range []string{"", "foo", "bar", "0000000000000000000000000000000000000000000000000000000000000000"} {
|
2016-09-01 19:19:30 +00:00
|
|
|
h := restic.Handle{Type: restic.ConfigFile, Name: name}
|
2016-02-21 15:02:13 +00:00
|
|
|
buf, err := backend.LoadAll(b, h, nil)
|
2016-01-23 16:08:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to read config with name %q: %v", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(buf) != testString {
|
|
|
|
t.Fatalf("wrong data returned, want %q, got %q", testString, string(buf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 18:12:02 +00:00
|
|
|
// TestLoad tests the backend's Load function.
|
|
|
|
func TestLoad(t testing.TB) {
|
2016-01-23 16:08:03 +00:00
|
|
|
b := open(t)
|
|
|
|
defer close(t)
|
|
|
|
|
2016-08-31 20:39:36 +00:00
|
|
|
_, err := b.Load(restic.Handle{}, nil, 0)
|
2016-01-23 16:08:03 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Load() did not return an error for invalid handle")
|
|
|
|
}
|
|
|
|
|
2016-09-01 19:19:30 +00:00
|
|
|
_, err = b.Load(restic.Handle{Type: restic.DataFile, Name: "foobar"}, nil, 0)
|
2016-01-23 16:08:03 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Load() did not return an error for non-existing blob")
|
|
|
|
}
|
|
|
|
|
|
|
|
length := rand.Intn(1<<24) + 2000
|
|
|
|
|
2016-09-04 12:38:18 +00:00
|
|
|
data := test.Random(23, length)
|
2016-08-31 20:51:35 +00:00
|
|
|
id := restic.Hash(data)
|
2016-01-23 16:08:03 +00:00
|
|
|
|
2016-09-01 19:19:30 +00:00
|
|
|
handle := restic.Handle{Type: restic.DataFile, Name: id.String()}
|
2016-01-24 19:23:50 +00:00
|
|
|
err = b.Save(handle, data)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Save() error: %v", err)
|
|
|
|
}
|
2016-01-23 16:08:03 +00:00
|
|
|
|
2016-01-24 20:40:54 +00:00
|
|
|
for i := 0; i < 50; i++ {
|
2016-01-23 16:08:03 +00:00
|
|
|
l := rand.Intn(length + 2000)
|
|
|
|
o := rand.Intn(length + 2000)
|
|
|
|
|
|
|
|
d := data
|
|
|
|
if o < len(d) {
|
|
|
|
d = d[o:]
|
|
|
|
} else {
|
|
|
|
o = len(d)
|
|
|
|
d = d[:0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if l > 0 && l < len(d) {
|
|
|
|
d = d[:l]
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, l)
|
2016-01-24 19:23:50 +00:00
|
|
|
n, err := b.Load(handle, buf, int64(o))
|
2016-01-23 16:08:03 +00:00
|
|
|
|
2016-08-07 13:52:31 +00:00
|
|
|
// if we requested data beyond the end of the file, require
|
2016-01-23 16:08:03 +00:00
|
|
|
// ErrUnexpectedEOF error
|
2016-08-07 13:52:31 +00:00
|
|
|
if l > len(d) {
|
2016-08-29 17:18:57 +00:00
|
|
|
if errors.Cause(err) != io.ErrUnexpectedEOF {
|
2016-08-07 13:52:31 +00:00
|
|
|
t.Errorf("Load(%d, %d) did not return io.ErrUnexpectedEOF", len(buf), int64(o))
|
|
|
|
}
|
|
|
|
err = nil
|
|
|
|
buf = buf[:len(d)]
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Load(%d, %d): unexpected error: %v", len(buf), int64(o), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if n != len(buf) {
|
|
|
|
t.Errorf("Load(%d, %d): wrong length returned, want %d, got %d",
|
|
|
|
len(buf), int64(o), len(buf), n)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = buf[:n]
|
|
|
|
if !bytes.Equal(buf, d) {
|
|
|
|
t.Errorf("Load(%d, %d) returned wrong bytes", len(buf), int64(o))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test with negative offset
|
|
|
|
for i := 0; i < 50; i++ {
|
|
|
|
l := rand.Intn(length + 2000)
|
|
|
|
o := rand.Intn(length + 2000)
|
|
|
|
|
|
|
|
d := data
|
|
|
|
if o < len(d) {
|
|
|
|
d = d[len(d)-o:]
|
|
|
|
} else {
|
|
|
|
o = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if l > 0 && l < len(d) {
|
|
|
|
d = d[:l]
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, l)
|
|
|
|
n, err := b.Load(handle, buf, -int64(o))
|
|
|
|
|
|
|
|
// if we requested data beyond the end of the file, require
|
|
|
|
// ErrUnexpectedEOF error
|
|
|
|
if l > len(d) {
|
2016-08-29 17:18:57 +00:00
|
|
|
if errors.Cause(err) != io.ErrUnexpectedEOF {
|
2016-08-07 13:52:31 +00:00
|
|
|
t.Errorf("Load(%d, %d) did not return io.ErrUnexpectedEOF", len(buf), int64(o))
|
2016-08-08 19:57:50 +00:00
|
|
|
continue
|
2016-08-07 13:52:31 +00:00
|
|
|
}
|
2016-01-23 16:08:03 +00:00
|
|
|
err = nil
|
|
|
|
buf = buf[:len(d)]
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Load(%d, %d): unexpected error: %v", len(buf), int64(o), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if n != len(buf) {
|
|
|
|
t.Errorf("Load(%d, %d): wrong length returned, want %d, got %d",
|
|
|
|
len(buf), int64(o), len(buf), n)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = buf[:n]
|
|
|
|
if !bytes.Equal(buf, d) {
|
|
|
|
t.Errorf("Load(%d, %d) returned wrong bytes", len(buf), int64(o))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-07 22:48:03 +00:00
|
|
|
// load with a too-large buffer, this should return io.ErrUnexpectedEOF
|
|
|
|
buf := make([]byte, length+100)
|
|
|
|
n, err := b.Load(handle, buf, 0)
|
|
|
|
if n != length {
|
|
|
|
t.Errorf("wrong length for larger buffer returned, want %d, got %d", length, n)
|
|
|
|
}
|
|
|
|
|
2016-08-29 17:18:57 +00:00
|
|
|
if errors.Cause(err) != io.ErrUnexpectedEOF {
|
2016-02-07 22:48:03 +00:00
|
|
|
t.Errorf("wrong error returned for larger buffer: want io.ErrUnexpectedEOF, got %#v", err)
|
|
|
|
}
|
|
|
|
|
2016-09-04 12:38:18 +00:00
|
|
|
test.OK(t, b.Remove(restic.DataFile, id.String()))
|
2016-01-23 16:08:03 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 13:52:31 +00:00
|
|
|
// TestLoadNegativeOffset tests the backend's Load function with negative offsets.
|
|
|
|
func TestLoadNegativeOffset(t testing.TB) {
|
|
|
|
b := open(t)
|
|
|
|
defer close(t)
|
|
|
|
|
|
|
|
length := rand.Intn(1<<24) + 2000
|
|
|
|
|
2016-09-04 12:38:18 +00:00
|
|
|
data := test.Random(23, length)
|
2016-08-31 20:51:35 +00:00
|
|
|
id := restic.Hash(data)
|
2016-08-07 13:52:31 +00:00
|
|
|
|
2016-09-01 19:19:30 +00:00
|
|
|
handle := restic.Handle{Type: restic.DataFile, Name: id.String()}
|
2016-08-07 13:52:31 +00:00
|
|
|
err := b.Save(handle, data)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Save() error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// test normal reads
|
|
|
|
for i := 0; i < 50; i++ {
|
|
|
|
l := rand.Intn(length + 2000)
|
|
|
|
o := -rand.Intn(length + 2000)
|
|
|
|
|
|
|
|
buf := make([]byte, l)
|
|
|
|
n, err := b.Load(handle, buf, int64(o))
|
|
|
|
|
|
|
|
// if we requested data beyond the end of the file, require
|
|
|
|
// ErrUnexpectedEOF error
|
|
|
|
if len(buf) > -o {
|
2016-08-29 17:18:57 +00:00
|
|
|
if errors.Cause(err) != io.ErrUnexpectedEOF {
|
2016-08-07 13:52:31 +00:00
|
|
|
t.Errorf("Load(%d, %d) did not return io.ErrUnexpectedEOF", len(buf), o)
|
2016-08-08 19:57:50 +00:00
|
|
|
continue
|
2016-08-07 13:52:31 +00:00
|
|
|
}
|
|
|
|
err = nil
|
|
|
|
buf = buf[:-o]
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Load(%d, %d) returned error: %v", len(buf), o, err)
|
2016-08-08 19:57:50 +00:00
|
|
|
continue
|
2016-08-07 13:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if n != len(buf) {
|
|
|
|
t.Errorf("Load(%d, %d) returned short read, only got %d bytes", len(buf), o, n)
|
2016-08-08 19:57:50 +00:00
|
|
|
continue
|
2016-08-07 13:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p := len(data) + o
|
|
|
|
if !bytes.Equal(buf, data[p:p+len(buf)]) {
|
|
|
|
t.Errorf("Load(%d, %d) returned wrong bytes", len(buf), o)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-04 12:38:18 +00:00
|
|
|
test.OK(t, b.Remove(restic.DataFile, id.String()))
|
2016-08-07 13:52:31 +00:00
|
|
|
}
|
|
|
|
|
2016-01-24 15:59:38 +00:00
|
|
|
// TestSave tests saving data in the backend.
|
|
|
|
func TestSave(t testing.TB) {
|
|
|
|
b := open(t)
|
|
|
|
defer close(t)
|
2016-08-31 20:51:35 +00:00
|
|
|
var id restic.ID
|
2016-01-24 15:59:38 +00:00
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
2016-01-24 16:46:18 +00:00
|
|
|
length := rand.Intn(1<<23) + 200000
|
2016-09-04 12:38:18 +00:00
|
|
|
data := test.Random(23, length)
|
2016-01-24 16:46:18 +00:00
|
|
|
// use the first 32 byte as the ID
|
|
|
|
copy(id[:], data)
|
2016-01-24 15:59:38 +00:00
|
|
|
|
2016-08-31 20:39:36 +00:00
|
|
|
h := restic.Handle{
|
2016-09-01 19:19:30 +00:00
|
|
|
Type: restic.DataFile,
|
|
|
|
Name: fmt.Sprintf("%s-%d", id, i),
|
2016-01-24 15:59:38 +00:00
|
|
|
}
|
2016-01-24 16:46:18 +00:00
|
|
|
err := b.Save(h, data)
|
2016-09-04 12:38:18 +00:00
|
|
|
test.OK(t, err)
|
2016-01-24 15:59:38 +00:00
|
|
|
|
|
|
|
buf, err := backend.LoadAll(b, h, nil)
|
2016-09-04 12:38:18 +00:00
|
|
|
test.OK(t, err)
|
2016-01-24 15:59:38 +00:00
|
|
|
if len(buf) != len(data) {
|
|
|
|
t.Fatalf("number of bytes does not match, want %v, got %v", len(data), len(buf))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(buf, data) {
|
|
|
|
t.Fatalf("data not equal")
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := b.Stat(h)
|
2016-09-04 12:38:18 +00:00
|
|
|
test.OK(t, err)
|
2016-01-24 15:59:38 +00:00
|
|
|
|
|
|
|
if fi.Size != int64(len(data)) {
|
|
|
|
t.Fatalf("Stat() returned different size, want %q, got %d", len(data), fi.Size)
|
|
|
|
}
|
|
|
|
|
2016-09-01 19:19:30 +00:00
|
|
|
err = b.Remove(h.Type, h.Name)
|
2016-01-24 15:59:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error removing item: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var filenameTests = []struct {
|
|
|
|
name string
|
|
|
|
data string
|
|
|
|
}{
|
|
|
|
{"1dfc6bc0f06cb255889e9ea7860a5753e8eb9665c9a96627971171b444e3113e", "x"},
|
|
|
|
{"foobar", "foobar"},
|
|
|
|
{
|
|
|
|
"1dfc6bc0f06cb255889e9ea7860a5753e8eb9665c9a96627971171b444e3113e4bf8f2d9144cc5420a80f04a4880ad6155fc58903a4fb6457c476c43541dcaa6-5",
|
|
|
|
"foobar content of data blob",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSaveFilenames tests saving data with various file names in the backend.
|
|
|
|
func TestSaveFilenames(t testing.TB) {
|
|
|
|
b := open(t)
|
|
|
|
defer close(t)
|
|
|
|
|
|
|
|
for i, test := range filenameTests {
|
2016-09-01 19:19:30 +00:00
|
|
|
h := restic.Handle{Name: test.name, Type: restic.DataFile}
|
2016-01-24 15:59:38 +00:00
|
|
|
err := b.Save(h, []byte(test.data))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("test %d failed: Save() returned %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := backend.LoadAll(b, h, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("test %d failed: Load() returned %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(buf, []byte(test.data)) {
|
|
|
|
t.Errorf("test %d: returned wrong bytes", i)
|
|
|
|
}
|
|
|
|
|
2016-09-01 19:19:30 +00:00
|
|
|
err = b.Remove(h.Type, h.Name)
|
2016-01-24 15:59:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("test %d failed: Remove() returned %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 16:08:03 +00:00
|
|
|
var testStrings = []struct {
|
|
|
|
id string
|
|
|
|
data string
|
|
|
|
}{
|
|
|
|
{"c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2", "foobar"},
|
|
|
|
{"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"},
|
|
|
|
{"cc5d46bdb4991c6eae3eb739c9c8a7a46fe9654fab79c47b4fe48383b5b25e1c", "foo/bar"},
|
|
|
|
{"4e54d2c721cbdb730f01b10b62dec622962b36966ec685880effa63d71c808f2", "foo/../../baz"},
|
|
|
|
}
|
|
|
|
|
2016-08-31 20:51:35 +00:00
|
|
|
func store(t testing.TB, b restic.Backend, tpe restic.FileType, data []byte) {
|
|
|
|
id := restic.Hash(data)
|
2016-09-01 19:19:30 +00:00
|
|
|
err := b.Save(restic.Handle{Name: id.String(), Type: tpe}, data)
|
2016-09-04 12:38:18 +00:00
|
|
|
test.OK(t, err)
|
2016-01-23 16:08:03 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 18:12:02 +00:00
|
|
|
// TestBackend tests all functions of the backend.
|
|
|
|
func TestBackend(t testing.TB) {
|
2016-01-23 16:08:03 +00:00
|
|
|
b := open(t)
|
|
|
|
defer close(t)
|
|
|
|
|
2016-08-31 20:39:36 +00:00
|
|
|
for _, tpe := range []restic.FileType{
|
|
|
|
restic.DataFile, restic.KeyFile, restic.LockFile,
|
|
|
|
restic.SnapshotFile, restic.IndexFile,
|
2016-01-23 16:08:03 +00:00
|
|
|
} {
|
|
|
|
// detect non-existing files
|
2016-09-04 12:38:18 +00:00
|
|
|
for _, ts := range testStrings {
|
|
|
|
id, err := restic.ParseID(ts.id)
|
|
|
|
test.OK(t, err)
|
2016-01-23 16:08:03 +00:00
|
|
|
|
|
|
|
// test if blob is already in repository
|
|
|
|
ret, err := b.Test(tpe, id.String())
|
2016-09-04 12:38:18 +00:00
|
|
|
test.OK(t, err)
|
|
|
|
test.Assert(t, !ret, "blob was found to exist before creating")
|
2016-01-23 16:08:03 +00:00
|
|
|
|
2016-01-24 00:00:27 +00:00
|
|
|
// try to stat a not existing blob
|
2016-09-01 19:19:30 +00:00
|
|
|
h := restic.Handle{Type: tpe, Name: id.String()}
|
2016-01-24 00:00:27 +00:00
|
|
|
_, err = b.Stat(h)
|
2016-09-04 12:38:18 +00:00
|
|
|
test.Assert(t, err != nil, "blob data could be extracted before creation")
|
2016-01-23 16:08:03 +00:00
|
|
|
|
|
|
|
// try to read not existing blob
|
2016-01-24 00:00:27 +00:00
|
|
|
_, err = b.Load(h, nil, 0)
|
2016-09-04 12:38:18 +00:00
|
|
|
test.Assert(t, err != nil, "blob reader could be obtained before creation")
|
2016-01-23 16:08:03 +00:00
|
|
|
|
|
|
|
// try to get string out, should fail
|
|
|
|
ret, err = b.Test(tpe, id.String())
|
2016-09-04 12:38:18 +00:00
|
|
|
test.OK(t, err)
|
|
|
|
test.Assert(t, !ret, "id %q was found (but should not have)", ts.id)
|
2016-01-23 16:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add files
|
2016-09-04 12:38:18 +00:00
|
|
|
for _, ts := range testStrings {
|
|
|
|
store(t, b, tpe, []byte(ts.data))
|
2016-01-23 16:08:03 +00:00
|
|
|
|
2016-01-24 00:00:27 +00:00
|
|
|
// test Load()
|
2016-09-04 12:38:18 +00:00
|
|
|
h := restic.Handle{Type: tpe, Name: ts.id}
|
2016-01-24 00:00:27 +00:00
|
|
|
buf, err := backend.LoadAll(b, h, nil)
|
2016-09-04 12:38:18 +00:00
|
|
|
test.OK(t, err)
|
|
|
|
test.Equals(t, ts.data, string(buf))
|
2016-01-23 16:08:03 +00:00
|
|
|
|
|
|
|
// try to read it out with an offset and a length
|
|
|
|
start := 1
|
2016-09-04 12:38:18 +00:00
|
|
|
end := len(ts.data) - 2
|
2016-01-23 16:08:03 +00:00
|
|
|
length := end - start
|
|
|
|
|
2016-01-24 00:00:27 +00:00
|
|
|
buf2 := make([]byte, length)
|
|
|
|
n, err := b.Load(h, buf2, int64(start))
|
2016-09-04 12:38:18 +00:00
|
|
|
test.OK(t, err)
|
|
|
|
test.Equals(t, length, n)
|
|
|
|
test.Equals(t, ts.data[start:end], string(buf2))
|
2016-01-23 16:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test adding the first file again
|
2016-09-04 12:38:18 +00:00
|
|
|
ts := testStrings[0]
|
2016-01-23 16:08:03 +00:00
|
|
|
|
|
|
|
// create blob
|
2016-09-04 12:38:18 +00:00
|
|
|
err := b.Save(restic.Handle{Type: tpe, Name: ts.id}, []byte(ts.data))
|
|
|
|
test.Assert(t, err != nil, "expected error, got %v", err)
|
2016-01-23 16:08:03 +00:00
|
|
|
|
|
|
|
// remove and recreate
|
2016-09-04 12:38:18 +00:00
|
|
|
err = b.Remove(tpe, ts.id)
|
|
|
|
test.OK(t, err)
|
2016-01-23 16:08:03 +00:00
|
|
|
|
|
|
|
// test that the blob is gone
|
2016-09-04 12:38:18 +00:00
|
|
|
ok, err := b.Test(tpe, ts.id)
|
|
|
|
test.OK(t, err)
|
|
|
|
test.Assert(t, ok == false, "removed blob still present")
|
2016-01-23 16:08:03 +00:00
|
|
|
|
|
|
|
// create blob
|
2016-09-04 12:38:18 +00:00
|
|
|
err = b.Save(restic.Handle{Type: tpe, Name: ts.id}, []byte(ts.data))
|
|
|
|
test.OK(t, err)
|
2016-01-23 16:08:03 +00:00
|
|
|
|
|
|
|
// list items
|
2016-08-31 20:51:35 +00:00
|
|
|
IDs := restic.IDs{}
|
2016-01-23 16:08:03 +00:00
|
|
|
|
2016-09-04 12:38:18 +00:00
|
|
|
for _, ts := range testStrings {
|
|
|
|
id, err := restic.ParseID(ts.id)
|
|
|
|
test.OK(t, err)
|
2016-01-23 16:08:03 +00:00
|
|
|
IDs = append(IDs, id)
|
|
|
|
}
|
|
|
|
|
2016-08-31 20:51:35 +00:00
|
|
|
list := restic.IDs{}
|
2016-01-23 16:08:03 +00:00
|
|
|
|
|
|
|
for s := range b.List(tpe, nil) {
|
2016-09-04 12:38:18 +00:00
|
|
|
list = append(list, restic.TestParseID(s))
|
2016-01-23 16:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(IDs) != len(list) {
|
|
|
|
t.Fatalf("wrong number of IDs returned: want %d, got %d", len(IDs), len(list))
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(IDs)
|
|
|
|
sort.Sort(list)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(IDs, list) {
|
|
|
|
t.Fatalf("lists aren't equal, want:\n %v\n got:\n%v\n", IDs, list)
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove content if requested
|
2016-09-04 12:38:18 +00:00
|
|
|
if test.TestCleanupTempDirs {
|
|
|
|
for _, ts := range testStrings {
|
|
|
|
id, err := restic.ParseID(ts.id)
|
|
|
|
test.OK(t, err)
|
2016-01-23 16:08:03 +00:00
|
|
|
|
|
|
|
found, err := b.Test(tpe, id.String())
|
2016-09-04 12:38:18 +00:00
|
|
|
test.OK(t, err)
|
2016-01-23 16:08:03 +00:00
|
|
|
|
2016-09-04 12:38:18 +00:00
|
|
|
test.OK(t, b.Remove(tpe, id.String()))
|
2016-01-23 16:08:03 +00:00
|
|
|
|
|
|
|
found, err = b.Test(tpe, id.String())
|
2016-09-04 12:38:18 +00:00
|
|
|
test.OK(t, err)
|
|
|
|
test.Assert(t, !found, fmt.Sprintf("id %q not found after removal", id))
|
2016-01-23 16:08:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 18:12:02 +00:00
|
|
|
// TestDelete tests the Delete function.
|
|
|
|
func TestDelete(t testing.TB) {
|
2016-01-23 16:08:03 +00:00
|
|
|
b := open(t)
|
|
|
|
defer close(t)
|
|
|
|
|
2016-08-31 20:51:35 +00:00
|
|
|
be, ok := b.(restic.Deleter)
|
2016-01-23 16:08:03 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := be.Delete()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error deleting backend: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 18:12:02 +00:00
|
|
|
// TestCleanup runs the cleanup function after all tests are run.
|
|
|
|
func TestCleanup(t testing.TB) {
|
2016-01-23 16:08:03 +00:00
|
|
|
if CleanupFn == nil {
|
|
|
|
t.Log("CleanupFn function not set")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-04 12:38:18 +00:00
|
|
|
if !test.TestCleanupTempDirs {
|
2016-01-23 16:08:03 +00:00
|
|
|
t.Logf("not cleaning up backend")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := CleanupFn()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cleanup returned error: %v", err)
|
|
|
|
}
|
|
|
|
}
|