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.
 
 
 
 
 

50 lines
1.2 KiB

  1. import hashlib
  2. import libarchive
  3. import pathlib
  4. import unittest
  5. _defaulthash = 'sha512'
  6. def _readfp(fp):
  7. while True:
  8. r = fp.read(64*1024)
  9. # libarchive returns None on EOF
  10. if r == b'' or r is None:
  11. return
  12. yield r
  13. def _hashfp(fp):
  14. hash = getattr(hashlib, _defaulthash)()
  15. for r in _readfp(fp):
  16. hash.update(r)
  17. return '%s:%s' % (_defaulthash, hash.hexdigest())
  18. class TestArchive(unittest.TestCase):
  19. def setUp(self):
  20. self.fixtures = pathlib.Path(__file__).parent.parent / 'fixtures'
  21. def test_closed(self):
  22. fname = self.fixtures / 'testfile.tar.gz'
  23. with libarchive.Archive(fname) as arch:
  24. origfp = arch.f
  25. hashes = []
  26. for i in arch:
  27. if not i.isfile():
  28. continue
  29. with arch.readstream(i.size) as fp:
  30. hashes.append(_hashfp(fp))
  31. self.assertTrue(fp.closed)
  32. self.assertIsNone(arch._stream)
  33. self.assertEqual(hashes, [ 'sha512:90f8342520f0ac57fb5a779f5d331c2fa87aa40f8799940257f9ba619940951e67143a8d746535ed0284924b2b7bc1478f095198800ba96d01847d7b56ca465c', 'sha512:7d5768d47b6bc27dc4fa7e9732cfa2de506ca262a2749cb108923e5dddffde842bbfee6cb8d692fb43aca0f12946c521cce2633887914ca1f96898478d10ad3f' ])
  34. self.assertTrue(arch.f.closed)