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.
 
 
 
 
 

431 lines
14 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 hashlib
  30. import io
  31. import pathlib
  32. import shutil
  33. import zipfile
  34. from libarchive import Archive, is_archive_name, is_archive
  35. from libarchive.zip import is_zipfile, ZipFile, ZipEntry
  36. FILENAMES = [
  37. 'test1.txt',
  38. 'foo',
  39. # TODO: test non-ASCII chars.
  40. #'álért.txt',
  41. ]
  42. class MakeTempMixIn:
  43. def setUp(self):
  44. self.TMPDIR = tempfile.mkdtemp(suffix='.python-libarchive')
  45. self.ZIPFILE = 'test.zip'
  46. self.ZIPPATH = os.path.join(self.TMPDIR, self.ZIPFILE)
  47. def tearDown(self):
  48. shutil.rmtree(self.TMPDIR)
  49. self.TMPDIR = None
  50. self.ZIPFILE = None
  51. self.ZIPPATH = None
  52. def make_temp_files(self):
  53. if not os.path.exists(self.ZIPPATH):
  54. for name in FILENAMES:
  55. with open(os.path.join(self.TMPDIR, name), 'w') as f:
  56. f.write(''.join(random.sample(string.ascii_letters, 10)))
  57. def make_temp_archive(self):
  58. self.make_temp_files()
  59. with zipfile.ZipFile(self.ZIPPATH, mode="w") as z:
  60. for name in FILENAMES:
  61. z.write(os.path.join(self.TMPDIR, name), arcname=name)
  62. class TestIsArchiveName(unittest.TestCase):
  63. def test_formats(self):
  64. self.assertEqual(is_archive_name('foo'), None)
  65. self.assertEqual(is_archive_name('foo.txt'), None)
  66. self.assertEqual(is_archive_name('foo.txt.gz'), None)
  67. self.assertEqual(is_archive_name('foo.tar.gz'), 'tar')
  68. self.assertEqual(is_archive_name('foo.tar.bz2'), 'tar')
  69. self.assertEqual(is_archive_name('foo.zip'), 'zip')
  70. self.assertEqual(is_archive_name('foo.rar'), 'rar')
  71. self.assertEqual(is_archive_name('foo.iso'), 'iso')
  72. self.assertEqual(is_archive_name('foo.rpm'), 'cpio')
  73. class TestIsArchiveZip(unittest.TestCase, MakeTempMixIn):
  74. def setUp(self):
  75. MakeTempMixIn.setUp(self)
  76. self.make_temp_archive()
  77. def tearDown(self):
  78. MakeTempMixIn.tearDown(self)
  79. def test_zip(self):
  80. self.assertEqual(is_archive(self.ZIPPATH), True)
  81. self.assertEqual(is_archive(self.ZIPPATH, formats=('zip',)), True)
  82. self.assertEqual(is_archive(self.ZIPPATH, formats=('tar',)), False)
  83. class TestIsArchiveTar(unittest.TestCase):
  84. def test_tar(self):
  85. pass
  86. # TODO: incorporate tests from:
  87. # http://hg.python.org/cpython/file/a6e1d926cd98/Lib/test/test_zipfile.py
  88. class TestZipRead(unittest.TestCase, MakeTempMixIn):
  89. def setUp(self):
  90. MakeTempMixIn.setUp(self)
  91. self.make_temp_archive()
  92. self.f = open(self.ZIPPATH, mode='r')
  93. def tearDown(self):
  94. self.f.close()
  95. MakeTempMixIn.tearDown(self)
  96. def test_iszipfile(self):
  97. self.assertEqual(is_zipfile('/dev/null'), False)
  98. self.assertEqual(is_zipfile(self.ZIPPATH), True)
  99. def test_iterate(self):
  100. z = ZipFile(self.f, 'r')
  101. count = 0
  102. for e in z:
  103. count += 1
  104. self.assertEqual(count, len(FILENAMES), 'Did not enumerate correct number of items in archive.')
  105. def test_deferred_close_by_archive(self):
  106. """Test archive deferred close without a stream."""
  107. z = ZipFile(self.f, 'r')
  108. self.assertIsNotNone(z._a)
  109. self.assertIsNone(z._stream)
  110. z.close()
  111. self.assertIsNone(z._a)
  112. def test_deferred_close_by_stream(self):
  113. """Ensure archive closes self if stream is closed first."""
  114. z = ZipFile(self.f, 'r')
  115. stream = z.readstream(FILENAMES[0])
  116. stream.close()
  117. # Make sure archive stays open after stream is closed.
  118. self.assertIsNotNone(z._a)
  119. self.assertIsNone(z._stream)
  120. z.close()
  121. self.assertIsNone(z._a)
  122. self.assertTrue(stream.closed)
  123. def test_close_stream_first(self):
  124. """Ensure that archive stays open after being closed if a stream is
  125. open. Further, ensure closing the stream closes the archive."""
  126. z = ZipFile(self.f, 'r')
  127. stream = z.readstream(FILENAMES[0])
  128. z.close()
  129. try:
  130. stream.read()
  131. except:
  132. self.fail("Reading stream from closed archive failed!")
  133. stream.close()
  134. # Now the archive should close.
  135. self.assertIsNone(z._a)
  136. self.assertTrue(stream.closed)
  137. self.assertIsNone(z._stream)
  138. def test_filenames(self):
  139. z = ZipFile(self.f, 'r')
  140. names = []
  141. for e in z:
  142. names.append(e.filename)
  143. self.assertEqual(names, FILENAMES, 'File names differ in archive.')
  144. # ~ def test_non_ascii(self):
  145. # ~ pass
  146. def test_extract_str(self):
  147. pass
  148. class TestZipWrite(unittest.TestCase, MakeTempMixIn):
  149. def setUp(self):
  150. MakeTempMixIn.setUp(self)
  151. self.make_temp_files()
  152. self.f = open(self.ZIPPATH, mode='w')
  153. def tearDown(self):
  154. self.f.close()
  155. MakeTempMixIn.tearDown(self)
  156. def test_writepath(self):
  157. z = ZipFile(self.f, 'w')
  158. for fname in FILENAMES:
  159. with open(os.path.join(self.TMPDIR, fname), 'r') as f:
  160. z.writepath(f)
  161. z.close()
  162. def test_writepath_directory(self):
  163. """Test writing a directory."""
  164. z = ZipFile(self.f, 'w')
  165. z.writepath(None, pathname='/testdir', folder=True)
  166. z.writepath(None, pathname='/testdir/testinside', folder=True)
  167. z.close()
  168. self.f.close()
  169. f = open(self.ZIPPATH, mode='r')
  170. z = ZipFile(f, 'r')
  171. entries = z.infolist()
  172. assert len(entries) == 2
  173. assert entries[0].isdir()
  174. z.close()
  175. f.close()
  176. def test_writestream(self):
  177. z = ZipFile(self.f, 'w')
  178. for fname in FILENAMES:
  179. full_path = os.path.join(self.TMPDIR, fname)
  180. i = open(full_path)
  181. o = z.writestream(fname)
  182. while True:
  183. data = i.read(1)
  184. if not data:
  185. break
  186. o.write(data)
  187. o.close()
  188. i.close()
  189. z.close()
  190. def test_writestream_unbuffered(self):
  191. z = ZipFile(self.f, 'w')
  192. for fname in FILENAMES:
  193. full_path = os.path.join(self.TMPDIR, fname)
  194. i = open(full_path)
  195. o = z.writestream(fname, os.path.getsize(full_path))
  196. while True:
  197. data = i.read(1)
  198. if not data:
  199. break
  200. o.write(data)
  201. o.close()
  202. i.close()
  203. z.close()
  204. def test_deferred_close_by_archive(self):
  205. """Test archive deferred close without a stream."""
  206. z = ZipFile(self.f, 'w')
  207. o = z.writestream(FILENAMES[0])
  208. z.close()
  209. self.assertIsNotNone(z._a)
  210. self.assertIsNotNone(z._stream)
  211. o.write('testdata')
  212. o.close()
  213. self.assertIsNone(z._a)
  214. self.assertIsNone(z._stream)
  215. z.close()
  216. import base64
  217. # ZIP_CONTENT is base64 encoded password protected zip file with password: 'pwd' and following contents:
  218. # unzip -l /tmp/zzz.zip
  219. #Archive: /tmp/zzz.zip
  220. # Length Date Time Name
  221. #--------- ---------- ----- ----
  222. # 9 08-09-2022 19:29 test.txt
  223. #--------- -------
  224. # 9 1 file
  225. ZIP_CONTENT='UEsDBAoACQAAAKubCVVjZ7b1FQAAAAkAAAAIABwAdGVzdC50eHRVVAkAA5K18mKStfJid' + \
  226. 'XgLAAEEAAAAAAQAAAAA5ryoP1rrRK5apjO41YMAPjpkWdU3UEsHCGNntvUVAAAACQAAAF' + \
  227. 'BLAQIeAwoACQAAAKubCVVjZ7b1FQAAAAkAAAAIABgAAAAAAAEAAACkgQAAAAB0ZXN0LnR' + \
  228. '4dFVUBQADkrXyYnV4CwABBAAAAAAEAAAAAFBLBQYAAAAAAQABAE4AAABnAAAAAAA='
  229. ITEM_CONTENT='test.txt\n'
  230. ITEM_NAME='test.txt'
  231. ZIP1_PWD='pwd'
  232. ZIP2_PWD='12345'
  233. class TestProtectedReading(unittest.TestCase, MakeTempMixIn):
  234. def create_file_from_content(self):
  235. with open(self.ZIPPATH, mode='wb') as f:
  236. f.write(base64.b64decode(ZIP_CONTENT))
  237. def setUp(self):
  238. MakeTempMixIn.setUp(self)
  239. self.create_file_from_content()
  240. def tearDown(self):
  241. MakeTempMixIn.tearDown(self)
  242. def test_read_with_password(self):
  243. z = ZipFile(self.ZIPPATH, 'r', password=ZIP1_PWD)
  244. self.assertEqual(z.read(ITEM_NAME), bytes(ITEM_CONTENT, 'utf-8'))
  245. z.close()
  246. def test_read_without_password(self):
  247. z = ZipFile(self.ZIPPATH, 'r')
  248. self.assertRaises(RuntimeError, z.read, ITEM_NAME)
  249. z.close()
  250. def test_read_with_wrong_password(self):
  251. z = ZipFile(self.ZIPPATH, 'r', password='wrong')
  252. self.assertRaises(RuntimeError, z.read, ITEM_NAME)
  253. z.close()
  254. class TestProtectedWriting(unittest.TestCase, MakeTempMixIn):
  255. def create_protected_zip(self):
  256. z = ZipFile(self.ZIPPATH, mode='w', password=ZIP2_PWD)
  257. z.writestr(ITEM_NAME, ITEM_CONTENT)
  258. z.close()
  259. def setUp(self):
  260. MakeTempMixIn.setUp(self)
  261. self.create_protected_zip()
  262. def tearDown(self):
  263. MakeTempMixIn.tearDown(self)
  264. def test_read_with_password(self):
  265. z = ZipFile(self.ZIPPATH, 'r', password=ZIP2_PWD)
  266. self.assertEqual(z.read(ITEM_NAME), bytes(ITEM_CONTENT, 'utf-8'))
  267. z.close()
  268. def test_read_without_password(self):
  269. z = ZipFile(self.ZIPPATH, 'r')
  270. self.assertRaises(RuntimeError, z.read, ITEM_NAME)
  271. z.close()
  272. def test_read_with_wrong_password(self):
  273. z = ZipFile(self.ZIPPATH, 'r', password='wrong')
  274. self.assertRaises(RuntimeError, z.read, ITEM_NAME)
  275. z.close()
  276. def test_read_with_password_list(self):
  277. z = ZipFile(self.ZIPPATH, 'r', password=[ZIP1_PWD, ZIP2_PWD])
  278. self.assertEqual(z.read(ITEM_NAME), bytes(ITEM_CONTENT, 'utf-8'))
  279. z.close()
  280. class TestHighLevelAPI(unittest.TestCase, MakeTempMixIn):
  281. def setUp(self):
  282. MakeTempMixIn.setUp(self)
  283. self.make_temp_archive()
  284. def tearDown(self):
  285. MakeTempMixIn.tearDown(self)
  286. def _test_listing_content(self, f):
  287. """Test helper capturing file paths while iterating the archive."""
  288. found = []
  289. with Archive(f) as a:
  290. for entry in a:
  291. found.append(entry.pathname)
  292. self.assertEqual(set(found), set(FILENAMES))
  293. def test_open_by_name(self):
  294. """Test an archive opened directly by name."""
  295. self._test_listing_content(self.ZIPPATH)
  296. def test_open_by_named_fobj(self):
  297. """Test an archive using a file-like object opened by name."""
  298. with open(self.ZIPPATH, 'rb') as f:
  299. self._test_listing_content(f)
  300. def test_open_by_unnamed_fobj(self):
  301. """Test an archive using file-like object opened by fileno()."""
  302. with open(self.ZIPPATH, 'rb') as zf:
  303. with io.FileIO(zf.fileno(), mode='r', closefd=False) as f:
  304. self._test_listing_content(f)
  305. _defaulthash = 'sha512'
  306. def _readfp(fp):
  307. while True:
  308. r = fp.read(64*1024)
  309. # libarchive returns None on EOF
  310. if r == b'' or r is None:
  311. return
  312. yield r
  313. def _hashfp(fp):
  314. hash = getattr(hashlib, _defaulthash)()
  315. for r in _readfp(fp):
  316. hash.update(r)
  317. return '%s:%s' % (_defaulthash, hash.hexdigest())
  318. class TestArchive(unittest.TestCase):
  319. def setUp(self):
  320. self.fixtures = pathlib.Path(__file__).parent / 'fixtures'
  321. def test_closed(self):
  322. fname = self.fixtures / 'testfile.tar.gz'
  323. with Archive(fname) as arch:
  324. origfp = arch.f
  325. hashes = []
  326. for i in arch:
  327. if not i.isfile():
  328. continue
  329. with arch.readstream(i.size) as fp:
  330. hashes.append(_hashfp(fp))
  331. self.assertTrue(fp.closed)
  332. self.assertIsNone(arch._stream)
  333. self.assertEqual(hashes, [ 'sha512:90f8342520f0ac57fb5a779f5d331c2fa87aa40f8799940257f9ba619940951e67143a8d746535ed0284924b2b7bc1478f095198800ba96d01847d7b56ca465c', 'sha512:7d5768d47b6bc27dc4fa7e9732cfa2de506ca262a2749cb108923e5dddffde842bbfee6cb8d692fb43aca0f12946c521cce2633887914ca1f96898478d10ad3f' ])
  334. self.assertTrue(arch.f.closed)
  335. def test_noclose(self):
  336. fname = self.fixtures / 'testfile.tar.gz'
  337. with open(fname) as fp:
  338. with Archive(fp) as arch:
  339. pass
  340. self.assertFalse(fp.closed)
  341. self.assertTrue(fp.closed)
  342. if __name__ == '__main__':
  343. unittest.main()