From 7a489dbd116edd4ca5a6104b74748f3a4f712d15 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 22 Mar 2014 15:09:34 -0700 Subject: Fixed #809 -- switch back to always using UnsupportedAlgorithm --- tests/hazmat/backends/test_commoncrypto.py | 4 +-- tests/hazmat/backends/test_multibackend.py | 24 +++++++++--------- tests/hazmat/backends/test_openssl.py | 12 ++++----- tests/hazmat/primitives/test_block.py | 6 ++--- tests/hazmat/primitives/test_ciphers.py | 4 +-- tests/hazmat/primitives/test_hashes.py | 8 +++--- tests/hazmat/primitives/test_hkdf.py | 4 +-- tests/hazmat/primitives/test_hmac.py | 6 ++--- tests/hazmat/primitives/test_pbkdf2hmac.py | 6 ++--- tests/hazmat/primitives/test_rsa.py | 34 +++++++++++++------------- tests/hazmat/primitives/twofactor/test_hotp.py | 8 +++--- tests/hazmat/primitives/twofactor/test_totp.py | 8 +++--- tests/utils.py | 9 +++++++ 13 files changed, 70 insertions(+), 63 deletions(-) (limited to 'tests') diff --git a/tests/hazmat/backends/test_commoncrypto.py b/tests/hazmat/backends/test_commoncrypto.py index 72ed61c0..dc6c8c5b 100644 --- a/tests/hazmat/backends/test_commoncrypto.py +++ b/tests/hazmat/backends/test_commoncrypto.py @@ -16,7 +16,7 @@ from __future__ import absolute_import, division, print_function import pytest from cryptography import utils -from cryptography.exceptions import InternalError, UnsupportedCipher +from cryptography.exceptions import InternalError, UnsupportedAlgorithm from cryptography.hazmat.bindings.commoncrypto.binding import Binding from cryptography.hazmat.primitives import interfaces from cryptography.hazmat.primitives.ciphers.algorithms import AES @@ -63,5 +63,5 @@ class TestCommonCrypto(object): cipher = Cipher( DummyCipher(), GCM(b"fake_iv_here"), backend=b, ) - with pytest.raises(UnsupportedCipher): + with pytest.raises(UnsupportedAlgorithm): cipher.encryptor() diff --git a/tests/hazmat/backends/test_multibackend.py b/tests/hazmat/backends/test_multibackend.py index c5c0d82a..7ab9e446 100644 --- a/tests/hazmat/backends/test_multibackend.py +++ b/tests/hazmat/backends/test_multibackend.py @@ -16,9 +16,7 @@ from __future__ import absolute_import, division, print_function import pytest from cryptography import utils -from cryptography.exceptions import ( - UnsupportedAlgorithm, UnsupportedCipher, UnsupportedHash -) +from cryptography.exceptions import UnsupportedAlgorithm from cryptography.hazmat.backends.interfaces import ( CipherBackend, HMACBackend, HashBackend, PBKDF2HMACBackend, RSABackend ) @@ -38,11 +36,11 @@ class DummyCipherBackend(object): def create_symmetric_encryption_ctx(self, algorithm, mode): if not self.cipher_supported(algorithm, mode): - raise UnsupportedCipher + raise UnsupportedAlgorithm(None) def create_symmetric_decryption_ctx(self, algorithm, mode): if not self.cipher_supported(algorithm, mode): - raise UnsupportedCipher + raise UnsupportedAlgorithm(None) @utils.register_interface(HashBackend) @@ -55,7 +53,7 @@ class DummyHashBackend(object): def create_hash_ctx(self, algorithm): if not self.hash_supported(algorithm): - raise UnsupportedHash + raise UnsupportedAlgorithm(None) @utils.register_interface(HMACBackend) @@ -68,7 +66,7 @@ class DummyHMACBackend(object): def create_hmac_ctx(self, key, algorithm): if not self.hmac_supported(algorithm): - raise UnsupportedHash + raise UnsupportedAlgorithm(None) @utils.register_interface(PBKDF2HMACBackend) @@ -82,7 +80,7 @@ class DummyPBKDF2HMACBackend(object): def derive_pbkdf2_hmac(self, algorithm, length, salt, iterations, key_material): if not self.pbkdf2_hmac_supported(algorithm): - raise UnsupportedHash + raise UnsupportedAlgorithm(None) @utils.register_interface(RSABackend) @@ -123,9 +121,9 @@ class TestMultiBackend(object): modes.CBC(b"\x00" * 16), backend=backend ) - with pytest.raises(UnsupportedCipher): + with pytest.raises(UnsupportedAlgorithm): cipher.encryptor() - with pytest.raises(UnsupportedCipher): + with pytest.raises(UnsupportedAlgorithm): cipher.decryptor() def test_hashes(self): @@ -136,7 +134,7 @@ class TestMultiBackend(object): hashes.Hash(hashes.MD5(), backend=backend) - with pytest.raises(UnsupportedHash): + with pytest.raises(UnsupportedAlgorithm): hashes.Hash(hashes.SHA1(), backend=backend) def test_hmac(self): @@ -147,7 +145,7 @@ class TestMultiBackend(object): hmac.HMAC(b"", hashes.MD5(), backend=backend) - with pytest.raises(UnsupportedHash): + with pytest.raises(UnsupportedAlgorithm): hmac.HMAC(b"", hashes.SHA1(), backend=backend) def test_pbkdf2(self): @@ -158,7 +156,7 @@ class TestMultiBackend(object): backend.derive_pbkdf2_hmac(hashes.MD5(), 10, b"", 10, b"") - with pytest.raises(UnsupportedHash): + with pytest.raises(UnsupportedAlgorithm): backend.derive_pbkdf2_hmac(hashes.SHA1(), 10, b"", 10, b"") def test_rsa(self): diff --git a/tests/hazmat/backends/test_openssl.py b/tests/hazmat/backends/test_openssl.py index 3747f436..fdc661f0 100644 --- a/tests/hazmat/backends/test_openssl.py +++ b/tests/hazmat/backends/test_openssl.py @@ -16,9 +16,7 @@ from __future__ import absolute_import, division, print_function import pytest from cryptography import utils -from cryptography.exceptions import ( - InternalError, UnsupportedCipher, UnsupportedHash -) +from cryptography.exceptions import InternalError, UnsupportedAlgorithm from cryptography.hazmat.backends.openssl.backend import Backend, backend from cryptography.hazmat.primitives import hashes, interfaces from cryptography.hazmat.primitives.asymmetric import padding, rsa @@ -78,7 +76,7 @@ class TestOpenSSL(object): cipher = Cipher( DummyCipher(), mode, backend=b, ) - with pytest.raises(UnsupportedCipher): + with pytest.raises(UnsupportedAlgorithm): cipher.encryptor() def test_consume_errors(self): @@ -140,7 +138,7 @@ class TestOpenSSL(object): def test_derive_pbkdf2_raises_unsupported_on_old_openssl(self): if backend.pbkdf2_hmac_supported(hashes.SHA256()): pytest.skip("Requires an older OpenSSL") - with pytest.raises(UnsupportedHash): + with pytest.raises(UnsupportedAlgorithm): backend.derive_pbkdf2_hmac(hashes.SHA256(), 10, b"", 1000, b"") @pytest.mark.skipif( @@ -153,7 +151,7 @@ class TestOpenSSL(object): key_size=512, backend=backend ) - with pytest.raises(UnsupportedHash): + with pytest.raises(UnsupportedAlgorithm): private_key.signer( padding.PSS( mgf=padding.MGF1( @@ -165,7 +163,7 @@ class TestOpenSSL(object): backend ) public_key = private_key.public_key() - with pytest.raises(UnsupportedHash): + with pytest.raises(UnsupportedAlgorithm): public_key.verifier( b"sig", padding.PSS( diff --git a/tests/hazmat/primitives/test_block.py b/tests/hazmat/primitives/test_block.py index f2dab6cf..e9ea7fb0 100644 --- a/tests/hazmat/primitives/test_block.py +++ b/tests/hazmat/primitives/test_block.py @@ -18,7 +18,7 @@ import binascii import pytest from cryptography import utils -from cryptography.exceptions import AlreadyFinalized, UnsupportedCipher +from cryptography.exceptions import AlreadyFinalized, UnsupportedAlgorithm from cryptography.hazmat.primitives import interfaces from cryptography.hazmat.primitives.ciphers import ( Cipher, algorithms, modes @@ -114,10 +114,10 @@ class TestCipherContext(object): cipher = Cipher( DummyCipher(), mode, backend ) - with pytest.raises(UnsupportedCipher): + with pytest.raises(UnsupportedAlgorithm): cipher.encryptor() - with pytest.raises(UnsupportedCipher): + with pytest.raises(UnsupportedAlgorithm): cipher.decryptor() def test_incorrectly_padded(self, backend): diff --git a/tests/hazmat/primitives/test_ciphers.py b/tests/hazmat/primitives/test_ciphers.py index 1bea0bdb..827b3b90 100644 --- a/tests/hazmat/primitives/test_ciphers.py +++ b/tests/hazmat/primitives/test_ciphers.py @@ -17,7 +17,7 @@ import binascii import pytest -from cryptography.exceptions import UnsupportedInterface +from cryptography.exceptions import UnsupportedAlgorithm from cryptography.hazmat.primitives import ciphers from cryptography.hazmat.primitives.ciphers.algorithms import ( AES, ARC4, Blowfish, CAST5, Camellia, IDEA, TripleDES @@ -128,5 +128,5 @@ class TestIDEA(object): def test_invalid_backend(): pretend_backend = object() - with pytest.raises(UnsupportedInterface): + with pytest.raises(UnsupportedAlgorithm): ciphers.Cipher(AES(b"AAAAAAAAAAAAAAAA"), ECB, pretend_backend) diff --git a/tests/hazmat/primitives/test_hashes.py b/tests/hazmat/primitives/test_hashes.py index 5b318f64..ce8e92a3 100644 --- a/tests/hazmat/primitives/test_hashes.py +++ b/tests/hazmat/primitives/test_hashes.py @@ -20,9 +20,7 @@ import pytest import six from cryptography import utils -from cryptography.exceptions import ( - AlreadyFinalized, UnsupportedHash, UnsupportedInterface -) +from cryptography.exceptions import AlreadyFinalized, UnsupportedAlgorithm from cryptography.hazmat.backends.interfaces import HashBackend from cryptography.hazmat.primitives import hashes, interfaces @@ -72,7 +70,7 @@ class TestHashContext(object): h.finalize() def test_unsupported_hash(self, backend): - with pytest.raises(UnsupportedHash): + with pytest.raises(UnsupportedAlgorithm): hashes.Hash(UnsupportedDummyHash(), backend) @@ -183,5 +181,5 @@ class TestMD5(object): def test_invalid_backend(): pretend_backend = object() - with pytest.raises(UnsupportedInterface): + with pytest.raises(UnsupportedAlgorithm): hashes.Hash(hashes.SHA1(), pretend_backend) diff --git a/tests/hazmat/primitives/test_hkdf.py b/tests/hazmat/primitives/test_hkdf.py index 367addc9..b3d412c5 100644 --- a/tests/hazmat/primitives/test_hkdf.py +++ b/tests/hazmat/primitives/test_hkdf.py @@ -18,7 +18,7 @@ import pytest import six from cryptography.exceptions import ( - AlreadyFinalized, InvalidKey, UnsupportedInterface + AlreadyFinalized, InvalidKey, UnsupportedAlgorithm ) from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.kdf.hkdf import HKDF @@ -152,5 +152,5 @@ class TestHKDF(object): def test_invalid_backend(): pretend_backend = object() - with pytest.raises(UnsupportedInterface): + with pytest.raises(UnsupportedAlgorithm): HKDF(hashes.SHA256(), 16, None, None, pretend_backend) diff --git a/tests/hazmat/primitives/test_hmac.py b/tests/hazmat/primitives/test_hmac.py index 1065359a..cdf8909d 100644 --- a/tests/hazmat/primitives/test_hmac.py +++ b/tests/hazmat/primitives/test_hmac.py @@ -21,7 +21,7 @@ import six from cryptography import utils from cryptography.exceptions import ( - AlreadyFinalized, InvalidSignature, UnsupportedHash, UnsupportedInterface + AlreadyFinalized, InvalidSignature, UnsupportedAlgorithm ) from cryptography.hazmat.backends.interfaces import HMACBackend from cryptography.hazmat.primitives import hashes, hmac, interfaces @@ -106,12 +106,12 @@ class TestHMAC(object): h.verify(six.u('')) def test_unsupported_hash(self, backend): - with pytest.raises(UnsupportedHash): + with pytest.raises(UnsupportedAlgorithm): hmac.HMAC(b"key", UnsupportedDummyHash(), backend) def test_invalid_backend(): pretend_backend = object() - with pytest.raises(UnsupportedInterface): + with pytest.raises(UnsupportedAlgorithm): hmac.HMAC(b"key", hashes.SHA1(), pretend_backend) diff --git a/tests/hazmat/primitives/test_pbkdf2hmac.py b/tests/hazmat/primitives/test_pbkdf2hmac.py index 585693ea..ebc5fbf5 100644 --- a/tests/hazmat/primitives/test_pbkdf2hmac.py +++ b/tests/hazmat/primitives/test_pbkdf2hmac.py @@ -18,7 +18,7 @@ import six from cryptography import utils from cryptography.exceptions import ( - AlreadyFinalized, InvalidKey, UnsupportedHash, UnsupportedInterface + AlreadyFinalized, InvalidKey, UnsupportedAlgorithm ) from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes, interfaces @@ -48,7 +48,7 @@ class TestPBKDF2HMAC(object): kdf.verify(b"password", key) def test_unsupported_algorithm(self): - with pytest.raises(UnsupportedHash): + with pytest.raises(UnsupportedAlgorithm): PBKDF2HMAC(DummyHash(), 20, b"salt", 10, default_backend()) def test_invalid_key(self): @@ -72,5 +72,5 @@ class TestPBKDF2HMAC(object): def test_invalid_backend(): pretend_backend = object() - with pytest.raises(UnsupportedInterface): + with pytest.raises(UnsupportedAlgorithm): PBKDF2HMAC(hashes.SHA1(), 20, b"salt", 10, pretend_backend) diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index eb7e1e60..2d3ce092 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -21,9 +21,9 @@ import os import pytest -from cryptography import exceptions, utils +from cryptography import utils from cryptography.exceptions import ( - UnsupportedAlgorithm, UnsupportedInterface + AlreadyFinalized, InvalidSignature, UnsupportedAlgorithm ) from cryptography.hazmat.primitives import hashes, interfaces from cryptography.hazmat.primitives.asymmetric import padding, rsa @@ -398,7 +398,7 @@ class TestRSA(object): def test_rsa_generate_invalid_backend(): pretend_backend = object() - with pytest.raises(UnsupportedInterface): + with pytest.raises(UnsupportedAlgorithm): rsa.RSAPrivateKey.generate(65537, 2048, pretend_backend) @@ -594,9 +594,9 @@ class TestRSASignature(object): signer = private_key.signer(padding.PKCS1v15(), hashes.SHA1(), backend) signer.update(b"sign me") signer.finalize() - with pytest.raises(exceptions.AlreadyFinalized): + with pytest.raises(AlreadyFinalized): signer.finalize() - with pytest.raises(exceptions.AlreadyFinalized): + with pytest.raises(AlreadyFinalized): signer.update(b"more data") def test_unsupported_padding(self, backend): @@ -605,7 +605,7 @@ class TestRSASignature(object): key_size=512, backend=backend ) - with pytest.raises(exceptions.UnsupportedPadding): + with pytest.raises(UnsupportedAlgorithm): private_key.signer(DummyPadding(), hashes.SHA1(), backend) def test_padding_incorrect_type(self, backend): @@ -621,7 +621,7 @@ class TestRSASignature(object): pretend_backend = object() private_key = rsa.RSAPrivateKey.generate(65537, 2048, backend) - with pytest.raises(UnsupportedInterface): + with pytest.raises(UnsupportedAlgorithm): private_key.signer( padding.PKCS1v15(), hashes.SHA256, pretend_backend) @@ -678,7 +678,7 @@ class TestRSAVerification(object): backend ) verifier.update(b"incorrect data") - with pytest.raises(exceptions.InvalidSignature): + with pytest.raises(InvalidSignature): verifier.verify() def test_invalid_pkcs1v15_signature_wrong_key(self, backend): @@ -703,7 +703,7 @@ class TestRSAVerification(object): backend ) verifier.update(b"sign me") - with pytest.raises(exceptions.InvalidSignature): + with pytest.raises(InvalidSignature): verifier.verify() @pytest.mark.parametrize( @@ -759,7 +759,7 @@ class TestRSAVerification(object): backend ) verifier.update(b"incorrect data") - with pytest.raises(exceptions.InvalidSignature): + with pytest.raises(InvalidSignature): verifier.verify() def test_invalid_pss_signature_wrong_key(self, backend): @@ -789,7 +789,7 @@ class TestRSAVerification(object): backend ) verifier.update(b"sign me") - with pytest.raises(exceptions.InvalidSignature): + with pytest.raises(InvalidSignature): verifier.verify() def test_invalid_pss_signature_data_too_large_for_modulus(self, backend): @@ -819,7 +819,7 @@ class TestRSAVerification(object): backend ) verifier.update(b"sign me") - with pytest.raises(exceptions.InvalidSignature): + with pytest.raises(InvalidSignature): verifier.verify() def test_use_after_finalize(self, backend): @@ -841,9 +841,9 @@ class TestRSAVerification(object): ) verifier.update(b"sign me") verifier.verify() - with pytest.raises(exceptions.AlreadyFinalized): + with pytest.raises(AlreadyFinalized): verifier.verify() - with pytest.raises(exceptions.AlreadyFinalized): + with pytest.raises(AlreadyFinalized): verifier.update(b"more data") def test_unsupported_padding(self, backend): @@ -853,7 +853,7 @@ class TestRSAVerification(object): backend=backend ) public_key = private_key.public_key() - with pytest.raises(exceptions.UnsupportedPadding): + with pytest.raises(UnsupportedAlgorithm): public_key.verifier(b"sig", DummyPadding(), hashes.SHA1(), backend) def test_padding_incorrect_type(self, backend): @@ -871,7 +871,7 @@ class TestRSAVerification(object): private_key = rsa.RSAPrivateKey.generate(65537, 2048, backend) public_key = private_key.public_key() - with pytest.raises(UnsupportedInterface): + with pytest.raises(UnsupportedAlgorithm): public_key.verifier( b"foo", padding.PKCS1v15(), hashes.SHA256(), pretend_backend) @@ -939,7 +939,7 @@ class TestRSAVerification(object): backend ) verifier.update(b"sign me") - with pytest.raises(exceptions.InvalidSignature): + with pytest.raises(InvalidSignature): verifier.verify() diff --git a/tests/hazmat/primitives/twofactor/test_hotp.py b/tests/hazmat/primitives/twofactor/test_hotp.py index 4bb7c6b3..b2b2677c 100644 --- a/tests/hazmat/primitives/twofactor/test_hotp.py +++ b/tests/hazmat/primitives/twofactor/test_hotp.py @@ -17,12 +17,14 @@ import os import pytest -from cryptography.exceptions import InvalidToken, UnsupportedInterface +from cryptography.exceptions import InvalidToken, _Causes 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 +from ....utils import ( + load_nist_vectors, load_vectors_from_file, raises_unsupported +) vectors = load_vectors_from_file( "twofactor/rfc-4226.txt", load_nist_vectors) @@ -103,5 +105,5 @@ def test_invalid_backend(): pretend_backend = object() - with pytest.raises(UnsupportedInterface): + with raises_unsupported(_Causes.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 d5b0a8ed..208b0eea 100644 --- a/tests/hazmat/primitives/twofactor/test_totp.py +++ b/tests/hazmat/primitives/twofactor/test_totp.py @@ -15,11 +15,13 @@ from __future__ import absolute_import, division, print_function import pytest -from cryptography.exceptions import InvalidToken, UnsupportedInterface +from cryptography.exceptions import InvalidToken, _Causes from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.twofactor.totp import TOTP -from ....utils import load_nist_vectors, load_vectors_from_file +from ....utils import ( + load_nist_vectors, load_vectors_from_file, raises_unsupported +) vectors = load_vectors_from_file( "twofactor/rfc-6238.txt", load_nist_vectors) @@ -137,5 +139,5 @@ def test_invalid_backend(): pretend_backend = object() - with pytest.raises(UnsupportedInterface): + with raises_unsupported(_Causes.BACKEND_MISSING_INTERFACE): TOTP(secret, 8, hashes.SHA1(), 30, pretend_backend) diff --git a/tests/utils.py b/tests/utils.py index 3e35970e..fbe448fe 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -14,11 +14,13 @@ from __future__ import absolute_import, division, print_function import collections +from contextlib import contextmanager import pytest import six +from cryptography.exceptions import UnsupportedAlgorithm import cryptography_vectors @@ -67,6 +69,13 @@ def check_backend_support(item): "backend") +@contextmanager +def raises_unsupported(cause): + with pytest.raises(UnsupportedAlgorithm) as exc_info: + yield + assert exc_info.value._cause == cause + + def load_vectors_from_file(filename, loader): with cryptography_vectors.open_vector_file(filename) as vector_file: return loader(vector_file) -- cgit v1.2.3 From 112963e296aadfdeaa4e2624c3b81b6b8c726a06 Mon Sep 17 00:00:00 2001 From: Alex Stapleton Date: Wed, 26 Mar 2014 17:39:29 +0000 Subject: Address most of my own comments --- tests/hazmat/primitives/twofactor/test_hotp.py | 6 ++--- tests/hazmat/primitives/twofactor/test_totp.py | 6 ++--- tests/test_utils.py | 33 +++++++++++++++++++++++++- tests/utils.py | 6 ++--- 4 files changed, 41 insertions(+), 10 deletions(-) (limited to 'tests') 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): -- cgit v1.2.3 From 4c1401a3745af97fcd398ff5b7f0dcb94f5292a0 Mon Sep 17 00:00:00 2001 From: Alex Stapleton Date: Wed, 26 Mar 2014 20:26:49 +0000 Subject: Update tests and raise sites --- tests/hazmat/backends/test_commoncrypto.py | 8 ++++-- tests/hazmat/backends/test_multibackend.py | 26 +++++++++++--------- tests/hazmat/backends/test_openssl.py | 14 +++++++---- tests/hazmat/primitives/test_block.py | 9 ++++--- tests/hazmat/primitives/test_ciphers.py | 6 +++-- tests/hazmat/primitives/test_hashes.py | 9 ++++--- tests/hazmat/primitives/test_hkdf.py | 6 +++-- tests/hazmat/primitives/test_hmac.py | 7 +++--- tests/hazmat/primitives/test_pbkdf2hmac.py | 8 +++--- tests/hazmat/primitives/test_rsa.py | 39 +++++++++++++++--------------- 10 files changed, 78 insertions(+), 54 deletions(-) (limited to 'tests') diff --git a/tests/hazmat/backends/test_commoncrypto.py b/tests/hazmat/backends/test_commoncrypto.py index dc6c8c5b..40a9f4a1 100644 --- a/tests/hazmat/backends/test_commoncrypto.py +++ b/tests/hazmat/backends/test_commoncrypto.py @@ -16,13 +16,17 @@ from __future__ import absolute_import, division, print_function import pytest from cryptography import utils -from cryptography.exceptions import InternalError, UnsupportedAlgorithm +from cryptography.exceptions import ( + InternalError, _Reasons +) from cryptography.hazmat.bindings.commoncrypto.binding import Binding from cryptography.hazmat.primitives import interfaces from cryptography.hazmat.primitives.ciphers.algorithms import AES from cryptography.hazmat.primitives.ciphers.base import Cipher from cryptography.hazmat.primitives.ciphers.modes import CBC, GCM +from ...utils import raises_unsupported_algorithm + @utils.register_interface(interfaces.CipherAlgorithm) class DummyCipher(object): @@ -63,5 +67,5 @@ class TestCommonCrypto(object): cipher = Cipher( DummyCipher(), GCM(b"fake_iv_here"), backend=b, ) - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_CIPHER): cipher.encryptor() diff --git a/tests/hazmat/backends/test_multibackend.py b/tests/hazmat/backends/test_multibackend.py index 7ab9e446..b7bcaf69 100644 --- a/tests/hazmat/backends/test_multibackend.py +++ b/tests/hazmat/backends/test_multibackend.py @@ -16,7 +16,9 @@ from __future__ import absolute_import, division, print_function import pytest from cryptography import utils -from cryptography.exceptions import UnsupportedAlgorithm +from cryptography.exceptions import ( + UnsupportedAlgorithm, _Reasons +) from cryptography.hazmat.backends.interfaces import ( CipherBackend, HMACBackend, HashBackend, PBKDF2HMACBackend, RSABackend ) @@ -25,6 +27,8 @@ from cryptography.hazmat.primitives import hashes, hmac from cryptography.hazmat.primitives.asymmetric import padding from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes +from ...utils import raises_unsupported_algorithm + @utils.register_interface(CipherBackend) class DummyCipherBackend(object): @@ -36,11 +40,11 @@ class DummyCipherBackend(object): def create_symmetric_encryption_ctx(self, algorithm, mode): if not self.cipher_supported(algorithm, mode): - raise UnsupportedAlgorithm(None) + raise UnsupportedAlgorithm("", _Reasons.UNSUPPORTED_CIPHER) def create_symmetric_decryption_ctx(self, algorithm, mode): if not self.cipher_supported(algorithm, mode): - raise UnsupportedAlgorithm(None) + raise UnsupportedAlgorithm("", _Reasons.UNSUPPORTED_CIPHER) @utils.register_interface(HashBackend) @@ -53,7 +57,7 @@ class DummyHashBackend(object): def create_hash_ctx(self, algorithm): if not self.hash_supported(algorithm): - raise UnsupportedAlgorithm(None) + raise UnsupportedAlgorithm("", _Reasons.UNSUPPORTED_HASH) @utils.register_interface(HMACBackend) @@ -66,7 +70,7 @@ class DummyHMACBackend(object): def create_hmac_ctx(self, key, algorithm): if not self.hmac_supported(algorithm): - raise UnsupportedAlgorithm(None) + raise UnsupportedAlgorithm("", _Reasons.UNSUPPORTED_HASH) @utils.register_interface(PBKDF2HMACBackend) @@ -80,7 +84,7 @@ class DummyPBKDF2HMACBackend(object): def derive_pbkdf2_hmac(self, algorithm, length, salt, iterations, key_material): if not self.pbkdf2_hmac_supported(algorithm): - raise UnsupportedAlgorithm(None) + raise UnsupportedAlgorithm("", _Reasons.UNSUPPORTED_HASH) @utils.register_interface(RSABackend) @@ -121,9 +125,9 @@ class TestMultiBackend(object): modes.CBC(b"\x00" * 16), backend=backend ) - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_CIPHER): cipher.encryptor() - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_CIPHER): cipher.decryptor() def test_hashes(self): @@ -134,7 +138,7 @@ class TestMultiBackend(object): hashes.Hash(hashes.MD5(), backend=backend) - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_HASH): hashes.Hash(hashes.SHA1(), backend=backend) def test_hmac(self): @@ -145,7 +149,7 @@ class TestMultiBackend(object): hmac.HMAC(b"", hashes.MD5(), backend=backend) - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_HASH): hmac.HMAC(b"", hashes.SHA1(), backend=backend) def test_pbkdf2(self): @@ -156,7 +160,7 @@ class TestMultiBackend(object): backend.derive_pbkdf2_hmac(hashes.MD5(), 10, b"", 10, b"") - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_HASH): backend.derive_pbkdf2_hmac(hashes.SHA1(), 10, b"", 10, b"") def test_rsa(self): diff --git a/tests/hazmat/backends/test_openssl.py b/tests/hazmat/backends/test_openssl.py index fdc661f0..016da0fc 100644 --- a/tests/hazmat/backends/test_openssl.py +++ b/tests/hazmat/backends/test_openssl.py @@ -16,7 +16,9 @@ from __future__ import absolute_import, division, print_function import pytest from cryptography import utils -from cryptography.exceptions import InternalError, UnsupportedAlgorithm +from cryptography.exceptions import ( + InternalError, _Reasons +) from cryptography.hazmat.backends.openssl.backend import Backend, backend from cryptography.hazmat.primitives import hashes, interfaces from cryptography.hazmat.primitives.asymmetric import padding, rsa @@ -24,6 +26,8 @@ from cryptography.hazmat.primitives.ciphers import Cipher from cryptography.hazmat.primitives.ciphers.algorithms import AES from cryptography.hazmat.primitives.ciphers.modes import CBC +from ...utils import raises_unsupported_algorithm + @utils.register_interface(interfaces.Mode) class DummyMode(object): @@ -76,7 +80,7 @@ class TestOpenSSL(object): cipher = Cipher( DummyCipher(), mode, backend=b, ) - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_CIPHER): cipher.encryptor() def test_consume_errors(self): @@ -138,7 +142,7 @@ class TestOpenSSL(object): def test_derive_pbkdf2_raises_unsupported_on_old_openssl(self): if backend.pbkdf2_hmac_supported(hashes.SHA256()): pytest.skip("Requires an older OpenSSL") - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_HASH): backend.derive_pbkdf2_hmac(hashes.SHA256(), 10, b"", 1000, b"") @pytest.mark.skipif( @@ -151,7 +155,7 @@ class TestOpenSSL(object): key_size=512, backend=backend ) - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_HASH): private_key.signer( padding.PSS( mgf=padding.MGF1( @@ -163,7 +167,7 @@ class TestOpenSSL(object): backend ) public_key = private_key.public_key() - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_HASH): public_key.verifier( b"sig", padding.PSS( diff --git a/tests/hazmat/primitives/test_block.py b/tests/hazmat/primitives/test_block.py index e9ea7fb0..68d6c849 100644 --- a/tests/hazmat/primitives/test_block.py +++ b/tests/hazmat/primitives/test_block.py @@ -18,7 +18,9 @@ import binascii import pytest from cryptography import utils -from cryptography.exceptions import AlreadyFinalized, UnsupportedAlgorithm +from cryptography.exceptions import ( + AlreadyFinalized, _Reasons +) from cryptography.hazmat.primitives import interfaces from cryptography.hazmat.primitives.ciphers import ( Cipher, algorithms, modes @@ -27,6 +29,7 @@ from cryptography.hazmat.primitives.ciphers import ( from .utils import ( generate_aead_exception_test, generate_aead_tag_exception_test ) +from ...utils import raises_unsupported_algorithm @utils.register_interface(interfaces.Mode) @@ -114,10 +117,10 @@ class TestCipherContext(object): cipher = Cipher( DummyCipher(), mode, backend ) - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_CIPHER): cipher.encryptor() - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_CIPHER): cipher.decryptor() def test_incorrectly_padded(self, backend): diff --git a/tests/hazmat/primitives/test_ciphers.py b/tests/hazmat/primitives/test_ciphers.py index 827b3b90..9f8123eb 100644 --- a/tests/hazmat/primitives/test_ciphers.py +++ b/tests/hazmat/primitives/test_ciphers.py @@ -17,13 +17,15 @@ import binascii import pytest -from cryptography.exceptions import UnsupportedAlgorithm +from cryptography.exceptions import _Reasons from cryptography.hazmat.primitives import ciphers from cryptography.hazmat.primitives.ciphers.algorithms import ( AES, ARC4, Blowfish, CAST5, Camellia, IDEA, TripleDES ) from cryptography.hazmat.primitives.ciphers.modes import ECB +from ...utils import raises_unsupported_algorithm + class TestAES(object): @pytest.mark.parametrize(("key", "keysize"), [ @@ -128,5 +130,5 @@ class TestIDEA(object): def test_invalid_backend(): pretend_backend = object() - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE): ciphers.Cipher(AES(b"AAAAAAAAAAAAAAAA"), ECB, pretend_backend) diff --git a/tests/hazmat/primitives/test_hashes.py b/tests/hazmat/primitives/test_hashes.py index ce8e92a3..ffd65bde 100644 --- a/tests/hazmat/primitives/test_hashes.py +++ b/tests/hazmat/primitives/test_hashes.py @@ -20,11 +20,14 @@ import pytest import six from cryptography import utils -from cryptography.exceptions import AlreadyFinalized, UnsupportedAlgorithm +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 ...utils import raises_unsupported_algorithm @utils.register_interface(interfaces.HashAlgorithm) @@ -70,7 +73,7 @@ class TestHashContext(object): h.finalize() def test_unsupported_hash(self, backend): - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_HASH): hashes.Hash(UnsupportedDummyHash(), backend) @@ -181,5 +184,5 @@ class TestMD5(object): def test_invalid_backend(): pretend_backend = object() - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE): hashes.Hash(hashes.SHA1(), pretend_backend) diff --git a/tests/hazmat/primitives/test_hkdf.py b/tests/hazmat/primitives/test_hkdf.py index b3d412c5..2e3c0c3d 100644 --- a/tests/hazmat/primitives/test_hkdf.py +++ b/tests/hazmat/primitives/test_hkdf.py @@ -18,11 +18,13 @@ import pytest import six from cryptography.exceptions import ( - AlreadyFinalized, InvalidKey, UnsupportedAlgorithm + AlreadyFinalized, InvalidKey, _Reasons ) from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.kdf.hkdf import HKDF +from ...utils import raises_unsupported_algorithm + @pytest.mark.hmac class TestHKDF(object): @@ -152,5 +154,5 @@ class TestHKDF(object): def test_invalid_backend(): pretend_backend = object() - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE): HKDF(hashes.SHA256(), 16, None, None, pretend_backend) diff --git a/tests/hazmat/primitives/test_hmac.py b/tests/hazmat/primitives/test_hmac.py index cdf8909d..77dfb6be 100644 --- a/tests/hazmat/primitives/test_hmac.py +++ b/tests/hazmat/primitives/test_hmac.py @@ -21,12 +21,13 @@ import six from cryptography import utils from cryptography.exceptions import ( - AlreadyFinalized, InvalidSignature, UnsupportedAlgorithm + AlreadyFinalized, InvalidSignature, _Reasons ) from cryptography.hazmat.backends.interfaces import HMACBackend from cryptography.hazmat.primitives import hashes, hmac, interfaces from .utils import generate_base_hmac_test +from ...utils import raises_unsupported_algorithm @utils.register_interface(interfaces.HashAlgorithm) @@ -106,12 +107,12 @@ class TestHMAC(object): h.verify(six.u('')) def test_unsupported_hash(self, backend): - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_HASH): hmac.HMAC(b"key", UnsupportedDummyHash(), backend) def test_invalid_backend(): pretend_backend = object() - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE): hmac.HMAC(b"key", hashes.SHA1(), pretend_backend) diff --git a/tests/hazmat/primitives/test_pbkdf2hmac.py b/tests/hazmat/primitives/test_pbkdf2hmac.py index ebc5fbf5..62ca0921 100644 --- a/tests/hazmat/primitives/test_pbkdf2hmac.py +++ b/tests/hazmat/primitives/test_pbkdf2hmac.py @@ -18,12 +18,14 @@ import six from cryptography import utils from cryptography.exceptions import ( - AlreadyFinalized, InvalidKey, UnsupportedAlgorithm + AlreadyFinalized, InvalidKey, _Reasons ) from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes, interfaces from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC +from ...utils import raises_unsupported_algorithm + @utils.register_interface(interfaces.HashAlgorithm) class DummyHash(object): @@ -48,7 +50,7 @@ class TestPBKDF2HMAC(object): kdf.verify(b"password", key) def test_unsupported_algorithm(self): - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_HASH): PBKDF2HMAC(DummyHash(), 20, b"salt", 10, default_backend()) def test_invalid_key(self): @@ -72,5 +74,5 @@ class TestPBKDF2HMAC(object): def test_invalid_backend(): pretend_backend = object() - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE): PBKDF2HMAC(hashes.SHA1(), 20, b"salt", 10, pretend_backend) diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 2d3ce092..5d94e790 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -21,16 +21,15 @@ import os import pytest -from cryptography import utils -from cryptography.exceptions import ( - AlreadyFinalized, InvalidSignature, UnsupportedAlgorithm -) +from cryptography import exceptions, utils +from cryptography.exceptions import UnsupportedAlgorithm, _Reasons from cryptography.hazmat.primitives import hashes, interfaces from cryptography.hazmat.primitives.asymmetric import padding, rsa from .utils import generate_rsa_verification_test from ...utils import ( - load_pkcs1_vectors, load_rsa_nist_vectors, load_vectors_from_file + load_pkcs1_vectors, load_rsa_nist_vectors, load_vectors_from_file, + raises_unsupported_algorithm ) @@ -398,7 +397,7 @@ class TestRSA(object): def test_rsa_generate_invalid_backend(): pretend_backend = object() - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE): rsa.RSAPrivateKey.generate(65537, 2048, pretend_backend) @@ -594,9 +593,9 @@ class TestRSASignature(object): signer = private_key.signer(padding.PKCS1v15(), hashes.SHA1(), backend) signer.update(b"sign me") signer.finalize() - with pytest.raises(AlreadyFinalized): + with pytest.raises(exceptions.AlreadyFinalized): signer.finalize() - with pytest.raises(AlreadyFinalized): + with pytest.raises(exceptions.AlreadyFinalized): signer.update(b"more data") def test_unsupported_padding(self, backend): @@ -605,7 +604,7 @@ class TestRSASignature(object): key_size=512, backend=backend ) - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_PADDING): private_key.signer(DummyPadding(), hashes.SHA1(), backend) def test_padding_incorrect_type(self, backend): @@ -621,7 +620,7 @@ class TestRSASignature(object): pretend_backend = object() private_key = rsa.RSAPrivateKey.generate(65537, 2048, backend) - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE): private_key.signer( padding.PKCS1v15(), hashes.SHA256, pretend_backend) @@ -678,7 +677,7 @@ class TestRSAVerification(object): backend ) verifier.update(b"incorrect data") - with pytest.raises(InvalidSignature): + with pytest.raises(exceptions.InvalidSignature): verifier.verify() def test_invalid_pkcs1v15_signature_wrong_key(self, backend): @@ -703,7 +702,7 @@ class TestRSAVerification(object): backend ) verifier.update(b"sign me") - with pytest.raises(InvalidSignature): + with pytest.raises(exceptions.InvalidSignature): verifier.verify() @pytest.mark.parametrize( @@ -759,7 +758,7 @@ class TestRSAVerification(object): backend ) verifier.update(b"incorrect data") - with pytest.raises(InvalidSignature): + with pytest.raises(exceptions.InvalidSignature): verifier.verify() def test_invalid_pss_signature_wrong_key(self, backend): @@ -789,7 +788,7 @@ class TestRSAVerification(object): backend ) verifier.update(b"sign me") - with pytest.raises(InvalidSignature): + with pytest.raises(exceptions.InvalidSignature): verifier.verify() def test_invalid_pss_signature_data_too_large_for_modulus(self, backend): @@ -819,7 +818,7 @@ class TestRSAVerification(object): backend ) verifier.update(b"sign me") - with pytest.raises(InvalidSignature): + with pytest.raises(exceptions.InvalidSignature): verifier.verify() def test_use_after_finalize(self, backend): @@ -841,9 +840,9 @@ class TestRSAVerification(object): ) verifier.update(b"sign me") verifier.verify() - with pytest.raises(AlreadyFinalized): + with pytest.raises(exceptions.AlreadyFinalized): verifier.verify() - with pytest.raises(AlreadyFinalized): + with pytest.raises(exceptions.AlreadyFinalized): verifier.update(b"more data") def test_unsupported_padding(self, backend): @@ -853,7 +852,7 @@ class TestRSAVerification(object): backend=backend ) public_key = private_key.public_key() - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_PADDING): public_key.verifier(b"sig", DummyPadding(), hashes.SHA1(), backend) def test_padding_incorrect_type(self, backend): @@ -871,7 +870,7 @@ class TestRSAVerification(object): private_key = rsa.RSAPrivateKey.generate(65537, 2048, backend) public_key = private_key.public_key() - with pytest.raises(UnsupportedAlgorithm): + with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE): public_key.verifier( b"foo", padding.PKCS1v15(), hashes.SHA256(), pretend_backend) @@ -939,7 +938,7 @@ class TestRSAVerification(object): backend ) verifier.update(b"sign me") - with pytest.raises(InvalidSignature): + with pytest.raises(exceptions.InvalidSignature): verifier.verify() -- cgit v1.2.3 From d80195e1712469ae59d1f9adc306ebfa23cfb59c Mon Sep 17 00:00:00 2001 From: Alex Stapleton Date: Wed, 26 Mar 2014 20:41:31 +0000 Subject: Update tests --- tests/test_utils.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/tests/test_utils.py b/tests/test_utils.py index a8046dc3..b430f567 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1612,23 +1612,28 @@ def test_vector_version(): assert cryptography.__version__ == cryptography_vectors.__version__ +@pytest.mark.xfail 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 + with raises_unsupported_algorithm(None): + raise Exception +@pytest.mark.xfail 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) + with raises_unsupported_algorithm(None): + raise UnsupportedAlgorithm("An error.", + _Reasons.BACKEND_MISSING_INTERFACE) + + +@pytest.mark.xfail +def test_raises_unsupported_no_exc(): + # Check that it raises if no exception is raised. + with raises_unsupported_algorithm( + _Reasons.BACKEND_MISSING_INTERFACE + ): + pass def test_raises_unsupported_algorithm(): -- cgit v1.2.3 From 5e4c8c3666860fbe7320ea2227428f530cc8f176 Mon Sep 17 00:00:00 2001 From: Alex Stapleton Date: Thu, 27 Mar 2014 16:38:00 +0000 Subject: Fixes to @alex's comments --- tests/hazmat/backends/test_commoncrypto.py | 4 +--- tests/test_utils.py | 37 ++++++++++++++++-------------- tests/utils.py | 6 +++-- 3 files changed, 25 insertions(+), 22 deletions(-) (limited to 'tests') diff --git a/tests/hazmat/backends/test_commoncrypto.py b/tests/hazmat/backends/test_commoncrypto.py index 40a9f4a1..7c703f67 100644 --- a/tests/hazmat/backends/test_commoncrypto.py +++ b/tests/hazmat/backends/test_commoncrypto.py @@ -16,9 +16,7 @@ from __future__ import absolute_import, division, print_function import pytest from cryptography import utils -from cryptography.exceptions import ( - InternalError, _Reasons -) +from cryptography.exceptions import InternalError, _Reasons from cryptography.hazmat.bindings.commoncrypto.binding import Binding from cryptography.hazmat.primitives import interfaces from cryptography.hazmat.primitives.ciphers.algorithms import AES diff --git a/tests/test_utils.py b/tests/test_utils.py index b430f567..939845fc 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1612,35 +1612,38 @@ def test_vector_version(): assert cryptography.__version__ == cryptography_vectors.__version__ -@pytest.mark.xfail def test_raises_unsupported_algorithm_wrong_type(): - # Check that it asserts if the wrong type of exception is raised. - with raises_unsupported_algorithm(None): - raise Exception + # Check that it raises if the wrong type of exception is raised. + class TestException(Exception): + pass + + with pytest.raises(TestException): + with raises_unsupported_algorithm(None): + raise TestException -@pytest.mark.xfail def test_raises_unsupported_algorithm_wrong_reason(): - # Check that it asserts if the wrong reason code is raised. - with raises_unsupported_algorithm(None): - raise UnsupportedAlgorithm("An error.", - _Reasons.BACKEND_MISSING_INTERFACE) + # Check that it fails if the wrong reason code is raised. + with pytest.raises(pytest.fail.Exception): + with raises_unsupported_algorithm(None): + raise UnsupportedAlgorithm("An error.", + _Reasons.BACKEND_MISSING_INTERFACE) -@pytest.mark.xfail def test_raises_unsupported_no_exc(): - # Check that it raises if no exception is raised. - with raises_unsupported_algorithm( - _Reasons.BACKEND_MISSING_INTERFACE - ): - pass + # Check that it fails if no exception is raised. + with pytest.raises(pytest.fail.Exception): + with raises_unsupported_algorithm( + _Reasons.BACKEND_MISSING_INTERFACE + ): + pass 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: + ) as exc_info: raise UnsupportedAlgorithm("An error.", _Reasons.BACKEND_MISSING_INTERFACE) - assert exc + assert exc_info.type is UnsupportedAlgorithm diff --git a/tests/utils.py b/tests/utils.py index f948642e..3e5ea5f3 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -70,10 +70,12 @@ def check_backend_support(item): @contextmanager -def raises_unsupported_algorithm(cause): +def raises_unsupported_algorithm(reason): with pytest.raises(UnsupportedAlgorithm) as exc_info: yield exc_info - assert exc_info.value._cause is cause + + if exc_info.value._reason is not reason: + pytest.fail("Did not get expected reason tag for UnsupportedAlgorithm") def load_vectors_from_file(filename, loader): -- cgit v1.2.3 From 85a791f0fa061ec644f5bfca41ee6038eeef38eb Mon Sep 17 00:00:00 2001 From: Alex Stapleton Date: Thu, 27 Mar 2014 16:55:41 +0000 Subject: Pain the bikeshed a different colour --- tests/test_utils.py | 2 +- tests/utils.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/test_utils.py b/tests/test_utils.py index 939845fc..d0b70663 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1624,7 +1624,7 @@ def test_raises_unsupported_algorithm_wrong_type(): def test_raises_unsupported_algorithm_wrong_reason(): # Check that it fails if the wrong reason code is raised. - with pytest.raises(pytest.fail.Exception): + with pytest.raises(AssertionError): with raises_unsupported_algorithm(None): raise UnsupportedAlgorithm("An error.", _Reasons.BACKEND_MISSING_INTERFACE) diff --git a/tests/utils.py b/tests/utils.py index 3e5ea5f3..bdbf996f 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -74,8 +74,7 @@ def raises_unsupported_algorithm(reason): with pytest.raises(UnsupportedAlgorithm) as exc_info: yield exc_info - if exc_info.value._reason is not reason: - pytest.fail("Did not get expected reason tag for UnsupportedAlgorithm") + assert exc_info.value._reason is reason def load_vectors_from_file(filename, loader): -- cgit v1.2.3