Rev 2602: Make validate detect node reference parsing errors. in http://people.ubuntu.com/~robertc/baz2.0/repository

Robert Collins robertc at robertcollins.net
Thu Jul 12 16:47:35 BST 2007


At http://people.ubuntu.com/~robertc/baz2.0/repository

------------------------------------------------------------
revno: 2602
revision-id: robertc at robertcollins.net-20070712154729-ln0fpixap5lkcnsj
parent: robertc at robertcollins.net-20070712144809-3cnuwhg0k86uo3qq
committer: Robert Collins <robertc at robertcollins.net>
branch nick: repository
timestamp: Fri 2007-07-13 01:47:29 +1000
message:
  Make validate detect node reference parsing errors.
modified:
  bzrlib/errors.py               errors.py-20050309040759-20512168c4e14fbd
  bzrlib/index.py                index.py-20070712131115-lolkarso50vjr64s-1
  bzrlib/tests/test_errors.py    test_errors.py-20060210110251-41aba2deddf936a8
  bzrlib/tests/test_index.py     test_index.py-20070712131115-lolkarso50vjr64s-2
=== modified file 'bzrlib/errors.py'
--- a/bzrlib/errors.py	2007-07-12 14:05:34 +0000
+++ b/bzrlib/errors.py	2007-07-12 15:47:29 +0000
@@ -353,6 +353,15 @@
         self._type = _type
 
 
+class BadIndexOptions(BzrError):
+
+    _fmt = "Could not parse options for index %(value)s."
+
+    def __init__(self, value):
+        BzrError.__init__(self)
+        self.value = value
+
+
 class BadOptionValue(BzrError):
 
     _fmt = """Bad value "%(value)s" for option "%(name)s"."""

=== modified file 'bzrlib/index.py'
--- a/bzrlib/index.py	2007-07-12 14:44:10 +0000
+++ b/bzrlib/index.py	2007-07-12 15:47:29 +0000
@@ -44,7 +44,11 @@
 
 class GraphIndex(object):
     """An index for data with embedded graphs.
-    
+ 
+    The index maps keys to a list of key reference lists, and a value.
+    Each node has the same number of key reference lists. Each key reference
+    list can be empty or an arbitrary length. The value is an opaque NULL
+    terminated string.
     """
 
     def __init__(self, transport, name):
@@ -89,3 +93,10 @@
         signature = stream.read(len(self._signature()))
         if not signature == self._signature():
             raise errors.BadIndexFormatSignature(self._name, GraphIndex)
+        options_line = stream.readline()
+        if not options_line.startswith(_OPTION_NODE_REFS):
+            raise errors.BadIndexOptions(self)
+        try:
+            node_ref_lists = int(options_line[len(_OPTION_NODE_REFS):-1])
+        except ValueError:
+            raise errors.BadIndexOptions(self)

=== modified file 'bzrlib/tests/test_errors.py'
--- a/bzrlib/tests/test_errors.py	2007-07-12 14:05:34 +0000
+++ b/bzrlib/tests/test_errors.py	2007-07-12 15:47:29 +0000
@@ -188,6 +188,11 @@
         self.assertEqual("foo is not an index of type bar.",
             str(error))
 
+    def test_bad_index_options(self):
+        error = errors.BadIndexOptions("foo")
+        self.assertEqual("Could not parse options for index foo.",
+            str(error))
+
     def test_bzrnewerror_is_deprecated(self):
         class DeprecatedError(errors.BzrNewError):
             pass

=== modified file 'bzrlib/tests/test_index.py'
--- a/bzrlib/tests/test_index.py	2007-07-12 14:48:09 +0000
+++ b/bzrlib/tests/test_index.py	2007-07-12 15:47:29 +0000
@@ -35,10 +35,16 @@
         contents = stream.read()
         self.assertEqual("Bazaar Graph Index 1\nnode_ref_lists=1\n\n", contents)
 
+    def test_build_index_two_reference_list_empty(self):
+        builder = GraphIndexBuilder(reference_lists=2)
+        stream = builder.finish()
+        contents = stream.read()
+        self.assertEqual("Bazaar Graph Index 1\nnode_ref_lists=2\n\n", contents)
+
 
 class TestGraphIndex(TestCaseWithMemoryTransport):
 
-    def make_index(self):
+    def make_index(self, ref_lists=0):
         builder = GraphIndexBuilder()
         stream = builder.finish()
         trans = self.get_transport()
@@ -68,6 +74,15 @@
         index = GraphIndex(trans, 'name')
         self.assertRaises(errors.BadIndexFormatSignature, index.validate)
 
+    def test_validate_bad_node_refs(self):
+        index = self.make_index(2)
+        trans = self.get_transport()
+        content = trans.get_bytes('index')
+        # change the options line to end with a rather than a parseable number
+        new_content = content[:-2] + 'a\n\n'
+        trans.put_bytes('index', new_content)
+        self.assertRaises(errors.BadIndexOptions, index.validate)
+
     def test_validate_empty(self):
         index = self.make_index()
         index.validate()




More information about the bazaar-commits mailing list