From 2824ab72d30e8423d17496e2c3baa47106505c8c Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Mon, 28 Oct 2013 11:06:55 -0500 Subject: make hmac (mostly) compatible with stdlib hmac --- tests/hazmat/primitives/test_hmac.py | 12 ++++++++---- tests/hazmat/primitives/utils.py | 20 ++++++++++---------- 2 files changed, 18 insertions(+), 14 deletions(-) (limited to 'tests/hazmat') diff --git a/tests/hazmat/primitives/test_hmac.py b/tests/hazmat/primitives/test_hmac.py index e2b517ae..81d9ac86 100644 --- a/tests/hazmat/primitives/test_hmac.py +++ b/tests/hazmat/primitives/test_hmac.py @@ -32,22 +32,26 @@ class TestHMAC(object): ) def test_hmac_reject_unicode(self, backend): - h = hmac.HMAC(key=b"mykey", hash_cls=hashes.SHA1, backend=backend) + h = hmac.HMAC(key=b"mykey", digestmod=hashes.SHA1, backend=backend) with pytest.raises(TypeError): h.update(six.u("\u00FC")) def test_base_hash_hexdigest_string_type(self, backend): - h = hmac.HMAC(key=b"mykey", hash_cls=hashes.SHA1, backend=backend, - data=b"") + h = hmac.HMAC(key=b"mykey", digestmod=hashes.SHA1, backend=backend, + msg=b"") assert isinstance(h.hexdigest(), str) + def test_hmac_no_digestmod(self): + with pytest.raises(ValueError): + hmac.HMAC(key=b"shortkey") + class TestCopyHMAC(object): def test_copy_backend_object(self): pretend_hmac = pretend.stub(copy_ctx=lambda a: True) pretend_backend = pretend.stub(hmacs=pretend_hmac) pretend_ctx = pretend.stub() - h = hmac.HMAC(b"key", hashes.SHA1, backend=pretend_backend, + h = hmac.HMAC(b"key", digestmod=hashes.SHA1, backend=pretend_backend, ctx=pretend_ctx) assert h._backend is pretend_backend assert h.copy()._backend is pretend_backend diff --git a/tests/hazmat/primitives/utils.py b/tests/hazmat/primitives/utils.py index 73a2469a..c51fef52 100644 --- a/tests/hazmat/primitives/utils.py +++ b/tests/hazmat/primitives/utils.py @@ -93,11 +93,11 @@ def generate_base_hash_test(hash_cls, digest_size, block_size, return test_base_hash -def base_hash_test(backend, hash_cls, digest_size, block_size, only_if, +def base_hash_test(backend, digestmod, digest_size, block_size, only_if, skip_message): if only_if is not None and not only_if(backend): pytest.skip(skip_message) - m = hash_cls(backend=backend) + m = digestmod(backend=backend) assert m.digest_size == digest_size assert m.block_size == block_size m_copy = m.copy() @@ -128,7 +128,7 @@ def long_string_hash_test(backend, hash_factory, md, only_if, skip_message): assert m.hexdigest() == md.lower() -def generate_hmac_test(param_loader, path, file_names, hash_cls, +def generate_hmac_test(param_loader, path, file_names, digestmod, only_if=None, skip_message=None): def test_hmac(self): for backend in _ALL_BACKENDS: @@ -137,7 +137,7 @@ def generate_hmac_test(param_loader, path, file_names, hash_cls, yield ( hmac_test, backend, - hash_cls, + digestmod, params, only_if, skip_message @@ -145,17 +145,17 @@ def generate_hmac_test(param_loader, path, file_names, hash_cls, return test_hmac -def hmac_test(backend, hash_cls, params, only_if, skip_message): +def hmac_test(backend, digestmod, params, only_if, skip_message): if only_if is not None and not only_if(backend): pytest.skip(skip_message) msg = params[0] md = params[1] key = params[2] - h = hmac.HMAC(binascii.unhexlify(key), hash_cls) + h = hmac.HMAC(binascii.unhexlify(key), digestmod=digestmod) h.update(binascii.unhexlify(msg)) assert h.hexdigest() == md - digest = hmac.HMAC(binascii.unhexlify(key), hash_cls, - data=binascii.unhexlify(msg)).hexdigest() + digest = hmac.HMAC(binascii.unhexlify(key), digestmod=digestmod, + msg=binascii.unhexlify(msg)).hexdigest() assert digest == md @@ -172,11 +172,11 @@ def generate_base_hmac_test(hash_cls, only_if=None, skip_message=None): return test_base_hmac -def base_hmac_test(backend, hash_cls, only_if, skip_message): +def base_hmac_test(backend, digestmod, only_if, skip_message): if only_if is not None and not only_if(backend): pytest.skip(skip_message) key = b"ab" - h = hmac.HMAC(binascii.unhexlify(key), hash_cls) + h = hmac.HMAC(binascii.unhexlify(key), digestmod=digestmod) h_copy = h.copy() assert h != h_copy assert h._ctx != h_copy._ctx -- cgit v1.2.3