Example #1
0
    def test_badline_err(self):
        """protocol completes with incomplete line
        """
        T = proto_helpers.StringTransportWithDisconnection()
        P = MockBLP()
        P.rx_buf_size = 4
        T.protocol = P
        P.makeConnection(T)

        P.dataReceived('A\nB\nC')  # trailing '\n' for last line never sent

        self.assertEqual(P.lines, ['A', 'B'])

        P.connectionLost(protocol.connectionDone)  # normal close
        self.assertFalse(P._procD.called)
        self.assertFalse(
            P.defer.called)  # not called until processing completes

        P._finish(ok=42)

        self.assertTrue(P.defer.called)

        try:
            V = yield P.defer
            self.assertEqual(V, 42)  # should never get here
            self.assertTrue(False, 'missing expected exception')
        except RuntimeError as e:
            self.assertEqual(e.message, 'connection closed with partial line')

        self.alldone = True
Example #2
0
 def setUp(self):
     self.tr = proto_helpers.StringTransportWithDisconnection()
     self.clock = task.Clock()
     self.proto = RemoteCalculationClient()
     self.tr.protocol = self.proto
     self.proto.callLater = self.clock.callLater
     self.proto.makeConnection(self.tr)
Example #3
0
    def test_email_metrics(self):

        config = _createConfig()
        context = _createMailServerContext(config)
        message = SMTPMessage(context)
        message.lines = [
            "From: [email protected]",
            "To: [email protected]",
            "Subject: testing",
            "",
            "get transport obfs4",
        ]

        message.message = message.getIncomingMessage()
        responder = message.responder
        tr = proto_helpers.StringTransportWithDisconnection()
        tr.protocol = responder
        responder.makeConnection(tr)

        email_metrix = metrics.EmailMetrics()

        key1 = "email.obfs4.gmail.success.none"
        email_metrix.recordValidEmailRequest(responder)
        self.assertTrue(email_metrix.hotMetrics[key1] == 1)

        key2 = "email.obfs4.gmail.fail.none"
        email_metrix.recordInvalidEmailRequest(responder)
        self.assertTrue(email_metrix.hotMetrics[key2] == 1)
Example #4
0
    def test_conn2_err(self):
        """Connection closes with data proc. in progress
        """
        T = proto_helpers.StringTransportWithDisconnection()
        P = MockBLP()
        P.rx_buf_size = 4
        T.protocol = P
        P.makeConnection(T)

        P.dataReceived('A\nB\nC')
        self.assertEqual(P.lines, ['A', 'B'])

        P.connectionLost(failure.Failure(error.ConnectionAborted()))
        self.assertFalse(
            P.defer.called)  # not called until processing completes

        P._finish(ok=42)

        try:
            yield P.defer
            self.assertTrue(False)
        except error.ConnectionAborted:
            pass

        self.alldone = True
Example #5
0
 def __init__(self, user, realm, factory, address=address.IPv4Address('TCP', '127.0.0.1', 54321)):
     self.user = user
     self.transport = proto_helpers.StringTransportWithDisconnection()
     self.protocol = factory.buildProtocol(address)
     self.transport.protocol = self.protocol
     self.user.mind = self.protocol
     self.protocol.makeConnection(self.transport)
Example #6
0
 def setUp(self):
     self.protocol = TorControlProtocol()
     self.protocol.connectionMade = lambda: None
     self.transport = proto_helpers.StringTransportWithDisconnection()
     self.protocol.makeConnection(self.transport)
     # why doesn't makeConnection do this?
     self.transport.protocol = self.protocol
Example #7
0
 def setUp(self):
     self.event = DummyEvent()
     self.factory = VOEventSenderFactory(self.event)
     self.proto = self.factory.buildProtocol(('127.0.0.1', 0))
     self.tr = proto_helpers.StringTransportWithDisconnection()
     self.proto.makeConnection(self.tr)
     self.tr.protocol = self.proto
Example #8
0
 def _create_client(self, *args, **kwargs):
     self.cfactory = BroadcastClientFactory(*args, **kwargs)
     self.cproto = BroadcastClientProtocol()
     self.cproto.factory = self.cfactory
     self.cproto.failHandshake = MagicMock()
     self.ctr = proto_helpers.StringTransportWithDisconnection()
     self.cproto.transport = self.ctr
     self.ctr.protocol = self.cproto
