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'])
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'])
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'])
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'])
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'])
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'])
def test_request(self): self.assertDecodePacket( packets.request(b'thename', False, 12345, 1500, b'thepayload'), RequestPacket, name=b'thename', unidirectional=False, messageref=12345, timeout=1.5, payload=b'thepayload' ) self.assertDecodePacket( packets.request(b'thename', True, 12345, 1500, b'thepayload'), RequestPacket, name=b'thename', unidirectional=True, messageref=None, timeout=1.5, payload=b'thepayload' )
def test_request_no_responder_2(self): """ Test if a Request packet is NOT responded to when send in unidirectional mode. This is verified by expecting a ping packet because of the keepalive mechanism. """ s = setup_story(1) s.do_remote_send( PEER1, LHOST, packets.request(b'testname', True, 1234, 1000, b'thepayload')) s.expect_local_wait(10.0) s.expect_local_send(LHOST, PEER1, packets.ping()) with SysMock(s): main(['--nxtcp', ':9999'])
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'])
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'])
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)
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'])