diff --git a/Lib/test/libregrtest/cmdline.py b/Lib/test/libregrtest/cmdline.py index 64c035307e6654..6a752e9befd654 100644 --- a/Lib/test/libregrtest/cmdline.py +++ b/Lib/test/libregrtest/cmdline.py @@ -115,6 +115,9 @@ network - It is okay to run tests that use external network resource, e.g. testing SSL support for sockets. + netraw - It is okay to run tests that create raw sockets. These + tests may require elevated privileges. + decimal - Test the decimal module against a large suite that verifies compliance with standards. diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index 892d204700179f..56e5a78e8fa31a 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -48,10 +48,13 @@ # # - extralagefile (ex: test_zipfile64): really too slow to be enabled # "by default" +# - netraw: raw sockets may require elevated privileges and can trigger +# behavior monitoring systems. # - tzdata: while needed to validate fully test_datetime, it makes # test_datetime too slow (15-20 min on some buildbots) and so is disabled by # default (see bpo-30822). -RESOURCE_NAMES = ALL_RESOURCES + ('extralargefile', 'tzdata', 'xpickle', 'wantobjects') +RESOURCE_NAMES = ALL_RESOURCES + ( + 'extralargefile', 'netraw', 'tzdata', 'xpickle', 'wantobjects') # Types for types hints diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index 3fdfffdb213efc..6bb33a76831134 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -152,6 +152,13 @@ def connection_lost(self, exc): self.done.set_result(None) +class FakeRawSocket(socket.socket): + + @property + def type(self): + return socket.SOCK_RAW + + class MyReadPipeProto(asyncio.Protocol): done = None @@ -1496,6 +1503,25 @@ def test_create_datagram_endpoint_sock(self): tr.close() self.loop.run_until_complete(pr.done) + def test_create_datagram_endpoint_sock_raw(self): + if support.is_resource_enabled('netraw'): + sock = socket.socket( + socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP) + else: + sock = FakeRawSocket(socket.AF_INET, socket.SOCK_DGRAM) + sock.bind(('127.0.0.1', 0)) + + self.assertEqual(sock.type, socket.SOCK_RAW) + with sock: + f = self.loop.create_datagram_endpoint( + lambda: MyDatagramProto(loop=self.loop), sock=sock) + tr, pr = self.loop.run_until_complete(f) + self.assertIsInstance(tr, asyncio.Transport) + self.assertIsInstance(pr, MyDatagramProto) + tr.close() + self.loop.run_until_complete(pr.done) + self.assertEqual(sock.fileno(), -1) + def test_datagram_send_to_non_listening_address(self): # see: # https://github.com/python/cpython/issues/91227 diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py index 6d30d267cd5ad2..be39c01d687eaa 100644 --- a/Lib/test/test_regrtest.py +++ b/Lib/test/test_regrtest.py @@ -311,6 +311,9 @@ def test_use(self): # test another resource which is not part of "all" ns = self.parse_args([opt, 'extralargefile']) self.assertEqual(ns.use_resources, {'extralargefile': None}) + ns = self.parse_args([opt, 'netraw']) + self.assertEqual(ns.use_resources, {'netraw': None}) + self.assertNotIn('netraw', cmdline.ALL_RESOURCES) # test resource with value ns = self.parse_args([opt, 'xpickle=2.7']) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 7bb50f7b8aa47e..cb56d96a62340c 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -945,6 +945,13 @@ def requireSocket(*args): class GeneralModuleTests(unittest.TestCase): + def test_create_raw_socket(self): + support.requires('netraw') + sock = socket.socket( + socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP) + with sock: + self.assertEqual(sock.type, socket.SOCK_RAW) + @unittest.skipUnless(_socket is not None, 'need _socket module') def test_socket_type(self): self.assertTrue(gc.is_tracked(_socket.socket))