@@ -485,6 +485,7 @@ def test_snicallback_fires_with_data(self,
485
485
and will provide the appropriate data.
486
486
"""
487
487
callback_args = []
488
+
488
489
def callback (* args ):
489
490
callback_args .append (args )
490
491
return args [- 1 ]
@@ -510,3 +511,73 @@ def callback(*args):
510
511
assert conn_object is server
511
512
assert config == server_config
512
513
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