From e3960f62df56bd8931f758026738812bce3c45f2 Mon Sep 17 00:00:00 2001 From: David Reid Date: Fri, 1 Nov 2013 14:52:16 -0700 Subject: Strip down the HMAC interface to be HashContext. --- tests/hazmat/primitives/test_hmac.py | 15 ++------------- tests/hazmat/primitives/utils.py | 17 +++++++---------- 2 files changed, 9 insertions(+), 23 deletions(-) (limited to 'tests') diff --git a/tests/hazmat/primitives/test_hmac.py b/tests/hazmat/primitives/test_hmac.py index 42726a7c..43909024 100644 --- a/tests/hazmat/primitives/test_hmac.py +++ b/tests/hazmat/primitives/test_hmac.py @@ -32,26 +32,15 @@ class TestHMAC(object): ) def test_hmac_reject_unicode(self, backend): - h = hmac.HMAC(key=b"mykey", digestmod=hashes.SHA1, backend=backend) + h = hmac.HMAC(b"mykey", 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", digestmod=hashes.SHA1, backend=backend, - msg=b"") - assert isinstance(h.hexdigest(), str) - - def test_hmac_no_digestmod(self): - with pytest.raises(TypeError): - 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", digestmod=hashes.SHA1, backend=pretend_backend, + h = hmac.HMAC(b"key", 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 efc5fbf0..1dfad6e5 100644 --- a/tests/hazmat/primitives/utils.py +++ b/tests/hazmat/primitives/utils.py @@ -129,7 +129,7 @@ def long_string_hash_test(backend, algorithm, md, only_if, skip_message): assert m.finalize() == binascii.unhexlify(md.lower().encode("ascii")) -def generate_hmac_test(param_loader, path, file_names, digestmod, +def generate_hmac_test(param_loader, path, file_names, algorithm, only_if=None, skip_message=None): def test_hmac(self): for backend in _ALL_BACKENDS: @@ -138,7 +138,7 @@ def generate_hmac_test(param_loader, path, file_names, digestmod, yield ( hmac_test, backend, - digestmod, + algorithm, params, only_if, skip_message @@ -146,18 +146,15 @@ def generate_hmac_test(param_loader, path, file_names, digestmod, return test_hmac -def hmac_test(backend, digestmod, params, only_if, skip_message): +def hmac_test(backend, algorithm, 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), digestmod=digestmod) + h = hmac.HMAC(binascii.unhexlify(key), algorithm) h.update(binascii.unhexlify(msg)) - assert h.hexdigest() == md - digest = hmac.HMAC(binascii.unhexlify(key), digestmod=digestmod, - msg=binascii.unhexlify(msg)).hexdigest() - assert digest == md + assert h.finalize() == binascii.unhexlify(md) def generate_base_hmac_test(hash_cls, only_if=None, skip_message=None): @@ -173,11 +170,11 @@ def generate_base_hmac_test(hash_cls, only_if=None, skip_message=None): return test_base_hmac -def base_hmac_test(backend, digestmod, only_if, skip_message): +def base_hmac_test(backend, algorithm, 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), digestmod=digestmod) + h = hmac.HMAC(binascii.unhexlify(key), algorithm) h_copy = h.copy() assert h != h_copy assert h._ctx != h_copy._ctx -- cgit v1.2.3 From b864db11a3577ff6b117f6ce73e43f3c1e7b3e84 Mon Sep 17 00:00:00 2001 From: David Reid Date: Fri, 1 Nov 2013 15:11:45 -0700 Subject: Enforce HMAC taking an instance of HashAlgorithm --- tests/hazmat/primitives/test_hmac.py | 10 +++++++--- tests/hazmat/primitives/test_hmac_vectors.py | 14 +++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/tests/hazmat/primitives/test_hmac.py b/tests/hazmat/primitives/test_hmac.py index 43909024..bbaabb22 100644 --- a/tests/hazmat/primitives/test_hmac.py +++ b/tests/hazmat/primitives/test_hmac.py @@ -26,13 +26,13 @@ from .utils import generate_base_hmac_test class TestHMAC(object): test_copy = generate_base_hmac_test( - hashes.MD5, + hashes.MD5(), only_if=lambda backend: backend.hashes.supported(hashes.MD5), skip_message="Does not support MD5", ) def test_hmac_reject_unicode(self, backend): - h = hmac.HMAC(b"mykey", hashes.SHA1, backend=backend) + h = hmac.HMAC(b"mykey", hashes.SHA1(), backend=backend) with pytest.raises(TypeError): h.update(six.u("\u00FC")) @@ -40,7 +40,11 @@ class TestHMAC(object): 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", hashes.SHA1(), backend=pretend_backend, ctx=pretend_ctx) assert h._backend is pretend_backend assert h.copy()._backend is pretend_backend + + def test_hmac_algorithm_instance(self): + with pytest.raises(TypeError): + hmac.HMAC(hashes.SHA1) diff --git a/tests/hazmat/primitives/test_hmac_vectors.py b/tests/hazmat/primitives/test_hmac_vectors.py index 27b45012..52d592b6 100644 --- a/tests/hazmat/primitives/test_hmac_vectors.py +++ b/tests/hazmat/primitives/test_hmac_vectors.py @@ -26,7 +26,7 @@ class TestHMAC_MD5(object): [ "rfc-2202-md5.txt", ], - hashes.MD5, + hashes.MD5(), only_if=lambda backend: backend.hashes.supported(hashes.MD5), skip_message="Does not support MD5", ) @@ -39,7 +39,7 @@ class TestHMAC_SHA1(object): [ "rfc-2202-sha1.txt", ], - hashes.SHA1, + hashes.SHA1(), only_if=lambda backend: backend.hashes.supported(hashes.SHA1), skip_message="Does not support SHA1", ) @@ -52,7 +52,7 @@ class TestHMAC_SHA224(object): [ "rfc-4231-sha224.txt", ], - hashes.SHA224, + hashes.SHA224(), only_if=lambda backend: backend.hashes.supported(hashes.SHA224), skip_message="Does not support SHA224", ) @@ -65,7 +65,7 @@ class TestHMAC_SHA256(object): [ "rfc-4231-sha256.txt", ], - hashes.SHA256, + hashes.SHA256(), only_if=lambda backend: backend.hashes.supported(hashes.SHA256), skip_message="Does not support SHA256", ) @@ -78,7 +78,7 @@ class TestHMAC_SHA384(object): [ "rfc-4231-sha384.txt", ], - hashes.SHA384, + hashes.SHA384(), only_if=lambda backend: backend.hashes.supported(hashes.SHA384), skip_message="Does not support SHA384", ) @@ -91,7 +91,7 @@ class TestHMAC_SHA512(object): [ "rfc-4231-sha512.txt", ], - hashes.SHA512, + hashes.SHA512(), only_if=lambda backend: backend.hashes.supported(hashes.SHA512), skip_message="Does not support SHA512", ) @@ -104,7 +104,7 @@ class TestHMAC_RIPEMD160(object): [ "rfc-2286-ripemd160.txt", ], - hashes.RIPEMD160, + hashes.RIPEMD160(), only_if=lambda backend: backend.hashes.supported(hashes.RIPEMD160), skip_message="Does not support RIPEMD160", ) -- cgit v1.2.3 From 87744771e7ef142bf97b46c399421a504a4ce4ad Mon Sep 17 00:00:00 2001 From: David Reid Date: Fri, 1 Nov 2013 15:50:39 -0700 Subject: TypeErrors are pretty cool, there are lot of reasons you get them. --- tests/hazmat/primitives/test_hmac.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/hazmat/primitives/test_hmac.py b/tests/hazmat/primitives/test_hmac.py index bbaabb22..a44838cf 100644 --- a/tests/hazmat/primitives/test_hmac.py +++ b/tests/hazmat/primitives/test_hmac.py @@ -47,4 +47,4 @@ class TestHMAC(object): def test_hmac_algorithm_instance(self): with pytest.raises(TypeError): - hmac.HMAC(hashes.SHA1) + hmac.HMAC(b"key", hashes.SHA1) -- cgit v1.2.3 From 753ae19feaf33e4328a707d5588797c2a800c0c7 Mon Sep 17 00:00:00 2001 From: David Reid Date: Fri, 1 Nov 2013 16:28:41 -0700 Subject: Encode Reasons. --- tests/hazmat/primitives/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/hazmat/primitives/utils.py b/tests/hazmat/primitives/utils.py index 1dfad6e5..b4a8a3e6 100644 --- a/tests/hazmat/primitives/utils.py +++ b/tests/hazmat/primitives/utils.py @@ -154,7 +154,7 @@ def hmac_test(backend, algorithm, params, only_if, skip_message): key = params[2] h = hmac.HMAC(binascii.unhexlify(key), algorithm) h.update(binascii.unhexlify(msg)) - assert h.finalize() == binascii.unhexlify(md) + assert h.finalize() == binascii.unhexlify(md.encode("ascii")) def generate_base_hmac_test(hash_cls, only_if=None, skip_message=None): -- cgit v1.2.3