Skip to content

Commit 3a4e989

Browse files
author
Xavier de Gaye
committed
Issue python#28759: Fix the tests that fail with PermissionError when run as
a non-root user on Android where access rights are controled by SELinux MAC.
1 parent fb24eea commit 3a4e989

7 files changed

Lines changed: 22 additions & 3 deletions

File tree

Lib/test/eintrdata/eintr_tester.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import unittest
2121

2222
from test import support
23+
android_not_root = support.android_not_root
2324

2425
@contextlib.contextmanager
2526
def kill_on_error(proc):
@@ -311,6 +312,7 @@ def test_accept(self):
311312
# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=203162
312313
@support.requires_freebsd_version(10, 3)
313314
@unittest.skipUnless(hasattr(os, 'mkfifo'), 'needs mkfifo()')
315+
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
314316
def _test_open(self, do_open_close_reader, do_open_close_writer):
315317
filename = support.TESTFN
316318

Lib/test/support/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
"check__all__", "requires_android_level", "requires_multiprocessing_queue",
9494
# sys
9595
"is_jython", "is_android", "check_impl_detail", "unix_shell",
96-
"setswitchinterval",
96+
"setswitchinterval", "android_not_root",
9797
# network
9898
"HOST", "IPV6_ENABLED", "find_unused_port", "bind_port", "open_urlresource",
9999
# processes
@@ -769,6 +769,7 @@ def dec(*args, **kwargs):
769769

770770
_ANDROID_API_LEVEL = sysconfig.get_config_var('ANDROID_API_LEVEL')
771771
is_android = (_ANDROID_API_LEVEL is not None and _ANDROID_API_LEVEL > 0)
772+
android_not_root = (is_android and os.geteuid() != 0)
772773

773774
if sys.platform != 'win32':
774775
unix_shell = '/system/bin/sh' if is_android else '/bin/sh'

Lib/test/test_genericpath.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import unittest
99
import warnings
1010
from test import support
11+
android_not_root = support.android_not_root
1112

1213

1314
def create_file(filename, data=b'foo'):
@@ -212,6 +213,7 @@ def _test_samefile_on_link_func(self, func):
212213
def test_samefile_on_symlink(self):
213214
self._test_samefile_on_link_func(os.symlink)
214215

216+
@unittest.skipIf(android_not_root, "hard links not allowed, non root user")
215217
def test_samefile_on_link(self):
216218
self._test_samefile_on_link_func(os.link)
217219

@@ -251,6 +253,7 @@ def _test_samestat_on_link_func(self, func):
251253
def test_samestat_on_symlink(self):
252254
self._test_samestat_on_link_func(os.symlink)
253255

256+
@unittest.skipIf(android_not_root, "hard links not allowed, non root user")
254257
def test_samestat_on_link(self):
255258
self._test_samestat_on_link_func(os.link)
256259

Lib/test/test_pathlib.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import unittest
1111

1212
from test import support
13+
android_not_root = support.android_not_root
1314
TESTFN = support.TESTFN
1415

1516
try:
@@ -1864,6 +1865,7 @@ def test_is_fifo_false(self):
18641865
self.assertFalse((P / 'fileA' / 'bah').is_fifo())
18651866

18661867
@unittest.skipUnless(hasattr(os, "mkfifo"), "os.mkfifo() required")
1868+
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
18671869
def test_is_fifo_true(self):
18681870
P = self.cls(BASE, 'myfifo')
18691871
os.mkfifo(str(P))

Lib/test/test_posix.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"Test posix functions"
22

33
from test import support
4+
android_not_root = support.android_not_root
45

56
# Skip these tests if there is no posix module.
67
posix = support.import_module('posix')
@@ -422,13 +423,15 @@ def test_stat(self):
422423
posix.stat, list(os.fsencode(support.TESTFN)))
423424

424425
@unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()")
426+
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
425427
def test_mkfifo(self):
426428
support.unlink(support.TESTFN)
427429
posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR)
428430
self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
429431