Example #9
0
 def setUp(self):
     self.transport = proto_helpers.StringTransportWithDisconnection()
     self.clock     = task.Clock()
     self.factory   = MQTTFactory(MQTTFactory.PUBLISHER)
     self.protocol  = MQTTBaseProtocol(self.factory)
     self.transport.protocol = self.protocol
     MQTTBaseProtocol.callLater = self.clock.callLater
     self.protocol.makeConnection(self.transport)
Example #10
0
 def setUp(self):
     self.factory = VOEventReceiverFactory(DUMMY_SERVICE_IVOID)
     self.proto = self.factory.buildProtocol(('127.0.0.1', 0))
     self.clock = task.Clock()
     self.proto.callLater = self.clock.callLater
     self.tr = proto_helpers.StringTransportWithDisconnection()
     self.proto.makeConnection(self.tr)
     self.tr.protocol = self.proto
Example #11
0
 def setUp(self):
     factory = LiveServer()
     self.protocol = factory.buildProtocol(('127.0.0.1', 1557))
     self.transport = proto_helpers.StringTransportWithDisconnection()
     self.transport.protocol = self.protocol
     self.protocol.makeConnection(self.transport)
     self._shortNalu = '\x61\xea\x31'
     self._longNalu = ''.join(['\x61', '\x2f' * 4000])
Example #12
0
 def _create_server(self, *args, **kwargs):
     self.sfactory = BroadcastServerFactory(*args, **kwargs)
     self.sproto = BroadcastServerProtocol()
     self.sproto.factory = self.sfactory
     self.sproto.failHandshake = MagicMock()
     self.str = proto_helpers.StringTransportWithDisconnection()
     self.sproto.transport = self.str
     self.str.protocol = self.sproto
    def setUp(self):
        liveServer = LiveServer()
        liveServer.addSource(TestSource())

        factory = RTSPServer(liveServer)
        self.protocol = factory.buildProtocol(('127.0.0.1', 1556))
        self.transport = proto_helpers.StringTransportWithDisconnection()
        self.transport.protocol = self.protocol
        self.protocol.makeConnection(self.transport)
Example #14
0
 def setUp(self):
     factory = VOEventSubscriberFactory(DUMMY_EVENT_IVOID)
     self.clock = task.Clock()
     factory.callLater = self.clock.callLater
     self.proto = factory.buildProtocol(('127.0.0.1', 0))
     self.proto.callLater = self.clock.callLater  # Original in TimeoutMixin
     self.tr = proto_helpers.StringTransportWithDisconnection()
     self.proto.makeConnection(self.tr)
     self.tr.protocol = self.proto
Example #15
0
 def setUp(self):
     '''
     Set up a conencted state
     '''
     self.transport = proto_helpers.StringTransportWithDisconnection()
     self.clock = task.Clock()
     MQTTBaseProtocol.callLater = self.clock.callLater
     self.factory = MQTTFactory(MQTTFactory.SUBSCRIBER)
     self._rebuild()
Example #16
0
 def makeProto(self):
     wrappedProto = proto_helpers.AccumulatingProtocol()
     proto = I2PClientTunnelProtocol(wrappedProto, None, TEST_B64)
     proto.factory = FakeDisconnectingFactory()
     transport = proto_helpers.StringTransportWithDisconnection()
     transport.abortConnection = lambda: None
     transport.protocol = proto
     proto.makeConnection(transport)
     return proto
Example #17
0
    def setUp(self):
        factory = socks5.SOCKSv5Factory()
        self.proto = factory.buildProtocol(('127.0.0.1', 0))
        self.tr = proto_helpers.StringTransportWithDisconnection()
        self.tr.protocol = self.proto
        self.proto.makeConnection(self.tr)

        # Get things to where the next step is the client sends the auth message
        self._sendMsg("050102")
        self._recvMsg("0502")
Example #18
0
    def setUp(self):

        self.server = test_policies.Server()
        tServer = TestableWebSocketFactory(task.Clock(), self.server)
        self.port = tServer.buildProtocol(
            address.IPv4Address('TCP', '127.0..0.1', 0))
        self.tr = proto_helpers.StringTransportWithDisconnection()
        self.tr.protocol = self.port
        self.port.makeConnection(self.tr)
        self.port.producer = self.port.wrappedProtocol
