Fix a bug in the Hash checker when updating exactly to a piece boundary.
[quix0rs-apt-p2p.git] / apt_p2p / Hash.py
1
2 """Hash and store hash information for a file.
3
4 @var PIECE_SIZE: the piece size to use for hashing pieces of files
5
6 """
7
8 from binascii import b2a_hex, a2b_hex
9 import sys
10
11 from twisted.internet import threads, defer
12 from twisted.trial import unittest
13
14 PIECE_SIZE = 512*1024
15
16 class HashError(ValueError):
17     """An error has occurred while hashing a file."""
18     
19 class HashObject:
20     """Manages hashes and hashing for a file.
21     
22     @ivar ORDER: the priority ordering of hashes, and how to extract them
23
24     """
25
26     ORDER = [ {'name': 'sha1', 
27                    'length': 20,
28                    'AptPkgRecord': 'SHA1Hash', 
29                    'AptSrcRecord': False, 
30                    'AptIndexRecord': 'SHA1',
31                    'old_module': 'sha',
32                    'hashlib_func': 'sha1',
33                    },
34               {'name': 'sha256',
35                    'length': 32,
36                    'AptPkgRecord': 'SHA256Hash', 
37                    'AptSrcRecord': False, 
38                    'AptIndexRecord': 'SHA256',
39                    'hashlib_func': 'sha256',
40                    },
41               {'name': 'md5',
42                    'length': 16,
43                    'AptPkgRecord': 'MD5Hash', 
44                    'AptSrcRecord': True, 
45                    'AptIndexRecord': 'MD5SUM',
46                    'old_module': 'md5',
47                    'hashlib_func': 'md5',
48                    },
49             ]
50     
51     def __init__(self, digest = None, size = None, pieces = ''):
52         """Initialize the hash object."""
53         self.hashTypeNum = 0    # Use the first if nothing else matters
54         if sys.version_info < (2, 5):
55             # sha256 is not available in python before 2.5, remove it
56             for hashType in self.ORDER:
57                 if hashType['name'] == 'sha256':
58                     del self.ORDER[self.ORDER.index(hashType)]
59                     break
60
61         self.expHash = None
62         self.expHex = None
63         self.expSize = None
64         self.expNormHash = None
65         self.fileHasher = None
66         self.pieceHasher = None
67         self.fileHash = digest
68         self.pieceHash = [pieces[x:x+self.ORDER[self.hashTypeNum]['length']]
69                           for x in xrange(0, len(pieces), self.ORDER[self.hashTypeNum]['length'])]
70         self.size = size
71         self.fileHex = None
72         self.fileNormHash = None
73         self.done = True
74         self.result = None
75         
76     #{ Hashing data
77     def new(self, force = False):
78         """Generate a new hashing object suitable for hashing a file.
79         
80         @param force: set to True to force creating a new object even if
81             the hash has been verified already
82         """
83         if self.result is None or force:
84             self.result = None
85             self.done = False
86             self.fileHasher = self._new()
87             self.pieceHasher = None
88             self.fileHash = None
89             self.pieceHash = []
90             self.size = 0
91             self.fileHex = None
92             self.fileNormHash = None
93
94     def _new(self):
95         """Create a new hashing object according to the hash type."""
96         if sys.version_info < (2, 5):
97             mod = __import__(self.ORDER[self.hashTypeNum]['old_module'], globals(), locals(), [])
98             return mod.new()
99         else:
100             import hashlib
101             func = getattr(hashlib, self.ORDER[self.hashTypeNum]['hashlib_func'])
102             return func()
103
104     def update(self, data):
105         """Add more data to the file hasher."""
106         if self.result is None:
107             if self.done:
108                 raise HashError, "Already done, you can't add more data after calling digest() or verify()"
109             if self.fileHasher is None:
110                 raise HashError, "file hasher not initialized"
111             
112             if not self.pieceHasher and self.size + len(data) > PIECE_SIZE:
113                 # Hash up to the piece size
114                 self.fileHasher.update(data[:(PIECE_SIZE - self.size)])
115                 data = data[(PIECE_SIZE - self.size):]
116                 self.size = PIECE_SIZE
117                 self.pieceSize = 0
118
119                 # Save the first piece digest and initialize a new piece hasher
120                 self.pieceHash.append(self.fileHasher.digest())
121                 self.pieceHasher = self._new()
122
123             if self.pieceHasher:
124                 # Loop in case the data contains multiple pieces
125                 while self.pieceSize + len(data) > PIECE_SIZE:
126                     # Save the piece hash and start a new one
127                     self.pieceHasher.update(data[:(PIECE_SIZE - self.pieceSize)])
128                     self.pieceHash.append(self.pieceHasher.digest())
129                     self.pieceHasher = self._new()
130                     
131                     # Don't forget to hash the data normally
132                     self.fileHasher.update(data[:(PIECE_SIZE - self.pieceSize)])
133                     data = data[(PIECE_SIZE - self.pieceSize):]
134                     self.size += PIECE_SIZE - self.pieceSize
135                     self.pieceSize = 0
136
137                 # Hash any remaining data
138                 self.pieceHasher.update(data)
139                 self.pieceSize += len(data)
140             
141             self.fileHasher.update(data)
142             self.size += len(data)
143         
144     def hashInThread(self, file):
145         """Hashes a file in a separate thread, returning a deferred that will callback with the result."""
146         file.restat(False)
147         if not file.exists():
148             df = defer.Deferred()
149             df.errback(HashError("file not found"))
150             return df
151         
152         df = threads.deferToThread(self._hashInThread, file)
153         return df
154     
155     def _hashInThread(self, file):
156         """Hashes a file, returning itself as the result."""
157         f = file.open()
158         self.new(force = True)
159         data = f.read(4096)
160         while data:
161             self.update(data)
162             data = f.read(4096)
163         self.digest()
164         return self
165
166     #{ Checking hashes of data
167     def pieceDigests(self):
168         """Get the piece hashes of the added file data."""
169         self.digest()
170         return self.pieceHash
171
172     def digest(self):
173         """Get the hash of the added file data."""
174         if self.fileHash is None:
175             if self.fileHasher is None:
176                 raise HashError, "you must hash some data first"
177             self.fileHash = self.fileHasher.digest()
178             self.done = True
179             
180             # Save the last piece hash
181             if self.pieceHasher:
182                 self.pieceHash.append(self.pieceHasher.digest())
183         return self.fileHash
184
185     def hexdigest(self):
186         """Get the hash of the added file data in hex format."""
187         if self.fileHex is None:
188             self.fileHex = b2a_hex(self.digest())
189         return self.fileHex
190         
191     def verify(self):
192         """Verify that the added file data hash matches the expected hash."""
193         if self.result is None and self.fileHash is not None and self.expHash is not None:
194             self.result = (self.fileHash == self.expHash and self.size == self.expSize)
195         return self.result
196     
197     #{ Expected hash
198     def expected(self):
199         """Get the expected hash."""
200         return self.expHash
201     
202     def hexexpected(self):
203         """Get the expected hash in hex format."""
204         if self.expHex is None and self.expHash is not None:
205             self.expHex = b2a_hex(self.expHash)
206         return self.expHex
207     
208     #{ Setting the expected hash
209     def set(self, hashType, hashHex, size):
210         """Initialize the hash object.
211         
212         @param hashType: must be one of the dictionaries from L{ORDER}
213         """
214         self.hashTypeNum = self.ORDER.index(hashType)    # error if not found
215         self.expHex = hashHex
216         self.expSize = int(size)
217         self.expHash = a2b_hex(self.expHex)
218         
219     def setFromIndexRecord(self, record):
220         """Set the hash from the cache of index file records.
221         
222         @type record: C{dictionary}
223         @param record: keys are hash types, values are tuples of (hash, size)
224         """
225         for hashType in self.ORDER:
226             result = record.get(hashType['AptIndexRecord'], None)
227             if result:
228                 self.set(hashType, result[0], result[1])
229                 return True
230         return False
231
232     def setFromPkgRecord(self, record, size):
233         """Set the hash from Apt's binary packages cache.
234         
235         @param record: whatever is returned by apt_pkg.GetPkgRecords()
236         """
237         for hashType in self.ORDER:
238             hashHex = getattr(record, hashType['AptPkgRecord'], None)
239             if hashHex:
240                 self.set(hashType, hashHex, size)
241                 return True
242         return False
243     
244     def setFromSrcRecord(self, record):
245         """Set the hash from Apt's source package records cache.
246         
247         Currently very simple since Apt only tracks MD5 hashes of source files.
248         
249         @type record: (C{string}, C{int}, C{string})
250         @param record: the hash, size and path of the source file
251         """
252         for hashType in self.ORDER:
253             if hashType['AptSrcRecord']:
254                 self.set(hashType, record[0], record[1])
255                 return True
256         return False
257
258 class TestHashObject(unittest.TestCase):
259     """Unit tests for the hash objects."""
260     
261     timeout = 5
262     if sys.version_info < (2, 4):
263         skip = "skippingme"
264     
265     def test_failure(self):
266         """Tests that the hash object fails when treated badly."""
267         h = HashObject()
268         h.set(h.ORDER[0], b2a_hex('12345678901234567890'), '0')
269         self.failUnlessRaises(HashError, h.digest)
270         self.failUnlessRaises(HashError, h.hexdigest)
271         self.failUnlessRaises(HashError, h.update, 'gfgf')
272     
273     def test_pieces(self):
274         """Tests the hashing of large files into pieces."""
275         h = HashObject()
276         h.new()
277         h.update('1234567890'*120*1024)
278         self.failUnless(h.digest() == '1(j\xd2q\x0b\n\x91\xd2\x13\x90\x15\xa3E\xcc\xb0\x8d.\xc3\xc5')
279         pieces = h.pieceDigests()
280         self.failUnless(len(pieces) == 3)
281         self.failUnless(pieces[0] == ',G \xd8\xbbPl\xf1\xa3\xa0\x0cW\n\xe6\xe6a\xc9\x95/\xe5')
282         self.failUnless(pieces[1] == '\xf6V\xeb/\xa8\xad[\x07Z\xf9\x87\xa4\xf5w\xdf\xe1|\x00\x8e\x93')
283         self.failUnless(pieces[2] == 'M[\xbf\xee\xaa+\x19\xbaV\xf699\r\x17o\xcb\x8e\xcfP\x19')
284         h.new(True)
285         for i in xrange(120*1024):
286             h.update('1234567890')
287         pieces = h.pieceDigests()
288         self.failUnless(h.digest() == '1(j\xd2q\x0b\n\x91\xd2\x13\x90\x15\xa3E\xcc\xb0\x8d.\xc3\xc5')
289         self.failUnless(len(pieces) == 3)
290         self.failUnless(pieces[0] == ',G \xd8\xbbPl\xf1\xa3\xa0\x0cW\n\xe6\xe6a\xc9\x95/\xe5')
291         self.failUnless(pieces[1] == '\xf6V\xeb/\xa8\xad[\x07Z\xf9\x87\xa4\xf5w\xdf\xe1|\x00\x8e\x93')
292         self.failUnless(pieces[2] == 'M[\xbf\xee\xaa+\x19\xbaV\xf699\r\x17o\xcb\x8e\xcfP\x19')
293         
294     def test_sha1(self):
295         """Test hashing using the SHA1 hash."""
296         h = HashObject()
297         found = False
298         for hashType in h.ORDER:
299             if hashType['name'] == 'sha1':
300                 found = True
301                 break
302         self.failUnless(found == True)
303         h.set(hashType, '3bba0a5d97b7946ad2632002bf9caefe2cb18e00', '19')
304         h.new()
305         h.update('apt-p2p is the best')
306         self.failUnless(h.hexdigest() == '3bba0a5d97b7946ad2632002bf9caefe2cb18e00')
307         self.failUnlessRaises(HashError, h.update, 'gfgf')
308         self.failUnless(h.verify() == True)
309         
310     def test_md5(self):
311         """Test hashing using the MD5 hash."""
312         h = HashObject()
313         found = False
314         for hashType in h.ORDER:
315             if hashType['name'] == 'md5':
316                 found = True
317                 break
318         self.failUnless(found == True)
319         h.set(hashType, '6b5abdd30d7ed80edd229f9071d8c23c', '19')
320         h.new()
321         h.update('apt-p2p is the best')
322         self.failUnless(h.hexdigest() == '6b5abdd30d7ed80edd229f9071d8c23c')
323         self.failUnlessRaises(HashError, h.update, 'gfgf')
324         self.failUnless(h.verify() == True)
325         
326     def test_sha256(self):
327         """Test hashing using the SHA256 hash."""
328         h = HashObject()
329         found = False
330         for hashType in h.ORDER:
331             if hashType['name'] == 'sha256':
332                 found = True
333                 break
334         self.failUnless(found == True)
335         h.set(hashType, '47f2238a30a0340faa2bf01a9bdc42ba77b07b411cda1e24cd8d7b5c4b7d82a7', '19')
336         h.new()
337         h.update('apt-p2p is the best')
338         self.failUnless(h.hexdigest() == '47f2238a30a0340faa2bf01a9bdc42ba77b07b411cda1e24cd8d7b5c4b7d82a7')
339         self.failUnlessRaises(HashError, h.update, 'gfgf')
340         self.failUnless(h.verify() == True)
341
342     if sys.version_info < (2, 5):
343         test_sha256.skip = "SHA256 hashes are not supported by Python until version 2.5"