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.
 
 
 
 
 

221 lines
7.3 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, subprocess
  29. from libarchive import is_archive_name, is_archive
  30. from libarchive.zip import is_zipfile, ZipFile, ZipEntry
  31. TMPDIR = tempfile.mkdtemp()
  32. ZIPCMD = '/usr/bin/zip'
  33. ZIPFILE = 'test.zip'
  34. ZIPPATH = os.path.join(TMPDIR, ZIPFILE)
  35. FILENAMES = [
  36. 'test1.txt',
  37. 'foo',
  38. # TODO: test non-ASCII chars.
  39. #'álért.txt',
  40. ]
  41. def make_temp_files():
  42. print TMPDIR
  43. if not os.path.exists(ZIPPATH):
  44. for name in FILENAMES:
  45. file(os.path.join(TMPDIR, name), 'w').write(''.join(random.sample(string.printable, 10)))
  46. def make_temp_archive():
  47. if not os.access(ZIPCMD, os.X_OK):
  48. raise AssertionError('Cannot execute %s.' % ZIPCMD)
  49. cmd = [ZIPCMD, ZIPFILE]
  50. make_temp_files()
  51. cmd.extend(FILENAMES)
  52. os.chdir(TMPDIR)
  53. subprocess.call(cmd)
  54. class TestIsArchiveName(unittest.TestCase):
  55. def test_formats(self):
  56. self.assertEqual(is_archive_name('foo'), None)
  57. self.assertEqual(is_archive_name('foo.txt'), None)
  58. self.assertEqual(is_archive_name('foo.txt.gz'), None)
  59. self.assertEqual(is_archive_name('foo.tar.gz'), 'tar')
  60. self.assertEqual(is_archive_name('foo.tar.bz2'), 'tar')
  61. self.assertEqual(is_archive_name('foo.zip'), 'zip')
  62. self.assertEqual(is_archive_name('foo.rar'), 'rar')
  63. self.assertEqual(is_archive_name('foo.iso'), 'iso')
  64. self.assertEqual(is_archive_name('foo.rpm'), 'cpio')
  65. class TestIsArchiveZip(unittest.TestCase):
  66. def setUp(self):
  67. make_temp_archive()
  68. def test_zip(self):
  69. self.assertEqual(is_archive(ZIPPATH), True)
  70. self.assertEqual(is_archive(ZIPPATH, formats=('zip', )), True)
  71. self.assertEqual(is_archive(ZIPPATH, formats=('tar', )), False)
  72. class TestIsArchiveTar(unittest.TestCase):
  73. def test_tar(self):
  74. pass
  75. # TODO: incorporate tests from:
  76. # http://hg.python.org/cpython/file/a6e1d926cd98/Lib/test/test_zipfile.py
  77. class TestZipRead(unittest.TestCase):
  78. def setUp(self):
  79. make_temp_archive()
  80. def test_iszipfile(self):
  81. self.assertEqual(is_zipfile('/dev/null'), False)
  82. self.assertEqual(is_zipfile(ZIPPATH), True)
  83. def test_iterate(self):
  84. f = file(ZIPPATH, mode='r')
  85. z = ZipFile(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. f = file(ZIPPATH, mode='r')
  93. z = ZipFile(f, 'r')
  94. self.assertIsNotNone(z._a)
  95. self.assertIsNone(z._stream)
  96. z.close()
  97. self.assertIsNone(z._a)
  98. def test_deferred_close_by_stream(self):
  99. """ Ensure archive closes self if stream is closed first. """
  100. f = file(ZIPPATH, mode='r')
  101. z = ZipFile(f, 'r')
  102. stream = z.readstream(FILENAMES[0])
  103. stream.close()
  104. # Make sure archive stays open after stream is closed.
  105. self.assertIsNotNone(z._a)
  106. self.assertIsNone(z._stream)
  107. z.close()
  108. self.assertIsNone(z._a)
  109. self.assertTrue(stream.closed)
  110. def test_close_stream_first(self):
  111. """ Ensure that archive stays open after being closed if a stream is
  112. open. Further, ensure closing the stream closes the archive. """
  113. f = file(ZIPPATH, mode='r')
  114. z = ZipFile(f, 'r')
  115. stream = z.readstream(FILENAMES[0])
  116. z.close()
  117. try:
  118. stream.read()
  119. except:
  120. self.fail("Reading stream from closed archive failed!")
  121. stream.close()
  122. # Now the archive should close.
  123. self.assertIsNone(z._a)
  124. self.assertTrue(stream.closed)
  125. self.assertIsNone(z._stream)
  126. def test_filenames(self):
  127. f = file(ZIPPATH, mode='r')
  128. z = ZipFile(f, 'r')
  129. names = []
  130. for e in z:
  131. names.append(e.filename)
  132. self.assertEqual(names, FILENAMES, 'File names differ in archive.')
  133. #~ def test_non_ascii(self):
  134. #~ pass
  135. def test_extract_str(self):
  136. pass
  137. class TestZipWrite(unittest.TestCase):
  138. def setUp(self):
  139. make_temp_files()
  140. def test_writepath(self):
  141. f = file(ZIPPATH, mode='w')
  142. z = ZipFile(f, 'w')
  143. for fname in FILENAMES:
  144. z.writepath(file(os.path.join(TMPDIR, fname), 'r'))
  145. z.close()
  146. def test_writestream(self):
  147. f = file(ZIPPATH, mode='w')
  148. z = ZipFile(f, 'w')
  149. for fname in FILENAMES:
  150. full_path = os.path.join(TMPDIR, fname)
  151. i = file(full_path)
  152. o = z.writestream(fname)
  153. while True:
  154. data = i.read(1)
  155. if not data:
  156. break
  157. o.write(data)
  158. o.close()
  159. i.close()
  160. z.close()
  161. def test_writestream_unbuffered(self):
  162. f = file(ZIPPATH, mode='w')
  163. z = ZipFile(f, 'w')
  164. for fname in FILENAMES:
  165. full_path = os.path.join(TMPDIR, fname)
  166. i = file(full_path)
  167. o = z.writestream(fname, os.path.getsize(full_path))
  168. while True:
  169. data = i.read(1)
  170. if not data:
  171. break
  172. o.write(data)
  173. o.close()
  174. i.close()
  175. z.close()
  176. def test_deferred_close_by_archive(self):
  177. """ Test archive deferred close without a stream. """
  178. f = file(ZIPPATH, mode='w')
  179. z = ZipFile(f, 'w')
  180. o = z.writestream(FILENAMES[0])
  181. z.close()
  182. self.assertIsNotNone(z._a)
  183. self.assertIsNotNone(z._stream)
  184. o.write('testdata')
  185. o.close()
  186. self.assertIsNone(z._a)
  187. self.assertIsNone(z._stream)
  188. z.close()
  189. if __name__ == '__main__':
  190. unittest.main()