Skip to content

Commit 92c2ca7

Browse files
authored
bpo-28759: Skip some tests on PermissionError raised by Android (pythonGH-4350)
Access to mkfifo(), mknod() and hard link creation is controled by SELinux on Android. Also remove test.support.android_not_root.
1 parent e0582a3 commit 92c2ca7

File tree

7 files changed

+50
-34
lines changed

7 files changed

+50
-34
lines changed

Lib/test/eintrdata/eintr_tester.py

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

2222
from test import support
23-
android_not_root = support.android_not_root
2423

2524
@contextlib.contextmanager
2625
def kill_on_error(proc):
@@ -312,14 +311,16 @@ def test_accept(self):
312311
# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=203162
313312
@support.requires_freebsd_version(10, 3)
314313
@unittest.skipUnless(hasattr(os, 'mkfifo'), 'needs mkfifo()')
315-
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
316314
def _test_open(self, do_open_close_reader, do_open_close_writer):
317315
filename = support.TESTFN
318316

319317
# Use a fifo: until the child opens it for reading, the parent will
320318
# block when trying to open it for writing.
321319
support.unlink(filename)
322-
os.mkfifo(filename)
320+
try:
321+
os.mkfifo(filename)
322+
except PermissionError as e:
323+
self.skipTest('os.mkfifo(): %s' % e)
323324
self.addCleanup(support.unlink, filename)
324325

325326
code = '\n'.join((

Lib/test/support/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"check__all__", "requires_android_level", "requires_multiprocessing_queue",
9191
# sys
9292
"is_jython", "is_android", "check_impl_detail", "unix_shell",
93-
"setswitchinterval", "android_not_root",
93+
"setswitchinterval",
9494
# network
9595
"HOST", "IPV6_ENABLED", "find_unused_port", "bind_port", "open_urlresource",
9696
"bind_unix_socket",
@@ -780,7 +780,6 @@ def dec(*args, **kwargs):
780780
except AttributeError:
781781
# sys.getandroidapilevel() is only available on Android
782782
is_android = False
783-
android_not_root = (is_android and os.geteuid() != 0)
784783

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

Lib/test/test_genericpath.py

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

1312

1413
def create_file(filename, data=b'foo'):
@@ -213,9 +212,11 @@ def _test_samefile_on_link_func(self, func):
213212
def test_samefile_on_symlink(self):
214213
self._test_samefile_on_link_func(os.symlink)
215214

216-
@unittest.skipIf(android_not_root, "hard links not allowed, non root user")
217215
def test_samefile_on_link(self):
218-
self._test_samefile_on_link_func(os.link)
216+
try:
217+
self._test_samefile_on_link_func(os.link)
218+
except PermissionError as e:
219+
self.skipTest('os.link(): %s' % e)
219220

220221
def test_samestat(self):
221222
test_fn1 = support.TESTFN
@@ -253,9 +254,11 @@ def _test_samestat_on_link_func(self, func):
253254
def test_samestat_on_symlink(self):
254255
self._test_samestat_on_link_func(os.symlink)
255256

256-
@unittest.skipIf(android_not_root, "hard links not allowed, non root user")
257257
def test_samestat_on_link(self):
258-
self._test_samestat_on_link_func(os.link)
258+
try:
259+
self._test_samestat_on_link_func(os.link)
260+
except PermissionError as e:
261+
self.skipTest('os.link(): %s' % e)
259262

260263
def test_sameopenfile(self):
261264
filename = support.TESTFN

Lib/test/test_pathlib.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from unittest import mock
1212

1313
from test import support
14-
android_not_root = support.android_not_root
1514
TESTFN = support.TESTFN
1615

1716
try:
@@ -1911,10 +1910,12 @@ def test_is_fifo_false(self):
19111910
self.assertFalse((P / 'fileA' / 'bah').is_fifo())
19121911

19131912
@unittest.skipUnless(hasattr(os, "mkfifo"), "os.mkfifo() required")
1914-
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
19151913
def test_is_fifo_true(self):
19161914
P = self.cls(BASE, 'myfifo')
1917-
os.mkfifo(str(P))
1915+
try:
1916+
os.mkfifo(str(P))
1917+
except PermissionError as e:
1918+
self.skipTest('os.mkfifo(): %s' % e)
19181919
self.assertTrue(P.is_fifo())
19191920
self.assertFalse(P.is_socket())
19201921
self.assertFalse(P.is_file())

Lib/test/test_posix.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from test import support
44
from test.support.script_helper import assert_python_ok
5-
android_not_root = support.android_not_root
65

76
# Skip these tests if there is no posix module.
87
posix = support.import_module('posix')
@@ -504,15 +503,16 @@ def test_stat(self):
504503
posix.stat, list(os.fsencode(support.TESTFN)))
505504

506505
@unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()")
507-
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
508506
def test_mkfifo(self):
509507
support.unlink(support.TESTFN)
510-
posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR)
508+
try:
509+
posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR)
510+
except PermissionError as e:
511+
self.skipTest('posix.mkfifo(): %s' % e)
511512
self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
512513

