def __init__(self, reactor, addr, tr=None): self.reactor = reactor self.addr = addr self.tr = proto_helpers.StringTransport() if tr: self.tr = tr
def setUp(self): self.channel_layer = ChannelLayer() self.factory = HTTPFactory(self.channel_layer, send_channel="test!") self.proto = self.factory.buildProtocol(('127.0.0.1', 0)) self.tr = proto_helpers.StringTransport() self.proto.makeConnection(self.tr)
def setUp(self): self.p = telnet.TelnetTransport(TestProtocol) self.t = proto_helpers.StringTransport() self.p.makeConnection(self.t)
def setUp(self): self.protocol = TorControlProtocol() self.protocol.connectionMade = lambda: None self.transport = proto_helpers.StringTransport() self.protocol.makeConnection(self.transport)
def setUp(self): factory = RemoteCalculationFactory() self.proto = factory.buildProtocol(("127.0.0.1", 0)) self.tr = proto_helpers.StringTransport() self.proto.makeConnection(self.tr)
def init_clients(self, clients): for c in clients: self.protocols[c] = (self.factory.buildProtocol( ('127.0.0.1', 0)), proto_helpers.StringTransport()) self.protocols[c][0].makeConnection(self.protocols[c][1])
def _testProto(self, cls, data): proto = cls(self.env, 'a message') proto.transport = proto_helpers.StringTransport() proto.connectionMade() self.assertEqual(data, proto.transport.value())
def _set_protocol(self, factory, address): """Make protocol connection with a Twisted string transport.""" protocol = factory.buildProtocol(address) protocol.makeConnection(proto_helpers.StringTransport()) protocol.history = {'rcv': [], 'snd': []} return protocol
def setUp(self): self.protocol = AsyncAGIProtocol() self.transport = proto_helpers.StringTransport() self.protocol.makeConnection(self.transport) self.protocol.started = True
def setUp(self): super(SiteTest, self).setUp() factory = Site(ServerRoot()) self.proto = factory.buildProtocol(('127.0.0.1', 0)) self.tr = proto_helpers.StringTransport() self.proto.makeConnection(self.tr)
def doStart(self): ServerFactory.doStart(self) # Wait for AMQP to get ready self.log.info("Waiting for AMQP to get ready") yield self.pb['smppcm'].amqpBroker.channelReady # Load configuration profile proto = self.buildProtocol(('127.0.0.1', 0)) tr = proto_helpers.StringTransport() proto.makeConnection(tr) if (self.config.authentication and self.loadConfigProfileWithCreds['username'] is not None and self.loadConfigProfileWithCreds['password'] is not None): self.log.info( "OnStart loading configuration default profile with username: '******'", self.loadConfigProfileWithCreds['username']) if (self.loadConfigProfileWithCreds['username'] != self.config.admin_username or md5(self.loadConfigProfileWithCreds['password']).digest() != self.config.admin_password): self.log.error( "Authentication error, cannot load configuration profile with provided username: '******'", self.loadConfigProfileWithCreds['username']) proto.connectionLost(None) defer.returnValue(False) proto.dataReceived('%s\r\n' % self.loadConfigProfileWithCreds['username']) proto.dataReceived('%s\r\n' % self.loadConfigProfileWithCreds['password']) elif self.config.authentication: self.log.error( 'Authentication is required and no credentials given, config. profile will not be loaded' ) proto.connectionLost(None) defer.returnValue(False) else: self.log.info( "OnStart loading configuration default profile without credentials (auth. is not required)" ) proto.dataReceived('load\r\n') # Wait some more time till all configurations are loaded pending_load = [ 'mtrouter', 'morouter', 'filter', 'group', 'smppcc', 'httpcc', 'user' ] while True: for _pl in pending_load: if re.match(r'.*%s configuration loaded.*' % _pl, tr.value(), re.DOTALL): self.log.info("%s configuration loaded.", _pl) pending_load.remove(_pl) if len(pending_load) > 0: waitDeferred = defer.Deferred() reactor.callLater(0.3, waitDeferred.callback, None) yield waitDeferred else: break proto.dataReceived('quit\r\n') proto.connectionLost(None) defer.returnValue(False)
def setUp(self): load_test_config() factory = JMClientProtocolFactory(DummyMaker(), proto_type='MAKER') self.client = factory.buildProtocol(None) self.tr = proto_helpers.StringTransport() self.client.makeConnection(self.tr)
def setUp(self): self.transport = proto_helpers.StringTransport() self.clock = task.Clock() self.protocol = RemoteCalculationClientWithTimeout() self.protocol.callLater = self.clock.callLater self.protocol.makeConnection(self.transport)
def new_connection(self): self.proto = self.factory.buildProtocol(("127.0.0.1", 0)) self.tr = proto_helpers.StringTransport() self.proto.makeConnection(self.tr)
def setUp(self): self.transport = proto_helpers.StringTransport() self.netstringReceiver = TestNetstring() self.netstringReceiver.makeConnection(self.transport)
def test_client_to_client_establish_conversation(self): server_factory = HermesFactory(self._server_private_key, self._server_public_key) client_a_factory = HermesClientFactory(self._client_a_private_key, self._client_a_public_key, self._server_public_key) client_b_factory = HermesClientFactory(self._client_b_private_key, self._client_b_public_key, self._server_public_key) server_a_transport = proto_helpers.StringTransport() server_b_transport = proto_helpers.StringTransport() server_a = server_factory.buildProtocol("localhost") server_b = server_factory.buildProtocol("localhost") client_a = client_a_factory.buildProtocol("localhost") client_b = client_b_factory.buildProtocol("localhost") establish_session(client_a, server_a, server_a_transport) establish_session(client_b, server_b, server_b_transport) # Check that both client and the server are in a session using the same session key self.assertTrue(client_a.session) self.assertTrue(client_a.session.key) self.assertTrue(server_a.session) self.assertTrue(server_a.session.key) self.assertEqual(server_a.session.key, client_a.session.key) self.assertTrue(client_b.session) self.assertTrue(client_b.session.key) self.assertTrue(server_b.session) self.assertTrue(server_b.session.key) self.assertEqual(server_b.session.key, client_b.session.key) # Once a session has been established, we can attempt to login: login(client_a, server_a, server_a_transport, b'Eugene', self._user1_public_key, self._user1_private_key) login(client_b, server_b, server_b_transport, b'Jonathan', self._user2_public_key, self._user2_private_key) self.assertEqual(server_a.session.message_buffer[-1], b'LOGIN_USERNAME:Eugene') self.assertEqual(client_a.session.message_buffer[-1], b'LOGIN_USERNAME_ACCEPT:') self.assertEqual(server_b.session.message_buffer[-1], b'LOGIN_USERNAME:Jonathan') self.assertEqual(client_b.session.message_buffer[-1], b'LOGIN_USERNAME_ACCEPT:') jonathan = client_a.add_user(self._user2_public_key, b'Jonathan') eugene = client_b.add_user(self._user1_public_key, b'Eugene') client_a_convo = client_a.start_convo(jonathan) m = server_a_transport.value() server_a_transport.clear() # A(START_CONVERSATION)->S(START_CONVERSATION)->B m = send_receive(server_a, server_b_transport, m) # S(START_CONVERSATION)->B(START_CONVERSATION_CHALLENGE)->S m = send_receive(client_b, server_b_transport, m) # B(START_CONVERSATION_CHALLENGE)->S(START_CONVERSATION_CHALLENGE)->A m = send_receive(server_b, server_a_transport, m) # S(START_CONVERSATION_CHALLENGE)->A(START_CONVERSATION_RESPONSE)->S m = send_receive(client_a, server_a_transport, m) # A(START_CONVERSATION_RESPONSE)->S(START_CONVERSATION_RESPONSE)->B m = send_receive(server_a, server_b_transport, m) # S(START_CONVERSATION_RESPONSE)->B(START_CONVERSATION_ACCEPT)->S m = send_receive(client_b, server_b_transport, m) # B(START_CONVERSATION_ACCEPT)->S(START_CONVERSATION_ACCEPT)->A m = send_receive(server_b, server_a_transport, m) # S(START_CONVERSATION_ACCEPT)->A(START_CONVERSATION_KEY)->S m = send_receive(client_a, server_a_transport, m) # A(START_CONVERSATION_KEY)->S(START_CONVERSATION_KEY)->B m = send_receive(server_a, server_b_transport, m) # S(START_CONVERSATION_KEY)->B send_receive(client_b, server_b_transport, m) client_b_convo = client_b.session.conversations[client_a_convo.id] self.assertEqual(client_b_convo.state, client_b_convo.State.conversing) self.assertEqual(client_a_convo.state, client_a_convo.State.conversing) self.assertEqual(client_a_convo.key, client_b_convo.key)
def test_noUnicode(self): """ Test that L{proto_helpers.StringTransport} doesn't accept unicode data. """ s = proto_helpers.StringTransport() self.assertRaises(TypeError, s.write, u'foo')
def setUp(self): self.tr = proto_helpers.StringTransport() self.proto = RemoteCalculationClient() self.proto.makeConnection(self.tr)
def request(self, jbody=None, user_id=None, role=None, headers=None, body='', remote_ip='0.0.0.0', method='MOCK', kwargs={}): """ Function userful for performing mock requests. Args: jbody: The body of the request as a dict (it will be automatically converted to string) body: The body of the request as a string user_id: when simulating authentication the session should be bound to a certain user_id. role: when simulating authentication the session should be bound to a certain role. method: HTTP method, e.g. "GET" or "POST" uri: URL to fetch headers: (dict or :class:`cyclone.httputil.HTTPHeaders` instance) HTTP headers to pass on the request remote_ip: If a particular remote_ip should be set. """ if jbody and not body: body = json.dumps(jbody) elif body and jbody: raise ValueError('jbody and body in conflict') application = Application([]) tr = proto_helpers.StringTransport() connection = GLHTTPConnection() connection.factory = application connection.makeConnection(tr) request = httpserver.HTTPRequest(uri='mock', method=method, headers=headers, body=body, remote_ip=remote_ip, connection=connection) handler = self._handler(application, request, **kwargs) if role is not None: session = authentication.GLSession(user_id, role, 'enabled') handler.request.headers['X-Session'] = session.id return handler
def setUp(self): channel = "_testing" factory = KarmaBotFactory(channel=channel) self.proto = factory.buildProtocol(('127.0.0.1', 0)) self.trans = proto_helpers.StringTransport() self.proto.makeConnection(self.trans)
def setUp(self): rollbar.init(TOKEN, 'twisted-test') factory = SquareFactory() self.proto = factory.buildProtocol(('127.0.0.1', 0)) self.tr = proto_helpers.StringTransport() self.proto.makeConnection(self.tr)
def setUp(self): self.modem = TxGSMProtocol() self.modem.verbose = True self.modem_transport = proto_helpers.StringTransport() self.modem.makeConnection(self.modem_transport)
def setUp(self): self.protocol = TorControlProtocol() self.transport = proto_helpers.StringTransport()
def setUp(self): factory = EchoServerFactory() self.proto = factory.buildProtocol(('127.0.0.1', 0)) self.tr = proto_helpers.StringTransport() self.proto.makeConnection(self.tr)
def setUp(self): factory = EchoServerFactory(True) self.proto = factory.buildProtocol(("127.0.0.1", 0)) self.transport = proto_helpers.StringTransport() self.proto.makeConnection(self.transport) self.packer = msgpack.Packer(encoding="utf-8")
def request(self, jbody=None, user_id=None, role=None, headers=None, body='', remote_ip='0.0.0.0', method='MOCK', attached_file={}, kwargs={}): """ Function userful for performing mock requests. Args: jbody: The body of the request as a dict (it will be automatically converted to string) body: The body of the request as a string user_id: when simulating authentication the session should be bound to a certain user_id. role: when simulating authentication the session should be bound to a certain role. method: HTTP method, e.g. "GET" or "POST" headers: (dict or :class:`cyclone.httputil.HTTPHeaders` instance) HTTP headers to pass on the request remote_ip: If a particular remote_ip should be set. attached_file: A cyclone.httputil.HTTPFiles or a dict to place in the request.files obj """ if jbody and not body: body = json.dumps(jbody) elif body and jbody: raise ValueError('jbody and body in conflict') if attached_file is None: fake_files = {} else: fake_files = { 'file': [attached_file] } # Yes this is ugly, but it's the format application = Application([]) tr = proto_helpers.StringTransport() connection = GLHTTPConnection() connection.factory = application connection.makeConnection(tr) request = httpserver.HTTPRequest(uri='mock', method=method, headers=headers, body=body, remote_ip=remote_ip, connection=connection, files=fake_files) def mock_write(cls, response=None): if response: self.responses.append(response) self._handler.write = mock_write def mock_finish(cls): pass self._handler.finish = mock_finish handler = self._handler(application, request, **kwargs) # pylint: disable=not-callable if user_id is None and role is not None: if role == 'admin': user_id = self.dummyAdminUser['id'] elif role == 'receiver': user_id = self.dummyReceiverUser_1['id'] elif role == 'custodian': user_id = self.dummyCustodianUser['id'] if role is not None: session = GLSession(user_id, role, 'enabled') handler.request.headers['X-Session'] = session.id return handler
def setUp(self): self.proto = BurlyBot() self.tr = proto_helpers.StringTransport() self.proto.makeConnection(self.tr)
def setUp(self): self.protocol = TorControlProtocol(lambda: defer.succeed('foo')) self.transport = proto_helpers.StringTransport()
def setUp(self): self.proto = ExcessivelyLargeLineCatcher() self.proto.MAX_LENGTH = 6 self.transport = proto_helpers.StringTransport() self.proto.makeConnection(self.transport)
def setUp(self): # Create the client connection factory = TrackerClientFactory() self.proto = factory.buildProtocol(('127.0.0.1', 0)) self.tr = proto_helpers.StringTransport() self.proto.makeConnection(self.tr)