From 87899d4fd55881a4dbec1ee32b79a5bd315ec33f Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 30 Jan 2014 11:58:40 -0800 Subject: Direct tests for the hash support --- tests/hazmat/backends/test_multibackend.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'tests/hazmat/backends') diff --git a/tests/hazmat/backends/test_multibackend.py b/tests/hazmat/backends/test_multibackend.py index dc58a585..f300b6c8 100644 --- a/tests/hazmat/backends/test_multibackend.py +++ b/tests/hazmat/backends/test_multibackend.py @@ -15,6 +15,7 @@ import pytest from cryptography.exceptions import UnsupportedAlgorithm from cryptography.hazmat.backends.multibackend import PrioritizedMultiBackend +from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes @@ -34,6 +35,19 @@ class DummyCipherBackend(object): raise UnsupportedAlgorithm +class DummyHashBackend(object): + def __init__(self, supported_algorithms): + self._algorithms = supported_algorithms + + def hash_supported(self, algorithm): + return type(algorithm) in self._algorithms + + def create_hash_ctx(self, algorithm): + if not self.hash_supported(algorithm): + raise UnsupportedAlgorithm + + + class TestPrioritizedMultiBackend(object): def test_ciphers(self): backend = PrioritizedMultiBackend([ @@ -62,3 +76,14 @@ class TestPrioritizedMultiBackend(object): cipher.encryptor() with pytest.raises(UnsupportedAlgorithm): cipher.decryptor() + + def test_hashes(self): + backend = PrioritizedMultiBackend([ + DummyHashBackend([hashes.MD5]) + ]) + assert backend.hash_supported(hashes.MD5()) + + hashes.Hash(hashes.MD5(), backend=backend) + + with pytest.raises(UnsupportedAlgorithm): + hashes.Hash(hashes.SHA1(), backend=backend) -- cgit v1.2.3