3030)
3131from test .support .os_helper import (
3232 TESTFN , unlink , rmtree , temp_dir , temp_cwd , fd_count , FakePath ,
33+ skip_unless_working_chmod , temp_umask ,
3334 with_source_date_epoch , without_source_date_epoch ,
3435)
3536from test .support .import_helper import ensure_lazy_imports
@@ -3969,7 +3970,7 @@ def _test_extract_hackers_arcnames(self, hacknames):
39693970
39703971
39713972@unittest .skipIf (sys .platform == 'win32' , 'requires Unix permissions' )
3972- @os_helper . skip_unless_working_chmod
3973+ @skip_unless_working_chmod
39733974class PreservePermissionsTests (unittest .TestCase ):
39743975 # File modes saved in the test archive.
39753976 FILE_MODES = {
@@ -3983,7 +3984,7 @@ class PreservePermissionsTests(unittest.TestCase):
39833984
39843985 def setUp (self ):
39853986 # Fixed umask, so we know the default mode (0o644).
3986- self .enterContext (os_helper . temp_umask (0o022 ))
3987+ self .enterContext (temp_umask (0o022 ))
39873988 self .addCleanup (unlink , TESTFN2 )
39883989
39893990 def make_zip (self , members , dirs = ()):
@@ -4032,10 +4033,25 @@ def test_preserve_safe(self):
40324033 preserve_permissions = zipfile .PreserveMode .SAFE )
40334034
40344035 def test_preserve_all (self ):
4035- # No setgid or sticky here: normal users can't always set them.
4036- file_modes = {'private' : 0o600 , 'script' : 0o755 , 'setuid' : 0o4755 }
4036+ # Regular users can't always set the set-user-ID, set-group-ID and
4037+ # sticky bits: chmod() may fail or silently drop them. Probe what
4038+ # works here and expect that (see also test_tarfile).
4039+ file_modes = {'private' : 0o600 , 'script' : 0o755 }
4040+ expected = dict (file_modes )
4041+ with temp_dir () as probe_dir :
4042+ probe = os .path .join (probe_dir , 'probe' )
4043+ with open (probe , 'wb' ):
4044+ pass
4045+ for name , mode in (('setuid' , 0o4755 ), ('setgid' , 0o2755 ),
4046+ ('sticky' , 0o1644 )):
4047+ try :
4048+ os .chmod (probe , mode )
4049+ except OSError :
4050+ continue
4051+ file_modes [name ] = mode
4052+ expected [name ] = stat .S_IMODE (os .stat (probe ).st_mode )
40374053 self .make_unix_zip (file_modes )
4038- self .check_extract (file_modes ,
4054+ self .check_extract (expected ,
40394055 preserve_permissions = zipfile .PreserveMode .ALL )
40404056
40414057 def test_mode_not_used (self ):
@@ -4057,6 +4073,23 @@ def test_read_only_directory(self):
40574073 methods = ['extractall' ],
40584074 preserve_permissions = zipfile .PreserveMode .SAFE )
40594075
4076+ def test_extraction_root_unchanged (self ):
4077+ # A "./" member names the directory extracted into. Its stored
4078+ # permissions must not be applied to that directory.
4079+ self .make_unix_zip ({'file' : 0o644 }, dirs = [('./' , 0o777 )])
4080+ ALL = zipfile .PreserveMode .ALL
4081+ for method in ('extract' , 'extractall' ):
4082+ with self .subTest (method = method ), temp_dir () as extdir :
4083+ mode = os .stat (extdir ).st_mode
4084+ with zipfile .ZipFile (TESTFN2 ) as zf :
4085+ if method == 'extract' :
4086+ for name in zf .namelist ():
4087+ zf .extract (name , extdir , preserve_permissions = ALL )
4088+ else :
4089+ zf .extractall (extdir , preserve_permissions = ALL )
4090+ self .assertEqual (os .stat (extdir ).st_mode , mode )
4091+ self .assertTrue (os .path .isfile (os .path .join (extdir , 'file' )))
4092+
40604093 def test_bad_value (self ):
40614094 self .make_unix_zip ({'plain' : 0o644 })
40624095 with zipfile .ZipFile (TESTFN2 ) as zf , temp_dir () as extdir :
0 commit comments