From a7cfe5ba326b950d882619f88ae17b3cd2d18ba3 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Fri, 9 Aug 2019 17:15:00 +0300 Subject: [PATCH] Try to test with tor --- .travis.yml | 1 + requirements.txt | 1 + src/bitmessagemain.py | 5 +++-- src/plugins/proxyconfig_stem.py | 13 +++++++++++-- src/tests/core.py | 28 +++++++++++++++++++++------- 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1edba418..d7141188 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ addons: packages: - build-essential - libcap-dev + - tor install: - pip install -r requirements.txt - ln -s src pybitmessage # tests environment diff --git a/requirements.txt b/requirements.txt index c55e5cf1..be429a9f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ python_prctl psutil pycrypto +stem diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 5dcfea6c..1dd2f271 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -193,7 +193,8 @@ class Main: from plugins.plugin import get_plugin try: proxyconfig_start = time.time() - get_plugin('proxyconfig', name=proxy_type)(config) + if not get_plugin('proxyconfig', name=proxy_type)(config): + raise TypeError except TypeError: logger.error( 'Failed to run proxy config plugin %s', @@ -424,7 +425,7 @@ class Main: self.stop() elif not state.enableGUI: from tests import core as test_core # pylint: disable=relative-import - test_core_result = test_core.run() + test_core_result = test_core.run(self) state.enableGUI = True self.stop() test_core.cleanup() diff --git a/src/plugins/proxyconfig_stem.py b/src/plugins/proxyconfig_stem.py index 75605c07..e8b5417e 100644 --- a/src/plugins/proxyconfig_stem.py +++ b/src/plugins/proxyconfig_stem.py @@ -8,6 +8,7 @@ import tempfile import stem import stem.control import stem.process +import stem.version class DebugLogger(object): @@ -31,7 +32,7 @@ class DebugLogger(object): self._logger.log(self._levels.get(level, 10), '(tor)' + line) -def connect_plugin(config): +def connect_plugin(config): # pylint: disable=too-many-branches """Run stem proxy configurator""" logwrite = DebugLogger() if config.safeGet('bitmessagesettings', 'sockshostname') not in ( @@ -60,8 +61,14 @@ def connect_plugin(config): # So if there is a system wide tor, use it for outbound connections. try: stem.process.launch_tor_with_config( - tor_config, take_ownership=True, init_msg_handler=logwrite) + tor_config, take_ownership=True, timeout=20, + init_msg_handler=logwrite) except OSError: + if not attempt: + try: + stem.version.get_system_tor_version() + except IOError: + return continue else: logwrite('Started tor on port %s' % port) @@ -108,3 +115,5 @@ def connect_plugin(config): onionhostname, 'keytype', response.private_key_type) config.save() config.set('bitmessagesettings', 'socksproxytype', 'SOCKS5') + + return True diff --git a/src/tests/core.py b/src/tests/core.py index a9df05fc..005900d0 100644 --- a/src/tests/core.py +++ b/src/tests/core.py @@ -21,6 +21,7 @@ from network.tcp import Socks4aBMConnection, Socks5BMConnection, TCPConnection from queues import excQueue knownnodes_file = os.path.join(state.appdata, 'knownnodes.dat') +program = None def pickle_knownnodes(): @@ -132,6 +133,7 @@ class TestCore(unittest.TestCase): self._outdate_knownnodes() # time.sleep(303) # singleCleaner wakes up every 5 min knownnodes.cleanupKnownNodes() + self.assertTrue(knownnodes.knownNodes[1]) while True: try: thread, exc = excQueue.get(block=False) @@ -140,14 +142,15 @@ class TestCore(unittest.TestCase): if thread == 'Asyncore' and isinstance(exc, IndexError): self.fail("IndexError because of empty knownNodes!") - def test_bootstrap(self): - """test bootstrapping""" + def _initiate_bootstrap(self): BMConfigParser().set('bitmessagesettings', 'dontconnect', 'true') self._outdate_knownnodes() - knownnodes.cleanupKnownNodes() - # it's weird, knownnodes appear empty knownnodes.addKnownNode(1, state.Peer('127.0.0.1', 8444), is_self=True) - time.sleep(0.25) + knownnodes.cleanupKnownNodes() + time.sleep(2) + + def _check_bootstrap(self): + _started = time.time() BMConfigParser().remove_option('bitmessagesettings', 'dontconnect') proxy_type = BMConfigParser().safeGet( 'bitmessagesettings', 'socksproxytype') @@ -157,20 +160,31 @@ class TestCore(unittest.TestCase): connection_base = Socks4aBMConnection else: connection_base = TCPConnection - _started = time.time() for _ in range(180): time.sleep(1) for peer, con in BMConnectionPool().outboundConnections.iteritems(): if not peer.host.startswith('bootstrap'): self.assertIsInstance(con, connection_base) + self.assertNotEqual(peer.host, '127.0.0.1') return else: # pylint: disable=useless-else-on-loop self.fail( 'Failed to connect during %s sec' % (time.time() - _started)) + def test_bootstrap(self): + """test bootstrapping""" + self._initiate_bootstrap() + self._check_bootstrap() + self._initiate_bootstrap() + BMConfigParser().set('bitmessagesettings', 'socksproxytype', 'stem') + program.start_proxyconfig(BMConfigParser()) + self._check_bootstrap() -def run(): + +def run(prog): """Starts all tests defined in this module""" + global program # pylint: disable=global-statement + program = prog loader = unittest.TestLoader() loader.sortTestMethodsUsing = None suite = loader.loadTestsFromTestCase(TestCore)