Skip to content
Snippets Groups Projects
Unverified Commit f75be5a3 authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

swh.model.hashutil: Make unknown variable length algo creation break

Remove the limit on the python3 version, this should be transparent.
If the hash requested is not available, this will raise with an
explanation on the error.

Related T692
parent 8776435d
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,6 @@ import binascii
import functools
import hashlib
import os
import sys
from io import BytesIO
......@@ -77,16 +76,11 @@ def _new_hash(algo, length=None):
raise ValueError('Missing length for git hashing algorithm')
base_algo = algo[:-4]
h = _new_git_hash(base_algo, 'blob', length)
elif ':' in algo: # variable length hashing algorithms (only from
# python3 >= 3.6)
if sys.version_info.major == 3 and sys.version_info.minor >= 6:
_algo = algo.split(':')
base_algo = _algo[0]
variable_length = int(_algo[1])
h = hashlib.new('%s%s' % (base_algo, variable_length))
else:
raise ValueError('Unsupported hashing algorithm %s' % algo)
elif ':' in algo: # variable length hashing algorithms
_algo = algo.split(':')
base_algo = _algo[0]
variable_length = int(_algo[1])
h = hashlib.new('%s%s' % (base_algo, variable_length))
else:
h = hashlib.new(algo)
......
......@@ -8,7 +8,7 @@ import tempfile
import unittest
from nose.tools import istest
from unittest.mock import MagicMock, patch
from unittest.mock import patch
from swh.model import hashutil
......@@ -131,24 +131,11 @@ class Hashutil(unittest.TestCase):
hashutil._new_hash('blake2:10')
except ValueError as e:
self.assertEquals(str(e),
'Unsupported hashing algorithm blake2:10')
'unsupported hash type blake210')
@patch('swh.model.hashutil.sys')
@istest
def new_hash_unexpected_hashing_algo(self, mock_sys):
mock_sys.version_info = MagicMock(major=3, minor=6)
try:
hashutil._new_hash('blake3:256')
except ValueError as e:
self.assertEquals(str(e),
'unsupported hash type blake3256')
@patch('swh.model.hashutil.sys')
@patch('swh.model.hashutil.hashlib')
@istest
def new_hash_blake2b(self, mock_hashlib, mock_sys):
mock_sys.version_info = MagicMock(major=3, minor=6)
def new_hash_blake2b(self, mock_hashlib):
mock_hashlib.new.return_value = 'some-hashlib-object'
h = hashutil._new_hash('blake2b:512')
......@@ -156,17 +143,14 @@ class Hashutil(unittest.TestCase):
self.assertEquals(h, 'some-hashlib-object')
mock_hashlib.new.assert_called_with('blake2b512')
@patch('swh.model.hashutil.sys')
@patch('swh.model.hashutil.hashlib')
@istest
def new_hash_blake2s(self, mock_hashlib, mock_sys):
mock_sys.version_info = MagicMock(major=3, minor=6)
def new_hash_blake2s(self, mock_hashlib):
mock_hashlib.new.return_value = 'some-hashlib-object'
h = hashutil._new_hash('blake2s:256')
self.assertEquals(h, 'some-hashlib-object')
mock_hashlib.blake2s.assert_called_with(digest_size=128)
mock_hashlib.new.assert_called_with('blake2s256')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment