From 1b1327cfe537b9e7bdc271239d1025c2479239c3 Mon Sep 17 00:00:00 2001 From: Alex Stapleton Date: Sat, 21 Dec 2013 15:16:57 +0000 Subject: Raise UnsupportedAlgorithm when initing Hash() Instead of just an AssertionError. --- tests/hazmat/primitives/test_hashes.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'tests/hazmat/primitives/test_hashes.py') diff --git a/tests/hazmat/primitives/test_hashes.py b/tests/hazmat/primitives/test_hashes.py index ff42e8f4..72bc3e27 100644 --- a/tests/hazmat/primitives/test_hashes.py +++ b/tests/hazmat/primitives/test_hashes.py @@ -19,12 +19,18 @@ import pytest import six -from cryptography.exceptions import AlreadyFinalized -from cryptography.hazmat.primitives import hashes +from cryptography import utils +from cryptography.exceptions import AlreadyFinalized, UnsupportedAlgorithm +from cryptography.hazmat.primitives import hashes, interfaces from .utils import generate_base_hash_test +@utils.register_interface(interfaces.HashAlgorithm) +class UnsupportedDummyHash(object): + name = "unsupported-dummy-hash" + + class TestHashContext(object): def test_hash_reject_unicode(self, backend): m = hashes.Hash(hashes.SHA1(), backend=backend) @@ -57,6 +63,10 @@ class TestHashContext(object): with pytest.raises(AlreadyFinalized): h.finalize() + def test_unsupported_hash(self, backend): + with pytest.raises(UnsupportedAlgorithm): + hashes.Hash(UnsupportedDummyHash(), backend) + class TestSHA1(object): test_SHA1 = generate_base_hash_test( -- cgit v1.2.3 From 4f2b1031c3c155b9af3817126b9ac508cbf849a3 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Tue, 24 Dec 2013 22:24:31 -0600 Subject: add all the markers to the tests --- tests/hazmat/primitives/test_hashes.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'tests/hazmat/primitives/test_hashes.py') diff --git a/tests/hazmat/primitives/test_hashes.py b/tests/hazmat/primitives/test_hashes.py index 72bc3e27..45faaab2 100644 --- a/tests/hazmat/primitives/test_hashes.py +++ b/tests/hazmat/primitives/test_hashes.py @@ -31,6 +31,7 @@ class UnsupportedDummyHash(object): name = "unsupported-dummy-hash" +@pytest.mark.hash class TestHashContext(object): def test_hash_reject_unicode(self, backend): m = hashes.Hash(hashes.SHA1(), backend=backend) @@ -68,6 +69,7 @@ class TestHashContext(object): hashes.Hash(UnsupportedDummyHash(), backend) +@pytest.mark.hash class TestSHA1(object): test_SHA1 = generate_base_hash_test( hashes.SHA1(), @@ -78,6 +80,7 @@ class TestSHA1(object): ) +@pytest.mark.hash class TestSHA224(object): test_SHA224 = generate_base_hash_test( hashes.SHA224(), @@ -88,6 +91,7 @@ class TestSHA224(object): ) +@pytest.mark.hash class TestSHA256(object): test_SHA256 = generate_base_hash_test( hashes.SHA256(), @@ -98,6 +102,7 @@ class TestSHA256(object): ) +@pytest.mark.hash class TestSHA384(object): test_SHA384 = generate_base_hash_test( hashes.SHA384(), @@ -108,6 +113,7 @@ class TestSHA384(object): ) +@pytest.mark.hash class TestSHA512(object): test_SHA512 = generate_base_hash_test( hashes.SHA512(), @@ -118,6 +124,7 @@ class TestSHA512(object): ) +@pytest.mark.hash class TestRIPEMD160(object): test_RIPEMD160 = generate_base_hash_test( hashes.RIPEMD160(), @@ -128,6 +135,7 @@ class TestRIPEMD160(object): ) +@pytest.mark.hash class TestWhirlpool(object): test_Whirlpool = generate_base_hash_test( hashes.Whirlpool(), @@ -138,6 +146,7 @@ class TestWhirlpool(object): ) +@pytest.mark.hash class TestMD5(object): test_MD5 = generate_base_hash_test( hashes.MD5(), -- cgit v1.2.3 From 783479c6189d788ac2f721b5b017073736c578cb Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Thu, 26 Dec 2013 21:08:45 -0600 Subject: refactor all tests to use mark instead of generator skips --- tests/hazmat/primitives/test_hashes.py | 95 ++++++++++++++-------------------- 1 file changed, 40 insertions(+), 55 deletions(-) (limited to 'tests/hazmat/primitives/test_hashes.py') diff --git a/tests/hazmat/primitives/test_hashes.py b/tests/hazmat/primitives/test_hashes.py index 45faaab2..d52021eb 100644 --- a/tests/hazmat/primitives/test_hashes.py +++ b/tests/hazmat/primitives/test_hashes.py @@ -23,7 +23,7 @@ from cryptography import utils from cryptography.exceptions import AlreadyFinalized, UnsupportedAlgorithm from cryptography.hazmat.primitives import hashes, interfaces -from .utils import generate_base_hash_test +from .utils import base_hash_test @utils.register_interface(interfaces.HashAlgorithm) @@ -70,88 +70,73 @@ class TestHashContext(object): @pytest.mark.hash -class TestSHA1(object): - test_SHA1 = generate_base_hash_test( - hashes.SHA1(), - digest_size=20, - block_size=64, +class TestHashes(object): + @pytest.mark.supported( only_if=lambda backend: backend.hash_supported(hashes.SHA1), skip_message="Does not support SHA1", ) + def test_SHA1(self, backend): + base_hash_test(backend, hashes.SHA1(), digest_size=20, block_size=64) - -@pytest.mark.hash -class TestSHA224(object): - test_SHA224 = generate_base_hash_test( - hashes.SHA224(), - digest_size=28, - block_size=64, + @pytest.mark.supported( only_if=lambda backend: backend.hash_supported(hashes.SHA224), skip_message="Does not support SHA224", ) + def test_SHA224(self, backend): + base_hash_test(backend, hashes.SHA224(), digest_size=28, block_size=64) - -@pytest.mark.hash -class TestSHA256(object): - test_SHA256 = generate_base_hash_test( - hashes.SHA256(), - digest_size=32, - block_size=64, + @pytest.mark.supported( only_if=lambda backend: backend.hash_supported(hashes.SHA256), skip_message="Does not support SHA256", ) + def test_SHA256(self, backend): + base_hash_test(backend, hashes.SHA256(), digest_size=32, block_size=64) - -@pytest.mark.hash -class TestSHA384(object): - test_SHA384 = generate_base_hash_test( - hashes.SHA384(), - digest_size=48, - block_size=128, + @pytest.mark.supported( only_if=lambda backend: backend.hash_supported(hashes.SHA384), skip_message="Does not support SHA384", ) + def test_SHA384(self, backend): + base_hash_test(backend, hashes.SHA384(), + digest_size=48, block_size=128) - -@pytest.mark.hash -class TestSHA512(object): - test_SHA512 = generate_base_hash_test( - hashes.SHA512(), - digest_size=64, - block_size=128, + @pytest.mark.supported( only_if=lambda backend: backend.hash_supported(hashes.SHA512), skip_message="Does not support SHA512", ) + def test_SHA512(self, backend): + base_hash_test(backend, hashes.SHA512(), + digest_size=64, block_size=128) +@pytest.mark.supported( + only_if=lambda backend: backend.hash_supported(hashes.RIPEMD160), + skip_message="Does not support RIPEMD160", +) @pytest.mark.hash class TestRIPEMD160(object): - test_RIPEMD160 = generate_base_hash_test( - hashes.RIPEMD160(), - digest_size=20, - block_size=64, - only_if=lambda backend: backend.hash_supported(hashes.RIPEMD160), - skip_message="Does not support RIPEMD160", - ) + def test_RIPEMD160(self, backend): + base_hash_test(backend, hashes.RIPEMD160(), + digest_size=20, block_size=64) +@pytest.mark.supported( + only_if=lambda backend: backend.hash_supported(hashes.Whirlpool), + skip_message="Does not support Whirlpool", +) @pytest.mark.hash class TestWhirlpool(object): - test_Whirlpool = generate_base_hash_test( - hashes.Whirlpool(), - digest_size=64, - block_size=64, - only_if=lambda backend: backend.hash_supported(hashes.Whirlpool), - skip_message="Does not support Whirlpool", - ) + def test_Whirlpool(self, backend): + base_hash_test(backend, hashes.Whirlpool(), + digest_size=64, block_size=64) +@pytest.mark.supported( + only_if=lambda backend: backend.hash_supported(hashes.MD5), + skip_message="Does not support MD5", +) @pytest.mark.hash class TestMD5(object): - test_MD5 = generate_base_hash_test( - hashes.MD5(), - digest_size=16, - block_size=64, - only_if=lambda backend: backend.hash_supported(hashes.MD5), - skip_message="Does not support MD5", - ) + def test_MD5(self, backend): + base_hash_test(backend, hashes.MD5(), + digest_size=16, block_size=64) -- cgit v1.2.3 From b078d8e1f446a1d2d13453e65e37fbaf4f6b17f2 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 27 Dec 2013 16:33:14 -0600 Subject: re-add some removed generators to simplify patch --- tests/hazmat/primitives/test_hashes.py | 107 +++++++++++++++++++++------------ 1 file changed, 69 insertions(+), 38 deletions(-) (limited to 'tests/hazmat/primitives/test_hashes.py') diff --git a/tests/hazmat/primitives/test_hashes.py b/tests/hazmat/primitives/test_hashes.py index d52021eb..c907ef61 100644 --- a/tests/hazmat/primitives/test_hashes.py +++ b/tests/hazmat/primitives/test_hashes.py @@ -23,7 +23,7 @@ from cryptography import utils from cryptography.exceptions import AlreadyFinalized, UnsupportedAlgorithm from cryptography.hazmat.primitives import hashes, interfaces -from .utils import base_hash_test +from .utils import generate_base_hash_test @utils.register_interface(interfaces.HashAlgorithm) @@ -69,44 +69,69 @@ class TestHashContext(object): hashes.Hash(UnsupportedDummyHash(), backend) +@pytest.mark.supported( + only_if=lambda backend: backend.hash_supported(hashes.SHA1), + skip_message="Does not support SHA1", +) @pytest.mark.hash -class TestHashes(object): - @pytest.mark.supported( - only_if=lambda backend: backend.hash_supported(hashes.SHA1), - skip_message="Does not support SHA1", +class TestSHA1(object): + test_SHA1 = generate_base_hash_test( + hashes.SHA1(), + digest_size=20, + block_size=64, ) - def test_SHA1(self, backend): - base_hash_test(backend, hashes.SHA1(), digest_size=20, block_size=64) - @pytest.mark.supported( - only_if=lambda backend: backend.hash_supported(hashes.SHA224), - skip_message="Does not support SHA224", + +@pytest.mark.supported( + only_if=lambda backend: backend.hash_supported(hashes.SHA224), + skip_message="Does not support SHA224", +) +@pytest.mark.hash +class TestSHA224(object): + test_SHA224 = generate_base_hash_test( + hashes.SHA224(), + digest_size=28, + block_size=64, ) - def test_SHA224(self, backend): - base_hash_test(backend, hashes.SHA224(), digest_size=28, block_size=64) - @pytest.mark.supported( - only_if=lambda backend: backend.hash_supported(hashes.SHA256), - skip_message="Does not support SHA256", + +@pytest.mark.supported( + only_if=lambda backend: backend.hash_supported(hashes.SHA256), + skip_message="Does not support SHA256", +) +@pytest.mark.hash +class TestSHA256(object): + test_SHA256 = generate_base_hash_test( + hashes.SHA256(), + digest_size=32, + block_size=64, ) - def test_SHA256(self, backend): - base_hash_test(backend, hashes.SHA256(), digest_size=32, block_size=64) - @pytest.mark.supported( - only_if=lambda backend: backend.hash_supported(hashes.SHA384), - skip_message="Does not support SHA384", + +@pytest.mark.supported( + only_if=lambda backend: backend.hash_supported(hashes.SHA384), + skip_message="Does not support SHA384", +) +@pytest.mark.hash +class TestSHA384(object): + test_SHA384 = generate_base_hash_test( + hashes.SHA384(), + digest_size=48, + block_size=128, ) - def test_SHA384(self, backend): - base_hash_test(backend, hashes.SHA384(), - digest_size=48, block_size=128) - @pytest.mark.supported( - only_if=lambda backend: backend.hash_supported(hashes.SHA512), - skip_message="Does not support SHA512", + +@pytest.mark.supported( + only_if=lambda backend: backend.hash_supported(hashes.SHA512), + skip_message="Does not support SHA512", +) +@pytest.mark.hash +class TestSHA512(object): + test_SHA512 = generate_base_hash_test( + hashes.SHA512(), + digest_size=64, + block_size=128, ) - def test_SHA512(self, backend): - base_hash_test(backend, hashes.SHA512(), - digest_size=64, block_size=128) @pytest.mark.supported( @@ -115,9 +140,11 @@ class TestHashes(object): ) @pytest.mark.hash class TestRIPEMD160(object): - def test_RIPEMD160(self, backend): - base_hash_test(backend, hashes.RIPEMD160(), - digest_size=20, block_size=64) + test_RIPEMD160 = generate_base_hash_test( + hashes.RIPEMD160(), + digest_size=20, + block_size=64, + ) @pytest.mark.supported( @@ -126,9 +153,11 @@ class TestRIPEMD160(object): ) @pytest.mark.hash class TestWhirlpool(object): - def test_Whirlpool(self, backend): - base_hash_test(backend, hashes.Whirlpool(), - digest_size=64, block_size=64) + test_Whirlpool = generate_base_hash_test( + hashes.Whirlpool(), + digest_size=64, + block_size=64, + ) @pytest.mark.supported( @@ -137,6 +166,8 @@ class TestWhirlpool(object): ) @pytest.mark.hash class TestMD5(object): - def test_MD5(self, backend): - base_hash_test(backend, hashes.MD5(), - digest_size=16, block_size=64) + test_MD5 = generate_base_hash_test( + hashes.MD5(), + digest_size=16, + block_size=64, + ) -- cgit v1.2.3