Skip to content

Commit b26a475

Browse files
committed
pythongh-107902: Don't test setting suid/sgid on systems that don't support them
1 parent 29bc616 commit b26a475

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

Lib/test/test_tarfile.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3809,33 +3809,38 @@ def test_modes(self):
38093809
arc.add('no_bits', mode='?---------')
38103810
arc.add('dir/', mode='?---rwsrwt')
38113811

3812-
# On some systems, setting the sticky bit is a no-op.
3813-
# Check if that's the case.
3812+
# On some systems, setting the uid, gid, and/or sticky bit is a no-ops.
3813+
# Check which bits we can set, so we can compare tarfile machinery to
3814+
# a simple chmod.
38143815
tmp_filename = os.path.join(TEMPDIR, "tmp.file")
38153816
with open(tmp_filename, 'w'):
38163817
pass
3817-
os.chmod(tmp_filename, os.stat(tmp_filename).st_mode | stat.S_ISVTX)
3818-
have_sticky_files = (os.stat(tmp_filename).st_mode & stat.S_ISVTX)
3818+
new_mode = (os.stat(tmp_filename).st_mode
3819+
| stat.S_ISVTX | stat.S_ISGID | stat.S_ISUID)
3820+
os.chmod(tmp_filename, new_mode)
3821+
got_mode = os.stat(tmp_filename).st_mode
3822+
_t_file = 't' if (got_mode & stat.S_ISVTX) else 'x'
3823+
_suid_file = 's' if (got_mode & stat.S_ISUID) else 'x'
3824+
_sgid_file = 's' if (got_mode & stat.S_ISGID) else 'x'
38193825
os.unlink(tmp_filename)
38203826

38213827
os.mkdir(tmp_filename)
3822-
os.chmod(tmp_filename, os.stat(tmp_filename).st_mode | stat.S_ISVTX)
3823-
have_sticky_dirs = (os.stat(tmp_filename).st_mode & stat.S_ISVTX)
3828+
new_mode = os.stat(tmp_filename).st_mode | stat.S_ISVTX | stat.S_ISGID
3829+
os.chmod(tmp_filename, new_mode)
3830+
got_mode = os.stat(tmp_filename).st_mode
3831+
_t_dir = 't' if (got_mode & stat.S_ISVTX) else 'x'
3832+
_suid_dir = 's' if (got_mode & stat.S_ISUID) else 'x'
3833+
_sgid_dir = 's' if (got_mode & stat.S_ISGID) else 'x'
38243834
os.rmdir(tmp_filename)
38253835

38263836
with self.check_context(arc.open(), 'fully_trusted'):
3827-
if have_sticky_files:
3828-
self.expect_file('all_bits', mode='?rwsrwsrwt')
3829-
else:
3830-
self.expect_file('all_bits', mode='?rwsrwsrwx')
3837+
self.expect_file('all_bits',
3838+
mode=f'?rw{_suid_file}rw{_sgid_file}rw{_t_file}')
38313839
self.expect_file('perm_bits', mode='?rwxrwxrwx')
38323840
self.expect_file('exec_group_other', mode='?rw-rwxrwx')
38333841
self.expect_file('read_group_only', mode='?---r-----')
38343842
self.expect_file('no_bits', mode='?---------')
3835-
if have_sticky_dirs:
3836-
self.expect_file('dir/', mode='?---rwsrwt')
3837-
else:
3838-
self.expect_file('dir/', mode='?---rwsrwx')
3843+
self.expect_file('dir/', mode=f'?---rw{_sgid_dir}rw{_t_dir}')
38393844

38403845
with self.check_context(arc.open(), 'tar'):
38413846
self.expect_file('all_bits', mode='?rwxr-xr-x')

0 commit comments

Comments
 (0)