Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 54db2fd

Browse files
Issue #15301: Enhance os.*chown() testing. Based on patch by Larry Hastings.
1 parent 774a39f commit 54db2fd

1 file changed

Lines changed: 42 additions & 24 deletions

File tree

Lib/test/test_posix.py

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -234,30 +234,42 @@ def test_mknod(self):
234234

235235
def _test_all_chown_common(self, chown_func, first_param, stat_func):
236236
"""Common code for chown, fchown and lchown tests."""
237-
def check_stat():
237+
def check_stat(uid, gid):
238238
if stat_func is not None:
239239
stat = stat_func(first_param)
240-
self.assertEqual(stat.st_uid, os.getuid())
241-
self.assertEqual(stat.st_gid, os.getgid())
240+
self.assertEqual(stat.st_uid, uid)
241+
self.assertEqual(stat.st_gid, gid)
242+
uid = os.getuid()
243+
gid = os.getgid()
242244
# test a successful chown call
243-
chown_func(first_param, os.getuid(), os.getgid())
244-
check_stat()
245-
chown_func(first_param, -1, os.getgid())
246-
check_stat()
247-
chown_func(first_param, os.getuid(), -1)
248-
check_stat()
249-
250-
if os.getuid() == 0:
251-
try:
252-
# Many linux distros have a nfsnobody user as MAX_UID-2
253-
# that makes a good test case for signedness issues.
254-
# http://bugs.python.org/issue1747858
255-
# This part of the test only runs when run as root.
256-
# Only scary people run their tests as root.
257-
ent = pwd.getpwnam('nfsnobody')
258-
chown_func(first_param, ent.pw_uid, ent.pw_gid)
259-
except KeyError:
260-
pass
245+
chown_func(first_param, uid, gid)
246+
check_stat(uid, gid)
247+
chown_func(first_param, -1, gid)
248+
check_stat(uid, gid)
249+
chown_func(first_param, uid, -1)
250+
check_stat(uid, gid)
251+
252+
if uid == 0:
253+
# Try an amusingly large uid/gid to make sure we handle
254+
# large unsigned values. (chown lets you use any
255+
# uid/gid you like, even if they aren't defined.)
256+
#
257+
# This problem keeps coming up:
258+
# http://bugs.python.org/issue1747858
259+
# http://bugs.python.org/issue4591
260+
# http://bugs.python.org/issue15301
261+
# Hopefully the fix in 4591 fixes it for good!
262+
#
263+
# This part of the test only runs when run as root.
264+
# Only scary people run their tests as root.
265+
266+
big_value = 2**31
267+
chown_func(first_param, big_value, big_value)
268+
check_stat(big_value, big_value)
269+
chown_func(first_param, -1, -1)
270+
check_stat(big_value, big_value)
271+
chown_func(first_param, uid, gid)
272+
check_stat(uid, gid)
261273
elif platform.system() in ('HP-UX', 'SunOS'):
262274
# HP-UX and Solaris can allow a non-root user to chown() to root
263275
# (issue #5113)
@@ -266,11 +278,17 @@ def check_stat():
266278
else:
267279
# non-root cannot chown to root, raises OSError
268280
self.assertRaises(OSError, chown_func, first_param, 0, 0)
269-
check_stat()
281+
check_stat(uid, gid)
270282
self.assertRaises(OSError, chown_func, first_param, -1, 0)
271-
check_stat()
283+
check_stat(uid, gid)
272284
self.assertRaises(OSError, chown_func, first_param, 0, -1)
273-
check_stat()
285+
check_stat(uid, gid)
286+
# test illegal types
287+
for t in str, float:
288+
self.assertRaises(TypeError, chown_func, first_param, t(uid), gid)
289+
check_stat(uid, gid)
290+
self.assertRaises(TypeError, chown_func, first_param, uid, t(gid))
291+
check_stat(uid, gid)
274292

275293
@unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()")
276294
def test_chown(self):

0 commit comments

Comments
 (0)