blob: 3cf08c28d7fcdf152d16d86be726f303dcf9aa2a [file] [log] [blame]
Alex Gaynorbd458ae2013-10-16 11:59:30 -07001import binascii
2import os
3
4import pytest
5
Alex Gaynora20631d2013-10-16 14:17:36 -07006from cryptography.bindings import _ALL_APIS
Alex Gaynorbd458ae2013-10-16 11:59:30 -07007from cryptography.primitives.block import BlockCipher
8
9
Alex Gaynor016eed12013-10-16 14:16:04 -070010def generate_encrypt_test(param_loader, path, file_names, cipher_factory,
Alex Gaynor512dd692013-10-16 14:27:52 -070011 mode_factory, only_if=lambda api: True,
12 skip_message=None):
Alex Gaynorbd458ae2013-10-16 11:59:30 -070013 def test_encryption(self):
Alex Gaynora20631d2013-10-16 14:17:36 -070014 for api in _ALL_APIS:
Alex Gaynor512dd692013-10-16 14:27:52 -070015 for file_name in file_names:
16 for params in param_loader(os.path.join(path, file_name)):
17 yield (
18 encrypt_test,
19 api,
20 cipher_factory,
21 mode_factory,
22 params,
23 only_if,
24 skip_message
25 )
Alex Gaynorbd458ae2013-10-16 11:59:30 -070026 return test_encryption
27
28
Alex Gaynor512dd692013-10-16 14:27:52 -070029def encrypt_test(api, cipher_factory, mode_factory, params, only_if,
30 skip_message):
31 if not only_if(api):
32 pytest.skip(skip_message)
Alex Gaynorbd458ae2013-10-16 11:59:30 -070033 plaintext = params.pop("plaintext")
34 ciphertext = params.pop("ciphertext")
35 cipher = BlockCipher(
36 cipher_factory(**params),
37 mode_factory(**params),
38 api
39 )
40 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
41 actual_ciphertext += cipher.finalize()
Alex Gaynorfb39b3f2013-10-16 14:30:59 -070042 assert actual_ciphertext == binascii.unhexlify(ciphertext)