summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_zipfile
diff options
context:
space:
mode:
authorTim Hatch <tim@timhatch.com>2023-02-20 17:07:03 (GMT)
committerGitHub <noreply@github.com>2023-02-20 17:07:03 (GMT)
commit59e86caca812fc993c5eb7dc8ccd1508ffccba86 (patch)
treef667e940591ba09c44ff51635453c71e0ded890b /Lib/test/test_zipfile
parented01addb59a554804995303ad3e7bf0c6067737b (diff)
downloadcpython-59e86caca812fc993c5eb7dc8ccd1508ffccba86.zip
cpython-59e86caca812fc993c5eb7dc8ccd1508ffccba86.tar.gz
cpython-59e86caca812fc993c5eb7dc8ccd1508ffccba86.tar.bz2
gh-88233: zipfile: handle extras after a zip64 extra (GH-96161)
Previously, any data _after_ the zip64 extra would be removed. With many new tests. Fixes #88233 Automerge-Triggered-By: GH:jaraco
Diffstat (limited to 'Lib/test/test_zipfile')
-rw-r--r--Lib/test/test_zipfile/test_core.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/Lib/test/test_zipfile/test_core.py b/Lib/test/test_zipfile/test_core.py
index cf41d0e8..e23f5c2 100644
--- a/Lib/test/test_zipfile/test_core.py
+++ b/Lib/test/test_zipfile/test_core.py
@@ -3010,5 +3010,67 @@ class EncodedMetadataTests(unittest.TestCase):
self.assertIn(name, listing)
+class StripExtraTests(unittest.TestCase):
+ # Note: all of the "z" characters are technically invalid, but up
+ # to 3 bytes at the end of the extra will be passed through as they
+ # are too short to encode a valid extra.
+
+ ZIP64_EXTRA = 1
+
+ def test_no_data(self):
+ s = struct.Struct("<HH")
+ a = s.pack(self.ZIP64_EXTRA, 0)
+ b = s.pack(2, 0)
+ c = s.pack(3, 0)
+
+ self.assertEqual(b'', zipfile._strip_extra(a, (self.ZIP64_EXTRA,)))
+ self.assertEqual(b, zipfile._strip_extra(b, (self.ZIP64_EXTRA,)))
+ self.assertEqual(
+ b+b"z", zipfile._strip_extra(b+b"z", (self.ZIP64_EXTRA,)))
+
+ self.assertEqual(b+c, zipfile._strip_extra(a+b+c, (self.ZIP64_EXTRA,)))
+ self.assertEqual(b+c, zipfile._strip_extra(b+a+c, (self.ZIP64_EXTRA,)))
+ self.assertEqual(b+c, zipfile._strip_extra(b+c+a, (self.ZIP64_EXTRA,)))
+
+ def test_with_data(self):
+ s = struct.Struct("<HH")
+ a = s.pack(self.ZIP64_EXTRA, 1) + b"a"
+ b = s.pack(2, 2) + b"bb"
+ c = s.pack(3, 3) + b"ccc"
+
+ self.assertEqual(b"", zipfile._strip_extra(a, (self.ZIP64_EXTRA,)))
+ self.assertEqual(b, zipfile._strip_extra(b, (self.ZIP64_EXTRA,)))
+ self.assertEqual(
+ b+b"z", zipfile._strip_extra(b+b"z", (self.ZIP64_EXTRA,)))
+
+ self.assertEqual(b+c, zipfile._strip_extra(a+b+c, (self.ZIP64_EXTRA,)))
+ self.assertEqual(b+c, zipfile._strip_extra(b+a+c, (self.ZIP64_EXTRA,)))
+ self.assertEqual(b+c, zipfile._strip_extra(b+c+a, (self.ZIP64_EXTRA,)))
+
+ def test_multiples(self):
+ s = struct.Struct("<HH")
+ a = s.pack(self.ZIP64_EXTRA, 1) + b"a"
+ b = s.pack(2, 2) + b"bb"
+
+ self.assertEqual(b"", zipfile._strip_extra(a+a, (self.ZIP64_EXTRA,)))
+ self.assertEqual(b"", zipfile._strip_extra(a+a+a, (self.ZIP64_EXTRA,)))
+ self.assertEqual(
+ b"z", zipfile._strip_extra(a+a+b"z", (self.ZIP64_EXTRA,)))
+ self.assertEqual(
+ b+b"z", zipfile._strip_extra(a+a+b+b"z", (self.ZIP64_EXTRA,)))
+
+ self.assertEqual(b, zipfile._strip_extra(a+a+b, (self.ZIP64_EXTRA,)))
+ self.assertEqual(b, zipfile._strip_extra(a+b+a, (self.ZIP64_EXTRA,)))
+ self.assertEqual(b, zipfile._strip_extra(b+a+a, (self.ZIP64_EXTRA,)))
+
+ def test_too_short(self):
+ self.assertEqual(b"", zipfile._strip_extra(b"", (self.ZIP64_EXTRA,)))
+ self.assertEqual(b"z", zipfile._strip_extra(b"z", (self.ZIP64_EXTRA,)))
+ self.assertEqual(
+ b"zz", zipfile._strip_extra(b"zz", (self.ZIP64_EXTRA,)))
+ self.assertEqual(
+ b"zzz", zipfile._strip_extra(b"zzz", (self.ZIP64_EXTRA,)))
+
+
if __name__ == "__main__":
unittest.main()