Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/cryptography/hazmat/backends/multibackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class MultiBackend(object):
name = "multibackend"

def __init__(self, backends):
if len(backends) == 0:
raise ValueError(
"Multibackend cannot be initialized with no backends. If you "
"are seeing this error when trying to use default_backend() "
"please try uninstalling and reinstalling cryptography."
)

self._backends = backends

def _filtered_backends(self, interface):
Expand Down
20 changes: 15 additions & 5 deletions tests/hazmat/backends/test_multibackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from __future__ import absolute_import, division, print_function

import pytest

from cryptography import utils
from cryptography.exceptions import (
UnsupportedAlgorithm, _Reasons
Expand All @@ -21,6 +23,10 @@
from ...utils import raises_unsupported_algorithm


class DummyBackend(object):
pass


@utils.register_interface(CipherBackend)
class DummyCipherBackend(object):
def __init__(self, supported_ciphers):
Expand Down Expand Up @@ -226,6 +232,10 @@ def create_x509_revoked_certificate(self, builder):


class TestMultiBackend(object):
def test_raises_error_with_empty_list(self):
with pytest.raises(ValueError):
MultiBackend([])

def test_ciphers(self):
backend = MultiBackend([
DummyHashBackend([]),
Expand Down Expand Up @@ -310,7 +320,7 @@ def test_rsa(self):

backend.load_rsa_public_numbers("public_numbers")

backend = MultiBackend([])
backend = MultiBackend([DummyBackend()])
with raises_unsupported_algorithm(
_Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM
):
Expand Down Expand Up @@ -353,7 +363,7 @@ def test_dsa(self):
backend.load_dsa_public_numbers("numbers")
backend.load_dsa_parameter_numbers("numbers")

backend = MultiBackend([])
backend = MultiBackend([DummyBackend()])
with raises_unsupported_algorithm(
_Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM
):
Expand Down Expand Up @@ -491,7 +501,7 @@ def test_pem_serialization_backend(self):
backend.load_pem_private_key(b"keydata", None)
backend.load_pem_public_key(b"keydata")

backend = MultiBackend([])
backend = MultiBackend([DummyBackend()])
with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_SERIALIZATION):
backend.load_pem_private_key(b"keydata", None)
with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_SERIALIZATION):
Expand All @@ -503,7 +513,7 @@ def test_der_serialization_backend(self):
backend.load_der_private_key(b"keydata", None)
backend.load_der_public_key(b"keydata")

backend = MultiBackend([])
backend = MultiBackend([DummyBackend()])
with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_SERIALIZATION):
backend.load_der_private_key(b"keydata", None)
with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_SERIALIZATION):
Expand All @@ -523,7 +533,7 @@ def test_x509_backend(self):
backend.create_x509_crl(object(), b"privatekey", hashes.SHA1())
backend.create_x509_revoked_certificate(object())

backend = MultiBackend([])
backend = MultiBackend([DummyBackend()])
with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_X509):
backend.load_pem_x509_certificate(b"certdata")
with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_X509):
Expand Down