[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Parsing tor version list attributes



commit fd0deb96f30fc691019e0d2a09068a4787d80e97
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sat Sep 8 19:11:13 2012 -0700

    Parsing tor version list attributes
    
    Handling the 'client-versions' and 'server-versions' lines, comma separated
    lists of tor versions.
---
 stem/descriptor/networkstatus.py               |   27 +++++++++++++++--------
 test/unit/descriptor/networkstatus/document.py |   24 +++++++++++++++++++++
 2 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py
index 2b30249..eca3b71 100644
--- a/stem/descriptor/networkstatus.py
+++ b/stem/descriptor/networkstatus.py
@@ -208,9 +208,9 @@ class NetworkStatusDocument(stem.descriptor.Descriptor):
   :var datetime fresh_until: **\*** time until when the consensus is considered to be fresh
   :var datetime valid_until: **\*** time until when the consensus is valid
   :var int vote_delay: **\*** number of seconds allowed for collecting votes from all authorities
-  :var int dist_delay: number of seconds allowed for collecting signatures from all authorities
-  :var list client_versions: list of recommended Tor client versions
-  :var list server_versions: list of recommended Tor server versions
+  :var int dist_delay: **\*** number of seconds allowed for collecting signatures from all authorities
+  :var list client_versions: list of recommended client tor versions
+  :var list server_versions: list of recommended server tor versions
   :var list known_flags: **\*** list of known router flags
   :var list params: dict of parameter(str) => value(int) mappings
   :var list directory_authorities: **\*** list of DirectoryAuthority objects that have generated this document
@@ -371,6 +371,18 @@ class NetworkStatusDocument(stem.descriptor.Descriptor):
           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)
+      elif keyword in ("client-versions", "server-versions"):
+        for entry in value.split(","):
+          try:
+            version_value = stem.version.Version(entry)
+            
+            if keyword == 'client-versions':
+              self.client_versions.append(version_value)
+            elif keyword == 'server-versions':
+              self.server_versions.append(version_value)
+          except ValueError:
+            if validate:
+              raise ValueError("Network status document's '%s' line had '%s', which isn't a parseable tor version: %s" % (keyword, entry, line))
     
     # doing this validation afterward so we know our 'is_consensus' and
     # 'is_vote' attributes
@@ -394,16 +406,11 @@ class NetworkStatusDocument(stem.descriptor.Descriptor):
     _read_keyword_line("fresh-until", content, False, True)
     _read_keyword_line("valid-until", content, False, True)
     _read_keyword_line("voting-delay", content, False, True)
+    _read_keyword_line("client-versions", content, False, True)
+    _read_keyword_line("server-versions", content, False, True)
     
     vote = self.is_vote
     
-    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(",")]
-    server_versions = _read_keyword_line("server-versions", content, validate, True)
-    if server_versions:
-      self.server_versions = [stem.version.Version(version_string) for version_string in server_versions.split(",")]
-    
     flags_content = _read_keyword_line("known-flags", content, validate)
     
     if flags_content:
diff --git a/test/unit/descriptor/networkstatus/document.py b/test/unit/descriptor/networkstatus/document.py
index c16e054..032c095 100644
--- a/test/unit/descriptor/networkstatus/document.py
+++ b/test/unit/descriptor/networkstatus/document.py
@@ -5,6 +5,7 @@ Unit tests for the NetworkStatusDocument of stem.descriptor.networkstatus.
 import datetime
 import unittest
 
+import stem.version
 from stem.descriptor.networkstatus import HEADER_STATUS_DOCUMENT_FIELDS, FOOTER_STATUS_DOCUMENT_FIELDS, Flag, NetworkStatusDocument, DirectorySignature
 
 NETWORK_STATUS_DOCUMENT_ATTR = {
@@ -330,4 +331,27 @@ class TestNetworkStatusDocument(unittest.TestCase):
       document = NetworkStatusDocument(content, False)
       self.assertEquals(None, document.vote_delay)
       self.assertEquals(None, document.dist_delay)
+  
+  def test_invalid_version_lists(self):
+    """
+    Parses invalid client-versions and server-versions fields. Both are comma
+    separated lists of tor versions.
+    """
+    
+    test_values = (
+      ("", []),
+      ("   ", []),
+      ("1.2.3.4,", [stem.version.Version("1.2.3.4")]),
+      ("1.2.3.4,1.2.3.a", [stem.version.Version("1.2.3.4")]),
+    )
+    
+    for field in ('client-versions', 'server-versions'):
+      attr = field.replace('-', '_')
+      
+      for test_value, expected_value in test_values:
+        content = get_network_status_document({field: test_value})
+        self.assertRaises(ValueError, NetworkStatusDocument, content)
+        
+        document = NetworkStatusDocument(content, False)
+        self.assertEquals(expected_value, getattr(document, attr))
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits