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

[tor-commits] [stem/master] Minor formatting changes for DNSEL addition



commit d7833f31c392623f8dad5a30e7b16713cff510aa
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Thu Aug 22 09:20:20 2013 -0700

    Minor formatting changes for DNSEL addition
    
    Nothing functional, just rearranging a few things to satisfy my OCD. :)
---
 stem/descriptor/tordnsel.py      |   33 +++++++++-------------
 test/unit/descriptor/tordnsel.py |   58 ++++++++++++++++++++++----------------
 2 files changed, 47 insertions(+), 44 deletions(-)

diff --git a/stem/descriptor/tordnsel.py b/stem/descriptor/tordnsel.py
index ddeef2e..6e6b36d 100644
--- a/stem/descriptor/tordnsel.py
+++ b/stem/descriptor/tordnsel.py
@@ -1,8 +1,9 @@
-# Copyright 2012-2013, Damian Johnson
+# Copyright 2013, Damian Johnson
 # See LICENSE for licensing information
 
 """
-Parsing for TorDNSEL files.
+Parsing for `TorDNSEL <https://www.torproject.org/projects/tordnsel.html.en>`_
+exit list files.
 """
 
 import datetime
@@ -36,6 +37,7 @@ def _parse_file(tordnsel_file, validate = True, **kwargs):
   while True:
     contents = _read_until_keywords("ExitAddress", tordnsel_file)
     contents += _read_until_keywords("ExitNode", tordnsel_file)
+
     if contents:
       yield TorDNSEL(bytes.join(b"", contents), validate, **kwargs)
     else:
@@ -79,42 +81,35 @@ class TorDNSEL(Descriptor):
       if keyword == "ExitNode":
         if validate and not stem.util.tor_tools.is_valid_fingerprint(value):
           raise ValueError("Tor relay fingerprints consist of forty hex digits: %s" % value)
-        self.fingerprint = value
 
+        self.fingerprint = value
       elif keyword == "Published":
         try:
           self.published = datetime.datetime.strptime(value, "%Y-%m-%d %H:%M:%S")
         except ValueError:
           if validate:
             raise ValueError("Published time wasn't parsable: %s" % value)
-
       elif keyword == "LastStatus":
         try:
           self.last_status = datetime.datetime.strptime(value, "%Y-%m-%d %H:%M:%S")
         except ValueError:
           if validate:
             raise ValueError("LastStatus time wasn't parsable: %s" % value)
-
       elif keyword == "ExitAddress":
         for value, block_content in values:
-
-          if validate and block_content:
-            raise ValueError("Unexpected block content: %s" % block_content)
-
           address, date = value.split(" ", 1)
 
-          if validate and not stem.util.connection.is_valid_ipv4_address(address):
-            raise ValueError("ExitAddress isn't a valid IPv4 address: %s" % address)
+          if validate:
+            if not stem.util.connection.is_valid_ipv4_address(address):
+              raise ValueError("ExitAddress isn't a valid IPv4 address: %s" % address)
+            elif block_content:
+              raise ValueError("Unexpected block content: %s" % block_content)
+
           try:
             date = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
+            self.exit_addresses.append((address, date))
           except ValueError:
             if validate:
               raise ValueError("ExitAddress found time wasn't parsable: %s" % value)
-            else:
-              continue
-
-          self.exit_addresses.append((address, date))
-
-      else:
-        if validate:
-          raise ValueError("Saw a keyword that wasn't expected.")
+      elif validate:
+        raise ValueError("Unrecognized keyword: %s" % keyword)
diff --git a/test/unit/descriptor/tordnsel.py b/test/unit/descriptor/tordnsel.py
index 4861b38..4944a4b 100644
--- a/test/unit/descriptor/tordnsel.py
+++ b/test/unit/descriptor/tordnsel.py
@@ -9,13 +9,7 @@ import datetime
 from stem.util.tor_tools import is_valid_fingerprint
 from stem.descriptor.tordnsel import TorDNSEL, _parse_file
 
