cpython: a0baf5347cd1 (original) (raw)
--- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -234,30 +234,42 @@ class PosixTester(unittest.TestCase): def _test_all_chown_common(self, chown_func, first_param, stat_func): """Common code for chown, fchown and lchown tests."""
def check_stat():[](#l1.7)
def check_stat(uid, gid):[](#l1.8) if stat_func is not None:[](#l1.9) stat = stat_func(first_param)[](#l1.10)
self.assertEqual(stat.st_uid, os.getuid())[](#l1.11)
self.assertEqual(stat.st_gid, os.getgid())[](#l1.12)
self.assertEqual(stat.st_uid, uid)[](#l1.13)
self.assertEqual(stat.st_gid, gid)[](#l1.14)
uid = os.getuid()[](#l1.15)
gid = os.getgid()[](#l1.16) # test a successful chown call[](#l1.17)
chown_func(first_param, os.getuid(), os.getgid())[](#l1.18)
check_stat()[](#l1.19)
chown_func(first_param, -1, os.getgid())[](#l1.20)
check_stat()[](#l1.21)
chown_func(first_param, os.getuid(), -1)[](#l1.22)
check_stat()[](#l1.23)
chown_func(first_param, uid, gid)[](#l1.24)
check_stat(uid, gid)[](#l1.25)
chown_func(first_param, -1, gid)[](#l1.26)
check_stat(uid, gid)[](#l1.27)
chown_func(first_param, uid, -1)[](#l1.28)
check_stat(uid, gid)[](#l1.29)
if os.getuid() == 0:[](#l1.31)
try:[](#l1.32)
# Many linux distros have a nfsnobody user as MAX_UID-2[](#l1.33)
# that makes a good test case for signedness issues.[](#l1.34)
# http://bugs.python.org/issue1747858[](#l1.35)
# This part of the test only runs when run as root.[](#l1.36)
# Only scary people run their tests as root.[](#l1.37)
ent = pwd.getpwnam('nfsnobody')[](#l1.38)
chown_func(first_param, ent.pw_uid, ent.pw_gid)[](#l1.39)
except KeyError:[](#l1.40)
pass[](#l1.41)
if uid == 0:[](#l1.42)
# Try an amusingly large uid/gid to make sure we handle[](#l1.43)
# large unsigned values. (chown lets you use any[](#l1.44)
# uid/gid you like, even if they aren't defined.)[](#l1.45)
#[](#l1.46)
# This problem keeps coming up:[](#l1.47)
# http://bugs.python.org/issue1747858[](#l1.48)
# http://bugs.python.org/issue4591[](#l1.49)
# http://bugs.python.org/issue15301[](#l1.50)
# Hopefully the fix in 4591 fixes it for good
#[](#l1.52)
# This part of the test only runs when run as root.[](#l1.53)
# Only scary people run their tests as root.[](#l1.54)
big_value = 2**31[](#l1.56)
chown_func(first_param, big_value, big_value)[](#l1.57)
check_stat(big_value, big_value)[](#l1.58)
chown_func(first_param, -1, -1)[](#l1.59)
check_stat(big_value, big_value)[](#l1.60)
chown_func(first_param, uid, gid)[](#l1.61)
check_stat(uid, gid)[](#l1.62) elif platform.system() in ('HP-UX', 'SunOS'):[](#l1.63) # HP-UX and Solaris can allow a non-root user to chown() to root[](#l1.64) # (issue #5113)[](#l1.65)
@@ -266,11 +278,17 @@ class PosixTester(unittest.TestCase): else: # non-root cannot chown to root, raises OSError self.assertRaises(OSError, chown_func, first_param, 0, 0)
check_stat()[](#l1.70)
check_stat(uid, gid)[](#l1.71) self.assertRaises(OSError, chown_func, first_param, -1, 0)[](#l1.72)
check_stat()[](#l1.73)
check_stat(uid, gid)[](#l1.74) self.assertRaises(OSError, chown_func, first_param, 0, -1)[](#l1.75)
check_stat()[](#l1.76)
check_stat(uid, gid)[](#l1.77)
# test illegal types[](#l1.78)
for t in str, float:[](#l1.79)
self.assertRaises(TypeError, chown_func, first_param, t(uid), gid)[](#l1.80)
check_stat(uid, gid)[](#l1.81)
self.assertRaises(TypeError, chown_func, first_param, uid, t(gid))[](#l1.82)
check_stat(uid, gid)[](#l1.83)
@unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()") def test_chown(self):