aboutsummaryrefslogtreecommitdiffstats
path: root/tests/hazmat/primitives/test_hmac.py
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2014-01-01 08:11:13 -0800
committerAlex Gaynor <alex.gaynor@gmail.com>2014-01-01 08:11:13 -0800
commit2a160d6a159817dd9d08a84e77d102e328f9af4f (patch)
tree461b7c607367f243bb46996ec16ad7424e48d440 /tests/hazmat/primitives/test_hmac.py
parent62aefffb1396190930074bf04c91459d1536bd0e (diff)
parent522487e5a7dd3004747da85c9f6c53fc5dc4de06 (diff)
downloadcryptography-2a160d6a159817dd9d08a84e77d102e328f9af4f.tar.gz
cryptography-2a160d6a159817dd9d08a84e77d102e328f9af4f.tar.bz2
cryptography-2a160d6a159817dd9d08a84e77d102e328f9af4f.zip
Merge branch 'master' into validate-iv
Conflicts: tests/hazmat/backends/test_openssl.py tests/hazmat/primitives/test_block.py
Diffstat (limited to 'tests/hazmat/primitives/test_hmac.py')
-rw-r--r--tests/hazmat/primitives/test_hmac.py51
1 files changed, 46 insertions, 5 deletions
diff --git a/tests/hazmat/primitives/test_hmac.py b/tests/hazmat/primitives/test_hmac.py
index 992bcb1a..04913af6 100644
--- a/tests/hazmat/primitives/test_hmac.py
+++ b/tests/hazmat/primitives/test_hmac.py
@@ -19,19 +19,33 @@ import pytest
import six
-from cryptography.exceptions import AlreadyFinalized
-from cryptography.hazmat.primitives import hashes, hmac
+from cryptography import utils
+from cryptography.exceptions import (
+ AlreadyFinalized, UnsupportedAlgorithm, InvalidSignature
+)
+from cryptography.hazmat.primitives import hashes, hmac, interfaces
from .utils import generate_base_hmac_test
-class TestHMAC(object):
+@utils.register_interface(interfaces.HashAlgorithm)
+class UnsupportedDummyHash(object):
+ name = "unsupported-dummy-hash"
+
+
+@pytest.mark.supported(
+ only_if=lambda backend: backend.hmac_supported(hashes.MD5),
+ skip_message="Does not support MD5",
+)
+@pytest.mark.hmac
+class TestHMACCopy(object):
test_copy = generate_base_hmac_test(
hashes.MD5(),
- only_if=lambda backend: backend.hash_supported(hashes.MD5),
- skip_message="Does not support MD5",
)
+
+@pytest.mark.hmac
+class TestHMAC(object):
def test_hmac_reject_unicode(self, backend):
h = hmac.HMAC(b"mykey", hashes.SHA1(), backend=backend)
with pytest.raises(TypeError):
@@ -63,3 +77,30 @@ class TestHMAC(object):
with pytest.raises(AlreadyFinalized):
h.finalize()
+
+ def test_verify(self, backend):
+ h = hmac.HMAC(b'', hashes.SHA1(), backend=backend)
+ digest = h.finalize()
+
+ h = hmac.HMAC(b'', hashes.SHA1(), backend=backend)
+ h.verify(digest)
+
+ with pytest.raises(AlreadyFinalized):
+ h.verify(b'')
+
+ def test_invalid_verify(self, backend):
+ h = hmac.HMAC(b'', hashes.SHA1(), backend=backend)
+ with pytest.raises(InvalidSignature):
+ h.verify(b'')
+
+ with pytest.raises(AlreadyFinalized):
+ h.verify(b'')
+
+ def test_verify_reject_unicode(self, backend):
+ h = hmac.HMAC(b'', hashes.SHA1(), backend=backend)
+ with pytest.raises(TypeError):
+ h.verify(six.u(''))
+
+ def test_unsupported_hash(self, backend):
+ with pytest.raises(UnsupportedAlgorithm):
+ hmac.HMAC(b"key", UnsupportedDummyHash(), backend)