test_pack.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. def _test_roundtrip(self, base, target):
  81. self.assertEquals(target,
  82. apply_delta(base, create_delta(base, target)))
  83. def test_nochange(self):
  84. self._test_roundtrip(self.test_string1, self.test_string1)
  85. def test_change(self):
  86. self._test_roundtrip(self.test_string1, self.test_string2)
  87. def test_rewrite(self):
  88. self._test_roundtrip(self.test_string1, self.test_string3)
  89. class TestPackData(PackTests):
  90. """Tests getting the data from the packfile."""
  91. def test_create_pack(self):
  92. p = self.get_pack_data(pack1_sha)
  93. def test_pack_len(self):
  94. p = self.get_pack_data(pack1_sha)
  95. self.assertEquals(3, len(p))
  96. def test_index_check(self):
  97. p = self.get_pack_data(pack1_sha)
  98. self.assertEquals(True, p.check())
  99. def test_iterobjects(self):
  100. p = self.get_pack_data(pack1_sha)
  101. 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()))
  102. def test_iterentries(self):
  103. p = self.get_pack_data(pack1_sha)
  104. 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()))
  105. def test_create_index_v1(self):
  106. p = self.get_pack_data(pack1_sha)
  107. p.create_index_v1("v1test.idx")
  108. idx1 = PackIndex("v1test.idx")
  109. idx2 = self.get_pack_index(pack1_sha)
  110. self.assertEquals(idx1, idx2)
  111. def test_create_index_v2(self):
  112. p = self.get_pack_data(pack1_sha)
  113. p.create_index_v2("v2test.idx")
  114. idx1 = PackIndex("v2test.idx")
  115. idx2 = self.get_pack_index(pack1_sha)
  116. self.assertEquals(idx1, idx2)
  117. class TestPack(PackTests):
  118. def test_len(self):
  119. p = self.get_pack(pack1_sha)
  120. self.assertEquals(3, len(p))
  121. def test_contains(self):
  122. p = self.get_pack(pack1_sha)
  123. self.assertTrue(tree_sha in p)
  124. def test_get(self):
  125. p = self.get_pack(pack1_sha)
  126. self.assertEquals(type(p[tree_sha]), Tree)
  127. def test_iter(self):
  128. p = self.get_pack(pack1_sha)
  129. self.assertEquals(set([tree_sha, commit_sha, a_sha]), set(p))
  130. def test_get_object_at(self):
  131. """Tests random access for non-delta objects"""
  132. p = self.get_pack(pack1_sha)
  133. obj = p[a_sha]
  134. self.assertEqual(obj._type, 'blob')
  135. self.assertEqual(obj.sha().hexdigest(), a_sha)
  136. obj = p[tree_sha]
  137. self.assertEqual(obj._type, 'tree')
  138. self.assertEqual(obj.sha().hexdigest(), tree_sha)
  139. obj = p[commit_sha]
  140. self.assertEqual(obj._type, 'commit')
  141. self.assertEqual(obj.sha().hexdigest(), commit_sha)
  142. def test_copy(self):
  143. p = self.get_pack(pack1_sha)
  144. write_pack("Elch", p.iterobjects(), len(p))
  145. self.assertEquals(p, Pack("Elch"))
  146. def test_commit_obj(self):
  147. p = self.get_pack(pack1_sha)
  148. commit = p[commit_sha]
  149. self.assertEquals("James Westby <jw+debian@jameswestby.net>", commit.author)
  150. self.assertEquals([], commit.parents)
  151. def test_name(self):
  152. p = self.get_pack(pack1_sha)
  153. self.assertEquals(pack1_sha, p.name())
  154. class TestHexToSha(unittest.TestCase):
  155. def test_simple(self):
  156. self.assertEquals('\xab\xcd' * 10, hex_to_sha("abcd" * 10))
  157. def test_reverse(self):
  158. self.assertEquals("abcd" * 10, sha_to_hex('\xab\xcd' * 10))
  159. class BaseTestPackIndexWriting(object):
  160. def test_empty(self):
  161. pack_checksum = 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7'
  162. self._write_fn("empty.idx", [], pack_checksum)
  163. idx = PackIndex("empty.idx")
  164. self.assertTrue(idx.check())
  165. self.assertEquals(idx.get_stored_checksums()[0], pack_checksum)
  166. self.assertEquals(0, len(idx))
  167. def test_single(self):
  168. pack_checksum = 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7'
  169. my_entries = [('og\x0c\x0f\xb5?\x94cv\x0br\x95\xfb\xb8\x14\xe9e\xfb \xc8', 178, 42)]
  170. my_entries.sort()
  171. self._write_fn("single.idx", my_entries, pack_checksum)
  172. idx = PackIndex("single.idx")
  173. self.assertEquals(idx.version, self._expected_version)
  174. self.assertTrue(idx.check())
  175. self.assertEquals(idx.get_stored_checksums()[0], pack_checksum)
  176. self.assertEquals(1, len(idx))
  177. actual_entries = list(idx.iterentries())
  178. self.assertEquals(len(my_entries), len(actual_entries))
  179. for a, b in zip(my_entries, actual_entries):
  180. self.assertEquals(a[0], b[0])
  181. self.assertEquals(a[1], b[1])
  182. if self._has_crc32_checksum:
  183. self.assertEquals(a[2], b[2])
  184. else:
  185. self.assertTrue(b[2] is None)
  186. class TestPackIndexWritingv1(unittest.TestCase, BaseTestPackIndexWriting):
  187. def setUp(self):
  188. unittest.TestCase.setUp(self)
  189. self._has_crc32_checksum = False
  190. self._expected_version = 1
  191. self._write_fn = write_pack_index_v1
  192. class TestPackIndexWritingv2(unittest.TestCase, BaseTestPackIndexWriting):
  193. def setUp(self):
  194. unittest.TestCase.setUp(self)
  195. self._has_crc32_checksum = True
  196. self._expected_version = 2
  197. self._write_fn = write_pack_index_v2