Merge lp:~aaron-whitehouse/duplicity/add-additional-verify-tests-for-corrupted-archives into lp:~duplicity-team/duplicity/0.7-series

Proposed by Aaron Whitehouse
Status: Merged
Merged at revision: 1039
Proposed branch: lp:~aaron-whitehouse/duplicity/add-additional-verify-tests-for-corrupted-archives
Merge into: lp:~duplicity-team/duplicity/0.7-series
Diff against target: 179 lines (+62/-13)
2 files modified
testing/functional/test_verify.py (+47/-9)
testing/verify_test.sh (+15/-4)
To merge this branch: bzr merge lp:~aaron-whitehouse/duplicity/add-additional-verify-tests-for-corrupted-archives
Reviewer Review Type Date Requested Status
duplicity-team Pending
Review via email: mp+245399@code.launchpad.net

Description of the change

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).

To post a comment you must log in.
1040. By Aaron Whitehouse <email address hidden>

Add comments to the verify_test.sh script matching up the tests in there to the tests implemented in test_verify.py. All the tests in the shell script have now been implemented, so the shell script should perhaps be deleted.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'testing/functional/test_verify.py'
2--- testing/functional/test_verify.py 2014-12-19 14:17:15 +0000
3+++ testing/functional/test_verify.py 2014-12-27 17:43:18 +0000
4@@ -37,12 +37,12 @@
5
6 def test_verify_changed_source_file(self):
7 """Test verify (without --compare-data) gives no error if a source file is changed"""
8- # TODO fix code to make this test pass (Bug #1354880)
9+ # This test was made to pass in fixing Bug #1354880
10 self.backup("full", "testfiles/various_file_types", options=[])
11
12 # Edit source file
13- f = open('testfiles/various_file_types/executable', 'w')
14- f.write('This changes a source file.')
15+ with open('testfiles/various_file_types/executable', 'r+') as f:
16+ f.write('This changes a source file.')
17
18 # Test verify for the file
19 self.verify('testfiles/various_file_types/executable', file_to_verify='executable', options=[])
20@@ -61,8 +61,8 @@
21 self.backup("full", "testfiles/various_file_types", options=[])
22
23 # Edit source file
24- f = open('testfiles/various_file_types/executable', 'w')
25- f.write('This changes a source file.')
26+ with open('testfiles/various_file_types/executable', 'r+') as f:
27+ f.write('This changes a source file.')
28
29 # Set the atime and mtime for the file back to what it was prior to the edit
30 os.utime('testfiles/various_file_types/executable', (file_info.st_atime, file_info.st_mtime))
31@@ -83,8 +83,8 @@
32 self.backup("full", "testfiles/various_file_types", options=[])
33
34 # Edit source file
35- f = open('testfiles/various_file_types/executable', 'w')
36- f.write('This changes a source file.')
37+ with open('testfiles/various_file_types/executable', 'r+') as f:
38+ f.write('This changes a source file.')
39
40 # Test verify for edited file fails with --compare-data
41 try:
42@@ -107,8 +107,8 @@
43
44 self.backup("full", "testfiles/various_file_types", options=[])
45 # Edit source file
46- f = open('testfiles/various_file_types/executable', 'w')
47- f.write('This changes a source file.')
48+ with open('testfiles/various_file_types/executable', 'r+') as f:
49+ f.write('This changes a source file.')
50
51 # Set the atime and mtime for the file back to what it was prior to the edit
52 os.utime('testfiles/various_file_types/executable', (file_info.st_atime, file_info.st_mtime))
53@@ -122,5 +122,43 @@
54 else:
55 self.fail('Expected CmdError not thrown')
56
57+
58+ def test_verify_corrupt_archive(self):
59+ """Test verify (without --compare-data) gives an error if the archive is corrupted"""
60+ self.backup("full", "testfiles/various_file_types", options=[])
61+ output_files = os.listdir("testfiles/output")
62+ archives = [elem for elem in output_files if "vol" in elem]
63+ for archive in archives:
64+ # Edit source file
65+ with open("testfiles/output/" + archive, 'r+') as f:
66+ f.write('This writes text into each archive file to corrupt it.')
67+ # Test verify for the file
68+ try:
69+ self.verify('testfiles/various_file_types/executable', file_to_verify='executable', options=[])
70+ except CmdError as e:
71+ # Should return a 21 error code for "hash mismatch"
72+ self.assertEqual(e.exit_status, 21, str(e))
73+ else:
74+ self.fail('Expected Hash Mismatch Error not thrown')
75+
76+ def test_verify_corrupt_archive_compare_data(self):
77+ """Test verify with --compare-data gives an error if the archive is corrupted"""
78+ self.backup("full", "testfiles/various_file_types", options=[])
79+ output_files = os.listdir("testfiles/output")
80+ archives = [elem for elem in output_files if "vol" in elem]
81+ for archive in archives:
82+ # Edit source file
83+ with open("testfiles/output/" + archive, 'r+') as f:
84+ f.write('This writes text into each archive file to corrupt it.')
85+ # Test verify for the file
86+ try:
87+ self.verify('testfiles/various_file_types/executable', file_to_verify='executable',
88+ options=["--compare-data"])
89+ except CmdError as e:
90+ # Should return a 21 error code for "hash mismatch"
91+ self.assertEqual(e.exit_status, 21, str(e))
92+ else:
93+ self.fail('Expected Hash Mismatch Error not thrown')
94+
95 if __name__ == "__main__":
96 unittest.main()
97
98=== modified file 'testing/verify_test.sh'
99--- testing/verify_test.sh 2014-12-12 15:29:23 +0000
100+++ testing/verify_test.sh 2014-12-27 17:43:18 +0000
101@@ -2,6 +2,8 @@
102 # Script to show the current behaviour of duplicity's verify action
103 # (c) 2014 Aaron Whitehouse <aaron@whitehouse.kiwi.nz>
104 # GPLv2 or any later version
105+# These tests have all now been implemented in the test suite (functional/test_verify.py)
106+# Perhaps this file should now be removed.
107 export PASSPHRASE=test
108
109 TESTDIR=/tmp/duplicity_test
110@@ -22,12 +24,14 @@
111 echo "This is a test" > $SOURCEDIR/test.txt
112 $DUPLICITY_CMD $SOURCEDIR file://$TARGETDIR
113 echo "-----------"
114-echo "Normal verify before we've done anything. This should pass and does"
115+echo "Normal verify before we've done anything. This should pass and does"
116 echo "on 0.6.23."
117+# Implemented in the test suite as functional/test_verify.py test_verify
118 $DUPLICITY_CMD verify file://$TARGETDIR $SOURCEDIR
119 echo "-----------"
120 echo "Change the source file after sleeping for a second."
121 # Sleep for a second, as otherwise the mtime can be the same even if it was edited after backup.
122+# Implemented in the test suite as functional/test_verify.py test_verify_changed_source_file
123 sleep 1
124 echo "This is changing the test file." > $SOURCEDIR/test.txt
125 echo "-----------"
126@@ -38,6 +42,7 @@
127 echo "check against the filesystem, this should fail and does on 0.6.23."
128 echo "Note that this should flag a difference in the Data as well, but does not"
129 echo "on 0.6.23, whereas it does so in the Second Test."
130+# Implemented in the test suite as functional/test_verify.py test_verify_compare_data_changed_source_file
131 $DUPLICITY_CMD verify --compare-data file://$TARGETDIR $SOURCEDIR
132
133 rm -r $TESTDIR
134@@ -57,7 +62,8 @@
135 $DUPLICITY_CMD $SOURCEDIR file://$TARGETDIR
136 echo "-----------"
137 echo "Normal verify before we've done anything. This should pass and does on 0.6.23."
138-$DUPLICITY_CMD verify file://$TARGETDIR $SOURCEDIR
139+# Implemented in the test suite as functional/test_verify.py test_verify
140+$DUPLICITY_CMD verify file://$TARGETDIR $SOURCEDIR
141 echo "-----------"
142 echo "Change the source file after sleeping for a second, but change the mtime to match the original."
143 # Sleep for a second, as otherwise the mtime can be the same even if it was edited after backup.
144@@ -69,11 +75,13 @@
145 touch --reference=$SOURCEDIR/test.txt $SOURCEDIR
146 echo "-----------"
147 echo "Verify again. This should pass and does on 0.6.23 (though only because it is 'tricked' by the mtime)."
148+# Implemented in the test suite as functional/test_verify.py test_verify test_verify_changed_source_file_adjust_mtime
149 $DUPLICITY_CMD verify file://$TARGETDIR $SOURCEDIR
150 echo "-----------"
151 echo "Verify again, but with --compare-data. As --compare-data is designed to"
152 echo "check against the filesystem, this should fail and currently does."
153 echo "Note that, unlike the First Test, on 0.6.23 this flags a difference in the Data."
154+# Implemented in the test suite as functional/test_verify.py test_verify_compare_data_changed_source_file_adjust_mtime
155 $DUPLICITY_CMD verify --compare-data file://$TARGETDIR $SOURCEDIR
156
157 rm -r $TESTDIR
158@@ -92,16 +100,19 @@
159 $DUPLICITY_CMD $SOURCEDIR file://$TARGETDIR
160 echo "-----------"
161 echo "Normal verify before we've done anything. This should pass and does on 0.6.23."
162-$DUPLICITY_CMD verify file://$TARGETDIR $SOURCEDIR
163+# Implemented in the test suite as functional/test_verify.py test_verify
164+$DUPLICITY_CMD verify file://$TARGETDIR $SOURCEDIR
165 echo "-----------"
166 echo "Corrupt the archive."
167-find $TARGETDIR -name duplicity-full*.vol* -exec dd if=/dev/urandom of='{}' bs=1024 seek=$((RANDOM%10+1)) count=1 conv=notrunc \;
168+find $TARGETDIR -name 'duplicity-full*.vol*' -exec dd if=/dev/urandom of='{}' bs=1024 seek=$((RANDOM%10+1)) count=1 conv=notrunc \;
169 echo "-----------"
170 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."
171+# Implemented in the test suite as functional/test_verify.py test_verify_corrupt_archive
172 $DUPLICITY_CMD verify file://$TARGETDIR $SOURCEDIR
173 echo "-----------"
174 echo "Verify again, but with --compare-data. This should also fail, as verify should check the integrity of the archive"
175 echo "in either mode, and it does on 0.6.23."
176+# Implemented in the test suite as functional/test_verify.py test_verify_corrupt_archive_compare_data
177 $DUPLICITY_CMD verify --compare-data file://$TARGETDIR $SOURCEDIR
178
179 rm -r $TESTDIR

Subscribers

People subscribed via source and target branches