def connectedServerAndClient(self, serverFactory, clientFactory): """ Set up an in-memory connection between protocols created by C{serverFactory} and C{clientFactory}. @return: A three-tuple. The first element is the protocol created by C{serverFactory}. The second element is the protocol created by C{clientFactory}. The third element is the L{IOPump} connecting them. """ clientProtocol = clientFactory.buildProtocol(None) serverProtocol = serverFactory.buildProtocol(None) clientTransport = AbortableFakeTransport( clientProtocol, isServer=False, hostAddress=self.clientAddress, peerAddress=self.serverAddress) serverTransport = AbortableFakeTransport( serverProtocol, isServer=True, hostAddress=self.serverAddress, peerAddress=self.clientAddress) pump = connect(serverProtocol, serverTransport, clientProtocol, clientTransport) return serverProtocol, clientProtocol, pump
def test_connect_socks_illegal_packet(self): class BadSocksServer(Protocol): def __init__(self): self._buffer = b'' def dataReceived(self, data): self._buffer += data if len(self._buffer) == 3: assert self._buffer == b'\x05\x01\x00' self._buffer = b'' self.transport.write(b'\x05\x01\x01') factory = socks._TorSocksFactory(u'meejah.ca', 1234, 'CONNECT', Mock()) server_proto = BadSocksServer() server_transport = FakeTransport(server_proto, isServer=True) client_proto = factory.buildProtocol('ignored') client_transport = FakeTransport(client_proto, isServer=False) pump = yield connect( server_proto, server_transport, client_proto, client_transport, ) self.assertTrue(server_proto.transport.disconnected) self.assertTrue(client_proto.transport.disconnected) pump.flush()
def test_connect_socks_unknown_version(self): class BadSocksServer(Protocol): def __init__(self): self._buffer = b'' self._recv_stack = [ (b'\x05\x01\x00', b'\x05\xff'), ] def dataReceived(self, data): self._buffer += data if len(self._recv_stack) == 0: assert "not expecting any more data, got {}".format(repr(self._buffer)) return expecting, to_send = self._recv_stack.pop(0) got = self._buffer[:len(expecting)] self._buffer = self._buffer[len(expecting):] assert got == expecting, "wanted {} but got {}".format(repr(expecting), repr(got)) self.transport.write(to_send) factory = socks._TorSocksFactory(u'1.2.3.4', 1234, 'CONNECT', Mock()) server_proto = BadSocksServer() server_transport = FakeTransport(server_proto, isServer=True) client_proto = factory.buildProtocol('ignored') client_transport = FakeTransport(client_proto, isServer=False) # returns IOPump yield connect( server_proto, server_transport, client_proto, client_transport, ) self.assertTrue(server_proto.transport.disconnected) self.assertTrue(client_proto.transport.disconnected)
def accept(self, host_pattern): """ Accept a pending connection request. :param str host_pattern: :func:`fnmatch.fnmatchcase` pattern against which the connection request must match. :returns: `KafkaConnection` """ host, port, protocolFactory, d = self._match(host_pattern) peerAddress = _DebugAddress(host, port) client_protocol = protocolFactory.buildProtocol(peerAddress) assert client_protocol is not None server_protocol = KafkaBrokerProtocol() client_transport = iosim.FakeTransport(client_protocol, isServer=False, peerAddress=peerAddress) server_transport = iosim.FakeTransport(server_protocol, isServer=True) pump = iosim.connect(server_protocol, server_transport, client_protocol, client_transport) self._pumps.append(pump) d.callback(client_protocol) return KafkaConnection( server=server_protocol, client=client_protocol, pump=pump, )
def _testStreamingProducer(self, mode): """ Connect a couple protocol/transport pairs to an L{IOPump} and then pump it. Verify that a streaming producer registered with one of the transports does not receive invalid L{IPushProducer} method calls and ends in the right state. @param mode: C{u"server"} to test a producer registered with the server transport. C{u"client"} to test a producer registered with the client transport. """ serverProto = Protocol() serverTransport = FakeTransport(serverProto, isServer=True) clientProto = Protocol() clientTransport = FakeTransport(clientProto, isServer=False) pump = connect( serverProto, serverTransport, clientProto, clientTransport, greet=False, ) producer = StrictPushProducer() victim = { u"server": serverTransport, u"client": clientTransport, }[mode] victim.registerProducer(producer, streaming=True) pump.pump() self.assertEqual(u"running", producer._state)
def test_adds_user(self): """ When L{UserAdder} is connected to L{IdentityAdmin}, the L{AddUser} command is called and L{IdentityAdmin} adds the user to its factory's store. """ admin = IdentityAdmin() admin.factory = self.adminFactory serverTransport = makeFakeServer(admin) serverTransport.getQ2QHost = lambda: Q2QAddress('Q2Q Host') client = AMP() pump = connect(admin, serverTransport, client, makeFakeClient(client)) d = client.callRemote(AddUser, name='q2q username', password='******') pump.flush() # The username and password are added, along with the domain=q2q # host, to the IdentityAdmin's factory's store self.assertEqual([call('Q2Q Host', 'q2q username', 'q2q password')], self.addUser.calls) # The server responds with {} self.assertEqual({}, self.successResultOf(d))
def request(self, method, uri, headers=None, bodyProducer=None): """ Implement IAgent.request. """ # We want to use Agent to parse the HTTP response, so let's ask it to # make a request against our in-memory reactor. response = self._realAgent.request(method, uri, headers, bodyProducer) # If the request has already finished, just propagate the result. In # reality this would only happen in failure, but if the agent ever adds # a local cache this might be a success. already_called = [] def check_already_called(r): already_called.append(r) return r response.addBoth(check_already_called) if already_called: return response # That will try to establish an HTTP connection with the reactor's # connectTCP method, and MemoryReactor will place Agent's factory into # the tcpClients list. Alternately, it will try to establish an HTTPS # connection with the reactor's connectSSL method, and MemoryReactor # will place it into the sslClients list. We'll extract that. if PY3: scheme = URLPath.fromBytes(uri).scheme else: scheme = URLPath.fromString(uri).scheme host, port, factory, timeout, bindAddress = ( self._memoryReactor.tcpClients[-1]) serverAddress = IPv4Address('TCP', '127.0.0.1', port) clientAddress = IPv4Address('TCP', '127.0.0.1', 31337) # Create the protocol and fake transport for the client and server, # using the factory that was passed to the MemoryReactor for the # client, and a Site around our rootResource for the server. serverProtocol = Site(self._rootResource).buildProtocol(None) serverTransport = iosim.FakeTransport( serverProtocol, isServer=True, hostAddress=serverAddress, peerAddress=clientAddress) clientProtocol = factory.buildProtocol(None) clientTransport = iosim.FakeTransport( clientProtocol, isServer=False, hostAddress=clientAddress, peerAddress=serverAddress) if scheme == b"https": # Provide ISSLTransport on both transports, so everyone knows that # this is HTTPS. directlyProvides(serverTransport, ISSLTransport) directlyProvides(clientTransport, ISSLTransport) # Make a pump for wiring the client and server together. pump = iosim.connect( serverProtocol, serverTransport, clientProtocol, clientTransport) self._pumps.add(pump) return response
def connect_application_session(server_factory, MyApplicationSession, component_config=None): """ Connect an ApplicationSession class to the given server factory. """ application_session_factory = ApplicationSessionFactory(component_config) application_session_factory.session = MyApplicationSession client_factory = WampRawSocketClientFactory(application_session_factory) # client_factory = WampWebSocketClientFactory(application_session_factory) server_protocol = server_factory.buildProtocol(None) client_protocol = client_factory.buildProtocol(None) server_transport = FakeTransport(server_protocol, True) client_transport = FakeTransport(client_protocol, False) pump = connect(server_protocol, server_transport, client_protocol, client_transport, debug=False) return client_protocol._session, pump
def new_protocol_tcp(self): """ Create a new client protocol connected to the server. :returns: a IRelayTestClient implementation """ server_factory = ServerFactory() server_factory.protocol = TransitConnection server_factory.transit = self._transit_server server_factory.log_requests = self.log_requests server_protocol = server_factory.buildProtocol(('127.0.0.1', 0)) @implementer(IRelayTestClient) class TransitClientProtocolTcp(Protocol): """ Speak the transit client protocol used by the tests over TCP """ _received = b"" connected = False # override Protocol callbacks def connectionMade(self): self.connected = True return Protocol.connectionMade(self) def connectionLost(self, reason): self.connected = False return Protocol.connectionLost(self, reason) def dataReceived(self, data): self._received = self._received + data # IRelayTestClient def send(self, data): self.transport.write(data) def disconnect(self): self.transport.loseConnection() def reset_received_data(self): self._received = b"" def get_received_data(self): return self._received client_factory = ClientFactory() client_factory.protocol = TransitClientProtocolTcp client_protocol = client_factory.buildProtocol(('127.0.0.1', 31337)) pump = iosim.connect( server_protocol, iosim.makeFakeServer(server_protocol), client_protocol, iosim.makeFakeClient(client_protocol), ) pump.flush() self._pumps.append(pump) return client_protocol
def open(self, transport_config, options, protocol_class=None): """ Implement IWebSocketClientAgent with in-memory transports. :param transport_config: a string starting with 'wss://' or 'ws://' :param options: a dict containing options :param protocol_class: the client protocol class to instantiate (or `None` for defaults, which is to use `WebSocketClientProtocol`) """ is_secure = transport_config.startswith("wss://") # call our "real" agent real_client_protocol = self._agent.open( transport_config, options, protocol_class=protocol_class, ) if is_secure: host, port, factory, context_factory, timeout, bindAddress = self._reactor.sslClients[ -1] else: host, port, factory, timeout, bindAddress = self._reactor.tcpClients[ -1] server_address = IPv4Address('TCP', '127.0.0.1', port) client_address = IPv4Address('TCP', '127.0.0.1', 31337) server_protocol = self._server_protocol() server_protocol.factory = WebSocketServerFactory() server_transport = iosim.FakeTransport(server_protocol, isServer=True, hostAddress=server_address, peerAddress=client_address) clientProtocol = factory.buildProtocol(None) client_transport = iosim.FakeTransport(clientProtocol, isServer=False, hostAddress=client_address, peerAddress=server_address) if is_secure: directlyProvides(server_transport, ISSLTransport) directlyProvides(client_transport, ISSLTransport) pump = iosim.connect(server_protocol, server_transport, clientProtocol, client_transport) self._pumper.add(pump) def add_mapping(proto): self._servers[proto] = server_protocol return proto real_client_protocol.addCallback(add_mapping) return real_client_protocol
def _new_protocol_ws(transit_server, log_requests): """ Internal helper for test-suites that need to provide WebSocket client/server pairs. :returns: a 2-tuple: (iosim.IOPump, protocol) """ ws_factory = WebSocketServerFactory("ws://localhost:4002") ws_factory.protocol = WebSocketTransitConnection ws_factory.transit = transit_server ws_factory.log_requests = log_requests ws_protocol = ws_factory.buildProtocol(('127.0.0.1', 0)) @implementer(IRelayTestClient) class TransitWebSocketClientProtocol(WebSocketClientProtocol): _received = b"" connected = False def connectionMade(self): self.connected = True return super(TransitWebSocketClientProtocol, self).connectionMade() def connectionLost(self, reason): self.connected = False return super(TransitWebSocketClientProtocol, self).connectionLost(reason) def onMessage(self, data, isBinary): self._received = self._received + data def send(self, data): self.sendMessage(data, True) def get_received_data(self): return self._received def reset_received_data(self): self._received = b"" def disconnect(self): self.sendClose(1000, True) client_factory = WebSocketClientFactory() client_factory.protocol = TransitWebSocketClientProtocol client_protocol = client_factory.buildProtocol(('127.0.0.1', 31337)) client_protocol.disconnect = client_protocol.dropConnection pump = iosim.connect( ws_protocol, iosim.makeFakeServer(ws_protocol), client_protocol, iosim.makeFakeClient(client_protocol), ) return pump, client_protocol
def connect_mqtt_server(server_factory): server_protocol = server_factory.buildProtocol(None) server_transport = FakeTransport(server_protocol, True) client_protocol = AccumulatingProtocol() client_transport = FakeTransport(client_protocol, False) mqtt_pump = connect(server_protocol, server_transport, client_protocol, client_transport, debug=False) return client_transport, client_protocol, mqtt_pump
def connect(self, factory): if self.pump is not None: raise Exception("SingleUseMemoryEndpoint was already used") try: protocol = factory.buildProtocol(MemoryAddress()) except: return fail() else: self.pump = connect( self._server, AbortableFakeTransport(self._server, isServer=True), protocol, AbortableFakeTransport(protocol, isServer=False)) return succeed(protocol)
def test_socks_ipv6(self): class BadSocksServer(Protocol): def __init__(self): self._buffer = b'' self._recv_stack = [ (b'\x05\x01\x00', b'\x05\x00'), (b'\x05\x01\x00\x04\x20\x02\x44\x93\x04\xd2', b'\x05\x00\x00\x04%s\xbe\xef' % (b'\x00' * 16)), ] def dataReceived(self, data): self._buffer += data if len(self._recv_stack) == 0: assert "not expecting any more data, got {}".format( repr(self._buffer)) return expecting, to_send = self._recv_stack.pop(0) got = self._buffer[:len(expecting)] self._buffer = self._buffer[len(expecting):] assert got == expecting, "wanted {} but got {}".format( repr(expecting), repr(got)) self.transport.write(to_send) factory = socks._TorSocksFactory( u'2002:4493:5105::a299:9bff:fe0e:4471', 1234, 'CONNECT', Mock()) server_proto = BadSocksServer() expected_address = object() server_transport = FakeTransport(server_proto, isServer=True) client_proto = factory.buildProtocol(u'ignored') client_transport = FakeTransport(client_proto, isServer=False, hostAddress=expected_address) pump = yield connect( server_proto, server_transport, client_proto, client_transport, ) # should be relaying now, try sending some datas client_proto.transport.write(b'abcdef') addr = yield factory._get_address() # FIXME how shall we test for IPv6-ness? assert addr is expected_address pump.flush() self.assertEqual(b'abcdef', server_proto._buffer)
def connect(self, factory): if self.pump is not None: raise Exception("SingleUseMemoryEndpoint was already used") try: protocol = factory.buildProtocol(MemoryAddress()) except: return fail() else: self.pump = connect( self._server, AbortableFakeTransport( self._server, isServer=True), protocol, AbortableFakeTransport(protocol, isServer=False)) return succeed(protocol)
def connect(self, cluster, region): """Wire up a connection between cluster and region. :type cluster: `twisted.internet.interfaces.IProtocol` :type region: `twisted.internet.interfaces.IProtocol` :return: py:class:`twisted.test.iosim.IOPump` """ return iosim.connect( region, iosim.makeFakeServer(region), cluster, iosim.makeFakeClient(cluster), debug=False, # Debugging is useful, but too noisy by default. )
def addCluster(self, protocol): """Add a new stub cluster using the given `protocol`. The `protocol` should be an instance of `amp.AMP`. :return: py:class:`twisted.test.iosim.IOPump` """ server_factory = Factory.forProtocol(RegionServer) server_factory.service = self.rpc server = server_factory.buildProtocol(addr=None) return iosim.connect( server, iosim.makeFakeServer(server), protocol, iosim.makeFakeClient(protocol), debug=False, # Debugging is useful, but too noisy by default. )
def test_socks_ipv6(self): class BadSocksServer(Protocol): def __init__(self): self._buffer = b'' self._recv_stack = [ (b'\x05\x01\x00', b'\x05\x00'), (b'\x05\x01\x00\x04\x20\x02\x44\x93\x04\xd2', b'\x05\x00\x00\x04' + (b'\x00' * 16) + b'\xbe\xef'), ] def dataReceived(self, data): self._buffer += data if len(self._recv_stack) == 0: assert "not expecting any more data, got {}".format(repr(self._buffer)) return expecting, to_send = self._recv_stack.pop(0) got = self._buffer[:len(expecting)] self._buffer = self._buffer[len(expecting):] assert got == expecting, "wanted {} but got {}".format(repr(expecting), repr(got)) self.transport.write(to_send) factory = socks._TorSocksFactory(u'2002:4493:5105::a299:9bff:fe0e:4471', 1234, 'CONNECT', Mock()) server_proto = BadSocksServer() expected_address = object() server_transport = FakeTransport(server_proto, isServer=True) client_proto = factory.buildProtocol(u'ignored') client_transport = FakeTransport(client_proto, isServer=False, hostAddress=expected_address) pump = yield connect( server_proto, server_transport, client_proto, client_transport, ) # should be relaying now, try sending some datas client_proto.transport.write(b'abcdef') addr = yield factory._get_address() # FIXME how shall we test for IPv6-ness? assert addr is expected_address pump.flush() self.assertEqual(b'abcdef', server_proto._buffer)
def test_connect_socks_unknown_reply_code(self): class BadSocksServer(Protocol): def __init__(self): self._buffer = b'' self._recv_stack = [ (b'\x05\x01\x00', b'\x05\x00'), # the \xff is an invalid reply-code (b'\x05\x01\x00\x01\x01\x02\x03\x04\x04\xd2', b'\x05\xff\x00\x04\x01\x01\x01\x01'), ] def dataReceived(self, data): self._buffer += data if len(self._recv_stack) == 0: assert "not expecting any more data, got {}".format( repr(self._buffer)) return expecting, to_send = self._recv_stack.pop(0) got = self._buffer[:len(expecting)] self._buffer = self._buffer[len(expecting):] assert got == expecting, "wanted {} but got {}".format( repr(expecting), repr(got)) self.transport.write(to_send) factory = socks._TorSocksFactory(u'1.2.3.4', 1234, 'CONNECT', Mock()) server_proto = BadSocksServer() server_transport = FakeTransport(server_proto, isServer=True) client_proto = factory.buildProtocol('ignored') client_transport = FakeTransport(client_proto, isServer=False) d = client_proto._machine.when_done() # returns IOPump yield connect( server_proto, server_transport, client_proto, client_transport, ) with self.assertRaises(Exception) as ctx: yield d self.assertIn('Unknown SOCKS error-code', str(ctx.exception))
def connect_application_session(server_factory, MyApplicationSession, component_config=None): """ Connect an ApplicationSession class to the given server factory. """ application_session_factory = ApplicationSessionFactory(component_config) application_session_factory.session = MyApplicationSession client_factory = WampRawSocketClientFactory(application_session_factory) server_protocol = server_factory.buildProtocol(None) client_protocol = client_factory.buildProtocol(None) server_transport = FakeTransport(server_protocol, True) client_transport = FakeTransport(client_protocol, False) pump = connect(server_protocol, server_transport, client_protocol, client_transport, debug=False) return client_protocol._session, pump
async def execute(reactor, dbDir): root = SoftFabRoot(dbDir, reactor=None, anonOperator=False) await root.startup() clientInfo, = reactor.unixClients clientFactory = clientInfo[1] serverFactory = ControlSocket(root.apiRoot) clientProtocol = clientFactory.buildProtocol(None) serverProtocol = serverFactory.buildProtocol(None) serverTransport = makeFakeServer(serverProtocol) clientTransport = makeFakeClient(clientProtocol) pump = connect(serverProtocol, serverTransport, clientProtocol, clientTransport, debug=False) pump.flush()
def test_socks_relay_data(self): class BadSocksServer(Protocol): def __init__(self): self._buffer = b'' self._recv_stack = [ (b'\x05\x01\x00', b'\x05\x00'), (b'\x05\x01\x00\x01\x01\x02\x03\x04\x04\xd2', b'\x05\x00\x00\x01\x01\x02\x03\x04\x12\x34'), ] def dataReceived(self, data): self._buffer += data if len(self._recv_stack) == 0: assert "not expecting any more data, got {}".format( repr(self._buffer)) return expecting, to_send = self._recv_stack.pop(0) got = self._buffer[:len(expecting)] self._buffer = self._buffer[len(expecting):] assert got == expecting, "wanted {} but got {}".format( repr(expecting), repr(got)) self.transport.write(to_send) factory = socks._TorSocksFactory(u'1.2.3.4', 1234, 'CONNECT', Mock()) server_proto = BadSocksServer() server_transport = FakeTransport(server_proto, isServer=True) client_proto = factory.buildProtocol('ignored') client_transport = FakeTransport(client_proto, isServer=False) pump = yield connect( server_proto, server_transport, client_proto, client_transport, ) # should be relaying now, try sending some datas client_proto.transport.write(b'abcdef') pump.flush() self.assertEqual(b'abcdef', server_proto._buffer)
def test_connect_socks_unknown_reply_code(self): class BadSocksServer(Protocol): def __init__(self): self._buffer = b'' self._recv_stack = [ (b'\x05\x01\x00', b'\x05\x00'), # the \xff is an invalid reply-code (b'\x05\x01\x00\x01\x01\x02\x03\x04\x04\xd2', b'\x05\xff\x00\x04\x01\x01\x01\x01'), ] def dataReceived(self, data): self._buffer += data if len(self._recv_stack) == 0: assert "not expecting any more data, got {}".format(repr(self._buffer)) return expecting, to_send = self._recv_stack.pop(0) got = self._buffer[:len(expecting)] self._buffer = self._buffer[len(expecting):] assert got == expecting, "wanted {} but got {}".format(repr(expecting), repr(got)) self.transport.write(to_send) factory = socks._TorSocksFactory(u'1.2.3.4', 1234, 'CONNECT', Mock()) server_proto = BadSocksServer() server_transport = FakeTransport(server_proto, isServer=True) client_proto = factory.buildProtocol('ignored') client_transport = FakeTransport(client_proto, isServer=False) d = client_proto._machine.when_done() # returns IOPump yield connect( server_proto, server_transport, client_proto, client_transport, ) with self.assertRaises(Exception) as ctx: yield d self.assertIn('Unknown SOCKS error-code', str(ctx.exception))
def test_socks_relay_data(self): class BadSocksServer(Protocol): def __init__(self): self._buffer = b'' self._recv_stack = [ (b'\x05\x01\x00', b'\x05\x00'), (b'\x05\x01\x00\x01\x01\x02\x03\x04\x04\xd2', b'\x05\x00\x00\x01\x01\x02\x03\x04\x12\x34'), ] def dataReceived(self, data): self._buffer += data if len(self._recv_stack) == 0: assert "not expecting any more data, got {}".format(repr(self._buffer)) return expecting, to_send = self._recv_stack.pop(0) got = self._buffer[:len(expecting)] self._buffer = self._buffer[len(expecting):] assert got == expecting, "wanted {} but got {}".format(repr(expecting), repr(got)) self.transport.write(to_send) factory = socks._TorSocksFactory(u'1.2.3.4', 1234, 'CONNECT', Mock()) server_proto = BadSocksServer() server_transport = FakeTransport(server_proto, isServer=True) client_proto = factory.buildProtocol('ignored') client_transport = FakeTransport(client_proto, isServer=False) pump = yield connect( server_proto, server_transport, client_proto, client_transport, ) # should be relaying now, try sending some datas client_proto.transport.write(b'abcdef') pump.flush() self.assertEqual(b'abcdef', server_proto._buffer)
def connectedServerAndClient(self, serverFactory, clientFactory): """ Set up an in-memory connection between protocols created by C{serverFactory} and C{clientFactory}. @return: A three-tuple. The first element is the protocol created by C{serverFactory}. The second element is the protocol created by C{clientFactory}. The third element is the L{IOPump} connecting them. """ clientProtocol = clientFactory.buildProtocol(None) serverProtocol = serverFactory.buildProtocol(None) clientTransport = AbortableFakeTransport( clientProtocol, isServer=False, hostAddress=self.clientAddress, peerAddress=self.serverAddress) serverTransport = AbortableFakeTransport( serverProtocol, isServer=True, hostAddress=self.serverAddress, peerAddress=self.clientAddress) pump = connect( serverProtocol, serverTransport, clientProtocol, clientTransport) return serverProtocol, clientProtocol, pump
def request(self, method, uri, headers=None, bodyProducer=None): """ Implement IAgent.request. """ # We want to use Agent to parse the HTTP response, so let's ask it to # make a request against our in-memory reactor. response = self._realAgent.request(method, uri, headers, bodyProducer) # If the request has already finished, just propagate the result. In # reality this would only happen in failure, but if the agent ever adds # a local cache this might be a success. already_called = [] def check_already_called(r): already_called.append(r) return r response.addBoth(check_already_called) if already_called: return response # That will try to establish an HTTP connection with the reactor's # connectTCP method, and MemoryReactor will place Agent's factory into # the tcpClients list. Alternately, it will try to establish an HTTPS # connection with the reactor's connectSSL method, and MemoryReactor # will place it into the sslClients list. We'll extract that. if PY3: scheme = URLPath.fromBytes(uri).scheme else: scheme = URLPath.fromString(uri).scheme if scheme == b"https": host, port, factory, context_factory, timeout, bindAddress = ( self._memoryReactor.sslClients[-1]) else: host, port, factory, timeout, bindAddress = ( self._memoryReactor.tcpClients[-1]) serverAddress = IPv4Address('TCP', '127.0.0.1', port) clientAddress = IPv4Address('TCP', '127.0.0.1', 31337) # Create the protocol and fake transport for the client and server, # using the factory that was passed to the MemoryReactor for the # client, and a Site around our rootResource for the server. serverProtocol = Site(self._rootResource).buildProtocol(None) serverTransport = iosim.FakeTransport( serverProtocol, isServer=True, hostAddress=serverAddress, peerAddress=clientAddress) clientProtocol = factory.buildProtocol(None) clientTransport = iosim.FakeTransport( clientProtocol, isServer=False, hostAddress=clientAddress, peerAddress=serverAddress) # Twisted 13.2 compatibility. serverTransport.abortConnection = serverTransport.loseConnection clientTransport.abortConnection = clientTransport.loseConnection if scheme == b"https": # Provide ISSLTransport on both transports, so everyone knows that # this is HTTPS. directlyProvides(serverTransport, ISSLTransport) directlyProvides(clientTransport, ISSLTransport) # Make a pump for wiring the client and server together. pump = iosim.connect( serverProtocol, serverTransport, clientProtocol, clientTransport) self._pumps.add(pump) return response