2016-04-10 14:52:15 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2017-06-04 09:16:55 +00:00
|
|
|
"context"
|
2022-04-29 21:16:16 +00:00
|
|
|
"fmt"
|
2016-04-10 14:52:15 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
2016-07-31 15:45:22 +00:00
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/backend/local"
|
|
|
|
"github.com/restic/restic/internal/backend/mem"
|
2023-01-14 15:06:35 +00:00
|
|
|
"github.com/restic/restic/internal/backend/retry"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/crypto"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/test"
|
|
|
|
|
2016-07-31 15:45:22 +00:00
|
|
|
"github.com/restic/chunker"
|
2016-04-10 14:52:15 +00:00
|
|
|
)
|
|
|
|
|
2017-03-25 17:00:42 +00:00
|
|
|
type logger interface {
|
|
|
|
Logf(format string, args ...interface{})
|
|
|
|
}
|
|
|
|
|
2016-08-21 11:42:04 +00:00
|
|
|
// TestUseLowSecurityKDFParameters configures low-security KDF parameters for testing.
|
2017-03-25 17:00:42 +00:00
|
|
|
func TestUseLowSecurityKDFParameters(t logger) {
|
2016-08-21 11:42:04 +00:00
|
|
|
t.Logf("using low-security KDF parameters for test")
|
2024-02-24 16:20:10 +00:00
|
|
|
Params = &crypto.Params{
|
|
|
|
N: 128,
|
|
|
|
R: 1,
|
|
|
|
P: 1,
|
|
|
|
}
|
2016-08-21 11:42:04 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 14:52:15 +00:00
|
|
|
// TestBackend returns a fully configured in-memory backend.
|
2023-10-01 09:40:12 +00:00
|
|
|
func TestBackend(_ testing.TB) backend.Backend {
|
2022-12-11 09:41:22 +00:00
|
|
|
return mem.New()
|
2016-04-10 14:52:15 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 13:29:34 +00:00
|
|
|
const TestChunkerPol = chunker.Pol(0x3DA3358B4DC173)
|
2016-07-31 15:45:22 +00:00
|
|
|
|
2016-04-10 14:52:15 +00:00
|
|
|
// TestRepositoryWithBackend returns a repository initialized with a test
|
2016-07-31 15:45:22 +00:00
|
|
|
// password. If be is nil, an in-memory backend is used. A constant polynomial
|
2016-08-21 11:42:04 +00:00
|
|
|
// is used for the chunker and low-security test parameters.
|
2024-02-03 16:47:36 +00:00
|
|
|
func TestRepositoryWithBackend(t testing.TB, be backend.Backend, version uint, opts Options) restic.Repository {
|
2020-02-17 12:07:55 +00:00
|
|
|
t.Helper()
|
2016-08-21 11:42:04 +00:00
|
|
|
TestUseLowSecurityKDFParameters(t)
|
2018-03-11 20:42:39 +00:00
|
|
|
restic.TestDisableCheckPolynomial(t)
|
2016-08-21 11:42:04 +00:00
|
|
|
|
2016-04-10 14:52:15 +00:00
|
|
|
if be == nil {
|
2022-12-11 09:41:22 +00:00
|
|
|
be = TestBackend(t)
|
2016-04-10 14:52:15 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 16:47:36 +00:00
|
|
|
repo, err := New(be, opts)
|
2022-07-02 21:30:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("TestRepository(): new repo failed: %v", err)
|
|
|
|
}
|
2016-04-10 14:52:15 +00:00
|
|
|
|
2022-04-29 21:16:16 +00:00
|
|
|
cfg := restic.TestCreateConfig(t, TestChunkerPol, version)
|
2022-07-02 21:30:26 +00:00
|
|
|
err = repo.init(context.TODO(), test.TestPassword, cfg)
|
2016-04-10 14:52:15 +00:00
|
|
|
if err != nil {
|
2016-07-31 15:45:22 +00:00
|
|
|
t.Fatalf("TestRepository(): initialize repo failed: %v", err)
|
2016-04-10 14:52:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-11 09:41:22 +00:00
|
|
|
return repo
|
2016-04-10 14:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestRepository returns a repository initialized with a test password on an
|
|
|
|
// in-memory backend. When the environment variable RESTIC_TEST_REPO is set to
|
|
|
|
// a non-existing directory, a local backend is created there and this is used
|
2016-07-31 15:45:22 +00:00
|
|
|
// instead. The directory is not removed, but left there for inspection.
|
2022-12-11 09:41:22 +00:00
|
|
|
func TestRepository(t testing.TB) restic.Repository {
|
2022-04-29 21:16:16 +00:00
|
|
|
t.Helper()
|
|
|
|
return TestRepositoryWithVersion(t, 0)
|
|
|
|
}
|
|
|
|
|
2022-12-11 09:41:22 +00:00
|
|
|
func TestRepositoryWithVersion(t testing.TB, version uint) restic.Repository {
|
2020-02-17 12:07:55 +00:00
|
|
|
t.Helper()
|
2016-04-10 14:52:15 +00:00
|
|
|
dir := os.Getenv("RESTIC_TEST_REPO")
|
2024-02-03 16:47:36 +00:00
|
|
|
opts := Options{}
|
2016-04-10 14:52:15 +00:00
|
|
|
if dir != "" {
|
|
|
|
_, err := os.Stat(dir)
|
|
|
|
if err != nil {
|
2020-09-19 20:01:32 +00:00
|
|
|
be, err := local.Create(context.TODO(), local.Config{Path: dir})
|
2016-04-10 14:52:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating local backend at %v: %v", dir, err)
|
|
|
|
}
|
2024-02-03 16:47:36 +00:00
|
|
|
return TestRepositoryWithBackend(t, be, version, opts)
|
2016-04-10 14:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Logf("directory at %v already exists, using mem backend", dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-03 16:47:36 +00:00
|
|
|
return TestRepositoryWithBackend(t, nil, version, opts)
|
2016-04-10 14:52:15 +00:00
|
|
|
}
|
2016-09-04 12:29:04 +00:00
|
|
|
|
2024-02-24 20:45:24 +00:00
|
|
|
func TestFromFixture(t testing.TB, repoFixture string) (restic.Repository, func()) {
|
|
|
|
repodir, cleanup := test.Env(t, repoFixture)
|
|
|
|
repo := TestOpenLocal(t, repodir)
|
|
|
|
|
|
|
|
return repo, cleanup
|
|
|
|
}
|
|
|
|
|
2016-09-04 12:29:04 +00:00
|
|
|
// TestOpenLocal opens a local repository.
|
2024-02-24 20:45:24 +00:00
|
|
|
func TestOpenLocal(t testing.TB, dir string) restic.Repository {
|
2023-10-01 09:40:12 +00:00
|
|
|
var be backend.Backend
|
2021-08-07 17:50:00 +00:00
|
|
|
be, err := local.Open(context.TODO(), local.Config{Path: dir, Connections: 2})
|
2016-09-04 12:29:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-01-14 15:06:35 +00:00
|
|
|
be = retry.New(be, 3, nil, nil)
|
|
|
|
|
2024-02-24 20:45:24 +00:00
|
|
|
return TestOpenBackend(t, be)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenBackend(t testing.TB, be backend.Backend) restic.Repository {
|
2022-07-02 21:30:26 +00:00
|
|
|
repo, err := New(be, Options{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-25 14:10:45 +00:00
|
|
|
err = repo.SearchKey(context.TODO(), test.TestPassword, 10, "")
|
2016-09-04 12:29:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return repo
|
|
|
|
}
|
2022-04-29 21:16:16 +00:00
|
|
|
|
|
|
|
type VersionedTest func(t *testing.T, version uint)
|
|
|
|
|
|
|
|
func TestAllVersions(t *testing.T, test VersionedTest) {
|
|
|
|
for version := restic.MinRepoVersion; version <= restic.MaxRepoVersion; version++ {
|
|
|
|
t.Run(fmt.Sprintf("v%d", version), func(t *testing.T) {
|
|
|
|
test(t, uint(version))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type VersionedBenchmark func(b *testing.B, version uint)
|
|
|
|
|
|
|
|
func BenchmarkAllVersions(b *testing.B, bench VersionedBenchmark) {
|
|
|
|
for version := restic.MinRepoVersion; version <= restic.MaxRepoVersion; version++ {
|
|
|
|
b.Run(fmt.Sprintf("v%d", version), func(b *testing.B) {
|
|
|
|
bench(b, uint(version))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|