[Pkg-privacy-commits] [txtorcon] 55/96: Nicer way to create TorState instances

Jérémy Bobbio lunar at moszumanska.debian.org
Sun Sep 6 18:33:39 UTC 2015


This is an automated email from the git hooks/post-receive script.

lunar pushed a commit to branch master
in repository txtorcon.

commit 8aa2b6427c805f008f51fc5a247c06d2390d45e9
Author: meejah <meejah at meejah.ca>
Date:   Thu Feb 5 18:09:44 2015 -0700

    Nicer way to create TorState instances
    
    Also, refactor + simplify one of the tests and
    include TorState.from_protocol() coverage
---
 test/test_torconfig.py |  8 +++++
 test/test_torstate.py  | 79 ++++++++++++--------------------------------------
 txtorcon/torstate.py   | 17 +++++++++--
 3 files changed, 41 insertions(+), 63 deletions(-)

diff --git a/test/test_torconfig.py b/test/test_torconfig.py
index 0f47c39..46e8b94 100644
--- a/test/test_torconfig.py
+++ b/test/test_torconfig.py
@@ -62,6 +62,7 @@ class FakeControlProtocol:
         self.sets = []
         self.events = {}  #: event type -> callback
         self.pending_events = {}  #: event type -> list
+        self.is_owned = -1
 
     def event_happened(self, event_type, *args):
         '''
@@ -89,6 +90,13 @@ class FakeControlProtocol:
         self.answers = self.answers[1:]
         return d
 
+    @defer.inlineCallbacks
+    def get_info_incremental(self, info, cb):
+        text = yield self.get_info_raw(info)
+        for line in text.split('\r\n'):
+            cb(line)
+        defer.returnValue('')  # FIXME uh....what's up at torstate.py:350?
+
     def get_conf(self, info):
         if len(self.answers) == 0:
             d = defer.Deferred()
diff --git a/test/test_torstate.py b/test/test_torstate.py
index 35b9097..f3348b8 100644
--- a/test/test_torstate.py
+++ b/test/test_torstate.py
@@ -197,22 +197,6 @@ class FakeEndpointAnswers:
         return defer.succeed(self.proto)
 
 
-class FakeControlProtocol:
-    implements(ITorControlProtocol)  # actually we don't, it's a lie
-
-    def __init__(self):
-        self.is_owned = None
-        self.post_bootstrap = defer.succeed(self)
-        self.on_disconnect = defer.Deferred()
-
-
-class InternalMethodsTests(unittest.TestCase):
-
-    def test_state_diagram(self):
-        TorState(FakeControlProtocol(), bootstrap=False, write_state_diagram=True)
-        self.assertTrue(os.path.exists('routerfsm.dot'))
-
-
 class BootstrapTests(unittest.TestCase):
 
     def confirm_proto(self, x):
@@ -375,60 +359,35 @@ class StateTests(unittest.TestCase):
     def send(self, line):
         self.protocol.dataReceived(line.strip() + "\r\n")
 
+    @defer.inlineCallbacks
     def test_bootstrap_callback(self):
         '''
         FIXME: something is still screwy with this; try throwing an
         exception from TorState.bootstrap and we'll just hang...
         '''
 
-        d = self.state.post_bootstrap
-
-        self.protocol._set_valid_events(' '.join(self.state.event_map.keys()))
-        self.protocol.is_owned = 999
-        self.state._bootstrap()
-
-        self.send("250+ns/all=")
-        self.send(".")
-        self.send("250 OK")
-
-        self.send("250+circuit-status=")
-        self.send(".")
-        self.send("250 OK")
-
-        self.send("250-stream-status=")
-        self.send("250 OK")
-
-        self.send("250-address-mappings/all=")
-        self.send("250 OK")
-
-        for ignored in self.state.event_map.items():
-            self.send("250 OK")
-
-        fakerouter = object()
-        self.state.routers['$0000000000000000000000000000000000000000'] = fakerouter
-        self.state.routers['$9999999999999999999999999999999999999999'] = fakerouter
-        self.send("250+entry-guards=")
-        self.send("$0000000000000000000000000000000000000000=name up")
-        self.send("$1111111111111111111111111111111111111111=foo up")
-        self.send("$9999999999999999999999999999999999999999=eman unusable 2012-01-01 22:00:00")
-        self.send(".")
-        self.send("250 OK")
+        from test_torconfig import FakeControlProtocol
+        protocol = FakeControlProtocol(
+            [
+                "ns/all=",  # ns/all
+                "",  # circuit-status
+                "",  # stream-status
+                "",  # address-mappings/all
+                "entry-guards=\r\n$0000000000000000000000000000000000000000=name up\r\n$1111111111111111111111111111111111111111=foo up\r\n$9999999999999999999999999999999999999999=eman unusable 2012-01-01 22:00:00\r\n", # entry-guards
+                "99999",  # process/pid
+                "??",  # ip-to-country/0.0.0.0
+            ]
+        )
 
-        # implicitly created Router object for the $1111...11 lookup
-        # but 0.0.0.0 will have to country, so Router will ask Tor
-        # for one via GETINFO ip-to-country
-        self.send("250-ip-to-country/0.0.0.0=??")
-        self.send("250 OK")
 
-        self.assertEqual(len(self.state.entry_guards), 2)
-        self.assertTrue('$0000000000000000000000000000000000000000' in self.state.entry_guards)
-        self.assertEqual(self.state.entry_guards['$0000000000000000000000000000000000000000'], fakerouter)
-        self.assertTrue('$1111111111111111111111111111111111111111' in self.state.entry_guards)
+        state = yield TorState.from_protocol(protocol)
 
-        self.assertEqual(len(self.state.unusable_entry_guards), 1)
-        self.assertTrue('$9999999999999999999999999999999999999999' in self.state.unusable_entry_guards[0])
 
-        return d
+        self.assertEqual(len(state.entry_guards), 2)
+        self.assertTrue('$0000000000000000000000000000000000000000' in state.entry_guards)
+        self.assertTrue('$1111111111111111111111111111111111111111' in state.entry_guards)
+        self.assertEqual(len(state.unusable_entry_guards), 1)
+        self.assertTrue('$9999999999999999999999999999999999999999' in state.unusable_entry_guards[0])
 
     def test_bootstrap_existing_addresses(self):
         '''