430432
@unittest.skipUnless(hasattr(posix, 'mknod') and hasattr(stat, 'S_IFIFO'),
431433
"don't have mknod()/S_IFIFO")
434+
@unittest.skipIf(android_not_root, "mknod not allowed, non root user")
432435
def test_mknod(self):
433436
# Test using mknod() to create a FIFO (the only use specified
434437
# by POSIX).
@@ -907,6 +910,7 @@ def test_utime_dir_fd(self):
907910
posix.close(f)
908911

909912
@unittest.skipUnless(os.link in os.supports_dir_fd, "test needs dir_fd support in os.link()")
913+
@unittest.skipIf(android_not_root, "hard link not allowed, non root user")
910914
def test_link_dir_fd(self):
911915
f = posix.open(posix.getcwd(), posix.O_RDONLY)
912916
try:
@@ -930,6 +934,7 @@ def test_mkdir_dir_fd(self):
930934

931935
@unittest.skipUnless((os.mknod in os.supports_dir_fd) and hasattr(stat, 'S_IFIFO'),
932936
"test requires both stat.S_IFIFO and dir_fd support for os.mknod()")
937+
@unittest.skipIf(android_not_root, "mknod not allowed, non root user")
933938
def test_mknod_dir_fd(self):
934939
# Test using mknodat() to create a FIFO (the only use specified
935940
# by POSIX).
@@ -1013,6 +1018,7 @@ def test_unlink_dir_fd(self):
10131018
posix.close(f)
10141019

10151020
@unittest.skipUnless(os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
1021+
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
10161022
def test_mkfifo_dir_fd(self):
10171023
support.unlink(support.TESTFN)
10181024
f = posix.open(posix.getcwd(), posix.O_RDONLY)

Lib/test/test_shutil.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
import warnings
2323

2424
from test import support
25-
from test.support import TESTFN, check_warnings, captured_stdout, requires_zlib
25+
from test.support import (TESTFN, check_warnings, captured_stdout,
26+
requires_zlib, android_not_root)
2627

2728
try:
2829
import bz2
@@ -787,6 +788,7 @@ def test_copytree_winerror(self, mock_patch):
787788

788789
@unittest.skipIf(os.name == 'nt', 'temporarily disabled on Windows')
789790
@unittest.skipUnless(hasattr(os, 'link'), 'requires os.link')
791+
@unittest.skipIf(android_not_root, "hard links not allowed, non root user")
790792
def test_dont_copy_file_onto_link_to_itself(self):
791793
# bug 851123.
792794
os.mkdir(TESTFN)
@@ -839,6 +841,7 @@ def test_rmtree_on_symlink(self):
839841

840842
# Issue #3002: copyfile and copytree block indefinitely on named pipes
841843
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
844+
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
842845
def test_copyfile_named_pipe(self):
843846
os.mkfifo(TESTFN)
844847
try:
@@ -849,6 +852,7 @@ def test_copyfile_named_pipe(self):
849852
finally:
850853
os.remove(TESTFN)
851854

855+
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
852856
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
853857
@support.skip_unless_symlink
854858
def test_copytree_named_pipe(self):

Lib/test/test_stat.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
import os
33
import sys
4-
from test.support import TESTFN, import_fresh_module
4+
from test.support import TESTFN, import_fresh_module, android_not_root
55

66
c_stat = import_fresh_module('stat', fresh=['_stat'])
77
py_stat = import_fresh_module('stat', blocked=['_stat'])
@@ -168,6 +168,7 @@ def test_link(self):
168168
self.assertS_IS("LNK", st_mode)
169169

170170
@unittest.skipUnless(hasattr(os, 'mkfifo'), 'os.mkfifo not available')
171+
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
171172
def test_fifo(self):
172173
os.mkfifo(TESTFN, 0o700)
173174
st_mode, modestr = self.get_mode()

0 commit comments

Comments
 (0)