Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_hashutil.py
# Copyright (C) 2015-2017 The Software Heritage developers | # Copyright (C) 2015-2017 The Software Heritage developers | ||||
# 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 hashlib | |||||
import io | import io | ||||
import os | import os | ||||
import tempfile | import tempfile | ||||
import unittest | import unittest | ||||
from nose.tools import istest | from nose.tools import istest | ||||
from unittest.mock import patch | from unittest.mock import patch | ||||
from swh.model import hashutil | from swh.model import hashutil | ||||
class Hashutil(unittest.TestCase): | class Hashutil(unittest.TestCase): | ||||
def setUp(self): | def setUp(self): | ||||
# Reset function 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', | ||||
'sha1_git': '568aaf43d83b2c3df8067f3bedbb97d83260be6d', | 'sha1_git': '568aaf43d83b2c3df8067f3bedbb97d83260be6d', | ||||
'sha256': '26602113b4b9afd9d55466b08580d3c2' | 'sha256': '26602113b4b9afd9d55466b08580d3c2' | ||||
'4a9b50ee5b5866c0d91fab0e65907311', | '4a9b50ee5b5866c0d91fab0e65907311', | ||||
'blake2s256': '63cfb259e1fdb485bc5c55749697a6b21ef31fb7445f6c78a' | 'blake2s256': '63cfb259e1fdb485bc5c55749697a6b21ef31fb7445f6c78a' | ||||
'c9422f9f2dc8906', | 'c9422f9f2dc8906', | ||||
▲ Show 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | def new_hash_unsupported_hashing_algorithm(self): | ||||
try: | try: | ||||
hashutil._new_hash('blake2:10') | hashutil._new_hash('blake2:10') | ||||
except ValueError as e: | except ValueError as e: | ||||
self.assertEquals(str(e), | self.assertEquals(str(e), | ||||
'Unexpected hashing algorithm blake2:10, ' | 'Unexpected hashing algorithm blake2:10, ' | ||||
'expected one of blake2b512, blake2s256, ' | 'expected one of blake2b512, blake2s256, ' | ||||
'sha1, sha1_git, sha256') | 'sha1, sha1_git, sha256') | ||||
@patch('swh.model.hashutil.hashlib') | @patch('hashlib.new') | ||||
@istest | @istest | ||||
def new_hash_blake2b(self, mock_hashlib): | def new_hash_blake2b_blake2b512_builtin(self, mock_hashlib_new): | ||||
mock_hashlib.new.return_value = 'some-hashlib-object' | if 'blake2b512' not in hashlib.algorithms_available: | ||||
self.skipTest('blake2b512 not built-in') | |||||
mock_hashlib_new.return_value = sentinel = object() | |||||
h = hashutil._new_hash('blake2b512') | h = hashutil._new_hash('blake2b512') | ||||
self.assertEquals(h, 'some-hashlib-object') | self.assertIs(h, sentinel) | ||||
mock_hashlib.new.assert_called_with('blake2b512') | mock_hashlib_new.assert_called_with('blake2b512') | ||||
@patch('hashlib.new') | |||||
@istest | |||||
def new_hash_blake2s_blake2s256_builtin(self, mock_hashlib_new): | |||||
if 'blake2s256' not in hashlib.algorithms_available: | |||||
self.skipTest('blake2s256 not built-in') | |||||
mock_hashlib_new.return_value = sentinel = object() | |||||
h = hashutil._new_hash('blake2s256') | |||||
self.assertIs(h, sentinel) | |||||
mock_hashlib_new.assert_called_with('blake2s256') | |||||
@patch('swh.model.hashutil.hashlib') | |||||
@istest | @istest | ||||
def new_hash_blake2s(self, mock_hashlib): | def new_hash_blake2b_builtin(self): | ||||
mock_hashlib.new.return_value = 'some-hashlib-object' | removed_hash = False | ||||
try: | |||||
if 'blake2b512' in hashlib.algorithms_available: | |||||
removed_hash = True | |||||
hashlib.algorithms_available.remove('blake2b512') | |||||
if 'blake2b' not in hashlib.algorithms_available: | |||||
self.skipTest('blake2b not built in') | |||||
with patch('hashlib.blake2b') as mock_blake2b: | |||||
mock_blake2b.return_value = sentinel = object() | |||||
h = hashutil._new_hash('blake2b512') | |||||
self.assertIs(h, sentinel) | |||||
mock_blake2b.assert_called_with(digest_size=512//8) | |||||
finally: | |||||
if removed_hash: | |||||
hashlib.algorithms_available.add('blake2b512') | |||||
@istest | |||||
def new_hash_blake2s_builtin(self): | |||||
removed_hash = False | |||||
try: | |||||
if 'blake2s256' in hashlib.algorithms_available: | |||||
removed_hash = True | |||||
hashlib.algorithms_available.remove('blake2s256') | |||||
if 'blake2s' not in hashlib.algorithms_available: | |||||
self.skipTest('blake2s not built in') | |||||
with patch('hashlib.blake2s') as mock_blake2s: | |||||
mock_blake2s.return_value = sentinel = object() | |||||
h = hashutil._new_hash('blake2s256') | |||||
self.assertIs(h, sentinel) | |||||
mock_blake2s.assert_called_with(digest_size=256//8) | |||||
finally: | |||||
if removed_hash: | |||||
hashlib.algorithms_available.add('blake2s256') | |||||
@istest | |||||
def new_hash_blake2b_pyblake2(self): | |||||
if 'blake2b512' in hashlib.algorithms_available: | |||||
self.skipTest('blake2b512 built in') | |||||
if 'blake2b' in hashlib.algorithms_available: | |||||
self.skipTest('blake2b built in') | |||||
with patch('pyblake2.blake2b') as mock_blake2b: | |||||
mock_blake2b.return_value = sentinel = object() | |||||
h = hashutil._new_hash('blake2b512') | |||||
self.assertIs(h, sentinel) | |||||
mock_blake2b.assert_called_with(digest_size=512//8) | |||||
@istest | |||||
def new_hash_blake2s_pyblake2(self): | |||||
if 'blake2s256' in hashlib.algorithms_available: | |||||
self.skipTest('blake2s256 built in') | |||||
if 'blake2s' in hashlib.algorithms_available: | |||||
self.skipTest('blake2s built in') | |||||
with patch('pyblake2.blake2s') as mock_blake2s: | |||||
mock_blake2s.return_value = sentinel = object() | |||||
h = hashutil._new_hash('blake2s256') | h = hashutil._new_hash('blake2s256') | ||||
self.assertEquals(h, 'some-hashlib-object') | self.assertIs(h, sentinel) | ||||
mock_hashlib.new.assert_called_with('blake2s256') | mock_blake2s.assert_called_with(digest_size=256//8) | ||||
class HashlibGit(unittest.TestCase): | class HashlibGit(unittest.TestCase): | ||||
def setUp(self): | def setUp(self): | ||||
self.blob_data = b'42\n' | self.blob_data = b'42\n' | ||||
self.tree_data = b''.join([b'40000 barfoo\0', | self.tree_data = b''.join([b'40000 barfoo\0', | ||||
▲ Show 20 Lines • Show All 77 Lines • Show Last 20 Lines |