@@ -4516,103 +4516,71 @@ def test_chmod_outside_dir(self):
45164516 self .assertNotEqual (st_mode & 0o777 , 0o777 )
45174517
45184518 @symlink_test
4519- def test_chown_links_on_extract (self ):
4520- for link_type in (tarfile .SYMTYPE , tarfile .LNKTYPE ):
4521- buf = io .BytesIO ()
4522- with tarfile .open (fileobj = buf , mode = 'w' ) as tf :
4523- # Create a regular file entry with uid/gid
4524- info = tarfile .TarInfo (name = "test.txt" )
4525- info .uid = 1337
4526- info .gid = 1337
4527- info .uname = ""
4528- info .gname = ""
4529- info .mode = 0o755
4530- tf .addfile (info , io .BytesIO (b"" ))
4531-
4532- # Create a link to the normal file.
4533- link_info = tarfile .TarInfo (name = "link" )
4534- link_info .type = link_type
4535- link_info .linkname = "test.txt"
4536- link_info .uid = 1337
4537- link_info .gid = 1337
4538- link_info .uname = ""
4539- link_info .gname = ""
4540- link_info .mode = 0o644
4541- tf .addfile (link_info )
4542-
4543- buf .seek (0 )
4544- with (
4545- self .subTest (f"type={ link_type !r} " ),
4546- os_helper .temp_dir () as tmpdir ,
4547- tarfile .open (fileobj = buf ) as tar ,
4548- unittest .mock .patch ("os.chown" ) as mock_chown ,
4549- unittest .mock .patch ("os.lchown" ) as mock_lchown ,
4550- unittest .mock .patch ("os.geteuid" ) as mock_geteuid ,
4551- ):
4552- # Set UID to 0 so chown() is attempted.
4553- mock_geteuid .return_value = 0
4554- tar .extract ("link" , path = tmpdir , filter = 'data' )
4555- extract_path = os .path .join (tmpdir , "link" )
4556-
4557- if link_type == tarfile .SYMTYPE :
4558- mock_chown .assert_not_called ()
4559- mock_lchown .assert_called_once_with (extract_path , - 1 , - 1 )
4560- else :
4561- mock_chown .assert_has_calls ([
4562- unittest .mock .call (extract_path , - 1 , - 1 ),
4563- unittest .mock .call (extract_path , - 1 , - 1 )
4564- ])
4565- mock_lchown .assert_not_called ()
4519+ @support .subTests ('link_type' , (tarfile .SYMTYPE , tarfile .LNKTYPE ))
4520+ def test_chown_links_on_extract (self , link_type ):
4521+ with ArchiveMaker () as arc :
4522+ arc .add ("test.txt" ,
4523+ uid = 1337 , gid = 1337 , uname = "" , gname = "" , mode = '-rwxr-xr-x' )
4524+ arc .add ("link" ,
4525+ type = link_type ,
4526+ linkname = 'test.txt' ,
4527+ uid = 1337 , gid = 1337 , uname = "" , gname = "" , mode = '-rwxr-xr-x' )
4528+
4529+ with (
4530+ os_helper .temp_dir () as tmpdir ,
4531+ arc .open () as tar ,
4532+ unittest .mock .patch ("os.chown" ) as mock_chown ,
4533+ unittest .mock .patch ("os.lchown" ) as mock_lchown ,
4534+ unittest .mock .patch ("os.geteuid" ) as mock_geteuid ,
4535+ ):
4536+ # Set UID to 0 so chown() is attempted.
4537+ mock_geteuid .return_value = 0
4538+ tar .extract ("link" , path = tmpdir , filter = 'data' )
4539+ extract_path = os .path .join (tmpdir , "link" )
4540+
4541+ if link_type == tarfile .SYMTYPE :
4542+ mock_chown .assert_not_called ()
4543+ mock_lchown .assert_called_once_with (extract_path , - 1 , - 1 )
4544+ else :
4545+ mock_chown .assert_has_calls ([
4546+ unittest .mock .call (extract_path , - 1 , - 1 ),
4547+ unittest .mock .call (extract_path , - 1 , - 1 )
4548+ ])
4549+ mock_lchown .assert_not_called ()
45664550
45674551 @symlink_test
4568- def test_chown_links_on_extractall (self ):
4569- for link_type in (tarfile .SYMTYPE , tarfile .LNKTYPE ):
4570- buf = io .BytesIO ()
4571- with tarfile .open (fileobj = buf , mode = 'w' ) as tf :
4572- # Create a regular file entry with uid/gid
4573- info = tarfile .TarInfo (name = "test.txt" )
4574- info .uid = 1337
4575- info .gid = 1337
4576- info .uname = ""
4577- info .gname = ""
4578- info .mode = 0o755
4579- tf .addfile (info , io .BytesIO (b"" ))
4580-
4581- # Create a link to the normal file.
4582- link_info = tarfile .TarInfo (name = "link" )
4583- link_info .type = link_type
4584- link_info .linkname = "test.txt"
4585- link_info .uid = 1337
4586- link_info .gid = 1337
4587- link_info .uname = ""
4588- link_info .gname = ""
4589- link_info .mode = 0o644
4590- tf .addfile (link_info )
4591-
4592- buf .seek (0 )
4593- with (
4594- self .subTest (f"type={ link_type !r} " ),
4595- os_helper .temp_dir () as tmpdir ,
4596- tarfile .open (fileobj = buf ) as tar ,
4597- unittest .mock .patch ("os.chown" ) as mock_chown ,
4598- unittest .mock .patch ("os.lchown" ) as mock_lchown ,
4599- unittest .mock .patch ("os.geteuid" ) as mock_geteuid ,
4600- ):
4601- # Set UID to 0 so chown() is attempted.
4602- mock_geteuid .return_value = 0
4603- tar .extractall (path = tmpdir , filter = 'data' )
4604- extract_link_path = os .path .join (tmpdir , "link" )
4605- extract_file_path = os .path .join (tmpdir , "test.txt" )
4606-
4607- if link_type == tarfile .SYMTYPE :
4608- mock_chown .assert_called_once_with (extract_file_path , - 1 , - 1 )
4609- mock_lchown .assert_called_once_with (extract_link_path , - 1 , - 1 )
4610- else :
4611- mock_chown .assert_has_calls ([
4612- unittest .mock .call (extract_file_path , - 1 , - 1 ),
4613- unittest .mock .call (extract_link_path , - 1 , - 1 )
4614- ])
4615- mock_lchown .assert_not_called ()
4552+ @support .subTests ('link_type' , (tarfile .SYMTYPE , tarfile .LNKTYPE ))
4553+ def test_chown_links_on_extractall (self , link_type ):
4554+ with ArchiveMaker () as arc :
4555+ arc .add ("test.txt" ,
4556+ uid = 1337 , gid = 1337 , uname = "" , gname = "" , mode = '-rwxr-xr-x' )
4557+ arc .add ("link" ,
4558+ type = link_type ,
4559+ linkname = 'test.txt' ,
4560+ uid = 1337 , gid = 1337 , uname = "" , gname = "" , mode = '-rwxr-xr-x' )
4561+
4562+ with (
4563+ os_helper .temp_dir () as tmpdir ,
4564+ arc .open () as tar ,
4565+ unittest .mock .patch ("os.chown" ) as mock_chown ,
4566+ unittest .mock .patch ("os.lchown" ) as mock_lchown ,
4567+ unittest .mock .patch ("os.geteuid" ) as mock_geteuid ,
4568+ ):
4569+ # Set UID to 0 so chown() is attempted.
4570+ mock_geteuid .return_value = 0
4571+ tar .extractall (path = tmpdir , filter = 'data' )
4572+ extract_link_path = os .path .join (tmpdir , "link" )
4573+ extract_file_path = os .path .join (tmpdir , "test.txt" )
4574+
4575+ if link_type == tarfile .SYMTYPE :
4576+ mock_chown .assert_called_once_with (extract_file_path , - 1 , - 1 )
4577+ mock_lchown .assert_called_once_with (extract_link_path , - 1 , - 1 )
4578+ else :
4579+ mock_chown .assert_has_calls ([
4580+ unittest .mock .call (extract_file_path , - 1 , - 1 ),
4581+ unittest .mock .call (extract_link_path , - 1 , - 1 )
4582+ ])
4583+ mock_lchown .assert_not_called ()
46164584
46174585 def test_extract_filters_target (self ):
46184586 # Test that when extract() falls back to extracting (rather than
0 commit comments