diff options
author | Donald Stufft <donald@stufft.io> | 2013-10-16 15:07:30 -0700 |
---|---|---|
committer | Donald Stufft <donald@stufft.io> | 2013-10-16 15:07:30 -0700 |
commit | 9a76847c67f76184afbd5274d55ac55c34e06dd2 (patch) | |
tree | 0169b36528a7ef6fddcd3f5c097e0083f84f80ec /tests/primitives/utils.py | |
parent | b98118f59f1f6ba79f3b5cdd705ebc56f9ec9f34 (diff) | |
parent | 745c95c75cc6588ecd9b927e5974bf00426125a2 (diff) | |
download | cryptography-9a76847c67f76184afbd5274d55ac55c34e06dd2.tar.gz cryptography-9a76847c67f76184afbd5274d55ac55c34e06dd2.tar.bz2 cryptography-9a76847c67f76184afbd5274d55ac55c34e06dd2.zip |
Merge pull request #106 from alex/duplication-reduction
Remove much of the duplication found in the tests
Diffstat (limited to 'tests/primitives/utils.py')
-rw-r--r-- | tests/primitives/utils.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/primitives/utils.py b/tests/primitives/utils.py new file mode 100644 index 00000000..3cf08c28 --- /dev/null +++ b/tests/primitives/utils.py @@ -0,0 +1,42 @@ +import binascii +import os + +import pytest + +from cryptography.bindings import _ALL_APIS +from cryptography.primitives.block import BlockCipher + + +def generate_encrypt_test(param_loader, path, file_names, cipher_factory, + mode_factory, only_if=lambda api: True, + skip_message=None): + def test_encryption(self): + for api in _ALL_APIS: + for file_name in file_names: + for params in param_loader(os.path.join(path, file_name)): + yield ( + encrypt_test, + api, + cipher_factory, + mode_factory, + params, + only_if, + skip_message + ) + return test_encryption + + +def encrypt_test(api, cipher_factory, mode_factory, params, only_if, + skip_message): + if not only_if(api): + pytest.skip(skip_message) + plaintext = params.pop("plaintext") + ciphertext = params.pop("ciphertext") + cipher = BlockCipher( + cipher_factory(**params), + mode_factory(**params), + api + ) + actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) + actual_ciphertext += cipher.finalize() + assert actual_ciphertext == binascii.unhexlify(ciphertext) |