[tor-commits] [stem/master] Parsing AUTHDIR_NEWDESCS events

atagar at torproject.org atagar at torproject.org
Mon Dec 3 02:35:44 UTC 2012


commit 0f4e52bf20ae82a8951ed910639f0e4fa41aeac8
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Nov 23 19:54:58 2012 -0800

    Parsing AUTHDIR_NEWDESCS events
    
    The specification for the AUTHDIR_NEWDESCS event type was expanded in...
    https://gitweb.torproject.org/torspec.git/commitdiff/a1331ca
    
    We still don't have an example of the event so no tests, but it's enough to
    implement the parsing. It would be nice if the event type included a field for
    the descriptor type, but oh well.
---
 stem/response/events.py      |   21 ++++++++++++++++-----
 test/unit/response/events.py |    3 +--
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/stem/response/events.py b/stem/response/events.py
index c066097..e571c4e 100644
--- a/stem/response/events.py
+++ b/stem/response/events.py
@@ -171,16 +171,27 @@ class AddrMapEvent(Event):
 class AuthDirNewDescEvent(Event):
   """
   Event specific to directory authorities, indicating that we just received new
-  descriptors.
-  
-  **Unimplemented, waiting on 'https://trac.torproject.org/7534'.**
+  descriptors. The descriptor type contained within this event is unspecified
+  so the descriptor contents are left unparsed.
   
   :var stem.AuthDescriptorAction action: what is being done with the descriptor
-  :var str message: unknown
-  :var stem.descriptor.Descriptor descriptor: unknown
+  :var str message: explanation of why we chose this action
+  :var str descriptor: content of the descriptor
   """
   
   _SKIP_PARSING = True
+  
+  def _parse(self):
+    lines = str(self).split('\n')
+    
+    if len(lines) < 5:
+      raise stem.ProtocolError("AUTHDIR_NEWDESCS events must contain lines for at least the type, action, message, descriptor, and terminating 'OK'")
+    elif not lines[-1] == "OK":
+      raise stem.ProtocolError("AUTHDIR_NEWDESCS doesn't end with an 'OK'")
+    
+    self.action = lines[1]
+    self.message = lines[2]
+    self.descriptor = '\n'.join(lines[3:-1])
 
 class BandwidthEvent(Event):
   """
diff --git a/test/unit/response/events.py b/test/unit/response/events.py
index ef2699e..0a5921f 100644
--- a/test/unit/response/events.py
+++ b/test/unit/response/events.py
@@ -142,10 +142,9 @@ class TestEvents(unittest.TestCase):
     # TODO: We aren't actually parsing the event yet. Until then we can only
     # check that we properly get a AuthDirNewDescEvent for it.
     
-    event = _get_event("650 AUTHDIR_NEWDESCS")
+    event = _get_event("650+AUTHDIR_NEWDESCS\nAction\nMessage\nDescriptor\n.\n650 OK\n")
     
     self.assertTrue(isinstance(event, stem.response.events.AuthDirNewDescEvent))
-    self.assertEqual("AUTHDIR_NEWDESCS", str(event))
     self.assertEqual([], event.positional_args)
     self.assertEqual({}, event.keyword_args)
   





More information about the tor-commits mailing list