513514
@unittest.skipUnless(hasattr(posix, 'mknod') and hasattr(stat, 'S_IFIFO'),
514515
"don't have mknod()/S_IFIFO")
515-
@unittest.skipIf(android_not_root, "mknod not allowed, non root user")
516516
def test_mknod(self):
517517
# Test using mknod() to create a FIFO (the only use specified
518518
# by POSIX).
@@ -523,7 +523,7 @@ def test_mknod(self):
523523
except OSError as e:
524524
# Some old systems don't allow unprivileged users to use
525525
# mknod(), or only support creating device nodes.
526-
self.assertIn(e.errno, (errno.EPERM, errno.EINVAL))
526+
self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
527527
else:
528528
self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
529529

@@ -533,7 +533,7 @@ def test_mknod(self):
533533
posix.mknod(path=support.TESTFN, mode=mode, device=0,
534534
dir_fd=None)
535535
except OSError as e:
536-
self.assertIn(e.errno, (errno.EPERM, errno.EINVAL))
536+
self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
537537

538538
@unittest.skipUnless(hasattr(posix, 'stat'), 'test needs posix.stat()')
539539
@unittest.skipUnless(hasattr(posix, 'makedev'), 'test needs posix.makedev()')
@@ -1018,11 +1018,13 @@ def test_utime_dir_fd(self):
10181018
posix.close(f)
10191019

10201020
@unittest.skipUnless(os.link in os.supports_dir_fd, "test needs dir_fd support in os.link()")
1021-
@unittest.skipIf(android_not_root, "hard link not allowed, non root user")
10221021
def test_link_dir_fd(self):
10231022
f = posix.open(posix.getcwd(), posix.O_RDONLY)
10241023
try:
10251024
posix.link(support.TESTFN, support.TESTFN + 'link', src_dir_fd=f, dst_dir_fd=f)
1025+
except PermissionError as e:
1026+
self.skipTest('posix.link(): %s' % e)
1027+
else:
10261028
# should have same inodes
10271029
self.assertEqual(posix.stat(support.TESTFN)[1],
10281030
posix.stat(support.TESTFN + 'link')[1])
@@ -1042,7 +1044,6 @@ def test_mkdir_dir_fd(self):
10421044

10431045
@unittest.skipUnless((os.mknod in os.supports_dir_fd) and hasattr(stat, 'S_IFIFO'),
10441046
"test requires both stat.S_IFIFO and dir_fd support for os.mknod()")
1045-
@unittest.skipIf(android_not_root, "mknod not allowed, non root user")
10461047
def test_mknod_dir_fd(self):
10471048
# Test using mknodat() to create a FIFO (the only use specified
10481049
# by POSIX).
@@ -1054,7 +1055,7 @@ def test_mknod_dir_fd(self):
10541055
except OSError as e:
10551056
# Some old systems don't allow unprivileged users to use
10561057
# mknod(), or only support creating device nodes.
1057-
self.assertIn(e.errno, (errno.EPERM, errno.EINVAL))
1058+
self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
10581059
else:
10591060
self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
10601061
finally:
@@ -1126,12 +1127,15 @@ def test_unlink_dir_fd(self):
11261127
posix.close(f)
11271128

