def test_foolscap_handlers(self): """ ``create_connection_handlers`` returns a Foolscap connection handlers dictionary mapping ``"tcp"`` to ``foolscap.connections.tcp.DefaultTCP``, ``"tor"`` to the supplied Tor provider's handler, and ``"i2p"`` to the supplied I2P provider's handler. """ config = config_from_string( "fake.port", "no-basedir", BASECONFIG, ) tor_endpoint = object() tor = ConstantAddresses(handler=tor_endpoint) i2p_endpoint = object() i2p = ConstantAddresses(handler=i2p_endpoint) _, foolscap_handlers = create_connection_handlers( config, i2p, tor, ) self.assertThat( foolscap_handlers, MatchesDict({ "tcp": IsInstance(tcp.DefaultTCP), "i2p": Equals(i2p_endpoint), "tor": Equals(tor_endpoint), }), )
def _do_test_connect(self, endpoint, reachable): reactor = object() txi2p = object() args = [] if endpoint: args = ["--i2p-sam-port=%s" % endpoint] cli_config = make_cli_config("basedir", "--listen=i2p", *args) stdout = cli_config.stdout expected_port = "tcp:127.0.0.1:7656" if endpoint: expected_port = endpoint tried = [] def _try_to_connect(reactor, port, stdout, txi2p): tried.append( (reactor, port, stdout, txi2p) ) if not reachable: return defer.succeed(None) if port == expected_port: return defer.succeed(True) return defer.succeed(None) with mock.patch("allmydata.util.i2p_provider._try_to_connect", _try_to_connect): d = i2p_provider._connect_to_i2p(reactor, cli_config, txi2p) if not reachable: f = self.failureResultOf(d) self.assertIsInstance(f.value, ValueError) self.assertEqual(str(f.value), "unable to reach any default I2P SAM port") return successful_port = self.successResultOf(d) self.assertEqual(successful_port, expected_port) expected = [(reactor, "tcp:127.0.0.1:7656", stdout, txi2p)] if endpoint: expected = [(reactor, endpoint, stdout, txi2p)] self.assertEqual(tried, expected)
def test_default(self): default_connection_handlers, _ = create_connection_handlers( self.config, ConstantAddresses(handler=object()), ConstantAddresses(handler=object()), ) self.assertEqual(default_connection_handlers["tcp"], "tcp") self.assertEqual(default_connection_handlers["tor"], "tor") self.assertEqual(default_connection_handlers["i2p"], "i2p")
def _do_test_launch(self, executable): basedir = self.mktemp() os.mkdir(basedir) private_dir = os.path.join(basedir, "private") os.mkdir(private_dir) reactor = object() args = ["--listen=tor", "--tor-launch"] if executable: args.append("--tor-executable=%s" % executable) cli_config = make_cli_config(basedir, *args) protocol = object() launch_tor = mock.Mock(return_value=defer.succeed(("control_endpoint", protocol))) txtorcon = mock.Mock() ehs = mock.Mock() # This appears to be a native string in the real txtorcon object... ehs.private_key = ensure_str("privkey") ehs.hostname = "ONION.onion" txtorcon.EphemeralHiddenService = mock.Mock(return_value=ehs) ehs.add_to_tor = mock.Mock(return_value=defer.succeed(None)) ehs.remove_from_tor = mock.Mock(return_value=defer.succeed(None)) with mock_txtorcon(txtorcon): with mock.patch("allmydata.util.tor_provider._launch_tor", launch_tor): with mock.patch( "allmydata.util.tor_provider.allocate_tcp_port", return_value=999999): d = tor_provider.create_config(reactor, cli_config) tahoe_config_tor, tor_port, tor_location = self.successResultOf(d) launch_tor.assert_called_with(reactor, executable, os.path.abspath(private_dir), txtorcon) txtorcon.EphemeralHiddenService.assert_called_with( "3457 127.0.0.1:999999") ehs.add_to_tor.assert_called_with(protocol) ehs.remove_from_tor.assert_called_with(protocol) expected = { "launch": "true", "onion": "true", "onion.local_port": "999999", "onion.external_port": "3457", "onion.private_key_file": os.path.join("private", "tor_onion.privkey"), } if executable: expected["tor.executable"] = executable self.assertEqual(tahoe_config_tor, expected) self.assertEqual(tor_port, "tcp:999999:interface=127.0.0.1") self.assertEqual(tor_location, "tor:ONION.onion:3457") fn = os.path.join(basedir, tahoe_config_tor["onion.private_key_file"]) with open(fn, "rb") as f: privkey = f.read() self.assertEqual(privkey, b"privkey")
def test_handler_default(self): i2p = mock.Mock() handler = object() i2p.default = mock.Mock(return_value=handler) reactor = object() with mock_i2p(i2p): p = i2p_provider.create(reactor, FakeConfig()) h = p.get_i2p_handler() self.assertIs(h, handler) i2p.default.assert_called_with(reactor, keyfile=None)
def test_handler_launch(self): i2p = mock.Mock() handler = object() i2p.launch = mock.Mock(return_value=handler) reactor = object() with mock_i2p(i2p): p = i2p_provider.create(reactor, FakeConfig(launch=True)) h = p.get_i2p_handler() self.assertIs(h, handler) i2p.launch.assert_called_with(i2p_configdir=None, i2p_binary=None)
def test_handler_configdir(self): i2p = mock.Mock() handler = object() i2p.local_i2p = mock.Mock(return_value=handler) reactor = object() with mock_i2p(i2p): p = i2p_provider.create(reactor, FakeConfig(**{"i2p.configdir": "configdir"})) h = p.get_i2p_handler() i2p.local_i2p.assert_called_with("configdir") self.assertIs(h, handler)
def test_connections_tcp_disabled(self): config = config_from_string( "no-basedir", "fake.port", BASECONFIG + "[connections]\ntcp = disabled\n" + "[node]\nreveal-IP-address = false\n", ) default_connection_handlers, _ = create_connection_handlers( config, ConstantAddresses(handler=object()), ConstantAddresses(handler=object()), ) self.assertEqual(default_connection_handlers["tcp"], None)
def test_tcp_disabled(self): config = config_from_string( "fake.port", "no-basedir", BASECONFIG + "[connections]\ntcp = disabled\n", ) default_connection_handlers, _ = create_connection_handlers( config, ConstantAddresses(handler=object()), ConstantAddresses(handler=object()), ) self.assertEqual(default_connection_handlers["tcp"], None) self.assertEqual(default_connection_handlers["tor"], "tor") self.assertEqual(default_connection_handlers["i2p"], "i2p")
def test_try(self): reactor = object() txi2p = mock.Mock() d = defer.succeed(True) txi2p.testAPI = mock.Mock(return_value=d) ep = object() stdout = StringIO() with mock.patch("allmydata.util.i2p_provider.clientFromString", return_value=ep) as cfs: d = i2p_provider._try_to_connect(reactor, "desc", stdout, txi2p) r = self.successResultOf(d) self.assertTrue(r) cfs.assert_called_with(reactor, "desc") txi2p.testAPI.assert_called_with(reactor, 'SAM', ep)
def test_try(self): reactor = object() txtorcon = mock.Mock() tor_state = object() d = defer.succeed(tor_state) txtorcon.build_tor_connection = mock.Mock(return_value=d) ep = object() stdout = StringIO() with mock.patch("allmydata.util.tor_provider.clientFromString", return_value=ep) as cfs: d = tor_provider._try_to_connect(reactor, "desc", stdout, txtorcon) r = self.successResultOf(d) self.assertIs(r, tor_state) cfs.assert_called_with(reactor, "desc") txtorcon.build_tor_connection.assert_called_with(ep)
def test_handler_launch_configdir_executable(self): i2p = mock.Mock() handler = object() i2p.launch = mock.Mock(return_value=handler) reactor = object() with mock_i2p(i2p): p = i2p_provider.create(reactor, FakeConfig(launch=True, **{"i2p.configdir": "configdir", "i2p.executable": "myi2p", })) h = p.get_i2p_handler() self.assertIs(h, handler) i2p.launch.assert_called_with(i2p_configdir="configdir", i2p_binary="myi2p")
def test_unknown(self): config = config_from_string( "fake.port", "no-basedir", BASECONFIG + "[connections]\ntcp = unknown\n", ) with self.assertRaises(ValueError) as ctx: create_connection_handlers( config, ConstantAddresses(handler=object()), ConstantAddresses(handler=object()), ) self.assertIn("'tahoe.cfg [connections] tcp='", str(ctx.exception)) self.assertIn("uses unknown handler type 'unknown'", str(ctx.exception))
def test_control_endpoint(self): basedir = self.mktemp() os.mkdir(basedir) private_dir = os.path.join(basedir, "private") os.mkdir(private_dir) reactor = object() cli_config = make_cli_config(basedir, "--listen=tor") protocol = object() connect_to_tor = mock.Mock(return_value=defer.succeed(("goodport", protocol))) txtorcon = mock.Mock() ehs = mock.Mock() ehs.private_key = b"privkey" ehs.hostname = "ONION.onion" txtorcon.EphemeralHiddenService = mock.Mock(return_value=ehs) ehs.add_to_tor = mock.Mock(return_value=defer.succeed(None)) ehs.remove_from_tor = mock.Mock(return_value=defer.succeed(None)) with mock_txtorcon(txtorcon): with mock.patch("allmydata.util.tor_provider._connect_to_tor", connect_to_tor): with mock.patch( "allmydata.util.tor_provider.allocate_tcp_port", return_value=999999): d = tor_provider.create_config(reactor, cli_config) tahoe_config_tor, tor_port, tor_location = self.successResultOf(d) connect_to_tor.assert_called_with(reactor, cli_config, txtorcon) txtorcon.EphemeralHiddenService.assert_called_with( "3457 127.0.0.1:999999") ehs.add_to_tor.assert_called_with(protocol) ehs.remove_from_tor.assert_called_with(protocol) expected = { "control.port": "goodport", "onion": "true", "onion.local_port": "999999", "onion.external_port": "3457", "onion.private_key_file": os.path.join("private", "tor_onion.privkey"), } self.assertEqual(tahoe_config_tor, expected) self.assertEqual(tor_port, "tcp:999999:interface=127.0.0.1") self.assertEqual(tor_location, "tor:ONION.onion:3457") fn = os.path.join(basedir, tahoe_config_tor["onion.private_key_file"]) with open(fn, "rb") as f: privkey = f.read() self.assertEqual(privkey, b"privkey")
def test_types_isinstance_newobject(self): a = list() b = dict() c = set() self.assertTrue(isinstance(a, object)) self.assertTrue(isinstance(b, object)) self.assertTrue(isinstance(c, object)) # Old-style class instances on Py2 should still report as an instance # of object as usual on Py2: class D: pass d = D() self.assertTrue(isinstance(d, object)) e = object() self.assertTrue(isinstance(e, object)) class F(object): pass f = F() self.assertTrue(isinstance(f, object)) class G(F): pass g = G() self.assertTrue(isinstance(g, object)) class H(): pass h = H() self.assertTrue(isinstance(h, object))
def test_tor_unimportable(self): """ If the configuration calls for substituting Tor for TCP and ``foolscap.connections.tor`` is not importable then ``create_connection_handlers`` raises ``ValueError`` with a message explaining this makes Tor unusable. """ self.config = config_from_string( "fake.port", "no-basedir", BASECONFIG + "[connections]\ntcp = tor\n", ) tor_provider = create_tor_provider( reactor, self.config, import_tor=lambda: None, ) with self.assertRaises(ValueError) as ctx: default_connection_handlers, _ = create_connection_handlers( self.config, i2p_provider=ConstantAddresses(handler=object()), tor_provider=tor_provider, ) self.assertEqual( str(ctx.exception), "'tahoe.cfg [connections] tcp='" " uses unavailable/unimportable handler type 'tor'." " Please pip install tahoe-lafs[tor] to fix.", )
def test_try_unhandled_error(self): reactor = object() txi2p = mock.Mock() d = defer.fail(ValueError("oops")) txi2p.testAPI = mock.Mock(return_value=d) ep = object() stdout = StringIO() with mock.patch("allmydata.util.i2p_provider.clientFromString", return_value=ep) as cfs: d = i2p_provider._try_to_connect(reactor, "desc", stdout, txi2p) f = self.failureResultOf(d) self.assertIsInstance(f.value, ValueError) self.assertEqual(str(f.value), "oops") cfs.assert_called_with(reactor, "desc") txi2p.testAPI.assert_called_with(reactor, 'SAM', ep) self.assertEqual(stdout.getvalue(), "")
def test_handler_sam_endpoint(self): i2p = mock.Mock() handler = object() i2p.sam_endpoint = mock.Mock(return_value=handler) ep = object() reactor = object() with mock_i2p(i2p): p = i2p_provider.create(reactor, FakeConfig(**{"sam.port": "ep_desc"})) with mock.patch("allmydata.util.i2p_provider.clientFromString", return_value=ep) as cfs: h = p.get_i2p_handler() cfs.assert_called_with(reactor, "ep_desc") self.assertIs(h, handler) i2p.sam_endpoint.assert_called_with(ep, keyfile=None)
def test_handler_launch(self): reactor = object() tor = mock.Mock() txtorcon = mock.Mock() handler = object() tor.control_endpoint_maker = mock.Mock(return_value=handler) tor.add_context = mock.Mock(return_value=EmptyContext()) with mock_tor(tor): with mock_txtorcon(txtorcon): p = tor_provider.create(reactor, FakeConfig(launch=True)) h = p.get_tor_handler() self.assertIs(h, handler) tor.control_endpoint_maker.assert_called_with(p._make_control_endpoint, takes_status=True) # make sure Tor is launched just once, the first time an endpoint is # requested, and never again. The clientFromString() function is # called once each time. ep_desc = object() launch_tor = mock.Mock(return_value=defer.succeed((ep_desc, None))) ep = object() cfs = mock.Mock(return_value=ep) with mock.patch("allmydata.util.tor_provider._launch_tor", launch_tor): with mock.patch("allmydata.util.tor_provider.clientFromString", cfs): d = p._make_control_endpoint(reactor, update_status=lambda status: None) yield flushEventualQueue() self.assertIs(self.successResultOf(d), ep) launch_tor.assert_called_with( reactor, None, os.path.join("basedir", "private"), txtorcon) cfs.assert_called_with(reactor, ep_desc) launch_tor2 = mock.Mock(return_value=defer.succeed((ep_desc, None))) cfs2 = mock.Mock(return_value=ep) with mock.patch("allmydata.util.tor_provider._launch_tor", launch_tor2): with mock.patch("allmydata.util.tor_provider.clientFromString", cfs2): d2 = p._make_control_endpoint( reactor, update_status=lambda status: None) yield flushEventualQueue() self.assertIs(self.successResultOf(d2), ep) self.assertEqual(launch_tor2.mock_calls, []) cfs2.assert_called_with(reactor, ep_desc)
def test_handler_control_endpoint(self): tor = mock.Mock() handler = object() tor.control_endpoint = mock.Mock(return_value=handler) ep = object() cfs = mock.Mock(return_value=ep) reactor = object() with mock_tor(tor): p = tor_provider.create(reactor, FakeConfig(**{"control.port": "ep_desc"})) with mock.patch("allmydata.util.tor_provider.clientFromString", cfs): h = p.get_tor_handler() self.assertIs(h, handler) cfs.assert_called_with(reactor, "ep_desc") tor.control_endpoint.assert_called_with(ep)
def test_try_handled_error(self): reactor = object() txi2p = mock.Mock() d = defer.fail(error.ConnectError("oops")) txi2p.testAPI = mock.Mock(return_value=d) ep = object() stdout = StringIO() with mock.patch("allmydata.util.i2p_provider.clientFromString", return_value=ep) as cfs: d = i2p_provider._try_to_connect(reactor, "desc", stdout, txi2p) r = self.successResultOf(d) self.assertIs(r, None) cfs.assert_called_with(reactor, "desc") txi2p.testAPI.assert_called_with(reactor, 'SAM', ep) self.assertEqual(stdout.getvalue(), "Unable to reach I2P SAM API at 'desc': " "An error occurred while connecting: oops.\n")
def test_non_iterator(self): """ The default behaviour of next(o) for a newobject o should be to raise a TypeError, as with the corresponding builtin object. """ o = object() with self.assertRaises(TypeError): next(o)
def test_return_value(self, logger): """ The decorated function's return value is passed through. """ result = object() @log_call_deferred(action_type=u"the-action") def f(): return result self.assertThat(f(), succeeded(Is(result)))
def test_cannot_assign_new_attributes_to_object(self): """ New attributes cannot be assigned to object() instances in Python. The same should apply to newobject. """ from builtins import object with self.assertRaises(AttributeError): object().arbitrary_attribute_name = True
def _do_test_connect(self, endpoint, reachable): reactor = object() txtorcon = object() args = [] if endpoint: args = ["--tor-control-port=%s" % endpoint] cli_config = make_cli_config("basedir", "--listen=tor", *args) stdout = cli_config.stdout expected_port = "tcp:127.0.0.1:9151" if endpoint: expected_port = endpoint tor_state = mock.Mock tor_state.protocol = object() tried = [] def _try_to_connect(reactor, port, stdout, txtorcon): tried.append((reactor, port, stdout, txtorcon)) if not reachable: return defer.succeed(None) if port == expected_port: # second one on the list return defer.succeed(tor_state) return defer.succeed(None) with mock.patch("allmydata.util.tor_provider._try_to_connect", _try_to_connect): d = tor_provider._connect_to_tor(reactor, cli_config, txtorcon) if not reachable: f = self.failureResultOf(d) self.assertIsInstance(f.value, ValueError) self.assertEqual(str(f.value), "unable to reach any default Tor control port") return successful_port, tor_control_proto = self.successResultOf(d) self.assertEqual(successful_port, expected_port) self.assertIs(tor_control_proto, tor_state.protocol) expected = [ (reactor, "unix:/var/run/tor/control", stdout, txtorcon), (reactor, "tcp:127.0.0.1:9051", stdout, txtorcon), (reactor, "tcp:127.0.0.1:9151", stdout, txtorcon), ] if endpoint: expected = [(reactor, endpoint, stdout, txtorcon)] self.assertEqual(tried, expected)
def test_connections(self): config = config_from_string( "fake.port", "no-basedir", BASECONFIG + "[node]\nreveal-IP-address = false\n", ) with self.assertRaises(PrivacyError) as ctx: create_connection_handlers( config, ConstantAddresses(handler=object()), ConstantAddresses(handler=object()), ) self.assertEqual( str(ctx.exception), "Privacy requested with `reveal-IP-address = false` " "but `tcp = tcp` conflicts with this.", )
def test_handler_default(self): tor = mock.Mock() handler = object() tor.default_socks = mock.Mock(return_value=handler) with mock_tor(tor): p = tor_provider.create("reactor", FakeConfig()) h = p.get_tor_handler() self.assertIs(h, handler) tor.default_socks.assert_called_with()
def test_stdout(self): """ A ``file:`` description with a path of ``-`` causes logs to be written to stdout. """ reactor = object() self.assertThat( _parse_destination_description("file:-")(reactor), Equals(FileDestination(stdout, encoder=AnyBytesJSONEncoder)), )
def test_enabled_plugin(self): """ An announcement that could be matched by a plugin that is enabled with configuration is matched and the plugin's storage client is used. """ plugin_config = { "abc": "xyz", } plugin_name = "tahoe-lafs-dummy-v1" yield self.make_node( introducer_furl=SOME_FURL, storage_plugin=plugin_name, plugin_config=plugin_config, ) server_id = b"v0-abcdef" ann = { u"service-name": u"storage", u"storage-options": [{ # and this announcement is for a plugin with a matching name u"name": plugin_name, u"storage-server-FURL": SOME_FURL.decode("ascii"), }], } self.publish(server_id, ann, self.introducer_client) storage = self.get_storage(server_id, self.node) self.assertTrue(verifyObject( IFoolscapStorageServer, storage, ), ) expected_rref = object() # Can't easily establish a real Foolscap connection so fake the result # of doing so... self.set_rref(server_id, self.node, expected_rref) self.expectThat( storage.storage_server, MatchesAll( IsInstance(DummyStorageClient), MatchesStructure( get_rref=AfterPreprocessing( lambda get_rref: get_rref(), Is(expected_rref), ), configuration=Equals(plugin_config), announcement=Equals({ u'name': plugin_name, u'storage-server-FURL': u'pb://abcde@nowhere/fake', }), ), ), )
def test_control_endpoint(self): basedir = self.mktemp() os.mkdir(basedir) fn = os.path.join(basedir, "keyfile") with open(fn, "w") as f: f.write("private key") reactor = object() cfg = FakeConfig(basedir=basedir, onion=True, **{ "control.port": "ep_desc", "onion.local_port": 123, "onion.external_port": 456, "onion.private_key_file": "keyfile", }) txtorcon = mock.Mock() with mock_txtorcon(txtorcon): p = tor_provider.create(reactor, cfg) tor_state = mock.Mock() tor_state.protocol = object() txtorcon.build_tor_connection = mock.Mock(return_value=tor_state) ehs = mock.Mock() ehs.add_to_tor = mock.Mock(return_value=defer.succeed(None)) ehs.remove_from_tor = mock.Mock(return_value=defer.succeed(None)) txtorcon.EphemeralHiddenService = mock.Mock(return_value=ehs) tcep = object() cfs = mock.Mock(return_value=tcep) with mock.patch("allmydata.util.tor_provider.clientFromString", cfs): d = p.startService() yield flushEventualQueue() self.successResultOf(d) self.assertIs(p._onion_ehs, ehs) self.assertIs(p._onion_tor_control_proto, tor_state.protocol) cfs.assert_called_with(reactor, "ep_desc") txtorcon.build_tor_connection.assert_called_with(tcep) txtorcon.EphemeralHiddenService.assert_called_with( "456 127.0.0.1:123", b"private key") ehs.add_to_tor.assert_called_with(tor_state.protocol) yield p.stopService() ehs.remove_from_tor.assert_called_with(tor_state.protocol)
def test_bool_empty_object(self): """ The default result of bool(newobject()) should be True, as with builtin objects. """ o = object() self.assertTrue(bool(o)) class MyClass(object): pass obj = MyClass() self.assertTrue(bool(obj))
from future.backports.email import errors from future.backports.email import message from future.backports.email._policybase import compat32 NLCRE = re.compile('\r\n|\r|\n') NLCRE_bol = re.compile('(\r\n|\r|\n)') NLCRE_eol = re.compile('(\r\n|\r|\n)\Z') NLCRE_crack = re.compile('(\r\n|\r|\n)') # RFC 2822 $3.6.8 Optional fields. ftext is %d33-57 / %d59-126, Any character # except controls, SP, and ":". headerRE = re.compile(r'^(From |[\041-\071\073-\176]{1,}:|[\t ])') EMPTYSTRING = '' NL = '\n' NeedMoreData = object() # @implements_iterator class BufferedSubFile(object): """A file-ish object that can have new data loaded into it. You can also push and pop line-matching predicates onto a stack. When the current predicate matches the current line, a false EOF response (i.e. empty string) is returned instead. This lets the parser adhere to a simple abstraction -- it parses until EOF closes the current message. """ def __init__(self): # The last partial line pushed into this object. self._partial = '' # The list of full, pushed lines, in reverse order