Rev 38: A bit more defensive parsing in the from_bytes() method. in lp:~jameinel/+junk/file_locking

John Arbash Meinel john at arbash-meinel.com
Wed Sep 23 18:31:49 BST 2009


At lp:~jameinel/+junk/file_locking

------------------------------------------------------------
revno: 38
revision-id: john at arbash-meinel.com-20090923173135-qyvkjer4i63yatfm
parent: john at arbash-meinel.com-20090923042855-oi2y3cqj5yc3oay0
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: file_locking
timestamp: Wed 2009-09-23 12:31:35 -0500
message:
  A bit more defensive parsing in the from_bytes() method.
-------------- next part --------------
=== modified file 'file_lock.py'
--- a/file_lock.py	2009-09-23 04:28:55 +0000
+++ b/file_lock.py	2009-09-23 17:31:35 +0000
@@ -157,11 +157,15 @@
         num_active_locks = int(active_locks_stanza['active_locks'])
         active_locks = {}
         for lock_num in xrange(num_active_locks):
-            # TODO: Handle having a repeated file-id
             stanza = rio.read_stanza(as_file)
-            # TODO: Test this
-            # if stanza is None:
-            #     raise ValueError
+            if stanza is None:
+                raise ValueError('we were told there were %d active locks'
+                                 ', but we only read %d' % (num_active_locks,
+                                                            len(active_locks)))
+            file_id = stanza['file_id']
+            if file_id in active_locks:
+                raise ValueError('file_id %s was claimed to be in 2 active'
+                                 ' locks' % (file_id,))
             active_locks[stanza['file_id']] = stanza
         return FileLockingInfo(tracked_ids, active_locks, logger)
 

=== modified file 'tests/test_file_lock.py'
--- a/tests/test_file_lock.py	2009-09-23 04:28:55 +0000
+++ b/tests/test_file_lock.py	2009-09-23 17:31:35 +0000
@@ -78,6 +78,41 @@
         self.assertEqual('1253679708', info['time'])
         self.assertEqual('I Am <sam at iam.com>', info['user'])
 
+    def test_from_bytes_insufficient_active_locks(self):
+        s = file_lock.FileLockingInfoSerializer()
+        self.assertRaises(ValueError, s.from_bytes,
+                            '%s\n'
+                            't: file-id-1\n'
+                            't: file-id-2\n'
+                            '\n'
+                            'active_locks: 1\n'
+                            '\n' % (s._HEADER,))
+
+    def test_from_bytes_repeat_file_ids(self):
+        s = file_lock.FileLockingInfoSerializer()
+        self.assertRaises(ValueError, s.from_bytes,
+                            '%s\n'
+                            't: file-id-1\n'
+                            't: file-id-2\n'
+                            '\n'
+                            'active_locks: 2\n'
+                            '\n'
+                            'file_id: file-id-1\n'
+                            'host: localhost\n'
+                            'nonce: b80nt21jmniqbetst31l\n'
+                            'pid: 12345\n'
+                            'time: 1253679708\n'
+                            'user: I Am <sam at iam.com>\n'
+                            '\n'
+                            'file_id: file-id-1\n'
+                            'host: localhost\n'
+                            'nonce: b80nt21jmniqbetst31l\n'
+                            'pid: 12345\n'
+                            'time: 1253679708\n'
+                            'user: I Am <sam at iam.com>\n'
+                            '\n' % (s._HEADER,))
+                    
+
     def test_to_bytes_trivial(self):
         li = file_lock.FileLockingInfo([], {})
         s = file_lock.FileLockingInfoSerializer()



More information about the bazaar-commits mailing list