Skip to content

Commit e3ecd5b

Browse files
committed
pythongh-107902: Don't test setting suid/sgid on systems that don't support them (pythonGH-108368)
1 parent 850189a commit e3ecd5b

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

Lib/test/test_tarfile.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3691,34 +3691,43 @@ def test_modes(self):
36913691
arc.add('read_group_only', mode='?---r-----')
36923692
arc.add('no_bits', mode='?---------')
36933693
arc.add('dir/', mode='?---rwsrwt')
3694+
arc.add('dir_all_bits/', mode='?rwsrwsrwt')
36943695

3695-
# On some systems, setting the sticky bit is a no-op.
3696-
# Check if that's the case.
3696+
# On some systems, setting the uid, gid, and/or sticky bit is a no-ops.
3697+
# Check which bits we can set, so we can compare tarfile machinery to
3698+
# a simple chmod.
36973699
tmp_filename = os.path.join(TEMPDIR, "tmp.file")
36983700
with open(tmp_filename, 'w'):
36993701
pass
3700-
os.chmod(tmp_filename, os.stat(tmp_filename).st_mode | stat.S_ISVTX)
3701-
have_sticky_files = (os.stat(tmp_filename).st_mode & stat.S_ISVTX)
3702+
new_mode = (os.stat(tmp_filename).st_mode
3703+
| stat.S_ISVTX | stat.S_ISGID | stat.S_ISUID)
3704+
os.chmod(tmp_filename, new_mode)
3705+
got_mode = os.stat(tmp_filename).st_mode
3706+
_t_file = 't' if (got_mode & stat.S_ISVTX) else 'x'
3707+
_suid_file = 's' if (got_mode & stat.S_ISUID) else 'x'
3708+
_sgid_file = 's' if (got_mode & stat.S_ISGID) else 'x'
37023709
os.unlink(tmp_filename)
37033710

37043711
os.mkdir(tmp_filename)
3705-
os.chmod(tmp_filename, os.stat(tmp_filename).st_mode | stat.S_ISVTX)
3706-
have_sticky_dirs = (os.stat(tmp_filename).st_mode & stat.S_ISVTX)
3712+
new_mode = (os.stat(tmp_filename).st_mode
3713+
| stat.S_ISVTX | stat.S_ISGID | stat.S_ISUID)
3714+
os.chmod(tmp_filename, new_mode)
3715+
got_mode = os.stat(tmp_filename).st_mode
3716+
_t_dir = 't' if (got_mode & stat.S_ISVTX) else 'x'
3717+
_suid_dir = 's' if (got_mode & stat.S_ISUID) else 'x'
3718+
_sgid_dir = 's' if (got_mode & stat.S_ISGID) else 'x'
37073719
os.rmdir(tmp_filename)
37083720

37093721
with self.check_context(arc.open(), 'fully_trusted'):
3710-
if have_sticky_files:
3711-
self.expect_file('all_bits', mode='?rwsrwsrwt')
3712-
else:
3713-
self.expect_file('all_bits', mode='?rwsrwsrwx')
3722+
self.expect_file('all_bits',
3723+
mode=f'?rw{_suid_file}rw{_sgid_file}rw{_t_file}')
37143724
self.expect_file('perm_bits', mode='?rwxrwxrwx')
37153725
self.expect_file('exec_group_other', mode='?rw-rwxrwx')
37163726
self.expect_file('read_group_only', mode='?---r-----')
37173727
self.expect_file('no_bits', mode='?---------')
3718-
if have_sticky_dirs:
3719-
self.expect_file('dir/', mode='?---rwsrwt')
3720-
else:
3721-
self.expect_file('dir/', mode='?---rwsrwx')
3728+
self.expect_file('dir/', mode=f'?---rw{_sgid_dir}rw{_t_dir}')
3729+
self.expect_file('dir_all_bits/',
3730+
mode=f'?rw{_suid_dir}rw{_sgid_dir}rw{_t_dir}')
37223731

37233732
with self.check_context(arc.open(), 'tar'):
37243733
self.expect_file('all_bits', mode='?rwxr-xr-x')
@@ -3727,6 +3736,7 @@ def test_modes(self):
37273736
self.expect_file('read_group_only', mode='?---r-----')
37283737
self.expect_file('no_bits', mode='?---------')
37293738
self.expect_file('dir/', mode='?---r-xr-x')
3739+
self.expect_file('dir_all_bits/', mode='?rwxr-xr-x')
37303740

37313741
with self.check_context(arc.open(), 'data'):
37323742
normal_dir_mode = stat.filemode(stat.S_IMODE(
@@ -3737,6 +3747,7 @@ def test_modes(self):
37373747
self.expect_file('read_group_only', mode='?rw-r-----')
37383748
self.expect_file('no_bits', mode='?rw-------')
37393749
self.expect_file('dir/', mode=normal_dir_mode)
3750+
self.expect_file('dir_all_bits/', mode=normal_dir_mode)
37403751

37413752
def test_pipe(self):
37423753
# Test handling of a special file

0 commit comments

Comments
 (0)