From 6f386b9019f2d6ca8e6ff01a3d0433c9a371732f Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Tue, 28 Oct 2014 09:16:13 -0700 Subject: When using a test double for backends, always use one which really implements the interface --- tests/hazmat/primitives/test_cmac.py | 16 ++++++---------- tests/hazmat/primitives/test_hashes.py | 16 +++++----------- tests/hazmat/primitives/test_hmac.py | 14 +++++--------- 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/tests/hazmat/primitives/test_cmac.py b/tests/hazmat/primitives/test_cmac.py index 49e2043e..c778ebee 100644 --- a/tests/hazmat/primitives/test_cmac.py +++ b/tests/hazmat/primitives/test_cmac.py @@ -21,7 +21,6 @@ import pytest import six -from cryptography import utils from cryptography.exceptions import ( AlreadyFinalized, InvalidSignature, _Reasons ) @@ -31,7 +30,8 @@ from cryptography.hazmat.primitives.ciphers.algorithms import ( ) from cryptography.hazmat.primitives.cmac import CMAC -from tests.utils import ( +from ..backends.test_multibackend import DummyCMACBackend +from ...utils import ( load_nist_vectors, load_vectors_from_file, raises_unsupported_algorithm ) @@ -195,18 +195,14 @@ class TestCMAC(object): def test_copy(): - @utils.register_interface(CMACBackend) - class PretendBackend(object): - pass - - pretend_backend = PretendBackend() + backend = DummyCMACBackend([AES]) copied_ctx = pretend.stub() pretend_ctx = pretend.stub(copy=lambda: copied_ctx) key = b"2b7e151628aed2a6abf7158809cf4f3c" - cmac = CMAC(AES(key), backend=pretend_backend, ctx=pretend_ctx) + cmac = CMAC(AES(key), backend=backend, ctx=pretend_ctx) - assert cmac._backend is pretend_backend - assert cmac.copy()._backend is pretend_backend + assert cmac._backend is backend + assert cmac.copy()._backend is backend def test_invalid_backend(): diff --git a/tests/hazmat/primitives/test_hashes.py b/tests/hazmat/primitives/test_hashes.py index 0fdd7550..053a1a46 100644 --- a/tests/hazmat/primitives/test_hashes.py +++ b/tests/hazmat/primitives/test_hashes.py @@ -20,13 +20,12 @@ import pytest import six from cryptography import utils -from cryptography.exceptions import ( - AlreadyFinalized, _Reasons -) +from cryptography.exceptions import AlreadyFinalized, _Reasons from cryptography.hazmat.backends.interfaces import HashBackend from cryptography.hazmat.primitives import hashes, interfaces from .utils import generate_base_hash_test +from ..backends.test_multibackend import DummyHashBackend from ...utils import raises_unsupported_algorithm @@ -45,16 +44,11 @@ class TestHashContext(object): m.update(six.u("\u00FC")) def test_copy_backend_object(self): - @utils.register_interface(HashBackend) - class PretendBackend(object): - pass - - pretend_backend = PretendBackend() + backend = DummyHashBackend([hashes.SHA1]) copied_ctx = pretend.stub() pretend_ctx = pretend.stub(copy=lambda: copied_ctx) - h = hashes.Hash(hashes.SHA1(), backend=pretend_backend, - ctx=pretend_ctx) - assert h._backend is pretend_backend + h = hashes.Hash(hashes.SHA1(), backend=backend, ctx=pretend_ctx) + assert h._backend is backend assert h.copy()._backend is h._backend def test_hash_algorithm_instance(self, backend): diff --git a/tests/hazmat/primitives/test_hmac.py b/tests/hazmat/primitives/test_hmac.py index 3553632c..497b37e1 100644 --- a/tests/hazmat/primitives/test_hmac.py +++ b/tests/hazmat/primitives/test_hmac.py @@ -27,6 +27,7 @@ from cryptography.hazmat.backends.interfaces import HMACBackend from cryptography.hazmat.primitives import hashes, hmac, interfaces from .utils import generate_base_hmac_test +from ..backends.test_multibackend import DummyHMACBackend from ...utils import raises_unsupported_algorithm @@ -56,17 +57,12 @@ class TestHMAC(object): h.update(six.u("\u00FC")) def test_copy_backend_object(self): - @utils.register_interface(HMACBackend) - class PretendBackend(object): - pass - - pretend_backend = PretendBackend() + backend = DummyHMACBackend([hashes.SHA1]) copied_ctx = pretend.stub() pretend_ctx = pretend.stub(copy=lambda: copied_ctx) - 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 + h = hmac.HMAC(b"key", hashes.SHA1(), backend=backend, ctx=pretend_ctx) + assert h._backend is backend + assert h.copy()._backend is backend def test_hmac_algorithm_instance(self, backend): with pytest.raises(TypeError): -- cgit v1.2.3