Skip to content
Prev Previous commit
Next Next commit
Fix tests and comments, server_hostname='' still raises ValueError
  • Loading branch information
sethmlarson committed Sep 30, 2026
commit ff797896110b54d9a59dbe1a6a63d9d55baa331e
14 changes: 10 additions & 4 deletions Lib/test/test_asyncio/test_sslproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ def test_check_hostname_accepts_server_hostname(self):
server_hostname='example.org')
self.addCleanup(ssl_proto._app_transport.close)

@support.subTests("server_hostname", [None, ''])
def test_check_hostname_requires_server_hostname(self, server_hostname):
def test_check_hostname_requires_server_hostname(self):
# A caller-supplied context asking for hostname checking used to be
# taken through wrap_bio() with no name to check against, verifying
# the certificate chain but never the peer's identity.
Expand All @@ -94,10 +93,17 @@ def test_check_hostname_requires_server_hostname(self, server_hostname):
sslcontext.check_hostname = True
app_proto = mock.Mock()
waiter = mock.Mock()
server_hostname = None

# Supplying an empty server_hostname fails with check_hostname enabled.
# Supplying no server_hostname warns with check_hostname enabled.
with self.assertWarnsRegex(
UserWarning,
DeprecationWarning,
'check_hostname requires server_hostname'):
sslproto.SSLProtocol(self.loop, app_proto, sslcontext,
waiter)

with self.assertWarnsRegex(
DeprecationWarning,
'check_hostname requires server_hostname'):
sslproto.SSLProtocol(self.loop, app_proto, sslcontext,
waiter,
Expand Down
38 changes: 20 additions & 18 deletions Lib/test/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,7 @@ def test_bad_server_hostname(self):
with self.assertWarns(DeprecationWarning):
ctx.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO(),
server_hostname=None)
with self.assertRaises(DeprecationWarning):
with self.assertRaises(ValueError):
ctx.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO(),
server_hostname="")
with self.assertRaises(ValueError):
Expand Down Expand Up @@ -1817,30 +1817,32 @@ def test_check_hostname_requires_server_hostname(self):
client_context, _, hostname = testing_context()
self.assertTrue(client_context.check_hostname)

for server_hostname in (None, ""):
with self.subTest(server_hostname=server_hostname):
with self.assertWarnsRegex(
DeprecationWarning,
"check_hostname requires server_hostname"):
client_context.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO(),
server_hostname=server_hostname)
# The sibling constructor refuses the very same call.
with socket.socket() as sock:
with self.assertWarnsRegex(
DeprecationWarning,
"check_hostname requires server_hostname"):
client_context.wrap_socket(
sock, server_hostname=server_hostname)
server_hostname = None
with self.assertWarnsRegex(
DeprecationWarning,
"check_hostname requires server_hostname"):
client_context.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO(),
server_hostname=server_hostname)
# The sibling constructor refuses the very same call, but with
# a ValueError instead of DeprecationWarning.
with socket.socket() as sock:
with self.assertRaisesRegex(
ValueError,
"check_hostname requires server_hostname"):
client_context.wrap_socket(
sock, server_hostname=server_hostname)

# A name was all that was missing.
client_context.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO(),
server_hostname=hostname)
with warnings_helper.check_no_warnings(self):
client_context.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO(),
server_hostname=hostname)

# Asking for no hostname check remains a way to say so explicitly.
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.check_hostname = False
self.assertFalse(context.check_hostname)
context.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO())
with warnings_helper.check_no_warnings(self):
context.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO())

def test_server_side_bad_params(self):
# A server neither sends a hostname nor resumes a client's session,
Expand Down
Loading