2024-05-09 18:59:29 +02:00
|
|
|
package repository_test
|
2016-01-23 23:41:55 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-06-03 17:39:57 +02:00
|
|
|
"context"
|
2018-01-16 23:59:16 -05:00
|
|
|
"io"
|
2016-01-23 23:41:55 +01:00
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
2017-07-23 14:21:03 +02:00
|
|
|
"github.com/restic/restic/internal/backend"
|
|
|
|
"github.com/restic/restic/internal/backend/mem"
|
2022-06-12 14:38:19 +02:00
|
|
|
"github.com/restic/restic/internal/backend/mock"
|
2024-05-09 18:59:29 +02:00
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
2017-07-24 17:42:25 +02:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-10-02 15:06:39 +02:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2016-01-23 23:41:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const KiB = 1 << 10
|
|
|
|
const MiB = 1 << 20
|
|
|
|
|
|
|
|
func TestLoadAll(t *testing.T) {
|
|
|
|
b := mem.New()
|
2024-05-09 18:59:29 +02:00
|
|
|
repo, err := repository.New(b, repository.Options{})
|
|
|
|
rtest.OK(t, err)
|
2016-01-23 23:41:55 +01:00
|
|
|
|
2024-05-09 18:59:29 +02:00
|
|
|
for i := 0; i < 5; i++ {
|
2017-10-02 15:06:39 +02:00
|
|
|
data := rtest.Random(23+i, rand.Intn(MiB)+500*KiB)
|
2016-01-23 23:41:55 +01:00
|
|
|
|
2016-08-31 22:51:35 +02:00
|
|
|
id := restic.Hash(data)
|
2023-10-01 11:40:12 +02:00
|
|
|
h := backend.Handle{Name: id.String(), Type: backend.PackFile}
|
|
|
|
err := b.Save(context.TODO(), h, backend.NewByteReader(data, b.Hasher()))
|
2017-10-02 15:06:39 +02:00
|
|
|
rtest.OK(t, err)
|
2016-01-23 23:41:55 +01:00
|
|
|
|
2024-05-09 18:59:29 +02:00
|
|
|
buf, err := repo.LoadRaw(context.TODO(), backend.PackFile, id)
|
2017-10-02 15:06:39 +02:00
|
|
|
rtest.OK(t, err)
|
2016-01-23 23:41:55 +01: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 23:48:03 +01:00
|
|
|
|
2021-09-19 19:58:47 +02:00
|
|
|
type quickRetryBackend struct {
|
2023-10-01 11:40:12 +02:00
|
|
|
backend.Backend
|
2021-09-19 19:58:47 +02:00
|
|
|
}
|
|
|
|
|
2023-10-01 11:40:12 +02:00
|
|
|
func (be *quickRetryBackend) Load(ctx context.Context, h backend.Handle, length int, offset int64, fn func(rd io.Reader) error) error {
|
2021-09-19 19:58:47 +02:00
|
|
|
err := be.Backend.Load(ctx, h, length, offset, fn)
|
|
|
|
if err != nil {
|
|
|
|
// retry
|
|
|
|
err = be.Backend.Load(ctx, h, length, offset, fn)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadAllBroken(t *testing.T) {
|
|
|
|
b := mock.NewBackend()
|
2024-05-09 18:59:29 +02:00
|
|
|
repo, err := repository.New(b, repository.Options{})
|
|
|
|
rtest.OK(t, err)
|
2021-09-19 19:58:47 +02:00
|
|
|
|
|
|
|
data := rtest.Random(23, rand.Intn(MiB)+500*KiB)
|
|
|
|
id := restic.Hash(data)
|
|
|
|
// damage buffer
|
|
|
|
data[0] ^= 0xff
|
|
|
|
|
2023-10-01 11:40:12 +02:00
|
|
|
b.OpenReaderFn = func(ctx context.Context, h backend.Handle, length int, offset int64) (io.ReadCloser, error) {
|
2022-12-02 19:36:43 +01:00
|
|
|
return io.NopCloser(bytes.NewReader(data)), nil
|
2021-09-19 19:58:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// must fail on first try
|
2024-05-09 18:59:29 +02:00
|
|
|
_, err = repo.LoadRaw(context.TODO(), backend.PackFile, id)
|
|
|
|
rtest.Assert(t, errors.Is(err, restic.ErrInvalidData), "missing expected ErrInvalidData error, got %v", err)
|
2021-09-19 19:58:47 +02:00
|
|
|
|
|
|
|
// must return the broken data after a retry
|
|
|
|
be := &quickRetryBackend{Backend: b}
|
2024-05-09 18:59:29 +02:00
|
|
|
repo, err = repository.New(be, repository.Options{})
|
2021-09-19 19:58:47 +02:00
|
|
|
rtest.OK(t, err)
|
2024-05-09 18:59:29 +02:00
|
|
|
buf, err := repo.LoadRaw(context.TODO(), backend.PackFile, id)
|
|
|
|
rtest.Assert(t, errors.Is(err, restic.ErrInvalidData), "missing expected ErrInvalidData error, got %v", err)
|
2021-09-19 19:58:47 +02:00
|
|
|
|
|
|
|
if !bytes.Equal(buf, data) {
|
|
|
|
t.Fatalf("wrong data returned")
|
|
|
|
}
|
|
|
|
}
|