diff --git a/txtorcon/torstate.py b/txtorcon/torstate.py
index 164ef1a..c117a85 100644
--- a/txtorcon/torstate.py
+++ b/txtorcon/torstate.py
@@ -155,6 +155,10 @@ def flags_from_dict(kw):
     return flags
 
 
+ at implementer(ICircuitListener)
+ at implementer(ICircuitContainer)
+ at implementer(IRouterContainer)
+ at implementer(IStreamListener)
 class TorState(object):
     """
     This tracks the current state of Tor using a TorControlProtocol.
@@ -181,10 +185,17 @@ class TorState(object):
     attach this stream at all.
     """
 
-    implements(ICircuitListener, ICircuitContainer, IRouterContainer,
-               IStreamListener)
+    @classmethod
+    def from_protocol(cls, protocol, **kw):
+        '''
+        Create a new, boot-strapped TorState from a TorControlProtocol instance.
 
-    def __init__(self, protocol, bootstrap=True, write_state_diagram=False):
+        :return: a Deferred that fires with a TorState instance
+        '''
+        state = TorState(protocol, bootstrap=True)
+        return state.post_bootstrap
+
+    def __init__(self, protocol, bootstrap=True):
         self.protocol = ITorControlProtocol(protocol)
         # fixme could use protocol.on_disconnect to re-connect; see issue #3
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/txtorcon.git



More information about the Pkg-privacy-commits mailing list