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>
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.
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.
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,
23 from dulwich.objects import (
26 from dulwich.pack import (
40 pack1_sha = 'bc63ddad95e7321ee734ea11a7a62d314e0d7481'
42 a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
43 tree_sha = 'b2a2766a2879c209ab1176e7e778b81ae422eeaa'
44 commit_sha = 'f18faa16531ac570a3fdc8c7ca16682548dafd12'
46 class PackTests(unittest.TestCase):
47 """Base class for testing packs"""
49 datadir = os.path.join(os.path.dirname(__file__), 'data/packs')
51 def get_pack_index(self, sha):
52 """Returns a PackIndex from the datadir with the given sha"""
53 return PackIndex(os.path.join(self.datadir, 'pack-%s.idx' % sha))
55 def get_pack_data(self, sha):
56 """Returns a PackData object from the datadir with the given sha"""
57 return PackData(os.path.join(self.datadir, 'pack-%s.pack' % sha))
59 def get_pack(self, sha):
60 return Pack(os.path.join(self.datadir, 'pack-%s' % sha))
63 class PackIndexTests(PackTests):
64 """Class that tests the index of packfiles"""
66 def test_object_index(self):
67 """Tests that the correct object offset is returned from the index."""
68 p = self.get_pack_index(pack1_sha)
69 self.assertEqual(p.object_index(pack1_sha), None)
70 self.assertEqual(p.object_index(a_sha), 178)
71 self.assertEqual(p.object_index(tree_sha), 138)
72 self.assertEqual(p.object_index(commit_sha), 12)
74 def test_index_len(self):
75 p = self.get_pack_index(pack1_sha)
76 self.assertEquals(3, len(p))
78 def test_get_stored_checksum(self):
79 p = self.get_pack_index(pack1_sha)
80 self.assertEquals("\xf2\x84\x8e*\xd1o2\x9a\xe1\xc9.;\x95\xe9\x18\x88\xda\xa5\xbd\x01", str(p.get_stored_checksums()[1]))
81 self.assertEquals( 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7' , str(p.get_stored_checksums()[0]))
83 def test_index_check(self):
84 p = self.get_pack_index(pack1_sha)
85 self.assertEquals(True, p.check())
88 def test_iterentries(self):
89 p = self.get_pack_index(pack1_sha)
90 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()))
93 p = self.get_pack_index(pack1_sha)
94 self.assertEquals(set([tree_sha, commit_sha, a_sha]), set(p))
97 class TestPackDeltas(unittest.TestCase):
99 test_string1 = "The answer was flailing in the wind"
100 test_string2 = "The answer was falling down the pipe"
101 test_string3 = "zzzzz"
103 test_string_empty = ""
104 test_string_big = "Z" * 8192
106 def _test_roundtrip(self, base, target):
107 self.assertEquals(target,
108 apply_delta(base, create_delta(base, target)))
110 def test_nochange(self):
111 self._test_roundtrip(self.test_string1, self.test_string1)
113 def test_change(self):
114 self._test_roundtrip(self.test_string1, self.test_string2)
116 def test_rewrite(self):
117 self._test_roundtrip(self.test_string1, self.test_string3)
119 def test_overflow(self):
120 self._test_roundtrip(self.test_string_empty, self.test_string_big)
123 class TestPackData(PackTests):
124 """Tests getting the data from the packfile."""
126 def test_create_pack(self):
127 p = self.get_pack_data(pack1_sha)
129 def test_pack_len(self):
130 p = self.get_pack_data(pack1_sha)
131 self.assertEquals(3, len(p))
133 def test_index_check(self):
134 p = self.get_pack_data(pack1_sha)
135 self.assertEquals(True, p.check())
137 def test_iterobjects(self):
138 p = self.get_pack_data(pack1_sha)
139 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()))
141 def test_iterentries(self):
142 p = self.get_pack_data(pack1_sha)
143 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()))
145 def test_create_index_v1(self):
146 p = self.get_pack_data(pack1_sha)
147 p.create_index_v1("v1test.idx")
148 idx1 = PackIndex("v1test.idx")
149 idx2 = self.get_pack_index(pack1_sha)
150 self.assertEquals(idx1, idx2)
152 def test_create_index_v2(self):
153 p = self.get_pack_data(pack1_sha)
154 p.create_index_v2("v2test.idx")
155 idx1 = PackIndex("v2test.idx")
156 idx2 = self.get_pack_index(pack1_sha)
157 self.assertEquals(idx1, idx2)
161 class TestPack(PackTests):
164 p = self.get_pack(pack1_sha)
165 self.assertEquals(3, len(p))
167 def test_contains(self):
168 p = self.get_pack(pack1_sha)
169 self.assertTrue(tree_sha in p)
172 p = self.get_pack(pack1_sha)
173 self.assertEquals(type(p[tree_sha]), Tree)
176 p = self.get_pack(pack1_sha)
177 self.assertEquals(set([tree_sha, commit_sha, a_sha]), set(p))
179 def test_get_object_at(self):
180 """Tests random access for non-delta objects"""
181 p = self.get_pack(pack1_sha)
183 self.assertEqual(obj._type, 'blob')
184 self.assertEqual(obj.sha().hexdigest(), a_sha)
186 self.assertEqual(obj._type, 'tree')
187 self.assertEqual(obj.sha().hexdigest(), tree_sha)
189 self.assertEqual(obj._type, 'commit')
190 self.assertEqual(obj.sha().hexdigest(), commit_sha)
193 p = self.get_pack(pack1_sha)
194 write_pack("Elch", [(x, "") for x in p.iterobjects()], len(p))
195 self.assertEquals(p, Pack("Elch"))
197 def test_commit_obj(self):
198 p = self.get_pack(pack1_sha)
199 commit = p[commit_sha]
200 self.assertEquals("James Westby <jw+debian@jameswestby.net>", commit.author)
201 self.assertEquals([], commit.parents)
204 p = self.get_pack(pack1_sha)
205 self.assertEquals(pack1_sha, p.name())
208 class TestHexToSha(unittest.TestCase):
210 def test_simple(self):
211 self.assertEquals('\xab\xcd' * 10, hex_to_sha("abcd" * 10))
213 def test_reverse(self):
214 self.assertEquals("abcd" * 10, sha_to_hex('\xab\xcd' * 10))
217 class BaseTestPackIndexWriting(object):
219 def test_empty(self):
220 pack_checksum = 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7'
221 self._write_fn("empty.idx", [], pack_checksum)
222 idx = PackIndex("empty.idx")
223 self.assertTrue(idx.check())
224 self.assertEquals(idx.get_stored_checksums()[0], pack_checksum)
225 self.assertEquals(0, len(idx))
227 def test_single(self):
228 pack_checksum = 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7'
229 my_entries = [('og\x0c\x0f\xb5?\x94cv\x0br\x95\xfb\xb8\x14\xe9e\xfb \xc8', 178, 42)]
231 self._write_fn("single.idx", my_entries, pack_checksum)
232 idx = PackIndex("single.idx")
233 self.assertEquals(idx.version, self._expected_version)
234 self.assertTrue(idx.check())
235 self.assertEquals(idx.get_stored_checksums()[0], pack_checksum)
236 self.assertEquals(1, len(idx))
237 actual_entries = list(idx.iterentries())
238 self.assertEquals(len(my_entries), len(actual_entries))
239 for a, b in zip(my_entries, actual_entries):
240 self.assertEquals(a[0], b[0])
241 self.assertEquals(a[1], b[1])
242 if self._has_crc32_checksum:
243 self.assertEquals(a[2], b[2])
245 self.assertTrue(b[2] is None)
248 class TestPackIndexWritingv1(unittest.TestCase, BaseTestPackIndexWriting):
251 unittest.TestCase.setUp(self)
252 self._has_crc32_checksum = False
253 self._expected_version = 1
254 self._write_fn = write_pack_index_v1
257 class TestPackIndexWritingv2(unittest.TestCase, BaseTestPackIndexWriting):
260 unittest.TestCase.setUp(self)
261 self._has_crc32_checksum = True
262 self._expected_version = 2
263 self._write_fn = write_pack_index_v2
265 TEST_COMP1 = """\x78\x9c\x9d\x8e\xc1\x0a\xc2\x30\x10\x44\xef\xf9\x8a\xbd\xa9\x08\x92\x86\xb4\x26\x20\xe2\xd9\x83\x78\xf2\xbe\x49\x37\xb5\xa5\x69\xca\x36\xf5\xfb\x4d\xfd\x04\x67\x6e\x33\xcc\xf0\x32\x13\x81\xc6\x16\x8d\xa9\xbd\xad\x6c\xe3\x8a\x03\x4a\x73\xd6\xda\xd5\xa6\x51\x2e\x58\x65\x6c\x13\xbc\x94\x4a\xcc\xc8\x34\x65\x78\xa4\x89\x04\xae\xf9\x9d\x18\xee\x34\x46\x62\x78\x11\x4f\x29\xf5\x03\x5c\x86\x5f\x70\x5b\x30\x3a\x3c\x25\xee\xae\x50\xa9\xf2\x60\xa4\xaa\x34\x1c\x65\x91\xf0\x29\xc6\x3e\x67\xfa\x6f\x2d\x9e\x9c\x3e\x7d\x4b\xc0\x34\x8f\xe8\x29\x6e\x48\xa1\xa0\xc4\x88\xf3\xfe\xb0\x5b\x20\x85\xb0\x50\x06\xe4\x6e\xdd\xca\xd3\x17\x26\xfa\x49\x23"""
268 class ZlibTests(unittest.TestCase):
270 def test_simple_decompress(self):
271 self.assertEquals(("tree 4ada885c9196b6b6fa08744b5862bf92896fc002\nparent None\nauthor Jelmer Vernooij <jelmer@samba.org> 1228980214 +0000\ncommitter Jelmer Vernooij <jelmer@samba.org> 1228980214 +0000\n\nProvide replacement for mmap()'s offset argument.", 158),
272 read_zlib(TEST_COMP1, 0, 229))