From: Ben Darnell Date: Sun, 5 Nov 2017 15:38:03 +0000 (-0500) Subject: test: More robust ipv6 detection X-Git-Tag: v5.0.0~46^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=33abbbd48332f4950a686fbb389e802e1853a6e3;p=thirdparty%2Ftornado.git test: More robust ipv6 detection There are many ways ipv6 can be disabled or broken (and it just stopped working on travis-ci), so instead of checking flags like socket.has_ipv6, attempt to actually bind to an ipv6 address. Closes #2185 --- diff --git a/tornado/test/simple_httpclient_test.py b/tornado/test/simple_httpclient_test.py index f8d1a0546..df41abd7b 100644 --- a/tornado/test/simple_httpclient_test.py +++ b/tornado/test/simple_httpclient_test.py @@ -271,16 +271,9 @@ class SimpleHTTPClientTestMixin(object): @skipIfNoIPv6 def test_ipv6(self): - try: - [sock] = bind_sockets(None, '::1', family=socket.AF_INET6) - port = sock.getsockname()[1] - self.http_server.add_socket(sock) - except socket.gaierror as e: - if e.args[0] == socket.EAI_ADDRFAMILY: - # python supports ipv6, but it's not configured on the network - # interface, so skip this test. - return - raise + [sock] = bind_sockets(None, '::1', family=socket.AF_INET6) + port = sock.getsockname()[1] + self.http_server.add_socket(sock) url = '%s://[::1]:%d/hello' % (self.get_protocol(), port) # ipv6 is currently enabled by default but can be disabled diff --git a/tornado/test/util.py b/tornado/test/util.py index 0a9438bab..1d34ec083 100644 --- a/tornado/test/util.py +++ b/tornado/test/util.py @@ -28,9 +28,6 @@ skipOnAppEngine = unittest.skipIf('APPENGINE_RUNTIME' in os.environ, skipIfNoNetwork = unittest.skipIf('NO_NETWORK' in os.environ, 'network access disabled') -skipIfNoIPv6 = unittest.skipIf(not socket.has_ipv6, 'ipv6 support not present') - - skipBefore33 = unittest.skipIf(sys.version_info < (3, 3), 'PEP 380 (yield from) not available') skipBefore35 = unittest.skipIf(sys.version_info < (3, 5), 'PEP 492 (async/await) not available') skipNotCPython = unittest.skipIf(platform.python_implementation() != 'CPython', @@ -44,6 +41,24 @@ skipPypy3V58 = unittest.skipIf(platform.python_implementation() == 'PyPy' and sys.pypy_version_info < (5, 9), 'pypy3 5.8 has buggy ssl module') +def _detect_ipv6(): + if not socket.has_ipv6: + # socket.has_ipv6 check reports whether ipv6 was present at compile + # time. It's usually true even when ipv6 doesn't work for other reasons. + return False + sock = None + try: + sock = socket.socket(socket.AF_INET6) + sock.bind(('::1', 0)) + except socket.error: + return False + finally: + if sock is not None: + sock.close() + return True + +skipIfNoIPv6 = unittest.skipIf(not _detect_ipv6(), 'ipv6 support not present') + def refusing_port(): """Returns a local port number that will refuse all connections.