From 2dbb18128c4923f271691ad2530cb24b60d520b5 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 3 Feb 2024 17:47:48 +0100 Subject: [PATCH] repository: Allow skipping verification for tests Some tests have to explicitly create pack files with blobs that don't match their ID. For those blobs the builtin verification of the repository must be disabled. --- internal/repository/repack_test.go | 6 ++++-- internal/repository/repair_pack_test.go | 3 ++- internal/repository/repository.go | 13 +++++++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/internal/repository/repack_test.go b/internal/repository/repack_test.go index 63845d5b1..c07c0a943 100644 --- a/internal/repository/repack_test.go +++ b/internal/repository/repack_test.go @@ -336,7 +336,8 @@ func TestRepackWrongBlob(t *testing.T) { } func testRepackWrongBlob(t *testing.T, version uint) { - repo := repository.TestRepositoryWithVersion(t, version) + // disable verification to allow adding corrupted blobs to the repository + repo := repository.TestRepositoryWithBackend(t, nil, version, repository.Options{NoVerifyPack: true}) seed := time.Now().UnixNano() rand.Seed(seed) @@ -361,7 +362,8 @@ func TestRepackBlobFallback(t *testing.T) { } func testRepackBlobFallback(t *testing.T, version uint) { - repo := repository.TestRepositoryWithVersion(t, version) + // disable verification to allow adding corrupted blobs to the repository + repo := repository.TestRepositoryWithBackend(t, nil, version, repository.Options{NoVerifyPack: true}) seed := time.Now().UnixNano() rand.Seed(seed) diff --git a/internal/repository/repair_pack_test.go b/internal/repository/repair_pack_test.go index 3f9477945..c9b0badfc 100644 --- a/internal/repository/repair_pack_test.go +++ b/internal/repository/repair_pack_test.go @@ -102,7 +102,8 @@ func testRepairBrokenPack(t *testing.T, version uint) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - repo := repository.TestRepositoryWithVersion(t, version) + // disable verification to allow adding corrupted blobs to the repository + repo := repository.TestRepositoryWithBackend(t, nil, version, repository.Options{NoVerifyPack: true}) seed := time.Now().UnixNano() rand.Seed(seed) diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 2584f42c7..706e84876 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -59,8 +59,9 @@ type Repository struct { } type Options struct { - Compression CompressionMode - PackSize uint + Compression CompressionMode + PackSize uint + NoVerifyPack bool } // CompressionMode configures if data should be compressed. @@ -444,6 +445,10 @@ func (r *Repository) saveAndEncrypt(ctx context.Context, t restic.BlobType, data } func (r *Repository) verifyCiphertext(buf []byte, uncompressedLength int, id restic.ID) error { + if r.opts.NoVerifyPack { + return nil + } + nonce, ciphertext := buf[:r.key.NonceSize()], buf[r.key.NonceSize():] plaintext, err := r.key.Open(nil, nonce, ciphertext, nil) if err != nil { @@ -539,6 +544,10 @@ func (r *Repository) SaveUnpacked(ctx context.Context, t restic.FileType, buf [] } func (r *Repository) verifyUnpacked(buf []byte, t restic.FileType, expected []byte) error { + if r.opts.NoVerifyPack { + return nil + } + nonce, ciphertext := buf[:r.key.NonceSize()], buf[r.key.NonceSize():] plaintext, err := r.key.Open(nil, nonce, ciphertext, nil) if err != nil {