Sfoglia il codice sorgente

pack: Extract a method to check pack length and SHA.

Change-Id: I1ce460d3d2a4f1b897e3c1af1c93812ebb216a4f
Dave Borowitz 13 anni fa
parent
commit
ad5748d951
3 ha cambiato i file con 18 aggiunte e 8 eliminazioni
  1. 2 0
      NEWS
  2. 10 6
      dulwich/pack.py
  3. 6 2
      dulwich/tests/test_pack.py

+ 2 - 0
NEWS

@@ -46,6 +46,8 @@
 
   * Move PackStreamReader from server to pack. (Dave Borowitz)
 
+  * Extract a check_length_and_checksum function. (Dave Borowitz)
+
  TEST CHANGES
 
   * If setuptools is installed, "python setup.py test" will now run the testsuite.

+ 10 - 6
dulwich/pack.py

@@ -1661,12 +1661,7 @@ class Pack(object):
         if self._data is None:
             self._data = self._data_load()
             self._data.pack = self
-            assert len(self.index) == len(self._data)
-            idx_stored_checksum = self.index.get_pack_checksum()
-            data_stored_checksum = self._data.get_stored_checksum()
-            if idx_stored_checksum != data_stored_checksum:
-                raise ChecksumMismatch(sha_to_hex(idx_stored_checksum),
-                                       sha_to_hex(data_stored_checksum))
+            self.check_length_and_checksum()
         return self._data
 
     @property
@@ -1698,6 +1693,15 @@ class Pack(object):
         """Iterate over all the sha1s of the objects in this pack."""
         return iter(self.index)
 
+    def check_length_and_checksum(self):
+        """Sanity check the length and checksum of the pack index and data."""
+        assert len(self.index) == len(self.data)
+        idx_stored_checksum = self.index.get_pack_checksum()
+        data_stored_checksum = self.data.get_stored_checksum()
+        if idx_stored_checksum != data_stored_checksum:
+            raise ChecksumMismatch(sha_to_hex(idx_stored_checksum),
+                                   sha_to_hex(data_stored_checksum))
+
     def check(self):
         """Check the integrity of this pack.
 

+ 6 - 2
dulwich/tests/test_pack.py

@@ -381,7 +381,7 @@ class TestPack(PackTests):
     def test_length_mismatch(self):
         data = self.get_pack_data(pack1_sha)
         index = self.get_pack_index(pack1_sha)
-        self.assertTrue(Pack.from_objects(data, index).data)
+        Pack.from_objects(data, index).check_length_and_checksum()
 
         data._file.seek(12)
         bad_file = StringIO()
@@ -391,17 +391,21 @@ class TestPack(PackTests):
         bad_data = PackData('', file=bad_file)
         bad_pack = Pack.from_lazy_objects(lambda: bad_data, lambda: index)
         self.assertRaises(AssertionError, lambda: bad_pack.data)
+        self.assertRaises(AssertionError,
+                          lambda: bad_pack.check_length_and_checksum())
 
     def test_checksum_mismatch(self):
         data = self.get_pack_data(pack1_sha)
         index = self.get_pack_index(pack1_sha)
-        self.assertTrue(Pack.from_objects(data, index).data)
+        Pack.from_objects(data, index).check_length_and_checksum()
 
         data._file.seek(0)
         bad_file = StringIO(data._file.read()[:-20] + ('\xff' * 20))
         bad_data = PackData('', file=bad_file)
         bad_pack = Pack.from_lazy_objects(lambda: bad_data, lambda: index)
         self.assertRaises(ChecksumMismatch, lambda: bad_pack.data)
+        self.assertRaises(ChecksumMismatch, lambda:
+                          bad_pack.check_length_and_checksum())
 
 
 class WritePackTests(TestCase):