aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorAyrx <terrycwk1994@gmail.com>2014-04-22 18:05:04 +0800
committerAyrx <terrycwk1994@gmail.com>2014-04-22 23:32:52 +0800
commit771fc77eb3f3e0d43471c6bde39a194bb17affbb (patch)
treec6d7ebd201f93d4824dfad6f78d1c4e63aac533b /tests
parent26d959668ff0a651b7637d0bad11854cc6788ee2 (diff)
downloadcryptography-771fc77eb3f3e0d43471c6bde39a194bb17affbb.tar.gz
cryptography-771fc77eb3f3e0d43471c6bde39a194bb17affbb.tar.bz2
cryptography-771fc77eb3f3e0d43471c6bde39a194bb17affbb.zip
Added tests for multibackend
Diffstat (limited to 'tests')
-rw-r--r--tests/hazmat/backends/test_multibackend.py36
1 files changed, 33 insertions, 3 deletions
diff --git a/tests/hazmat/backends/test_multibackend.py b/tests/hazmat/backends/test_multibackend.py
index f46009d4..2fa9f758 100644
--- a/tests/hazmat/backends/test_multibackend.py
+++ b/tests/hazmat/backends/test_multibackend.py
@@ -13,16 +13,18 @@
from __future__ import absolute_import, division, print_function
+import pytest
+
from cryptography import utils
from cryptography.exceptions import (
UnsupportedAlgorithm, _Reasons
)
from cryptography.hazmat.backends.interfaces import (
- CipherBackend, DSABackend, HMACBackend, HashBackend, PBKDF2HMACBackend,
- RSABackend
+ CMACBackend, CipherBackend, DSABackend, HMACBackend, HashBackend,
+ PBKDF2HMACBackend, RSABackend
)
from cryptography.hazmat.backends.multibackend import MultiBackend
-from cryptography.hazmat.primitives import hashes, hmac
+from cryptography.hazmat.primitives import cmac, hashes, hmac
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
@@ -108,6 +110,19 @@ class DummyDSABackend(object):
pass
+@utils.register_interface(CMACBackend)
+class DummyCMACBackend(object):
+ def __init__(self, supported_algorithms):
+ self._algorithms = supported_algorithms
+
+ def cmac_algorithm_supported(self, algorithm):
+ return type(algorithm) in self._algorithms
+
+ def create_cmac_ctx(self, algorithm):
+ if not self.cmac_algorithm_supported(algorithm):
+ raise UnsupportedAlgorithm("")
+
+
class TestMultiBackend(object):
def test_ciphers(self):
backend = MultiBackend([
@@ -224,3 +239,18 @@ class TestMultiBackend(object):
_Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM
):
backend.generate_dsa_private_key(parameters)
+
+ def test_cmac(self):
+ backend = MultiBackend([
+ DummyCMACBackend([algorithms.AES])
+ ])
+
+ fake_key = b"\x00" * 16
+
+ assert backend.cmac_algorithm_supported(
+ algorithms.AES(fake_key)) is True
+
+ cmac.CMAC(algorithms.AES(fake_key), backend)
+
+ with pytest.raises(UnsupportedAlgorithm):
+ cmac.CMAC(algorithms.TripleDES(fake_key), backend)