[tor-commits] [stem/master] Parsing geoip-db-digest lines in extrainfo descriptors

atagar at torproject.org atagar at torproject.org
Mon May 14 00:14:27 UTC 2012


commit 15a25f0b05eacfff21309715a4838d4ccd6f11f9
Author: Damian Johnson <atagar at torproject.org>
Date:   Thu May 10 08:31:26 2012 -0700

    Parsing geoip-db-digest lines in extrainfo descriptors
---
 stem/descriptor/extrainfo_descriptor.py      |    7 ++++++
 test/unit/descriptor/extrainfo_descriptor.py |   27 ++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/stem/descriptor/extrainfo_descriptor.py b/stem/descriptor/extrainfo_descriptor.py
index b0d7864..d2ee780 100644
--- a/stem/descriptor/extrainfo_descriptor.py
+++ b/stem/descriptor/extrainfo_descriptor.py
@@ -283,6 +283,13 @@ class ExtraInfoDescriptor(stem.descriptor.Descriptor):
         except ValueError:
           if validate:
             raise ValueError("Published line's time wasn't parseable: %s" % line)
+      elif keyword == "geoip-db-digest":
+        # "geoip-db-digest" Digest
+        
+        if validate and not re.match("^[0-9a-fA-F]{40}$", value):
+          raise ValueError("Geoip digest line had an invalid sha1 digest: %s" % line)
+        
+        self.geoip_db_digest = value
       elif keyword in ("read-history", "write-history", "dirreq-read-history", "dirreq-write-history"):
         try:
           timestamp, interval, remainder = _parse_timestamp_and_interval(keyword, value)
diff --git a/test/unit/descriptor/extrainfo_descriptor.py b/test/unit/descriptor/extrainfo_descriptor.py
index 15ffe68..5a4a450 100644
--- a/test/unit/descriptor/extrainfo_descriptor.py
+++ b/test/unit/descriptor/extrainfo_descriptor.py
@@ -108,6 +108,33 @@ class TestExtraInfoDescriptor(unittest.TestCase):
       self.assertEquals(None, desc.nickname)
       self.assertEquals(None, desc.fingerprint)
   
+  def test_geoip_db_digest(self):
+    """
+    Parses a geoip-db-digest line with valid data.
+    """
+    
+    geoip_db_digest = "916A3CA8B7DF61473D5AE5B21711F35F301CE9E8"
+    desc_text = _make_descriptor({"geoip-db-digest": geoip_db_digest})
+    desc = ExtraInfoDescriptor(desc_text)
+    self.assertEquals(geoip_db_digest, desc.geoip_db_digest)
+  
+  def test_geoip_db_digest_invalid(self):
+    """
+    Parses the geoip-db-digest line with a variety of bad input.
+    """
+    
+    test_entry = (
+      "",
+      "916A3CA8B7DF61473D5AE5B21711F35F301CE9E",
+      "916A3CA8B7DF61473D5AE5B21711F35F301CE9E88",
+      "916A3CA8B7DF61473D5AE5B21711F35F301CE9EG",
+      "916A3CA8B7DF61473D5AE5B21711F35F301CE9E-",
+    )
+    
+    for entry in test_entry:
+      desc_text = _make_descriptor({"geoip-db-digest": entry})
+      desc = self._expect_invalid_attr(desc_text, "geoip_db_digest", entry)
+  
   def _expect_invalid_attr(self, desc_text, attr = None, expected_value = None):
     """
     Asserts that construction will fail due to desc_text having a malformed





More information about the tor-commits mailing list