~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_weave.py

Adding sha1 check when weave extracts a text.

Show diffs side-by-side

added added

removed removed

Lines of Context:
288
288
                'last line',
289
289
                ('}', 0),
290
290
                ]
 
291
        k._sha1s = [sha_string('first lineline to be deletedlast line')
 
292
                  , sha_string('first linelast line')]
291
293
 
292
294
        self.assertEqual(k.get(0),
293
295
                         ['first line',
321
323
                'last line',
322
324
                ('}', 0),
323
325
                ]
 
326
        k._sha1s = [sha_string('first lineline to be deletedlast line')
 
327
                  , sha_string('first linereplacement linelast line')]
324
328
 
325
329
        self.assertEqual(k.get(0),
326
330
                         ['first line',
421
425
                '}',
422
426
                ('}', 0)]
423
427
 
 
428
        k._sha1s = [sha_string('foo {}')
 
429
                  , sha_string('foo {  added in version 1  also from v1}')
 
430
                  , sha_string('foo {  added in v2}')
 
431
                  , sha_string('foo {  added in version 1  added in v2  also from v1}')
 
432
                  ]
 
433
 
424
434
        self.assertEqual(k.get(0),
425
435
                         ['foo {',
426
436
                          '}'])
494
504
                "second line",
495
505
                ('}', 1)]
496
506
 
 
507
        k._sha1s = [sha_string('first line')
 
508
                  , sha_string('first linesecond line')]
 
509
 
497
510
        self.assertEqual(k.get(1),
498
511
                         ["first line",
499
512
                          "second line"])
523
536
                ('}', 2),                
524
537
                ]
525
538
 
 
539
        k._sha1s = [sha_string('first line')
 
540
                  , sha_string('first linesecond line')
 
541
                  , sha_string('first linealternative second line')]
 
542
 
526
543
        self.assertEqual(k.get(0),
527
544
                         ["first line"])
528
545
 
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')
 
901
 
 
902
 
 
903
 
 
904
class Corruption(TestCase):
 
905
 
 
906
    def test_detection(self):
 
907
        """Test weaves detect corruption.
 
908
 
 
909
        Weaves contain a checksum of their texts.
 
910
        When a text is extracted, this checksum should be
 
911
        verified.
 
912
        """
 
913
 
 
914
        w = Weave()
 
915
        w.add('v1', [], ['hello\n'])
 
916
        w.add('v2', ['v1'], ['hello\n', 'there\n'])
 
917
 
 
918
        # We are going to invasively corrupt the text
 
919
        # Make sure the internals of weave are the same
 
920
        self.assertEqual([('{', 0)
 
921
                        , 'hello\n'
 
922
                        , ('}', None)
 
923
                        , ('{', 1)
 
924
                        , 'there\n'
 
925
                        , ('}', None)
 
926
                        ], w._weave)
 
927
 
 
928
        self.assertEqual(['f572d396fae9206628714fb2ce00f72e94f2258f'
 
929
                        , '90f265c6e75f1c8f9ab76dcf85528352c5f215ef'
 
930
                        ], w._sha1s)
 
931
        # Corrupted
 
932
        w._weave[4] = 'There\n'
 
933
 
 
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'))
 
938
 
 
939
        # Corrected
 
940
        w._weave[4] = 'there\n'
 
941
        self.assertEqual('hello\nthere\n', w.get_text('v2'))
 
942
 
 
943
        #Invalid checksum, first digit changed
 
944
        w._sha1s[1] =  'f0f265c6e75f1c8f9ab76dcf85528352c5f215ef'
 
945
 
 
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'))
 
950
 
 
951
    def test_written_detection(self):
 
952
        """Test detection of weave file corruption.
 
953
 
 
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.
 
957
        """
 
958
        from cStringIO import StringIO
 
959
 
 
960
        w = Weave()
 
961
        w.add('v1', [], ['hello\n'])
 
962
        w.add('v2', ['v1'], ['hello\n', 'there\n'])
 
963
 
 
964
        tmpf = StringIO()
 
965
        write_weave(w, tmpf)
 
966
 
 
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',
 
972
                          tmpf.getvalue())
 
973
 
 
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')
 
979
 
 
980
        w = read_weave(tmpf)
 
981
 
 
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'))
 
986
 
 
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')
 
992
 
 
993
        w = read_weave(tmpf)
 
994
 
 
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'))
 
999
 
 
1000