[Pkg-privacy-commits] [txtorcon] 26/96: Fix up post_bootstrap handling, and tests

Jérémy Bobbio lunar at moszumanska.debian.org
Sun Sep 6 18:33:35 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 c280826e589de59b917740fcf9bdb4e96df75501
Author: meejah <meejah at meejah.ca>
Date:   Sat Jan 17 00:37:07 2015 -0700

    Fix up post_bootstrap handling, and tests
    
    Cover a bunch more cases, and improve error handling. Also don't
    blindly log all errors (up to user).
---
 test/test_torconfig.py         | 126 ++++++++++++++++++++++++++++++++++-------
 txtorcon/torconfig.py          |  16 ++----
 txtorcon/torcontrolprotocol.py |   7 +--
 txtorcon/torstate.py           |   5 +-
 4 files changed, 113 insertions(+), 41 deletions(-)

diff --git a/test/test_torconfig.py b/test/test_torconfig.py
index d58544b..be4c294 100644
--- a/test/test_torconfig.py
+++ b/test/test_torconfig.py
@@ -123,11 +123,7 @@ class ConfigTests(unittest.TestCase):
         self.protocol.answers.append('config/names=\nfoo Boolean')
         self.protocol.answers.append({'foo': 'bar'})
         cfg = TorConfig(self.protocol)
-        self.assertEqual(cfg.get_type('foo'), torconfig.Boolean)
-        errs = self.flushLoggedErrors(ValueError)
-        self.assertEqual(len(errs), 1)
-        ## dunno if asserting strings in messages is a good idea...
-        self.assertTrue('invalid literal' in errs[0].getErrorMessage())
+        return self.assertFailure(cfg.post_bootstrap, ValueError)
 
     def test_contains(self):
         cfg = TorConfig()
@@ -171,18 +167,14 @@ class ConfigTests(unittest.TestCase):
     def test_int_parser_error(self):
         self.protocol.answers.append('config/names=\nfoo Integer')
         self.protocol.answers.append({'foo': '123foo'})
-        TorConfig(self.protocol)
-        errs = self.flushLoggedErrors(ValueError)
-        self.assertEqual(len(errs), 1)
-        self.assertTrue(isinstance(errs[0].value, ValueError))
+        cfg = TorConfig(self.protocol)
+        self.assertFailure(cfg.post_bootstrap, ValueError)
 
     def test_int_parser_error_2(self):
         self.protocol.answers.append('config/names=\nfoo Integer')
         self.protocol.answers.append({'foo': '1.23'})
-        TorConfig(self.protocol)
-        errs = self.flushLoggedErrors(ValueError)
-        self.assertEqual(len(errs), 1)
-        self.assertTrue(isinstance(errs[0].value, ValueError))
+        cfg = TorConfig(self.protocol)
+        return self.assertFailure(cfg.post_bootstrap, ValueError)
 
     def test_linelist_parser(self):
         self.protocol.answers.append('config/names=\nfoo LineList')
@@ -206,10 +198,8 @@ class ConfigTests(unittest.TestCase):
     def test_float_parser_error(self):
         self.protocol.answers.append('config/names=\nfoo Float')
         self.protocol.answers.append({'foo': '1.23fff'})
-        TorConfig(self.protocol)
-        errs = self.flushLoggedErrors(ValueError)
-        self.assertEqual(len(errs), 1)
-        self.assertTrue(isinstance(errs[0].value, ValueError))
+        cfg = TorConfig(self.protocol)
+        return self.assertFailure(cfg.post_bootstrap, ValueError)
 
     def test_list(self):
         self.protocol.answers.append('config/names=\nbing CommaList')
@@ -222,6 +212,7 @@ class ConfigTests(unittest.TestCase):
         self.protocol.answers.append('config/names=\nbing CommaList')
         self.protocol.answers.append({'bing': 'foo'})
         conf = TorConfig(self.protocol)
+        self.assertTrue(conf.post_bootstrap.called)
         self.assertEqual(conf.config['bing'], ['foo'])
 
     def test_multi_list_space(self):
@@ -264,10 +255,24 @@ class ConfigTests(unittest.TestCase):
 
     def test_invalid_parser(self):
         self.protocol.answers.append('config/names=\nSomethingExciting NonExistantParserType')
