From bd458ae1e3bdd48f74437216ac467ab2e4d68b13 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 16 Oct 2013 11:59:30 -0700 Subject: Missed file --- tests/primitives/utils.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/primitives/utils.py (limited to 'tests/primitives/utils.py') diff --git a/tests/primitives/utils.py b/tests/primitives/utils.py new file mode 100644 index 00000000..e35c915a --- /dev/null +++ b/tests/primitives/utils.py @@ -0,0 +1,40 @@ +import binascii +import os + +import pytest + +from cryptography.bindings import openssl +from cryptography.primitives.block import BlockCipher + + +def generate_encrypt_test(param_loader, cipher_name, vector_type, file_names, + cipher_factory, mode_factory, + only_if=lambda api: True): + def test_encryption(self): + for api in [openssl.api]: + if not only_if(api): + yield encrypt_skipped + else: + for file_name in file_names: + for params in param_loader( + os.path.join(cipher_name, vector_type, file_name) + ): + yield encrypt_test, api, cipher_factory, mode_factory, params + return test_encryption + + +def encrypt_test(api, cipher_factory, mode_factory, params): + 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 binascii.hexlify(actual_ciphertext) == ciphertext + + +def encrypt_skipped(): + pytest.skip("because reasons") -- cgit v1.2.3 From 016eed1cc1cc26ff404ac31ed3858de362ca37f2 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 16 Oct 2013 14:16:04 -0700 Subject: Ported openssl vector tests --- tests/primitives/utils.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'tests/primitives/utils.py') diff --git a/tests/primitives/utils.py b/tests/primitives/utils.py index e35c915a..13bf0d89 100644 --- a/tests/primitives/utils.py +++ b/tests/primitives/utils.py @@ -7,18 +7,15 @@ from cryptography.bindings import openssl from cryptography.primitives.block import BlockCipher -def generate_encrypt_test(param_loader, cipher_name, vector_type, file_names, - cipher_factory, mode_factory, - only_if=lambda api: True): +def generate_encrypt_test(param_loader, path, file_names, cipher_factory, + mode_factory, only_if=lambda api: True): def test_encryption(self): for api in [openssl.api]: if not only_if(api): yield encrypt_skipped else: for file_name in file_names: - for params in param_loader( - os.path.join(cipher_name, vector_type, file_name) - ): + for params in param_loader(os.path.join(path, file_name)): yield encrypt_test, api, cipher_factory, mode_factory, params return test_encryption -- cgit v1.2.3 From a20631d332d47903a85b61c4b68c5398125a3ebe Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 16 Oct 2013 14:17:36 -0700 Subject: Consolidate this list --- tests/primitives/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/primitives/utils.py') diff --git a/tests/primitives/utils.py b/tests/primitives/utils.py index 13bf0d89..addd7123 100644 --- a/tests/primitives/utils.py +++ b/tests/primitives/utils.py @@ -3,14 +3,14 @@ import os import pytest -from cryptography.bindings import openssl +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): def test_encryption(self): - for api in [openssl.api]: + for api in _ALL_APIS: if not only_if(api): yield encrypt_skipped else: -- cgit v1.2.3 From 512dd6925202c5dc6680dd7157a132a9ffc4855f Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 16 Oct 2013 14:27:52 -0700 Subject: Move around the skip logic --- tests/primitives/utils.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'tests/primitives/utils.py') diff --git a/tests/primitives/utils.py b/tests/primitives/utils.py index addd7123..e4cb8da7 100644 --- a/tests/primitives/utils.py +++ b/tests/primitives/utils.py @@ -8,19 +8,28 @@ from cryptography.primitives.block import BlockCipher def generate_encrypt_test(param_loader, path, file_names, cipher_factory, - mode_factory, only_if=lambda api: True): + mode_factory, only_if=lambda api: True, + skip_message=None): def test_encryption(self): for api in _ALL_APIS: - if not only_if(api): - yield encrypt_skipped - else: - 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 + 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): +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( @@ -31,7 +40,3 @@ def encrypt_test(api, cipher_factory, mode_factory, params): actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) actual_ciphertext += cipher.finalize() assert binascii.hexlify(actual_ciphertext) == ciphertext - - -def encrypt_skipped(): - pytest.skip("because reasons") -- cgit v1.2.3 From fb39b3ffc6fcd3df0f89cd3978796a4377335075 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 16 Oct 2013 14:30:59 -0700 Subject: Rewrite to avoid capitalization issues --- tests/primitives/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/primitives/utils.py') diff --git a/tests/primitives/utils.py b/tests/primitives/utils.py index e4cb8da7..3cf08c28 100644 --- a/tests/primitives/utils.py +++ b/tests/primitives/utils.py @@ -39,4 +39,4 @@ def encrypt_test(api, cipher_factory, mode_factory, params, only_if, ) actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) actual_ciphertext += cipher.finalize() - assert binascii.hexlify(actual_ciphertext) == ciphertext + assert actual_ciphertext == binascii.unhexlify(ciphertext) -- cgit v1.2.3