Merge Dave's fixes for the compatibility tests and web.
[jelmer/dulwich-libgit2.git] / dulwich / tests / test_pack.py
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
20
21 """Tests for Dulwich packs."""
22
23
24 from cStringIO import StringIO
25 import os
26 import unittest
27
28 from dulwich.objects import (
29     Tree,
30     )
31 from dulwich.pack import (
32     Pack,
33     PackData,
34     apply_delta,
35     create_delta,
36     load_pack_index,
37     hex_to_sha,
38     read_zlib_chunks,
39     sha_to_hex,
40     write_pack_index_v1,
41     write_pack_index_v2,
42     write_pack,
43     )
44
45 pack1_sha = 'bc63ddad95e7321ee734ea11a7a62d314e0d7481'
46
47 a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
48 tree_sha = 'b2a2766a2879c209ab1176e7e778b81ae422eeaa'
49 commit_sha = 'f18faa16531ac570a3fdc8c7ca16682548dafd12'
50
51 class PackTests(unittest.TestCase):
52     """Base class for testing packs"""
53   
54     datadir = os.path.join(os.path.dirname(__file__), 'data/packs')
55   
56     def get_pack_index(self, sha):
57         """Returns a PackIndex from the datadir with the given sha"""
58         return load_pack_index(os.path.join(self.datadir, 'pack-%s.idx' % sha))
59   
60     def get_pack_data(self, sha):
61         """Returns a PackData object from the datadir with the given sha"""
62         return PackData(os.path.join(self.datadir, 'pack-%s.pack' % sha))
63   
64     def get_pack(self, sha):
65         return Pack(os.path.join(self.datadir, 'pack-%s' % sha))
66
67
68 class PackIndexTests(PackTests):
69     """Class that tests the index of packfiles"""
70   
71     def test_object_index(self):
72         """Tests that the correct object offset is returned from the index."""
73         p = self.get_pack_index(pack1_sha)
74         self.assertRaises(KeyError, p.object_index, pack1_sha)
75         self.assertEqual(p.object_index(a_sha), 178)
76         self.assertEqual(p.object_index(tree_sha), 138)
77         self.assertEqual(p.object_index(commit_sha), 12)
78   
79     def test_index_len(self):
80         p = self.get_pack_index(pack1_sha)
81         self.assertEquals(3, len(p))
82   
83     def test_get_stored_checksum(self):
84         p = self.get_pack_index(pack1_sha)
85         self.assertEquals("\xf2\x84\x8e*\xd1o2\x9a\xe1\xc9.;\x95\xe9\x18\x88\xda\xa5\xbd\x01", str(p.get_stored_checksum()))
86         self.assertEquals( 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7' , str(p.get_pack_checksum()))
87   
88     def test_index_check(self):
89         p = self.get_pack_index(pack1_sha)
90         self.assertEquals(True, p.check())
91   
92     def test_iterentries(self):
93         p = self.get_pack_index(pack1_sha)
94         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()))
95   
96     def test_iter(self):
97         p = self.get_pack_index(pack1_sha)
98         self.assertEquals(set([tree_sha, commit_sha, a_sha]), set(p))
99   
100
101 class TestPackDeltas(unittest.TestCase):
102   
103     test_string1 = "The answer was flailing in the wind"
104     test_string2 = "The answer was falling down the pipe"
105     test_string3 = "zzzzz"
106   
107     test_string_empty = ""
108     test_string_big = "Z" * 8192
109   
110     def _test_roundtrip(self, base, target):
111         self.assertEquals([target],
112             apply_delta(base, create_delta(base, target)))
113   
114     def test_nochange(self):
115         self._test_roundtrip(self.test_string1, self.test_string1)
116   
117     def test_change(self):
118         self._test_roundtrip(self.test_string1, self.test_string2)
119   
120     def test_rewrite(self):
121         self._test_roundtrip(self.test_string1, self.test_string3)
122   
123     def test_overflow(self):
124         self._test_roundtrip(self.test_string_empty, self.test_string_big)
125
126
127 class TestPackData(PackTests):
128     """Tests getting the data from the packfile."""
129   
130     def test_create_pack(self):
131         p = self.get_pack_data(pack1_sha)
132   
133     def test_pack_len(self):
134         p = self.get_pack_data(pack1_sha)
135         self.assertEquals(3, len(p))
136   
137     def test_index_check(self):
138         p = self.get_pack_data(pack1_sha)
139         self.assertEquals(True, p.check())
140   
141     def test_iterobjects(self):
142         p = self.get_pack_data(pack1_sha)
143         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', 3775879613L), (138, 2, '100644 a\x00og\x0c\x0f\xb5?\x94cv\x0br\x95\xfb\xb8\x14\xe9e\xfb \xc8', 912998690L), (178, 3, 'test 1\n', 1373561701L)], [(len, type, "".join(chunks), offset) for (len, type, chunks, offset) in p.iterobjects()])
144   
145     def test_iterentries(self):
146         p = self.get_pack_data(pack1_sha)
147         self.assertEquals(set([('og\x0c\x0f\xb5?\x94cv\x0br\x95\xfb\xb8\x14\xe9e\xfb \xc8', 178, 1373561701L), ('\xb2\xa2vj(y\xc2\t\xab\x11v\xe7\xe7x\xb8\x1a\xe4"\xee\xaa', 138, 912998690L), ('\xf1\x8f\xaa\x16S\x1a\xc5p\xa3\xfd\xc8\xc7\xca\x16h%H\xda\xfd\x12', 12, 3775879613L)]), set(p.iterentries()))
148   
149     def test_create_index_v1(self):
150         p = self.get_pack_data(pack1_sha)
151         p.create_index_v1("v1test.idx")
152         idx1 = load_pack_index("v1test.idx")
153         idx2 = self.get_pack_index(pack1_sha)
154         self.assertEquals(idx1, idx2)
155   
156     def test_create_index_v2(self):
157         p = self.get_pack_data(pack1_sha)
158         p.create_index_v2("v2test.idx")
159         idx1 = load_pack_index("v2test.idx")
160         idx2 = self.get_pack_index(pack1_sha)
161         self.assertEquals(idx1, idx2)
162
163
164 class TestPack(PackTests):
165
166     def test_len(self):
167         p = self.get_pack(pack1_sha)
168         self.assertEquals(3, len(p))
169
170     def test_contains(self):
171         p = self.get_pack(pack1_sha)
172         self.assertTrue(tree_sha in p)
173
174     def test_get(self):
175         p = self.get_pack(pack1_sha)
176         self.assertEquals(type(p[tree_sha]), Tree)
177
178     def test_iter(self):
179         p = self.get_pack(pack1_sha)
180         self.assertEquals(set([tree_sha, commit_sha, a_sha]), set(p))
181
182     def test_get_object_at(self):
183         """Tests random access for non-delta objects"""
184         p = self.get_pack(pack1_sha)
185         obj = p[a_sha]
186         self.assertEqual(obj.type_name, 'blob')
187         self.assertEqual(obj.sha().hexdigest(), a_sha)
188         obj = p[tree_sha]
189         self.assertEqual(obj.type_name, 'tree')
190         self.assertEqual(obj.sha().hexdigest(), tree_sha)
191         obj = p[commit_sha]
192         self.assertEqual(obj.type_name, 'commit')
193         self.assertEqual(obj.sha().hexdigest(), commit_sha)
194
195     def test_copy(self):
196         origpack = self.get_pack(pack1_sha)
197         self.assertEquals(True, origpack.index.check())
198         write_pack("Elch", [(x, "") for x in origpack.iterobjects()], 
199             len(origpack))
200         newpack = Pack("Elch")
201         self.assertEquals(origpack, newpack)
202         self.assertEquals(True, newpack.index.check())
203         self.assertEquals(origpack.name(), newpack.name())
204         self.assertEquals(origpack.index.get_pack_checksum(), 
205                           newpack.index.get_pack_checksum())
206         
207         self.assertTrue(
208                 (origpack.index.version != newpack.index.version) or
209                 (origpack.index.get_stored_checksum() == newpack.index.get_stored_checksum()))
210
211     def test_commit_obj(self):
212         p = self.get_pack(pack1_sha)
213         commit = p[commit_sha]
214         self.assertEquals("James Westby <jw+debian@jameswestby.net>",
215             commit.author)
216         self.assertEquals([], commit.parents)
217
218     def test_name(self):
219         p = self.get_pack(pack1_sha)
220         self.assertEquals(pack1_sha, p.name())
221
222
223 class TestHexToSha(unittest.TestCase):
224
225     def test_simple(self):
226         self.assertEquals('\xab\xcd' * 10, hex_to_sha("abcd" * 10))
227
228     def test_reverse(self):
229         self.assertEquals("abcd" * 10, sha_to_hex('\xab\xcd' * 10))
230
231
232 class BaseTestPackIndexWriting(object):
233
234     def test_empty(self):
235         pack_checksum = 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7'
236         self._write_fn("empty.idx", [], pack_checksum)
237         idx = load_pack_index("empty.idx")
238         self.assertTrue(idx.check())
239         self.assertEquals(idx.get_pack_checksum(), pack_checksum)
240         self.assertEquals(0, len(idx))
241
242     def test_single(self):
243         pack_checksum = 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7'
244         my_entries = [('og\x0c\x0f\xb5?\x94cv\x0br\x95\xfb\xb8\x14\xe9e\xfb \xc8', 178, 42)]
245         my_entries.sort()
246         self._write_fn("single.idx", my_entries, pack_checksum)
247         idx = load_pack_index("single.idx")
248         self.assertEquals(idx.version, self._expected_version)
249         self.assertTrue(idx.check())
250         self.assertEquals(idx.get_pack_checksum(), pack_checksum)
251         self.assertEquals(1, len(idx))
252         actual_entries = list(idx.iterentries())
253         self.assertEquals(len(my_entries), len(actual_entries))
254         for a, b in zip(my_entries, actual_entries):
255             self.assertEquals(a[0], b[0])
256             self.assertEquals(a[1], b[1])
257             if self._has_crc32_checksum:
258                 self.assertEquals(a[2], b[2])
259             else:
260                 self.assertTrue(b[2] is None)
261
262
263 class TestPackIndexWritingv1(unittest.TestCase, BaseTestPackIndexWriting):
264
265     def setUp(self):
266         unittest.TestCase.setUp(self)
267         self._has_crc32_checksum = False
268         self._expected_version = 1
269         self._write_fn = write_pack_index_v1
270
271
272 class TestPackIndexWritingv2(unittest.TestCase, BaseTestPackIndexWriting):
273
274     def setUp(self):
275         unittest.TestCase.setUp(self)
276         self._has_crc32_checksum = True
277         self._expected_version = 2
278         self._write_fn = write_pack_index_v2
279
280 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"""
281
282
283 class ZlibTests(unittest.TestCase):
284
285     def test_simple_decompress(self):
286         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, 'Z'), 
287         read_zlib_chunks(StringIO(TEST_COMP1).read, 229))