test_pack.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. # test_pack.py -- Tests for the handling of git packs.
  2. # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
  3. # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; version 2
  8. # of the License, or (at your option) any later version of the license.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. # MA 02110-1301, USA.
  19. import os
  20. import unittest
  21. from dulwich.objects import (
  22. Tree,
  23. )
  24. from dulwich.pack import (
  25. Pack,
  26. PackIndex,
  27. PackData,
  28. hex_to_sha,
  29. sha_to_hex,
  30. write_pack_index_v1,
  31. write_pack_index_v2,
  32. write_pack,
  33. apply_delta,
  34. create_delta,
  35. )
  36. pack1_sha = 'bc63ddad95e7321ee734ea11a7a62d314e0d7481'
  37. a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
  38. tree_sha = 'b2a2766a2879c209ab1176e7e778b81ae422eeaa'
  39. commit_sha = 'f18faa16531ac570a3fdc8c7ca16682548dafd12'
  40. class PackTests(unittest.TestCase):
  41. """Base class for testing packs"""
  42. datadir = os.path.join(os.path.dirname(__file__), 'data/packs')
  43. def get_pack_index(self, sha):
  44. """Returns a PackIndex from the datadir with the given sha"""
  45. return PackIndex(os.path.join(self.datadir, 'pack-%s.idx' % sha))
  46. def get_pack_data(self, sha):
  47. """Returns a PackData object from the datadir with the given sha"""
  48. return PackData(os.path.join(self.datadir, 'pack-%s.pack' % sha))
  49. def get_pack(self, sha):
  50. return Pack(os.path.join(self.datadir, 'pack-%s' % sha))
  51. class PackIndexTests(PackTests):
  52. """Class that tests the index of packfiles"""
  53. def test_object_index(self):
  54. """Tests that the correct object offset is returned from the index."""
  55. p = self.get_pack_index(pack1_sha)
  56. self.assertEqual(p.object_index(pack1_sha), None)
  57. self.assertEqual(p.object_index(a_sha), 178)
  58. self.assertEqual(p.object_index(tree_sha), 138)
  59. self.assertEqual(p.object_index(commit_sha), 12)
  60. def test_index_len(self):
  61. p = self.get_pack_index(pack1_sha)
  62. self.assertEquals(3, len(p))
  63. def test_get_stored_checksum(self):
  64. p = self.get_pack_index(pack1_sha)
  65. self.assertEquals("\xf2\x84\x8e*\xd1o2\x9a\xe1\xc9.;\x95\xe9\x18\x88\xda\xa5\xbd\x01", str(p.get_stored_checksums()[1]))
  66. self.assertEquals( 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7' , str(p.get_stored_checksums()[0]))
  67. def test_index_check(self):
  68. p = self.get_pack_index(pack1_sha)
  69. self.assertEquals(True, p.check())
  70. def test_iterentries(self):
  71. p = self.get_pack_index(pack1_sha)
  72. self.assertEquals([('og\x0c\x0f\xb5?\x94cv\x0br\x95\xfb\xb8\x14\xe9e\xfb \xc8', 178, None), ('\xb2\xa2vj(y\xc2\t\xab\x11v\xe7\xe7x\xb8\x1a\xe4"\xee\xaa', 138, None), ('\xf1\x8f\xaa\x16S\x1a\xc5p\xa3\xfd\xc8\xc7\xca\x16h%H\xda\xfd\x12', 12, None)], list(p.iterentries()))
  73. def test_iter(self):
  74. p = self.get_pack_index(pack1_sha)
  75. self.assertEquals(set([tree_sha, commit_sha, a_sha]), set(p))
  76. class TestPackDeltas(unittest.TestCase):
  77. test_string1 = "The answer was flailing in the wind"
  78. test_string2 = "The answer was falling down the pipe"
  79. test_string3 = "zzzzz"
  80. test_string_empty = ""
  81. test_string_big = "Z" * 8192
  82. def _test_roundtrip(self, base, target):
  83. self.assertEquals(target,
  84. apply_delta(base, create_delta(base, target)))
  85. def test_nochange(self):
  86. self._test_roundtrip(self.test_string1, self.test_string1)
  87. def test_change(self):
  88. self._test_roundtrip(self.test_string1, self.test_string2)
  89. def test_rewrite(self):
  90. self._test_roundtrip(self.test_string1, self.test_string3)
  91. def test_overflow(self):
  92. self._test_roundtrip(self.test_string_empty, self.test_string_big)
  93. class TestPackData(PackTests):
  94. """Tests getting the data from the packfile."""
  95. def test_create_pack(self):
  96. p = self.get_pack_data(pack1_sha)
  97. def test_pack_len(self):
  98. p = self.get_pack_data(pack1_sha)
  99. self.assertEquals(3, len(p))
  100. def test_index_check(self):
  101. p = self.get_pack_data(pack1_sha)
  102. self.assertEquals(True, p.check())
  103. def test_iterobjects(self):
  104. p = self.get_pack_data(pack1_sha)
  105. self.assertEquals([(12, 1, 'tree b2a2766a2879c209ab1176e7e778b81ae422eeaa\nauthor James Westby <jw+debian@jameswestby.net> 1174945067 +0100\ncommitter James Westby <jw+debian@jameswestby.net> 1174945067 +0100\n\nTest commit\n'), (138, 2, '100644 a\x00og\x0c\x0f\xb5?\x94cv\x0br\x95\xfb\xb8\x14\xe9e\xfb \xc8'), (178, 3, 'test 1\n')], list(p.iterobjects()))
  106. def test_iterentries(self):
  107. p = self.get_pack_data(pack1_sha)
  108. self.assertEquals(set([('og\x0c\x0f\xb5?\x94cv\x0br\x95\xfb\xb8\x14\xe9e\xfb \xc8', 178, -1718046665), ('\xb2\xa2vj(y\xc2\t\xab\x11v\xe7\xe7x\xb8\x1a\xe4"\xee\xaa', 138, -901046474), ('\xf1\x8f\xaa\x16S\x1a\xc5p\xa3\xfd\xc8\xc7\xca\x16h%H\xda\xfd\x12', 12, 1185722901)]), set(p.iterentries()))
  109. def test_create_index_v1(self):
  110. p = self.get_pack_data(pack1_sha)
  111. p.create_index_v1("v1test.idx")
  112. idx1 = PackIndex("v1test.idx")
  113. idx2 = self.get_pack_index(pack1_sha)
  114. self.assertEquals(idx1, idx2)
  115. def test_create_index_v2(self):
  116. p = self.get_pack_data(pack1_sha)
  117. p.create_index_v2("v2test.idx")
  118. idx1 = PackIndex("v2test.idx")
  119. idx2 = self.get_pack_index(pack1_sha)
  120. self.assertEquals(idx1, idx2)
  121. class TestPack(PackTests):
  122. def test_len(self):
  123. p = self.get_pack(pack1_sha)
  124. self.assertEquals(3, len(p))
  125. def test_contains(self):
  126. p = self.get_pack(pack1_sha)
  127. self.assertTrue(tree_sha in p)
  128. def test_get(self):
  129. p = self.get_pack(pack1_sha)
  130. self.assertEquals(type(p[tree_sha]), Tree)
  131. def test_iter(self):
  132. p = self.get_pack(pack1_sha)
  133. self.assertEquals(set([tree_sha, commit_sha, a_sha]), set(p))
  134. def test_get_object_at(self):
  135. """Tests random access for non-delta objects"""
  136. p = self.get_pack(pack1_sha)
  137. obj = p[a_sha]
  138. self.assertEqual(obj._type, 'blob')
  139. self.assertEqual(obj.sha().hexdigest(), a_sha)
  140. obj = p[tree_sha]
  141. self.assertEqual(obj._type, 'tree')
  142. self.assertEqual(obj.sha().hexdigest(), tree_sha)
  143. obj = p[commit_sha]
  144. self.assertEqual(obj._type, 'commit')
  145. self.assertEqual(obj.sha().hexdigest(), commit_sha)
  146. def test_copy(self):
  147. p = self.get_pack(pack1_sha)
  148. write_pack("Elch", [(x, "") for x in p.iterobjects()], len(p))
  149. self.assertEquals(p, Pack("Elch"))
  150. def test_commit_obj(self):
  151. p = self.get_pack(pack1_sha)
  152. commit = p[commit_sha]
  153. self.assertEquals("James Westby <jw+debian@jameswestby.net>", commit.author)
  154. self.assertEquals([], commit.parents)
  155. def test_name(self):
  156. p = self.get_pack(pack1_sha)
  157. self.assertEquals(pack1_sha, p.name())
  158. class TestHexToSha(unittest.TestCase):
  159. def test_simple(self):
  160. self.assertEquals('\xab\xcd' * 10, hex_to_sha("abcd" * 10))
  161. def test_reverse(self):
  162. self.assertEquals("abcd" * 10, sha_to_hex('\xab\xcd' * 10))
  163. class BaseTestPackIndexWriting(object):
  164. def test_empty(self):
  165. pack_checksum = 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7'
  166. self._write_fn("empty.idx", [], pack_checksum)
  167. idx = PackIndex("empty.idx")
  168. self.assertTrue(idx.check())
  169. self.assertEquals(idx.get_stored_checksums()[0], pack_checksum)
  170. self.assertEquals(0, len(idx))
  171. def test_single(self):
  172. pack_checksum = 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7'
  173. my_entries = [('og\x0c\x0f\xb5?\x94cv\x0br\x95\xfb\xb8\x14\xe9e\xfb \xc8', 178, 42)]
  174. my_entries.sort()
  175. self._write_fn("single.idx", my_entries, pack_checksum)
  176. idx = PackIndex("single.idx")
  177. self.assertEquals(idx.version, self._expected_version)
  178. self.assertTrue(idx.check())
  179. self.assertEquals(idx.get_stored_checksums()[0], pack_checksum)
  180. self.assertEquals(1, len(idx))
  181. actual_entries = list(idx.iterentries())
  182. self.assertEquals(len(my_entries), len(actual_entries))
  183. for a, b in zip(my_entries, actual_entries):
  184. self.assertEquals(a[0], b[0])
  185. self.assertEquals(a[1], b[1])
  186. if self._has_crc32_checksum:
  187. self.assertEquals(a[2], b[2])
  188. else:
  189. self.assertTrue(b[2] is None)
  190. class TestPackIndexWritingv1(unittest.TestCase, BaseTestPackIndexWriting):
  191. def setUp(self):
  192. unittest.TestCase.setUp(self)
  193. self._has_crc32_checksum = False
  194. self._expected_version = 1
  195. self._write_fn = write_pack_index_v1
  196. class TestPackIndexWritingv2(unittest.TestCase, BaseTestPackIndexWriting):
  197. def setUp(self):
  198. unittest.TestCase.setUp(self)
  199. self._has_crc32_checksum = True
  200. self._expected_version = 2
  201. self._write_fn = write_pack_index_v2