Refactor create_connection_handlers so we don't need Tor and I2P mocks

This commit is contained in:
Jean-Paul Calderone 2020-12-09 16:18:48 -05:00
parent e2963856d3
commit 89441d9169
2 changed files with 67 additions and 32 deletions

View File

@ -616,28 +616,20 @@ def _make_tcp_handler():
return default() return default()
def create_connection_handlers(reactor, config, i2p_provider, tor_provider): def create_default_connection_handlers(reactor, config, handlers):
""" """
:returns: 2-tuple of default_connection_handlers, foolscap_connection_handlers :return: A dictionary giving the default connection handlers. The keys
are strings like "tcp" and the values are strings like "tor" or
``None``.
""" """
reveal_ip = config.get_config("node", "reveal-IP-address", True, boolean=True) reveal_ip = config.get_config("node", "reveal-IP-address", True, boolean=True)
# We store handlers for everything. None means we were unable to # Remember the default mappings from tahoe.cfg
# create that handler, so hints which want it will be ignored. default_connection_handlers = {
handlers = foolscap_connection_handlers = { name: name
"tcp": _make_tcp_handler(), for name
"tor": tor_provider.get_tor_handler(), in handlers
"i2p": i2p_provider.get_i2p_handler(), }
}
log.msg(
format="built Foolscap connection handlers for: %(known_handlers)s",
known_handlers=sorted([k for k,v in handlers.items() if v]),
facility="tahoe.node",
umid="PuLh8g",
)
# then we remember the default mappings from tahoe.cfg
default_connection_handlers = {"tor": "tor", "i2p": "i2p"}
tcp_handler_name = config.get_config("connections", "tcp", "tcp").lower() tcp_handler_name = config.get_config("connections", "tcp", "tcp").lower()
if tcp_handler_name == "disabled": if tcp_handler_name == "disabled":
default_connection_handlers["tcp"] = None default_connection_handlers["tcp"] = None
@ -662,10 +654,35 @@ def create_connection_handlers(reactor, config, i2p_provider, tor_provider):
if not reveal_ip: if not reveal_ip:
if default_connection_handlers.get("tcp") == "tcp": if default_connection_handlers.get("tcp") == "tcp":
raise PrivacyError("tcp = tcp, must be set to 'tor' or 'disabled'") raise PrivacyError("tcp = tcp, must not be set to 'tcp'")
return default_connection_handlers, foolscap_connection_handlers return default_connection_handlers
def create_connection_handlers(reactor, config, i2p_provider, tor_provider):
"""
:returns: 2-tuple of default_connection_handlers, foolscap_connection_handlers
"""
# We store handlers for everything. None means we were unable to
# create that handler, so hints which want it will be ignored.
handlers = {
"tcp": _make_tcp_handler(),
}
handlers.update({
"tor": tor_provider.get_tor_handler(),
"i2p": i2p_provider.get_i2p_handler(),
})
log.msg(
format="built Foolscap connection handlers for: %(known_handlers)s",
known_handlers=sorted([k for k,v in handlers.items() if v]),
facility="tahoe.node",
umid="PuLh8g",
)
return create_default_connection_handlers(
reactor,
config,
handlers,
), handlers
def create_tub(tub_options, default_connection_handlers, foolscap_connection_handlers, def create_tub(tub_options, default_connection_handlers, foolscap_connection_handlers,
handler_overrides={}, **kwargs): handler_overrides={}, **kwargs):

View File

