@@ -1847,6 +1847,35 @@ def test__create_stdlib_context(self):
18471847 self .assertEqual (ctx .verify_mode , ssl .CERT_NONE )
18481848 self ._assert_context_options (ctx )
18491849
1850+ def test__create_stdlib_context_check_hostname (self ):
1851+ # gh-114905: check_hostname cannot be combined with CERT_NONE,
1852+ # the default for cert_reqs.
1853+ msg = "Cannot set verify_mode to CERT_NONE when check_hostname"
1854+ with self .assertRaisesRegex (ValueError , msg ):
1855+ ssl ._create_stdlib_context (check_hostname = True )
1856+ with self .assertRaisesRegex (ValueError , msg ):
1857+ ssl ._create_stdlib_context (cert_reqs = ssl .CERT_NONE ,
1858+ check_hostname = True )
1859+
1860+ # Accepted before 3.10 with a legacy protocol.
1861+ if has_tls_protocol ('PROTOCOL_TLSv1_2' ):
1862+ with warnings_helper .check_warnings ():
1863+ with self .assertRaisesRegex (ValueError , msg ):
1864+ ssl ._create_stdlib_context (ssl .PROTOCOL_TLSv1_2 ,
1865+ cert_reqs = ssl .CERT_NONE ,
1866+ check_hostname = True )
1867+
1868+ # cert_reqs=None leaves PROTOCOL_TLS_CLIENT's CERT_REQUIRED.
1869+ ctx = ssl ._create_stdlib_context (cert_reqs = None , check_hostname = True )
1870+ self .assertEqual (ctx .verify_mode , ssl .CERT_REQUIRED )
1871+ self .assertTrue (ctx .check_hostname )
1872+
1873+ # CERT_REQUIRED is covered by test__create_stdlib_context().
1874+ ctx = ssl ._create_stdlib_context (cert_reqs = ssl .CERT_OPTIONAL ,
1875+ check_hostname = True )
1876+ self .assertEqual (ctx .verify_mode , ssl .CERT_OPTIONAL )
1877+ self .assertTrue (ctx .check_hostname )
1878+
18501879 def test_delete_sslobject_attributes (self ):
18511880 # None of the attributes of _ssl._SSLSocket can be deleted.
18521881 ctx = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
0 commit comments