← Back to team overview

dulwich-users team mailing list archive

[PATCH 21/33] pack: Extract a method to check pack length and SHA.

 

From: Dave Borowitz <dborowitz@xxxxxxxxxx>

Change-Id: I1ce460d3d2a4f1b897e3c1af1c93812ebb216a4f
---
 NEWS                       |    2 ++
 dulwich/pack.py            |   16 ++++++++++------
 dulwich/tests/test_pack.py |    8 ++++++--
 3 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/NEWS b/NEWS
index 4c3bc2b..17fb2f7 100644
--- a/NEWS
+++ b/NEWS
@@ -48,6 +48,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.
diff --git a/dulwich/pack.py b/dulwich/pack.py
index acdddcc..a0f10d4 100644
--- a/dulwich/pack.py
+++ b/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.
 
diff --git a/dulwich/tests/test_pack.py b/dulwich/tests/test_pack.py
index dcaffff..7ac04f6 100644
--- a/dulwich/tests/test_pack.py
+++ b/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):
-- 
1.7.3.1



References