Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move tarfile eftype test
test_extract_chmod_eftype was run once per testtar.* file but it did not use those files
  • Loading branch information
sorcio committed Sep 16, 2023
commit 83c08fca9d053e026e3533b47e0d5126ed7078cf
63 changes: 33 additions & 30 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,36 +691,6 @@ def format_mtime(mtime):
tar.close()
os_helper.rmtree(DIR)

@unittest.skipUnless(hasattr(errno, "EFTYPE"), "errno.EFTYPE required")
def test_extract_chmod_eftype(self):
# Extracting a file as non-root should skip the sticky bit (gh-108948)
# even on platforms where chmod fails with EFTYPE (i.e. FreeBSD). But
# we need to take care that any other error is preserved.
mode = "-rwxrwxrwt"
with ArchiveMaker() as arc:
arc.add("sticky1", mode=mode)
arc.add("sticky2", mode=mode)
tar = arc.open(errorlevel=2)
DIR = os.path.join(TEMPDIR, "chmod")
os.mkdir(DIR)
self.addCleanup(os_helper.rmtree, DIR)
with tar:
# this should not raise:
tar.extract("sticky1", DIR, filter="fully_trusted")
got_mode = stat.filemode(os.stat(os.path.join(DIR, "sticky1")).st_mode)
expected_mode = "-rwxrwxrwx" if os.geteuid() != 0 else "-rwxrwxrwt"
self.assertEqual(got_mode, expected_mode)

# but we can create a situation where it does raise:
with unittest.mock.patch("os.chmod") as mock:
eftype_error = OSError(errno.EFTYPE, "EFTYPE")
other_error = OSError(errno.EPERM, "different error")
mock.side_effect = [eftype_error, other_error]
with self.assertRaises(tarfile.ExtractError) as excinfo:
tar.extract("sticky2", DIR, filter="fully_trusted")
self.assertEqual(excinfo.exception.__cause__, other_error)
self.assertEqual(excinfo.exception.__cause__.__cause__, eftype_error)

@os_helper.skip_unless_working_chmod
def test_extract_directory(self):
dirtype = "ustar/dirtype"
Expand Down Expand Up @@ -3087,6 +3057,39 @@ def test_keyword_only(self, mock_geteuid):
tarfl.extract, filename_1, TEMPDIR, False, True)


@os_helper.skip_unless_working_chmod
class FileModesTest(unittest.TestCase):
@unittest.skipUnless(hasattr(errno, "EFTYPE"), "errno.EFTYPE required")
def test_extract_chmod_eftype(self):
# Extracting a file as non-root should skip the sticky bit (gh-108948)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Extracting a file as non-root should skip the sticky bit (gh-108948)
# gh-108948: Extracting a file as non-root should skip the sticky bit

# even on platforms where chmod fails with EFTYPE (i.e. FreeBSD). But
# we need to take care that any other error is preserved.
mode = "-rwxrwxrwt"
with ArchiveMaker() as arc:
arc.add("sticky1", mode=mode)
arc.add("sticky2", mode=mode)
tar = arc.open(errorlevel=2)
DIR = os.path.join(TEMPDIR, "chmod")
os.mkdir(DIR)
self.addCleanup(os_helper.rmtree, DIR)
with tar:
# this should not raise:
tar.extract("sticky1", DIR, filter="fully_trusted")
got_mode = stat.filemode(os.stat(os.path.join(DIR, "sticky1")).st_mode)
expected_mode = "-rwxrwxrwx" if os.geteuid() != 0 else "-rwxrwxrwt"
self.assertEqual(got_mode, expected_mode)

# but we can create a situation where it does raise:
with unittest.mock.patch("os.chmod") as mock:
eftype_error = OSError(errno.EFTYPE, "EFTYPE")
other_error = OSError(errno.EPERM, "different error")
mock.side_effect = [eftype_error, other_error]
with self.assertRaises(tarfile.ExtractError) as excinfo:
tar.extract("sticky2", DIR, filter="fully_trusted")
self.assertEqual(excinfo.exception.__cause__, other_error)
self.assertEqual(excinfo.exception.__cause__.__cause__, eftype_error)


class ReplaceTests(ReadTest, unittest.TestCase):
def test_replace_name(self):
member = self.tar.getmember('ustar/regtype')
Expand Down