mirror of https://github.com/python/cpython
Issue #16923: Fix ResourceWarnings in test_ssl.
This commit is contained in:
parent
457a2292ca
commit
e1ceb50cdf
|
@ -208,20 +208,21 @@ class BasicSocketTests(unittest.TestCase):
|
||||||
s = socket.socket(socket.AF_INET)
|
s = socket.socket(socket.AF_INET)
|
||||||
ss = ssl.wrap_socket(s)
|
ss = ssl.wrap_socket(s)
|
||||||
wr = weakref.ref(ss)
|
wr = weakref.ref(ss)
|
||||||
del ss
|
with support.check_warnings(("", ResourceWarning)):
|
||||||
self.assertEqual(wr(), None)
|
del ss
|
||||||
|
self.assertEqual(wr(), None)
|
||||||
|
|
||||||
def test_wrapped_unconnected(self):
|
def test_wrapped_unconnected(self):
|
||||||
# Methods on an unconnected SSLSocket propagate the original
|
# Methods on an unconnected SSLSocket propagate the original
|
||||||
# socket.error raise by the underlying socket object.
|
# socket.error raise by the underlying socket object.
|
||||||
s = socket.socket(socket.AF_INET)
|
s = socket.socket(socket.AF_INET)
|
||||||
ss = ssl.wrap_socket(s)
|
with ssl.wrap_socket(s) as ss:
|
||||||
self.assertRaises(socket.error, ss.recv, 1)
|
self.assertRaises(socket.error, ss.recv, 1)
|
||||||
self.assertRaises(socket.error, ss.recv_into, bytearray(b'x'))
|
self.assertRaises(socket.error, ss.recv_into, bytearray(b'x'))
|
||||||
self.assertRaises(socket.error, ss.recvfrom, 1)
|
self.assertRaises(socket.error, ss.recvfrom, 1)
|
||||||
self.assertRaises(socket.error, ss.recvfrom_into, bytearray(b'x'), 1)
|
self.assertRaises(socket.error, ss.recvfrom_into, bytearray(b'x'), 1)
|
||||||
self.assertRaises(socket.error, ss.send, b'x')
|
self.assertRaises(socket.error, ss.send, b'x')
|
||||||
self.assertRaises(socket.error, ss.sendto, b'x', ('0.0.0.0', 0))
|
self.assertRaises(socket.error, ss.sendto, b'x', ('0.0.0.0', 0))
|
||||||
|
|
||||||
def test_timeout(self):
|
def test_timeout(self):
|
||||||
# Issue #8524: when creating an SSL socket, the timeout of the
|
# Issue #8524: when creating an SSL socket, the timeout of the
|
||||||
|
@ -229,8 +230,8 @@ class BasicSocketTests(unittest.TestCase):
|
||||||
for timeout in (None, 0.0, 5.0):
|
for timeout in (None, 0.0, 5.0):
|
||||||
s = socket.socket(socket.AF_INET)
|
s = socket.socket(socket.AF_INET)
|
||||||
s.settimeout(timeout)
|
s.settimeout(timeout)
|
||||||
ss = ssl.wrap_socket(s)
|
with ssl.wrap_socket(s) as ss:
|
||||||
self.assertEqual(timeout, ss.gettimeout())
|
self.assertEqual(timeout, ss.gettimeout())
|
||||||
|
|
||||||
def test_errors(self):
|
def test_errors(self):
|
||||||
sock = socket.socket()
|
sock = socket.socket()
|
||||||
|
@ -243,9 +244,9 @@ class BasicSocketTests(unittest.TestCase):
|
||||||
self.assertRaisesRegex(ValueError,
|
self.assertRaisesRegex(ValueError,
|
||||||
"certfile must be specified for server-side operations",
|
"certfile must be specified for server-side operations",
|
||||||
ssl.wrap_socket, sock, server_side=True, certfile="")
|
ssl.wrap_socket, sock, server_side=True, certfile="")
|
||||||
s = ssl.wrap_socket(sock, server_side=True, certfile=CERTFILE)
|
with ssl.wrap_socket(sock, server_side=True, certfile=CERTFILE) as s:
|
||||||
self.assertRaisesRegex(ValueError, "can't connect in server-side mode",
|
self.assertRaisesRegex(ValueError, "can't connect in server-side mode",
|
||||||
s.connect, (HOST, 8080))
|
s.connect, (HOST, 8080))
|
||||||
with self.assertRaises(IOError) as cm:
|
with self.assertRaises(IOError) as cm:
|
||||||
with socket.socket() as sock:
|
with socket.socket() as sock:
|
||||||
ssl.wrap_socket(sock, certfile=WRONGCERT)
|
ssl.wrap_socket(sock, certfile=WRONGCERT)
|
||||||
|
@ -358,21 +359,21 @@ class BasicSocketTests(unittest.TestCase):
|
||||||
def test_unknown_channel_binding(self):
|
def test_unknown_channel_binding(self):
|
||||||
# should raise ValueError for unknown type
|
# should raise ValueError for unknown type
|
||||||
s = socket.socket(socket.AF_INET)
|
s = socket.socket(socket.AF_INET)
|
||||||
ss = ssl.wrap_socket(s)
|
with ssl.wrap_socket(s) as ss:
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
ss.get_channel_binding("unknown-type")
|
ss.get_channel_binding("unknown-type")
|
||||||
|
|
||||||
@unittest.skipUnless("tls-unique" in ssl.CHANNEL_BINDING_TYPES,
|
@unittest.skipUnless("tls-unique" in ssl.CHANNEL_BINDING_TYPES,
|
||||||
"'tls-unique' channel binding not available")
|
"'tls-unique' channel binding not available")
|
||||||
def test_tls_unique_channel_binding(self):
|
def test_tls_unique_channel_binding(self):
|
||||||
# unconnected should return None for known type
|
# unconnected should return None for known type
|
||||||
s = socket.socket(socket.AF_INET)
|
s = socket.socket(socket.AF_INET)
|
||||||
ss = ssl.wrap_socket(s)
|
with ssl.wrap_socket(s) as ss:
|
||||||
self.assertIsNone(ss.get_channel_binding("tls-unique"))
|
self.assertIsNone(ss.get_channel_binding("tls-unique"))
|
||||||
# the same for server-side
|
# the same for server-side
|
||||||
s = socket.socket(socket.AF_INET)
|
s = socket.socket(socket.AF_INET)
|
||||||
ss = ssl.wrap_socket(s, server_side=True, certfile=CERTFILE)
|
with ssl.wrap_socket(s, server_side=True, certfile=CERTFILE) as ss:
|
||||||
self.assertIsNone(ss.get_channel_binding("tls-unique"))
|
self.assertIsNone(ss.get_channel_binding("tls-unique"))
|
||||||
|
|
||||||
def test_dealloc_warn(self):
|
def test_dealloc_warn(self):
|
||||||
ss = ssl.wrap_socket(socket.socket(socket.AF_INET))
|
ss = ssl.wrap_socket(socket.socket(socket.AF_INET))
|
||||||
|
@ -623,10 +624,10 @@ class SSLErrorTests(unittest.TestCase):
|
||||||
with socket.socket() as s:
|
with socket.socket() as s:
|
||||||
s.bind(("127.0.0.1", 0))
|
s.bind(("127.0.0.1", 0))
|
||||||
s.listen(5)
|
s.listen(5)
|
||||||
with socket.socket() as c:
|
c = socket.socket()
|
||||||
c.connect(s.getsockname())
|
c.connect(s.getsockname())
|
||||||
c.setblocking(False)
|
c.setblocking(False)
|
||||||
c = ctx.wrap_socket(c, False, do_handshake_on_connect=False)
|
with ctx.wrap_socket(c, False, do_handshake_on_connect=False) as c:
|
||||||
with self.assertRaises(ssl.SSLWantReadError) as cm:
|
with self.assertRaises(ssl.SSLWantReadError) as cm:
|
||||||
c.do_handshake()
|
c.do_handshake()
|
||||||
s = str(cm.exception)
|
s = str(cm.exception)
|
||||||
|
@ -867,12 +868,12 @@ class NetworkedTests(unittest.TestCase):
|
||||||
def test_ciphers(self):
|
def test_ciphers(self):
|
||||||
remote = ("svn.python.org", 443)
|
remote = ("svn.python.org", 443)
|
||||||
with support.transient_internet(remote[0]):
|
with support.transient_internet(remote[0]):
|
||||||
s = ssl.wrap_socket(socket.socket(socket.AF_INET),
|
with ssl.wrap_socket(socket.socket(socket.AF_INET),
|
||||||
cert_reqs=ssl.CERT_NONE, ciphers="ALL")
|
cert_reqs=ssl.CERT_NONE, ciphers="ALL") as s:
|
||||||
s.connect(remote)
|
s.connect(remote)
|
||||||
s = ssl.wrap_socket(socket.socket(socket.AF_INET),
|
with ssl.wrap_socket(socket.socket(socket.AF_INET),
|
||||||
cert_reqs=ssl.CERT_NONE, ciphers="DEFAULT")
|
cert_reqs=ssl.CERT_NONE, ciphers="DEFAULT") as s:
|
||||||
s.connect(remote)
|
s.connect(remote)
|
||||||
# Error checking can happen at instantiation or when connecting
|
# Error checking can happen at instantiation or when connecting
|
||||||
with self.assertRaisesRegex(ssl.SSLError, "No cipher can be selected"):
|
with self.assertRaisesRegex(ssl.SSLError, "No cipher can be selected"):
|
||||||
with socket.socket(socket.AF_INET) as sock:
|
with socket.socket(socket.AF_INET) as sock:
|
||||||
|
@ -1847,6 +1848,8 @@ else:
|
||||||
client_addr = client.getsockname()
|
client_addr = client.getsockname()
|
||||||
client.close()
|
client.close()
|
||||||
t.join()
|
t.join()
|
||||||
|
remote.close()
|
||||||
|
server.close()
|
||||||
# Sanity checks.
|
# Sanity checks.
|
||||||
self.assertIsInstance(remote, ssl.SSLSocket)
|
self.assertIsInstance(remote, ssl.SSLSocket)
|
||||||
self.assertEqual(peer, client_addr)
|
self.assertEqual(peer, client_addr)
|
||||||
|
@ -1861,8 +1864,7 @@ else:
|
||||||
with ThreadedEchoServer(CERTFILE,
|
with ThreadedEchoServer(CERTFILE,
|
||||||
ssl_version=ssl.PROTOCOL_SSLv23,
|
ssl_version=ssl.PROTOCOL_SSLv23,
|
||||||
chatty=False) as server:
|
chatty=False) as server:
|
||||||
with socket.socket() as sock:
|
with context.wrap_socket(socket.socket()) as s:
|
||||||
s = context.wrap_socket(sock)
|
|
||||||
with self.assertRaises((OSError, ssl.SSLError)):
|
with self.assertRaises((OSError, ssl.SSLError)):
|
||||||
s.connect((HOST, server.port))
|
s.connect((HOST, server.port))
|
||||||
self.assertIn("no shared cipher", str(server.conn_errors[0]))
|
self.assertIn("no shared cipher", str(server.conn_errors[0]))
|
||||||
|
|
|
@ -434,6 +434,8 @@ Library
|
||||||
Tests
|
Tests
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
- Issue #16923: Fix ResourceWarnings in test_ssl.
|
||||||
|
|
||||||
- Issue #15539: Added regression tests for Tools/scripts/pindent.py.
|
- Issue #15539: Added regression tests for Tools/scripts/pindent.py.
|
||||||
|
|
||||||
- Issue #16925: test_configparser now works with unittest test discovery.
|
- Issue #16925: test_configparser now works with unittest test discovery.
|
||||||
|
|
Loading…
Reference in New Issue