Issue #28652: Make loop methods reject socket kinds they do not support. · python/cpython@a1a8b7d · GitHub
Skip to content

Commit a1a8b7d

Browse files
author
Yury Selivanov
committed
Issue #28652: Make loop methods reject socket kinds they do not support.
1 parent d2fd359 commit a1a8b7d

6 files changed

Lines changed: 139 additions & 24 deletions

File tree

Lib/asyncio/base_events.py

Lines changed: 45 additions & 11 deletions

Lib/asyncio/unix_events.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def create_unix_connection(self, protocol_factory, path, *,
235235
if sock is None:
236236
raise ValueError('no path and sock were specified')
237237
if (sock.family != socket.AF_UNIX or
238-
sock.type != socket.SOCK_STREAM):
238+
not base_events._is_stream_socket(sock)):
239239
raise ValueError(
240240
'A UNIX Domain Stream Socket was expected, got {!r}'
241241
.format(sock))
@@ -289,7 +289,7 @@ def create_unix_server(self, protocol_factory, path=None, *,
289289
'path was not specified, and no sock specified')
290290

291291
if (sock.family != socket.AF_UNIX or
292-
sock.type != socket.SOCK_STREAM):
292+
not base_events._is_stream_socket(sock)):
293293
raise ValueError(
294294
'A UNIX Domain Stream Socket was expected, got {!r}'
295295
.format(sock))

Lib/test/test_asyncio/test_base_events.py

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ def test_ipaddr_info(self):
116116
self.assertIsNone(
117117
base_events._ipaddr_info('::3%lo0', 1, INET6, STREAM, TCP))
118118

119+
if hasattr(socket, 'SOCK_NONBLOCK'):
120+
self.assertEqual(
121+
None,
122+
base_events._ipaddr_info(
123+
'1.2.3.4', 1, INET, STREAM | socket.SOCK_NONBLOCK, TCP))
124+
125+
119126
def test_port_parameter_types(self):
120127
# Test obscure kinds of arguments for "port".
121128
INET = socket.AF_INET
@@ -1040,6 +1047,43 @@ def test_create_connection_host_port_sock(self):
10401047
MyProto, 'example.com', 80, sock=object())
10411048
self.assertRaises(ValueError, self.loop.run_until_complete, coro)
10421049

1050+
@unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'no Unix sockets')
1051+
def test_create_connection_wrong_sock(self):
1052+
sock = socket.socket(socket.AF_UNIX)
1053+
with sock:
1054+
coro = self.loop.create_connection(MyProto, sock=sock)
1055+
with self.assertRaisesRegex(ValueError,
1056+
'A TCP Stream Socket was expected'):
1057+
self.loop.run_until_complete(coro)
1058+
1059+
@unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'no Unix sockets')
1060+
def test_create_server_wrong_sock(self):
1061+
sock = socket.socket(socket.AF_UNIX)
1062+
with sock:
1063+
coro = self.loop.create_server(MyProto, sock=sock)
1064+
with self.assertRaisesRegex(ValueError,
1065+
'A TCP Stream Socket was expected'):
1066+
self.loop.run_until_complete(coro)
1067+
1068+
@unittest.skipUnless(hasattr(socket, 'SOCK_NONBLOCK'),
1069+
'no socket.SOCK_NONBLOCK (linux only)')
1070+
def test_create_server_stream_bittype(self):
1071+
sock = socket.socket(
1072+
socket.AF_INET, socket.SOCK_STREAM | socket.SOCK_NONBLOCK)
1073+
with sock:
1074+
coro = self.loop.create_server(lambda: None, sock=sock)
1075+
srv = self.loop.run_until_complete(coro)
1076+
srv.close()
1077+
self.loop.run_until_complete(srv.wait_closed())
1078+
1079+
def test_create_datagram_endpoint_wrong_sock(self):
1080+
sock = socket.socket(socket.AF_INET)
1081+
with sock:
1082+
coro = self.loop.create_datagram_endpoint(MyProto, sock=sock)
1083+
with self.assertRaisesRegex(ValueError,
1084+
'A UDP Socket was expected'):
1085+
self.loop.run_until_complete(coro)
1086+
10431087
def test_create_connection_no_host_port_sock(self):
10441088
coro = self.loop.create_connection(MyProto)
10451089
self.assertRaises(ValueError, self.loop.run_until_complete, coro)
@@ -1487,36 +1531,39 @@ def test_create_datagram_endpoint_sock(self):
14871531
self.assertEqual('CLOSED', protocol.state)
14881532

14891533
def test_create_datagram_endpoint_sock_sockopts(self):
1534+
class FakeSock:
1535+
type = socket.SOCK_DGRAM
1536+
14901537
fut = self.loop.create_datagram_endpoint(
1491-
MyDatagramProto, local_addr=('127.0.0.1', 0), sock=object())
1538+
MyDatagramProto, local_addr=('127.0.0.1', 0), sock=FakeSock())
14921539
self.assertRaises(ValueError, self.loop.run_until_complete, fut)
14931540

