test_zip.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. from panda3d.core import ZipArchive, IStreamWrapper, StringStream, Filename
  2. from direct.stdpy.file import StreamIOWrapper
  3. import zipfile
  4. from io import BytesIO
  5. import os
  6. EMPTY_ZIP = b'PK\x05\x06' + b'\x00' * 18
  7. def test_zip_read_empty():
  8. stream = StringStream(EMPTY_ZIP)
  9. wrapper = IStreamWrapper(stream)
  10. zip = ZipArchive()
  11. zip.open_read(wrapper)
  12. assert zip.is_read_valid()
  13. assert not zip.is_write_valid()
  14. assert not zip.needs_repack()
  15. assert zip.get_num_subfiles() == 0
  16. zip.close()
  17. def test_zip_write_empty():
  18. stream = StringStream()
  19. zip = ZipArchive()
  20. zip.open_write(stream)
  21. assert not zip.is_read_valid()
  22. assert zip.is_write_valid()
  23. assert not zip.needs_repack()
  24. zip.close()
  25. assert stream.data == EMPTY_ZIP
  26. with zipfile.ZipFile(StreamIOWrapper(stream), 'r') as zf:
  27. assert zf.testzip() is None
  28. def test_zip_read_extract(tmp_path):
  29. stream = StringStream()
  30. zf = zipfile.ZipFile(StreamIOWrapper(stream), mode='w', allowZip64=True)
  31. zf.writestr("test.txt", b"test stored", compress_type=zipfile.ZIP_STORED)
  32. zf.writestr("test2.txt", b"test deflated", compress_type=zipfile.ZIP_DEFLATED)
  33. zf.writestr("dir/dtest.txt", b"test in dir")
  34. zf.writestr("dir1/dir2/test.txt", b"test nested dir")
  35. zf.writestr("emptydir/", b"", compress_type=zipfile.ZIP_STORED)
  36. zf.close()
  37. wrapper = IStreamWrapper(stream)
  38. zip = ZipArchive()
  39. zip.open_read(wrapper)
  40. assert zip.is_read_valid()
  41. assert not zip.is_write_valid()
  42. assert not zip.needs_repack()
  43. assert zip.verify()
  44. assert zip.find_subfile("nonexistent.txt") == -1
  45. sf = zip.find_subfile("test.txt")
  46. assert sf >= 0
  47. assert zip.read_subfile(sf) == b"test stored"
  48. assert zip.extract_subfile(sf, tmp_path / "test.txt")
  49. assert open(tmp_path / "test.txt", 'rb').read() == b"test stored"
  50. sf = zip.find_subfile("test2.txt")
  51. assert sf >= 0
  52. assert zip.read_subfile(sf) == b"test deflated"
  53. assert zip.extract_subfile(sf, tmp_path / "test2.txt")
  54. assert open(tmp_path / "test2.txt", 'rb').read() == b"test deflated"
  55. def test_zip_write():
  56. stream = StringStream()
  57. zip = ZipArchive()
  58. zip.open_write(stream)
  59. zip.add_subfile("test.txt", StringStream(b"test deflated"), 6)
  60. zip.add_subfile("test2.txt", StringStream(b"test stored"), 0)
  61. zip.close()
  62. with zipfile.ZipFile(StreamIOWrapper(stream), 'r') as zf:
  63. assert zf.testzip() is None
  64. assert tuple(sorted(zf.namelist())) == ("test.txt", "test2.txt")
  65. def test_zip_replace_subfile(tmp_path):
  66. stream = StringStream()
  67. zf = zipfile.ZipFile(StreamIOWrapper(stream), mode='w', allowZip64=True)
  68. zf.writestr("test1.txt", b"contents of first file")
  69. zf.writestr("test2.txt", b"")
  70. zf.writestr("test3.txt", b"contents of third file")
  71. zf.close()
  72. zip = ZipArchive()
  73. zip.open_read_write(stream)
  74. assert zip.is_read_valid()
  75. assert zip.is_write_valid()
  76. assert not zip.needs_repack()
  77. assert zip.verify()
  78. sf = zip.find_subfile("test2.txt")
  79. assert sf >= 0
  80. zip.add_subfile("test2.txt", StringStream(b"contents of second file"), 6)
  81. zip.close()
  82. with zipfile.ZipFile(StreamIOWrapper(stream), 'r') as zf:
  83. assert zf.testzip() is None
  84. assert zf.read("test1.txt") == b"contents of first file"
  85. assert zf.read("test2.txt") == b"contents of second file"
  86. assert zf.read("test3.txt") == b"contents of third file"
  87. def test_zip_remove_subfile(tmp_path):
  88. stream = StringStream()
  89. zf = zipfile.ZipFile(StreamIOWrapper(stream), mode='w', allowZip64=True)
  90. zf.writestr("test1.txt", b"contents of first file")
  91. zf.writestr("test2.txt", b"contents of second file")
  92. zf.writestr("test3.txt", b"contents of third file")
  93. zf.close()
  94. zip = ZipArchive()
  95. zip.open_read_write(stream)
  96. assert zip.is_read_valid()
  97. assert zip.is_write_valid()
  98. assert not zip.needs_repack()
  99. assert zip.verify()
  100. removed = zip.remove_subfile("test2.txt")
  101. assert removed
  102. zip.close()
  103. with zipfile.ZipFile(StreamIOWrapper(stream), 'r') as zf:
  104. assert zf.testzip() is None
  105. names = zf.namelist()
  106. assert "test1.txt" in names
  107. assert "test2.txt" not in names
  108. assert "test3.txt" in names
  109. def test_zip_repack(tmp_path):
  110. zip_path = tmp_path / "test_zip_repack.zip"
  111. zf = zipfile.ZipFile(zip_path, mode='w', allowZip64=True)
  112. zf.writestr("test1.txt", b"contents of first file")
  113. zf.writestr("test2.txt", b"contents of second file")
  114. zf.close()
  115. zip = ZipArchive()
  116. zip.open_read_write(zip_path)
  117. assert zip.is_read_valid()
  118. assert zip.is_write_valid()
  119. assert not zip.needs_repack()
  120. assert zip.verify()
  121. removed = zip.remove_subfile("test2.txt")
  122. assert removed
  123. zip.add_subfile("test3.txt", StringStream(b"contents of third file"), 6)
  124. assert zip.needs_repack()
  125. result = zip.repack()
  126. assert result
  127. assert not zip.needs_repack()
  128. assert zip.verify()
  129. zip.close()
  130. with zipfile.ZipFile(zip_path, 'r') as zf:
  131. assert zf.testzip() is None
  132. assert zf.read("test1.txt") == b"contents of first file"
  133. assert "test2.txt" not in zf.namelist()
  134. assert zf.read("test3.txt") == b"contents of third file"
  135. def test_zip_jar_signature():
  136. cur_dir = Filename.from_os_specific(os.path.dirname(__file__))
  137. stream = StringStream()
  138. zip = ZipArchive()
  139. zip.open_read_write(stream)
  140. zip.add_subfile("test.txt", StringStream(b"contents of test file"), 6)
  141. zip.add_jar_signature(Filename(cur_dir, "cert.pem"), Filename(cur_dir, "private.pem"))
  142. zip.close()
  143. with zipfile.ZipFile(StreamIOWrapper(stream), 'r') as zf:
  144. assert zf.read("META-INF/MANIFEST.MF") == b'Manifest-Version: 1.0\r\n\r\nName: test.txt\r\nSHA-256-Digest: k5XWgStAZvRlNWIcz67qLSzso8Mc+OUG1QOlAwysyhE=\r\n\r\n'
  145. assert zf.read("META-INF/CERT.SF") == b'Signature-Version: 1.0\r\nSHA-256-Digest-Manifest-Main-Attributes: VmrRqAIgAm0FCZViZFzpaP8OfDbN4iY0MyYFuzTMPv8=\r\nSHA-256-Digest-Manifest: 9R83KbhgHCBaYGXhJ/bV2MofgjRU254oUx+YilOvRcE=\r\n\r\nName: test.txt\r\nSHA-256-Digest: q8FmiLsrdoC5XQRaN9KmaPCcd2revsR0NzDul9cK6bk=\r\n\r\n'