コード例 #1
0
    def test_disconnect_reconnect_1(self):
        """ Test if the connection will retry connecting after it was disconnected from the server.
        """

        mock = Sysmock()
        mock.system.add_unused_local_address(CLIENT)
        mock.system.add_unused_local_address(CLIENT)

        ready_handler = unittest.mock.Mock()

        with patch(mock):
            loop = Mainloop()

            mock.expect_tcp_syn(CLIENT, SERVER)
            mock.do_tcp_syn_ack(SERVER, CLIENT)
            mock.do_tcp_input(SERVER, CLIENT, packets.welcome())

            conn = NxtcpConnection(loop, SERVER.address)
            conn.set_ready_handler(ready_handler)

            ready_handler.assert_called_once_with(False)
            ready_handler.reset_mock()

            mock.run_events(loop.run_once)

            ready_handler.assert_called_once_with(True)
            ready_handler.reset_mock()

            # now close connection from server

            mock.do_tcp_fin(SERVER, CLIENT)
            mock.expect_tcp_fin(CLIENT, SERVER)

            mock.run_events(loop.run_once)

            ready_handler.assert_called_once_with(False)
            ready_handler.reset_mock()

            # now the connection should retry after 5 seconds
            mock.expect_sleep(5.0)
            mock.expect_tcp_syn(CLIENT, SERVER)

            mock.run_events(loop.run_once)

            # when this the connection is ready the callback should be called again
            mock.do_tcp_syn_ack(SERVER, CLIENT)
            mock.do_tcp_input(SERVER, CLIENT, packets.welcome())

            mock.run_events(loop.run_once)

            ready_handler.assert_called_once_with(True)
コード例 #2
0
def test_logout_1(self):
    """ Test that a logout packet is send.
    """

    mock = Sysmock()
    mock.system.add_unused_local_address(CLIENT)

    with patch(mock):
        loop = Mainloop()

        mock.expect_tcp_syn(CLIENT, SERVER)
        mock.do_tcp_syn_ack(SERVER, CLIENT)
        mock.do_tcp_input(SERVER, CLIENT, packets.welcome())

        conn = NxtcpConnection(loop, SERVER.address)

        mock.run_events(loop.run_once)

        # the expected packet
        mock.expect_tcp_output(CLIENT, SERVER, packets.logout(name=b'name', ))

        # put the verb upstream
        conn.send_verb(verbs.LogoutVerb(name=b'name', ))

        mock.run_events(loop.run_once)
コード例 #3
0
    def test_connection_ready_2(self):
        """ Test if the connection will be ready after the connection is set up and the welcome packet has been
        received.
        """

        mock = Sysmock()
        mock.system.add_unused_local_address(CLIENT)

        ready_handler = unittest.mock.Mock()

        with patch(mock):
            loop = Mainloop()

            mock.expect_tcp_syn(CLIENT, SERVER)
            mock.do_tcp_syn_ack(SERVER, CLIENT)
            mock.do_tcp_input(SERVER, CLIENT, packets.welcome())

            conn = NxtcpConnection(loop, SERVER.address)
            conn.set_ready_handler(ready_handler)

            ready_handler.assert_called_once_with(False)
            ready_handler.reset_mock()

            mock.run_events(loop.run_once)

            ready_handler.assert_called_once_with(True)
コード例 #4
0
def test_session_3(self):
    """ Test that a Session verb is produced.
    """

    mock = Sysmock()
    mock.system.add_unused_local_address(CLIENT)

    downstream_handler = unittest.mock.Mock()

    with patch(mock):
        loop = Mainloop()

        mock.expect_tcp_syn(CLIENT, SERVER)
        mock.do_tcp_syn_ack(SERVER, CLIENT)
        mock.do_tcp_input(SERVER, CLIENT, packets.welcome())

        # send the packet
        mock.do_tcp_input(
            SERVER, CLIENT,
            packets.session(b'name', packets.SESSION_STATE_STANDBY))

        conn = NxtcpConnection(loop, SERVER.address)
        conn.set_downstream_handler(downstream_handler)

        mock.run_events(loop.run_once)

        # verify the verb
        downstream_handler.assert_called_once_with(
            verbs.SessionVerb(b'name', verbs.SessionVerb.STATE_STANDBY))
