mirror of
https://github.com/borgbackup/borg.git
synced 2026-06-11 01:41:57 -04:00
file integrity: add pure_hash option and tests for SHA256FileHashingWrapper, fixes #9704
This commit is contained in:
parent
6b93751a9a
commit
4c6e8fb957
2 changed files with 33 additions and 2 deletions
|
|
@ -60,13 +60,14 @@ class FileHashingWrapper(FileLikeWrapper):
|
|||
ALGORITHM: str = None
|
||||
FACTORY: Callable = None
|
||||
|
||||
def __init__(self, backing_fd, write):
|
||||
def __init__(self, backing_fd, write, *, pure_hash: bool = False):
|
||||
super().__init__(backing_fd)
|
||||
self.writing = write
|
||||
self.hash = self.FACTORY()
|
||||
self.pure_hash = pure_hash # if True, we don't hash the length of the file
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
if exc_type is None:
|
||||
if exc_type is None and not self.pure_hash:
|
||||
self.hash_length()
|
||||
super().__exit__(exc_type, exc_val, exc_tb)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
import hashlib
|
||||
import io
|
||||
|
||||
import pytest
|
||||
|
||||
from ...crypto.file_integrity import DetachedIntegrityCheckedFile, FileIntegrityError, IntegrityCheckedFile
|
||||
from ...crypto.file_integrity import SHA256FileHashingWrapper
|
||||
from ...platform import SyncFile
|
||||
|
||||
|
||||
|
|
@ -147,3 +151,29 @@ class TestIntegrityCheckedFileWithSyncFile:
|
|||
# verify the written data can be read back with integrity check
|
||||
with IntegrityCheckedFile(path=path, write=False, integrity_data=integrity_data) as fd:
|
||||
assert fd.read() == b"test data for integrity check"
|
||||
|
||||
|
||||
class TestSHA256FileHashingWrapper:
|
||||
def test_pure_hash_write(self):
|
||||
bio = io.BytesIO()
|
||||
data = b"hello world"
|
||||
with SHA256FileHashingWrapper(bio, write=True, pure_hash=True) as wrapper:
|
||||
wrapper.write(data)
|
||||
assert bio.getvalue() == data
|
||||
assert wrapper.hexdigest() == hashlib.sha256(data).hexdigest()
|
||||
|
||||
def test_pure_hash_read(self):
|
||||
data = b"hello world"
|
||||
bio = io.BytesIO(data)
|
||||
with SHA256FileHashingWrapper(bio, write=False, pure_hash=True) as wrapper:
|
||||
assert wrapper.read() == data
|
||||
assert wrapper.hexdigest() == hashlib.sha256(data).hexdigest()
|
||||
|
||||
def test_impure_hash_write(self):
|
||||
bio = io.BytesIO()
|
||||
data = b"hello world"
|
||||
with SHA256FileHashingWrapper(bio, write=True, pure_hash=False) as wrapper:
|
||||
wrapper.write(data)
|
||||
# pure_hash=False appends the file length ("11" in this case) at exit
|
||||
expected_hash = hashlib.sha256(data + b"11").hexdigest()
|
||||
assert wrapper.hexdigest() == expected_hash
|
||||
|
|
|
|||
Loading…
Reference in a new issue