Skip to content

Commit 7163c33

Browse files
Generate data on the fly. Test with mode"r:gz" too.
1 parent 3005be6 commit 7163c33

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

-4.08 KB
Binary file not shown.

Lib/test/test_tarfile.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def sha256sum(data):
5050
TEMPDIR = os.path.abspath(os_helper.TESTFN) + "-tardir"
5151
tarextdir = TEMPDIR + '-extract-test'
5252
tarname = support.findfile("testtar.tar", subdir="archivetestdata")
53-
tgzname_with_comment_extra_data_in_header = support.findfile("tgz_with_comment_extra_data_in_header.tgz", subdir="archivetestdata")
5453
gzipname = os.path.join(TEMPDIR, "testtar.tar.gz")
5554
bz2name = os.path.join(TEMPDIR, "testtar.tar.bz2")
5655
xzname = os.path.join(TEMPDIR, "testtar.tar.xz")
@@ -894,10 +893,39 @@ def test_extractall_hardlink_on_symlink(self):
894893
self._assert_on_file_content(hardlink_filepath, sha256_regtype)
895894

896895

896+
class GzipReadTestBase:
897+
898+
def test_read_with_extra_field(self):
899+
with open(self.tarname, 'rb') as f:
900+
data = bytearray(f.read())
901+
flags = data[3]
902+
self.assertEqual(flags, 8)
903+
data[3] = flags | 4
904+
data[10:10] = b'\x05\x00extra'
905+
with open(tmpname, 'wb') as f:
906+
f.write(data)
907+
print(self.mode)
908+
with tarfile.open(tmpname, mode=self.mode):
909+
pass
910+
911+
def test_read_with_file_comment(self):
912+
with open(self.tarname, 'rb') as f:
913+
data = bytearray(f.read())
914+
flags = data[3]
915+
self.assertEqual(flags, 8)
916+
data[3] = flags | 16
917+
i = data.index(0, 10) + 1
918+
data[i:i] = b'comment\x00'
919+
with open(tmpname, 'wb') as f:
920+
f.write(data)
921+
with tarfile.open(tmpname, mode=self.mode):
922+
pass
923+
924+
897925
class MiscReadTest(MiscReadTestBase, unittest.TestCase):
898926
test_fail_comp = None
899927

900-
class GzipMiscReadTest(GzipTest, MiscReadTestBase, unittest.TestCase):
928+
class GzipMiscReadTest(GzipTest, GzipReadTestBase, MiscReadTestBase, unittest.TestCase):
901929
pass
902930

903931
class Bz2MiscReadTest(Bz2Test, MiscReadTestBase, unittest.TestCase):
@@ -971,13 +999,8 @@ def test_compare_members(self):
971999
finally:
9721000
tar1.close()
9731001

974-
class GzipStreamReadTest(GzipTest, StreamReadTest):
975-
976-
@unittest.skipIf(zlib is None, "requires zlib")
977-
def test_read_with_extra_header(self):
978-
with tarfile.open(tgzname_with_comment_extra_data_in_header,
979-
mode="r|*") as _:
980-
pass
1002+
class GzipStreamReadTest(GzipTest, GzipReadTestBase, StreamReadTest):
1003+
pass
9811004

9821005
class Bz2StreamReadTest(Bz2Test, StreamReadTest):
9831006
pass

0 commit comments

Comments
 (0)