Exemple #1
0
    def test_subscribe_post_1(self):
        """ Test if mulitple post on a topic will indeed result in multiple messages to all subscribers.
        """

        s = setup_story(3)

        s.do_remote_send(PEER1, LHOST,
                         packets.login(b'testname', False, False, False))
        s.expect_local_send(
            LHOST, PEER1,
            packets.session(b'testname', packets.SESSION_STATE_ACTIVE))

        s.do_remote_send(PEER2, LHOST,
                         packets.subscribe(1234, b'testname', b'testtopic'))

        s.expect_local_send(
            LHOST, PEER1,
            packets.interest(1, packets.INTEREST_STATUS_INTEREST,
                             b'testtopic'))

        s.do_remote_send(PEER3, LHOST,
                         packets.subscribe(1234, b'testname', b'testtopic'))

        s.do_remote_send(PEER1, LHOST, packets.post(1, b'testpayload'))

        s.expect_local_send(
            LHOST, PEER2,
            packets.message(1234, packets.MESSAGE_STATUS_OK, b'testpayload'))
        s.expect_local_send(
            LHOST, PEER3,
            packets.message(1234, packets.MESSAGE_STATUS_OK, b'testpayload'))

        with SysMock(s):
            main(['--nxtcp', ':9999'])
    def test_message(self):
        self.assertEncodePacket(
            MessagePacket(1234, MessagePacket.STATUS_OK, b'thepayload'),
            packets.message(1234, packets.MESSAGE_STATUS_OK, b'thepayload'))

        self.assertEncodePacket(
            MessagePacket(1234, MessagePacket.STATUS_TIMEOUT, b'thepayload'),
            packets.message(1234, packets.MESSAGE_STATUS_TIMEOUT))

        self.assertEncodePacket(
            MessagePacket(1234, MessagePacket.STATUS_UNREACHABLE,
                          b'thepayload'),
            packets.message(1234, packets.MESSAGE_STATUS_UNREACHABLE))
Exemple #3
0
    def test_post_after_timeout(self):
        """ Test if a post is ignored when the request timeout is already expired.
        """

        s = setup_story(2)

        s.do_remote_send(PEER1, LHOST,
                         packets.login(b'testname', False, False, False))
        s.expect_local_send(
            LHOST, PEER1,
            packets.session(b'testname', packets.SESSION_STATE_ACTIVE))

        s.do_remote_send(
            PEER2, LHOST,
            packets.request(b'testname', False, 1234, 1000, b'thepayload'))
        s.expect_local_send(LHOST, PEER1,
                            packets.call(False, 1, b'testname', b'thepayload'))

        s.expect_local_wait(1.0)

        s.expect_local_send(
            LHOST, PEER2, packets.message(1234,
                                          packets.MESSAGE_STATUS_TIMEOUT))

        s.do_remote_send(PEER2, LHOST, packets.post(1, b'thepayload'))

        # no verify that it is indeed ignored by waiting for the keepalive packets
        s.expect_local_wait(9.0)
        s.expect_local_send(LHOST, PEER1, packets.ping())
        s.expect_local_wait(1.0)
        s.expect_local_send(LHOST, PEER2, packets.ping())

        with SysMock(s):
            main(['--nxtcp', ':9999'])
Exemple #4
0
    def test_post_from_wrong_client(self):
        """ Test if a post placed by a third client instead of the first client is correctly handled,
        that is, the unlawfull post of c3 should be ignored, and eventually a timouet message will be
        received by c2.
        """

        s = setup_story(3)

        s.do_remote_send(PEER1, LHOST,
                         packets.login(b'testname', False, False, False))
        s.expect_local_send(
            LHOST, PEER1,
            packets.session(b'testname', packets.SESSION_STATE_ACTIVE))

        s.do_remote_send(
            PEER2, LHOST,
            packets.request(b'testname', False, 1234, 1000, b'thepayload'))
        s.expect_local_send(LHOST, PEER1,
                            packets.call(False, 1, b'testname', b'thepayload'))

        s.do_remote_send(PEER3, LHOST, packets.post(1, b'thepayload'))

        s.expect_local_wait(1.0)

        s.expect_local_send(
            LHOST, PEER2, packets.message(1234,
                                          packets.MESSAGE_STATUS_TIMEOUT))

        with SysMock(s):
            main(['--nxtcp', ':9999'])
Exemple #5
0
    def test_dual_post_1(self):
        """ Check if a second post to the same postref is ignored by the server, and will not
        result in a second Message packet to client.
        """

        s = setup_story(2)

        s.do_remote_send(PEER1, LHOST,
                         packets.login(b'testname', False, False, False))
        s.expect_local_send(
            LHOST, PEER1,
            packets.session(b'testname', packets.SESSION_STATE_ACTIVE))

        s.do_remote_send(
            PEER2, LHOST,
            packets.request(b'testname', False, 1234, 1000, b'thepayload'))
        s.expect_local_send(LHOST, PEER1,
                            packets.call(False, 1, b'testname', b'thepayload'))

        # 1st post
        s.do_remote_send(PEER1, LHOST, packets.post(1, b'thepayload'))
        s.expect_local_send(
            LHOST, PEER2,
            packets.message(1234, packets.MESSAGE_STATUS_OK, b'thepayload'))

        # 2nd post
        s.do_remote_send(PEER1, LHOST, packets.post(1, b'thepayload'))

        # no verify that it is indeed ignored by waiting for the keepalive packets
        s.expect_local_wait(10.0)
        s.expect_local_send(LHOST, PEER1, packets.ping())
        s.expect_local_send(LHOST, PEER2, packets.ping())

        with SysMock(s):
            main(['--nxtcp', ':9999'])