Example #19
0
 def setUp(self):
     if twistedreactor is None:
         raise unittest.SkipTest("Twisted libraries not available")
     twistedreactor.TwistedConnection.initialize_reactor()
     self.tr = proto_helpers.StringTransportWithDisconnection()
     self.tr.connector = Mock()
     self.mock_connection = Mock()
     self.obj_ut = twistedreactor.TwistedConnectionProtocol(
         self.mock_connection)
     self.tr.protocol = self.obj_ut
Example #20
0
 def setUp(self):
     self.factory = VOEventBroadcasterFactory(DUMMY_SERVICE_IVOID.decode(),
                                              BCAST_TEST_INTERVAL)
     self.factory.alive_loop.clock = task.Clock()
     self.factory.test_loop.clock = task.Clock()
     self.connector = reactor.listenTCP(0, self.factory)
     self.proto = self.factory.buildProtocol(('127.0.0.1', 0))
     self.tr = proto_helpers.StringTransportWithDisconnection()
     self.proto.makeConnection(self.tr)
     self.tr.protocol = self.proto
Example #21
0
 def test_send(self):
     finished = defer.Deferred()
     handler = async_log_client.ResponseBodyHandler(finished)
     transport = proto_helpers.StringTransportWithDisconnection()
     handler.makeConnection(transport)
     transport.protocol = handler
     handler.dataReceived("test")
     transport.loseConnection()
     finished.addCallback(self.assertEqual, "test")
     return finished
Example #22
0
 def setUp(self):
     '''
     Set up a conencted state
     '''
     self.transport = proto_helpers.StringTransportWithDisconnection()
     self.clock = task.Clock()
     MQTTBaseProtocol.callLater = self.clock.callLater
     self.factory = MQTTFactory(MQTTFactory.PUBLISHER)
     self.addr = IPv4Address('TCP', 'localhost', 1880)
     self._rebuild()
Example #23
0
    def setUp(self):
        config.pingEnable = False
        self.cmd_tr = proto_helpers.StringTransportWithDisconnection()
        self.x1_tr = proto_helpers.StringTransportWithDisconnection()
        cmdFac = newCmdProxyFactory()

        self.cmdProto = cmdFac.buildProtocol(('127.0.0.1', 0))
        self.cmdProto.makeConnection(self.cmd_tr)

        def start_x1_client():
            self.x1Proto = cmdFac.x1CliFac.buildProtocol(('127.0.0.1', 0))
            self.x1Proto.makeConnection(self.x1_tr)

        cmdFac.start_x1_client = start_x1_client

        class dummyX1TCP(object):
            def disconnect(self):
                return

        cmdFac.x1tcp = dummyX1TCP()
Example #24
0
 def setUp(self):
     self.tr = proto_helpers.StringTransportWithDisconnection()
     proto = Mock()
     proto.sender = Mock()
     proto.sender.transport = self.tr
     self.tr.protocol = proto
     self.s = session.SAMSession()
     self.s.nickname = 'foo'
     self.s.samVersion = '3.1'
     self.s.id = 'foo'
     self.s._proto = proto
     session._sessions['foo'] = self.s
Example #25
0
 def setUp(self):
     self.tr = proto_helpers.StringTransportWithDisconnection()
     self.clock = task.Clock()
     cmd_queue = DeferredQueue()
     x1_queue = DeferredQueue()
     state = State()
     factory = X1ClientFactory(cmd_queue, x1_queue, state)
     self.proto = factory.buildProtocol(('127.0.0.1', 0))
     self.proto.callLater = self.clock.callLater
     self.tr.protocol = self.proto
     config.pingEnable = False
     self.cmd_data = []
