Merge branch 'main' into windows-socket-sendfile

This commit is contained in:
AN Long 2024-04-24 20:51:45 +08:00 committed by GitHub
commit 8bd91bb15d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
1889 changed files with 127947 additions and 60594 deletions

View file

@ -1,9 +1,8 @@
import unittest
from test import support
from test.support import os_helper
from test.support import socket_helper
from test.support import threading_helper
from test.support import (
is_apple, os_helper, refleak_helper, socket_helper, threading_helper
)
import _thread as thread
import array
import contextlib
@ -37,6 +36,10 @@
import fcntl
except ImportError:
fcntl = None
try:
import _testcapi
except ImportError:
_testcapi = None
support.requires_working_socket(module=True)
@ -46,12 +49,42 @@
VSOCKPORT = 1234
AIX = platform.system() == "AIX"
WSL = "microsoft-standard-WSL" in platform.release()
try:
import _socket
except ImportError:
_socket = None
def skipForRefleakHuntinIf(condition, issueref):
if not condition:
def decorator(f):
f.client_skip = lambda f: f
return f
else:
def decorator(f):
@contextlib.wraps(f)
def wrapper(*args, **kwds):
if refleak_helper.hunting_for_refleaks():
raise unittest.SkipTest(f"ignore while hunting for refleaks, see {issueref}")
return f(*args, **kwds)
def client_skip(f):
@contextlib.wraps(f)
def wrapper(*args, **kwds):
if refleak_helper.hunting_for_refleaks():
return
return f(*args, **kwds)
return wrapper
wrapper.client_skip = client_skip
return wrapper
return decorator
def get_cid():
if fcntl is None:
return None
@ -179,7 +212,10 @@ def socket_setdefaulttimeout(timeout):
HAVE_SOCKET_VSOCK = _have_socket_vsock()
HAVE_SOCKET_UDPLITE = hasattr(socket, "IPPROTO_UDPLITE")
# Older Android versions block UDPLITE with SELinux.
HAVE_SOCKET_UDPLITE = (
hasattr(socket, "IPPROTO_UDPLITE")
and not (support.is_android and platform.android_ver().api_level < 29))
HAVE_SOCKET_BLUETOOTH = _have_socket_bluetooth()
@ -481,6 +517,7 @@ def clientTearDown(self):
ThreadableTest.clientTearDown(self)
@unittest.skipIf(fcntl is None, "need fcntl")
@unittest.skipIf(WSL, 'VSOCK does not work on Microsoft WSL')
@unittest.skipUnless(HAVE_SOCKET_VSOCK,
'VSOCK sockets required for this test.')
@unittest.skipUnless(get_cid() != 2,
@ -497,6 +534,7 @@ def setUp(self):
self.serv.bind((socket.VMADDR_CID_ANY, VSOCKPORT))
self.serv.listen()
self.serverExplicitReady()
self.serv.settimeout(support.LOOPBACK_TIMEOUT)
self.conn, self.connaddr = self.serv.accept()
self.addCleanup(self.conn.close)
@ -1138,6 +1176,7 @@ def testNtoH(self):
self.assertRaises(OverflowError, func, 1<<34)
@support.cpython_only
@unittest.skipIf(_testcapi is None, "requires _testcapi")
def testNtoHErrors(self):
import _testcapi
s_good_values = [0, 1, 2, 0xffff]
@ -1166,8 +1205,11 @@ def testGetServBy(self):
# Find one service that exists, then check all the related interfaces.
# I've ordered this by protocols that have both a tcp and udp
# protocol, at least for modern Linuxes.
if (sys.platform.startswith(('freebsd', 'netbsd', 'gnukfreebsd'))
or sys.platform in ('linux', 'darwin')):
if (
sys.platform.startswith(
('linux', 'android', 'freebsd', 'netbsd', 'gnukfreebsd'))
or is_apple
):
# avoid the 'echo' service on this platform, as there is an
# assumption breaking non-standard port/protocol entry
services = ('daytime', 'qotd', 'domain')
@ -1182,9 +1224,8 @@ def testGetServBy(self):
else:
raise OSError
# Try same call with optional protocol omitted
# Issue #26936: Android getservbyname() was broken before API 23.
if (not hasattr(sys, 'getandroidapilevel') or
sys.getandroidapilevel() >= 23):
# Issue gh-71123: this fails on Android before API level 23.
if not (support.is_android and platform.android_ver().api_level < 23):
port2 = socket.getservbyname(service)
eq(port, port2)
# Try udp, but don't barf if it doesn't exist
@ -1195,8 +1236,9 @@ def testGetServBy(self):
else:
eq(udpport, port)
# Now make sure the lookup by port returns the same service name
# Issue #26936: Android getservbyport() is broken.
if not support.is_android:
# Issue #26936: when the protocol is omitted, this fails on Android
# before API level 28.
if not (support.is_android and platform.android_ver().api_level < 28):
eq(socket.getservbyport(port2), service)
eq(socket.getservbyport(port, 'tcp'), service)
if udpport is not None:
@ -1541,9 +1583,8 @@ def testGetaddrinfo(self):
socket.getaddrinfo('::1', 80)
# port can be a string service name such as "http", a numeric
# port number or None
# Issue #26936: Android getaddrinfo() was broken before API level 23.
if (not hasattr(sys, 'getandroidapilevel') or
sys.getandroidapilevel() >= 23):
# Issue #26936: this fails on Android before API level 23.
if not (support.is_android and platform.android_ver().api_level < 23):
socket.getaddrinfo(HOST, "http")
socket.getaddrinfo(HOST, 80)
socket.getaddrinfo(HOST, None)
@ -1601,6 +1642,7 @@ def testGetaddrinfo(self):
except socket.gaierror:
pass
@unittest.skipIf(_testcapi is None, "requires _testcapi")
def test_getaddrinfo_int_port_overflow(self):
# gh-74895: Test that getaddrinfo does not raise OverflowError on port.
#
@ -1794,6 +1836,7 @@ def test_listen_backlog(self):
srv.listen()
@support.cpython_only
@unittest.skipIf(_testcapi is None, "requires _testcapi")
def test_listen_backlog_overflow(self):
# Issue 15989
import _testcapi
@ -2675,22 +2718,29 @@ def testDup(self):
def _testDup(self):
self.serv_conn.send(MSG)
def testShutdown(self):
# Testing shutdown()
def check_shutdown(self):
# Test shutdown() helper
msg = self.cli_conn.recv(1024)
self.assertEqual(msg, MSG)
# wait for _testShutdown to finish: on OS X, when the server
# wait for _testShutdown[_overflow] to finish: on OS X, when the server
# closes the connection the client also becomes disconnected,
# and the client's shutdown call will fail. (Issue #4397.)
self.done.wait()
def testShutdown(self):
self.check_shutdown()
def _testShutdown(self):
self.serv_conn.send(MSG)
self.serv_conn.shutdown(2)
testShutdown_overflow = support.cpython_only(testShutdown)
@support.cpython_only
@unittest.skipIf(_testcapi is None, "requires _testcapi")
def testShutdown_overflow(self):
self.check_shutdown()
@support.cpython_only
@unittest.skipIf(_testcapi is None, "requires _testcapi")
def _testShutdown_overflow(self):
import _testcapi
self.serv_conn.send(MSG)
@ -3161,7 +3211,7 @@ def _testSendmsgTimeout(self):
# Linux supports MSG_DONTWAIT when sending, but in general, it
# only works when receiving. Could add other platforms if they
# support it too.
@skipWithClientIf(sys.platform not in {"linux"},
@skipWithClientIf(sys.platform not in {"linux", "android"},
"MSG_DONTWAIT not known to work on this platform when "
"sending")
def testSendmsgDontWait(self):
@ -3678,7 +3728,7 @@ def testFDPassCMSG_LEN(self):
def _testFDPassCMSG_LEN(self):
self.createAndSendFDs(1)
@unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958")
@unittest.skipIf(is_apple, "skipping, see issue #12958")
@unittest.skipIf(AIX, "skipping, see issue #22397")
@requireAttrs(socket, "CMSG_SPACE")
def testFDPassSeparate(self):
@ -3689,7 +3739,7 @@ def testFDPassSeparate(self):
maxcmsgs=2)
@testFDPassSeparate.client_skip
@unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958")
@unittest.skipIf(is_apple, "skipping, see issue #12958")
@unittest.skipIf(AIX, "skipping, see issue #22397")
def _testFDPassSeparate(self):
fd0, fd1 = self.newFDs(2)
@ -3702,7 +3752,7 @@ def _testFDPassSeparate(self):
array.array("i", [fd1]))]),
len(MSG))
@unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958")
@unittest.skipIf(is_apple, "skipping, see issue #12958")
@unittest.skipIf(AIX, "skipping, see issue #22397")
@requireAttrs(socket, "CMSG_SPACE")
def testFDPassSeparateMinSpace(self):
@ -3716,7 +3766,7 @@ def testFDPassSeparateMinSpace(self):
maxcmsgs=2, ignoreflags=socket.MSG_CTRUNC)
@testFDPassSeparateMinSpace.client_skip
@unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958")
@unittest.skipIf(is_apple, "skipping, see issue #12958")
@unittest.skipIf(AIX, "skipping, see issue #22397")
def _testFDPassSeparateMinSpace(self):
fd0, fd1 = self.newFDs(2)
@ -3740,7 +3790,7 @@ def sendAncillaryIfPossible(self, msg, ancdata):
nbytes = self.sendmsgToServer([msg])
self.assertEqual(nbytes, len(msg))
@unittest.skipIf(sys.platform == "darwin", "see issue #24725")
@unittest.skipIf(is_apple, "skipping, see issue #12958")
def testFDPassEmpty(self):
# Try to pass an empty FD array. Can receive either no array
# or an empty array.
@ -3814,6 +3864,7 @@ def checkTruncatedHeader(self, result, ignoreflags=0):
self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC,
ignore=ignoreflags)
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTruncNoBufSize(self):
# Check that no ancillary data is received when no buffer size
# is specified.
@ -3823,26 +3874,32 @@ def testCmsgTruncNoBufSize(self):
# received.
ignoreflags=socket.MSG_CTRUNC)
@testCmsgTruncNoBufSize.client_skip
def _testCmsgTruncNoBufSize(self):
self.createAndSendFDs(1)
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTrunc0(self):
# Check that no ancillary data is received when buffer size is 0.
self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), 0),
ignoreflags=socket.MSG_CTRUNC)
@testCmsgTrunc0.client_skip
def _testCmsgTrunc0(self):
self.createAndSendFDs(1)
# Check that no ancillary data is returned for various non-zero
# (but still too small) buffer sizes.
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTrunc1(self):
self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), 1))
@testCmsgTrunc1.client_skip
def _testCmsgTrunc1(self):
self.createAndSendFDs(1)
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTrunc2Int(self):
# The cmsghdr structure has at least three members, two of
# which are ints, so we still shouldn't see any ancillary
@ -3850,13 +3907,16 @@ def testCmsgTrunc2Int(self):
self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG),
SIZEOF_INT * 2))
@testCmsgTrunc2Int.client_skip
def _testCmsgTrunc2Int(self):
self.createAndSendFDs(1)
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTruncLen0Minus1(self):
self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG),
socket.CMSG_LEN(0) - 1))
@testCmsgTruncLen0Minus1.client_skip
def _testCmsgTruncLen0Minus1(self):
self.createAndSendFDs(1)
@ -3887,29 +3947,38 @@ def checkTruncatedArray(self, ancbuf, maxdata, mindata=0):
len(cmsg_data) - (len(cmsg_data) % fds.itemsize)])
self.checkFDs(fds)
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTruncLen0(self):
self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(0), maxdata=0)
@testCmsgTruncLen0.client_skip
def _testCmsgTruncLen0(self):
self.createAndSendFDs(1)
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTruncLen0Plus1(self):
self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(0) + 1, maxdata=1)
@testCmsgTruncLen0Plus1.client_skip
def _testCmsgTruncLen0Plus1(self):
self.createAndSendFDs(2)
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTruncLen1(self):
self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(SIZEOF_INT),
maxdata=SIZEOF_INT)
@testCmsgTruncLen1.client_skip
def _testCmsgTruncLen1(self):
self.createAndSendFDs(2)
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTruncLen2Minus1(self):
self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(2 * SIZEOF_INT) - 1,
maxdata=(2 * SIZEOF_INT) - 1)
@testCmsgTruncLen2Minus1.client_skip
def _testCmsgTruncLen2Minus1(self):
self.createAndSendFDs(2)
@ -4828,6 +4897,7 @@ def _testSetBlocking(self):
pass
@support.cpython_only
@unittest.skipIf(_testcapi is None, "requires _testcapi")
def testSetBlocking_overflow(self):
# Issue 15989
import _testcapi
@ -5580,7 +5650,7 @@ def test_setblocking_invalidfd(self):
sock.setblocking(False)
@unittest.skipUnless(sys.platform == 'linux', 'Linux specific test')
@unittest.skipUnless(sys.platform in ('linux', 'android'), 'Linux specific test')
class TestLinuxAbstractNamespace(unittest.TestCase):
UNIX_PATH_MAX = 108
@ -5705,7 +5775,8 @@ def testUnencodableAddr(self):
self.addCleanup(os_helper.unlink, path)
self.assertEqual(self.sock.getsockname(), path)
@unittest.skipIf(sys.platform == 'linux', 'Linux specific test')
@unittest.skipIf(sys.platform in ('linux', 'android'),
'Linux behavior is tested by TestLinuxAbstractNamespace')
def testEmptyAddress(self):
# Test that binding empty address fails.
self.assertRaises(OSError, self.sock.bind, "")