11281129
@unittest.skipUnless(os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
1129-
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
11301130
def test_mkfifo_dir_fd(self):
11311131
support.unlink(support.TESTFN)
11321132
f = posix.open(posix.getcwd(), posix.O_RDONLY)
11331133
try:
1134-
posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR, dir_fd=f)
1134+
try:
1135+
posix.mkfifo(support.TESTFN,
1136+
stat.S_IRUSR | stat.S_IWUSR, dir_fd=f)
1137+
except PermissionError as e:
1138+
self.skipTest('posix.mkfifo(): %s' % e)
11351139
self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
11361140
finally:
11371141
posix.close(f)

Lib/test/test_shutil.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import zipfile
2323

2424
from test import support
25-
from test.support import TESTFN, android_not_root
25+
from test.support import TESTFN
2626

2727
TESTFN2 = TESTFN + "2"
2828

@@ -769,7 +769,6 @@ def test_copytree_winerror(self, mock_patch):
769769

770770
@unittest.skipIf(os.name == 'nt', 'temporarily disabled on Windows')
771771
@unittest.skipUnless(hasattr(os, 'link'), 'requires os.link')
772-
@unittest.skipIf(android_not_root, "hard links not allowed, non root user")
773772
def test_dont_copy_file_onto_link_to_itself(self):
774773
# bug 851123.
775774
os.mkdir(TESTFN)
@@ -778,7 +777,10 @@ def test_dont_copy_file_onto_link_to_itself(self):
778777
try:
779778
with open(src, 'w') as f:
780779
f.write('cheddar')
781-
os.link(src, dst)
780+
try:
781+
os.link(src, dst)
782+
except PermissionError as e:
783+
self.skipTest('os.link(): %s' % e)
782784
self.assertRaises(shutil.SameFileError, shutil.copyfile, src, dst)
783785
with open(src, 'r') as f:
784786
self.assertEqual(f.read(), 'cheddar')
@@ -822,9 +824,11 @@ def test_rmtree_on_symlink(self):
822824

823825
# Issue #3002: copyfile and copytree block indefinitely on named pipes
824826
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
825-
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
826827
def test_copyfile_named_pipe(self):
827-
os.mkfifo(TESTFN)
828+
try:
829+
os.mkfifo(TESTFN)
830+
except PermissionError as e:
831+
self.skipTest('os.mkfifo(): %s' % e)
828832
try:
829833
self.assertRaises(shutil.SpecialFileError,
830834
shutil.copyfile, TESTFN, TESTFN2)
@@ -833,7 +837,6 @@ def test_copyfile_named_pipe(self):
833837
finally:
834838
os.remove(TESTFN)
835839

836-
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
837840
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
838841
@support.skip_unless_symlink
839842
def test_copytree_named_pipe(self):
@@ -842,7 +845,10 @@ def test_copytree_named_pipe(self):
842845
subdir = os.path.join(TESTFN, "subdir")
843846
os.mkdir(subdir)
844847
pipe = os.path.join(subdir, "mypipe")
845-
os.mkfifo(pipe)
848+
try:
849+
os.mkfifo(pipe)
850+
except PermissionError as e:
851+
self.skipTest('os.mkfifo(): %s' % e)
846852
try:
847853
shutil.copytree(TESTFN, TESTFN2)
848854
except shutil.Error as e:

Lib/test/test_stat.py

Lines changed: 5 additions & 3 deletions
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, android_not_root
4+
from test.support import TESTFN, import_fresh_module
55

66
c_stat = import_fresh_module('stat', fresh=['_stat'])
77
py_stat = import_fresh_module('stat', blocked=['_stat'])
@@ -168,9 +168,11 @@ 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")
172171
def test_fifo(self):
173-
os.mkfifo(TESTFN, 0o700)
172+
try:
173+
os.mkfifo(TESTFN, 0o700)
174+
except PermissionError as e:
175+
self.skipTest('os.mkfifo(): %s' % e)
174176
st_mode, modestr = self.get_mode()
175177
self.assertEqual(modestr, 'prwx------')
176178
self.assertS_IS("FIFO", st_mode)

0 commit comments

Comments
 (0)