コード例 #5
0
    def test_disconnect_1(self):
        """ Test if the connection's ready status will be removed when the connection is closed on the server's end.
        """

        mock = Sysmock()
        mock.system.add_unused_local_address(CLIENT)

        ready_handler = unittest.mock.Mock()

        with patch(mock):
            loop = Mainloop()

            mock.expect_tcp_syn(CLIENT, SERVER)
            mock.do_tcp_syn_ack(SERVER, CLIENT)
            mock.do_tcp_input(SERVER, CLIENT, packets.welcome())

            conn = NxtcpConnection(loop, SERVER.address)
            conn.set_ready_handler(ready_handler)

            ready_handler.assert_called_once_with(False)
            ready_handler.reset_mock()

            mock.run_events(loop.run_once)

            ready_handler.assert_called_once_with(True)
            ready_handler.reset_mock()

            # now let the server close the connection and check if the ready_handler will be called with False

            mock.do_tcp_fin(SERVER, CLIENT)
            mock.expect_tcp_fin(CLIENT, SERVER)

            mock.run_events(loop.run_once)

            ready_handler.assert_called_once_with(False)
コード例 #6
0
def setup_story(nr_of_clients):
    """ Convenience function to quicly setup a story with n clients.
    """

    s = Story()

    s.expect_local_listen(LHOST)

    for i in range(nr_of_clients):
        peer = REMOTE_PEERS[i]
        s.do_remote_connect(peer, LHOST)
        s.expect_local_send(LHOST, peer, packets.welcome())

    return s
コード例 #7
0
def test_request_2(self):
    """ Test that a request packet is send.
    """

    mock = Sysmock()
    mock.system.add_unused_local_address(CLIENT)

    with patch(mock):
        loop = Mainloop()

        mock.expect_tcp_syn(CLIENT, SERVER)
        mock.do_tcp_syn_ack(SERVER, CLIENT)
        mock.do_tcp_input(SERVER, CLIENT, packets.welcome())

        conn = NxtcpConnection(loop, SERVER.address)

        mock.run_events(loop.run_once)

        # the expected packet
        mock.expect_tcp_output(
            CLIENT, SERVER,
            packets.request(
                name=b'name',
                unidirectional=True,
                messageref=0,
                timeout_ms=0,
                payload=b'payload',
            ))

        # put the verb upstream
        conn.send_verb(
            verbs.RequestVerb(
                name=b'name',
                unidirectional=True,
                messageref=None,
                timeout=None,
                payload=b'payload',
            ))

        mock.run_events(loop.run_once)
コード例 #8
0
def test_interest_2(self):
    """ Test that a Interest verb is produced.
    """

    mock = Sysmock()
    mock.system.add_unused_local_address(CLIENT)

    downstream_handler = unittest.mock.Mock()

    with patch(mock):
        loop = Mainloop()

        mock.expect_tcp_syn(CLIENT, SERVER)
        mock.do_tcp_syn_ack(SERVER, CLIENT)
        mock.do_tcp_input(SERVER, CLIENT, packets.welcome())

        # send the packet
        mock.do_tcp_input(
            SERVER, CLIENT,
            packets.interest(
                postref=1234,
                name=b'thename',
                status=packets.INTEREST_STATUS_NOINTEREST,
                topic=b'topic',
            ))

        conn = NxtcpConnection(loop, SERVER.address)
        conn.set_downstream_handler(downstream_handler)

        mock.run_events(loop.run_once)

        # verify the verb
        downstream_handler.assert_called_once_with(
            verbs.InterestVerb(
                postref=1234,
                name=b'thename',
                status=verbs.InterestVerb.STATUS_NO_INTEREST,
                topic=b'topic',
            ))
コード例 #9
0
def test_call_2(self):
    """ Test that a Call verb is produced.
    """

    mock = Sysmock()
    mock.system.add_unused_local_address(CLIENT)

    downstream_handler = unittest.mock.Mock()

    with patch(mock):
        loop = Mainloop()

        mock.expect_tcp_syn(CLIENT, SERVER)
        mock.do_tcp_syn_ack(SERVER, CLIENT)
        mock.do_tcp_input(SERVER, CLIENT, packets.welcome())

        # send the packet
        mock.do_tcp_input(
            SERVER, CLIENT,
            packets.call(
                unidirectional=True,
                postref=1234,
                name=b'name',
                payload=b'payload',
            ))

        conn = NxtcpConnection(loop, SERVER.address)
        conn.set_downstream_handler(downstream_handler)

        mock.run_events(loop.run_once)

        # verify the verb
        downstream_handler.assert_called_once_with(
            verbs.CallVerb(
                unidirectional=True,
                postref=None,
                name=b'name',
                payload=b'payload',
            ))
