You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

354 lines
11 KiB

  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. #
  4. # Copyright (c) 2011, SmartFile <btimby@smartfile.com>
  5. # All rights reserved.
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions are met:
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. # * Neither the name of the organization nor the
  15. # names of its contributors may be used to endorse or promote products
  16. # derived from this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  22. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. import os, unittest, tempfile, random, string, sys
  29. import zipfile
  30. import io
  31. from libarchive import Archive, is_archive_name, is_archive
  32. from libarchive.zip import is_zipfile, ZipFile, ZipEntry
  33. TMPDIR = tempfile.mkdtemp(suffix='.python-libarchive')
  34. ZIPFILE = 'test.zip'
  35. ZIPPATH = os.path.join(TMPDIR, ZIPFILE)
  36. FILENAMES = [
  37. 'test1.txt',
  38. 'foo',
  39. # TODO: test non-ASCII chars.
  40. #'álért.txt',
  41. ]
  42. def make_temp_files():
  43. if not os.path.exists(ZIPPATH):
  44. for name in FILENAMES:
  45. with open(os.path.join(TMPDIR, name), 'w') as f:
  46. f.write(''.join(random.sample(string.ascii_letters, 10)))
  47. def make_temp_archive():
  48. make_temp_files()
  49. with zipfile.ZipFile(ZIPPATH, mode="w") as z:
  50. for name in FILENAMES:
  51. z.write(os.path.join(TMPDIR, name), arcname=name)
  52. class TestIsArchiveName(unittest.TestCase):
  53. def test_formats(self):
  54. self.assertEqual(is_archive_name('foo'), None)
  55. self.assertEqual(is_archive_name('foo.txt'), None)
  56. self.assertEqual(is_archive_name('foo.txt.gz'), None)
  57. self.assertEqual(is_archive_name('foo.tar.gz'), 'tar')
  58. self.assertEqual(is_archive_name('foo.tar.bz2'), 'tar')
  59. self.assertEqual(is_archive_name('foo.zip'), 'zip')
  60. self.assertEqual(is_archive_name('foo.rar'), 'rar')
  61. self.assertEqual(is_archive_name('foo.iso'), 'iso')
  62. self.assertEqual(is_archive_name('foo.rpm'), 'cpio')
  63. class TestIsArchiveZip(unittest.TestCase):
  64. def setUp(self):
  65. make_temp_archive()
  66. def test_zip(self):
  67. self.assertEqual(is_archive(ZIPPATH), True)
  68. self.assertEqual(is_archive(ZIPPATH, formats=('zip',)), True)
  69. self.assertEqual(is_archive(ZIPPATH, formats=('tar',)), False)
  70. class TestIsArchiveTar(unittest.TestCase):
  71. def test_tar(self):
  72. pass
  73. # TODO: incorporate tests from:
  74. # http://hg.python.org/cpython/file/a6e1d926cd98/Lib/test/test_zipfile.py
  75. class TestZipRead(unittest.TestCase):
  76. def setUp(self):
  77. make_temp_archive()
  78. self.f = open(ZIPPATH, mode='r')
  79. def tearDown(self):
  80. self.f.close()
  81. def test_iszipfile(self):
  82. self.assertEqual(is_zipfile('/dev/null'), False)
  83. self.assertEqual(is_zipfile(ZIPPATH), True)
  84. def test_iterate(self):
  85. z = ZipFile(self.f, 'r')
  86. count = 0
  87. for e in z:
  88. count += 1
  89. self.assertEqual(count, len(FILENAMES), 'Did not enumerate correct number of items in archive.')
  90. def test_deferred_close_by_archive(self):
  91. """Test archive deferred close without a stream."""
  92. z = ZipFile(self.f, 'r')
  93. self.assertIsNotNone(z._a)
  94. self.assertIsNone(z._stream)
  95. z.close()
  96. self.assertIsNone(z._a)
  97. def test_deferred_close_by_stream(self):
  98. """Ensure archive closes self if stream is closed first."""
  99. z = ZipFile(self.f, 'r')
  100. stream = z.readstream(FILENAMES[0])
  101. stream.close()
  102. # Make sure archive stays open after stream is closed.
  103. self.assertIsNotNone(z._a)
  104. self.assertIsNone(z._stream)
  105. z.close()
  106. self.assertIsNone(z._a)
  107. self.assertTrue(stream.closed)
  108. def test_close_stream_first(self):
  109. """Ensure that archive stays open after being closed if a stream is
  110. open. Further, ensure closing the stream closes the archive."""
  111. z = ZipFile(self.f, 'r')
  112. stream = z.readstream(FILENAMES[0])
  113. z.close()
  114. try:
  115. stream.read()
  116. except:
  117. self.fail("Reading stream from closed archive failed!")
  118. stream.close()
  119. # Now the archive should close.
  120. self.assertIsNone(z._a)
  121. self.assertTrue(stream.closed)
  122. self.assertIsNone(z._stream)
  123. def test_filenames(self):
  124. z = ZipFile(self.f, 'r')
  125. names = []
  126. for e in z:
  127. names.append(e.filename)
  128. self.assertEqual(names, FILENAMES, 'File names differ in archive.')
  129. # ~ def test_non_ascii(self):
  130. # ~ pass
  131. def test_extract_str(self):
  132. pass
  133. class TestZipWrite(unittest.TestCase):
  134. def setUp(self):
  135. make_temp_files()
  136. self.f = open(ZIPPATH, mode='w')
  137. def tearDown(self):
  138. self.f.close()
  139. def test_writepath(self):
  140. z = ZipFile(self.f, 'w')
  141. for fname in FILENAMES:
  142. with open(os.path.join(TMPDIR, fname), 'r') as f:
  143. z.writepath(f)
  144. z.close()
  145. def test_writepath_directory(self):
  146. """Test writing a directory."""
  147. z = ZipFile(self.f, 'w')
  148. z.writepath(None, pathname='/testdir', folder=True)
  149. z.writepath(None, pathname='/testdir/testinside', folder=True)
  150. z.close()
  151. self.f.close()
  152. f = open(ZIPPATH, mode='r')
  153. z = ZipFile(f, 'r')
  154. entries = z.infolist()
  155. assert len(entries) == 2
  156. assert entries[0].isdir()
  157. z.close()
  158. f.close()
  159. def test_writestream(self):
  160. z = ZipFile(self.f, 'w')
  161. for fname in FILENAMES:
  162. full_path = os.path.join(TMPDIR, fname)
  163. i = open(full_path)
  164. o = z.writestream(fname)
  165. while True:
  166. data = i.read(1)
  167. if not data:
  168. break
  169. o.write(data)
  170. o.close()
  171. i.close()
  172. z.close()
  173. def test_writestream_unbuffered(self):
  174. z = ZipFile(self.f, 'w')
  175. for fname in FILENAMES:
  176. full_path = os.path.join(TMPDIR, fname)
  177. i = open(full_path)
  178. o = z.writestream(fname, os.path.getsize(full_path))
  179. while True:
  180. data = i.read(1)
  181. if not data:
  182. break
  183. o.write(data)
  184. o.close()
  185. i.close()
  186. z.close()
  187. def test_deferred_close_by_archive(self):
  188. """Test archive deferred close without a stream."""
  189. z = ZipFile(self.f, 'w')
  190. o = z.writestream(FILENAMES[0])
  191. z.close()
  192. self.assertIsNotNone(z._a)
  193. self.assertIsNotNone(z._stream)
  194. o.write('testdata')
  195. o.close()
  196. self.assertIsNone(z._a)
  197. self.assertIsNone(z._stream)
  198. z.close()
  199. import base64
  200. # ZIP_CONTENT is base64 encoded password protected zip file with password: 'pwd' and following contents:
  201. # unzip -l /tmp/zzz.zip
  202. #Archive: /tmp/zzz.zip
  203. # Length Date Time Name
  204. #--------- ---------- ----- ----
  205. # 9 08-09-2022 19:29 test.txt
  206. #--------- -------
  207. # 9 1 file
  208. ZIP_CONTENT='UEsDBAoACQAAAKubCVVjZ7b1FQAAAAkAAAAIABwAdGVzdC50eHRVVAkAA5K18mKStfJid' + \
  209. 'XgLAAEEAAAAAAQAAAAA5ryoP1rrRK5apjO41YMAPjpkWdU3UEsHCGNntvUVAAAACQAAAF' + \
  210. 'BLAQIeAwoACQAAAKubCVVjZ7b1FQAAAAkAAAAIABgAAAAAAAEAAACkgQAAAAB0ZXN0LnR' + \
  211. '4dFVUBQADkrXyYnV4CwABBAAAAAAEAAAAAFBLBQYAAAAAAQABAE4AAABnAAAAAAA='
  212. ITEM_CONTENT='test.txt\n'
  213. ITEM_NAME='test.txt'
  214. ZIP1_PWD='pwd'
  215. ZIP2_PWD='12345'
  216. def create_file_from_content():
  217. with open(ZIPPATH, mode='wb') as f:
  218. f.write(base64.b64decode(ZIP_CONTENT))
  219. def create_protected_zip():
  220. z = ZipFile(ZIPPATH, mode='w', password=ZIP2_PWD)
  221. z.writestr(ITEM_NAME, ITEM_CONTENT)
  222. z.close()
  223. class TestProtectedReading(unittest.TestCase):
  224. def setUp(self):
  225. create_file_from_content()
  226. def tearDown(self):
  227. os.remove(ZIPPATH)
  228. def test_read_with_password(self):
  229. z = ZipFile(ZIPPATH, 'r', password=ZIP1_PWD)
  230. self.assertEqual(z.read(ITEM_NAME), bytes(ITEM_CONTENT, 'utf-8'))
  231. z.close()
  232. def test_read_without_password(self):
  233. z = ZipFile(ZIPPATH, 'r')
  234. self.assertRaises(RuntimeError, z.read, ITEM_NAME)
  235. z.close()
  236. def test_read_with_wrong_password(self):
  237. z = ZipFile(ZIPPATH, 'r', password='wrong')
  238. self.assertRaises(RuntimeError, z.read, ITEM_NAME)
  239. z.close()
  240. class TestProtectedWriting(unittest.TestCase):
  241. def setUp(self):
  242. create_protected_zip()
  243. def tearDown(self):
  244. os.remove(ZIPPATH)
  245. def test_read_with_password(self):
  246. z = ZipFile(ZIPPATH, 'r', password=ZIP2_PWD)
  247. self.assertEqual(z.read(ITEM_NAME), bytes(ITEM_CONTENT, 'utf-8'))
  248. z.close()
  249. def test_read_without_password(self):
  250. z = ZipFile(ZIPPATH, 'r')
  251. self.assertRaises(RuntimeError, z.read, ITEM_NAME)
  252. z.close()
  253. def test_read_with_wrong_password(self):
  254. z = ZipFile(ZIPPATH, 'r', password='wrong')
  255. self.assertRaises(RuntimeError, z.read, ITEM_NAME)
  256. z.close()
  257. def test_read_with_password_list(self):
  258. z = ZipFile(ZIPPATH, 'r', password=[ZIP1_PWD, ZIP2_PWD])
  259. self.assertEqual(z.read(ITEM_NAME), bytes(ITEM_CONTENT, 'utf-8'))
  260. z.close()
  261. class TestHighLevelAPI(unittest.TestCase):
  262. def setUp(self):
  263. make_temp_archive()
  264. def _test_listing_content(self, f):
  265. """Test helper capturing file paths while iterating the archive."""
  266. found = []
  267. with Archive(f) as a:
  268. for entry in a:
  269. found.append(entry.pathname)
  270. self.assertEqual(set(found), set(FILENAMES))
  271. def test_open_by_name(self):
  272. """Test an archive opened directly by name."""
  273. self._test_listing_content(ZIPPATH)
  274. def test_open_by_named_fobj(self):
  275. """Test an archive using a file-like object opened by name."""
  276. with open(ZIPPATH, 'rb') as f:
  277. self._test_listing_content(f)
  278. def test_open_by_unnamed_fobj(self):
  279. """Test an archive using file-like object opened by fileno()."""
  280. with open(ZIPPATH, 'rb') as zf:
  281. with io.FileIO(zf.fileno(), mode='r', closefd=False) as f:
  282. self._test_listing_content(f)
  283. if __name__ == '__main__':
  284. unittest.main()