Skip to content

bpo-28759: Skip some tests on PermissionError raised by Android #4350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Lib/test/eintrdata/eintr_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import unittest

from test import support
android_not_root = support.android_not_root

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

# Use a fifo: until the child opens it for reading, the parent will
# block when trying to open it for writing.
support.unlink(filename)
os.mkfifo(filename)
try:
os.mkfifo(filename)
except PermissionError as e:
self.skipTest('os.mkfifo(): %s' % e)
self.addCleanup(support.unlink, filename)

code = '\n'.join((
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"check__all__", "requires_android_level", "requires_multiprocessing_queue",
# sys
"is_jython", "is_android", "check_impl_detail", "unix_shell",
"setswitchinterval", "android_not_root",
"setswitchinterval",
# network
"HOST", "IPV6_ENABLED", "find_unused_port", "bind_port", "open_urlresource",
"bind_unix_socket",
Expand Down Expand Up @@ -780,7 +780,6 @@ def dec(*args, **kwargs):
except AttributeError:
# sys.getandroidapilevel() is only available on Android
is_android = False
android_not_root = (is_android and os.geteuid() != 0)

if sys.platform != 'win32':
unix_shell = '/system/bin/sh' if is_android else '/bin/sh'
Expand Down
13 changes: 8 additions & 5 deletions Lib/test/test_genericpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import unittest
import warnings
from test import support
android_not_root = support.android_not_root


def create_file(filename, data=b'foo'):
Expand Down Expand Up @@ -213,9 +212,11 @@ def _test_samefile_on_link_func(self, func):
def test_samefile_on_symlink(self):
self._test_samefile_on_link_func(os.symlink)

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

def test_samestat(self):
test_fn1 = support.TESTFN
Expand Down Expand Up @@ -253,9 +254,11 @@ def _test_samestat_on_link_func(self, func):
def test_samestat_on_symlink(self):
self._test_samestat_on_link_func(os.symlink)

@unittest.skipIf(android_not_root, "hard links not allowed, non root user")
def test_samestat_on_link(self):
self._test_samestat_on_link_func(os.link)
try:
self._test_samestat_on_link_func(os.link)
except PermissionError as e:
self.skipTest('os.link(): %s' % e)

def test_sameopenfile(self):
filename = support.TESTFN
Expand Down
7 changes: 4 additions & 3 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from unittest import mock

from test import support
android_not_root = support.android_not_root
TESTFN = support.TESTFN

try:
Expand Down Expand Up @@ -1911,10 +1910,12 @@ def test_is_fifo_false(self):
self.assertFalse((P / 'fileA' / 'bah').is_fifo())

@unittest.skipUnless(hasattr(os, "mkfifo"), "os.mkfifo() required")
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
def test_is_fifo_true(self):
P = self.cls(BASE, 'myfifo')
os.mkfifo(str(P))
try:
os.mkfifo(str(P))
except PermissionError as e:
self.skipTest('os.mkfifo(): %s' % e)
self.assertTrue(P.is_fifo())
self.assertFalse(P.is_socket())
self.assertFalse(P.is_file())
Expand Down
26 changes: 15 additions & 11 deletions Lib/test/test_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from test import support
from test.support.script_helper import assert_python_ok
android_not_root = support.android_not_root

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

@unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()")
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
def test_mkfifo(self):
support.unlink(support.TESTFN)
posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR)
try:
posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR)
except PermissionError as e:
self.skipTest('posix.mkfifo(): %s' % e)
self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))

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

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

@unittest.skipUnless(hasattr(posix, 'stat'), 'test needs posix.stat()')
@unittest.skipUnless(hasattr(posix, 'makedev'), 'test needs posix.makedev()')
Expand Down Expand Up @@ -1018,11 +1018,13 @@ def test_utime_dir_fd(self):
posix.close(f)

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

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

@unittest.skipUnless(os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
def test_mkfifo_dir_fd(self):
support.unlink(support.TESTFN)
f = posix.open(posix.getcwd(), posix.O_RDONLY)
try:
posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR, dir_fd=f)
try:
posix.mkfifo(support.TESTFN,
stat.S_IRUSR | stat.S_IWUSR, dir_fd=f)
except PermissionError as e:
self.skipTest('posix.mkfifo(): %s' % e)
self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
finally:
posix.close(f)
Expand Down
20 changes: 13 additions & 7 deletions Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import zipfile

from test import support
from test.support import TESTFN, android_not_root
from test.support import TESTFN

TESTFN2 = TESTFN + "2"

Expand Down Expand Up @@ -769,7 +769,6 @@ def test_copytree_winerror(self, mock_patch):

@unittest.skipIf(os.name == 'nt', 'temporarily disabled on Windows')
@unittest.skipUnless(hasattr(os, 'link'), 'requires os.link')
@unittest.skipIf(android_not_root, "hard links not allowed, non root user")
def test_dont_copy_file_onto_link_to_itself(self):
# bug 851123.
os.mkdir(TESTFN)
Expand All @@ -778,7 +777,10 @@ def test_dont_copy_file_onto_link_to_itself(self):
try:
with open(src, 'w') as f:
f.write('cheddar')
os.link(src, dst)
try:
os.link(src, dst)
except PermissionError as e:
self.skipTest('os.link(): %s' % e)
self.assertRaises(shutil.SameFileError, shutil.copyfile, src, dst)
with open(src, 'r') as f:
self.assertEqual(f.read(), 'cheddar')
Expand Down Expand Up @@ -822,9 +824,11 @@ def test_rmtree_on_symlink(self):

# Issue #3002: copyfile and copytree block indefinitely on named pipes
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
def test_copyfile_named_pipe(self):
os.mkfifo(TESTFN)
try:
os.mkfifo(TESTFN)
except PermissionError as e:
self.skipTest('os.mkfifo(): %s' % e)
try:
self.assertRaises(shutil.SpecialFileError,
shutil.copyfile, TESTFN, TESTFN2)
Expand All @@ -833,7 +837,6 @@ def test_copyfile_named_pipe(self):
finally:
os.remove(TESTFN)

@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
@support.skip_unless_symlink
def test_copytree_named_pipe(self):
Expand All @@ -842,7 +845,10 @@ def test_copytree_named_pipe(self):
subdir = os.path.join(TESTFN, "subdir")
os.mkdir(subdir)
pipe = os.path.join(subdir, "mypipe")
os.mkfifo(pipe)
try:
os.mkfifo(pipe)
except PermissionError as e:
self.skipTest('os.mkfifo(): %s' % e)
try:
shutil.copytree(TESTFN, TESTFN2)
except shutil.Error as e:
Expand Down
8 changes: 5 additions & 3 deletions Lib/test/test_stat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import os
import sys
from test.support import TESTFN, import_fresh_module, android_not_root
from test.support import TESTFN, import_fresh_module

c_stat = import_fresh_module('stat', fresh=['_stat'])
py_stat = import_fresh_module('stat', blocked=['_stat'])
Expand Down Expand Up @@ -168,9 +168,11 @@ def test_link(self):
self.assertS_IS("LNK", st_mode)

@unittest.skipUnless(hasattr(os, 'mkfifo'), 'os.mkfifo not available')
@unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
def test_fifo(self):
os.mkfifo(TESTFN, 0o700)
try:
os.mkfifo(TESTFN, 0o700)
except PermissionError as e:
self.skipTest('os.mkfifo(): %s' % e)
st_mode, modestr = self.get_mode()
self.assertEqual(modestr, 'prwx------')
self.assertS_IS("FIFO", st_mode)
Expand Down