Skip to content

Commit fe5da2d

Browse files
committed
Address review comments
1 parent fcb43e9 commit fe5da2d

3 files changed

Lines changed: 46 additions & 16 deletions

File tree

Lib/socket.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -653,11 +653,20 @@ def socketpair(family=AF_INET, type=SOCK_STREAM, proto=0):
653653

654654
# Authenticating avoids using a connection from something else
655655
# able to connect to {host}:{port} instead of us.
656-
if (
657-
ssock.getsockname()[:2] != csock.getpeername()[:2]
658-
or csock.getsockname()[:2] != ssock.getpeername()[:2]
659-
):
660-
raise ConnectionError("Unexpected peer connection")
656+
# We expect only AF_INET and AF_INET6 families.
657+
try:
658+
if (
659+
ssock.getsockname() != csock.getpeername()
660+
or csock.getsockname() != ssock.getpeername()
661+
):
662+
raise ConnectionError("Unexpected peer connection")
663+
except:
664+
# getsockname() and getpeername() can fail
665+
# if either socket isn't connected.
666+
ssock.close()
667+
csock.close()
668+
raise
669+
661670
return (ssock, csock)
662671
__all__.append("socketpair")
663672

Lib/test/test_socket.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -595,12 +595,12 @@ def __init__(self, methodName='runTest'):
595595
self.cli = None
596596
self.serv = None
597597

598-
def call_socketpair(self):
598+
def socketpair(self):
599599
# To be overridden by some child classes.
600600
return socket.socketpair()
601601

602602
def setUp(self):
603-
self.serv, self.cli = self.call_socketpair()
603+
self.serv, self.cli = self.socketpair()
604604

605605
def tearDown(self):
606606
if self.serv:
@@ -4907,17 +4907,30 @@ def _test_recv(self):
49074907
self.cli.send(MSG)
49084908

49094909
def test_send(self):
4910-
self.serv.send(MSG)
4911-
4912-
def _test_send(self):
49134910
msg = self.cli.recv(1024)
49144911
self.assertEqual(msg, MSG)
49154912

4916-
def _test_injected_authentication_failure(self):
4917-
# No-op. Exists for base class threading infrastructure to call.
4918-
# We could refactor this test into its own lesser class along with the
4919-
# setUp and tearDown code to construct an ideal; it is simpler to keep
4920-
# it here and live with extra overhead one this _one_ failure test.
4913+
def _test_send(self):
4914+
self.serv.send(MSG)
4915+
4916+
def test_ipv4(self):
4917+
cli, srv = socket.socketpair(socket.AF_INET)
4918+
cli.close()
4919+
srv.close()
4920+
4921+
def _test_ipv4(self):
4922+
pass
4923+
4924+
@unittest.skipIf(not hasattr(_socket, 'IPPROTO_IPV6') or
4925+
not hasattr(_socket, 'IPV6_V6ONLY'),
4926+
"IPV6_V6ONLY option not supported")
4927+
@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test')
4928+
def test_ipv6(self):
4929+
cli, srv = socket.socketpair(socket.AF_INET6)
4930+
cli.close()
4931+
srv.close()
4932+
4933+
def _test_ipv6(self):
49214934
pass
49224935

49234936
def test_injected_authentication_failure(self):
@@ -4950,8 +4963,16 @@ def inject_getsocketname(self):
49504963
inject_sock.close()
49514964
if sock1: # This cleanup isn't needed on a successful test.
49524965
sock1.close()
4966+
if sock2:
49534967
sock2.close()
49544968

4969+
def _test_injected_authentication_failure(self):
4970+
# No-op. Exists for base class threading infrastructure to call.
4971+
# We could refactor this test into its own lesser class along with the
4972+
# setUp and tearDown code to construct an ideal; it is simpler to keep
4973+
# it here and live with extra overhead one this _one_ failure test.
4974+
pass
4975+
49554976

49564977
class NonBlockingTCPTests(ThreadedTCPSocketTest):
49574978

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Authenticate the socket connection for the ``socket.socketpair()`` fallback
22
on platforms where ``AF_UNIX`` is not available like Windows.
33

4-
Patch by Gregory P. Smith <greg@krypto.org>. Reported by Ellie
4+
Patch by Gregory P. Smith <greg@krypto.org> and Seth Larson <seth@python.org>. Reported by Ellie
55
<el@horse64.org>

0 commit comments

Comments
 (0)