[tor-commits] [stem/master] Parsing the voting-delay attribute

atagar at torproject.org atagar at torproject.org
Sat Oct 13 18:35:45 UTC 2012


commit fb808fde025a8c4f7f04d243f6e679f2f8b5e255
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Sep 8 18:52:58 2012 -0700

    Parsing the voting-delay attribute
    
    Nothing special here. Simply a pair of integer fields.
---
 stem/descriptor/networkstatus.py               |   16 +++++++++++-----
 test/unit/descriptor/networkstatus/document.py |   21 +++++++++++++++++++++
 2 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py
index 039222b..2b30249 100644
--- a/stem/descriptor/networkstatus.py
+++ b/stem/descriptor/networkstatus.py
@@ -361,6 +361,16 @@ class NetworkStatusDocument(stem.descriptor.Descriptor):
         except ValueError:
           if validate:
             raise ValueError("Network status document's '%s' time wasn't parseable: %s" % (keyword, value))
+      elif keyword == "voting-delay":
+        # "voting-delay" VoteSeconds DistSeconds
+        
+        value_comp = value.split(' ')
+        
+        if len(value_comp) == 2 and value_comp[0].isdigit() and value_comp[1].isdigit():
+          self.vote_delay = int(value_comp[0])
+          self.dist_delay = int(value_comp[1])
+        elif validate:
+          raise ValueError("A network status document's 'voting-delay' line must be a pair of integer values, but was '%s'" % value)
     
     # doing this validation afterward so we know our 'is_consensus' and
     # 'is_vote' attributes
@@ -383,14 +393,10 @@ class NetworkStatusDocument(stem.descriptor.Descriptor):
     _read_keyword_line("valid-after", content, False, True)
     _read_keyword_line("fresh-until", content, False, True)
     _read_keyword_line("valid-until", content, False, True)
+    _read_keyword_line("voting-delay", content, False, True)
     
     vote = self.is_vote
     
-    voting_delay = _read_keyword_line("voting-delay", content, validate)
-    
-    if voting_delay:
-      self.vote_delay, self.dist_delay = [int(delay) for delay in voting_delay.split(" ")]
-    
     client_versions = _read_keyword_line("client-versions", content, validate, True)
     if client_versions:
       self.client_versions = [stem.version.Version(version_string) for version_string in client_versions.split(",")]
diff --git a/test/unit/descriptor/networkstatus/document.py b/test/unit/descriptor/networkstatus/document.py
index 91796a3..c16e054 100644
--- a/test/unit/descriptor/networkstatus/document.py
+++ b/test/unit/descriptor/networkstatus/document.py
@@ -309,4 +309,25 @@ class TestNetworkStatusDocument(unittest.TestCase):
         
         document = NetworkStatusDocument(content, False)
         self.assertEquals(None, getattr(document, attr))
+  
+  def test_invalid_voting_delay(self):
+    """
+    Parses an invalid voting-delay field.
+    """
+    
+    test_values = (
+      "",
+      "   ",
+      "1 a",
+      "1\t2",
+      "1 2.0",
+    )
+    
+    for test_value in test_values:
+      content = get_network_status_document({"voting-delay": test_value})
+      self.assertRaises(ValueError, NetworkStatusDocument, content)
+      
+      document = NetworkStatusDocument(content, False)
+      self.assertEquals(None, document.vote_delay)
+      self.assertEquals(None, document.dist_delay)
 





More information about the tor-commits mailing list