blob: addd712355db72ea0ab228f78f39c5cb6a492290 [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,
11 mode_factory, only_if=lambda api: True):
Alex Gaynorbd458ae2013-10-16 11:59:30 -070012 def test_encryption(self):
Alex Gaynora20631d2013-10-16 14:17:36 -070013 for api in _ALL_APIS:
Alex Gaynorbd458ae2013-10-16 11:59:30 -070014 if not only_if(api):
15 yield encrypt_skipped
16 else:
17 for file_name in file_names:
Alex Gaynor016eed12013-10-16 14:16:04 -070018 for params in param_loader(os.path.join(path, file_name)):
Alex Gaynorbd458ae2013-10-16 11:59:30 -070019 yield encrypt_test, api, cipher_factory, mode_factory, params
20 return test_encryption
21
22
23def encrypt_test(api, cipher_factory, mode_factory, params):
24 plaintext = params.pop("plaintext")
25 ciphertext = params.pop("ciphertext")
26 cipher = BlockCipher(
27 cipher_factory(**params),
28 mode_factory(**params),
29 api
30 )
31 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
32 actual_ciphertext += cipher.finalize()
33 assert binascii.hexlify(actual_ciphertext) == ciphertext
34
35
36def encrypt_skipped():
37 pytest.skip("because reasons")