-        TorConfig(self.protocol)
-        errs = self.flushLoggedErrors()
-        self.assertEqual(len(errs), 1)
-        self.assertTrue('NonExistantParserType' in str(errs[0]))
+        cfg = TorConfig(self.protocol)
+        return self.assertFailure(cfg.post_bootstrap, RuntimeError)
+
+    def test_iteration(self):
+        conf = TorConfig()
+        conf.SOCKSPort = 9876
+        conf.save()
+        x = list(conf)
+        self.assertEqual(x, ['SOCKSPort'])
+        conf.save()
+
+    def test_get_type(self):
+        self.protocol.answers.append('config/names=\nSomethingExciting CommaList')
+        self.protocol.answers.append({'SomethingExciting': 'a,b'})
+        conf = TorConfig(self.protocol)
+
+        from txtorcon.torconfig import CommaList
+        self.assertEqual(conf.get_type('SomethingExciting'), CommaList)
 
     def foo(self, *args):
         print "FOOO", args
@@ -499,7 +504,7 @@ class LogTests(unittest.TestCase):
 
     def test_set_wrong_object(self):
         conf = TorConfig(self.protocol)
-
+        self.assertTrue(conf.post_bootstrap.called)
         try:
             conf.log = ('this', 'is', 'a', 'tuple')
             self.fail()