Example #26
0
 def connect(self, fac):
     self.factory = fac
     if self.accept_port:
         if self.port != self.accept_port:
             return defer.fail(self.failure)
     else:
         if self.failure:
             return defer.fail(self.failure)
     self.proto = fac.buildProtocol(None)
     transport = proto_helpers.StringTransportWithDisconnection()
     self.proto.makeConnection(transport)
     self.transport = transport
     return defer.succeed(self.proto)
    def test_protocol(self):
        self.test_val = 0

        def callback(response):
            self.test_val += 1

        protocol = self.protocol
        self.assertIsInstance(protocol, Usk3Protocol)
        transport = proto_helpers.StringTransportWithDisconnection()
        transport.protocol = protocol
        transport.write('hello')
        self.assertEquals(transport.value(), 'hello')
        transport.clear()

        protocol.makeConnection(transport)

        self.assertTrue(protocol.connected)
        self.assertEquals(transport.value(), '')
        self.clock.advance(22)
        self.assertTrue(protocol.connected)
        packet = Usk3Packet(1, 0x100)
        packet.callback_func = callback
        protocol.add_command(packet)
        protocol.add_command(packet)
        protocol.add_command(packet)
        self.clock.advance(20)
        self.assertFalse(protocol.connected)
        transport.connected = True
        protocol.makeConnection(transport)
        self.assertTrue(protocol.connected)
        self.clock.advance(10)
        self.assertTrue(protocol.connected)
        self.clock.advance(6)
        self.assertFalse(protocol.connected)
        transport.connected = True
        protocol.makeConnection(transport)
        self.assertTrue(protocol.connected)
        self.assertEquals(len(self.shared_data.command_queue), 0x02)
        self.assertIsInstance(self.shared_data.current_command, Usk3Packet)
        protocol.dataReceived(str(Usk3Packet(0, 0x00)))
        self.assertEquals(len(self.shared_data.command_queue), 0x01)
        self.assertIsInstance(self.shared_data.current_command, Usk3Packet)
        protocol.dataReceived(str(Usk3Packet(0, 0x00)))
        self.assertEquals(len(self.shared_data.command_queue), 0x00)
        self.assertIsInstance(self.shared_data.current_command, Usk3Packet)
        protocol.dataReceived(str(Usk3Packet(0, 0x00)))
        self.assertEquals(None, self.shared_data.current_command)

        self.assertEquals(self.test_val, 0x03)
Example #28
0
    def test_connection(self):

        self.factory = LiveServer()
        #build a connection here
        self.transport = proto_helpers.StringTransportWithDisconnection()
        self.protocol = self.factory.buildProtocol(('127.0.0.1', 1557))
        self.transport.protocol = self.protocol
        self.protocol.makeConnection(self.transport)
        self.assertEqual(self.factory.getClientNum(), 1)

        #build another connection here
        self.transport_1 = proto_helpers.StringTransportWithDisconnection()
        self.protocol_1 = self.factory.buildProtocol(('127.0.0.1', 1557))
        self.transport_1.protocol = self.protocol_1
        self.protocol_1.makeConnection(self.transport_1)
        self.assertEqual(self.factory.getClientNum(), 2)

        #tear down the first connection
        self.protocol.transport.loseConnection()
        self.assertEqual(self.factory.getClientNum(), 1)

        #tear down the second connection
        self.protocol_1.transport.loseConnection()
        self.assertEqual(self.factory.getClientNum(), 0)
Example #29
0
 def test_buffer_overflow(self):
     original = FLAGS.response_buffer_size_bytes
     FLAGS.response_buffer_size_bytes = 10
     test_msg = "x"*11
     finished = defer.Deferred()
     handler = async_log_client.ResponseBodyHandler(finished)
     transport = proto_helpers.StringTransportWithDisconnection()
     handler.makeConnection(transport)
     transport.protocol = handler
     handler.dataReceived(test_msg)
     transport.loseConnection()
     # TODO(ekasper): find a more elegant and robust way to save flags.
     FLAGS.response_buffer_size_bytes = original
     return self.assertFailure(finished,
                               async_log_client.HTTPResponseSizeExceededError)
Example #30
0
 def test_send_chunks(self):
     test_msg = "x"*1024
     chunk_size = 100
     finished = defer.Deferred()
     handler = async_log_client.ResponseBodyHandler(finished)
     transport = proto_helpers.StringTransportWithDisconnection()
     handler.makeConnection(transport)
     transport.protocol = handler
     sent = 0
     while sent < len(test_msg):
         handler.dataReceived(test_msg[sent:sent + chunk_size])
         sent += chunk_size
     transport.loseConnection()
     finished.addCallback(self.assertEqual, test_msg)
     return finished