@@ -2777,7 +2777,7 @@ def test_useful_error_message_when_modules_missing(self):
27772777 str (excinfo .exception ),
27782778 )
27792779
2780- @unittest . skipUnless ( os_helper .can_symlink (), 'requires symlink support' )
2780+ @os_helper .skip_unless_symlink
27812781 @unittest .skipUnless (hasattr (os , 'chmod' ), "missing os.chmod" )
27822782 @unittest .mock .patch ('os.chmod' )
27832783 def test_deferred_directory_attributes_update (self , mock_chmod ):
@@ -3663,6 +3663,37 @@ class TestExtractionFilters(unittest.TestCase):
36633663 # The destination for the extraction, within `outerdir`
36643664 destdir = outerdir / 'dest'
36653665
3666+ @classmethod
3667+ def setUpClass (cls ):
3668+ # Posix and Windows have different pathname resolution:
3669+ # either symlink or a '..' component resolve first.
3670+ # Let's see which we are on.
3671+ if os_helper .can_symlink ():
3672+ testpath = os .path .join (TEMPDIR , 'resolution_test' )
3673+ os .mkdir (testpath )
3674+
3675+ # testpath/current links to `.` which is all of:
3676+ # - `testpath`
3677+ # - `testpath/current`
3678+ # - `testpath/current/current`
3679+ # - etc.
3680+ os .symlink ('.' , os .path .join (testpath , 'current' ))
3681+
3682+ # we'll test where `testpath/current/../file` ends up
3683+ with open (os .path .join (testpath , 'current' , '..' , 'file' ), 'w' ):
3684+ pass
3685+
3686+ if os .path .exists (os .path .join (testpath , 'file' )):
3687+ # Windows collapses 'current\..' to '.' first, leaving
3688+ # 'testpath\file'
3689+ cls .dotdot_resolves_early = True
3690+ elif os .path .exists (os .path .join (testpath , '..' , 'file' )):
3691+ # Posix resolves 'current' to '.' first, leaving
3692+ # 'testpath/../file'
3693+ cls .dotdot_resolves_early = False
3694+ else :
3695+ raise AssertionError ('Could not determine link resolution' )
3696+
36663697 @contextmanager
36673698 def check_context (self , tar , filter , * , check_flag = True ):
36683699 """Extracts `tar` to `self.destdir` and allows checking the result
@@ -3809,23 +3840,21 @@ def test_parent_symlink(self):
38093840 arc .add ('current' , symlink_to = '.' )
38103841
38113842 # effectively points to ./../
3812- arc .add ('parent' , symlink_to = 'current/..' )
3843+ if self .dotdot_resolves_early :
3844+ arc .add ('parent' , symlink_to = 'current/../..' )
3845+ else :
3846+ arc .add ('parent' , symlink_to = 'current/..' )
38133847
38143848 arc .add ('parent/evil' )
38153849
38163850 if os_helper .can_symlink ():
38173851 with self .check_context (arc .open (), 'fully_trusted' ):
3818- if self .raised_exception is not None :
3819- # Windows will refuse to create a file that's a symlink to itself
3820- # (and tarfile doesn't swallow that exception)
3821- self .expect_exception (FileExistsError )
3822- # The other cases will fail with this error too.
3823- # Skip the rest of this test.
3824- return
3852+ self .expect_file ('current' , symlink_to = '.' )
3853+ if self .dotdot_resolves_early :
3854+ self .expect_file ('parent' , symlink_to = 'current/../..' )
38253855 else :
3826- self .expect_file ('current' , symlink_to = '.' )
38273856 self .expect_file ('parent' , symlink_to = 'current/..' )
3828- self .expect_file ('../evil' )
3857+ self .expect_file ('../evil' )
38293858
38303859 with self .check_context (arc .open (), 'tar' ):
38313860 self .expect_exception (
@@ -3927,35 +3956,6 @@ def test_parent_symlink2(self):
39273956 # Test interplaying symlinks
39283957 # Inspired by 'dirsymlink2b' in jwilk/traversal-archives
39293958
3930- # Posix and Windows have different pathname resolution:
3931- # either symlink or a '..' component resolve first.
3932- # Let's see which we are on.
3933- if os_helper .can_symlink ():
3934- testpath = os .path .join (TEMPDIR , 'resolution_test' )
3935- os .mkdir (testpath )
3936-
3937- # testpath/current links to `.` which is all of:
3938- # - `testpath`
3939- # - `testpath/current`
3940- # - `testpath/current/current`
3941- # - etc.
3942- os .symlink ('.' , os .path .join (testpath , 'current' ))
3943-
3944- # we'll test where `testpath/current/../file` ends up
3945- with open (os .path .join (testpath , 'current' , '..' , 'file' ), 'w' ):
3946- pass
3947-
3948- if os .path .exists (os .path .join (testpath , 'file' )):
3949- # Windows collapses 'current\..' to '.' first, leaving
3950- # 'testpath\file'
3951- dotdot_resolves_early = True
3952- elif os .path .exists (os .path .join (testpath , '..' , 'file' )):
3953- # Posix resolves 'current' to '.' first, leaving
3954- # 'testpath/../file'
3955- dotdot_resolves_early = False
3956- else :
3957- raise AssertionError ('Could not determine link resolution' )
3958-
39593959 with ArchiveMaker () as arc :
39603960
39613961 # `current` links to `.` which is both the destination directory
@@ -3991,7 +3991,7 @@ def test_parent_symlink2(self):
39913991
39923992 with self .check_context (arc .open (), 'data' ):
39933993 if os_helper .can_symlink ():
3994- if dotdot_resolves_early :
3994+ if self . dotdot_resolves_early :
39953995 # Fail when extracting a file outside destination
39963996 self .expect_exception (
39973997 tarfile .OutsideDestinationError ,
@@ -4039,8 +4039,8 @@ def test_absolute_symlink(self):
40394039 tarfile .AbsoluteLinkError ,
40404040 "'parent' is a link to an absolute path" )
40414041
4042- @unittest .skipUnless (os_helper .can_symlink (), 'requires symlink support' )
40434042 @symlink_test
4043+ @os_helper .skip_unless_symlink
40444044 def test_symlink_target_sanitized_on_windows (self ):
40454045 with ArchiveMaker () as arc :
40464046 arc .add ('link' , symlink_to = "relative/test/path" )
0 commit comments