14941541
fut = self.loop.create_datagram_endpoint(
1495-
MyDatagramProto, remote_addr=('127.0.0.1', 0), sock=object())
1542+
MyDatagramProto, remote_addr=('127.0.0.1', 0), sock=FakeSock())
14961543
self.assertRaises(ValueError, self.loop.run_until_complete, fut)
14971544

14981545
fut = self.loop.create_datagram_endpoint(
1499-
MyDatagramProto, family=1, sock=object())
1546+
MyDatagramProto, family=1, sock=FakeSock())
15001547
self.assertRaises(ValueError, self.loop.run_until_complete, fut)
15011548

15021549
fut = self.loop.create_datagram_endpoint(
1503-
MyDatagramProto, proto=1, sock=object())
1550+
MyDatagramProto, proto=1, sock=FakeSock())
15041551
self.assertRaises(ValueError, self.loop.run_until_complete, fut)
15051552

15061553
fut = self.loop.create_datagram_endpoint(
1507-
MyDatagramProto, flags=1, sock=object())
1554+
MyDatagramProto, flags=1, sock=FakeSock())
15081555
self.assertRaises(ValueError, self.loop.run_until_complete, fut)
15091556

15101557
fut = self.loop.create_datagram_endpoint(
1511-
MyDatagramProto, reuse_address=True, sock=object())
1558+
MyDatagramProto, reuse_address=True, sock=FakeSock())
15121559
self.assertRaises(ValueError, self.loop.run_until_complete, fut)
15131560

15141561
fut = self.loop.create_datagram_endpoint(
1515-
MyDatagramProto, reuse_port=True, sock=object())
1562+
MyDatagramProto, reuse_port=True, sock=FakeSock())
15161563
self.assertRaises(ValueError, self.loop.run_until_complete, fut)
15171564

15181565
fut = self.loop.create_datagram_endpoint(
1519-
MyDatagramProto, allow_broadcast=True, sock=object())
1566+
MyDatagramProto, allow_broadcast=True, sock=FakeSock())
15201567
self.assertRaises(ValueError, self.loop.run_until_complete, fut)
15211568

15221569
def test_create_datagram_endpoint_sockopts(self):

Lib/test/test_asyncio/test_events.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,9 @@ def client():
791791
conn, _ = lsock.accept()
792792
proto = MyProto(loop=loop)
793793
proto.loop = loop
794-
f = loop.create_task(
794+
loop.run_until_complete(
795795
loop.connect_accepted_socket(
796-
(lambda : proto), conn, ssl=server_ssl))
796+
(lambda: proto), conn, ssl=server_ssl))
797797
loop.run_forever()
798798
proto.transport.close()
799799
lsock.close()
@@ -1377,6 +1377,11 @@ def datagram_received(self, data, addr):
13771377
server.transport.close()
13781378

13791379
def test_create_datagram_endpoint_sock(self):
1380+
if (sys.platform == 'win32' and
1381+
isinstance(self.loop, proactor_events.BaseProactorEventLoop)):
1382+
raise unittest.SkipTest(
1383+
'UDP is not supported with proactor event loops')
1384+
13801385
sock = None
13811386
local_address = ('127.0.0.1', 0)
13821387
infos = self.loop.run_until_complete(
@@ -1394,7 +1399,7 @@ def test_create_datagram_endpoint_sock(self):
13941399
else:
13951400
assert False, 'Can not create socket.'
13961401

1397-
f = self.loop.create_connection(
1402+
f = self.loop.create_datagram_endpoint(
13981403
lambda: MyDatagramProto(loop=self.loop), sock=sock)
13991404
tr, pr = self.loop.run_until_complete(f)
14001405
self.assertIsInstance(tr, asyncio.Transport)

Lib/test/test_asyncio/test_unix_events.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,33 @@ def test_create_unix_server_path_inetsock(self):
280280
'A UNIX Domain Stream.*was expected'):
281281
self.loop.run_until_complete(coro)
282282

283+
def test_create_unix_server_path_dgram(self):
284+
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
285+
with sock:
286+
coro = self.loop.create_unix_server(lambda: None, path=None,
287+
sock=sock)
288+
with self.assertRaisesRegex(ValueError,
289+
'A UNIX Domain Stream.*was expected'):
290+
self.loop.run_until_complete(coro)
291+
292+
@unittest.skipUnless(hasattr(socket, 'SOCK_NONBLOCK'),
293+
'no socket.SOCK_NONBLOCK (linux only)')
294+
def test_create_unix_server_path_stream_bittype(self):
295+
sock = socket.socket(
296+
socket.AF_UNIX, socket.SOCK_STREAM | socket.SOCK_NONBLOCK)
297+
with tempfile.NamedTemporaryFile() as file:
298+
fn = file.name
299+
try:
300+
with sock:
301+
sock.bind(fn)
302+
coro = self.loop.create_unix_server(lambda: None, path=None,
303+
sock=sock)
304+
srv = self.loop.run_until_complete(coro)
305+
srv.close()
306+
self.loop.run_until_complete(srv.wait_closed())
307+
finally:
308+
os.unlink(fn)
309+
283310
def test_create_unix_connection_path_inetsock(self):
284311
sock = socket.socket()
285312
with sock:

Misc/NEWS

Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)