From 51b7e3119b5edc9b2539f19c7a344ba07d74c21f Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 19 Dec 2020 12:50:22 +0100 Subject: [PATCH] mem: calculate md5 content hash for uploads The mem backend is primarily used for testing. This ensures that the upload hash calculation gets appropriate test coverage. --- internal/backend/mem/mem_backend.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/internal/backend/mem/mem_backend.go b/internal/backend/mem/mem_backend.go index 0227ee6ed..983976519 100644 --- a/internal/backend/mem/mem_backend.go +++ b/internal/backend/mem/mem_backend.go @@ -3,6 +3,8 @@ package mem import ( "bytes" "context" + "crypto/md5" + "encoding/base64" "hash" "io" "io/ioutil" @@ -87,6 +89,16 @@ func (be *MemoryBackend) Save(ctx context.Context, h restic.Handle, rd restic.Re return errors.Errorf("wrote %d bytes instead of the expected %d bytes", len(buf), rd.Length()) } + beHash := be.Hasher() + // must never fail according to interface + _, _ = beHash.Write(buf) + if !bytes.Equal(beHash.Sum(nil), rd.Hash()) { + return errors.Errorf("invalid file hash or content, got %s expected %s", + base64.RawStdEncoding.EncodeToString(beHash.Sum(nil)), + base64.RawStdEncoding.EncodeToString(rd.Hash()), + ) + } + be.data[h] = buf debug.Log("saved %v bytes at %v", len(buf), h) @@ -217,7 +229,7 @@ func (be *MemoryBackend) Location() string { // Hasher may return a hash function for calculating a content hash for the backend func (be *MemoryBackend) Hasher() hash.Hash { - return nil + return md5.New() } // Delete removes all data in the backend.