From 0929f8ff07e71410bd2ce89d407805bd476c1761 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 30 Jan 2014 15:23:17 -0800 Subject: More direct tests --- tests/hazmat/backends/test_multibackend.py | 50 +++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/hazmat/backends/test_multibackend.py b/tests/hazmat/backends/test_multibackend.py index f300b6c8..03b3187b 100644 --- a/tests/hazmat/backends/test_multibackend.py +++ b/tests/hazmat/backends/test_multibackend.py @@ -15,8 +15,9 @@ import pytest from cryptography.exceptions import UnsupportedAlgorithm from cryptography.hazmat.backends.multibackend import PrioritizedMultiBackend -from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.primitives import hashes, hmac from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes +from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC class DummyCipherBackend(object): @@ -47,6 +48,31 @@ class DummyHashBackend(object): raise UnsupportedAlgorithm +class DummyHMACBackend(object): + def __init__(self, supported_algorithms): + self._algorithms = supported_algorithms + + def hmac_supported(self, algorithm): + return type(algorithm) in self._algorithms + + def create_hmac_ctx(self, key, algorithm): + if not self.hmac_supported(algorithm): + raise UnsupportedAlgorithm + + +class DummyPBKDF2HMAC(object): + def __init__(self, supported_algorithms): + self._algorithms = supported_algorithms + + def pbkdf2_hmac_supported(self, algorithm): + return type(algorithm) in self._algorithms + + def derive_pbkdf2_hmac(self, algorithm, length, salt, iterations, + key_material): + if not self.pbkdf2_hmac_supported(algorithm): + raise UnsupportedAlgorithm + + class TestPrioritizedMultiBackend(object): def test_ciphers(self): @@ -87,3 +113,25 @@ class TestPrioritizedMultiBackend(object): with pytest.raises(UnsupportedAlgorithm): hashes.Hash(hashes.SHA1(), backend=backend) + + def test_hmac(self): + backend = PrioritizedMultiBackend([ + DummyHMACBackend([hashes.MD5]) + ]) + assert backend.hmac_supported(hashes.MD5()) + + hmac.HMAC(b"", hashes.MD5(), backend=backend) + + with pytest.raises(UnsupportedAlgorithm): + hmac.HMAC(b"", hashes.SHA1(), backend=backend) + + def test_pbkdf2(self): + backend = PrioritizedMultiBackend([ + DummyPBKDF2HMAC([hashes.MD5]) + ]) + assert backend.pbkdf2_hmac_supported(hashes.MD5()) + + backend.derive_pbkdf2_hmac(hashes.MD5(), 10, b"", 10, b"") + + with pytest.raises(UnsupportedAlgorithm): + backend.derive_pbkdf2_hmac(hashes.SHA1(), 10, b"", 10, b"") -- cgit v1.2.3