diff options
Diffstat (limited to 'tests/hazmat')
-rw-r--r-- | tests/hazmat/bindings/test_openssl.py | 25 | ||||
-rw-r--r-- | tests/hazmat/primitives/test_block.py | 32 | ||||
-rw-r--r-- | tests/hazmat/primitives/test_hashes.py | 16 | ||||
-rw-r--r-- | tests/hazmat/primitives/test_hmac.py | 8 | ||||
-rw-r--r-- | tests/hazmat/primitives/utils.py | 8 |
5 files changed, 59 insertions, 30 deletions
diff --git a/tests/hazmat/bindings/test_openssl.py b/tests/hazmat/bindings/test_openssl.py index 1cadc75c..4923d698 100644 --- a/tests/hazmat/bindings/test_openssl.py +++ b/tests/hazmat/bindings/test_openssl.py @@ -15,6 +15,7 @@ import pytest from cryptography import utils from cryptography.exceptions import UnsupportedAlgorithm +from cryptography.hazmat.bindings import default_backend from cryptography.hazmat.bindings.openssl.backend import backend, Backend from cryptography.hazmat.primitives import interfaces from cryptography.hazmat.primitives.ciphers import Cipher @@ -36,6 +37,9 @@ class TestOpenSSL(object): def test_backend_exists(self): assert backend + def test_is_default(self): + assert backend == default_backend() + def test_openssl_version_text(self): """ This test checks the value of OPENSSL_VERSION_TEXT. @@ -71,3 +75,24 @@ class TestOpenSSL(object): ) with pytest.raises(UnsupportedAlgorithm): cipher.encryptor() + + def test_handle_unknown_error(self): + with pytest.raises(SystemError): + backend._handle_error_code(0, 0, 0) + + with pytest.raises(SystemError): + backend._handle_error_code(backend.lib.ERR_LIB_EVP, 0, 0) + + with pytest.raises(SystemError): + backend._handle_error_code( + backend.lib.ERR_LIB_EVP, + backend.lib.EVP_F_EVP_ENCRYPTFINAL_EX, + 0 + ) + + with pytest.raises(SystemError): + backend._handle_error_code( + backend.lib.ERR_LIB_EVP, + backend.lib.EVP_F_EVP_DECRYPTFINAL_EX, + 0 + ) diff --git a/tests/hazmat/primitives/test_block.py b/tests/hazmat/primitives/test_block.py index e0deb36b..2a3e82d4 100644 --- a/tests/hazmat/primitives/test_block.py +++ b/tests/hazmat/primitives/test_block.py @@ -36,23 +36,19 @@ class DummyMode(object): class TestCipher(object): - def test_instantiate_without_backend(self): - Cipher( - algorithms.AES(binascii.unhexlify(b"0" * 32)), - modes.CBC(binascii.unhexlify(b"0" * 32)) - ) - - def test_creates_encryptor(self): + def test_creates_encryptor(self, backend): cipher = Cipher( algorithms.AES(binascii.unhexlify(b"0" * 32)), - modes.CBC(binascii.unhexlify(b"0" * 32)) + modes.CBC(binascii.unhexlify(b"0" * 32)), + backend ) assert isinstance(cipher.encryptor(), interfaces.CipherContext) - def test_creates_decryptor(self): + def test_creates_decryptor(self, backend): cipher = Cipher( algorithms.AES(binascii.unhexlify(b"0" * 32)), - modes.CBC(binascii.unhexlify(b"0" * 32)) + modes.CBC(binascii.unhexlify(b"0" * 32)), + backend ) assert isinstance(cipher.decryptor(), interfaces.CipherContext) @@ -114,6 +110,22 @@ class TestCipherContext(object): with pytest.raises(UnsupportedAlgorithm): cipher.decryptor() + def test_incorrectly_padded(self, backend): + cipher = Cipher( + algorithms.AES(b"\x00" * 16), + modes.CBC(b"\x00" * 16), + backend + ) + encryptor = cipher.encryptor() + encryptor.update(b"1") + with pytest.raises(ValueError): + encryptor.finalize() + + decryptor = cipher.decryptor() + decryptor.update(b"1") + with pytest.raises(ValueError): + decryptor.finalize() + class TestModeValidation(object): def test_cbc(self, backend): diff --git a/tests/hazmat/primitives/test_hashes.py b/tests/hazmat/primitives/test_hashes.py index 367e764f..ff42e8f4 100644 --- a/tests/hazmat/primitives/test_hashes.py +++ b/tests/hazmat/primitives/test_hashes.py @@ -20,7 +20,6 @@ import pytest import six from cryptography.exceptions import AlreadyFinalized -from cryptography.hazmat.bindings import _default_backend from cryptography.hazmat.primitives import hashes from .utils import generate_base_hash_test @@ -41,19 +40,12 @@ class TestHashContext(object): assert h._backend is pretend_backend assert h.copy()._backend is h._backend - def test_default_backend_creation(self): - """ - This test assumes the presence of SHA1 in the default backend. - """ - h = hashes.Hash(hashes.SHA1()) - assert h._backend is _default_backend - - def test_hash_algorithm_instance(self): + def test_hash_algorithm_instance(self, backend): with pytest.raises(TypeError): - hashes.Hash(hashes.SHA1) + hashes.Hash(hashes.SHA1, backend=backend) - def test_raises_after_finalize(self): - h = hashes.Hash(hashes.SHA1()) + def test_raises_after_finalize(self, backend): + h = hashes.Hash(hashes.SHA1(), backend=backend) h.finalize() with pytest.raises(AlreadyFinalized): diff --git a/tests/hazmat/primitives/test_hmac.py b/tests/hazmat/primitives/test_hmac.py index d17049e3..992bcb1a 100644 --- a/tests/hazmat/primitives/test_hmac.py +++ b/tests/hazmat/primitives/test_hmac.py @@ -47,12 +47,12 @@ class TestHMAC(object): assert h._backend is pretend_backend assert h.copy()._backend is pretend_backend - def test_hmac_algorithm_instance(self): + def test_hmac_algorithm_instance(self, backend): with pytest.raises(TypeError): - hmac.HMAC(b"key", hashes.SHA1) + hmac.HMAC(b"key", hashes.SHA1, backend=backend) - def test_raises_after_finalize(self): - h = hmac.HMAC(b"key", hashes.SHA1()) + def test_raises_after_finalize(self, backend): + h = hmac.HMAC(b"key", hashes.SHA1(), backend=backend) h.finalize() with pytest.raises(AlreadyFinalized): diff --git a/tests/hazmat/primitives/utils.py b/tests/hazmat/primitives/utils.py index 9327b0eb..6c67ddb3 100644 --- a/tests/hazmat/primitives/utils.py +++ b/tests/hazmat/primitives/utils.py @@ -42,7 +42,7 @@ def encrypt_test(backend, cipher_factory, mode_factory, params, only_if, cipher = Cipher( cipher_factory(**params), mode_factory(**params), - backend + backend=backend ) encryptor = cipher.encryptor() actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext)) @@ -82,7 +82,7 @@ def stream_encryption_test(backend, cipher_factory, params, only_if, plaintext = params.pop("plaintext") ciphertext = params.pop("ciphertext") offset = params.pop("offset") - cipher = Cipher(cipher_factory(**params), None, backend) + cipher = Cipher(cipher_factory(**params), None, backend=backend) encryptor = cipher.encryptor() # throw away offset bytes encryptor.update(b"\x00" * int(offset)) @@ -211,7 +211,7 @@ def hmac_test(backend, algorithm, params, only_if, skip_message): msg = params[0] md = params[1] key = params[2] - h = hmac.HMAC(binascii.unhexlify(key), algorithm) + h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend) h.update(binascii.unhexlify(msg)) assert h.finalize() == binascii.unhexlify(md.encode("ascii")) @@ -233,7 +233,7 @@ 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), algorithm) + h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend) h_copy = h.copy() assert h != h_copy assert h._ctx != h_copy._ctx |