test_pack.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. )
  34. pack1_sha = 'bc63ddad95e7321ee734ea11a7a62d314e0d7481'
  35. a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
  36. tree_sha = 'b2a2766a2879c209ab1176e7e778b81ae422eeaa'
  37. commit_sha = 'f18faa16531ac570a3fdc8c7ca16682548dafd12'
  38. class PackTests(unittest.TestCase):
  39. """Base class for testing packs"""
  40. datadir = os.path.join(os.path.dirname(__file__), 'data/packs')
  41. def get_pack_index(self, sha):
  42. """Returns a PackIndex from the datadir with the given sha"""
  43. return PackIndex(os.path.join(self.datadir, 'pack-%s.idx' % sha))
  44. def get_pack_data(self, sha):
  45. """Returns a PackData object from the datadir with the given sha"""
  46. return PackData(os.path.join(self.datadir, 'pack-%s.pack' % sha))
  47. def get_pack(self, sha):
  48. return Pack(os.path.join(self.datadir, 'pack-%s' % sha))
  49. class PackIndexTests(PackTests):
  50. """Class that tests the index of packfiles"""
  51. def test_object_index(self):
  52. """Tests that the correct object offset is returned from the index."""
  53. p = self.get_pack_index(pack1_sha)
  54. self.assertEqual(p.object_index(pack1_sha), None)
  55. self.assertEqual(p.object_index(a_sha), 178)
  56. self.assertEqual(p.object_index(tree_sha), 138)
  57. self.assertEqual(p.object_index(commit_sha), 12)
  58. def test_index_len(self):
  59. p = self.get_pack_index(pack1_sha)
  60. self.assertEquals(3, len(p))
  61. def test_get_stored_checksum(self):
  62. p = self.get_pack_index(pack1_sha)
  63. self.assertEquals("\xf2\x84\x8e*\xd1o2\x9a\xe1\xc9.;\x95\xe9\x18\x88\xda\xa5\xbd\x01", str(p.get_stored_checksums()[1]))
  64. self.assertEquals( 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7' , str(p.get_stored_checksums()[0]))
  65. def test_index_check(self):
  66. p = self.get_pack_index(pack1_sha)
  67. self.assertEquals(True, p.check())
  68. def test_iterentries(self):
  69. p = self.get_pack_index(pack1_sha)
  70. 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()))
  71. def test_iter(self):
  72. p = self.get_pack_index(pack1_sha)
  73. self.assertEquals(set([tree_sha, commit_sha, a_sha]), set(p))
  74. class TestPackData(PackTests):
  75. """Tests getting the data from the packfile."""
  76. def test_create_pack(self):
  77. p = self.get_pack_data(pack1_sha)
  78. def test_pack_len(self):
  79. p = self.get_pack_data(pack1_sha)
  80. self.assertEquals(3, len(p))
  81. def test_index_check(self):
  82. p = self.get_pack_data(pack1_sha)
  83. self.assertEquals(True, p.check())
  84. def test_iterobjects(self):
  85. p = self.get_pack_data(pack1_sha)
  86. 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()))
  87. def test_iterentries(self):
  88. p = self.get_pack_data(pack1_sha)
  89. self.assertEquals([('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)], list(p.iterentries()))
  90. def test_create_index_v1(self):
  91. p = self.get_pack_data(pack1_sha)
  92. p.create_index_v1("v1test.idx")
  93. idx1 = PackIndex("v1test.idx")
  94. idx2 = self.get_pack_index(pack1_sha)
  95. self.assertEquals(idx1, idx2)
  96. def test_create_index_v2(self):
  97. p = self.get_pack_data(pack1_sha)
  98. p.create_index_v2("v2test.idx")
  99. idx1 = PackIndex("v2test.idx")
  100. idx2 = self.get_pack_index(pack1_sha)
  101. self.assertEquals(idx1, idx2)
  102. class TestPack(PackTests):
  103. def test_len(self):
  104. p = self.get_pack(pack1_sha)
  105. self.assertEquals(3, len(p))
  106. def test_contains(self):
  107. p = self.get_pack(pack1_sha)
  108. self.assertTrue(tree_sha in p)
  109. def test_get(self):
  110. p = self.get_pack(pack1_sha)
  111. self.assertEquals(type(p[tree_sha]), Tree)
  112. def test_iter(self):
  113. p = self.get_pack(pack1_sha)
  114. self.assertEquals(set([tree_sha, commit_sha, a_sha]), set(p))
  115. def test_get_object_at(self):
  116. """Tests random access for non-delta objects"""
  117. p = self.get_pack(pack1_sha)
  118. obj = p[a_sha]
  119. self.assertEqual(obj._type, 'blob')
  120. self.assertEqual(obj.sha().hexdigest(), a_sha)
  121. obj = p[tree_sha]
  122. self.assertEqual(obj._type, 'tree')
  123. self.assertEqual(obj.sha().hexdigest(), tree_sha)
  124. obj = p[commit_sha]
  125. self.assertEqual(obj._type, 'commit')
  126. self.assertEqual(obj.sha().hexdigest(), commit_sha)
  127. def test_copy(self):
  128. p = self.get_pack(pack1_sha)
  129. write_pack("testcopy", list(p.iterobjects()))
  130. self.assertEquals(p, Pack("testcopy"))
  131. class TestHexToSha(unittest.TestCase):
  132. def test_simple(self):
  133. self.assertEquals('\xab\xcd\xef', hex_to_sha("abcdef"))
  134. def test_reverse(self):
  135. self.assertEquals("abcdef", sha_to_hex('\xab\xcd\xef'))
  136. class TestPackIndexWriting(object):
  137. def test_empty(self):
  138. pack_checksum = 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7'
  139. self._write_fn("empty.idx", [], pack_checksum)
  140. idx = PackIndex("empty.idx")
  141. self.assertTrue(idx.check())
  142. self.assertEquals(idx.get_stored_checksums()[0], pack_checksum)
  143. self.assertEquals(0, len(idx))
  144. def test_single(self):
  145. pack_checksum = 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7'
  146. my_entries = [('og\x0c\x0f\xb5?\x94cv\x0br\x95\xfb\xb8\x14\xe9e\xfb \xc8', 178, 42)]
  147. self._write_fn("single.idx", my_entries, pack_checksum)
  148. idx = PackIndex("single.idx")
  149. self.assertEquals(idx.version, self._expected_version)
  150. self.assertTrue(idx.check())
  151. self.assertEquals(idx.get_stored_checksums()[0], pack_checksum)
  152. self.assertEquals(1, len(idx))
  153. actual_entries = list(idx.iterentries())
  154. self.assertEquals(len(my_entries), len(actual_entries))
  155. for a, b in zip(my_entries, actual_entries):
  156. self.assertEquals(a[0], b[0])
  157. self.assertEquals(a[1], b[1])
  158. if self._has_crc32_checksum:
  159. self.assertEquals(a[2], b[2])
  160. else:
  161. self.assertTrue(b[2] is None)
  162. class TestPackIndexWritingv1(unittest.TestCase, TestPackIndexWriting):
  163. def setUp(self):
  164. unittest.TestCase.setUp(self)
  165. self._has_crc32_checksum = False
  166. self._expected_version = 1
  167. self._write_fn = write_pack_index_v1
  168. class TestPackIndexWritingv2(unittest.TestCase, TestPackIndexWriting):
  169. def setUp(self):
  170. unittest.TestCase.setUp(self)
  171. self._has_crc32_checksum = True
  172. self._expected_version = 2
  173. self._write_fn = write_pack_index_v2