-
-class TestTorDNSELDescriptor(unittest.TestCase):
-  def test_parse_file(self):
-    """
-    Try parsing a document via the _parse_file() function.
-    """
-    desc_text = """
+TEST_DESC = """\
 @type tordnsel 1.0
 Downloaded 2013-08-19 04:02:03
 ExitNode 003A71137D959748C8157C4A76ECA639CEF5E33E
@@ -34,8 +28,31 @@ LastStatus 2013-08-18 13:02:57
 ExitAddress 46.10.211.205 2013-08-18 13:18:48
 """
 
+MALFORMED_ENTRY_1 = """\
+ExitNode 030B22437D99B2DB2908B747B6962EAD13AB4038
+Published Today!
+LastStatus 2013-08-18 13:02:57
+ExitAddress 46.10.211.205 2013-08-18 13:18:48
+"""
+
+MALFORMED_ENTRY_2 = """\
+@type tordnsel 1.0
+ExitNode 030B22437D99B2DB2908B747B6962EAD13AB4038
+Published Today!
+LastStatus 2013-08-18 13:02:57
+ExitAddress 46.10.211.205 2013-08-18 Never
+"""
+
+
+class TestTorDNSELDescriptor(unittest.TestCase):
+  def test_parse_file(self):
+    """
+    Try parsing a document via the _parse_file() function.
+    """
+
     # parse file and assert values
-    descriptors = list(_parse_file(io.BytesIO(desc_text)))
+
+    descriptors = list(_parse_file(io.BytesIO(TEST_DESC)))
     self.assertEqual(3, len(descriptors))
     self.assertTrue(isinstance(descriptors[0], TorDNSEL))
     desc = descriptors[1]
@@ -49,32 +66,23 @@ ExitAddress 46.10.211.205 2013-08-18 13:18:48
     self.assertEqual(datetime.datetime(2013, 8, 18, 8, 3, 1), exit[1])
 
     # block content raises value error
+
     extra = "ExtraContent goes here\n"
-    descriptors = _parse_file(io.BytesIO(desc_text + extra))
+    descriptors = _parse_file(io.BytesIO(TEST_DESC + extra))
     self.assertRaises(ValueError, list, descriptors)
 
     # malformed fingerprint raises value errors
+
     extra = "ExitNode 030B22437D99B2DB2908B747B6"
-    self.assertRaises(ValueError, list, _parse_file(io.BytesIO(desc_text + extra)))
+    self.assertRaises(ValueError, list, _parse_file(io.BytesIO(TEST_DESC + extra)))
 
     # malformed date raises value errors
-    extra = """
-ExitNode 030B22437D99B2DB2908B747B6962EAD13AB4038
-Published Today!
-LastStatus 2013-08-18 13:02:57
-ExitAddress 46.10.211.205 2013-08-18 13:18:48
-"""
-    self.assertRaises(ValueError, list, _parse_file(io.BytesIO(desc_text + extra)))
+
+    self.assertRaises(ValueError, list, _parse_file(io.BytesIO(TEST_DESC + MALFORMED_ENTRY_1)))
 
     # skip exit address if malformed date and validate is False
-    extra = """
-@type tordnsel 1.0
-ExitNode 030B22437D99B2DB2908B747B6962EAD13AB4038
-Published Today!
-LastStatus 2013-08-18 13:02:57
-ExitAddress 46.10.211.205 2013-08-18 Never
-"""
-    desc = _parse_file(io.BytesIO(extra), validate=False).next()
+    
+    desc = _parse_file(io.BytesIO(MALFORMED_ENTRY_2), validate=False).next()
     self.assertTrue(is_valid_fingerprint(desc.fingerprint))
     self.assertEqual("030B22437D99B2DB2908B747B6962EAD13AB4038", desc.fingerprint)
     self.assertEqual(0, len(desc.exit_addresses))

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