Skip to content

Commit 0d0aade

Browse files
committed
Add more tests
1 parent be3dfe5 commit 0d0aade

2 files changed

Lines changed: 144 additions & 0 deletions

File tree

‎Lib/test/test_shutil.py‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,69 @@ def onexc(*args):
566566
self.assertTrue(onexc_called)
567567
self.assertFalse(onerror_called)
568568

569+
@unittest.skipUnless(shutil.rmtree.avoids_symlink_attacks,
570+
'requires the fd based implementation of rmtree()')
571+
def test_onexc_kwargs_dir_fd(self):
572+
os.mkdir(TESTFN)
573+
self.addCleanup(os_helper.rmtree, TESTFN)
574+
top_name = os.path.join(TESTFN, 'top')
575+
child_name = os.path.join(TESTFN, 'top', 'child')
576+
os.mkdir(top_name)
577+
os.mkdir(child_name)
578+
parent_fd = os.open(TESTFN, os.O_RDONLY)
579+
self.addCleanup(os.close, parent_fd)
580+
581+
errors = []
582+
def onexc(func, path, err, direntry=None, dir_fd=None):
583+
errors.append((func, path.replace(os.path.sep, '/')))
584+
name = path if direntry is None else direntry.name
585+
self.assertEqual(name, os.path.basename(path))
586+
self.assertTrue(os.path.samestat(
587+
os.stat(os.path.join(TESTFN, path)),
588+
os.stat(name, dir_fd=dir_fd)))
589+
590+
def rmtree():
591+
errors.clear()
592+
shutil.rmtree('top', dir_fd=parent_fd, onexc=onexc,
593+
_onexc_kwargs=True)
594+
return errors
595+
596+
def failing_rmdir(path, *, dir_fd=None):
597+
raise PermissionError(errno.EACCES, 'simulated')
598+
with unittest.mock.patch('os.rmdir', failing_rmdir):
599+
shutil.rmtree('top', dir_fd=parent_fd, onexc=onexc,
600+
_onexc_kwargs=True)
601+
self.assertEqual(
602+
errors,
603+
[(failing_rmdir, 'top/child'), (failing_rmdir, 'top')])
604+
605+
child_stat = os.stat(child_name)
606+
scandir = os.scandir
607+
608+
# Fail for the top-level directory
609+
errors.clear()
610+
def failing_scandir(fd):
611+
raise PermissionError(errno.EACCES, 'simulated')
612+
with unittest.mock.patch('os.scandir', failing_scandir):
613+
shutil.rmtree('top', dir_fd=parent_fd, onexc=onexc,
614+
_onexc_kwargs=True)
615+
self.assertEqual(
616+
errors,
617+
[(failing_scandir, 'top'), (os.rmdir, 'top')])
618+
self.assertTrue(os.path.exists(top_name))
619+
620+
# Fail for the child (which is empty, so the rmdir succeeds)
621+
errors.clear()
622+
def failing_scandir_2(fd):
623+
if not os.path.samestat(os.fstat(fd), child_stat):
624+
return scandir(fd)
625+
raise PermissionError(errno.EACCES, 'simulated')
626+
with unittest.mock.patch('os.scandir', failing_scandir_2):
627+
shutil.rmtree('top', dir_fd=parent_fd, onexc=onexc,
628+
_onexc_kwargs=True)
629+
self.assertEqual(errors, [(failing_scandir_2, 'top/child')])
630+
self.assertFalse(os.path.exists(top_name))
631+
569632
def test_rmtree_does_not_choke_on_failing_lstat(self):
570633
try:
571634
orig_lstat = os.lstat

‎Lib/test/test_tempfile.py‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,6 +1910,87 @@ def hook(path, *, dir_fd=None):
19101910
self.assertTrue(os.path.exists(target_file))
19111911
self.assertEqual(os.stat(target_file).st_mode, target_mode)
19121912

