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

[tor-commits] [stem/master] Support for NS events



commit 92af0a5c0f9c6924cfddfb6388f581f3b6014732
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Dec 2 16:16:20 2012 -0800

    Support for NS events
    
    Implementation and testing for NS events. To get test data I wiped my
    cached_consensus, then listened for NS events while bootstraping. The first
    time I did this it provided a flood of entries (probably the whole consensus),
    but then when I restarted tor and tried again it just proved one.
---
 stem/control.py              |    1 +
 stem/response/events.py      |   22 ++++++++++++++++++++++
 test/unit/response/events.py |   20 ++++++++++++++++++++
 3 files changed, 43 insertions(+), 0 deletions(-)

diff --git a/stem/control.py b/stem/control.py
index c2c014d..5fd69c7 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -89,6 +89,7 @@ providing its own for interacting at a higher level.
   **INFO**              :class:`stem.response.events.LogEvent`
   **NEWDESC**           :class:`stem.response.events.NewDescEvent`
   **NOTICE**            :class:`stem.response.events.LogEvent`
+  **NS**                :class:`stem.response.events.NetworkStatusEvent`
   **ORCONN**            :class:`stem.response.events.ORConnEvent`
   **STATUS_CLIENT**     :class:`stem.response.events.StatusEvent`
   **STATUS_GENERAL**    :class:`stem.response.events.StatusEvent`
diff --git a/stem/response/events.py b/stem/response/events.py
index bca9557..1bdfd02 100644
--- a/stem/response/events.py
+++ b/stem/response/events.py
@@ -1,9 +1,11 @@
 import re
 import datetime
+import StringIO
 
 import stem
 import stem.control
 import stem.response
+import stem.descriptor.router_status_entry
 
 from stem.util import connection, log, str_tools, tor_tools
 
@@ -360,6 +362,25 @@ class LogEvent(Event):
       log_id = "event.logging.unknown_runlevel.%s" % self.runlevel
       log.log_once(log_id, log.INFO, unrecognized_msg % ('runlevel', self.runlevel))
 
+class NetworkStatusEvent(Event):
+  """
+  Event for when our copy of the consensus has changed. This was introduced in
+  tor version 0.1.2.3.
+  
+  :param list desc: :class:`~stem.descriptor.router_status_entry.RouterStatusEntryV3` for the changed descriptors
+  """
+  
+  _SKIP_PARSING = True
+  
+  def _parse(self):
+    content = str(self).lstrip("NS\n")
+    
+    self.desc = list(stem.descriptor.router_status_entry.parse_file(
+      StringIO.StringIO(content),
+      True,
+      entry_class = stem.descriptor.router_status_entry.RouterStatusEntryV3,
+    ))
+
 class NewDescEvent(Event):
   """
   Event that indicates that a new descriptor is available.
@@ -567,6 +588,7 @@ EVENT_TYPE_TO_CLASS = {
   "INFO": LogEvent,
   "NEWDESC": NewDescEvent,
   "NOTICE": LogEvent,
+  "NS": NetworkStatusEvent,
   "ORCONN": ORConnEvent,
   "STATUS_CLIENT": StatusEvent,
   "STATUS_GENERAL": StatusEvent,
diff --git a/test/unit/response/events.py b/test/unit/response/events.py
index 2a64a27..84a1e7b 100644
--- a/test/unit/response/events.py
+++ b/test/unit/response/events.py
@@ -47,6 +47,15 @@ GUARD_NEW = "650 GUARD ENTRY $36B5DBA788246E8369DBAF58577C6BC044A9A374 NEW"
 GUARD_GOOD = "650 GUARD ENTRY $5D0034A368E0ABAF663D21847E1C9B6CFA09752A GOOD"
 GUARD_BAD = "650 GUARD ENTRY $5D0034A368E0ABAF663D21847E1C9B6CFA09752A BAD"
 
+# NS event from tor v0.2.1.30.
+
+NS_EVENT = """650+NS
+r whnetz dbBxYcJriTTrcxsuy4PUZcMRwCA VStM7KAIH/mXXoGDUpoGB1OXufg 2012-12-02 21:03:56 141.70.120.13 9001 9030
+s Fast HSDir Named Stable V2Dir Valid
+.
+650 OK
+"""
+
 # STATUS_* events that I was able to easily trigger. Most came from starting
 # TBB, then listening while it bootstrapped.
 
@@ -380,6 +389,17 @@ class TestEvents(unittest.TestCase):
     self.assertEqual(NEWDESC_MULTIPLE.lstrip("650 "), str(event))
     self.assertEqual(expected_relays, event.relays)
   
+  def test_ns_event(self):
+    expected_desc = mocking.get_router_status_entry_v3({
+      "r": "whnetz dbBxYcJriTTrcxsuy4PUZcMRwCA VStM7KAIH/mXXoGDUpoGB1OXufg 2012-12-02 21:03:56 141.70.120.13 9001 9030",
+      "s": "Fast HSDir Named Stable V2Dir Valid",
+    })
+    
+    event = _get_event(NS_EVENT)
+    
+    self.assertTrue(isinstance(event, stem.response.events.NetworkStatusEvent))
+    self.assertEqual([expected_desc], event.desc)
+  
   def test_orconn_event(self):
     event = _get_event(ORCONN_CONNECTED)
     



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