af04a496dd7282f1df0add75f2aa0221162aaecc
[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 import os
21 import unittest
22
23 from dulwich.pack import (
24         PackIndex,
25         PackData,
26         hex_to_sha,
27         multi_ord,
28         write_pack_index,
29         write_pack,
30         )
31
32 pack1_sha = 'bc63ddad95e7321ee734ea11a7a62d314e0d7481'
33
34 a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
35 tree_sha = 'b2a2766a2879c209ab1176e7e778b81ae422eeaa'
36 commit_sha = 'f18faa16531ac570a3fdc8c7ca16682548dafd12'
37
38 class PackTests(unittest.TestCase):
39   """Base class for testing packs"""
40
41   datadir = os.path.join(os.path.dirname(__file__), 'data/packs')
42
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
47   def get_pack_data(self, sha):
48     """Returns a PackData object from the datadir with the given sha"""
49     return PackData(os.path.join(self.datadir, 'pack-%s.pack' % sha))
50
51
52 class PackIndexTests(PackTests):
53   """Class that tests the index of packfiles"""
54
55   def test_object_index(self):
56     """Tests that the correct object offset is returned from the index."""
57     p = self.get_pack_index(pack1_sha)
58     self.assertEqual(p.object_index(pack1_sha), None)
59     self.assertEqual(p.object_index(a_sha), 178)
60     self.assertEqual(p.object_index(tree_sha), 138)
61     self.assertEqual(p.object_index(commit_sha), 12)
62
63
64 class TestPackData(PackTests):
65   """Tests getting the data from the packfile."""
66
67   def test_create_pack(self):
68     p = self.get_pack_data(pack1_sha)
69
70   def test_get_object_at(self):
71     """Tests random access for non-delta objects"""
72     p = self.get_pack_data(pack1_sha)
73     idx = self.get_pack_index(pack1_sha)
74     obj = p.get_object_at(idx.object_index(a_sha))
75     self.assertEqual(obj._type, 'blob')
76     self.assertEqual(obj.sha().hexdigest(), a_sha)
77     obj = p.get_object_at(idx.object_index(tree_sha))
78     self.assertEqual(obj._type, 'tree')
79     self.assertEqual(obj.sha().hexdigest(), tree_sha)
80     obj = p.get_object_at(idx.object_index(commit_sha))
81     self.assertEqual(obj._type, 'commit')
82     self.assertEqual(obj.sha().hexdigest(), commit_sha)
83
84   def test_pack_len(self):
85     p = self.get_pack_data(pack1_sha)
86     self.assertEquals(3, len(p))
87
88   def test_index_len(self):
89     p = self.get_pack_index(pack1_sha)
90     self.assertEquals(3, len(p))
91
92   def test_get_stored_checksum(self):
93     p = self.get_pack_index(pack1_sha)
94     self.assertEquals("\xf2\x84\x8e*\xd1o2\x9a\xe1\xc9.;\x95\xe9\x18\x88\xda\xa5\xbd\x01", str(p.get_stored_checksums()[1]))
95     self.assertEquals( 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7' , str(p.get_stored_checksums()[0]))
96
97   def test_check(self):
98     p = self.get_pack_index(pack1_sha)
99     self.assertEquals(True, p.check())
100
101   def test_iterentries(self):
102     p = self.get_pack_index(pack1_sha)
103     self.assertEquals([(178, 'og\x0c\x0f\xb5?\x94cv\x0br\x95\xfb\xb8\x14\xe9e\xfb \xc8'), (138, '\xb2\xa2vj(y\xc2\t\xab\x11v\xe7\xe7x\xb8\x1a\xe4"\xee\xaa'), (12, '\xf1\x8f\xaa\x16S\x1a\xc5p\xa3\xfd\xc8\xc7\xca\x16h%H\xda\xfd\x12')], list(p.iterentries()))
104
105
106 class TestHexToSha(unittest.TestCase):
107
108     def test_simple(self):
109         self.assertEquals('\xab\xcd\x0e', hex_to_sha("abcde"))
110
111
112 class TestMultiOrd(unittest.TestCase):
113
114     def test_simple(self):
115         self.assertEquals(418262508645L, multi_ord("abcde", 0, 5))
116
117
118 class TestPackIndexWriting(unittest.TestCase):
119
120     def test_empty(self):
121         pack_checksum = 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7'
122         write_pack_index("empty.idx", [], pack_checksum)
123         idx = PackIndex("empty.idx")
124         self.assertTrue(idx.check())
125         self.assertEquals(idx.get_stored_checksums()[0], pack_checksum)
126         self.assertEquals(0, len(idx))