commit c1e88e4257c606593ecd2abf4212cc5266b34bf4 Author: Damian Johnson atagar@torproject.org Date: Sun Dec 2 18:24:54 2012 -0800
Support for NEWCONSENSUS events
Implementation and testing for NEWCONSENSUS events. This is pretty much the exact same thing from our perspective as NS events. --- stem/response/events.py | 24 ++++++++++++++++++++++++ test/unit/response/events.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 0 deletions(-)
diff --git a/stem/response/events.py b/stem/response/events.py index 49a8169..522f9cc 100644 --- a/stem/response/events.py +++ b/stem/response/events.py @@ -438,6 +438,29 @@ class NetworkStatusEvent(Event): entry_class = stem.descriptor.router_status_entry.RouterStatusEntryV3, ))
+class NewConsensusEvent(Event): + """ + Event for when we have a new consensus. This is similar to + :class:`~stem.response.events.NetworkStatusEvent`, except that it contains + the whole consensus so anything not listed is implicitly no longer + recommended. + + This was introduced in tor version 0.2.1.13. + + :param list desc: :class:`~stem.descriptor.router_status_entry.RouterStatusEntryV3` for the changed descriptors + """ + + _SKIP_PARSING = True + + def _parse(self): + content = str(self).lstrip("NEWCONSENSUS\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. @@ -644,6 +667,7 @@ EVENT_TYPE_TO_CLASS = { "ERR": LogEvent, "GUARD": GuardEvent, "INFO": LogEvent, + "NEWCONSENSUS": NewConsensusEvent, "NEWDESC": NewDescEvent, "NOTICE": LogEvent, "NS": NetworkStatusEvent, diff --git a/test/unit/response/events.py b/test/unit/response/events.py index 0b225d9..6f18676 100644 --- a/test/unit/response/events.py +++ b/test/unit/response/events.py @@ -54,6 +54,17 @@ GUARD_NEW = "650 GUARD ENTRY $36B5DBA788246E8369DBAF58577C6BC044A9A374 NEW" GUARD_GOOD = "650 GUARD ENTRY $5D0034A368E0ABAF663D21847E1C9B6CFA09752A GOOD" GUARD_BAD = "650 GUARD ENTRY $5D0034A368E0ABAF663D21847E1C9B6CFA09752A BAD"
+# NEWCONSENSUS event from v0.2.1.30. + +NEWCONSENSUS_EVENT = """650+NEWCONSENSUS +r Beaver /96bKo4soysolMgKn5Hex2nyFSY pAJH9dSBp/CG6sPhhVY/5bLaVPM 2012-12-02 22:02:45 77.223.43.54 9001 0 +s Fast Named Running Stable Valid +r Unnamed /+fJRWjmIGNAL2C5rRZHq3R91tA 7AnpZjfdBpYzXnMNm+w1bTsFF6Y 2012-12-02 17:51:10 91.121.184.87 9001 0 +s Fast Guard Running Stable Valid +. +650 OK +""" + # NS event from tor v0.2.1.30.
NS_EVENT = """650+NS @@ -405,6 +416,24 @@ class TestEvents(unittest.TestCase): self.assertEqual(NEWDESC_MULTIPLE.lstrip("650 "), str(event)) self.assertEqual(expected_relays, event.relays)
+ def test_new_consensus_event(self): + expected_desc = [] + + expected_desc.append(mocking.get_router_status_entry_v3({ + "r": "Beaver /96bKo4soysolMgKn5Hex2nyFSY pAJH9dSBp/CG6sPhhVY/5bLaVPM 2012-12-02 22:02:45 77.223.43.54 9001 0", + "s": "Fast Named Running Stable Valid", + })) + + expected_desc.append(mocking.get_router_status_entry_v3({ + "r": "Unnamed /+fJRWjmIGNAL2C5rRZHq3R91tA 7AnpZjfdBpYzXnMNm+w1bTsFF6Y 2012-12-02 17:51:10 91.121.184.87 9001 0", + "s": "Fast Guard Running Stable Valid", + })) + + event = _get_event(NEWCONSENSUS_EVENT) + + self.assertTrue(isinstance(event, stem.response.events.NewConsensusEvent)) + self.assertEqual(expected_desc, event.desc) + 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",