From bde6fb52129909cf319157dba95d65fb557d5013 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 18 Oct 2013 18:08:49 -0500 Subject: Hash Saga Part 3 - API changes + SHA1 support + tests --- tests/primitives/utils.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'tests/primitives/utils.py') 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 -- cgit v1.2.3 From bb069c2fee6460185ee435ea848d80bab2ccec6c Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 18 Oct 2013 19:51:01 -0500 Subject: remove unneeded lambdas from tests --- tests/primitives/utils.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'tests/primitives/utils.py') diff --git a/tests/primitives/utils.py b/tests/primitives/utils.py index f301199c..0d4c0eb3 100644 --- a/tests/primitives/utils.py +++ b/tests/primitives/utils.py @@ -42,7 +42,7 @@ def encrypt_test(api, cipher_factory, mode_factory, params, only_if, assert actual_ciphertext == binascii.unhexlify(ciphertext) -def generate_hash_test(param_loader, path, file_names, hash_factory, +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: @@ -51,7 +51,7 @@ def generate_hash_test(param_loader, path, file_names, hash_factory, yield ( hash_test, api, - hash_factory, + hash_cls, params, only_if, skip_message @@ -59,24 +59,24 @@ def generate_hash_test(param_loader, path, file_names, hash_factory, return test_hash -def hash_test(api, hash_factory, params, only_if, skip_message): +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_factory(api) + m = hash_cls(api=api) m.update(binascii.unhexlify(msg)) assert m.hexdigest() == md.replace(" ", "").lower() -def generate_base_hash_test(hash_factory, digest_size, block_size, +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_factory, + hash_cls, digest_size, block_size, only_if, @@ -85,11 +85,11 @@ def generate_base_hash_test(hash_factory, digest_size, block_size, return test_base_hash -def base_hash_test(api, hash_factory, digest_size, block_size, only_if, +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_factory(api=api) + m = hash_cls(api=api) assert m.digest_size == digest_size assert m.block_size == block_size m_copy = m.copy() -- cgit v1.2.3 From c179407406f0ef5c2b7b5b6316521408ba3803b3 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 18 Oct 2013 21:42:57 -0500 Subject: ripemd160 support + long string hash test * Note that the long string hash test for RIPEMD160 adds a vector in the test. You can verify this vector (for b"a" * 1000000) on the RIPE homepage: http://homes.esat.kuleuven.be/~bosselae/ripemd160.html --- tests/primitives/utils.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'tests/primitives/utils.py') diff --git a/tests/primitives/utils.py b/tests/primitives/utils.py index 0d4c0eb3..8b32700b 100644 --- a/tests/primitives/utils.py +++ b/tests/primitives/utils.py @@ -95,3 +95,26 @@ def base_hash_test(api, hash_cls, digest_size, block_size, only_if, 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=lambda api: True, + 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 not only_if(api): + pytest.skip(skip_message) + m = hash_factory(api) + m.update(b"a" * 1000000) + assert m.hexdigest() == md.lower() -- cgit v1.2.3