2024-05-09 16:59:29 +00:00
|
|
|
package repository_test
|
2016-01-23 22:41:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-06-03 15:39:57 +00:00
|
|
|
"context"
|
2018-01-17 04:59:16 +00:00
|
|
|
"io"
|
2016-01-23 22:41:55 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2024-05-24 21:04:06 +00:00
|
|
|
"github.com/restic/restic/internal/backend/cache"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/backend/mem"
|
2022-06-12 12:38:19 +00:00
|
|
|
"github.com/restic/restic/internal/backend/mock"
|
2024-05-09 16:59:29 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2016-01-23 22:41:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const KiB = 1 << 10
|
|
|
|
const MiB = 1 << 20
|
|
|
|
|
2024-05-09 19:21:42 +00:00
|
|
|
func TestLoadRaw(t *testing.T) {
|
2016-01-23 22:41:55 +00:00
|
|
|
b := mem.New()
|
2024-05-09 16:59:29 +00:00
|
|
|
repo, err := repository.New(b, repository.Options{})
|
|
|
|
rtest.OK(t, err)
|
2016-01-23 22:41:55 +00:00
|
|
|
|
2024-05-09 16:59:29 +00:00
|
|
|
for i := 0; i < 5; i++ {
|
2024-05-09 19:21:42 +00:00
|
|
|
data := rtest.Random(23+i, 500*KiB)
|
2016-01-23 22:41:55 +00:00
|
|
|
|
2016-08-31 20:51:35 +00:00
|
|
|
id := restic.Hash(data)
|
2023-10-01 09:40:12 +00:00
|
|
|
h := backend.Handle{Name: id.String(), Type: backend.PackFile}
|
|
|
|
err := b.Save(context.TODO(), h, backend.NewByteReader(data, b.Hasher()))
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2016-01-23 22:41:55 +00:00
|
|
|
|
2024-05-09 16:59:29 +00:00
|
|
|
buf, err := repo.LoadRaw(context.TODO(), backend.PackFile, id)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2016-01-23 22:41:55 +00:00
|
|
|
|
|
|
|
if len(buf) != len(data) {
|
|
|
|
t.Errorf("length of returned buffer does not match, want %d, got %d", len(data), len(buf))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(buf, data) {
|
|
|
|
t.Errorf("wrong data returned")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-07 22:48:03 +00:00
|
|
|
|
2024-05-09 19:21:42 +00:00
|
|
|
func TestLoadRawBroken(t *testing.T) {
|
2021-09-19 17:58:47 +00:00
|
|
|
b := mock.NewBackend()
|
2024-05-09 16:59:29 +00:00
|
|
|
repo, err := repository.New(b, repository.Options{})
|
|
|
|
rtest.OK(t, err)
|
2021-09-19 17:58:47 +00:00
|
|
|
|
2024-05-09 19:21:42 +00:00
|
|
|
data := rtest.Random(23, 10*KiB)
|
2021-09-19 17:58:47 +00:00
|
|
|
id := restic.Hash(data)
|
|
|
|
// damage buffer
|
|
|
|
data[0] ^= 0xff
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
b.OpenReaderFn = func(ctx context.Context, h backend.Handle, length int, offset int64) (io.ReadCloser, error) {
|
2022-12-02 18:36:43 +00:00
|
|
|
return io.NopCloser(bytes.NewReader(data)), nil
|
2021-09-19 17:58:47 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 19:21:42 +00:00
|
|
|
// must detect but still return corrupt data
|
|
|
|
buf, err := repo.LoadRaw(context.TODO(), backend.PackFile, id)
|
|
|
|
rtest.Assert(t, bytes.Equal(buf, data), "wrong data returned")
|
2024-05-09 16:59:29 +00:00
|
|
|
rtest.Assert(t, errors.Is(err, restic.ErrInvalidData), "missing expected ErrInvalidData error, got %v", err)
|
2021-09-19 17:58:47 +00:00
|
|
|
|
2024-05-09 19:21:42 +00:00
|
|
|
// cause the first access to fail, but repair the data for the second access
|
|
|
|
data[0] ^= 0xff
|
|
|
|
loadCtr := 0
|
|
|
|
b.OpenReaderFn = func(ctx context.Context, h backend.Handle, length int, offset int64) (io.ReadCloser, error) {
|
|
|
|
data[0] ^= 0xff
|
|
|
|
loadCtr++
|
|
|
|
return io.NopCloser(bytes.NewReader(data)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// must retry load of corrupted data
|
|
|
|
buf, err = repo.LoadRaw(context.TODO(), backend.PackFile, id)
|
2021-09-19 17:58:47 +00:00
|
|
|
rtest.OK(t, err)
|
2024-05-09 19:21:42 +00:00
|
|
|
rtest.Assert(t, bytes.Equal(buf, data), "wrong data returned")
|
|
|
|
rtest.Equals(t, 2, loadCtr, "missing retry on broken data")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadRawBrokenWithCache(t *testing.T) {
|
|
|
|
b := mock.NewBackend()
|
|
|
|
c := cache.TestNewCache(t)
|
|
|
|
repo, err := repository.New(b, repository.Options{})
|
|
|
|
rtest.OK(t, err)
|
|
|
|
repo.UseCache(c)
|
2021-09-19 17:58:47 +00:00
|
|
|
|
2024-05-09 19:21:42 +00:00
|
|
|
data := rtest.Random(23, 10*KiB)
|
|
|
|
id := restic.Hash(data)
|
|
|
|
|
|
|
|
loadCtr := 0
|
|
|
|
// cause the first access to fail, but repair the data for the second access
|
|
|
|
b.OpenReaderFn = func(ctx context.Context, h backend.Handle, length int, offset int64) (io.ReadCloser, error) {
|
|
|
|
data[0] ^= 0xff
|
|
|
|
loadCtr++
|
|
|
|
return io.NopCloser(bytes.NewReader(data)), nil
|
2021-09-19 17:58:47 +00:00
|
|
|
}
|
2024-05-09 19:21:42 +00:00
|
|
|
|
|
|
|
// must retry load of corrupted data
|
|
|
|
buf, err := repo.LoadRaw(context.TODO(), backend.SnapshotFile, id)
|
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.Assert(t, bytes.Equal(buf, data), "wrong data returned")
|
|
|
|
rtest.Equals(t, 2, loadCtr, "missing retry on broken data")
|
2021-09-19 17:58:47 +00:00
|
|
|
}
|