@ -36,6 +36,7 @@ from twisted.trial import unittest
from twisted.internet import defer from twisted.internet import defer
import foolscap.logging.log import foolscap.logging.log
from foolscap.connections.tcp import default as make_tcp_handler
from twisted.application import service from twisted.application import service
from allmydata.node import ( from allmydata.node import (
@ -43,6 +44,7 @@ from allmydata.node import (
create_tub_options, create_tub_options,
create_main_tub, create_main_tub,
create_node_dir, create_node_dir,
create_default_connection_handlers,
create_connection_handlers, create_connection_handlers,
config_from_string, config_from_string,
read_config, read_config,
@ -778,14 +780,19 @@ class Listeners(unittest.TestCase):
f.write("tub.location = AUTO\n") f.write("tub.location = AUTO\n")
config = client.read_config(basedir, "client.port") config = client.read_config(basedir, "client.port")
i2p_provider = mock.Mock() fch = {"tcp": make_tcp_handler()}
tor_provider = mock.Mock() dfh = create_default_connection_handlers(
dfh, fch = create_connection_handlers(None, config, i2p_provider, tor_provider) None,
config,
fch,
)
tub_options = create_tub_options(config) tub_options = create_tub_options(config)
t = FakeTub() t = FakeTub()
with mock.patch("allmydata.node.Tub", return_value=t): with mock.patch("allmydata.node.Tub", return_value=t):
with self.assertRaises(ValueError) as ctx: with self.assertRaises(ValueError) as ctx:
i2p_provider = mock.Mock()
tor_provider = mock.Mock()
create_main_tub(config, tub_options, dfh, fch, i2p_provider, tor_provider) create_main_tub(config, tub_options, dfh, fch, i2p_provider, tor_provider)
self.assertIn( self.assertIn(
"you must choose", "you must choose",
@ -818,13 +825,18 @@ class Listeners(unittest.TestCase):
f.write("tub.location = %s\n" % location) f.write("tub.location = %s\n" % location)
config = client.read_config(basedir, "client.port") config = client.read_config(basedir, "client.port")
i2p_provider = mock.Mock() fch = {"tcp": make_tcp_handler()}
tor_provider = mock.Mock() dfh = create_default_connection_handlers(
dfh, fch = create_connection_handlers(None, config, i2p_provider, tor_provider) None,
config,
fch,
)
tub_options = create_tub_options(config) tub_options = create_tub_options(config)
t = FakeTub() t = FakeTub()
with mock.patch("allmydata.node.Tub", return_value=t): with mock.patch("allmydata.node.Tub", return_value=t):
i2p_provider = mock.Mock()
tor_provider = mock.Mock()
create_main_tub(config, tub_options, dfh, fch, i2p_provider, tor_provider) create_main_tub(config, tub_options, dfh, fch, i2p_provider, tor_provider)
self.assertEqual(t.listening_ports, self.assertEqual(t.listening_ports,
["tcp:%d:interface=127.0.0.1" % port1, ["tcp:%d:interface=127.0.0.1" % port1,
@ -844,11 +856,16 @@ class Listeners(unittest.TestCase):
tub_options = create_tub_options(config) tub_options = create_tub_options(config)
t = FakeTub() t = FakeTub()
i2p_provider = mock.Mock() fch = {"tcp": make_tcp_handler()}
tor_provider = mock.Mock() dfh = create_default_connection_handlers(
dfh, fch = create_connection_handlers(None, config, i2p_provider, tor_provider) None,
config,
fch,
)
with mock.patch("allmydata.node.Tub", return_value=t): with mock.patch("allmydata.node.Tub", return_value=t):
i2p_provider = mock.Mock()
tor_provider = mock.Mock()
create_main_tub(config, tub_options, dfh, fch, i2p_provider, tor_provider) create_main_tub(config, tub_options, dfh, fch, i2p_provider, tor_provider)
self.assertEqual(i2p_provider.get_listener.mock_calls, [mock.call()]) self.assertEqual(i2p_provider.get_listener.mock_calls, [mock.call()])
@ -990,8 +1007,9 @@ class CreateConnectionHandlers(unittest.TestCase):
tcp = disabled tcp = disabled
""")) """))
reactor = object() # it's not actually used?! reactor = object() # it's not actually used?!
provider = FakeProvider() default_handlers = create_default_connection_handlers(
default_handlers, _ = create_connection_handlers( reactor,
reactor, config, provider, provider config,
{},
) )
self.assertIs(default_handlers["tcp"], None) self.assertIs(default_handlers["tcp"], None)