repository: fix test setup race conditions

This commit is contained in:
Michael Eischer 2024-03-28 23:14:32 +01:00
parent 8155dbe711
commit 5e98f1e2eb
3 changed files with 25 additions and 16 deletions

View File

@ -43,9 +43,9 @@ type Key struct {
id restic.ID id restic.ID
} }
// Params tracks the parameters used for the KDF. If not set, it will be // params tracks the parameters used for the KDF. If not set, it will be
// calibrated on the first run of AddKey(). // calibrated on the first run of AddKey().
var Params *crypto.Params var params *crypto.Params
const ( const (
// KDFTimeout specifies the maximum runtime for the KDF. // KDFTimeout specifies the maximum runtime for the KDF.
@ -196,13 +196,13 @@ func LoadKey(ctx context.Context, s *Repository, id restic.ID) (k *Key, err erro
// AddKey adds a new key to an already existing repository. // AddKey adds a new key to an already existing repository.
func AddKey(ctx context.Context, s *Repository, password, username, hostname string, template *crypto.Key) (*Key, error) { func AddKey(ctx context.Context, s *Repository, password, username, hostname string, template *crypto.Key) (*Key, error) {
// make sure we have valid KDF parameters // make sure we have valid KDF parameters
if Params == nil { if params == nil {
p, err := crypto.Calibrate(KDFTimeout, KDFMemory) p, err := crypto.Calibrate(KDFTimeout, KDFMemory)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "Calibrate") return nil, errors.Wrap(err, "Calibrate")
} }
Params = &p params = &p
debug.Log("calibrated KDF parameters are %v", p) debug.Log("calibrated KDF parameters are %v", p)
} }
@ -213,9 +213,9 @@ func AddKey(ctx context.Context, s *Repository, password, username, hostname str
Hostname: hostname, Hostname: hostname,
KDF: "scrypt", KDF: "scrypt",
N: Params.N, N: params.N,
R: Params.R, R: params.R,
P: Params.P, P: params.P,
} }
if newkey.Hostname == "" { if newkey.Hostname == "" {
@ -237,7 +237,7 @@ func AddKey(ctx context.Context, s *Repository, password, username, hostname str
} }
// call KDF to derive user key // call KDF to derive user key
newkey.user, err = crypto.KDF(*Params, newkey.Salt, password) newkey.user, err = crypto.KDF(*params, newkey.Salt, password)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"sync"
"testing" "testing"
"github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend"
@ -21,14 +22,18 @@ type logger interface {
Logf(format string, args ...interface{}) Logf(format string, args ...interface{})
} }
var paramsOnce sync.Once
// TestUseLowSecurityKDFParameters configures low-security KDF parameters for testing. // TestUseLowSecurityKDFParameters configures low-security KDF parameters for testing.
func TestUseLowSecurityKDFParameters(t logger) { func TestUseLowSecurityKDFParameters(t logger) {
t.Logf("using low-security KDF parameters for test") t.Logf("using low-security KDF parameters for test")
Params = &crypto.Params{ paramsOnce.Do(func() {
N: 128, params = &crypto.Params{
R: 1, N: 128,
P: 1, R: 1,
} P: 1,
}
})
} }
// TestBackend returns a fully configured in-memory backend. // TestBackend returns a fully configured in-memory backend.
@ -36,7 +41,7 @@ func TestBackend(_ testing.TB) backend.Backend {
return mem.New() return mem.New()
} }
const TestChunkerPol = chunker.Pol(0x3DA3358B4DC173) const testChunkerPol = chunker.Pol(0x3DA3358B4DC173)
// TestRepositoryWithBackend returns a repository initialized with a test // TestRepositoryWithBackend returns a repository initialized with a test
// password. If be is nil, an in-memory backend is used. A constant polynomial // password. If be is nil, an in-memory backend is used. A constant polynomial
@ -55,7 +60,7 @@ func TestRepositoryWithBackend(t testing.TB, be backend.Backend, version uint, o
t.Fatalf("TestRepository(): new repo failed: %v", err) t.Fatalf("TestRepository(): new repo failed: %v", err)
} }
cfg := restic.TestCreateConfig(t, TestChunkerPol, version) cfg := restic.TestCreateConfig(t, testChunkerPol, version)
err = repo.init(context.TODO(), test.TestPassword, cfg) err = repo.init(context.TODO(), test.TestPassword, cfg)
if err != nil { if err != nil {
t.Fatalf("TestRepository(): initialize repo failed: %v", err) t.Fatalf("TestRepository(): initialize repo failed: %v", err)

View File

@ -2,6 +2,7 @@ package restic
import ( import (
"context" "context"
"sync"
"testing" "testing"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
@ -67,12 +68,15 @@ func TestCreateConfig(t testing.TB, pol chunker.Pol, version uint) (cfg Config)
} }
var checkPolynomial = true var checkPolynomial = true
var checkPolynomialOnce sync.Once
// TestDisableCheckPolynomial disables the check that the polynomial used for // TestDisableCheckPolynomial disables the check that the polynomial used for
// the chunker. // the chunker.
func TestDisableCheckPolynomial(t testing.TB) { func TestDisableCheckPolynomial(t testing.TB) {
t.Logf("disabling check of the chunker polynomial") t.Logf("disabling check of the chunker polynomial")
checkPolynomial = false checkPolynomialOnce.Do(func() {
checkPolynomial = false
})
} }
// LoadConfig returns loads, checks and returns the config for a repository. // LoadConfig returns loads, checks and returns the config for a repository.