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

Skip to content

Commit 5a5b9b9

Browse files
committed
gh-107902: Don't test setting suid/sgid on systems that don't support them (GH-108368)
1 parent 8b9a8e0 commit 5a5b9b9

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
@@ -3651,34 +3651,43 @@ def test_modes(self):
36513651
arc.add('read_group_only', mode='?---r-----')
36523652
arc.add('no_bits', mode='?---------')
36533653
arc.add('dir/', mode='?---rwsrwt')
3654+
arc.add('dir_all_bits/', mode='?rwsrwsrwt')
36543655

3655-
# On some systems, setting the sticky bit is a no-op.
3656-
# Check if that's the case.
3656+
# On some systems, setting the uid, gid, and/or sticky bit is a no-ops.
3657+
# Check which bits we can set, so we can compare tarfile machinery to
3658+
# a simple chmod.
36573659
tmp_filename = os.path.join(TEMPDIR, "tmp.file")
36583660
with open(tmp_filename, 'w'):
36593661
pass
3660-
os.chmod(tmp_filename, os.stat(tmp_filename).st_mode | stat.S_ISVTX)
3661-
have_sticky_files = (os.stat(tmp_filename).st_mode & stat.S_ISVTX)
3662+
new_mode = (os.stat(tmp_filename).st_mode
3663+
| stat.S_ISVTX | stat.S_ISGID | stat.S_ISUID)
3664+
os.chmod(tmp_filename, new_mode)
3665+
got_mode = os.stat(tmp_filename).st_mode
3666+
_t_file = 't' if (got_mode & stat.S_ISVTX) else 'x'
3667+
_suid_file = 's' if (got_mode & stat.S_ISUID) else 'x'
3668+
_sgid_file = 's' if (got_mode & stat.S_ISGID) else 'x'
36623669
os.unlink(tmp_filename)
36633670

36643671
os.mkdir(tmp_filename)
3665-
os.chmod(tmp_filename, os.stat(tmp_filename).st_mode | stat.S_ISVTX)
3666-
have_sticky_dirs = (os.stat(tmp_filename).st_mode & stat.S_ISVTX)
3672+
new_mode = (os.stat(tmp_filename).st_mode
3673+
| stat.S_ISVTX | stat.S_ISGID | stat.S_ISUID)
3674+
os.chmod(tmp_filename, new_mode)
3675+
got_mode = os.stat(tmp_filename).st_mode
3676+
_t_dir = 't' if (got_mode & stat.S_ISVTX) else 'x'
3677+
_suid_dir = 's' if (got_mode & stat.S_ISUID) else 'x'
3678+
_sgid_dir = 's' if (got_mode & stat.S_ISGID) else 'x'
36673679
os.rmdir(tmp_filename)
36683680

36693681
with self.check_context(arc.open(), 'fully_trusted'):
3670-
if have_sticky_files:
3671-
self.expect_file('all_bits', mode='?rwsrwsrwt')
3672-
else:
3673-
self.expect_file('all_bits', mode='?rwsrwsrwx')
3682+
self.expect_file('all_bits',
3683+
mode=f'?rw{_suid_file}rw{_sgid_file}rw{_t_file}')
36743684
self.expect_file('perm_bits', mode='?rwxrwxrwx')
36753685
self.expect_file('exec_group_other', mode='?rw-rwxrwx')
36763686
self.expect_file('read_group_only', mode='?---r-----')
36773687
self.expect_file('no_bits', mode='?---------')
3678-
if have_sticky_dirs:
3679-
self.expect_file('dir/', mode='?---rwsrwt')
3680-
else:
3681-
self.expect_file('dir/', mode='?---rwsrwx')
3688+
self.expect_file('dir/', mode=f'?---rw{_sgid_dir}rw{_t_dir}')
3689+
self.expect_file('dir_all_bits/',
3690+
mode=f'?rw{_suid_dir}rw{_sgid_dir}rw{_t_dir}')
36823691

36833692
with self.check_context(arc.open(), 'tar'):
36843693
self.expect_file('all_bits', mode='?rwxr-xr-x')
@@ -3687,6 +3696,7 @@ def test_modes(self):
36873696
self.expect_file('read_group_only', mode='?---r-----')
36883697
self.expect_file('no_bits', mode='?---------')
36893698
self.expect_file('dir/', mode='?---r-xr-x')
3699+
self.expect_file('dir_all_bits/', mode='?rwxr-xr-x')
36903700

36913701
with self.check_context(arc.open(), 'data'):
36923702
normal_dir_mode = stat.filemode(stat.S_IMODE(
@@ -3697,6 +3707,7 @@ def test_modes(self):
36973707
self.expect_file('read_group_only', mode='?rw-r-----')
36983708
self.expect_file('no_bits', mode='?rw-------')
36993709
self.expect_file('dir/', mode=normal_dir_mode)
3710+
self.expect_file('dir_all_bits/', mode=normal_dir_mode)
37003711

37013712
def test_pipe(self):
37023713
# Test handling of a special file

0 commit comments

Comments
 (0)