Skip to content

Commit 016a485

Browse files
author
Paul Monson
committed
update tests to work with openssl fixes
investigated test_pha_required_nocert failure
1 parent bade87e commit 016a485

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed

Lib/test/test_ssl.py

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,13 +1822,9 @@ def test_connect(self):
18221822
with test_wrap_socket(socket.socket(socket.AF_INET),
18231823
cert_reqs=ssl.CERT_REQUIRED,
18241824
ca_certs=SIGNING_CA) as s:
1825-
try:
1826-
s.connect(self.server_addr)
1827-
self.assertTrue(s.getpeercert())
1828-
self.assertFalse(s.server_side)
1829-
except (ConnectionResetError, ConnectionAbortedError) as e:
1830-
# sometimes windows throws ConnectionResetError during the handshake
1831-
sys.stdout.write(repr(e))
1825+
s.connect(self.server_addr)
1826+
self.assertTrue(s.getpeercert())
1827+
self.assertFalse(s.server_side)
18321828

18331829
def test_connect_fail(self):
18341830
# This should fail because we have no verification certs. Connection
@@ -1885,18 +1881,13 @@ def test_connect_with_context(self):
18851881
with ctx.wrap_socket(socket.socket(socket.AF_INET),
18861882
server_hostname="dummy") as s:
18871883
s.connect(self.server_addr)
1888-
self.assertEqual({}, s.getpeercert())
18891884
ctx.verify_mode = ssl.CERT_REQUIRED
18901885
# This should succeed because we specify the root cert
18911886
ctx.load_verify_locations(SIGNING_CA)
18921887
with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s:
1893-
try:
1894-
s.connect(self.server_addr)
1895-
cert = s.getpeercert()
1896-
self.assertTrue(cert)
1897-
except (ConnectionResetError, ConnectionRefusedError) as e:
1898-
# sometimes windows throws ConnectionResetError during the handshake
1899-
sys.stdout.write(repr(e))
1888+
s.connect(self.server_addr)
1889+
cert = s.getpeercert()
1890+
self.assertTrue(cert)
19001891

19011892
def test_connect_with_context_fail(self):
19021893
# This should fail because we have no verification certs. Connection
@@ -1928,13 +1919,9 @@ def test_connect_capath(self):
19281919
ctx.verify_mode = ssl.CERT_REQUIRED
19291920
ctx.load_verify_locations(capath=BYTES_CAPATH)
19301921
with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s:
1931-
try:
1932-
s.connect(self.server_addr)
1933-
cert = s.getpeercert()
1934-
self.assertTrue(cert)
1935-
except ConnectionResetError as e:
1936-
# sometimes windows throws ConnectionResetError during the handshake
1937-
sys.stdout.write(repr(e))
1922+
s.connect(self.server_addr)
1923+
cert = s.getpeercert()
1924+
self.assertTrue(cert)
19381925

19391926
def test_connect_cadata(self):
19401927
with open(SIGNING_CA) as f:
@@ -2248,7 +2235,7 @@ def wrap_conn(self):
22482235
if support.verbose and self.server.chatty:
22492236
sys.stdout.write(" client cert is " + pprint.pformat(cert) + "\n")
22502237
cert_binary = self.sslconn.getpeercert(True)
2251-
if support.verbose and self.server.chatty and cert_binary != None:
2238+
if support.verbose and self.server.chatty:
22522239
sys.stdout.write(" cert binary is " + str(len(cert_binary)) + " bytes\n")
22532240
cipher = self.sslconn.cipher()
22542241
if support.verbose and self.server.chatty:
@@ -2347,8 +2334,9 @@ def run(self):
23472334
sys.stdout.write(" server: read %r (%s), sending back %r (%s)...\n"
23482335
% (msg, ctype, msg.lower(), ctype))
23492336
self.write(msg.lower())
2350-
except ConnectionResetError:
2351-
# XXX: OpenSSL 1.1.1 sometimes raises ConnectionResetError
2337+
except (ConnectionResetError, ConnectionAbortedError):
2338+
# XXX: OpenSSL 1.1.1 sometimes raises ConnectionResetError
2339+
# or ConnectionAbortedError (on Windows)
23522340
# when connection is not shut down gracefully.
23532341
if self.server.chatty and support.verbose:
23542342
sys.stdout.write(
@@ -2357,22 +2345,27 @@ def run(self):
23572345
)
23582346
self.close()
23592347
self.running = False
2360-
except OSError as err:
2361-
if 'peer did not return a certificate' in err.args[1]:
2362-
# test_pha_required_nocert causes this error which results in a false(?) failure
2348+
except ssl.SSLError as err:
2349+
# On Windows sometimes test_pha_required_nocert receives the
2350+
# PEER_DID_NOT_RETURN_A_CERTIFICATE exception
2351+
# before the 'tlsv13 alert certificate required' exception.
2352+
# If the server is stopped when PEER_DID_NOT_RETURN_A_CERTIFICATE
2353+
# is received test_pha_required_nocert fails with ConnectionResetError
2354+
# because the underlying socket is closed
2355+
if 'PEER_DID_NOT_RETURN_A_CERTIFICATE' == err.reason:
23632356
if self.server.chatty and support.verbose:
23642357
sys.stdout.write(err.args[1])
23652358
# test_pha_required_nocert is expecting this exception
23662359
raise ssl.SSLError('tlsv13 alert certificate required')
2367-
else:
2368-
if self.server.chatty:
2369-
handle_error("Test server failure:\n")
2370-
self.close()
2371-
self.running = False
2360+
except OSError:
2361+
if self.server.chatty:
2362+
handle_error("Test server failure:\n")
2363+
self.close()
2364+
self.running = False
23722365

2373-
# normally, we'd just stop here, but for the test
2374-
# harness, we want to stop the server
2375-
self.server.stop()
2366+
# normally, we'd just stop here, but for the test
2367+
# harness, we want to stop the server
2368+
self.server.stop()
23762369

23772370
def __init__(self, certificate=None, ssl_version=None,
23782371
certreqs=None, cacerts=None,

0 commit comments

Comments
 (0)