diff options
author | Donald Stufft <donald@stufft.io> | 2013-10-18 19:21:27 -0700 |
---|---|---|
committer | Donald Stufft <donald@stufft.io> | 2013-10-18 19:21:27 -0700 |
commit | a3011beae965fde326d4977d850b2aaa9c7b216e (patch) | |
tree | 59095660e7061e238847c3e076d2d115ac716b2f /tests/primitives/utils.py | |
parent | 90d2a77af90e9ed68125668e59b4d1f250938aec (diff) | |
parent | 3b7730cf90c6a5114391d2d5a2ccc1cdb448da9e (diff) | |
download | cryptography-a3011beae965fde326d4977d850b2aaa9c7b216e.tar.gz cryptography-a3011beae965fde326d4977d850b2aaa9c7b216e.tar.bz2 cryptography-a3011beae965fde326d4977d850b2aaa9c7b216e.zip |
Merge pull request #123 from reaperhulk/hash-saga-sha1
Hash Saga Part 3 (API changes + SHA1)
Diffstat (limited to 'tests/primitives/utils.py')
-rw-r--r-- | tests/primitives/utils.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/primitives/utils.py b/tests/primitives/utils.py index 3cf08c28..0d4c0eb3 100644 --- a/tests/primitives/utils.py +++ b/tests/primitives/utils.py @@ -40,3 +40,58 @@ def encrypt_test(api, cipher_factory, mode_factory, params, only_if, actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) actual_ciphertext += cipher.finalize() assert actual_ciphertext == binascii.unhexlify(ciphertext) + + +def generate_hash_test(param_loader, path, file_names, hash_cls, + only_if=lambda api: True, skip_message=None): + def test_hash(self): + for api in _ALL_APIS: + for file_name in file_names: + for params in param_loader(os.path.join(path, file_name)): + yield ( + hash_test, + api, + hash_cls, + params, + only_if, + skip_message + ) + return test_hash + + +def hash_test(api, hash_cls, params, only_if, skip_message): + if not only_if(api): + pytest.skip(skip_message) + msg = params[0] + md = params[1] + m = hash_cls(api=api) + m.update(binascii.unhexlify(msg)) + assert m.hexdigest() == md.replace(" ", "").lower() + + +def generate_base_hash_test(hash_cls, digest_size, block_size, + only_if=lambda api: True, skip_message=None): + def test_base_hash(self): + for api in _ALL_APIS: + yield ( + base_hash_test, + api, + hash_cls, + digest_size, + block_size, + only_if, + skip_message, + ) + return test_base_hash + + +def base_hash_test(api, hash_cls, digest_size, block_size, only_if, + skip_message): + if not only_if(api): + pytest.skip(skip_message) + m = hash_cls(api=api) + assert m.digest_size == digest_size + assert m.block_size == block_size + m_copy = m.copy() + assert m != m_copy + assert m._ctx != m_copy._ctx |