def test_ping_pong_server(self): self.stupcore.get_timeout = lambda x: 0.1 self.stupcore.NAGLE_TIMEOUT = 0.0 self.stupcore.RETRY_THRESHOLD = 2 self.stupcore.send_seq_id = 0 self.stupcore.output_buffer = RingBuffer(1000, 0) syn_pack = StupPacket.SynPacket() self.stupcore.recv(syn_pack) self.assertEqual(self.stupcore.state.state_nr, StateMachine.LISTEN) self.assertEqual(self.protocol.last_packet().syn, 1) self.assertEqual(self.protocol.last_packet().ack, 1) ack_pack = StupPacket.AckPacket() ack_pack.ack_number = 1 ack_pack.psh = 1 self.stupcore.recv(ack_pack) self.assertEqual(self.stupcore.state.state_nr, StateMachine.ESTABLISHED) ping_pack = StupPacket.Packet(b'ping') ping_pack.seq_number = 1 self.stupcore.recv(ping_pack) yield TwistedUtils.sleep(0.2) self.assertEqual(self.protocol.last_packet().ack, 1) self.assertEqual(self.protocol.last_packet().syn, 0) self.stupcore.send(b'pong') yield TwistedUtils.sleep(0.5) self.assertEqual(self.stupcore.state.state_nr, StateMachine.RST)
def test_retry_syn_ack(self): self.stupcore.get_timeout = lambda x: 0.1 self.stupcore.RETRY_THRESHOLD = 3 syn_pack = StupPacket.SynPacket() self.stupcore.recv(syn_pack) self.assertEqual(self.protocol.last_packet().syn, 1) self.assertEqual(self.protocol.last_packet().ack, 1) self.assertEqual(self.stupcore.state.state_nr, StateMachine.LISTEN) # retry & rst yield TwistedUtils.sleep(0.5) self.assertEqual(self.stupcore.state.state_nr, StateMachine.RST) for i, item in enumerate(self.protocol.output_buffer): packet = StupPacket.Packet.deserialize(item) print(packet) if i < 3: self.assertEquals(packet.syn, 1) self.assertEquals(packet.ack, 1) else: self.assertEquals(packet.ack, 0) self.assertEquals(packet.syn, 0) self.assertEquals(packet.rst, 1) self.assertEquals(len(self.protocol.output_buffer), 3 + 5)
def test_syn_then_reset(self): p = FakeProtocol() w = p.worker c = p.stupcore self.assertEqual(p.state.state_nr, ServerStates.CLOSED) p.recv(StupPacket.SynPacket()) self.assertEqual(p.state.state_nr, ServerStates.LISTEN) self.assertEqual(c.recv_buffer[0].syn, 1) p.recv(StupPacket.SynPacket()) self.assertEqual(p.state.state_nr, ServerStates.LISTEN) self.assertEqual(len(c.recv_buffer), 1) p.recv(StupPacket.Packet()) self.assertEqual(p.state.state_nr, ServerStates.LISTEN) self.assertEqual(len(c.recv_buffer), 1)
def test_connection_established(self): p = FakeProtocol() w = p.worker c = p.stupcore self.assertEqual(p.state.state_nr, ServerStates.CLOSED) p.recv(StupPacket.SynPacket()) self.assertEqual(p.state.state_nr, ServerStates.LISTEN) self.assertEqual(c.recv_buffer[0].syn, 1) p.recv(StupPacket.AckPacket()) self.assertEqual(p.state.state_nr, ServerStates.ESTABLISHED) self.assertEqual(c.recv_buffer[1].ack, 1)
def test_fast_resend(self): self.stupcore.get_timeout = lambda x: 0.5 self.stupcore.NAGLE_TIMEOUT = 0.0 self.stupcore.RETRY_THRESHOLD = 2 self.stupcore.FAST_RESEND_THRESHOLD = 5 self.stupcore.send_seq_id = 0 self.stupcore.output_buffer = RingBuffer(1000, 0) syn_pack = StupPacket.SynPacket() self.stupcore.recv(syn_pack) self.assertEqual(self.stupcore.state.state_nr, StateMachine.LISTEN) self.assertEqual(self.protocol.last_packet().syn, 1) self.assertEqual(self.protocol.last_packet().ack, 1) ack_pack = StupPacket.AckPacket() ack_pack.ack_number = 1 ack_pack.psh = 1 self.stupcore.recv(ack_pack) self.assertEqual(self.stupcore.state.state_nr, StateMachine.ESTABLISHED) ping_pack = StupPacket.Packet(b'ping') ping_pack.seq_number = 1 self.stupcore.recv(ping_pack) yield TwistedUtils.sleep(0.3) self.assertEqual(self.protocol.last_packet().ack, 1) self.assertEqual(self.protocol.last_packet().syn, 0) self.stupcore.send(b'pong1') yield TwistedUtils.sleep(0.1) self.stupcore.send(b'pong2') yield TwistedUtils.sleep(0.1) self.stupcore.send(b'pong3') yield TwistedUtils.sleep(0.1) for i in range(20): ack_pack = StupPacket.AckPacket() ack_pack.ack_number = 6 self.stupcore.recv(ack_pack) yield TwistedUtils.sleep(0.5) self.assertEqual( sum( map(lambda bin_: StupPacket.deserialize(bin_).urg, self.protocol.output_buffer)), 4) self.assertEqual(self.stupcore.state.state_nr, StateMachine.RST)
def test_happy_path(self): str_a = "hello world" str_b = "are you playing golf, or you're just f*****g around" p = FakeProtocol() w = p.worker c = p.stupcore self.assertEqual(p.state.state_nr, ServerStates.CLOSED) p.recv(StupPacket.SynPacket()) self.assertEqual(p.state.state_nr, ServerStates.LISTEN) self.assertEqual(c.recv_buffer[0].syn, 1) p.recv(StupPacket.SynPacket()) self.assertEqual(p.state.state_nr, ServerStates.LISTEN) self.assertEqual(len(c.recv_buffer), 1) p.recv(StupPacket.AckPacket()) self.assertEqual(p.state.state_nr, ServerStates.ESTABLISHED) self.assertEqual(len(c.recv_buffer), 2) p.recv(StupPacket.Packet(str_a.encode('UTF-8'))) self.assertEqual(p.state.state_nr, ServerStates.ESTABLISHED) self.assertEqual(len(c.recv_buffer), 3) p.recv(StupPacket.Packet(str_a.encode('UTF-8'))) self.assertEqual(p.state.state_nr, ServerStates.ESTABLISHED) self.assertEqual(len(c.recv_buffer), 4) p.recv(StupPacket.FinPacket()) self.assertEqual(p.state.state_nr, ServerStates.ESTABLISHED) self.assertEqual(len(c.recv_buffer), 5) p.recv(StupPacket.RstPacket()) self.assertEqual(p.state.state_nr, ServerStates.RST) self.assertEqual(len(c.recv_buffer), 5)
def test_happy_path(self): sm = StupPacket.SynPacket() sm.seq_number = 1 self.protocol.datagramReceived(StupPacket.serialize(sm), self.peer_addr) self.assertEqual(self.protocol.output, b'') self.assertEqual(self.protocol.state.state_nr, StateMachine.LISTEN) seq_id = 0 pack = StupPacket.deserialize(self.transport.written[0][0]) self.seq_id = pack.seq_number self.assertEqual(pack.syn, 1) self.assertEqual(pack.ack, 1) am = StupPacket.AckPacket() am.seq_number = 1 am.ack_number = seq_id self.protocol.datagramReceived(StupPacket.serialize(am), self.peer_addr) self.assertTrue(len(self.transport.written), 1) self.assertEqual(self.protocol.state.state_nr, StateMachine.ESTABLISHED) msg1 = StupPacket.Packet(b'de') msg1.seq_number = 5 msg2 = StupPacket.Packet(b'abc') msg2.seq_number = 2 self.protocol.datagramReceived(StupPacket.serialize(msg1), self.peer_addr) self.assertTrue(len(self.transport.written), 1) self.assertEqual(self.protocol.output, b'') self.protocol.datagramReceived(StupPacket.serialize(msg2), self.peer_addr) self.assertTrue(len(self.transport.written), 2) self.assertEqual(self.protocol.output, b'abcde') self.assertEqual(self.protocol.state.state_nr, StateMachine.ESTABLISHED)
def test_build_connection(self): self.stupcore.send_seq_id = 0 self.stupcore.output_buffer = RingBuffer(1000, 0) self.stupcore.get_timeout = lambda x: 0.1 self.stupcore.NAGLE_TIMEOUT = 0.2 self.stupcore.RETRY_THRESHOLD = 3 syn_pack = StupPacket.SynPacket() self.stupcore.recv(syn_pack) self.assertEqual(self.stupcore.state.state_nr, StateMachine.LISTEN) self.assertEqual(self.protocol.last_packet().syn, 1) self.assertEqual(self.protocol.last_packet().ack, 1) ack_pack = StupPacket.AckPacket() ack_pack.ack_number = 1 self.stupcore.recv(ack_pack) self.assertEqual(self.stupcore.state.state_nr, StateMachine.ESTABLISHED) self.stupcore.reset()