diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/conftest.py | 9 | ||||
-rw-r--r-- | tests/hazmat/backends/test_openssl.py | 18 | ||||
-rw-r--r-- | tests/test_fernet.py | 50 |
3 files changed, 72 insertions, 5 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 0ddc3338..1d9f96ed 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,6 @@ import pytest +from cryptography.hazmat.backends import _ALL_BACKENDS from cryptography.hazmat.backends.interfaces import ( HMACBackend, CipherBackend, HashBackend ) @@ -7,11 +8,9 @@ from cryptography.hazmat.backends.interfaces import ( from .utils import check_for_iface, check_backend_support -def pytest_generate_tests(metafunc): - from cryptography.hazmat.backends import _ALL_BACKENDS - - if "backend" in metafunc.fixturenames: - metafunc.parametrize("backend", _ALL_BACKENDS) +@pytest.fixture(params=_ALL_BACKENDS) +def backend(request): + return request.param @pytest.mark.trylast diff --git a/tests/hazmat/backends/test_openssl.py b/tests/hazmat/backends/test_openssl.py index ad399594..2a329920 100644 --- a/tests/hazmat/backends/test_openssl.py +++ b/tests/hazmat/backends/test_openssl.py @@ -95,3 +95,21 @@ class TestOpenSSL(object): backend._lib.EVP_F_EVP_DECRYPTFINAL_EX, 0 ) + + def test_ssl_ciphers_registered(self): + meth = backend._lib.TLSv1_method() + ctx = backend._lib.SSL_CTX_new(meth) + assert ctx != backend._ffi.NULL + backend._lib.SSL_CTX_free(ctx) + + def test_evp_ciphers_registered(self): + cipher = backend._lib.EVP_get_cipherbyname(b"aes-256-cbc") + assert cipher != backend._ffi.NULL + + def test_error_strings_loaded(self): + # returns a value in a static buffer + err = backend._lib.ERR_error_string(101183626, backend._ffi.NULL) + assert backend._ffi.string(err) == ( + b"error:0607F08A:digital envelope routines:EVP_EncryptFinal_ex:" + b"data not multiple of block length" + ) diff --git a/tests/test_fernet.py b/tests/test_fernet.py index 45188c47..bd4d90a5 100644 --- a/tests/test_fernet.py +++ b/tests/test_fernet.py @@ -25,6 +25,7 @@ import six from cryptography.fernet import Fernet, InvalidToken from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives.ciphers import algorithms, modes def json_parametrize(keys, fname): @@ -37,7 +38,14 @@ def json_parametrize(keys, fname): ]) +@pytest.mark.cipher class TestFernet(object): + @pytest.mark.supported( + only_if=lambda backend: backend.cipher_supported( + algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16) + ), + skip_message="Does not support AES CBC", + ) @json_parametrize( ("secret", "now", "iv", "src", "token"), "generate.json", ) @@ -50,6 +58,12 @@ class TestFernet(object): ) assert actual_token == token.encode("ascii") + @pytest.mark.supported( + only_if=lambda backend: backend.cipher_supported( + algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16) + ), + skip_message="Does not support AES CBC", + ) @json_parametrize( ("secret", "now", "src", "ttl_sec", "token"), "verify.json", ) @@ -61,6 +75,12 @@ class TestFernet(object): payload = f.decrypt(token.encode("ascii"), ttl=ttl_sec) assert payload == src.encode("ascii") + @pytest.mark.supported( + only_if=lambda backend: backend.cipher_supported( + algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16) + ), + skip_message="Does not support AES CBC", + ) @json_parametrize(("secret", "token", "now", "ttl_sec"), "invalid.json") def test_invalid(self, secret, token, now, ttl_sec, backend, monkeypatch): f = Fernet(secret.encode("ascii"), backend=backend) @@ -69,16 +89,34 @@ class TestFernet(object): with pytest.raises(InvalidToken): f.decrypt(token.encode("ascii"), ttl=ttl_sec) + @pytest.mark.supported( + only_if=lambda backend: backend.cipher_supported( + algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16) + ), + skip_message="Does not support AES CBC", + ) def test_invalid_start_byte(self, backend): f = Fernet(Fernet.generate_key(), backend=backend) with pytest.raises(InvalidToken): f.decrypt(base64.urlsafe_b64encode(b"\x81")) + @pytest.mark.supported( + only_if=lambda backend: backend.cipher_supported( + algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16) + ), + skip_message="Does not support AES CBC", + ) def test_timestamp_too_short(self, backend): f = Fernet(Fernet.generate_key(), backend=backend) with pytest.raises(InvalidToken): f.decrypt(base64.urlsafe_b64encode(b"\x80abc")) + @pytest.mark.supported( + only_if=lambda backend: backend.cipher_supported( + algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16) + ), + skip_message="Does not support AES CBC", + ) def test_unicode(self, backend): f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend) with pytest.raises(TypeError): @@ -86,6 +124,12 @@ class TestFernet(object): with pytest.raises(TypeError): f.decrypt(six.u("")) + @pytest.mark.supported( + only_if=lambda backend: backend.cipher_supported( + algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16) + ), + skip_message="Does not support AES CBC", + ) @pytest.mark.parametrize("message", [b"", b"Abc!", b"\x00\xFF\x00\x80"]) def test_roundtrips(self, message, backend): f = Fernet(Fernet.generate_key(), backend=backend) @@ -95,6 +139,12 @@ class TestFernet(object): f = Fernet(Fernet.generate_key()) assert f._backend is default_backend() + @pytest.mark.supported( + only_if=lambda backend: backend.cipher_supported( + algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16) + ), + skip_message="Does not support AES CBC", + ) def test_bad_key(self, backend): with pytest.raises(ValueError): Fernet(base64.urlsafe_b64encode(b"abc"), backend=backend) |