mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-25 10:58:52 -06:00
refactor: one digest loop for files and streams
This commit is contained in:
1 parent
88581cf95d
commit
2c8a9cfad8
11 files changed
+88
-98
No files matched your search
+42
-1
@@ -1,12 +1,16 @@
|
||||
"""compute_hashes reads a file once per process and change."""
|
||||
"""compute_hashes reads a file once per process and change, and one loop digests files and streams."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import tests
|
||||
|
||||
import hashlib
|
||||
import io
|
||||
import shutil
|
||||
import tempfile
|
||||
import unittest
|
||||
import zipfile
|
||||
import zlib
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
@@ -41,5 +45,42 @@ class HashesAreReadOncePerContent(unittest.TestCase):
|
||||
self.assertNotEqual(hashing.compute_hashes(self.file)["sha1"], first["sha1"])
|
||||
|
||||
|
||||
class OneDigestLoopForFilesAndStreams(unittest.TestCase):
|
||||
"""A ZIP member, a file and an open stream are hashed by the same loop,
|
||||
so the pack check, the PR review and the recipes agree with the resolver."""
|
||||
|
||||
DATA = bytes(range(256)) * 600 # over two 64 KiB reads
|
||||
|
||||
def setUp(self):
|
||||
self.dir = Path(tempfile.mkdtemp(dir=REPO_ROOT / "tmp"))
|
||||
self.addCleanup(shutil.rmtree, self.dir, True)
|
||||
self.file = self.dir / "rom.bin"
|
||||
self.file.write_bytes(self.DATA)
|
||||
|
||||
def test_a_stream_gives_the_file_digests(self):
|
||||
expected = {
|
||||
"sha1": hashlib.sha1(self.DATA).hexdigest(),
|
||||
"md5": hashlib.md5(self.DATA).hexdigest(),
|
||||
"sha256": hashlib.sha256(self.DATA).hexdigest(),
|
||||
"crc32": format(zlib.crc32(self.DATA) & 0xFFFFFFFF, "08x"),
|
||||
"adler32": format(zlib.adler32(self.DATA) & 0xFFFFFFFF, "08x"),
|
||||
}
|
||||
self.assertEqual(hashing.compute_hashes(self.file, cache=False), expected)
|
||||
with open(self.file, "rb") as handle:
|
||||
self.assertEqual(hashing.stream_hashes(handle, frozenset(expected)), expected)
|
||||
self.assertEqual(hashing.stream_hashes(io.BytesIO(self.DATA), frozenset({"crc32"})),
|
||||
{"crc32": expected["crc32"]})
|
||||
|
||||
def test_a_zip_member_and_md5sum(self):
|
||||
archive = self.dir / "set.zip"
|
||||
with zipfile.ZipFile(archive, "w", zipfile.ZIP_DEFLATED) as zf:
|
||||
zf.writestr("rom.bin", self.DATA)
|
||||
with zipfile.ZipFile(archive) as zf, zf.open("rom.bin") as member:
|
||||
sha1 = hashing.stream_hashes(member, frozenset({"sha1"}))["sha1"]
|
||||
self.assertEqual(sha1, hashing.file_digest(self.file))
|
||||
self.assertEqual(hashing.md5sum(self.file), hashlib.md5(self.DATA).hexdigest())
|
||||
self.assertEqual(hashing.md5sum(io.BytesIO(self.DATA)), hashlib.md5(self.DATA).hexdigest())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user