mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2024-12-29 01:08:54 +00:00
more tests work
This commit is contained in:
parent
3d5f8becb5
commit
d8b432700e
@ -5,7 +5,8 @@ from twisted.internet import reactor, endpoints, defer
|
|||||||
from twisted.internet.interfaces import IStreamClientEndpoint
|
from twisted.internet.interfaces import IStreamClientEndpoint
|
||||||
from foolscap.connections import tcp
|
from foolscap.connections import tcp
|
||||||
from ..node import Node, PrivacyError, config_from_string
|
from ..node import Node, PrivacyError, config_from_string
|
||||||
from ..node import create_connection_handlers, create_i2p_provider
|
from ..node import create_connection_handlers
|
||||||
|
from ..node import create_i2p_provider, create_tor_provider
|
||||||
from ..node import create_main_tub, _tub_portlocation
|
from ..node import create_main_tub, _tub_portlocation
|
||||||
from ..client import create_client_from_config
|
from ..client import create_client_from_config
|
||||||
from ..util import connection_status
|
from ..util import connection_status
|
||||||
@ -35,27 +36,36 @@ class TCP(unittest.TestCase):
|
|||||||
tcp.DefaultTCP,
|
tcp.DefaultTCP,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Tor(unittest.TestCase):
|
class Tor(unittest.TestCase):
|
||||||
|
|
||||||
def test_disabled(self):
|
def test_disabled(self):
|
||||||
n = FakeNode(BASECONFIG+"[tor]\nenabled = false\n")
|
config = config_from_string(
|
||||||
h = n._make_tor_handler()
|
BASECONFIG + "[tor]\nenabled = false\n",
|
||||||
|
"fake.port",
|
||||||
|
)
|
||||||
|
tor_provider = create_tor_provider(reactor, 'BASEDIR', config)
|
||||||
|
h = tor_provider.get_tor_handler()
|
||||||
self.assertEqual(h, None)
|
self.assertEqual(h, None)
|
||||||
|
|
||||||
def test_unimportable(self):
|
def test_unimportable(self):
|
||||||
with mock.patch("allmydata.util.tor_provider._import_tor",
|
with mock.patch("allmydata.util.tor_provider._import_tor",
|
||||||
return_value=None):
|
return_value=None):
|
||||||
n = FakeNode(BASECONFIG)
|
config = config_from_string(BASECONFIG, "fake.port")
|
||||||
h = n._make_tor_handler()
|
tor_provider = create_tor_provider(reactor, 'BASEDIR', config)
|
||||||
|
h = tor_provider.get_tor_handler()
|
||||||
self.assertEqual(h, None)
|
self.assertEqual(h, None)
|
||||||
|
|
||||||
def test_default(self):
|
def test_default(self):
|
||||||
h1 = mock.Mock()
|
h1 = mock.Mock()
|
||||||
with mock.patch("foolscap.connections.tor.default_socks",
|
with mock.patch("foolscap.connections.tor.default_socks",
|
||||||
return_value=h1) as f:
|
return_value=h1) as f:
|
||||||
n = FakeNode(BASECONFIG)
|
|
||||||
h = n._make_tor_handler()
|
config = config_from_string(BASECONFIG, "fake.port")
|
||||||
self.assertEqual(f.mock_calls, [mock.call()])
|
tor_provider = create_tor_provider(reactor, 'BASEDIR', config)
|
||||||
self.assertIdentical(h, h1)
|
h = tor_provider.get_tor_handler()
|
||||||
|
self.assertEqual(f.mock_calls, [mock.call()])
|
||||||
|
self.assertIdentical(h, h1)
|
||||||
|
|
||||||
def _do_test_launch(self, executable):
|
def _do_test_launch(self, executable):
|
||||||
# the handler is created right away
|
# the handler is created right away
|
||||||
@ -65,16 +75,18 @@ class Tor(unittest.TestCase):
|
|||||||
h1 = mock.Mock()
|
h1 = mock.Mock()
|
||||||
with mock.patch("foolscap.connections.tor.control_endpoint_maker",
|
with mock.patch("foolscap.connections.tor.control_endpoint_maker",
|
||||||
return_value=h1) as f:
|
return_value=h1) as f:
|
||||||
n = FakeNode(config)
|
|
||||||
h = n._make_tor_handler()
|
config = config_from_string(config, "fake.port")
|
||||||
private_dir = n.config.get_config_path("private")
|
tp = create_tor_provider("reactor", 'BASEDIR', config)
|
||||||
exp = mock.call(n._tor_provider._make_control_endpoint,
|
h = tp.get_tor_handler()
|
||||||
|
|
||||||
|
private_dir = config.get_config_path("private")
|
||||||
|
exp = mock.call(tp._make_control_endpoint,
|
||||||
takes_status=True)
|
takes_status=True)
|
||||||
self.assertEqual(f.mock_calls, [exp])
|
self.assertEqual(f.mock_calls, [exp])
|
||||||
self.assertIdentical(h, h1)
|
self.assertIdentical(h, h1)
|
||||||
|
|
||||||
# later, when Foolscap first connects, Tor should be launched
|
# later, when Foolscap first connects, Tor should be launched
|
||||||
tp = n._tor_provider
|
|
||||||
reactor = "reactor"
|
reactor = "reactor"
|
||||||
tcp = object()
|
tcp = object()
|
||||||
tcep = object()
|
tcep = object()
|
||||||
@ -101,45 +113,77 @@ class Tor(unittest.TestCase):
|
|||||||
h1 = mock.Mock()
|
h1 = mock.Mock()
|
||||||
with mock.patch("foolscap.connections.tor.socks_endpoint",
|
with mock.patch("foolscap.connections.tor.socks_endpoint",
|
||||||
return_value=h1) as f:
|
return_value=h1) as f:
|
||||||
n = FakeNode(BASECONFIG+"[tor]\nsocks.port = unix:/var/lib/fw-daemon/tor_socks.socket\n")
|
config = config_from_string(
|
||||||
h = n._make_tor_handler()
|
BASECONFIG + "[tor]\nsocks.port = unix:/var/lib/fw-daemon/tor_socks.socket\n",
|
||||||
self.assertTrue(IStreamClientEndpoint.providedBy(f.mock_calls[0]))
|
"fake.port",
|
||||||
self.assertIdentical(h, h1)
|
)
|
||||||
|
tor_provider = create_tor_provider(reactor, 'BASEDIR', config)
|
||||||
|
h = tor_provider.get_tor_handler()
|
||||||
|
self.assertTrue(IStreamClientEndpoint.providedBy(f.mock_calls[0]))
|
||||||
|
self.assertIdentical(h, h1)
|
||||||
|
|
||||||
def test_socksport_endpoint(self):
|
def test_socksport_endpoint(self):
|
||||||
h1 = mock.Mock()
|
h1 = mock.Mock()
|
||||||
with mock.patch("foolscap.connections.tor.socks_endpoint",
|
with mock.patch("foolscap.connections.tor.socks_endpoint",
|
||||||
return_value=h1) as f:
|
return_value=h1) as f:
|
||||||
n = FakeNode(BASECONFIG+"[tor]\nsocks.port = tcp:127.0.0.1:1234\n")
|
config = config_from_string(
|
||||||
h = n._make_tor_handler()
|
BASECONFIG + "[tor]\nsocks.port = tcp:127.0.0.1:1234\n",
|
||||||
self.assertTrue(IStreamClientEndpoint.providedBy(f.mock_calls[0]))
|
"fake.port",
|
||||||
self.assertIdentical(h, h1)
|
)
|
||||||
|
tor_provider = create_tor_provider(reactor, 'BASEDIR', config)
|
||||||
|
h = tor_provider.get_tor_handler()
|
||||||
|
self.assertTrue(IStreamClientEndpoint.providedBy(f.mock_calls[0]))
|
||||||
|
self.assertIdentical(h, h1)
|
||||||
|
|
||||||
def test_socksport_endpoint_otherhost(self):
|
def test_socksport_endpoint_otherhost(self):
|
||||||
h1 = mock.Mock()
|
h1 = mock.Mock()
|
||||||
with mock.patch("foolscap.connections.tor.socks_endpoint",
|
with mock.patch("foolscap.connections.tor.socks_endpoint",
|
||||||
return_value=h1) as f:
|
return_value=h1) as f:
|
||||||
n = FakeNode(BASECONFIG+"[tor]\nsocks.port = tcp:otherhost:1234\n")
|
config = config_from_string(
|
||||||
h = n._make_tor_handler()
|
BASECONFIG + "[tor]\nsocks.port = tcp:otherhost:1234\n",
|
||||||
self.assertTrue(IStreamClientEndpoint.providedBy(f.mock_calls[0]))
|
"fake.port",
|
||||||
self.assertIdentical(h, h1)
|
)
|
||||||
|
tor_provider = create_tor_provider(reactor, 'BASEDIR', config)
|
||||||
|
h = tor_provider.get_tor_handler()
|
||||||
|
self.assertTrue(IStreamClientEndpoint.providedBy(f.mock_calls[0]))
|
||||||
|
self.assertIdentical(h, h1)
|
||||||
|
|
||||||
def test_socksport_bad_endpoint(self):
|
def test_socksport_bad_endpoint(self):
|
||||||
n = FakeNode(BASECONFIG+"[tor]\nsocks.port = meow:unsupported\n")
|
config = config_from_string(
|
||||||
e = self.assertRaises(ValueError, n._make_tor_handler)
|
BASECONFIG + "[tor]\nsocks.port = meow:unsupported\n",
|
||||||
self.assertIn("Unknown endpoint type: 'meow'", str(e))
|
"fake.port",
|
||||||
|
)
|
||||||
|
with self.assertRaises(ValueError) as ctx:
|
||||||
|
tor_provider = create_tor_provider(reactor, 'BASEDIR', config)
|
||||||
|
h = tor_provider.get_tor_handler()
|
||||||
|
self.assertIn(
|
||||||
|
"Unknown endpoint type: 'meow'",
|
||||||
|
str(ctx.exception)
|
||||||
|
)
|
||||||
|
|
||||||
def test_socksport_not_integer(self):
|
def test_socksport_not_integer(self):
|
||||||
n = FakeNode(BASECONFIG+"[tor]\nsocks.port = tcp:localhost:kumquat\n")
|
config = config_from_string(
|
||||||
e = self.assertRaises(ValueError, n._make_tor_handler)
|
BASECONFIG + "[tor]\nsocks.port = tcp:localhost:kumquat\n",
|
||||||
self.assertIn("invalid literal for int() with base 10: 'kumquat'", str(e))
|
"fake.port",
|
||||||
|
)
|
||||||
|
with self.assertRaises(ValueError) as ctx:
|
||||||
|
tor_provider = create_tor_provider(reactor, 'BASEDIR', config)
|
||||||
|
h = tor_provider.get_tor_handler()
|
||||||
|
self.assertIn(
|
||||||
|
"invalid literal for int() with base 10: 'kumquat'",
|
||||||
|
str(ctx.exception)
|
||||||
|
)
|
||||||
|
|
||||||
def test_controlport(self):
|
def test_controlport(self):
|
||||||
h1 = mock.Mock()
|
h1 = mock.Mock()
|
||||||
with mock.patch("foolscap.connections.tor.control_endpoint",
|
with mock.patch("foolscap.connections.tor.control_endpoint",
|
||||||
return_value=h1) as f:
|
return_value=h1) as f:
|
||||||
n = FakeNode(BASECONFIG+"[tor]\ncontrol.port = tcp:localhost:1234\n")
|
config = config_from_string(
|
||||||
h = n._make_tor_handler()
|
BASECONFIG + "[tor]\ncontrol.port = tcp:localhost:1234\n",
|
||||||
|
"fake.port",
|
||||||
|
)
|
||||||
|
tor_provider = create_tor_provider(reactor, 'BASEDIR', config)
|
||||||
|
h = tor_provider.get_tor_handler()
|
||||||
self.assertEqual(len(f.mock_calls), 1)
|
self.assertEqual(len(f.mock_calls), 1)
|
||||||
ep = f.mock_calls[0][1][0]
|
ep = f.mock_calls[0][1][0]
|
||||||
self.assertIsInstance(ep, endpoints.TCP4ClientEndpoint)
|
self.assertIsInstance(ep, endpoints.TCP4ClientEndpoint)
|
||||||
|
Loading…
Reference in New Issue
Block a user