skip key decryption in tests

This commit is contained in:
Michael Eischer
2026-06-15 21:05:56 +02:00
parent c021b8cde0
commit b24d05bcb4
5 changed files with 33 additions and 0 deletions
+1
View File
@@ -34,6 +34,7 @@ func testWrapCheckPack(ctx context.Context, t *testing.T, repo *Repository,
// TestGapInBlobs creates a gap in the blob list by omitting the first entry before passing it to checkPack
func TestGapInBlobs(t *testing.T) {
TestInjectKey(t, restic.TestParseID("7bb3065bfb17da7430dc4dde4741d6db3dd83fdb0829500cf105755e067f879a"), `{"mac":{"k":"W1Y8bmQNJg6TAmuDt7lbpQ==","r":"r43DBmAdmwtQneoBTGAABQ=="},"encrypt":"JuZGBs6joRiLzqkyMWhmbZMLHe8+5oH6MDE5I6M8R/I="}`)
repo, _ := TestFromFixture(t, checkerTestData)
err := repo.LoadIndex(context.TODO(), restic.NoopTerminalCounterFactory)
+12
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"os"
"os/user"
"sync"
"time"
"github.com/restic/restic/internal/errors"
@@ -47,6 +48,9 @@ type Key struct {
// calibrated on the first run of AddKey().
var params *crypto.Params
// testKeyInjection is used to speed up tests by skipping the key decryption step.
var testKeyInjection = sync.Map{}
const (
// KDFTimeout specifies the maximum runtime for the KDF.
KDFTimeout = 500 * time.Millisecond
@@ -63,6 +67,14 @@ func createMasterKey(ctx context.Context, s *Repository, password string) (*Key,
// openKey tries do decrypt the key specified by name with the given password.
func openKey(ctx context.Context, s *Repository, id restic.ID, password string) (*Key, error) {
if key, ok := testKeyInjection.Load(id); ok {
return &Key{
master: key.(*crypto.Key),
user: key.(*crypto.Key), // not correct but good enough for testing
id: id,
}, nil
}
k, err := LoadKey(ctx, s, id)
if err != nil {
debug.Log("LoadKey(%v) returned error %v", id.String(), err)
+2
View File
@@ -319,6 +319,7 @@ func benchmarkLoadUnpacked(b *testing.B, version uint) {
var repoFixture = filepath.Join("testdata", "test-repo.tar.gz")
func TestRepositoryLoadIndex(t *testing.T) {
repository.TestInjectKey(t, restic.TestParseID("7bb3065bfb17da7430dc4dde4741d6db3dd83fdb0829500cf105755e067f879a"), `{"mac":{"k":"W1Y8bmQNJg6TAmuDt7lbpQ==","r":"r43DBmAdmwtQneoBTGAABQ=="},"encrypt":"JuZGBs6joRiLzqkyMWhmbZMLHe8+5oH6MDE5I6M8R/I="}`)
repo, _ := repository.TestFromFixture(t, repoFixture)
rtest.OK(t, repo.LoadIndex(context.TODO(), restic.NoopTerminalCounterFactory))
@@ -372,6 +373,7 @@ func (be *damageOnceBackend) Load(ctx context.Context, h backend.Handle, length
}
func TestRepositoryLoadUnpackedRetryBroken(t *testing.T) {
repository.TestInjectKey(t, restic.TestParseID("7bb3065bfb17da7430dc4dde4741d6db3dd83fdb0829500cf105755e067f879a"), `{"mac":{"k":"W1Y8bmQNJg6TAmuDt7lbpQ==","r":"r43DBmAdmwtQneoBTGAABQ=="},"encrypt":"JuZGBs6joRiLzqkyMWhmbZMLHe8+5oH6MDE5I6M8R/I="}`)
repodir := rtest.Env(t, repoFixture)
be, err := local.Open(context.TODO(), local.Config{Path: repodir, Connections: 2}, t.Logf)
+10
View File
@@ -2,6 +2,7 @@ package repository
import (
"context"
"encoding/json"
"fmt"
"os"
"sync"
@@ -193,3 +194,12 @@ func TestCheckRepo(t testing.TB, repo *Repository) {
t.Error(err)
}
}
func TestInjectKey(t testing.TB, keyID restic.ID, key string) {
var k crypto.Key
err := json.Unmarshal([]byte(key), &k)
if err != nil {
t.Fatal(err)
}
testKeyInjection.Store(keyID, &k)
}