1913+
def _make_unopenable_subdir(self):
1914+
# Create the fullowing structure:
1915+
# work/ - top-level
1916+
# <d>/ - tempdir to clean up (random name)
1917+
# sub/ - an unopenable dir
1918+
# subsub/
1919+
# file
1920+
# Return:
1921+
# - `work` name (str)
1922+
# - `d` (TemporaryDirectory)
1923+
# - `subsub` stat result
1924+
work = self.enterContext(os_helper.temp_dir())
1925+
d = tempfile.TemporaryDirectory(dir=work)
1926+
sub = os.path.join(d.name, 'sub')
1927+
subsub = os.path.join(sub, 'subsub')
1928+
os.mkdir(sub)
1929+
os.mkdir(subsub)
1930+
subsub_stat = os.lstat(subsub)
1931+
with open(os.path.join(subsub, 'file'), 'w') as f:
1932+
f.write("rm me pls")
1933+
os.chmod(sub, 0)
1934+
# If the test fails, make sure everything can still be removed.
1935+
self.addCleanup(d.cleanup)
1936+
@self.addCleanup
1937+
def _clean():
1938+
if os.path.isdir(sub):
1939+
os.chmod(sub, 0o700)
1940+
return work, d, subsub_stat
1941+
1942+
@os_helper.skip_unless_working_chmod
1943+
@support.requires_non_root_user
1944+
@unittest.skipUnless(tempfile._rmtree_use_dir_fd
1945+
and shutil.rmtree.avoids_symlink_attacks,
1946+
'requires the fd based cleanup')
1947+
def test_cleanup_nested_toplevel_error(self):
1948+
# rmtree() reports errors relative to the correct parent directory
1949+
work, d, subsub_stat = self._make_unopenable_subdir()
1950+
# An unrelated directory with the same name; should not be touched
1951+
unrelated = os.path.join(work, 'sub')
1952+
os.mkdir(unrelated)
1953+
1954+
rmdir = os.rmdir
1955+
failed_path = None
1956+
def failing_rmdir(path, *, dir_fd=None):
1957+
nonlocal failed_path
1958+
if path == 'sub' and dir_fd is not None and failed_path is None:
1959+
failed_path = path
1960+
raise PermissionError(errno.EPERM, 'simulated', path)
1961+
return rmdir(path, dir_fd=dir_fd)
1962+
with os_helper.change_cwd(work), mock.patch('os.rmdir', failing_rmdir):
1963+
d.cleanup()
1964+
self.assertEqual(failed_path, 'sub')
1965+
self.assertFalse(os.path.exists(d.name))
1966+
self.assertTrue(os.path.exists(unrelated))
1967+
1968+
@os_helper.skip_unless_working_chmod
1969+
@support.requires_non_root_user
1970+
@unittest.skipUnless(tempfile._rmtree_use_dir_fd
1971+
and shutil.rmtree.avoids_symlink_attacks,
1972+
'requires the fd based cleanup')
1973+
def test_cleanup_nested_scandir_error(self):
1974+
# A scandir() error is reported relative to the parent directory.
1975+
work, d, subsub_stat = self._make_unopenable_subdir()
1976+
1977+
scandir = os.scandir
1978+
failed = False
1979+
def failing_scandir(path='.'):
1980+
nonlocal failed
1981+
if (
1982+
not failed
1983+
and isinstance(path, int)
1984+
and os.path.samestat(os.fstat(path), subsub_stat)
1985+
):
1986+
failed = True
1987+
raise PermissionError(errno.EACCES, 'simulated')
1988+
return scandir(path)
1989+
with os_helper.change_cwd(work), mock.patch('os.scandir', failing_scandir):
1990+
d.cleanup()
1991+
self.assertTrue(failed)
1992+
self.assertFalse(os.path.exists(d.name))
1993+
19131994
@support.cpython_only
19141995
def test_del_on_collection(self):
19151996
# A TemporaryDirectory is deleted when garbage collected

0 commit comments

Comments
 (0)