diff options
author | David Reid <dreid@dreid.org> | 2014-10-21 11:19:13 -0700 |
---|---|---|
committer | David Reid <dreid@dreid.org> | 2014-10-21 11:19:13 -0700 |
commit | 6ec1b7bb991a123ed131f138c24eee2c6c18b839 (patch) | |
tree | d00d52f02e4e49d0f2809b4de07b7af909f52eba /tests/test_fernet.py | |
parent | 126afca70edc3fac2e493c6b7cd05219c8d8e373 (diff) | |
parent | 4c82513ac8ae9b319eecb1fc18d11c55305c1663 (diff) | |
download | cryptography-6ec1b7bb991a123ed131f138c24eee2c6c18b839.tar.gz cryptography-6ec1b7bb991a123ed131f138c24eee2c6c18b839.tar.bz2 cryptography-6ec1b7bb991a123ed131f138c24eee2c6c18b839.zip |
Merge pull request #1422 from alex/multi-fernet
Fixes #1327 -- adds multifernet
Diffstat (limited to 'tests/test_fernet.py')
-rw-r--r-- | tests/test_fernet.py | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/tests/test_fernet.py b/tests/test_fernet.py index 0b4e3e87..5c630b9e 100644 --- a/tests/test_fernet.py +++ b/tests/test_fernet.py @@ -24,7 +24,7 @@ import pytest import six -from cryptography.fernet import Fernet, InvalidToken +from cryptography.fernet import Fernet, InvalidToken, MultiFernet from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.ciphers import algorithms, modes @@ -115,3 +115,37 @@ class TestFernet(object): def test_bad_key(self, backend): with pytest.raises(ValueError): Fernet(base64.urlsafe_b64encode(b"abc"), backend=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", +) +class TestMultiFernet(object): + def test_encrypt(self, backend): + f1 = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend) + f2 = Fernet(base64.urlsafe_b64encode(b"\x01" * 32), backend=backend) + f = MultiFernet([f1, f2]) + + assert f1.decrypt(f.encrypt(b"abc")) == b"abc" + + def test_decrypt(self, backend): + f1 = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend) + f2 = Fernet(base64.urlsafe_b64encode(b"\x01" * 32), backend=backend) + f = MultiFernet([f1, f2]) + + assert f.decrypt(f1.encrypt(b"abc")) == b"abc" + assert f.decrypt(f2.encrypt(b"abc")) == b"abc" + + with pytest.raises(InvalidToken): + f.decrypt(b"\x00" * 16) + + def test_no_fernets(self, backend): + with pytest.raises(ValueError): + MultiFernet([]) + + def test_non_iterable_argument(self, backend): + with pytest.raises(TypeError): + MultiFernet(None) |