2017-06-10 11:10:08 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2018-06-09 15:22:33 +00:00
|
|
|
"io"
|
2017-06-10 11:10:08 +00:00
|
|
|
"math/rand"
|
2024-05-09 19:20:57 +00:00
|
|
|
"strings"
|
2018-06-09 15:22:33 +00:00
|
|
|
"sync"
|
2017-06-10 11:10:08 +00:00
|
|
|
"testing"
|
2018-06-09 15:22:33 +00:00
|
|
|
"time"
|
2017-06-10 11:10:08 +00:00
|
|
|
|
2018-06-09 15:22:33 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-06-10 11:10:08 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
|
|
|
"github.com/restic/restic/internal/backend/mem"
|
2024-05-09 16:59:29 +00:00
|
|
|
backendtest "github.com/restic/restic/internal/backend/test"
|
2017-06-10 11:10:08 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
"github.com/restic/restic/internal/test"
|
|
|
|
)
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func loadAndCompare(t testing.TB, be backend.Backend, h backend.Handle, data []byte) {
|
2024-05-09 16:59:29 +00:00
|
|
|
buf, err := backendtest.LoadAll(context.TODO(), be, h)
|
2017-06-10 11:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(buf) != len(data) {
|
|
|
|
t.Fatalf("wrong number of bytes read, want %v, got %v", len(data), len(buf))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(buf, data) {
|
|
|
|
t.Fatalf("wrong data returned, want:\n %02x\ngot:\n %02x", data[:16], buf[:16])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func save(t testing.TB, be backend.Backend, h backend.Handle, data []byte) {
|
|
|
|
err := be.Save(context.TODO(), h, backend.NewByteReader(data, be.Hasher()))
|
2017-06-10 11:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func remove(t testing.TB, be backend.Backend, h backend.Handle) {
|
2017-06-10 11:10:08 +00:00
|
|
|
err := be.Remove(context.TODO(), h)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func randomData(n int) (backend.Handle, []byte) {
|
2017-06-10 11:10:08 +00:00
|
|
|
data := test.Random(rand.Int(), n)
|
|
|
|
id := restic.Hash(data)
|
2023-10-01 09:40:12 +00:00
|
|
|
h := backend.Handle{
|
|
|
|
Type: backend.IndexFile,
|
2017-06-10 11:10:08 +00:00
|
|
|
Name: id.String(),
|
|
|
|
}
|
|
|
|
return h, data
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend(t *testing.T) {
|
|
|
|
be := mem.New()
|
2022-12-09 12:42:33 +00:00
|
|
|
c := TestNewCache(t)
|
2017-06-10 11:10:08 +00:00
|
|
|
wbe := c.Wrap(be)
|
|
|
|
|
|
|
|
h, data := randomData(5234142)
|
|
|
|
|
|
|
|
// save directly in backend
|
|
|
|
save(t, be, h, data)
|
|
|
|
if c.Has(h) {
|
|
|
|
t.Errorf("cache has file too early")
|
|
|
|
}
|
|
|
|
|
|
|
|
// load data via cache
|
|
|
|
loadAndCompare(t, wbe, h, data)
|
|
|
|
if !c.Has(h) {
|
2018-01-24 12:09:41 +00:00
|
|
|
t.Errorf("cache doesn't have file after load")
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove via cache
|
|
|
|
remove(t, wbe, h)
|
|
|
|
if c.Has(h) {
|
|
|
|
t.Errorf("cache has file after remove")
|
|
|
|
}
|
|
|
|
|
|
|
|
// save via cache
|
|
|
|
save(t, wbe, h, data)
|
|
|
|
if !c.Has(h) {
|
2018-01-24 12:09:41 +00:00
|
|
|
t.Errorf("cache doesn't have file after load")
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// load data directly from backend
|
|
|
|
loadAndCompare(t, be, h, data)
|
|
|
|
|
|
|
|
// load data via cache
|
2024-05-09 19:20:57 +00:00
|
|
|
loadAndCompare(t, wbe, h, data)
|
2017-06-10 11:10:08 +00:00
|
|
|
|
|
|
|
// remove directly
|
|
|
|
remove(t, be, h)
|
|
|
|
if !c.Has(h) {
|
|
|
|
t.Errorf("file not in cache any more")
|
|
|
|
}
|
|
|
|
|
|
|
|
// run stat
|
|
|
|
_, err := wbe.Stat(context.TODO(), h)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected error for removed file not found, got nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !wbe.IsNotExist(err) {
|
|
|
|
t.Errorf("Stat() returned error that does not match IsNotExist(): %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Has(h) {
|
|
|
|
t.Errorf("removed file still in cache after stat")
|
|
|
|
}
|
|
|
|
}
|
2018-06-09 15:22:33 +00:00
|
|
|
|
2024-05-09 19:20:57 +00:00
|
|
|
type loadCountingBackend struct {
|
|
|
|
backend.Backend
|
|
|
|
ctr int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *loadCountingBackend) Load(ctx context.Context, h backend.Handle, length int, offset int64, fn func(rd io.Reader) error) error {
|
|
|
|
l.ctr++
|
|
|
|
return l.Backend.Load(ctx, h, length, offset, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOutOfBoundsAccess(t *testing.T) {
|
|
|
|
be := &loadCountingBackend{Backend: mem.New()}
|
|
|
|
c := TestNewCache(t)
|
|
|
|
wbe := c.Wrap(be)
|
|
|
|
|
|
|
|
h, data := randomData(50)
|
|
|
|
save(t, be, h, data)
|
|
|
|
|
|
|
|
// load out of bounds
|
|
|
|
err := wbe.Load(context.TODO(), h, 100, 100, func(rd io.Reader) error {
|
|
|
|
t.Error("cache returned non-existant file section")
|
|
|
|
return errors.New("broken")
|
|
|
|
})
|
|
|
|
test.Assert(t, strings.Contains(err.Error(), " is too short"), "expected too short error, got %v", err)
|
|
|
|
test.Equals(t, 1, be.ctr, "expected file to be loaded only once")
|
|
|
|
// file must nevertheless get cached
|
|
|
|
if !c.Has(h) {
|
|
|
|
t.Errorf("cache doesn't have file after load")
|
|
|
|
}
|
|
|
|
|
|
|
|
// start within bounds, but request too large chunk
|
|
|
|
err = wbe.Load(context.TODO(), h, 100, 0, func(rd io.Reader) error {
|
|
|
|
t.Error("cache returned non-existant file section")
|
|
|
|
return errors.New("broken")
|
|
|
|
})
|
|
|
|
test.Assert(t, strings.Contains(err.Error(), " is too short"), "expected too short error, got %v", err)
|
|
|
|
test.Equals(t, 1, be.ctr, "expected file to be loaded only once")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestForget(t *testing.T) {
|
|
|
|
be := &loadCountingBackend{Backend: mem.New()}
|
|
|
|
c := TestNewCache(t)
|
|
|
|
wbe := c.Wrap(be)
|
|
|
|
|
|
|
|
h, data := randomData(50)
|
|
|
|
save(t, be, h, data)
|
|
|
|
|
|
|
|
loadAndCompare(t, wbe, h, data)
|
|
|
|
test.Equals(t, 1, be.ctr, "expected file to be loaded once")
|
|
|
|
|
|
|
|
// must still exist even if load returns an error
|
|
|
|
exp := errors.New("error")
|
|
|
|
err := wbe.Load(context.TODO(), h, 0, 0, func(rd io.Reader) error {
|
|
|
|
return exp
|
|
|
|
})
|
|
|
|
test.Equals(t, exp, err, "wrong error")
|
|
|
|
test.Assert(t, c.Has(h), "missing cache entry")
|
|
|
|
|
|
|
|
test.OK(t, c.Forget(h))
|
|
|
|
test.Assert(t, !c.Has(h), "cache entry should have been removed")
|
|
|
|
|
|
|
|
// cache it again
|
|
|
|
loadAndCompare(t, wbe, h, data)
|
|
|
|
test.Assert(t, c.Has(h), "missing cache entry")
|
|
|
|
|
|
|
|
// forget must delete file only once
|
|
|
|
err = c.Forget(h)
|
|
|
|
test.Assert(t, strings.Contains(err.Error(), "circuit breaker prevents repeated deletion of cached file"), "wrong error message %q", err)
|
|
|
|
test.Assert(t, c.Has(h), "cache entry should still exist")
|
|
|
|
}
|
|
|
|
|
2018-06-09 15:22:33 +00:00
|
|
|
type loadErrorBackend struct {
|
2023-10-01 09:40:12 +00:00
|
|
|
backend.Backend
|
2018-06-09 15:22:33 +00:00
|
|
|
loadError error
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func (be loadErrorBackend) Load(_ context.Context, _ backend.Handle, _ int, _ int64, _ func(rd io.Reader) error) error {
|
2018-06-09 15:22:33 +00:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
return be.loadError
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorBackend(t *testing.T) {
|
|
|
|
be := mem.New()
|
2022-12-09 12:42:33 +00:00
|
|
|
c := TestNewCache(t)
|
2018-06-09 15:22:33 +00:00
|
|
|
h, data := randomData(5234142)
|
|
|
|
|
|
|
|
// save directly in backend
|
|
|
|
save(t, be, h, data)
|
|
|
|
|
|
|
|
testErr := errors.New("test error")
|
|
|
|
errBackend := loadErrorBackend{
|
|
|
|
Backend: be,
|
|
|
|
loadError: testErr,
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
loadTest := func(wg *sync.WaitGroup, be backend.Backend) {
|
2018-06-09 15:22:33 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2024-05-09 16:59:29 +00:00
|
|
|
buf, err := backendtest.LoadAll(context.TODO(), be, h)
|
2018-06-09 15:22:33 +00:00
|
|
|
if err == testErr {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(buf, data) {
|
|
|
|
t.Errorf("data does not match")
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
wrappedBE := c.Wrap(errBackend)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go loadTest(&wg, wrappedBE)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|