]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #28683: Fix the tests that bind() a unix socket and raise PermissionError
authorXavier de Gaye <xdegaye@users.sourceforge.net>
Wed, 14 Dec 2016 10:52:28 +0000 (11:52 +0100)
committerXavier de Gaye <xdegaye@users.sourceforge.net>
Wed, 14 Dec 2016 10:52:28 +0000 (11:52 +0100)
on Android for a non-root user.

Lib/test/support/__init__.py
Lib/test/test_asyncore.py
Lib/test/test_pathlib.py
Lib/test/test_socket.py
Misc/NEWS

index ed1af2b5a5d3d686f979d9e5b93d813b592dede7..15d8fc849b9a0ab026080c6b5525243add5bd88d 100644 (file)
@@ -96,6 +96,7 @@ __all__ = [
     "setswitchinterval", "android_not_root",
     # network
     "HOST", "IPV6_ENABLED", "find_unused_port", "bind_port", "open_urlresource",
+    "bind_unix_socket",
     # processes
     'temp_umask', "reap_children",
     # logging
@@ -708,6 +709,15 @@ def bind_port(sock, host=HOST):
     port = sock.getsockname()[1]
     return port
 
+def bind_unix_socket(sock, addr):
+    """Bind a unix socket, raising SkipTest if PermissionError is raised."""
+    assert sock.family == socket.AF_UNIX
+    try:
+        sock.bind(addr)
+    except PermissionError:
+        sock.close()
+        raise unittest.SkipTest('cannot bind AF_UNIX sockets')
+
 def _is_ipv6_enabled():
     """Check whether IPv6 is enabled on this host."""
     if socket.has_ipv6:
index dbee593c54450f9bafc5fbdba53689be3792f11e..51c65737a441de5817a070ff52e9abd18d3bf612 100644 (file)
@@ -95,7 +95,9 @@ def bind_af_aware(sock, addr):
     if HAS_UNIX_SOCKETS and sock.family == socket.AF_UNIX:
         # Make sure the path doesn't exist.
         support.unlink(addr)
-    sock.bind(addr)
+        support.bind_unix_socket(sock, addr)
+    else:
+        sock.bind(addr)
 
 
 class HelperFunctionTests(unittest.TestCase):
index 65b2d5abda19d456ccc3968e3c7af8b19811d1b4..ce1ca153ed0b0386352377ba1bc24e87ec686cbf 100644 (file)
@@ -1888,7 +1888,8 @@ class _BasePathTest(object):
         try:
             sock.bind(str(P))
         except OSError as e:
-            if "AF_UNIX path too long" in str(e):
+            if (isinstance(e, PermissionError) or
+                    "AF_UNIX path too long" in str(e)):
                 self.skipTest("cannot bind Unix socket: " + str(e))
         self.assertTrue(P.is_socket())
         self.assertFalse(P.is_fifo())
index 59564c9063d6f47bd44b30563ee91b7430921a53..6b29e182348258a9f95c0ea7fb74775d0e6ccfc7 100644 (file)
@@ -278,8 +278,14 @@ class ThreadableTest:
 
     def clientRun(self, test_func):
         self.server_ready.wait()
-        self.clientSetUp()
-        self.client_ready.set()
+        try:
+            self.clientSetUp()
+        except BaseException as e:
+            self.queue.put(e)
+            self.clientTearDown()
+            return
+        finally:
+            self.client_ready.set()
         if self.server_crashed:
             self.clientTearDown()
             return
@@ -520,8 +526,11 @@ class ConnectedStreamTestMixin(SocketListeningTestMixin,
         self.serv_conn = self.cli
 
     def clientTearDown(self):
-        self.serv_conn.close()
-        self.serv_conn = None
+        try:
+            self.serv_conn.close()
+            self.serv_conn = None
+        except AttributeError:
+            pass
         super().clientTearDown()
 
 
@@ -540,7 +549,7 @@ class UnixSocketTestBase(SocketTestBase):
 
     def bindSock(self, sock):
         path = tempfile.mktemp(dir=self.dir_path)
-        sock.bind(path)
+        support.bind_unix_socket(sock, path)
         self.addCleanup(support.unlink, path)
 
 class UnixStreamBase(UnixSocketTestBase):
@@ -4631,7 +4640,7 @@ class TestUnixDomain(unittest.TestCase):
     def bind(self, sock, path):
         # Bind the socket
         try:
-            sock.bind(path)
+            support.bind_unix_socket(sock, path)
         except OSError as e:
             if str(e) == "AF_UNIX path too long":
                 self.skipTest(
index 97502a999b0d3df11e80696a63741ea0b63b4ed4..35ad11ff507bd230d91a7807549235bdded77878 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -47,6 +47,9 @@ Windows
 Tests
 -----
 
+- Issue #28683: Fix the tests that bind() a unix socket and raise
+  PermissionError on Android for a non-root user.
+
 - Issue #26939: Add the support.setswitchinterval() function to fix
   test_functools hanging on the Android armv7 qemu emulator.