aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tests/test_fernet.py63
1 files changed, 11 insertions, 52 deletions
diff --git a/tests/test_fernet.py b/tests/test_fernet.py
index 34760e3c..0683d950 100644
--- a/tests/test_fernet.py
+++ b/tests/test_fernet.py
@@ -41,14 +41,19 @@ def json_parametrize(keys, filename):
])
+def test_default_backend():
+ f = Fernet(Fernet.generate_key())
+ assert f._backend is default_backend()
+
+
@pytest.mark.cipher
+@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",
+)
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",
)
@@ -61,12 +66,6 @@ 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",
)
@@ -78,12 +77,6 @@ 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)
@@ -92,34 +85,16 @@ 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):
@@ -127,27 +102,11 @@ 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)
assert f.decrypt(f.encrypt(message)) == message
- def test_default_backend(self):
- 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)