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.
This commit is contained in:
Michael Eischer 2020-12-19 12:50:22 +01:00
parent a009b39e4c
commit 51b7e3119b

View file

@ -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.