Skip to content

Commit e6e4c9a

Browse files
committed
Test some failures
1 parent 42b24e4 commit e6e4c9a

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

test/backend_tests.py

+71
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ def test_snicallback_fires_with_data(self,
485485
and will provide the appropriate data.
486486
"""
487487
callback_args = []
488+
488489
def callback(*args):
489490
callback_args.append(args)
490491
return args[-1]
@@ -510,3 +511,73 @@ def callback(*args):
510511
assert conn_object is server
511512
assert config == server_config
512513
assert name == hostname
514+
515+
@pytest.mark.parametrize('load_chain', CHAIN_LOADERS)
516+
@pytest.mark.parametrize('rval', (None, object()))
517+
def test_snicallback_fails_with_none(self,
518+
server_cert,
519+
ca_cert,
520+
load_chain,
521+
rval):
522+
"""
523+
If the SNI callback returns any non TLSConfiguration value, the
524+
handshake fails.
525+
"""
526+
callback_args = []
527+
528+
def callback(*args):
529+
callback_args.append(args)
530+
return rval
531+
532+
cert_chain = load_chain(self.BACKEND, server_cert)
533+
trust_store = self.BACKEND.trust_store.from_pem_file(ca_cert['cert'])
534+
535+
client_config = pep543.TLSConfiguration(
536+
trust_store=trust_store
537+
)
538+
server_config = pep543.TLSConfiguration(
539+
certificate_chain=cert_chain,
540+
validate_certificates=False,
541+
sni_callback=callback
542+
)
543+
# TODO: This is really overbroad, this error could come from anywhere.
544+
with pytest.raises(pep543.TLSError):
545+
assert_configs_work(
546+
self.BACKEND, client_config, server_config
547+
)
548+
549+
assert callback_args
550+
551+
@pytest.mark.parametrize('load_chain', CHAIN_LOADERS)
552+
def test_snicallback_fails_with_exception(self,
553+
server_cert,
554+
ca_cert,
555+
load_chain):
556+
"""
557+
If the SNI callback raises an exception, the handshake fails.
558+
"""
559+
callback_args = []
560+
561+
def callback(*args):
562+
callback_args.append(args)
563+
raise ValueError("Whoops!")
564+
565+
cert_chain = load_chain(self.BACKEND, server_cert)
566+
trust_store = self.BACKEND.trust_store.from_pem_file(ca_cert['cert'])
567+
568+
client_config = pep543.TLSConfiguration(
569+
trust_store=trust_store
570+
)
571+
server_config = pep543.TLSConfiguration(
572+
certificate_chain=cert_chain,
573+
validate_certificates=False,
574+
sni_callback=callback
575+
)
576+
# TODO: This is really overbroad, this error could come from anywhere.
577+
# We allow either the underlying error or TLSError here.
578+
with pytest.raises((pep543.TLSError, ValueError)):
579+
assert_configs_work(
580+
self.BACKEND, client_config, server_config
581+
)
582+
583+
assert callback_args

0 commit comments

Comments
 (0)