diff options
author | Alex Stapleton <alexs@prol.etari.at> | 2014-03-26 17:39:29 +0000 |
---|---|---|
committer | Alex Stapleton <alexs@prol.etari.at> | 2014-03-27 07:31:12 +0000 |
commit | 112963e296aadfdeaa4e2624c3b81b6b8c726a06 (patch) | |
tree | cd3c462d09860a8c01ad756acf60acb89b23fd1b | |
parent | 7a489dbd116edd4ca5a6104b74748f3a4f712d15 (diff) | |
download | cryptography-112963e296aadfdeaa4e2624c3b81b6b8c726a06.tar.gz cryptography-112963e296aadfdeaa4e2624c3b81b6b8c726a06.tar.bz2 cryptography-112963e296aadfdeaa4e2624c3b81b6b8c726a06.zip |
Address most of my own comments
-rw-r--r-- | cryptography/exceptions.py | 4 | ||||
-rw-r--r-- | cryptography/hazmat/primitives/twofactor/hotp.py | 6 | ||||
-rw-r--r-- | cryptography/hazmat/primitives/twofactor/totp.py | 6 | ||||
-rw-r--r-- | tests/hazmat/primitives/twofactor/test_hotp.py | 6 | ||||
-rw-r--r-- | tests/hazmat/primitives/twofactor/test_totp.py | 6 | ||||
-rw-r--r-- | tests/test_utils.py | 33 | ||||
-rw-r--r-- | tests/utils.py | 6 |
7 files changed, 51 insertions, 16 deletions
diff --git a/cryptography/exceptions.py b/cryptography/exceptions.py index d97f20cc..8825d3b4 100644 --- a/cryptography/exceptions.py +++ b/cryptography/exceptions.py @@ -14,8 +14,8 @@ from __future__ import absolute_import, division, print_function -class _Causes(object): - BACKEND_MISSING_INTERFACE = 0 +class _Reasons(object): + BACKEND_MISSING_INTERFACE = object() class UnsupportedAlgorithm(Exception): diff --git a/cryptography/hazmat/primitives/twofactor/hotp.py b/cryptography/hazmat/primitives/twofactor/hotp.py index bac23d1b..41c467c8 100644 --- a/cryptography/hazmat/primitives/twofactor/hotp.py +++ b/cryptography/hazmat/primitives/twofactor/hotp.py @@ -17,7 +17,9 @@ import struct import six -from cryptography.exceptions import InvalidToken, UnsupportedAlgorithm, _Causes +from cryptography.exceptions import ( + InvalidToken, UnsupportedAlgorithm, _Reasons +) from cryptography.hazmat.backends.interfaces import HMACBackend from cryptography.hazmat.primitives import constant_time, hmac from cryptography.hazmat.primitives.hashes import SHA1, SHA256, SHA512 @@ -28,7 +30,7 @@ class HOTP(object): if not isinstance(backend, HMACBackend): raise UnsupportedAlgorithm( "Backend object does not implement HMACBackend", - _Causes.BACKEND_MISSING_INTERFACE + _Reasons.BACKEND_MISSING_INTERFACE ) if len(key) < 16: diff --git a/cryptography/hazmat/primitives/twofactor/totp.py b/cryptography/hazmat/primitives/twofactor/totp.py index d0162395..e55ba00d 100644 --- a/cryptography/hazmat/primitives/twofactor/totp.py +++ b/cryptography/hazmat/primitives/twofactor/totp.py @@ -13,7 +13,9 @@ from __future__ import absolute_import, division, print_function -from cryptography.exceptions import InvalidToken, UnsupportedAlgorithm, _Causes +from cryptography.exceptions import ( + InvalidToken, UnsupportedAlgorithm, _Reasons +) from cryptography.hazmat.backends.interfaces import HMACBackend from cryptography.hazmat.primitives import constant_time from cryptography.hazmat.primitives.twofactor.hotp import HOTP @@ -24,7 +26,7 @@ class TOTP(object): if not isinstance(backend, HMACBackend): raise UnsupportedAlgorithm( "Backend object does not implement HMACBackend", - _Causes.BACKEND_MISSING_INTERFACE + _Reasons.BACKEND_MISSING_INTERFACE ) self._time_step = time_step diff --git a/tests/hazmat/primitives/twofactor/test_hotp.py b/tests/hazmat/primitives/twofactor/test_hotp.py index b2b2677c..803f96f3 100644 --- a/tests/hazmat/primitives/twofactor/test_hotp.py +++ b/tests/hazmat/primitives/twofactor/test_hotp.py @@ -17,13 +17,13 @@ import os import pytest -from cryptography.exceptions import InvalidToken, _Causes +from cryptography.exceptions import InvalidToken, _Reasons from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.hashes import MD5, SHA1 from cryptography.hazmat.primitives.twofactor.hotp import HOTP from ....utils import ( - load_nist_vectors, load_vectors_from_file, raises_unsupported + load_nist_vectors, load_vectors_from_file, raises_unsupported_algorithm ) vectors = load_vectors_from_file( @@ -105,5 +105,5 @@ def test_invalid_backend(): pretend_backend = object() - with raises_unsupported(_Causes.BACKEND_MISSING_INTERFACE): + with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE): HOTP(secret, 8, hashes.SHA1(), pretend_backend) diff --git a/tests/hazmat/primitives/twofactor/test_totp.py b/tests/hazmat/primitives/twofactor/test_totp.py index 208b0eea..518d3ce8 100644 --- a/tests/hazmat/primitives/twofactor/test_totp.py +++ b/tests/hazmat/primitives/twofactor/test_totp.py @@ -15,12 +15,12 @@ from __future__ import absolute_import, division, print_function import pytest -from cryptography.exceptions import InvalidToken, _Causes +from cryptography.exceptions import InvalidToken, _Reasons from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.twofactor.totp import TOTP from ....utils import ( - load_nist_vectors, load_vectors_from_file, raises_unsupported + load_nist_vectors, load_vectors_from_file, raises_unsupported_algorithm ) vectors = load_vectors_from_file( @@ -139,5 +139,5 @@ def test_invalid_backend(): pretend_backend = object() - with raises_unsupported(_Causes.BACKEND_MISSING_INTERFACE): + with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE): TOTP(secret, 8, hashes.SHA1(), 30, pretend_backend) diff --git a/tests/test_utils.py b/tests/test_utils.py index b63f1bab..a8046dc3 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -21,13 +21,15 @@ import pretend import pytest import cryptography +from cryptography.exceptions import UnsupportedAlgorithm, _Reasons + import cryptography_vectors from .utils import ( check_backend_support, check_for_iface, load_cryptrec_vectors, load_fips_dsa_key_pair_vectors, load_hash_vectors, load_nist_vectors, load_pkcs1_vectors, load_rsa_nist_vectors, load_vectors_from_file, - select_backends + raises_unsupported_algorithm, select_backends ) @@ -1608,3 +1610,32 @@ de61329a78d526f65245380ce877e979c5b50de66c9c30d66382c8f254653d25a1eb1d3a4897d7\ def test_vector_version(): assert cryptography.__version__ == cryptography_vectors.__version__ + + +def test_raises_unsupported_algorithm_wrong_type(): + # Check that it asserts if the wrong type of exception is raised. + + class TestException(Exception): + pass + + with pytest.raises(TestException): + with raises_unsupported_algorithm(None): + raise TestException + + +def test_raises_unsupported_algorithm_wrong_reason(): + # Check that it asserts if the wrong reason code is raised. + with pytest.raises(AssertionError): + with raises_unsupported_algorithm(None): + raise UnsupportedAlgorithm("An error.", + _Reasons.BACKEND_MISSING_INTERFACE) + + +def test_raises_unsupported_algorithm(): + # Check that it doesnt assert if the right things are raised. + with raises_unsupported_algorithm( + _Reasons.BACKEND_MISSING_INTERFACE + ) as exc: + raise UnsupportedAlgorithm("An error.", + _Reasons.BACKEND_MISSING_INTERFACE) + assert exc diff --git a/tests/utils.py b/tests/utils.py index fbe448fe..f948642e 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -70,10 +70,10 @@ def check_backend_support(item): @contextmanager -def raises_unsupported(cause): +def raises_unsupported_algorithm(cause): with pytest.raises(UnsupportedAlgorithm) as exc_info: - yield - assert exc_info.value._cause == cause + yield exc_info + assert exc_info.value._cause is cause def load_vectors_from_file(filename, loader): |