aboutsummaryrefslogtreecommitdiffstats
path: root/tests/primitives/utils.py
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2013-10-21 14:44:42 -0700
committerAlex Gaynor <alex.gaynor@gmail.com>2013-10-21 14:44:42 -0700
commit6f874ff9279376b6962c514ae0e59b90e7082e15 (patch)
treedcbc2e2491bffc6ad19eea41bbeeba85d65a48b3 /tests/primitives/utils.py
parentd6644815913e878462aa51c95c84e6d871406e27 (diff)
parent71ed449ffe7e26e9217b6c44bb27e19996315a64 (diff)
downloadcryptography-6f874ff9279376b6962c514ae0e59b90e7082e15.tar.gz
cryptography-6f874ff9279376b6962c514ae0e59b90e7082e15.tar.bz2
cryptography-6f874ff9279376b6962c514ae0e59b90e7082e15.zip
Merge branch 'master' into refactor-cipher-names
Conflicts: cryptography/bindings/openssl/api.py
Diffstat (limited to 'tests/primitives/utils.py')
-rw-r--r--tests/primitives/utils.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/primitives/utils.py b/tests/primitives/utils.py
index 3cf08c28..a3759b03 100644
--- a/tests/primitives/utils.py
+++ b/tests/primitives/utils.py
@@ -40,3 +40,81 @@ 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=None, 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 only_if is not None and 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=None, 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 only_if is not None and 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
+
+
+def generate_long_string_hash_test(hash_factory, md, only_if=None,
+ skip_message=None):
+ def test_long_string_hash(self):
+ for api in _ALL_APIS:
+ yield(
+ long_string_hash_test,
+ api,
+ hash_factory,
+ md,
+ only_if,
+ skip_message
+ )
+ return test_long_string_hash
+
+
+def long_string_hash_test(api, hash_factory, md, only_if, skip_message):
+ if only_if is not None and not only_if(api):
+ pytest.skip(skip_message)
+ m = hash_factory(api)
+ m.update(b"a" * 1000000)
+ assert m.hexdigest() == md.lower()