Try to test with tor

This commit is contained in:
Dmitri Bogomolov 2019-08-09 17:15:00 +03:00
parent 6a0c3ae075
commit a7cfe5ba32
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
5 changed files with 37 additions and 11 deletions

View File

@ -6,6 +6,7 @@ addons:
packages: packages:
- build-essential - build-essential
- libcap-dev - libcap-dev
- tor
install: install:
- pip install -r requirements.txt - pip install -r requirements.txt
- ln -s src pybitmessage # tests environment - ln -s src pybitmessage # tests environment

View File

@ -1,3 +1,4 @@
python_prctl python_prctl
psutil psutil
pycrypto pycrypto
stem

View File

@ -193,7 +193,8 @@ class Main:
from plugins.plugin import get_plugin from plugins.plugin import get_plugin
try: try:
proxyconfig_start = time.time() proxyconfig_start = time.time()
get_plugin('proxyconfig', name=proxy_type)(config) if not get_plugin('proxyconfig', name=proxy_type)(config):
raise TypeError
except TypeError: except TypeError:
logger.error( logger.error(
'Failed to run proxy config plugin %s', 'Failed to run proxy config plugin %s',
@ -424,7 +425,7 @@ class Main:
self.stop() self.stop()
elif not state.enableGUI: elif not state.enableGUI:
from tests import core as test_core # pylint: disable=relative-import 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 state.enableGUI = True
self.stop() self.stop()
test_core.cleanup() test_core.cleanup()

View File

@ -8,6 +8,7 @@ import tempfile
import stem import stem
import stem.control import stem.control
import stem.process import stem.process
import stem.version
class DebugLogger(object): class DebugLogger(object):
@ -31,7 +32,7 @@ class DebugLogger(object):
self._logger.log(self._levels.get(level, 10), '(tor)' + line) 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""" """Run stem proxy configurator"""
logwrite = DebugLogger() logwrite = DebugLogger()
if config.safeGet('bitmessagesettings', 'sockshostname') not in ( 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. # So if there is a system wide tor, use it for outbound connections.
try: try:
stem.process.launch_tor_with_config( 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: except OSError:
if not attempt:
try:
stem.version.get_system_tor_version()
except IOError:
return
continue continue
else: else:
logwrite('Started tor on port %s' % port) logwrite('Started tor on port %s' % port)
@ -108,3 +115,5 @@ def connect_plugin(config):
onionhostname, 'keytype', response.private_key_type) onionhostname, 'keytype', response.private_key_type)
config.save() config.save()
config.set('bitmessagesettings', 'socksproxytype', 'SOCKS5') config.set('bitmessagesettings', 'socksproxytype', 'SOCKS5')
return True

View File

@ -21,6 +21,7 @@ from network.tcp import Socks4aBMConnection, Socks5BMConnection, TCPConnection
from queues import excQueue from queues import excQueue
knownnodes_file = os.path.join(state.appdata, 'knownnodes.dat') knownnodes_file = os.path.join(state.appdata, 'knownnodes.dat')
program = None
def pickle_knownnodes(): def pickle_knownnodes():
@ -132,6 +133,7 @@ class TestCore(unittest.TestCase):
self._outdate_knownnodes() self._outdate_knownnodes()
# time.sleep(303) # singleCleaner wakes up every 5 min # time.sleep(303) # singleCleaner wakes up every 5 min
knownnodes.cleanupKnownNodes() knownnodes.cleanupKnownNodes()
self.assertTrue(knownnodes.knownNodes[1])
while True: while True:
try: try:
thread, exc = excQueue.get(block=False) thread, exc = excQueue.get(block=False)
@ -140,14 +142,15 @@ class TestCore(unittest.TestCase):
if thread == 'Asyncore' and isinstance(exc, IndexError): if thread == 'Asyncore' and isinstance(exc, IndexError):
self.fail("IndexError because of empty knownNodes!") self.fail("IndexError because of empty knownNodes!")
def test_bootstrap(self): def _initiate_bootstrap(self):
"""test bootstrapping"""
BMConfigParser().set('bitmessagesettings', 'dontconnect', 'true') BMConfigParser().set('bitmessagesettings', 'dontconnect', 'true')
self._outdate_knownnodes() self._outdate_knownnodes()
knownnodes.cleanupKnownNodes()
# it's weird, knownnodes appear empty
knownnodes.addKnownNode(1, state.Peer('127.0.0.1', 8444), is_self=True) 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') BMConfigParser().remove_option('bitmessagesettings', 'dontconnect')
proxy_type = BMConfigParser().safeGet( proxy_type = BMConfigParser().safeGet(
'bitmessagesettings', 'socksproxytype') 'bitmessagesettings', 'socksproxytype')
@ -157,20 +160,31 @@ class TestCore(unittest.TestCase):
connection_base = Socks4aBMConnection connection_base = Socks4aBMConnection
else: else:
connection_base = TCPConnection connection_base = TCPConnection
_started = time.time()
for _ in range(180): for _ in range(180):
time.sleep(1) time.sleep(1)
for peer, con in BMConnectionPool().outboundConnections.iteritems(): for peer, con in BMConnectionPool().outboundConnections.iteritems():
if not peer.host.startswith('bootstrap'): if not peer.host.startswith('bootstrap'):
self.assertIsInstance(con, connection_base) self.assertIsInstance(con, connection_base)
self.assertNotEqual(peer.host, '127.0.0.1')
return return
else: # pylint: disable=useless-else-on-loop else: # pylint: disable=useless-else-on-loop
self.fail( self.fail(
'Failed to connect during %s sec' % (time.time() - _started)) '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""" """Starts all tests defined in this module"""
global program # pylint: disable=global-statement
program = prog
loader = unittest.TestLoader() loader = unittest.TestLoader()
loader.sortTestMethodsUsing = None loader.sortTestMethodsUsing = None
suite = loader.loadTestsFromTestCase(TestCore) suite = loader.loadTestsFromTestCase(TestCore)