commit fb0aec5d95e9d2e6d64b7ab7c51018f3581565c5 Author: Damian Johnson atagar@torproject.org Date: Mon Dec 3 23:20:24 2012 -0800
Tidying up event testing boilerplate
Most event unit tests initially check two things...
* That the event class matches an expected value. * That the string content for the event matches what we were constructed with.
Blending these two checks with the function that provides us with events. --- test/unit/response/events.py | 302 +++++++++++++++++------------------------- 1 files changed, 119 insertions(+), 183 deletions(-)
diff --git a/test/unit/response/events.py b/test/unit/response/events.py index 58aebb4..6fbd907 100644 --- a/test/unit/response/events.py +++ b/test/unit/response/events.py @@ -199,11 +199,6 @@ $B4BE08B22D4D2923EDC3970FD1B93D0448C6D8FF~Unnamed" ADDRMAP = '650 ADDRMAP www.atagar.com 75.119.206.243 "2012-11-19 00:50:13" \ EXPIRES="2012-11-19 08:50:13"'
-def _get_event(content): - controller_event = mocking.get_message(content) - stem.response.convert("EVENT", controller_event, arrived_at = 25) - return controller_event - class TestEvents(unittest.TestCase): def test_example(self): """ @@ -219,7 +214,7 @@ class TestEvents(unittest.TestCase):
def event_sender(): for i in xrange(3): - print_bw(_get_event("650 BW 15 25")) + print_bw(self._get_event("650 BW 15 25")) time.sleep(0.05)
controller = mocking.get_object(Controller, { @@ -236,29 +231,24 @@ class TestEvents(unittest.TestCase): events_thread.join()
def test_log_events(self): - event = _get_event("650 DEBUG connection_edge_process_relay_cell(): Got an extended cell! Yay.") + event = self._get_event("650 DEBUG connection_edge_process_relay_cell(): Got an extended cell! Yay.", stem.response.events.LogEvent)
- self.assertTrue(isinstance(event, stem.response.events.LogEvent)) self.assertEqual("DEBUG", event.runlevel) self.assertEqual("connection_edge_process_relay_cell(): Got an extended cell! Yay.", event.message)
- event = _get_event("650 INFO circuit_finish_handshake(): Finished building circuit hop:") + event = self._get_event("650 INFO circuit_finish_handshake(): Finished building circuit hop:", stem.response.events.LogEvent)
- self.assertTrue(isinstance(event, stem.response.events.LogEvent)) self.assertEqual("INFO", event.runlevel) self.assertEqual("circuit_finish_handshake(): Finished building circuit hop:", event.message)
- event = _get_event("650+WARN\na multi-line\nwarning message\n.\n650 OK\n") + event = self._get_event("650+WARN\na multi-line\nwarning message\n.\n650 OK\n", stem.response.events.LogEvent, False)
- self.assertTrue(isinstance(event, stem.response.events.LogEvent)) self.assertEqual("WARN", event.runlevel) self.assertEqual("a multi-line\nwarning message", event.message)
def test_addrmap_event(self): - event = _get_event(ADDRMAP) + event = self._get_event(ADDRMAP, stem.response.events.AddrMapEvent)
- self.assertTrue(isinstance(event, stem.response.events.AddrMapEvent)) - self.assertEqual(ADDRMAP.lstrip("650 "), str(event)) self.assertEqual("www.atagar.com", event.hostname) self.assertEqual("75.119.206.243", event.destination) self.assertEqual(datetime.datetime(2012, 11, 19, 0, 50, 13), event.expiry) @@ -266,20 +256,16 @@ class TestEvents(unittest.TestCase): self.assertEqual(datetime.datetime(2012, 11, 19, 8, 50, 13), event.utc_expiry)
def test_authdir_newdesc_event(self): - # TODO: We aren't actually parsing the event yet. Until then we can only - # check that we properly get a AuthDirNewDescEvent for it. + # TODO: awaiting test data - https://trac.torproject.org/7534
- event = _get_event("650+AUTHDIR_NEWDESCS\nAction\nMessage\nDescriptor\n.\n650 OK\n") + event = self._get_event("650+AUTHDIR_NEWDESCS\nAction\nMessage\nDescriptor\n.\n650 OK\n", stem.response.events.AuthDirNewDescEvent, False)
- self.assertTrue(isinstance(event, stem.response.events.AuthDirNewDescEvent)) self.assertEqual([], event.positional_args) self.assertEqual({}, event.keyword_args)
def test_build_timeout_set_event(self): - event = _get_event(BUILD_TIMEOUT_EVENT) + event = self._get_event(BUILD_TIMEOUT_EVENT, stem.response.events.BuildTimeoutSetEvent)
- self.assertTrue(isinstance(event, stem.response.events.BuildTimeoutSetEvent)) - self.assertEqual(BUILD_TIMEOUT_EVENT.lstrip("650 "), str(event)) self.assertEqual(TimeoutSetType.COMPUTED, event.set_type) self.assertEqual(124, event.total_times) self.assertEqual(9019, event.timeout) @@ -291,13 +277,11 @@ class TestEvents(unittest.TestCase): self.assertEqual(0.072581, event.close_rate)
def test_bw_event(self): - event = _get_event("650 BW 15 25") - - self.assertTrue(isinstance(event, stem.response.events.BandwidthEvent)) + event = self._get_event("650 BW 15 25", stem.response.events.BandwidthEvent) self.assertEqual(15, event.read) self.assertEqual(25, event.written)
- event = _get_event("650 BW 0 0") + event = self._get_event("650 BW 0 0") self.assertEqual(0, event.read) self.assertEqual(0, event.written)
@@ -306,21 +290,19 @@ class TestEvents(unittest.TestCase): # since tor doesn't actually do it yet (and likely never will), but might # as well sanity test that it'll be ok.
- event = _get_event("650 BW 10 20 OR=5 EXIT=500") + event = self._get_event("650 BW 10 20 OR=5 EXIT=500") self.assertEqual(10, event.read) self.assertEqual(20, event.written) self.assertEqual({'OR': '5', 'EXIT': '500'}, event.keyword_args)
- self.assertRaises(ProtocolError, _get_event, "650 BW 15") - self.assertRaises(ProtocolError, _get_event, "650 BW -15 25") - self.assertRaises(ProtocolError, _get_event, "650 BW 15 -25") - self.assertRaises(ProtocolError, _get_event, "650 BW x 25") + self.assertRaises(ProtocolError, self._get_event, "650 BW 15") + self.assertRaises(ProtocolError, self._get_event, "650 BW -15 25") + self.assertRaises(ProtocolError, self._get_event, "650 BW 15 -25") + self.assertRaises(ProtocolError, self._get_event, "650 BW x 25")
def test_circ_event(self): - event = _get_event(CIRC_LAUNCHED) + event = self._get_event(CIRC_LAUNCHED, stem.response.events.CircuitEvent)
- self.assertTrue(isinstance(event, stem.response.events.CircuitEvent)) - self.assertEqual(CIRC_LAUNCHED.lstrip("650 "), str(event)) self.assertEqual("7", event.id) self.assertEqual(CircStatus.LAUNCHED, event.status) self.assertEqual((), event.path) @@ -332,10 +314,8 @@ class TestEvents(unittest.TestCase): self.assertEqual(None, event.reason) self.assertEqual(None, event.remote_reason)
- event = _get_event(CIRC_EXTENDED) + event = self._get_event(CIRC_EXTENDED, stem.response.events.CircuitEvent)
- self.assertTrue(isinstance(event, stem.response.events.CircuitEvent)) - self.assertEqual(CIRC_EXTENDED.lstrip("650 "), str(event)) self.assertEqual("7", event.id) self.assertEqual(CircStatus.EXTENDED, event.status) self.assertEqual((("999A226EBED397F331B612FE1E4CFAE5C1F201BA", "piyaz"),), event.path) @@ -347,10 +327,8 @@ class TestEvents(unittest.TestCase): self.assertEqual(None, event.reason) self.assertEqual(None, event.remote_reason)
- event = _get_event(CIRC_FAILED) + event = self._get_event(CIRC_FAILED, stem.response.events.CircuitEvent)
- self.assertTrue(isinstance(event, stem.response.events.CircuitEvent)) - self.assertEqual(CIRC_FAILED.lstrip("650 "), str(event)) self.assertEqual("5", event.id) self.assertEqual(CircStatus.FAILED, event.status) self.assertEqual((("E57A476CD4DFBD99B4EE52A100A58610AD6E80B9", "ergebnisoffen"),), event.path) @@ -362,10 +340,8 @@ class TestEvents(unittest.TestCase): self.assertEqual(CircClosureReason.DESTROYED, event.reason) self.assertEqual(CircClosureReason.OR_CONN_CLOSED, event.remote_reason)
- event = _get_event(CIRC_LAUNCHED_OLD) + event = self._get_event(CIRC_LAUNCHED_OLD, stem.response.events.CircuitEvent)
- self.assertTrue(isinstance(event, stem.response.events.CircuitEvent)) - self.assertEqual(CIRC_LAUNCHED_OLD.lstrip("650 "), str(event)) self.assertEqual("4", event.id) self.assertEqual(CircStatus.LAUNCHED, event.status) self.assertEqual((), event.path) @@ -377,10 +353,8 @@ class TestEvents(unittest.TestCase): self.assertEqual(None, event.reason) self.assertEqual(None, event.remote_reason)
- event = _get_event(CIRC_EXTENDED_OLD) + event = self._get_event(CIRC_EXTENDED_OLD, stem.response.events.CircuitEvent)
- self.assertTrue(isinstance(event, stem.response.events.CircuitEvent)) - self.assertEqual(CIRC_EXTENDED_OLD.lstrip("650 "), str(event)) self.assertEqual("1", event.id) self.assertEqual(CircStatus.EXTENDED, event.status) self.assertEqual((("E57A476CD4DFBD99B4EE52A100A58610AD6E80B9", None), (None,"hamburgerphone")), event.path) @@ -392,10 +366,8 @@ class TestEvents(unittest.TestCase): self.assertEqual(None, event.reason) self.assertEqual(None, event.remote_reason)
- event = _get_event(CIRC_BUILT_OLD) + event = self._get_event(CIRC_BUILT_OLD, stem.response.events.CircuitEvent)
- self.assertTrue(isinstance(event, stem.response.events.CircuitEvent)) - self.assertEqual(CIRC_BUILT_OLD.lstrip("650 "), str(event)) self.assertEqual("1", event.id) self.assertEqual(CircStatus.BUILT, event.status) self.assertEqual((("E57A476CD4DFBD99B4EE52A100A58610AD6E80B9", None), (None,"hamburgerphone"), (None, "PrivacyRepublic14")), event.path) @@ -408,19 +380,14 @@ class TestEvents(unittest.TestCase): self.assertEqual(None, event.remote_reason)
def test_clients_seen_event(self): - event = _get_event(CLIENTS_SEEN_EVENT) - - self.assertTrue(isinstance(event, stem.response.events.ClientsSeenEvent)) - self.assertEqual(CLIENTS_SEEN_EVENT.lstrip("650 "), str(event)) + event = self._get_event(CLIENTS_SEEN_EVENT, stem.response.events.ClientsSeenEvent) self.assertEqual(datetime.datetime(2008, 12, 25, 23, 50, 43), event.start_time) self.assertEqual({'us': 16, 'de': 8, 'uk': 8}, event.locales) self.assertEqual({'v4': 16, 'v6': 40}, event.ip_versions)
def test_circ_minor_event(self): - event = _get_event(CIRC_MINOR_EVENT) + event = self._get_event(CIRC_MINOR_EVENT, stem.response.events.CircMinorEvent)
- self.assertTrue(isinstance(event, stem.response.events.CircMinorEvent)) - self.assertEqual(CIRC_MINOR_EVENT.lstrip("650 "), str(event)) self.assertEqual("7", event.id) self.assertEqual(CircEvent.PURPOSE_CHANGED, event.event) self.assertEqual((("67B2BDA4264D8A189D9270E28B1D30A262838243", "europa1"),), event.path) @@ -433,79 +400,59 @@ class TestEvents(unittest.TestCase): self.assertEqual(None, event.old_hs_state)
def test_conf_changed(self): - event = _get_event(CONF_CHANGED_EVENT) - expected_config = { 'ExitNodes': 'caerSidi', 'MaxCircuitDirtiness': '20', 'ExitPolicy': None, }
- self.assertTrue(isinstance(event, stem.response.events.ConfChangedEvent)) + event = self._get_event(CONF_CHANGED_EVENT, stem.response.events.ConfChangedEvent, False) self.assertEqual(expected_config, event.config)
def test_descchanged_event(self): - # all we can check for is that the event is properly parsed as a - # DescChangedEvent instance - - event = _get_event("650 DESCCHANGED") - - self.assertTrue(isinstance(event, stem.response.events.DescChangedEvent)) - self.assertEqual("DESCCHANGED", str(event)) - self.assertEqual([], event.positional_args) - self.assertEqual({}, event.keyword_args) + self._get_event("650 DESCCHANGED", stem.response.events.DescChangedEvent)
def test_guard_event(self): - event = _get_event(GUARD_NEW) - - self.assertTrue(isinstance(event, stem.response.events.GuardEvent)) - self.assertEqual(GUARD_NEW.lstrip("650 "), str(event)) + event = self._get_event(GUARD_NEW, stem.response.events.GuardEvent) self.assertEqual(GuardType.ENTRY, event.guard_type) self.assertEqual("$36B5DBA788246E8369DBAF58577C6BC044A9A374", event.name) self.assertEqual(GuardStatus.NEW, event.status)
- event = _get_event(GUARD_GOOD) + event = self._get_event(GUARD_GOOD, stem.response.events.GuardEvent) self.assertEqual(GuardType.ENTRY, event.guard_type) self.assertEqual("$5D0034A368E0ABAF663D21847E1C9B6CFA09752A", event.name) self.assertEqual(GuardStatus.GOOD, event.status)
- event = _get_event(GUARD_BAD) + event = self._get_event(GUARD_BAD, stem.response.events.GuardEvent) self.assertEqual(GuardType.ENTRY, event.guard_type) self.assertEqual("$5D0034A368E0ABAF663D21847E1C9B6CFA09752A", event.name) self.assertEqual(GuardStatus.BAD, event.status)
def test_newdesc_event(self): - event = _get_event(NEWDESC_SINGLE) - expected_relays = (("B3FA3110CC6F42443F039220C134CBD2FC4F0493", "Sakura"),) + event = self._get_event(NEWDESC_SINGLE, stem.response.events.NewDescEvent)
- self.assertTrue(isinstance(event, stem.response.events.NewDescEvent)) - self.assertEqual(NEWDESC_SINGLE.lstrip("650 "), str(event)) + expected_relays = (("B3FA3110CC6F42443F039220C134CBD2FC4F0493", "Sakura"),) self.assertEqual(expected_relays, event.relays)
- event = _get_event(NEWDESC_MULTIPLE) expected_relays = (("BE938957B2CA5F804B3AFC2C1EE6673170CDBBF8", "Moonshine"), ("B4BE08B22D4D2923EDC3970FD1B93D0448C6D8FF", "Unnamed"))
- self.assertTrue(isinstance(event, stem.response.events.NewDescEvent)) - self.assertEqual(NEWDESC_MULTIPLE.lstrip("650 "), str(event)) + event = self._get_event(NEWDESC_MULTIPLE, stem.response.events.NewDescEvent) 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)) + expected_desc = [ + 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", + }), + 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 = self._get_event(NEWCONSENSUS_EVENT, stem.response.events.NewConsensusEvent, False) self.assertEqual(expected_desc, event.desc)
def test_ns_event(self): @@ -514,16 +461,12 @@ class TestEvents(unittest.TestCase): "s": "Fast HSDir Named Stable V2Dir Valid", })
- event = _get_event(NS_EVENT) - - self.assertTrue(isinstance(event, stem.response.events.NetworkStatusEvent)) + event = self._get_event(NS_EVENT, stem.response.events.NetworkStatusEvent, False) self.assertEqual([expected_desc], event.desc)
def test_orconn_event(self): - event = _get_event(ORCONN_CONNECTED) + event = self._get_event(ORCONN_CONNECTED, stem.response.events.ORConnEvent)
- self.assertTrue(isinstance(event, stem.response.events.ORConnEvent)) - self.assertEqual(ORCONN_CONNECTED.lstrip("650 "), str(event)) self.assertEqual("$7ED90E2833EE38A75795BA9237B0A4560E51E1A0=GreenDragon", event.endpoint) self.assertEqual("7ED90E2833EE38A75795BA9237B0A4560E51E1A0", event.endpoint_fingerprint) self.assertEqual("GreenDragon", event.endpoint_nickname) @@ -533,10 +476,8 @@ class TestEvents(unittest.TestCase): self.assertEqual(None, event.reason) self.assertEqual(None, event.circ_count)
- event = _get_event(ORCONN_CLOSED) + event = self._get_event(ORCONN_CLOSED, stem.response.events.ORConnEvent)
- self.assertTrue(isinstance(event, stem.response.events.ORConnEvent)) - self.assertEqual(ORCONN_CLOSED.lstrip("650 "), str(event)) self.assertEqual("$A1130635A0CDA6F60C276FBF6994EFBD4ECADAB1~tama", event.endpoint) self.assertEqual("A1130635A0CDA6F60C276FBF6994EFBD4ECADAB1", event.endpoint_fingerprint) self.assertEqual("tama", event.endpoint_nickname) @@ -547,40 +488,32 @@ class TestEvents(unittest.TestCase): self.assertEqual(None, event.circ_count)
def test_signal_event(self): - event = _get_event("650 SIGNAL DEBUG") - self.assertTrue(isinstance(event, stem.response.events.SignalEvent)) - self.assertEqual("SIGNAL DEBUG", str(event)) + event = self._get_event("650 SIGNAL DEBUG", stem.response.events.SignalEvent) self.assertEqual(Signal.DEBUG, event.signal)
- event = _get_event("650 SIGNAL DUMP") + event = self._get_event("650 SIGNAL DUMP", stem.response.events.SignalEvent) self.assertEqual(Signal.DUMP, event.signal)
def test_status_event_consensus_arrived(self): - event = _get_event(STATUS_CLIENT_CONSENSUS_ARRIVED) - - self.assertTrue(isinstance(event, stem.response.events.StatusEvent)) - self.assertEqual(STATUS_CLIENT_CONSENSUS_ARRIVED.lstrip("650 "), str(event)) + event = self._get_event(STATUS_CLIENT_CONSENSUS_ARRIVED, stem.response.events.StatusEvent) self.assertEqual(StatusType.CLIENT, event.status_type) self.assertEqual(Runlevel.NOTICE, event.runlevel) self.assertEqual("CONSENSUS_ARRIVED", event.action)
def test_status_event_enough_dir_info(self): - event = _get_event(STATUS_CLIENT_ENOUGH_DIR_INFO) - + event = self._get_event(STATUS_CLIENT_ENOUGH_DIR_INFO, stem.response.events.StatusEvent) self.assertEqual(StatusType.CLIENT, event.status_type) self.assertEqual(Runlevel.NOTICE, event.runlevel) self.assertEqual("ENOUGH_DIR_INFO", event.action)
def test_status_event_circuit_established(self): - event = _get_event(STATUS_CLIENT_CIRC_ESTABLISHED) - + event = self._get_event(STATUS_CLIENT_CIRC_ESTABLISHED, stem.response.events.StatusEvent) self.assertEqual(StatusType.CLIENT, event.status_type) self.assertEqual(Runlevel.NOTICE, event.runlevel) self.assertEqual("CIRCUIT_ESTABLISHED", event.action)
def test_status_event_bootstrap_descriptors(self): - event = _get_event(STATUS_CLIENT_BOOTSTRAP_DESCRIPTORS) - + event = self._get_event(STATUS_CLIENT_BOOTSTRAP_DESCRIPTORS, stem.response.events.StatusEvent) self.assertEqual(StatusType.CLIENT, event.status_type) self.assertEqual(Runlevel.NOTICE, event.runlevel) self.assertEqual("BOOTSTRAP", event.action) @@ -594,8 +527,7 @@ class TestEvents(unittest.TestCase): self.assertEqual(expected_attr, event.keyword_args)
def test_status_event_bootstrap_stuck(self): - event = _get_event(STATUS_CLIENT_BOOTSTRAP_STUCK) - + event = self._get_event(STATUS_CLIENT_BOOTSTRAP_STUCK, stem.response.events.StatusEvent) self.assertEqual(StatusType.CLIENT, event.status_type) self.assertEqual(Runlevel.WARN, event.runlevel) self.assertEqual("BOOTSTRAP", event.action) @@ -613,8 +545,7 @@ class TestEvents(unittest.TestCase): self.assertEqual(expected_attr, event.keyword_args)
def test_status_event_bootstrap_connecting(self): - event = _get_event(STATUS_CLIENT_BOOTSTRAP_CONNECTING) - + event = self._get_event(STATUS_CLIENT_BOOTSTRAP_CONNECTING, stem.response.events.StatusEvent) self.assertEqual(StatusType.CLIENT, event.status_type) self.assertEqual(Runlevel.NOTICE, event.runlevel) self.assertEqual("BOOTSTRAP", event.action) @@ -628,8 +559,7 @@ class TestEvents(unittest.TestCase): self.assertEqual(expected_attr, event.keyword_args)
def test_status_event_bootstrap_first_handshake(self): - event = _get_event(STATUS_CLIENT_BOOTSTRAP_FIRST_HANDSHAKE) - + event = self._get_event(STATUS_CLIENT_BOOTSTRAP_FIRST_HANDSHAKE, stem.response.events.StatusEvent) self.assertEqual(StatusType.CLIENT, event.status_type) self.assertEqual(Runlevel.NOTICE, event.runlevel) self.assertEqual("BOOTSTRAP", event.action) @@ -643,8 +573,7 @@ class TestEvents(unittest.TestCase): self.assertEqual(expected_attr, event.keyword_args)
def test_status_event_bootstrap_established(self): - event = _get_event(STATUS_CLIENT_BOOTSTRAP_ESTABLISHED) - + event = self._get_event(STATUS_CLIENT_BOOTSTRAP_ESTABLISHED, stem.response.events.StatusEvent) self.assertEqual(StatusType.CLIENT, event.status_type) self.assertEqual(Runlevel.NOTICE, event.runlevel) self.assertEqual("BOOTSTRAP", event.action) @@ -658,8 +587,7 @@ class TestEvents(unittest.TestCase): self.assertEqual(expected_attr, event.keyword_args)
def test_status_event_bootstrap_done(self): - event = _get_event(STATUS_CLIENT_BOOTSTRAP_DONE) - + event = self._get_event(STATUS_CLIENT_BOOTSTRAP_DONE, stem.response.events.StatusEvent) self.assertEqual(StatusType.CLIENT, event.status_type) self.assertEqual(Runlevel.NOTICE, event.runlevel) self.assertEqual("BOOTSTRAP", event.action) @@ -673,8 +601,7 @@ class TestEvents(unittest.TestCase): self.assertEqual(expected_attr, event.keyword_args)
def test_status_event_bootstrap_check_reachability(self): - event = _get_event(STATUS_SERVER_CHECK_REACHABILITY) - + event = self._get_event(STATUS_SERVER_CHECK_REACHABILITY, stem.response.events.StatusEvent) self.assertEqual(StatusType.SERVER, event.status_type) self.assertEqual(Runlevel.NOTICE, event.runlevel) self.assertEqual("CHECKING_REACHABILITY", event.action) @@ -686,8 +613,7 @@ class TestEvents(unittest.TestCase): self.assertEqual(expected_attr, event.keyword_args)
def test_status_event_dns_timeout(self): - event = _get_event(STATUS_SERVER_DNS_TIMEOUT) - + event = self._get_event(STATUS_SERVER_DNS_TIMEOUT, stem.response.events.StatusEvent) self.assertEqual(StatusType.SERVER, event.status_type) self.assertEqual(Runlevel.NOTICE, event.runlevel) self.assertEqual("NAMESERVER_STATUS", event.action) @@ -701,15 +627,13 @@ class TestEvents(unittest.TestCase): self.assertEqual(expected_attr, event.keyword_args)
def test_status_event_dns_down(self): - event = _get_event(STATUS_SERVER_DNS_DOWN) - + event = self._get_event(STATUS_SERVER_DNS_DOWN, stem.response.events.StatusEvent) self.assertEqual(StatusType.SERVER, event.status_type) self.assertEqual(Runlevel.WARN, event.runlevel) self.assertEqual("NAMESERVER_ALL_DOWN", event.action)
def test_status_event_dns_up(self): - event = _get_event(STATUS_SERVER_DNS_UP) - + event = self._get_event(STATUS_SERVER_DNS_UP, stem.response.events.StatusEvent) self.assertEqual(StatusType.SERVER, event.status_type) self.assertEqual(Runlevel.NOTICE, event.runlevel) self.assertEqual("NAMESERVER_STATUS", event.action) @@ -721,11 +645,9 @@ class TestEvents(unittest.TestCase):
self.assertEqual(expected_attr, event.keyword_args)
- def test_stream_event(self): - event = _get_event(STREAM_NEW) + def test_stream_event_new(self): + event = self._get_event(STREAM_NEW, stem.response.events.StreamEvent)
- self.assertTrue(isinstance(event, stem.response.events.StreamEvent)) - self.assertEqual(STREAM_NEW.lstrip("650 "), str(event)) self.assertEqual("18", event.id) self.assertEqual(StreamStatus.NEW, event.status) self.assertEqual(None, event.circ_id) @@ -739,11 +661,10 @@ class TestEvents(unittest.TestCase): self.assertEqual("127.0.0.1", event.source_address) self.assertEqual(47849, event.source_port) self.assertEqual(StreamPurpose.USER, event.purpose) + + def test_stream_event_sent_connect(self): + event = self._get_event(STREAM_SENTCONNECT, stem.response.events.StreamEvent)
- event = _get_event(STREAM_SENTCONNECT) - - self.assertTrue(isinstance(event, stem.response.events.StreamEvent)) - self.assertEqual(STREAM_SENTCONNECT.lstrip("650 "), str(event)) self.assertEqual("18", event.id) self.assertEqual(StreamStatus.SENTCONNECT, event.status) self.assertEqual("26", event.circ_id) @@ -757,11 +678,10 @@ class TestEvents(unittest.TestCase): self.assertEqual(None, event.source_address) self.assertEqual(None, event.source_port) self.assertEqual(None, event.purpose) + + def test_stream_event_remap(self): + event = self._get_event(STREAM_REMAP, stem.response.events.StreamEvent)
- event = _get_event(STREAM_REMAP) - - self.assertTrue(isinstance(event, stem.response.events.StreamEvent)) - self.assertEqual(STREAM_REMAP.lstrip("650 "), str(event)) self.assertEqual("18", event.id) self.assertEqual(StreamStatus.REMAP, event.status) self.assertEqual("26", event.circ_id) @@ -775,11 +695,10 @@ class TestEvents(unittest.TestCase): self.assertEqual(None, event.source_address) self.assertEqual(None, event.source_port) self.assertEqual(None, event.purpose) + + def test_stream_event_succeeded(self): + event = self._get_event(STREAM_SUCCEEDED, stem.response.events.StreamEvent)
- event = _get_event(STREAM_SUCCEEDED) - - self.assertTrue(isinstance(event, stem.response.events.StreamEvent)) - self.assertEqual(STREAM_SUCCEEDED.lstrip("650 "), str(event)) self.assertEqual("18", event.id) self.assertEqual(StreamStatus.SUCCEEDED, event.status) self.assertEqual("26", event.circ_id) @@ -793,11 +712,10 @@ class TestEvents(unittest.TestCase): self.assertEqual(None, event.source_address) self.assertEqual(None, event.source_port) self.assertEqual(None, event.purpose) + + def test_stream_event_closed_reset(self): + event = self._get_event(STREAM_CLOSED_RESET, stem.response.events.StreamEvent)
- event = _get_event(STREAM_CLOSED_RESET) - - self.assertTrue(isinstance(event, stem.response.events.StreamEvent)) - self.assertEqual(STREAM_CLOSED_RESET.lstrip("650 "), str(event)) self.assertEqual("21", event.id) self.assertEqual(StreamStatus.CLOSED, event.status) self.assertEqual("26", event.circ_id) @@ -811,11 +729,10 @@ class TestEvents(unittest.TestCase): self.assertEqual(None, event.source_address) self.assertEqual(None, event.source_port) self.assertEqual(None, event.purpose) + + def test_stream_event_closed_done(self): + event = self._get_event(STREAM_CLOSED_DONE, stem.response.events.StreamEvent)
- event = _get_event(STREAM_CLOSED_DONE) - - self.assertTrue(isinstance(event, stem.response.events.StreamEvent)) - self.assertEqual(STREAM_CLOSED_DONE.lstrip("650 "), str(event)) self.assertEqual("25", event.id) self.assertEqual(StreamStatus.CLOSED, event.status) self.assertEqual("26", event.circ_id) @@ -829,11 +746,10 @@ class TestEvents(unittest.TestCase): self.assertEqual(None, event.source_address) self.assertEqual(None, event.source_port) self.assertEqual(None, event.purpose) + + def test_stream_event_dir_fetch(self): + event = self._get_event(STREAM_DIR_FETCH, stem.response.events.StreamEvent)
- event = _get_event(STREAM_DIR_FETCH) - - self.assertTrue(isinstance(event, stem.response.events.StreamEvent)) - self.assertEqual(STREAM_DIR_FETCH.lstrip("650 "), str(event)) self.assertEqual("14", event.id) self.assertEqual(StreamStatus.NEW, event.status) self.assertEqual(None, event.circ_id) @@ -847,11 +763,10 @@ class TestEvents(unittest.TestCase): self.assertEqual("(Tor_internal)", event.source_address) self.assertEqual(0, event.source_port) self.assertEqual(StreamPurpose.DIR_FETCH, event.purpose) + + def test_stream_event_dns_request(self): + event = self._get_event(STREAM_DNS_REQUEST, stem.response.events.StreamEvent)
- event = _get_event(STREAM_DNS_REQUEST) - - self.assertTrue(isinstance(event, stem.response.events.StreamEvent)) - self.assertEqual(STREAM_DNS_REQUEST.lstrip("650 "), str(event)) self.assertEqual("1113", event.id) self.assertEqual(StreamStatus.NEW, event.status) self.assertEqual(None, event.circ_id) @@ -867,26 +782,24 @@ class TestEvents(unittest.TestCase): self.assertEqual(StreamPurpose.DNS_REQUEST, event.purpose)
def test_stream_bw_event(self): - event = _get_event("650 STREAM_BW 2 15 25") - - self.assertTrue(isinstance(event, stem.response.events.StreamBwEvent)) + event = self._get_event("650 STREAM_BW 2 15 25", stem.response.events.StreamBwEvent) self.assertEqual("2", event.id) self.assertEqual(15, event.written) self.assertEqual(25, event.read)
- event = _get_event("650 STREAM_BW Stream02 0 0") + event = self._get_event("650 STREAM_BW Stream02 0 0") self.assertEqual("Stream02", event.id) self.assertEqual(0, event.written) self.assertEqual(0, event.read)
- self.assertRaises(ProtocolError, _get_event, "650 STREAM_BW") - self.assertRaises(ProtocolError, _get_event, "650 STREAM_BW 2") - self.assertRaises(ProtocolError, _get_event, "650 STREAM_BW 2 15") - self.assertRaises(ProtocolError, _get_event, "650 STREAM_BW - 15 25") - self.assertRaises(ProtocolError, _get_event, "650 STREAM_BW 12345678901234567 15 25") - self.assertRaises(ProtocolError, _get_event, "650 STREAM_BW 2 -15 25") - self.assertRaises(ProtocolError, _get_event, "650 STREAM_BW 2 15 -25") - self.assertRaises(ProtocolError, _get_event, "650 STREAM_BW 2 x 25") + self.assertRaises(ProtocolError, self._get_event, "650 STREAM_BW") + self.assertRaises(ProtocolError, self._get_event, "650 STREAM_BW 2") + self.assertRaises(ProtocolError, self._get_event, "650 STREAM_BW 2 15") + self.assertRaises(ProtocolError, self._get_event, "650 STREAM_BW - 15 25") + self.assertRaises(ProtocolError, self._get_event, "650 STREAM_BW 12345678901234567 15 25") + self.assertRaises(ProtocolError, self._get_event, "650 STREAM_BW 2 -15 25") + self.assertRaises(ProtocolError, self._get_event, "650 STREAM_BW 2 15 -25") + self.assertRaises(ProtocolError, self._get_event, "650 STREAM_BW 2 x 25")
def test_unrecognized_enum_logging(self): """ @@ -900,17 +813,40 @@ class TestEvents(unittest.TestCase):
# Try parsing a valid event. We shouldn't log anything.
- _get_event(STATUS_CLIENT_CONSENSUS_ARRIVED) + self._get_event(STATUS_CLIENT_CONSENSUS_ARRIVED) self.assertTrue(logging_buffer.is_empty()) self.assertEqual([], list(logging_buffer))
# Parse an invalid runlevel.
- _get_event(STATUS_CLIENT_CONSENSUS_ARRIVED.replace("NOTICE", "OMEGA_CRITICAL!!!")) + self._get_event(STATUS_CLIENT_CONSENSUS_ARRIVED.replace("NOTICE", "OMEGA_CRITICAL!!!")) logged_events = list(logging_buffer)
self.assertEqual(1, len(logged_events)) self.assertTrue("STATUS_CLIENT event had an unrecognized runlevel" in logged_events[0])
stem_logger.removeHandler(logging_buffer) + + def _get_event(self, content, assert_class = None, assert_content = True): + """ + Provides an :class:`~stem.response.events.Event` subclass for the given + content. + + :param str content: content to create an event from + :param class assert_class: assert that the response is an instance of this class + :param bool assert_content: check the event's string content if **True** + + :raises: :class:`stem.ProtocolError` if a problem arises in parsing the event + """ + + controller_event = mocking.get_message(content) + stem.response.convert("EVENT", controller_event, arrived_at = 25) + + if assert_class: + self.assertTrue(isinstance(controller_event, assert_class)) + + if assert_content: + self.assertEqual(content.lstrip("650 "), str(controller_event)) + + return controller_event
tor-commits@lists.torproject.org