import os.path
import socket
import sys
+import subprocess
import tempfile
import unittest
"""
return tempfile.mktemp(prefix="test_python_", suffix='.sock',
dir=os.path.curdir)
+
+
+# consider that sysctl values should not change while tests are running
+_sysctl_cache = {}
+
+def _get_sysctl(name):
+ """Get a sysctl value as an integer."""
+ try:
+ return _sysctl_cache[name]
+ except KeyError:
+ pass
+
+ # At least Linux and FreeBSD support the "-n" option
+ cmd = ['sysctl', '-n', name]
+ proc = subprocess.run(cmd,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ text=True)
+ if proc.returncode:
+ support.print_warning(f'{' '.join(cmd)!r} command failed with '
+ f'exit code {proc.returncode}')
+ # cache the error to only log the warning once
+ _sysctl_cache[name] = None
+ return None
+ output = proc.stdout
+
+ # Parse '0\n' to get '0'
+ try:
+ value = int(output.strip())
+ except Exception as exc:
+ support.print_warning(f'Failed to parse {' '.join(cmd)!r} '
+ f'command output {output!r}: {exc!r}')
+ # cache the error to only log the warning once
+ _sysctl_cache[name] = None
+ return None
+
+ _sysctl_cache[name] = value
+ return value
+
+
+def tcp_blackhole():
+ if not sys.platform.startswith('freebsd'):
+ return False
+
+ # gh-109015: test if FreeBSD TCP blackhole is enabled
+ value = _get_sysctl('net.inet.tcp.blackhole')
+ if value is None:
+ # don't skip if we fail to get the sysctl value
+ return False
+ return (value != 0)
+
+
+def skip_if_tcp_blackhole(test):
+ """Decorator skipping test if TCP blackhole is enabled."""
+ skip_if = unittest.skipIf(
+ tcp_blackhole(),
+ "TCP blackhole is enabled (sysctl net.inet.tcp.blackhole)"
+ )
+ return skip_if(test)
self.assertEqual(port, expected)
tr.close()
+ @socket_helper.skip_if_tcp_blackhole
def test_create_connection_local_addr_skip_different_family(self):
# See https://github.com/python/cpython/issues/86508
port1 = socket_helper.find_unused_port()
with self.assertRaises(OSError):
self.loop.run_until_complete(f)
+ @socket_helper.skip_if_tcp_blackhole
def test_create_connection_local_addr_nomatch_family(self):
# See https://github.com/python/cpython/issues/86508
port1 = socket_helper.find_unused_port()
server.close()
+ @socket_helper.skip_if_tcp_blackhole
def test_server_close(self):
f = self.loop.create_server(MyProto, '0.0.0.0', 0)
server = self.loop.run_until_complete(f)
from test import support
from test.support import socket_helper
+if socket_helper.tcp_blackhole():
+ raise unittest.SkipTest('Not relevant to ProactorEventLoop')
+
+
def tearDownModule():
asyncio.set_event_loop_policy(None)
import unittest
import weakref
from test import support
+from test.support import socket_helper
from unittest import mock
try:
import ssl
support.gc_collect()
self.assertIsNone(client_context())
+ @socket_helper.skip_if_tcp_blackhole
def test_start_tls_client_buf_proto_1(self):
HELLO_MSG = b'1' * self.PAYLOAD_SIZE
asyncio.wait_for(client(srv.addr),
timeout=support.SHORT_TIMEOUT))
+ @socket_helper.skip_if_tcp_blackhole
def test_start_tls_server_1(self):
HELLO_MSG = b'1' * self.PAYLOAD_SIZE
ANSWER = b'answer'
for t in self.timevalues():
imaplib.Time2Internaldate(t)
+ @socket_helper.skip_if_tcp_blackhole
def test_imap4_host_default_value(self):
# Check whether the IMAP4_PORT is truly unavailable.
with socket.socket() as s:
finally:
socket.socket = old_socket
+ @socket_helper.skip_if_tcp_blackhole
def test_connect(self):
port = socket_helper.find_unused_port()
cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cli.connect((HOST, port))
self.assertEqual(cm.exception.errno, errno.ECONNREFUSED)
+ @socket_helper.skip_if_tcp_blackhole
def test_create_connection(self):
# Issue #9792: errors raised by create_connection() should have
# a proper errno attribute.
--- /dev/null
+Fix test_asyncio, test_imaplib and test_socket tests on FreeBSD if the TCP
+blackhole is enabled (``sysctl net.inet.tcp.blackhole``). Skip the few tests
+which failed with ``ETIMEDOUT`` which such non standard configuration.
+Currently, the `FreeBSD GCP image enables TCP and UDP blackhole
+<https://reviews.freebsd.org/D41751>`_ (``sysctl net.inet.tcp.blackhole=2``
+and ``sysctl net.inet.udp.blackhole=1``). Patch by Victor Stinner.