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

[tor-commits] [stem/master] Unit test for minimal RouterStatusEntry



commit be0c484de0be3cfa98a32c9ef5210b3e1d6eb6ee
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Mon Aug 20 22:53:17 2012 -0700

    Unit test for minimal RouterStatusEntry
    
    As usual, starting the class unit testing by parsing a minimal example. Next
    gonna go through field by field to exercise interesting use cases.
---
 stem/descriptor/networkstatus.py       |   16 ++++----
 test/integ/descriptor/networkstatus.py |    8 ++--
 test/unit/descriptor/networkstatus.py  |   72 +++++++++++++++++++++++++++++--
 3 files changed, 79 insertions(+), 17 deletions(-)

diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py
index 5382c0f..96f10ce 100644
--- a/stem/descriptor/networkstatus.py
+++ b/stem/descriptor/networkstatus.py
@@ -441,7 +441,7 @@ class RouterStatusEntry(stem.descriptor.Descriptor):
   :var str nickname: **\*** router's nickname
   :var str fingerprint: **\*** router's fingerprint
   :var str digest: **\*** router's digest
-  :var datetime publication: **\*** router's publication
+  :var datetime published: **\*** router's publication
   :var str address: **\*** router's IP address
   :var int or_port: **\*** router's ORPort
   :var int dir_port: **\*** router's DirPort
@@ -479,7 +479,7 @@ class RouterStatusEntry(stem.descriptor.Descriptor):
     self.nickname = None
     self.fingerprint = None
     self.digest = None
-    self.publication = None
+    self.published = None
     self.address = None
     self.or_port = None
     self.dir_port = None
@@ -495,7 +495,7 @@ class RouterStatusEntry(stem.descriptor.Descriptor):
     
     self.exit_policy = None
     self.microdescriptor_hashes = None
-    self.unrecognized_lines = []
+    self._unrecognized_lines = []
     
     self._parse(raw_contents, validate)
   
@@ -556,7 +556,7 @@ class RouterStatusEntry(stem.descriptor.Descriptor):
         
         try:
           published = "%s %s" % (r_comp[3], r_comp[4])
-          self.publication = datetime.datetime.strptime(published, "%Y-%m-%d %H:%M:%S")
+          self.published = datetime.datetime.strptime(published, "%Y-%m-%d %H:%M:%S")
         except ValueError:
           if validate:
             raise ValueError("Publication time time wasn't parseable: %s" % line)
@@ -655,7 +655,7 @@ class RouterStatusEntry(stem.descriptor.Descriptor):
         
         self.microdescriptor_hashes.append((methods, hashes))
       else:
-        self.unrecognized_lines.append(line)
+        self._unrecognized_lines.append(line)
   
   def get_unrecognized_lines(self):
     """
@@ -664,7 +664,7 @@ class RouterStatusEntry(stem.descriptor.Descriptor):
     :returns: list of unrecognized lines
     """
     
-    return self.unrecognized_lines
+    return list(self._unrecognized_lines)
 
 class MicrodescriptorConsensus(NetworkStatusDocument):
   """
@@ -706,7 +706,7 @@ class RouterMicrodescriptor(RouterStatusEntry):
   
   :var str nickname: **\*** router's nickname
   :var str fingerprint: **\*** router's fingerprint
-  :var datetime publication: **\*** router's publication
+  :var datetime published: **\*** router's publication
   :var str ip: **\*** router's IP address
   :var int or_port: **\*** router's ORPort
   :var int dir_port: **\*** router's DirPort
@@ -758,7 +758,7 @@ class RouterMicrodescriptor(RouterStatusEntry):
       seen_keywords.add("r")
       values = r.split(" ")
       self.nickname, self.fingerprint = values[0], _decode_fingerprint(values[1], validate)
-      self.publication = _strptime(" ".join((values[2], values[3])), validate)
+      self.published = _strptime(" ".join((values[2], values[3])), validate)
       self.ip, self.or_port, self.dir_port = values[4], int(values[5]), int(values[6])
       if self.dir_port == 0: self.dir_port = None
     elif validate: raise ValueError("Invalid router descriptor: empty 'r' line")
diff --git a/test/integ/descriptor/networkstatus.py b/test/integ/descriptor/networkstatus.py
index 4c626e9..c21b4a2 100644
--- a/test/integ/descriptor/networkstatus.py
+++ b/test/integ/descriptor/networkstatus.py
@@ -68,7 +68,7 @@ class TestNetworkStatus(unittest.TestCase):
       self.assertEquals("sumkledi", router.nickname)
       self.assertEquals("0013D22389CD50D0B784A3E4061CB31E8CE8CEB5", router.fingerprint)
       self.assertEquals("8mCr8Sl7RF4ENU4jb0FZFA/3do8", router.digest)
-      self.assertEquals(_strptime("2012-07-12 04:01:55"), router.publication)
+      self.assertEquals(_strptime("2012-07-12 04:01:55"), router.published)
       self.assertEquals("178.218.213.229", router.address)
       self.assertEquals(80, router.or_port)
       self.assertEquals(None, router.dir_port)
@@ -113,7 +113,7 @@ class TestNetworkStatus(unittest.TestCase):
     self.assertEquals("sumkledi", router1.nickname)
     self.assertEquals("0013D22389CD50D0B784A3E4061CB31E8CE8CEB5", router1.fingerprint)
     self.assertEquals("8mCr8Sl7RF4ENU4jb0FZFA/3do8", router1.digest)
-    self.assertEquals(_strptime("2012-07-12 04:01:55"), router1.publication)
+    self.assertEquals(_strptime("2012-07-12 04:01:55"), router1.published)
     self.assertEquals("178.218.213.229", router1.address)
     self.assertEquals(80, router1.or_port)
     self.assertEquals(None, router1.dir_port)
@@ -161,7 +161,7 @@ I/TJmV928na7RLZe2mGHCAW3VQOvV+QkCfj05VZ8CsY=
       self.assertEquals("sumkledi", router.nickname)
       self.assertEquals("0013D22389CD50D0B784A3E4061CB31E8CE8CEB5", router.fingerprint)
       self.assertEquals("B5n4BiALAF8B5AqafxohyYiuj7E", router.digest)
-      self.assertEquals(_strptime("2012-07-11 04:22:53"), router.publication)
+      self.assertEquals(_strptime("2012-07-11 04:22:53"), router.published)
       self.assertEquals("178.218.213.229", router.address)
       self.assertEquals(80, router.or_port)
       self.assertEquals(None, router.dir_port)
@@ -198,7 +198,7 @@ I/TJmV928na7RLZe2mGHCAW3VQOvV+QkCfj05VZ8CsY=
     self.assertEquals("sumkledi", router1.nickname)
     self.assertEquals("0013D22389CD50D0B784A3E4061CB31E8CE8CEB5", router1.fingerprint)
     self.assertEquals("B5n4BiALAF8B5AqafxohyYiuj7E", router1.digest)
-    self.assertEquals(_strptime("2012-07-11 04:22:53"), router1.publication)
+    self.assertEquals(_strptime("2012-07-11 04:22:53"), router1.published)
     self.assertEquals("178.218.213.229", router1.address)
     self.assertEquals(80, router1.or_port)
     self.assertEquals(None, router1.dir_port)
diff --git a/test/unit/descriptor/networkstatus.py b/test/unit/descriptor/networkstatus.py
index f14c2be..e6a8514 100644
--- a/test/unit/descriptor/networkstatus.py
+++ b/test/unit/descriptor/networkstatus.py
@@ -2,9 +2,44 @@
 Unit tests for stem.descriptor.networkstatus.
 """
 
