def _make_i2p_handler(self): enabled = self.get_config("i2p", "enabled", True, boolean=True) if not enabled: return None i2p = _import_i2p() if not i2p: return None samport = self.get_config("i2p", "sam.port", None) launch = self.get_config("i2p", "launch", False, boolean=True) configdir = self.get_config("i2p", "i2p.configdir", None) if samport: if launch: raise ValueError("tahoe.cfg [i2p] must not set both " "sam.port and launch") ep = endpoints.clientFromString(reactor, samport) return i2p.sam_endpoint(ep) if launch: executable = self.get_config("i2p", "i2p.executable", None) return i2p.launch(i2p_configdir=configdir, i2p_binary=executable) if configdir: return i2p.local_i2p(configdir) return i2p.default(reactor)
def test_default_with_portnum_kwarg(self): # setting extra kwargs on the handler should provide a default for # the portnum. sequential calls with/without portnums in the hints # should get the right values. h = i2p.default(reactor, port=1234) with mock.patch( "foolscap.connections.i2p.SAMI2PStreamClientEndpoint") as sep: sep.new = n = mock.Mock() yield h.hint_to_endpoint("i2p:fppym.b32.i2p", reactor, discard_status) got_portnum = n.mock_calls[0][1][2] self.failUnlessEqual(got_portnum, 1234) with mock.patch( "foolscap.connections.i2p.SAMI2PStreamClientEndpoint") as sep: sep.new = n = mock.Mock() yield h.hint_to_endpoint("i2p:fppym.b32.i2p:3456", reactor, discard_status) got_portnum = n.mock_calls[0][1][2] self.failUnlessEqual(got_portnum, 3456) with mock.patch( "foolscap.connections.i2p.SAMI2PStreamClientEndpoint") as sep: sep.new = n = mock.Mock() yield h.hint_to_endpoint("i2p:fppym.b32.i2p", reactor, discard_status) got_portnum = n.mock_calls[0][1][2] self.failUnlessEqual(got_portnum, 1234)
def test_default(self): with mock.patch("foolscap.connections.i2p.SAMI2PStreamClientEndpoint") as sep: sep.new = n = mock.Mock() n.return_value = expected_ep = object() h = i2p.default(reactor) res = yield h.hint_to_endpoint("i2p:fppym.b32.i2p", reactor) self.assertEqual(len(n.mock_calls), 1) args = n.mock_calls[0][1] got_sep, got_host, got_portnum = args self.assertIsInstance(got_sep, endpoints.TCP4ClientEndpoint) self.failUnlessEqual(got_sep._host, "127.0.0.1") # fragile self.failUnlessEqual(got_sep._port, 7656) self.failUnlessEqual(got_host, "fppym.b32.i2p") self.failUnlessEqual(got_portnum, None) ep, host = res self.assertIdentical(ep, expected_ep) self.assertEqual(host, "fppym.b32.i2p")
def test_default_with_portnum(self): # I2P addresses generally don't use port numbers, but the parser is # supposed to handle them with mock.patch("foolscap.connections.i2p.SAMI2PStreamClientEndpoint") as sep: sep.new = n = mock.Mock() n.return_value = expected_ep = object() h = i2p.default(reactor) res = yield h.hint_to_endpoint("i2p:fppym.b32.i2p:1234", reactor) self.assertEqual(len(n.mock_calls), 1) args = n.mock_calls[0][1] got_sep, got_host, got_portnum = args self.assertIsInstance(got_sep, endpoints.TCP4ClientEndpoint) self.failUnlessEqual(got_sep._host, "127.0.0.1") # fragile self.failUnlessEqual(got_sep._port, 7656) self.failUnlessEqual(got_host, "fppym.b32.i2p") self.failUnlessEqual(got_portnum, 1234) ep, host = res self.assertIdentical(ep, expected_ep) self.assertEqual(host, "fppym.b32.i2p")
def test_default_with_portnum(self): # I2P addresses generally don't use port numbers, but the parser is # supposed to handle them with mock.patch( "foolscap.connections.i2p.SAMI2PStreamClientEndpoint") as sep: sep.new = n = mock.Mock() n.return_value = expected_ep = object() h = i2p.default(reactor) res = yield h.hint_to_endpoint("i2p:fppym.b32.i2p:1234", reactor, discard_status) self.assertEqual(len(n.mock_calls), 1) args = n.mock_calls[0][1] got_sep, got_host, got_portnum = args self.assertIsInstance(got_sep, endpoints.TCP4ClientEndpoint) self.failUnlessEqual(got_sep._host, "127.0.0.1") # fragile self.failUnlessEqual(got_sep._port, 7656) self.failUnlessEqual(got_host, "fppym.b32.i2p") self.failUnlessEqual(got_portnum, 1234) ep, host = res self.assertIdentical(ep, expected_ep) self.assertEqual(host, "fppym.b32.i2p")
def test_default(self): with mock.patch( "foolscap.connections.i2p.SAMI2PStreamClientEndpoint") as sep: sep.new = n = mock.Mock() n.return_value = expected_ep = object() h = i2p.default(reactor, misc_kwarg="foo") res = yield h.hint_to_endpoint("i2p:fppym.b32.i2p", reactor, discard_status) self.assertEqual(len(n.mock_calls), 1) args = n.mock_calls[0][1] got_sep, got_host, got_portnum = args self.assertIsInstance(got_sep, endpoints.TCP4ClientEndpoint) self.failUnlessEqual(got_sep._host, "127.0.0.1") # fragile self.failUnlessEqual(got_sep._port, 7656) self.failUnlessEqual(got_host, "fppym.b32.i2p") self.failUnlessEqual(got_portnum, None) kwargs = n.mock_calls[0][2] self.failUnlessEqual(kwargs, {"misc_kwarg": "foo"}) ep, host = res self.assertIdentical(ep, expected_ep) self.assertEqual(host, "fppym.b32.i2p") self.assertEqual(h.describe(), "i2p")
elif which == "tor-control": control_ep = clientFromString(reactor, sys.argv[2]) h = tor.control_endpoint(control_ep) elif which == "tor-launch": data_directory = None if len(sys.argv) > 2: data_directory = os.path.abspath(sys.argv[2]) h = tor.launch(data_directory) tub.removeAllConnectionHintHandlers() tub.addConnectionHintHandler("tor", h) furl = "pb://%s@tor:%s:%d/calculator" % (TUBID, ONION, ONIONPORT) elif which in ("i2p-default", "i2p-sam"): from foolscap.connections import i2p if which == "i2p-default": h = i2p.default(reactor) else: sam_ep = clientFromString(reactor, sys.argv[2]) h = i2p.sam_endpoint(sam_ep) tub.removeAllConnectionHintHandlers() tub.addConnectionHintHandler("i2p", h) furl = "pb://%s@i2p:%s:%d/calculator" % (TUBID, I2P, I2PPORT) else: print "run as 'check-connections-client.py [tcp|socks|tor-default|tor-socks|tor-control|tor-launch|i2p-default|i2p-sam]'" sys.exit(1) print "using %s: %s" % (which, furl) class Observer(Referenceable): def remote_event(self, msg): pass
def test_default_badhint(self): h = i2p.default(reactor) d = defer.maybeDeferred(h.hint_to_endpoint, "i2p:not@a@hint", reactor) f = self.failureResultOf(d, InvalidHintError) self.assertEqual(str(f.value), "unrecognized I2P hint")
h = tor.socks_port(int(sys.argv[2])) elif which == "tor-control": control_ep = clientFromString(reactor, sys.argv[2]) h = tor.control_endpoint(control_ep) elif which == "tor-launch": data_directory = None if len(sys.argv) > 2: data_directory = os.path.abspath(sys.argv[2]) h = tor.launch(data_directory) tub.removeAllConnectionHintHandlers() tub.addConnectionHintHandler("tor", h) furl = "pb://%s@tor:%s:%d/calculator" % (TUBID, ONION, ONIONPORT) elif which in ("i2p-default", "i2p-sam"): from foolscap.connections import i2p if which == "i2p-default": h = i2p.default(reactor) else: sam_ep = clientFromString(reactor, sys.argv[2]) h = i2p.sam_endpoint(sam_ep) tub.removeAllConnectionHintHandlers() tub.addConnectionHintHandler("i2p", h) furl = "pb://%s@i2p:%s:%d/calculator" % (TUBID, I2P, I2PPORT) else: print "run as 'check-connections-client.py [tcp|socks|tor-default|tor-socks|tor-control|tor-launch|i2p-default|i2p-sam]'" sys.exit(1) print "using %s: %s" % (which, furl) class Observer(Referenceable): def remote_event(self, msg): pass
def test_default_badhint(self): h = i2p.default(reactor) d = defer.maybeDeferred(h.hint_to_endpoint, "i2p:not@a@hint", reactor, discard_status) f = self.failureResultOf(d, InvalidHintError) self.assertEqual(str(f.value), "unrecognized I2P hint")