aboutsummaryrefslogtreecommitdiffstats
path: root/tests/primitives/utils.py
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2013-10-18 18:08:49 -0500
committerPaul Kehrer <paul.l.kehrer@gmail.com>2013-10-18 18:08:49 -0500
commitbde6fb52129909cf319157dba95d65fb557d5013 (patch)
treebf8b7f02ec9560fd5446239cc0bd27ac995e3a3f /tests/primitives/utils.py
parent87cd0db396e157b3fb160b8b6fd770e2c661ace2 (diff)
downloadcryptography-bde6fb52129909cf319157dba95d65fb557d5013.tar.gz
cryptography-bde6fb52129909cf319157dba95d65fb557d5013.tar.bz2
cryptography-bde6fb52129909cf319157dba95d65fb557d5013.zip
Hash Saga Part 3 - API changes + SHA1 support + tests
Diffstat (limited to 'tests/primitives/utils.py')
-rw-r--r--tests/primitives/utils.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/primitives/utils.py b/tests/primitives/utils.py
index 3cf08c28..f301199c 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_factory,
+ 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_factory,
+ params,
+ only_if,
+ skip_message
+ )
+ return test_hash
+
+
+def hash_test(api, hash_factory, params, only_if, skip_message):
+ if not only_if(api):
+ pytest.skip(skip_message)
+ msg = params[0]
+ md = params[1]
+ m = hash_factory(api)
+ m.update(binascii.unhexlify(msg))
+ assert m.hexdigest() == md.replace(" ", "").lower()
+
+
+def generate_base_hash_test(hash_factory, 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_factory,
+ digest_size,
+ block_size,
+ only_if,
+ skip_message,
+ )
+ return test_base_hash
+
+
+def base_hash_test(api, hash_factory, digest_size, block_size, only_if,
+ skip_message):
+ if not only_if(api):
+ pytest.skip(skip_message)
+ m = hash_factory(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