+import datetime
 import unittest
 
-from stem.descriptor import networkstatus
+from stem.descriptor.networkstatus import Flag, RouterStatusEntry, _decode_fingerprint
+
+ROUTER_STATUS_ENTRY_ATTR = (
+  ("r", "caerSidi p1aag7VwarGxqctS7/fS0y5FU+s oQZFLYe9e4A7bOkWKR7TaNxb0JE 2012-08-06 11:19:31 71.35.150.29 9001 0"),
+  ("s", "Fast Named Running Stable Valid"),
+)
+
+def get_router_status_entry(attr = None, exclude = None):
+  """
+  Constructs a minimal router status entry with the given attributes.
+  
+  :param dict attr: keyword/value mappings to be included in the entry
+  :param list exclude: mandatory keywords to exclude from the entry
+  
+  :returns: str with customized router status entry content
+  """
+  
+  descriptor_lines = []
+  if attr is None: attr = {}
+  if exclude is None: exclude = []
+  attr = dict(attr) # shallow copy since we're destructive
+  
+  for keyword, value in ROUTER_STATUS_ENTRY_ATTR:
+    if keyword in exclude: continue
+    elif keyword in attr:
+      value = attr[keyword]
+      del attr[keyword]
+    
+    descriptor_lines.append("%s %s" % (keyword, value))
+  
+  # dump in any unused attributes
+  for attr_keyword, attr_value in attr.items():
+    descriptor_lines.append("%s %s" % (attr_keyword, attr_value))
+  
+  return "\n".join(descriptor_lines)
 
 class TestNetworkStatus(unittest.TestCase):
   def test_fingerprint_decoding(self):
@@ -23,9 +58,36 @@ class TestNetworkStatus(unittest.TestCase):
     }
     
     for arg, expected in test_values.items():
-      self.assertEqual(expected, networkstatus._decode_fingerprint(arg))
+      self.assertEqual(expected, _decode_fingerprint(arg, True))
+    
+    # checks with some malformed inputs
+    for arg in ('', '20wYcb', '20wYcb' * 30):
+      self.assertRaises(ValueError, _decode_fingerprint, arg, True)
+      self.assertEqual(None, _decode_fingerprint(arg, False))
+  
+  def test_rse_minimal(self):
+    """
+    Parses a minimal router status entry.
+    """
+    
+    entry = RouterStatusEntry(get_router_status_entry(), None)
     
-    self.assertRaises(ValueError, networkstatus._decode_fingerprint, '')
-    self.assertRaises(ValueError, networkstatus._decode_fingerprint, '20wYcb')
-    self.assertRaises(ValueError, networkstatus._decode_fingerprint, '20wYcb' * 30)
+    expected_flags = set([Flag.FAST, Flag.NAMED, Flag.RUNNING, Flag.STABLE, Flag.VALID])
+    self.assertEqual(None, entry.document)
+    self.assertEqual("caerSidi", entry.nickname)
+    self.assertEqual("A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB", entry.fingerprint)
+    self.assertEqual("oQZFLYe9e4A7bOkWKR7TaNxb0JE", entry.digest)
+    self.assertEqual(datetime.datetime(2012, 8, 6, 11, 19, 31), entry.published)
+    self.assertEqual("71.35.150.29", entry.address)
+    self.assertEqual(9001, entry.or_port)
+    self.assertEqual(None, entry.dir_port)
+    self.assertEqual(expected_flags, set(entry.flags))
+    self.assertEqual(None, entry.version_line)
+    self.assertEqual(None, entry.version)
+    self.assertEqual(None, entry.bandwidth)
+    self.assertEqual(None, entry.measured)
+    self.assertEqual([], entry.unrecognized_bandwidth_entries)
+    self.assertEqual(None, entry.exit_policy)
+    self.assertEqual(None, entry.microdescriptor_hashes)
+    self.assertEqual([], entry.get_unrecognized_lines())
 



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