MCPcopy Create free account
hub / github.com/python/cpython / test_sni_callback_on_dead_references

Method test_sni_callback_on_dead_references

Lib/test/test_ssl.py:1554–1595  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1552 ctx.set_servername_callback(dummycallback)
1553
1554 def test_sni_callback_on_dead_references(self):
1555 # See https://github.com/python/cpython/issues/146080.
1556 c_ctx = make_test_context()
1557 c_inc, c_out = ssl.MemoryBIO(), ssl.MemoryBIO()
1558 client = c_ctx.wrap_bio(c_inc, c_out, server_hostname=SIGNED_CERTFILE_HOSTNAME)
1559
1560 def sni_callback(sock, servername, ctx): pass
1561 sni_callback = unittest.mock.Mock(wraps=sni_callback)
1562 s_ctx = make_test_context(server_side=True, certfile=SIGNED_CERTFILE)
1563 s_ctx.set_servername_callback(sni_callback)
1564
1565 s_inc, s_out = ssl.MemoryBIO(), ssl.MemoryBIO()
1566 server = s_ctx.wrap_bio(s_inc, s_out, server_side=True)
1567 server_impl = server._sslobj
1568
1569 # Perform the handshake on the client side first.
1570 data = do_ssl_object_handshake(client, c_out)
1571 sni_callback.assert_not_called()
1572 if data is None:
1573 self.skipTest("cannot establish a handshake from the client")
1574 s_inc.write(data)
1575 sni_callback.assert_not_called()
1576 # Delete the server object before it starts doing its handshake
1577 # and ensure that we did not call the SNI callback yet.
1578 del server
1579 gc.collect()
1580 # Try to continue the server's handshake by directly using
1581 # the internal SSL object. The latter is a weak reference
1582 # stored in the server context and has now a dead owner.
1583 with self.assertRaises(ssl.SSLError) as cm:
1584 server_impl.do_handshake()
1585 # The SNI C callback raised an exception before calling our callback.
1586 sni_callback.assert_not_called()
1587
1588 # In AWS-LC, any handshake failures reports SSL_R_PARSE_TLSEXT,
1589 # while OpenSSL uses SSL_R_CALLBACK_FAILED on SNI callback failures.
1590 if IS_AWS_LC:
1591 libssl_error_reason = "PARSE_TLSEXT"
1592 else:
1593 libssl_error_reason = "callback failed"
1594 self.assertIn(libssl_error_reason, str(cm.exception))
1595 self.assertEqual(cm.exception.errno, ssl.SSL_ERROR_SSL)
1596
1597 def test_sni_callback_refcycle(self):
1598 # Reference cycles through the servername callback are detected

Callers

nothing calls this directly

Calls 13

make_test_contextFunction · 0.85
do_ssl_object_handshakeFunction · 0.85
strFunction · 0.85
wrap_bioMethod · 0.80
assert_not_calledMethod · 0.80
skipTestMethod · 0.80
assertInMethod · 0.80
writeMethod · 0.45
collectMethod · 0.45
assertRaisesMethod · 0.45
do_handshakeMethod · 0.45

Tested by

no test coverage detected