コード例 #10
0
def test_keepalive_1(self):
    """ Test if the connection will respond with a pong when the server sends a ping.
    """

    mock = Sysmock()
    mock.system.add_unused_local_address(CLIENT)

    with patch(mock):
        loop = Mainloop()

        mock.expect_tcp_syn(CLIENT, SERVER)
        mock.do_tcp_syn_ack(SERVER, CLIENT)
        mock.do_tcp_input(SERVER, CLIENT, packets.welcome())

        conn = NxtcpConnection(loop, SERVER.address)

        mock.expect_sleep(5.0)
        mock.do_tcp_input(SERVER, CLIENT, packets.ping())

        mock.expect_tcp_output(CLIENT, SERVER, packets.pong())

        mock.run_events(loop.run_once)
コード例 #11
0
def test_byebye_2(self):
    """ Test that the client closes the connection when the server sends a byebye packet AFTER the welcome packet.
    """

    mock = Sysmock()
    mock.system.add_unused_local_address(CLIENT)

    with patch(mock):
        loop = Mainloop()

        mock.expect_tcp_syn(CLIENT, SERVER)
        mock.do_tcp_syn_ack(SERVER, CLIENT)
        mock.do_tcp_input(SERVER, CLIENT, packets.welcome())

        conn = NxtcpConnection(loop, SERVER.address)

        mock.run_events(loop.run_once)

        mock.do_tcp_input(SERVER, CLIENT, packets.byebye())

        mock.expect_tcp_fin(CLIENT, SERVER)

        mock.run_events(loop.run_once)
コード例 #12
0
def test_message_3(self):
    """ Test that a Message verb is produced.
    """

    mock = Sysmock()
    mock.system.add_unused_local_address(CLIENT)

    downstream_handler = unittest.mock.Mock()

    with patch(mock):
        loop = Mainloop()

        mock.expect_tcp_syn(CLIENT, SERVER)
        mock.do_tcp_syn_ack(SERVER, CLIENT)
        mock.do_tcp_input(SERVER, CLIENT, packets.welcome())

        # send the packet
        mock.do_tcp_input(
            SERVER, CLIENT,
            packets.message(
                messageref=1234,
                status=packets.MESSAGE_STATUS_UNREACHABLE,
                payload=b'ignoreme',
            ))

        conn = NxtcpConnection(loop, SERVER.address)
        conn.set_downstream_handler(downstream_handler)

        mock.run_events(loop.run_once)

        # verify the verb
        downstream_handler.assert_called_once_with(
            verbs.MessageVerb(
                messageref=1234,
                status=verbs.MessageVerb.STATUS_UNREACHABLE,
                payload=None,
            ))
コード例 #13
0
def test_subscribe_1(self):
    """ Test that a subscribe packet is send.
    """

    mock = Sysmock()
    mock.system.add_unused_local_address(CLIENT)

    with patch(mock):
        loop = Mainloop()

        mock.expect_tcp_syn(CLIENT, SERVER)
        mock.do_tcp_syn_ack(SERVER, CLIENT)
        mock.do_tcp_input(SERVER, CLIENT, packets.welcome())

        conn = NxtcpConnection(loop, SERVER.address)

        mock.run_events(loop.run_once)

        # the expected packet
        mock.expect_tcp_output(
            CLIENT, SERVER,
            packets.subscribe(
                messageref=1234,
                name=b'name',
                topic=b'topic',
            ))

        # put the verb upstream
        conn.send_verb(
            verbs.SubscribeVerb(
                messageref=1234,
                name=b'name',
                topic=b'topic',
            ))

        mock.run_events(loop.run_once)
コード例 #14
0
 def test_welcome(self):
     self.assertEncodePacket(WelcomePacket(1, 1), packets.welcome())