mirror of
				https://github.com/python/cpython.git
				synced 2025-10-30 21:21:22 +00:00 
			
		
		
		
	Issue #28683: Fix the tests that bind() a unix socket and raise PermissionError
on Android for a non-root user.
This commit is contained in:
		
							parent
							
								
									1351c31aa9
								
							
						
					
					
						commit
						e88ed05006
					
				
					 5 changed files with 33 additions and 8 deletions
				
			
		|  | @ -96,6 +96,7 @@ | ||||||
|     "setswitchinterval", "android_not_root", |     "setswitchinterval", "android_not_root", | ||||||
|     # network |     # network | ||||||
|     "HOST", "IPV6_ENABLED", "find_unused_port", "bind_port", "open_urlresource", |     "HOST", "IPV6_ENABLED", "find_unused_port", "bind_port", "open_urlresource", | ||||||
|  |     "bind_unix_socket", | ||||||
|     # processes |     # processes | ||||||
|     'temp_umask', "reap_children", |     'temp_umask', "reap_children", | ||||||
|     # logging |     # logging | ||||||
|  | @ -708,6 +709,15 @@ def bind_port(sock, host=HOST): | ||||||
|     port = sock.getsockname()[1] |     port = sock.getsockname()[1] | ||||||
|     return port |     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(): | def _is_ipv6_enabled(): | ||||||
|     """Check whether IPv6 is enabled on this host.""" |     """Check whether IPv6 is enabled on this host.""" | ||||||
|     if socket.has_ipv6: |     if socket.has_ipv6: | ||||||
|  |  | ||||||
|  | @ -95,6 +95,8 @@ def bind_af_aware(sock, addr): | ||||||
|     if HAS_UNIX_SOCKETS and sock.family == socket.AF_UNIX: |     if HAS_UNIX_SOCKETS and sock.family == socket.AF_UNIX: | ||||||
|         # Make sure the path doesn't exist. |         # Make sure the path doesn't exist. | ||||||
|         support.unlink(addr) |         support.unlink(addr) | ||||||
|  |         support.bind_unix_socket(sock, addr) | ||||||
|  |     else: | ||||||
|         sock.bind(addr) |         sock.bind(addr) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1888,7 +1888,8 @@ def test_is_socket_true(self): | ||||||
|         try: |         try: | ||||||
|             sock.bind(str(P)) |             sock.bind(str(P)) | ||||||
|         except OSError as e: |         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.skipTest("cannot bind Unix socket: " + str(e)) | ||||||
|         self.assertTrue(P.is_socket()) |         self.assertTrue(P.is_socket()) | ||||||
|         self.assertFalse(P.is_fifo()) |         self.assertFalse(P.is_fifo()) | ||||||
|  |  | ||||||
|  | @ -278,7 +278,13 @@ def _tearDown(self): | ||||||
| 
 | 
 | ||||||
|     def clientRun(self, test_func): |     def clientRun(self, test_func): | ||||||
|         self.server_ready.wait() |         self.server_ready.wait() | ||||||
|  |         try: | ||||||
|             self.clientSetUp() |             self.clientSetUp() | ||||||
|  |         except BaseException as e: | ||||||
|  |             self.queue.put(e) | ||||||
|  |             self.clientTearDown() | ||||||
|  |             return | ||||||
|  |         finally: | ||||||
|             self.client_ready.set() |             self.client_ready.set() | ||||||
|         if self.server_crashed: |         if self.server_crashed: | ||||||
|             self.clientTearDown() |             self.clientTearDown() | ||||||
|  | @ -520,8 +526,11 @@ def clientSetUp(self): | ||||||
|         self.serv_conn = self.cli |         self.serv_conn = self.cli | ||||||
| 
 | 
 | ||||||
|     def clientTearDown(self): |     def clientTearDown(self): | ||||||
|  |         try: | ||||||
|             self.serv_conn.close() |             self.serv_conn.close() | ||||||
|             self.serv_conn = None |             self.serv_conn = None | ||||||
|  |         except AttributeError: | ||||||
|  |             pass | ||||||
|         super().clientTearDown() |         super().clientTearDown() | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -540,7 +549,7 @@ def setUp(self): | ||||||
| 
 | 
 | ||||||
|     def bindSock(self, sock): |     def bindSock(self, sock): | ||||||
|         path = tempfile.mktemp(dir=self.dir_path) |         path = tempfile.mktemp(dir=self.dir_path) | ||||||
|         sock.bind(path) |         support.bind_unix_socket(sock, path) | ||||||
|         self.addCleanup(support.unlink, path) |         self.addCleanup(support.unlink, path) | ||||||
| 
 | 
 | ||||||
| class UnixStreamBase(UnixSocketTestBase): | class UnixStreamBase(UnixSocketTestBase): | ||||||
|  | @ -4631,7 +4640,7 @@ def encoded(self, path): | ||||||
|     def bind(self, sock, path): |     def bind(self, sock, path): | ||||||
|         # Bind the socket |         # Bind the socket | ||||||
|         try: |         try: | ||||||
|             sock.bind(path) |             support.bind_unix_socket(sock, path) | ||||||
|         except OSError as e: |         except OSError as e: | ||||||
|             if str(e) == "AF_UNIX path too long": |             if str(e) == "AF_UNIX path too long": | ||||||
|                 self.skipTest( |                 self.skipTest( | ||||||
|  |  | ||||||
|  | @ -47,6 +47,9 @@ Windows | ||||||
| Tests | 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 | - Issue #26939: Add the support.setswitchinterval() function to fix | ||||||
|   test_functools hanging on the Android armv7 qemu emulator. |   test_functools hanging on the Android armv7 qemu emulator. | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Xavier de Gaye
						Xavier de Gaye