diff options
Diffstat (limited to 'Lib/test/test_posix.py')
-rw-r--r-- | Lib/test/test_posix.py | 74 |
1 files changed, 48 insertions, 26 deletions
diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index 2047e52..78f7592 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -131,32 +131,54 @@ class PosixTester(unittest.TestCase): if hasattr(posix, 'stat'): self.assertTrue(posix.stat(support.TESTFN)) - if hasattr(posix, 'chown'): - def test_chown(self): - # raise an OSError if the file does not exist - os.unlink(support.TESTFN) - self.assertRaises(OSError, posix.chown, support.TESTFN, -1, -1) - - # re-create the file - open(support.TESTFN, 'w').close() - if os.getuid() == 0: - try: - # Many linux distros have a nfsnobody user as MAX_UID-2 - # that makes a good test case for signedness issues. - # http://bugs.python.org/issue1747858 - # This part of the test only runs when run as root. - # Only scary people run their tests as root. - ent = pwd.getpwnam('nfsnobody') - posix.chown(support.TESTFN, ent.pw_uid, ent.pw_gid) - except KeyError: - pass - else: - # non-root cannot chown to root, raises OSError - self.assertRaises(OSError, posix.chown, - support.TESTFN, 0, 0) - - # test a successful chown call - posix.chown(support.TESTFN, os.getuid(), os.getgid()) + def _test_all_chown_common(self, chown_func, first_param): + """Common code for chown, fchown and lchown tests.""" + if os.getuid() == 0: + try: + # Many linux distros have a nfsnobody user as MAX_UID-2 + # that makes a good test case for signedness issues. + # http://bugs.python.org/issue1747858 + # This part of the test only runs when run as root. + # Only scary people run their tests as root. + ent = pwd.getpwnam('nfsnobody') + chown_func(first_param, ent.pw_uid, ent.pw_gid) + except KeyError: + pass + else: + # non-root cannot chown to root, raises OSError + self.assertRaises(OSError, chown_func, + first_param, 0, 0) + # test a successful chown call + chown_func(first_param, os.getuid(), os.getgid()) + + @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()") + def test_chown(self): + # raise an OSError if the file does not exist + os.unlink(support.TESTFN) + self.assertRaises(OSError, posix.chown, support.TESTFN, -1, -1) + + # re-create the file + open(support.TESTFN, 'w').close() + self._test_all_chown_common(posix.chown, support.TESTFN) + + @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()") + def test_fchown(self): + os.unlink(support.TESTFN) + + # re-create the file + test_file = open(support.TESTFN, 'w') + try: + fd = test_file.fileno() + self._test_all_chown_common(posix.fchown, fd) + finally: + test_file.close() + + @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()") + def test_lchown(self): + os.unlink(support.TESTFN) + # create a symlink + os.symlink('/tmp/dummy-symlink-target', support.TESTFN) + self._test_all_chown_common(posix.lchown, support.TESTFN) def test_chdir(self): if hasattr(posix, 'chdir'): |