2017-06-10 11:10:08 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2022-12-02 18:36:43 +00:00
|
|
|
"io"
|
2017-06-10 11:10:08 +00:00
|
|
|
"math/rand"
|
2020-06-24 13:22:51 +00:00
|
|
|
"os"
|
2022-09-17 17:37:09 +00:00
|
|
|
"runtime"
|
2017-06-10 11:10:08 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2020-06-24 13:22:51 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2023-02-10 21:39:40 +00:00
|
|
|
"github.com/restic/restic/internal/fs"
|
2017-06-10 11:10:08 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2024-05-09 16:21:53 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2020-06-24 13:22:51 +00:00
|
|
|
|
|
|
|
"golang.org/x/sync/errgroup"
|
2017-06-10 11:10:08 +00:00
|
|
|
)
|
|
|
|
|
2024-08-10 17:34:49 +00:00
|
|
|
func generateRandomFiles(t testing.TB, random *rand.Rand, tpe backend.FileType, c *Cache) restic.IDSet {
|
2017-06-10 11:10:08 +00:00
|
|
|
ids := restic.NewIDSet()
|
2024-08-10 17:34:49 +00:00
|
|
|
for i := 0; i < random.Intn(15)+10; i++ {
|
|
|
|
buf := rtest.Random(random.Int(), 1<<19)
|
2017-06-10 11:10:08 +00:00
|
|
|
id := restic.Hash(buf)
|
2023-10-01 09:40:12 +00:00
|
|
|
h := backend.Handle{Type: tpe, Name: id.String()}
|
2017-06-10 11:10:08 +00:00
|
|
|
|
|
|
|
if c.Has(h) {
|
|
|
|
t.Errorf("index %v present before save", id)
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:07:21 +00:00
|
|
|
err := c.save(h, bytes.NewReader(buf))
|
2017-06-10 11:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ids.Insert(id)
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
|
|
|
// randomID returns a random ID from s.
|
|
|
|
func randomID(s restic.IDSet) restic.ID {
|
|
|
|
for id := range s {
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
panic("set is empty")
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func load(t testing.TB, c *Cache, h backend.Handle) []byte {
|
2024-05-09 16:21:53 +00:00
|
|
|
rd, inCache, err := c.load(h, 0, 0)
|
2017-06-10 11:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-05-09 16:21:53 +00:00
|
|
|
rtest.Equals(t, true, inCache, "expected inCache flag to be true")
|
2017-06-10 11:10:08 +00:00
|
|
|
|
|
|
|
if rd == nil {
|
2020-03-02 17:27:52 +00:00
|
|
|
t.Fatalf("load() returned nil reader")
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 18:36:43 +00:00
|
|
|
buf, err := io.ReadAll(rd)
|
2017-06-10 11:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = rd.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
func listFiles(t testing.TB, c *Cache, tpe restic.FileType) restic.IDSet {
|
|
|
|
list, err := c.list(tpe)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("listing failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
|
|
|
func clearFiles(t testing.TB, c *Cache, tpe restic.FileType, valid restic.IDSet) {
|
|
|
|
if err := c.Clear(tpe, valid); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFiles(t *testing.T) {
|
|
|
|
seed := time.Now().Unix()
|
|
|
|
t.Logf("seed is %v", seed)
|
2024-08-10 17:34:49 +00:00
|
|
|
random := rand.New(rand.NewSource(seed))
|
2017-06-10 11:10:08 +00:00
|
|
|
|
2022-12-09 12:42:33 +00:00
|
|
|
c := TestNewCache(t)
|
2017-06-10 11:10:08 +00:00
|
|
|
|
|
|
|
var tests = []restic.FileType{
|
|
|
|
restic.SnapshotFile,
|
2020-08-16 09:16:38 +00:00
|
|
|
restic.PackFile,
|
2017-06-10 11:10:08 +00:00
|
|
|
restic.IndexFile,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tpe := range tests {
|
2022-10-16 08:30:59 +00:00
|
|
|
t.Run(tpe.String(), func(t *testing.T) {
|
2024-08-10 17:34:49 +00:00
|
|
|
ids := generateRandomFiles(t, random, tpe, c)
|
2017-06-10 11:10:08 +00:00
|
|
|
id := randomID(ids)
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
h := backend.Handle{Type: tpe, Name: id.String()}
|
2017-06-10 11:10:08 +00:00
|
|
|
id2 := restic.Hash(load(t, c, h))
|
|
|
|
|
|
|
|
if !id.Equal(id2) {
|
|
|
|
t.Errorf("wrong data returned, want %v, got %v", id.Str(), id2.Str())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.Has(h) {
|
|
|
|
t.Errorf("cache thinks index %v isn't present", id.Str())
|
|
|
|
}
|
|
|
|
|
|
|
|
list := listFiles(t, c, tpe)
|
|
|
|
if !ids.Equals(list) {
|
|
|
|
t.Errorf("wrong list of index IDs returned, want:\n %v\ngot:\n %v", ids, list)
|
|
|
|
}
|
|
|
|
|
|
|
|
clearFiles(t, c, tpe, restic.NewIDSet(id))
|
|
|
|
list2 := listFiles(t, c, tpe)
|
|
|
|
ids.Delete(id)
|
|
|
|
want := restic.NewIDSet(id)
|
|
|
|
if !list2.Equals(want) {
|
|
|
|
t.Errorf("ClearIndexes removed indexes, want:\n %v\ngot:\n %v", list2, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
clearFiles(t, c, tpe, restic.NewIDSet())
|
|
|
|
want = restic.NewIDSet()
|
|
|
|
list3 := listFiles(t, c, tpe)
|
|
|
|
if !list3.Equals(want) {
|
|
|
|
t.Errorf("ClearIndexes returned a wrong list, want:\n %v\ngot:\n %v", want, list3)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileLoad(t *testing.T) {
|
|
|
|
seed := time.Now().Unix()
|
|
|
|
t.Logf("seed is %v", seed)
|
2024-08-10 17:34:49 +00:00
|
|
|
random := rand.New(rand.NewSource(seed))
|
2017-06-10 11:10:08 +00:00
|
|
|
|
2022-12-09 12:42:33 +00:00
|
|
|
c := TestNewCache(t)
|
2017-06-10 11:10:08 +00:00
|
|
|
|
|
|
|
// save about 5 MiB of data in the cache
|
2024-08-10 17:34:49 +00:00
|
|
|
data := rtest.Random(random.Int(), 5234142)
|
2017-06-10 11:10:08 +00:00
|
|
|
id := restic.ID{}
|
|
|
|
copy(id[:], data)
|
2023-10-01 09:40:12 +00:00
|
|
|
h := backend.Handle{
|
2020-08-16 09:16:38 +00:00
|
|
|
Type: restic.PackFile,
|
2017-06-10 11:10:08 +00:00
|
|
|
Name: id.String(),
|
|
|
|
}
|
2024-05-09 11:07:21 +00:00
|
|
|
if err := c.save(h, bytes.NewReader(data)); err != nil {
|
2017-06-10 11:10:08 +00:00
|
|
|
t.Fatalf("Save() returned error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
offset int64
|
|
|
|
length int
|
|
|
|
}{
|
|
|
|
{0, 0},
|
|
|
|
{5, 0},
|
|
|
|
{32*1024 + 5, 0},
|
|
|
|
{0, 123},
|
|
|
|
{0, 64*1024 + 234},
|
2018-08-11 21:11:51 +00:00
|
|
|
{100, 5234142 - 100},
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%v/%v", test.length, test.offset), func(t *testing.T) {
|
2024-05-09 16:21:53 +00:00
|
|
|
rd, inCache, err := c.load(h, test.length, test.offset)
|
2017-06-10 11:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-05-09 16:21:53 +00:00
|
|
|
rtest.Equals(t, true, inCache, "expected inCache flag to be true")
|
2017-06-10 11:10:08 +00:00
|
|
|
|
2022-12-02 18:36:43 +00:00
|
|
|
buf, err := io.ReadAll(rd)
|
2017-06-10 11:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = rd.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
o := int(test.offset)
|
|
|
|
l := test.length
|
|
|
|
if test.length == 0 {
|
|
|
|
l = len(data) - o
|
|
|
|
}
|
|
|
|
|
|
|
|
if l > len(data)-o {
|
|
|
|
l = len(data) - o
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(buf) != l {
|
|
|
|
t.Fatalf("wrong number of bytes returned: want %d, got %d", l, len(buf))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(buf, data[o:o+l]) {
|
|
|
|
t.Fatalf("wrong data returned, want:\n %02x\ngot:\n %02x", data[o:o+16], buf[:16])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-06-24 13:22:51 +00:00
|
|
|
|
|
|
|
// Simulate multiple processes writing to a cache, using goroutines.
|
2022-09-17 17:37:09 +00:00
|
|
|
//
|
|
|
|
// The possibility of sharing a cache between multiple concurrent restic
|
2022-09-24 11:07:01 +00:00
|
|
|
// processes isn't guaranteed in the docs and doesn't always work on Windows, hence the
|
|
|
|
// check on GOOS. Cache sharing is considered a "nice to have" on POSIX, for now.
|
2022-09-17 17:37:09 +00:00
|
|
|
//
|
2022-09-24 11:07:01 +00:00
|
|
|
// The cache first creates a temporary file and then renames it to its final name.
|
|
|
|
// On Windows renaming internally creates a file handle with a shareMode which
|
|
|
|
// includes FILE_SHARE_DELETE. The Go runtime opens files without FILE_SHARE_DELETE,
|
|
|
|
// thus Open(fn) will fail until the file handle used for renaming was closed.
|
2022-09-17 17:37:09 +00:00
|
|
|
// See https://devblogs.microsoft.com/oldnewthing/20211022-00/?p=105822
|
|
|
|
// for hints on how to fix this properly.
|
2020-06-24 13:22:51 +00:00
|
|
|
func TestFileSaveConcurrent(t *testing.T) {
|
2022-09-17 17:37:09 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("may not work due to FILE_SHARE_DELETE issue")
|
|
|
|
}
|
|
|
|
|
2024-08-10 17:34:49 +00:00
|
|
|
seed := time.Now().Unix()
|
|
|
|
t.Logf("seed is %v", seed)
|
|
|
|
random := rand.New(rand.NewSource(seed))
|
|
|
|
|
2020-06-24 13:22:51 +00:00
|
|
|
const nproc = 40
|
|
|
|
|
|
|
|
var (
|
2022-12-09 12:42:33 +00:00
|
|
|
c = TestNewCache(t)
|
2024-05-09 16:21:53 +00:00
|
|
|
data = rtest.Random(1, 10000)
|
2020-06-24 13:22:51 +00:00
|
|
|
g errgroup.Group
|
|
|
|
id restic.ID
|
|
|
|
)
|
2024-08-10 17:34:49 +00:00
|
|
|
|
|
|
|
random.Read(id[:])
|
2020-06-24 13:22:51 +00:00
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
h := backend.Handle{
|
2020-06-24 13:22:51 +00:00
|
|
|
Type: restic.PackFile,
|
|
|
|
Name: id.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < nproc/2; i++ {
|
2024-05-09 11:07:21 +00:00
|
|
|
g.Go(func() error { return c.save(h, bytes.NewReader(data)) })
|
2020-06-24 13:22:51 +00:00
|
|
|
|
|
|
|
// Can't use load because only the main goroutine may call t.Fatal.
|
|
|
|
g.Go(func() error {
|
|
|
|
// The timing is hard to get right, but the main thing we want to
|
|
|
|
// ensure is ENOENT or nil error.
|
|
|
|
time.Sleep(time.Duration(100+rand.Intn(200)) * time.Millisecond)
|
|
|
|
|
2024-05-09 16:21:53 +00:00
|
|
|
f, _, err := c.load(h, 0, 0)
|
2020-06-24 13:22:51 +00:00
|
|
|
t.Logf("Load error: %v", err)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
case errors.Is(err, os.ErrNotExist):
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() { _ = f.Close() }()
|
|
|
|
|
2022-12-02 18:36:43 +00:00
|
|
|
read, err := io.ReadAll(f)
|
2020-06-24 13:22:51 +00:00
|
|
|
if err == nil && !bytes.Equal(read, data) {
|
|
|
|
err = errors.New("mismatch between Save and Load")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-09 16:21:53 +00:00
|
|
|
rtest.OK(t, g.Wait())
|
2020-06-24 13:22:51 +00:00
|
|
|
saved := load(t, c, h)
|
2024-05-09 16:21:53 +00:00
|
|
|
rtest.Equals(t, data, saved)
|
2020-06-24 13:22:51 +00:00
|
|
|
}
|
2023-02-10 21:39:40 +00:00
|
|
|
|
|
|
|
func TestFileSaveAfterDamage(t *testing.T) {
|
|
|
|
c := TestNewCache(t)
|
2024-05-09 16:21:53 +00:00
|
|
|
rtest.OK(t, fs.RemoveAll(c.path))
|
2023-02-10 21:39:40 +00:00
|
|
|
|
|
|
|
// save a few bytes of data in the cache
|
2024-05-09 16:21:53 +00:00
|
|
|
data := rtest.Random(123456789, 42)
|
2023-02-10 21:39:40 +00:00
|
|
|
id := restic.Hash(data)
|
2023-10-01 09:40:12 +00:00
|
|
|
h := backend.Handle{
|
2023-02-10 21:39:40 +00:00
|
|
|
Type: restic.PackFile,
|
|
|
|
Name: id.String(),
|
|
|
|
}
|
2024-05-09 11:07:21 +00:00
|
|
|
if err := c.save(h, bytes.NewReader(data)); err == nil {
|
2023-02-10 21:39:40 +00:00
|
|
|
t.Fatal("Missing error when saving to deleted cache directory")
|
|
|
|
}
|
|
|
|
}
|