881
898
eq = self.assertEquals
882
899
eq(sorted(wa.iter_names()), ['v1', 'v2', 'v3', 'x1',])
883
900
eq(wa.get_text('x1'), 'line from x1\n')
904
class Corruption(TestCase):
906
def test_detection(self):
907
"""Test weaves detect corruption.
909
Weaves contain a checksum of their texts.
910
When a text is extracted, this checksum should be
915
w.add('v1', [], ['hello\n'])
916
w.add('v2', ['v1'], ['hello\n', 'there\n'])
918
# We are going to invasively corrupt the text
919
# Make sure the internals of weave are the same
920
self.assertEqual([('{', 0)
928
self.assertEqual(['f572d396fae9206628714fb2ce00f72e94f2258f'
929
, '90f265c6e75f1c8f9ab76dcf85528352c5f215ef'
932
w._weave[4] = 'There\n'
934
self.assertEqual('hello\n', w.get_text('v1'))
935
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
936
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
937
self.assertRaises(errors.WeaveInvalidChecksum, list, w.get_iter('v2'))
940
w._weave[4] = 'there\n'
941
self.assertEqual('hello\nthere\n', w.get_text('v2'))
943
#Invalid checksum, first digit changed
944
w._sha1s[1] = 'f0f265c6e75f1c8f9ab76dcf85528352c5f215ef'
946
self.assertEqual('hello\n', w.get_text('v1'))
947
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
948
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
949
self.assertRaises(errors.WeaveInvalidChecksum, list, w.get_iter('v2'))
951
def test_written_detection(self):
952
"""Test detection of weave file corruption.
954
Make sure that we can detect if a weave file has
955
been corrupted. This doesn't test all forms of corruption,
956
but it at least helps verify the data you get, is what you want.
958
from cStringIO import StringIO
961
w.add('v1', [], ['hello\n'])
962
w.add('v2', ['v1'], ['hello\n', 'there\n'])
967
# Because we are corrupting, we need to make sure we have the exact text
968
self.assertEquals('# bzr weave file v5\n'
969
'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
970
'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
971
'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n',
974
# Change a single letter
975
tmpf = StringIO('# bzr weave file v5\n'
976
'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
977
'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
978
'w\n{ 0\n. hello\n}\n{ 1\n. There\n}\nW\n')
982
self.assertEqual('hello\n', w.get_text('v1'))
983
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
984
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
985
self.assertRaises(errors.WeaveInvalidChecksum, list, w.get_iter('v2'))
987
# Change the sha checksum
988
tmpf = StringIO('# bzr weave file v5\n'
989
'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
990
'i 0\n1 f0f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
991
'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n')
995
self.assertEqual('hello\n', w.get_text('v1'))
996
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
997
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
998
self.assertRaises(errors.WeaveInvalidChecksum, list, w.get_iter('v2'))