@@ -2246,32 +2246,38 @@ def test_transport_eof(self):
22462246 self .assertRaises (ssl .SSLEOFError , sslobj .read )
22472247
22482248
2249- @requires_tls_version ('TLSv1_3' )
22502249class SimpleBackgroundTestsTLS_1_3 (unittest .TestCase ):
22512250 """Tests that connect to a simple server running in the background."""
22522251
2252+ @requires_tls_version ('TLSv1_3' )
22532253 def setUp (self ):
2254- ciphers = [cipher ['name' ] for cipher in ctx .get_ciphers ()
2254+ server_ctx = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
2255+ ciphers = [cipher ['name' ] for cipher in server_ctx .get_ciphers ()
22552256 if cipher ['protocol' ] == 'TLSv1.3' ]
22562257
2258+ if not ciphers :
2259+ self .skipTest ("No cipher supports TLSv1.3" )
2260+
22572261 self .matching_cipher = ciphers [0 ]
2262+ # Some tests need at least two ciphers.
22582263 self .mismatched_cipher = ciphers [- 1 ]
22592264
2260- self .server_context = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
2261- self .server_context .set_ciphersuites (self .matching_cipher )
2262- self .server_context .load_cert_chain (SIGNED_CERTFILE )
2263- server = ThreadedEchoServer (context = self .server_context )
2265+ server_ctx .set_ciphersuites (self .matching_cipher )
2266+ server_ctx .load_cert_chain (SIGNED_CERTFILE )
2267+ server = ThreadedEchoServer (context = server_ctx )
22642268 self .enterContext (server )
22652269 self .server_addr = (HOST , server .port )
22662270
22672271 def test_ciphersuites (self ):
22682272 # Test unrecognized TLS 1.3 cipher suite name
2269- with self .assertRaisesRegex (ssl .SSLError ,
2270- "No cipher suite can be selected" ):
2271- with socket .socket (socket .AF_INET ) as sock :
2272- s = test_wrap_socket (sock , cert_reqs = ssl .CERT_NONE ,
2273- ciphersuites = "XXX" ,
2274- min_version = ssl .TLSVersion .TLSv1_3 )
2273+ with (
2274+ socket .socket (socket .AF_INET ) as sock ,
2275+ self .assertRaisesRegex (ssl .SSLError ,
2276+ "No cipher suite can be selected" )
2277+ ):
2278+ test_wrap_socket (sock , cert_reqs = ssl .CERT_NONE ,
2279+ ciphersuites = "XXX" ,
2280+ min_version = ssl .TLSVersion .TLSv1_3 )
22752281
22762282 # Test successful TLS 1.3 handshake
22772283 with test_wrap_socket (socket .socket (socket .AF_INET ),
@@ -2281,6 +2287,15 @@ def test_ciphersuites(self):
22812287 s .connect (self .server_addr )
22822288 self .assertEqual (s .cipher ()[0 ], self .matching_cipher )
22832289
2290+ def test_ciphersuite_downgrade (self ):
2291+ with test_wrap_socket (socket .socket (socket .AF_INET ),
2292+ cert_reqs = ssl .CERT_NONE ,
2293+ ciphersuites = self .matching_cipher ,
2294+ min_version = ssl .TLSVersion .TLSv1_2 ,
2295+ max_version = ssl .TLSVersion .TLSv1_2 ) as s :
2296+ s .connect (self .server_addr )
2297+ self .assertEqual (s .cipher ()[1 ], 'TLSv1.2' )
2298+
22842299 def test_ciphersuite_mismatch (self ):
22852300 if self .matching_cipher == self .mismatched_cipher :
22862301 self .skipTest ("Multiple TLS 1.3 ciphers are not available" )
@@ -2289,8 +2304,7 @@ def test_ciphersuite_mismatch(self):
22892304 cert_reqs = ssl .CERT_NONE ,
22902305 ciphersuites = self .mismatched_cipher ,
22912306 min_version = ssl .TLSVersion .TLSv1_3 ) as s :
2292- with self .assertRaises (ssl .SSLError ):
2293- s .connect (self .server_addr )
2307+ self .assertRaises (ssl .SSLError , s .connect , self .server_addr )
22942308
22952309
22962310@support .requires_resource ('network' )
0 commit comments