Exemple #6
0
    def test_request_1(self):
        """ Test if the first client receives a message when the second client replies
        to the request.
        """

        s = setup_story(2)

        s.do_remote_send(PEER1, LHOST,
                         packets.login(b'testname', False, False, False))
        s.expect_local_send(
            LHOST, PEER1,
            packets.session(b'testname', packets.SESSION_STATE_ACTIVE))

        s.do_remote_send(
            PEER2, LHOST,
            packets.request(b'testname', False, 1234, 1000, b'thepayload'))
        s.expect_local_send(LHOST, PEER1,
                            packets.call(False, 1, b'testname', b'thepayload'))

        s.do_remote_send(PEER1, LHOST, packets.post(1, b'thepayload'))
        s.expect_local_send(
            LHOST, PEER2,
            packets.message(1234, packets.MESSAGE_STATUS_OK, b'thepayload'))

        with SysMock(s):
            main(['--nxtcp', ':9999'])
Exemple #7
0
    def test_request_timeout_2(self):
        """ Test if the timeout will be the default if unspecified in the request.
        """

        s = setup_story(2)

        s.do_remote_send(PEER1, LHOST,
                         packets.login(b'testname', False, False, False))
        s.expect_local_send(
            LHOST, PEER1,
            packets.session(b'testname', packets.SESSION_STATE_ACTIVE))

        s.do_remote_send(
            PEER2, LHOST,
            packets.request(b'testname', False, 1234, 0, b'thepayload'))
        s.expect_local_send(LHOST, PEER1,
                            packets.call(False, 1, b'testname', b'thepayload'))

        s.expect_local_wait(4.0)

        s.expect_local_send(
            LHOST, PEER2, packets.message(1234,
                                          packets.MESSAGE_STATUS_TIMEOUT))

        with SysMock(s):
            main(['--nxtcp', ':9999'])
Exemple #8
0
    def test_request_timeout_1(self):
        """ Test if the second client will receive a MESSAGE_TIMEOUT packet when a
        request is done to an owned session, but not responded to by the first client.
        """

        s = setup_story(2)

        s.do_remote_send(PEER1, LHOST,
                         packets.login(b'testname', False, False, False))
        s.expect_local_send(
            LHOST, PEER1,
            packets.session(b'testname', packets.SESSION_STATE_ACTIVE))

        s.do_remote_send(
            PEER2, LHOST,
            packets.request(b'testname', False, 1234, 2222, b'thepayload'))
        s.expect_local_send(LHOST, PEER1,
                            packets.call(False, 1, b'testname', b'thepayload'))

        s.expect_local_wait(2.222)

        s.expect_local_send(
            LHOST, PEER2, packets.message(1234,
                                          packets.MESSAGE_STATUS_TIMEOUT))

        with SysMock(s):
            main(['--nxtcp', ':9999'])
Exemple #9
0
    def test_request_no_responder_1(self):
        """ Test if a MESSAGE_UNREACHABLE packet is received when trying to do a request
        to an un-owned session.
        """

        s = setup_story(1)

        s.do_remote_send(
            PEER1, LHOST,
            packets.request(b'testname', False, 1234, 1000, b'thepayload'))
        s.expect_local_send(
            LHOST, PEER1,
            packets.message(1234, packets.MESSAGE_STATUS_UNREACHABLE))

        with SysMock(s):
            main(['--nxtcp', ':9999'])
Exemple #10
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,
            ))
Exemple #11
0
    def test_request_timeout_3(self):
        """ Test if the timeout will be limited if a too long timeout is specified.
        Because we are testing such a long timoeut, we have to account for the packets send by the keepalive
        mechanism as well, that is why it looks a bit messy.
        """

        s = setup_story(2)

        s.do_remote_send(PEER1, LHOST,
                         packets.login(b'testname', False, False, False))
        s.expect_local_send(
            LHOST, PEER1,
            packets.session(b'testname', packets.SESSION_STATE_ACTIVE))

        s.do_remote_send(
            PEER2, LHOST,
            packets.request(b'testname', False, 1234, 60001, b'thepayload'))
        s.expect_local_send(LHOST, PEER1,
                            packets.call(False, 1, b'testname', b'thepayload'))

        for _ in range(5):
            s.expect_local_wait(10.0)
            s.expect_local_send(LHOST, PEER1, packets.ping())
            s.expect_local_send(LHOST, PEER2, packets.ping())
            s.do_remote_send(PEER1, LHOST, packets.pong())
            s.do_remote_send(PEER2, LHOST, packets.pong())

        s.expect_local_wait(10.0)
        s.expect_local_send(LHOST, PEER1, packets.ping())
        s.expect_local_send(
            LHOST, PEER2, packets.message(1234,
                                          packets.MESSAGE_STATUS_TIMEOUT))
        s.expect_local_send(LHOST, PEER2, packets.ping())

        with SysMock(s):
            main(['--nxtcp', ':9999'])