def test_subscribe_2(self): """ Test that multiple subscries will still result in just one interest packet. """ 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.expect_local_wait(10.0) s.expect_local_send(LHOST, PEER1, packets.ping()) s.expect_local_send(LHOST, PEER2, packets.ping()) s.expect_local_send(LHOST, PEER3, 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_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_keepalive_2(self): """ Test if the keepalive timer will be reset if the client sends a PONG packet """ s = setup_story(1) s.expect_local_wait(10.0) s.expect_local_send(LHOST, PEER1, packets.ping()) s.expect_local_wait(9.0) s.do_remote_send(PEER1, LHOST, packets.pong()) s.expect_local_wait(10.0) s.expect_local_send(LHOST, PEER1, packets.ping()) with SysMock(s): main(['--nxtcp', ':9999'])
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'])
def test_keepalive_1(self): """ Test if a client will receive a ping client after 10 seconds, and will be disconnected after another 10 seconds. """ s = setup_story(1) s.expect_local_wait(10.0) s.expect_local_send(LHOST, PEER1, packets.ping()) s.expect_local_wait(10.0) s.expect_local_send(LHOST, PEER1, packets.byebye()) s.expect_local_close(LHOST, PEER1) s.expect_local_idle() with SysMock(s): main(['--nxtcp', ':9999'])
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_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)
def test_ping(self): self.assertEncodePacket(PingPacket(), packets.ping())