コード例 #1
0
    def test_call(self):
        self.assertEncodePacket(
            CallPacket(False, 1234, b'name', b'thepayload'),
            packets.call(False, 1234, b'name', b'thepayload'))

        self.assertEncodePacket(CallPacket(True, 1234, b'name', b'thepayload'),
                                packets.call(True, 0, b'name', b'thepayload'))

        self.assertEncodePacket(CallPacket(True, None, b'name', b'thepayload'),
                                packets.call(True, 0, b'name', b'thepayload'))
コード例 #2
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'])
コード例 #3
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'])
コード例 #4
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'])
コード例 #5
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'])
コード例 #6
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'])
コード例 #7
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'])
コード例 #8
0
    def test_request_unidirectional_1(self):
        """ Test if the first client receives an unidirectional Call packet when the
        second client sends an unidirectional 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', True, 0, 0, b'thepayload'))
        s.expect_local_send(LHOST, PEER1,
                            packets.call(True, 0, b'testname', b'thepayload'))

        with SysMock(s):
            main(['--nxtcp', ':9999'])
コード例 #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_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'])