← Back to team overview

duplicity-team team mailing list archive

[Merge] lp:~hooloovoo/duplicity/add-additional-verify-tests-for-corrupted-archives into lp:duplicity

 

Aaron Whitehouse has proposed merging lp:~hooloovoo/duplicity/add-additional-verify-tests-for-corrupted-archives into lp:duplicity.

Requested reviews:
  duplicity-team (duplicity-team)

For more details, see:
https://code.launchpad.net/~hooloovoo/duplicity/add-additional-verify-tests-for-corrupted-archives/+merge/245399

Add tests to test_verify.py to test that verify fails if the archive file is corrupted. Changed file objects to use the with keyword to ensure that the file is properly closed.
Small edit to find statement in verify_test.sh to make it work as expected (enclose string in quotes). 
-- 
Your team duplicity-team is requested to review the proposed merge of lp:~hooloovoo/duplicity/add-additional-verify-tests-for-corrupted-archives into lp:duplicity.
=== modified file 'testing/functional/test_verify.py'
--- testing/functional/test_verify.py	2014-12-19 14:17:15 +0000
+++ testing/functional/test_verify.py	2014-12-27 17:01:17 +0000
@@ -37,12 +37,12 @@
 
     def test_verify_changed_source_file(self):
         """Test verify (without --compare-data) gives no error if a source file is changed"""
-        # TODO fix code to make this test pass (Bug #1354880)
+        # This test was made to pass in fixing Bug #1354880
         self.backup("full", "testfiles/various_file_types", options=[])
 
         # Edit source file
-        f = open('testfiles/various_file_types/executable', 'w')
-        f.write('This changes a source file.')
+        with open('testfiles/various_file_types/executable', 'r+') as f:
+            f.write('This changes a source file.')
 
         # Test verify for the file
         self.verify('testfiles/various_file_types/executable', file_to_verify='executable', options=[])
@@ -61,8 +61,8 @@
         self.backup("full", "testfiles/various_file_types", options=[])
 
         # Edit source file
-        f = open('testfiles/various_file_types/executable', 'w')
-        f.write('This changes a source file.')
+        with open('testfiles/various_file_types/executable', 'r+') as f:
+            f.write('This changes a source file.')
 
         # Set the atime and mtime for the file back to what it was prior to the edit
         os.utime('testfiles/various_file_types/executable', (file_info.st_atime, file_info.st_mtime))
@@ -83,8 +83,8 @@
         self.backup("full", "testfiles/various_file_types", options=[])
 
         # Edit source file
-        f = open('testfiles/various_file_types/executable', 'w')
-        f.write('This changes a source file.')
+        with open('testfiles/various_file_types/executable', 'r+') as f:
+            f.write('This changes a source file.')
 
         # Test verify for edited file fails with --compare-data
         try:
@@ -107,8 +107,8 @@
 
         self.backup("full", "testfiles/various_file_types", options=[])
         # Edit source file
-        f = open('testfiles/various_file_types/executable', 'w')
-        f.write('This changes a source file.')
+        with open('testfiles/various_file_types/executable', 'r+') as f:
+            f.write('This changes a source file.')
 
         # Set the atime and mtime for the file back to what it was prior to the edit
         os.utime('testfiles/various_file_types/executable', (file_info.st_atime, file_info.st_mtime))
@@ -122,5 +122,43 @@
         else:
             self.fail('Expected CmdError not thrown')
 
+
+    def test_verify_corrupt_archive(self):
+        """Test verify (without --compare-data) gives an error if the archive is corrupted"""
+        self.backup("full", "testfiles/various_file_types", options=[])
+        output_files = os.listdir("testfiles/output")
+        archives = [elem for elem in output_files if "vol" in elem]
+        for archive in archives:
+            # Edit source file
+            with open("testfiles/output/" + archive, 'r+') as f:
+                f.write('This writes text into each archive file to corrupt it.')
+        # Test verify for the file
+        try:
+            self.verify('testfiles/various_file_types/executable', file_to_verify='executable', options=[])
+        except CmdError as e:
+            # Should return a 21 error code for "hash mismatch"
+            self.assertEqual(e.exit_status, 21, str(e))
+        else:
+            self.fail('Expected Hash Mismatch Error not thrown')
+
+    def test_verify_corrupt_archive_compare_data(self):
+        """Test verify with --compare-data gives an error if the archive is corrupted"""
+        self.backup("full", "testfiles/various_file_types", options=[])
+        output_files = os.listdir("testfiles/output")
+        archives = [elem for elem in output_files if "vol" in elem]
+        for archive in archives:
+            # Edit source file
+            with open("testfiles/output/" + archive, 'r+') as f:
+                f.write('This writes text into each archive file to corrupt it.')
+        # Test verify for the file
+        try:
+            self.verify('testfiles/various_file_types/executable', file_to_verify='executable',
+                        options=["--compare-data"])
+        except CmdError as e:
+            # Should return a 21 error code for "hash mismatch"
+            self.assertEqual(e.exit_status, 21, str(e))
+        else:
+            self.fail('Expected Hash Mismatch Error not thrown')
+
 if __name__ == "__main__":
     unittest.main()

=== modified file 'testing/verify_test.sh'
--- testing/verify_test.sh	2014-12-12 15:29:23 +0000
+++ testing/verify_test.sh	2014-12-27 17:01:17 +0000
@@ -95,7 +95,7 @@
 $DUPLICITY_CMD verify file://$TARGETDIR $SOURCEDIR 
 echo "-----------"
 echo "Corrupt the archive."
-find $TARGETDIR -name duplicity-full*.vol* -exec dd if=/dev/urandom of='{}' bs=1024 seek=$((RANDOM%10+1)) count=1 conv=notrunc \; 
+find $TARGETDIR -name 'duplicity-full*.vol*' -exec dd if=/dev/urandom of='{}' bs=1024 seek=$((RANDOM%10+1)) count=1 conv=notrunc \; 
 echo "-----------"
 echo "Verify again. This should fail, as verify should check the integrity of the archive in either mode, and it does on 0.6.23."
 $DUPLICITY_CMD verify file://$TARGETDIR $SOURCEDIR


Follow ups