@@ -666,6 +671,10 @@ HiddenServicePort=90 127.0.0.1:2345''')
         self.protocol.answers.append('HiddenServiceDir=/fake/path\nHiddenServicePort=80 127.0.0.1:1234\n')
 
         conf = TorConfig(self.protocol)
+        self.assertTrue(self.protocol.post_bootstrap.called)
+        self.assertTrue(conf.post_bootstrap == None or conf.post_bootstrap.called)
+        self.assertEqual(len(conf.hiddenservices), 1)
+        self.assertTrue(conf.hiddenservices[0].conf)
         conf.hiddenservices[0].version = 3
         self.assertTrue(conf.needs_save())
         conf.hiddenservices[0].version = 4
@@ -868,6 +877,79 @@ class LaunchTorTests(unittest.TestCase):
         self.flushLoggedErrors(RuntimeError)
         return d
 
+    def test_launch_with_timeout_no_ireactortime(self):
+        config = TorConfig()
+        return self.assertRaises(RuntimeError,
+            launch_tor, config, None, timeout=5, tor_binary='/bin/echo')
+
+    def test_launch_root_changes_tmpdir_ownership(self):
+        config = TorConfig()
+        try:
+            launch_tor(config, None, timeout=5, tor_binary='/bin/echo')
+            self.fail("Should have thrown an error")
+        except RuntimeError:
+            pass
+
+    @defer.inlineCallbacks
+    def test_launch_timeout_exception(self):
+        self.protocol = FakeControlProtocol([])
+        self.protocol.answers.append('''config/names=
+DataDirectory String
+ControlPort Port''')
+        self.protocol.answers.append({'DataDirectory':'foo'})
+        self.protocol.answers.append({'ControlPort':0})
+        config = TorConfig(self.protocol)
+        yield config.post_bootstrap
+        config.DataDirectory = '/dev/null'
+
+        trans = Mock()
+        d = launch_tor(config, FakeReactor(self, trans, Mock()), tor_binary='/bin/echo')
+        tpp = yield d
+        tpp.transport = trans
+        trans.signalProcess = Mock(side_effect=error.ProcessExitedAlready)
+        trans.loseConnection = Mock()
+
+        tpp.timeout_expired()
+
+        tpp.transport.loseConnection.assert_called()
+
+    @defer.inlineCallbacks
+    def test_launch_timeout_process_exits(self):
+        # cover the "one more edge case" where we get a processEnded()
+        # but we've already "done" a timeout.
+        self.protocol = FakeControlProtocol([])
+        self.protocol.answers.append('''config/names=
+DataDirectory String
+ControlPort Port''')
+        self.protocol.answers.append({'DataDirectory':'foo'})
+        self.protocol.answers.append({'ControlPort':0})
+        config = TorConfig(self.protocol)
+        yield config.post_bootstrap
+        config.DataDirectory = '/dev/null'
+
+        trans = Mock()
+        d = launch_tor(config, FakeReactor(self, trans, Mock()), tor_binary='/bin/echo')
+        tpp = yield d
+        tpp.timeout_expired()
+        tpp.transport = trans
+        trans.signalProcess = Mock()
+        trans.loseConnection = Mock()
+        status = Mock()
+        status.value.exitCode = None
+        self.assertTrue(tpp._did_timeout)
+        tpp.processEnded(status)
+
+        errs = self.flushLoggedErrors(RuntimeError)
+        self.assertEqual(1, len(errs))
+
+    def test_launch_wrong_stdout(self):
+        config = TorConfig()
+        try:
+            launch_tor(config, None, stdout=object(), tor_binary='/bin/echo')
+            self.fail("Should have thrown an error")
+        except RuntimeError:
+            pass
+
     def test_launch_with_timeout(self):
         config = TorConfig()
         config.OrPort = 1234
diff --git a/txtorcon/torconfig.py b/txtorcon/torconfig.py
index 4aef8d8..ea3bd97 100644
--- a/txtorcon/torconfig.py
+++ b/txtorcon/torconfig.py
@@ -776,7 +776,7 @@ class TorConfig(object):
                 self.bootstrap()
 
         else:
-            self.post_bootstrap.callback(self)
+            self.do_post_bootstrap(self)
 
         self.__dict__['_setup_'] = None
 
@@ -805,8 +805,6 @@ class TorConfig(object):
         self.__dict__['post_bootstrap'] = defer.Deferred()
         if proto.post_bootstrap:
             proto.post_bootstrap.addCallback(self.bootstrap)
-        else:
-            self.bootstrap()
         return self.__dict__['post_bootstrap']
 
     def _update_proto(self, proto):
@@ -922,18 +920,17 @@ class TorConfig(object):
                 "Can't listen for CONF_CHANGED event; won't stay up-to-date "
                 "with other clients.")
         d = self.protocol.get_info_raw("config/names")
-        d.addCallbacks(self._do_setup, log.err)
+        d.addCallback(self._do_setup)
         d.addCallback(self.do_post_bootstrap)
         d.addErrback(self.do_post_errback)
 
-    def do_post_errback(self, fail):
-        self.post_bootstrap.errback(fail)
-        self.__dict__['post_bootstrap'] = None
+    def do_post_errback(self, f):
+        self.post_bootstrap.errback(f)
         return None
 
     def do_post_bootstrap(self, arg):
-        self.post_bootstrap.callback(self)
-        self.__dict__['post_bootstrap'] = None
+        if not self.post_bootstrap.called:
+            self.post_bootstrap.callback(self)
         return self
 
     def needs_save(self):
@@ -991,7 +988,6 @@ class TorConfig(object):
         if self.protocol:
             d = self.protocol.set_conf(*args)
             d.addCallback(self._save_completed)
-            d.addErrback(log.err)
             return d
 
         else:
diff --git a/txtorcon/torcontrolprotocol.py b/txtorcon/torcontrolprotocol.py
index e9e5551..5a6ed45 100644
--- a/txtorcon/torcontrolprotocol.py
+++ b/txtorcon/torcontrolprotocol.py
@@ -588,10 +588,8 @@ class TorControlProtocol(LineOnlyReceiver):
         Errback if authentication fails.
         """
 
-        if self.post_bootstrap:
-            self.post_bootstrap.errback(fail)
-            return None
-        return fail
+        self.post_bootstrap.errback(fail)
+        return None
 
     def _safecookie_authchallenge(self, reply):
         """
@@ -696,7 +694,6 @@ class TorControlProtocol(LineOnlyReceiver):
         yield self.queue_command('USEFEATURE EXTENDED_EVENTS')
 
         self.post_bootstrap.callback(self)
-        self.post_bootstrap = None
         defer.returnValue(self)
 
     ##
diff --git a/txtorcon/torstate.py b/txtorcon/torstate.py
index 0342068..08de977 100644
--- a/txtorcon/torstate.py
+++ b/txtorcon/torstate.py
@@ -253,10 +253,7 @@ class TorState(object):
 
         self.post_bootstrap = defer.Deferred()
         if bootstrap:
-            if self.protocol.post_bootstrap:
-                self.protocol.post_bootstrap.addCallback(self._bootstrap).addErrback(self.post_bootstrap.errback)
-            else:
-                self._bootstrap()
+            self.protocol.post_bootstrap.addCallback(self._bootstrap).addErrback(self.post_bootstrap.errback)
 
     def _router_begin(self, data):
         args = data.split()

-- 
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