Esempio n. 1
0
    def test_file_transfer(self):
        self.stupcore.get_timeout = lambda x: 0.2
        self.stupcore.NAGLE_TIMEOUT = 1
        self.stupcore.RETRY_THRESHOLD = 2
        self.stupcore.SEND_CHUNK_SIZE = 10
        self.stupcore.output_buffer = RingBuffer(1000, 990)
        self.stupcore.send_seq_id = 990

        self.stupcore.send(b'', syn=1)
        self.assertEqual(self.stupcore.output_buffer.size(), 1)

        syn_ack_pack = StupPacket.SynAckPacket()
        syn_ack_pack.ack_number = 991

        self.stupcore.recv(syn_ack_pack)
        self.assertEqual(self.stupcore.output_buffer.size(), 0)

        self.assertEqual(self.stupcore.state.state_nr,
                         StateMachine.ESTABLISHED)

        packet = StupPacket.Packet(b'a' * 10)
        packet.seq_number = 1

        self.assertEqual(self.stupcore.recv(packet), b'a' * 10)

        packet = StupPacket.Packet(b'c' * 10)
        packet.seq_number = 21
        self.assertEqual(self.stupcore.recv(packet), b'')

        packet = StupPacket.Packet(b'b' * 10)
        packet.seq_number = 11
        self.assertEqual(self.stupcore.recv(packet), b'b' * 10 + b'c' * 10)

        self.stupcore.reset()
Esempio n. 2
0
    def test_conection_fin(self):
        p = FakeProtocol()
        w = p.worker

        self.assertEqual(p.state.state_nr, ClientStates.CLOSED)

        p.send(b'', syn=1)
        self.assertEqual(w.send_buffer[0][1]['syn'], 1)
        self.assertEqual(p.state.state_nr, ClientStates.LISTEN)

        syn_ack_pack = StupPacket.SynAckPacket()
        p.recv(syn_ack_pack)
        self.assertEqual(p.stupcore.recv_buffer[0].syn, 1)
        self.assertEqual(p.stupcore.recv_buffer[0].ack, 1)
        self.assertEqual(p.state.state_nr, ClientStates.ESTABLISHED)

        p.send(b'foo')
        self.assertEqual(w.send_buffer[1][0], b'foo')
        self.assertTrue(not w.send_buffer[1][1])

        pack = StupPacket.Packet(b'bar')
        p.recv(pack)
        self.assertEqual(p.stupcore.recv_buffer[1].data, b'bar')
        self.assertEqual(p.state.state_nr, ClientStates.ESTABLISHED)

        fin_pack = StupPacket.FinPacket()
        p.recv(fin_pack)
        self.assertEqual(p.state.state_nr, ClientStates.ESTABLISHED)

        rst_pack = StupPacket.RstPacket()
        p.recv(rst_pack)
        self.assertEqual(p.state.state_nr, ClientStates.RST)
Esempio n. 3
0
    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)
Esempio n. 4
0
    def test_ping_pong_client(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_ack_pack = StupPacket.SynAckPacket()
        syn_ack_pack.ack_number = 1

        self.stupcore.send(b'', syn=1)
        self.assertEqual(self.stupcore.output_buffer.size(), 1)

        self.stupcore.recv(syn_ack_pack)
        self.assertEqual(self.stupcore.output_buffer.size(), 0)

        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)
        self.assertEqual(self.protocol.last_packet().ack, 1)
        self.assertEqual(self.protocol.last_packet().syn, 0)
        self.assertEqual(self.stupcore.output_buffer.size(), 0)

        self.stupcore.send(b'pong')
        yield TwistedUtils.sleep(0.5)
        self.assertEqual(self.stupcore.output_buffer.size(), len(b'pong'))

        self.assertEqual(self.stupcore.state.state_nr, StateMachine.RST)
        self.stupcore.cancel_all_defers()
Esempio n. 5
0
    def test_invalid_syn(self):
        p = FakeProtocol()
        w = p.worker
        c = p.stupcore

        self.assertEqual(p.state.state_nr, ServerStates.CLOSED)
        p.recv(StupPacket.Packet())
        self.assertEqual(p.state.state_nr, ServerStates.RST)
Esempio n. 6
0
    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)
Esempio n. 7
0
    def send_msg(self, msg, **kwargs):
        assert isinstance(msg, bytes)
        syn = kwargs.get('syn', 0)
        fin = kwargs.get('fin', 0)
        psh = kwargs.get('psh', 0)

        if not msg and not syn and not fin:
            packet = StupPacket.Packet()
            packet.ack = 1
            packet.psh = psh
            packet.ack_number = self.next_ack_id()
            self.last_ack_id = packet.ack_number
            self.send_ctrl_packet(packet)
            return

        pack_size = len(msg) + (1 if (syn or fin) else 0)

        chunk_num = int(math.floor((pack_size + self.SEND_CHUNK_SIZE - 1) / self.SEND_CHUNK_SIZE))
        for chunk_idx in range(chunk_num):
            chunk = msg[chunk_idx * self.SEND_CHUNK_SIZE: (chunk_idx + 1) * self.SEND_CHUNK_SIZE]

            l = self.send_seq_id
            r = self.send_seq_id + len(chunk) + (1 if (syn or fin) else 0)

            self.send_seq_id = r

            packet = StupPacket.Packet(chunk)
            packet.seq_number = l
            packet.syn = syn
            packet.psh = psh

            if chunk_idx == chunk_num - 1 and fin:
                packet.fin = 1

            packet_item = self.PacketItem(packet, 0)
            self.output_buffer.append(ContainerItem(l, r, packet_item))
            self.send_packet(packet)
Esempio n. 8
0
    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)
Esempio n. 9
0
    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)
Esempio n. 10
0
    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)