Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_hashutil.py
# Copyright (C) 2015-2018 The Software Heritage developers | # Copyright (C) 2015-2021 The Software Heritage developers | ||||
ardumont: 2021 | |||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import contextlib | |||||
import hashlib | import hashlib | ||||
import io | import io | ||||
import os | import os | ||||
import tempfile | import tempfile | ||||
import unittest | import unittest | ||||
from unittest.mock import patch | from unittest.mock import patch | ||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from swh.model.hashutil import MultiHash | from swh.model.hashutil import MultiHash | ||||
@contextlib.contextmanager | |||||
def patch_blake2(function_name): | |||||
try: | |||||
with patch(function_name) as mock: | |||||
yield mock | |||||
finally: | |||||
# mocking blake2 inserts mock objects in the cache; we need | |||||
# to clean it before the next test runs | |||||
hashutil._blake2_hash_cache.clear() | |||||
class BaseHashutil(unittest.TestCase): | class BaseHashutil(unittest.TestCase): | ||||
def setUp(self): | def setUp(self): | ||||
# Reset function cache | # Reset function cache | ||||
hashutil._blake2_hash_cache = {} | hashutil._blake2_hash_cache = {} | ||||
self.data = b"1984\n" | self.data = b"1984\n" | ||||
self.hex_checksums = { | self.hex_checksums = { | ||||
"sha1": "62be35bf00ff0c624f4a621e2ea5595a049e0731", | "sha1": "62be35bf00ff0c624f4a621e2ea5595a049e0731", | ||||
▲ Show 20 Lines • Show All 165 Lines • ▼ Show 20 Lines | def test_new_hash_blake2b_builtin(self): | ||||
try: | try: | ||||
if "blake2b512" in hashlib.algorithms_available: | if "blake2b512" in hashlib.algorithms_available: | ||||
removed_hash = True | removed_hash = True | ||||
hashlib.algorithms_available.remove("blake2b512") | hashlib.algorithms_available.remove("blake2b512") | ||||
if "blake2b" not in hashlib.algorithms_available: | if "blake2b" not in hashlib.algorithms_available: | ||||
self.skipTest("blake2b not built in") | self.skipTest("blake2b not built in") | ||||
with patch("hashlib.blake2b") as mock_blake2b: | with patch_blake2("hashlib.blake2b") as mock_blake2b: | ||||
mock_blake2b.return_value = sentinel = object() | mock_blake2b.return_value = sentinel = object() | ||||
h = hashutil._new_hash("blake2b512") | h = hashutil._new_hash("blake2b512") | ||||
self.assertIs(h, sentinel) | self.assertIs(h, sentinel) | ||||
mock_blake2b.assert_called_with(digest_size=512 // 8) | mock_blake2b.assert_called_with(digest_size=512 // 8) | ||||
finally: | finally: | ||||
if removed_hash: | if removed_hash: | ||||
hashlib.algorithms_available.add("blake2b512") | hashlib.algorithms_available.add("blake2b512") | ||||
def test_new_hash_blake2s_builtin(self): | def test_new_hash_blake2s_builtin(self): | ||||
removed_hash = False | removed_hash = False | ||||
try: | try: | ||||
if "blake2s256" in hashlib.algorithms_available: | if "blake2s256" in hashlib.algorithms_available: | ||||
removed_hash = True | removed_hash = True | ||||
hashlib.algorithms_available.remove("blake2s256") | hashlib.algorithms_available.remove("blake2s256") | ||||
if "blake2s" not in hashlib.algorithms_available: | if "blake2s" not in hashlib.algorithms_available: | ||||
self.skipTest("blake2s not built in") | self.skipTest("blake2s not built in") | ||||
with patch("hashlib.blake2s") as mock_blake2s: | with patch_blake2("hashlib.blake2s") as mock_blake2s: | ||||
mock_blake2s.return_value = sentinel = object() | mock_blake2s.return_value = sentinel = object() | ||||
h = hashutil._new_hash("blake2s256") | h = hashutil._new_hash("blake2s256") | ||||
self.assertIs(h, sentinel) | self.assertIs(h, sentinel) | ||||
mock_blake2s.assert_called_with(digest_size=256 // 8) | mock_blake2s.assert_called_with(digest_size=256 // 8) | ||||
finally: | finally: | ||||
if removed_hash: | if removed_hash: | ||||
hashlib.algorithms_available.add("blake2s256") | hashlib.algorithms_available.add("blake2s256") | ||||
def test_new_hash_blake2b_pyblake2(self): | def test_new_hash_blake2b_pyblake2(self): | ||||
if "blake2b512" in hashlib.algorithms_available: | if "blake2b512" in hashlib.algorithms_available: | ||||
self.skipTest("blake2b512 built in") | self.skipTest("blake2b512 built in") | ||||
if "blake2b" in hashlib.algorithms_available: | if "blake2b" in hashlib.algorithms_available: | ||||
self.skipTest("blake2b built in") | self.skipTest("blake2b built in") | ||||
with patch("pyblake2.blake2b") as mock_blake2b: | with patch_blake2("pyblake2.blake2b") as mock_blake2b: | ||||
mock_blake2b.return_value = sentinel = object() | mock_blake2b.return_value = sentinel = object() | ||||
h = hashutil._new_hash("blake2b512") | h = hashutil._new_hash("blake2b512") | ||||
self.assertIs(h, sentinel) | self.assertIs(h, sentinel) | ||||
mock_blake2b.assert_called_with(digest_size=512 // 8) | mock_blake2b.assert_called_with(digest_size=512 // 8) | ||||
def test_new_hash_blake2s_pyblake2(self): | def test_new_hash_blake2s_pyblake2(self): | ||||
if "blake2s256" in hashlib.algorithms_available: | if "blake2s256" in hashlib.algorithms_available: | ||||
self.skipTest("blake2s256 built in") | self.skipTest("blake2s256 built in") | ||||
if "blake2s" in hashlib.algorithms_available: | if "blake2s" in hashlib.algorithms_available: | ||||
self.skipTest("blake2s built in") | self.skipTest("blake2s built in") | ||||
with patch("pyblake2.blake2s") as mock_blake2s: | with patch_blake2("pyblake2.blake2s") as mock_blake2s: | ||||
mock_blake2s.return_value = sentinel = object() | mock_blake2s.return_value = sentinel = object() | ||||
h = hashutil._new_hash("blake2s256") | h = hashutil._new_hash("blake2s256") | ||||
self.assertIs(h, sentinel) | self.assertIs(h, sentinel) | ||||
mock_blake2s.assert_called_with(digest_size=256 // 8) | mock_blake2s.assert_called_with(digest_size=256 // 8) | ||||
▲ Show 20 Lines • Show All 80 Lines • Show Last 20 Lines |
2021