示例#1
0
 def setUp(self):
     self.addr = ('127.0.0.1', 9999,)
     self.rrq = RRQ + b'\x00filename1\x00octet\x00'
     self.proto = RRQProtocol(self.rrq, MagicMock, self.addr, {})
     self.proto.set_proto_attributes()
     self.proto.h_timeout = MagicMock()
     self.proto.file_handler = MagicMock()
     self.proto.file_handler.read_chunk = MagicMock(return_value=b'AAAA')
     self.proto.counter = 10
     self.proto.transport = MagicMock()
示例#2
0
 def setUp(self):
     self.addr = ('127.0.0.1', 9999,)
     self.rrq = RRQ + b'\x00filename1\x00octet\x00'
     self.proto = RRQProtocol(self.rrq, MagicMock, self.addr, {})
     self.proto.set_proto_attributes()
     self.proto.h_timeout = MagicMock()
     self.proto.file_handler = MagicMock()
     self.proto.file_handler.read_chunk = MagicMock(return_value=b'AAAA')
     self.proto.counter = 10
     self.proto.transport = MagicMock()
示例#3
0
class TestRRQProtocol(t.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.packet_factory = TFTPPacketFactory()

    def setUp(self):
        self.addr = ('127.0.0.1', 9999,)
        self.rrq = RRQ + b'\x00filename1\x00octet\x00'
        self.proto = RRQProtocol(self.rrq, MagicMock, self.addr, {})
        self.proto.set_proto_attributes()
        self.proto.h_timeout = MagicMock()
        self.proto.file_handler = MagicMock()
        self.proto.file_handler.read_chunk = MagicMock(return_value=b'AAAA')
        self.proto.handle_err_pkt = MagicMock()
        self.proto.counter = 10
        self.proto.transport = MagicMock()

    def test_get_next_chunk_of_data(self):
        rsp = self.proto.next_datagram()

        self.assertEqual(rsp.to_bytes(), DAT + b'\x00\x0aAAAA')

    def test_get_sequence_of_chunks(self):
        self.proto.file_handler.finished = False
        ack1 = ACK + b'\x00\x0a'
        ack2 = ACK + b'\x00\x0b'
        dat1 = DAT + b'\x00\x0bAAAA'
        dat2 = DAT + b'\x00\x0cAAAA'

        self.proto.datagram_received(ack1, self.addr)
        self.proto.datagram_received(ack2, self.addr)

        calls = [call(dat1, self.addr), call(dat2, self.addr)]
        self.proto.transport.sendto.assert_has_calls(calls)

    def test_get_next_window_of_data(self):
        self.proto.file_handler.finished = False
        self.proto.opts[b'windowsize'] = 2
        self.proto.packets = [None] * 2
        ack1 = ACK + b'\x00\x0a'
        dat1 = DAT + b'\x00\x0bAAAA'
        dat2 = DAT + b'\x00\x0cAAAA'

        self.proto.datagram_received(ack1, self.addr)

        calls = [call(dat1, self.addr), call(dat2, self.addr)]
        self.proto.transport.sendto.assert_has_calls(calls)

    def test_get_sequence_of_windows(self):
        self.proto.file_handler.finished = False
        self.proto.opts[b'windowsize'] = 2
        self.proto.packets = [None] * 2
        ack1 = ACK + b'\x00\x0a'
        ack2 = ACK + b'\x00\x0c'
        dat1 = DAT + b'\x00\x0bAAAA'
        dat2 = DAT + b'\x00\x0cAAAA'
        dat3 = DAT + b'\x00\x0dAAAA'
        dat4 = DAT + b'\x00\x0eAAAA'

        self.proto.datagram_received(ack1, self.addr)
        self.proto.datagram_received(ack2, self.addr)
        calls = [call(dat1, self.addr), call(dat2, self.addr),
                 call(dat3, self.addr), call(dat4, self.addr)]
        self.proto.transport.sendto.assert_has_calls(calls)

    def test_send_last_packet(self):
        self.proto.file_handler.read_chunk = MagicMock(return_value=b'AA')
        self.proto.file_handler.finished = False
        ack1 = ACK + b'\x00\x0a'
        ack2 = ACK + b'\x00\x0b'

        self.proto.datagram_received(ack1, self.addr)

        self.proto.transport.sendto.assert_called_with(DAT + b'\x00\x0bAA',
                                                       self.addr)
        self.proto.file_handler.finished = True
        self.proto.datagram_received(ack2, self.addr)
        self.assertTrue(self.proto.transport.close.called)

    def test_bad_packet(self):
        bad_msg = DAT + b'\x00\x0aAAAA'

        self.proto.datagram_received(bad_msg, self.addr)

        self.assertFalse(self.proto.transport.sendto.called)

    def test_bad_tid(self):
        # this should get moved to base tests, same for wrq
        addr = ('127.0.0.1', 8888,)
        ack1 = ACK + b'\x00\x0a'

        self.proto.datagram_received(ack1, addr)

        err_tid = self.packet_factory.err_unknown_tid()

        self.proto.transport.sendto.assert_called_with(err_tid.to_bytes(),
                                                       addr)

    def test_bad_packet_sequence_is_ignored(self):
        ack1 = ACK + b'\x00\x0b'

        self.proto.datagram_received(ack1, self.addr)

        self.assertFalse(self.proto.transport.sendto.called)
        self.assertFalse(self.proto.file_handler.send.called)

    def test_bad_packet_sequence_starts_new_window(self):
        self.proto.file_handler.finished = False
        self.proto.opts[b'windowsize'] = 2
        self.proto.packets = [None] * 2
        ack1 = ACK + b'\x00\x0a'
        ack2 = ACK + b'\x00\x0b'  # ACK of block_no within window
        dat1 = DAT + b'\x00\x0bAAAA'
        dat2 = DAT + b'\x00\x0cAAAA'
        dat3 = DAT + b'\x00\x0dAAAA'

        # After ACK of block_no \x0a, block_no \x0b and \x0c are sent
        self.proto.datagram_received(ack1, self.addr)
        # After ACK of block_no \x0b, block_no \x0c and \x0d are sent
        self.proto.datagram_received(ack2, self.addr)

        calls = [call(dat1, self.addr), call(dat2, self.addr),
                 call(dat2, self.addr), call(dat3, self.addr)]
        self.proto.transport.sendto.assert_has_calls(calls)

    def test_roll_over(self):
        self.proto.file_handler.finished = False
        self.proto.counter = (2 ** 16) - 1
        ack1 = ACK + b'\xff\xff'
        ack2 = ACK + b'\x00\x00'
        dat1 = DAT + b'\x00\x00AAAA'
        dat2 = DAT + b'\x00\x01AAAA'

        self.proto.datagram_received(ack1, self.addr)
        self.proto.datagram_received(ack2, self.addr)

        calls = [call(dat1, self.addr), call(dat2, self.addr)]
        self.proto.transport.sendto.assert_has_calls(calls)

    def test_err_received(self):
        err = ERR + b'\x00TFTP Aborted.\x00'
        self.proto.datagram_received(err, self.addr)
        self.assertTrue(self.proto.handle_err_pkt.called)
        self.assertFalse(self.proto.transport.sendto.called)

    def test_err_received_windowsize(self):
        err = ERR + b'\x00TFTP Aborted.\x00'
        self.proto.opts[b'windowsize'] = 2
        self.proto.datagram_received(err, self.addr)
        self.assertTrue(self.proto.handle_err_pkt.called)
        self.assertFalse(self.proto.transport.sendto.called)
示例#4
0
class TestRRQProtocol(t.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.packet_factory = TFTPPacketFactory()

    def setUp(self):
        self.addr = (
            '127.0.0.1',
            9999,
        )
        self.rrq = RRQ + b'\x00filename1\x00octet\x00'
        self.proto = RRQProtocol(self.rrq, MagicMock, self.addr, {})
        self.proto.set_proto_attributes()
        self.proto.h_timeout = MagicMock()
        self.proto.file_handler = MagicMock()
        self.proto.file_handler.read_chunk = MagicMock(return_value=b'AAAA')
        self.proto.handle_err_pkt = MagicMock()
        self.proto.counter = 10
        self.proto.transport = MagicMock()

    def test_get_next_chunk_of_data(self):
        rsp = self.proto.next_datagram()

        self.assertEqual(rsp.to_bytes(), DAT + b'\x00\x0aAAAA')

    def test_get_sequence_of_chunks(self):
        self.proto.file_handler.finished = False
        ack1 = ACK + b'\x00\x0a'
        ack2 = ACK + b'\x00\x0b'
        dat1 = DAT + b'\x00\x0bAAAA'
        dat2 = DAT + b'\x00\x0cAAAA'

        self.proto.datagram_received(ack1, self.addr)
        self.proto.datagram_received(ack2, self.addr)

        calls = [call(dat1, self.addr), call(dat2, self.addr)]
        self.proto.transport.sendto.assert_has_calls(calls)

    def test_send_last_packet(self):
        self.proto.file_handler.read_chunk = MagicMock(return_value=b'AA')
        self.proto.file_handler.finished = False
        ack1 = ACK + b'\x00\x0a'
        ack2 = ACK + b'\x00\x0b'

        self.proto.datagram_received(ack1, self.addr)

        self.proto.transport.sendto.assert_called_with(DAT + b'\x00\x0bAA',
                                                       self.addr)
        self.proto.file_handler.finished = True
        self.proto.datagram_received(ack2, self.addr)
        self.assertTrue(self.proto.transport.close.called)

    def test_bad_packet(self):
        bad_msg = DAT + b'\x00\x0aAAAA'

        self.proto.datagram_received(bad_msg, self.addr)

        self.assertFalse(self.proto.transport.sendto.called)

    def test_bad_tid(self):
        # this should get moved to base tests, same for wrq
        addr = (
            '127.0.0.1',
            8888,
        )
        ack1 = ACK + b'\x00\x0a'

        self.proto.datagram_received(ack1, addr)

        err_tid = self.packet_factory.err_unknown_tid()

        self.proto.transport.sendto.assert_called_with(err_tid.to_bytes(),
                                                       addr)

    def test_bad_packet_sequence_is_ignored(self):
        ack1 = ACK + b'\x00\x0b'

        self.proto.datagram_received(ack1, self.addr)

        self.assertFalse(self.proto.transport.sendto.called)
        self.assertFalse(self.proto.file_handler.send.called)

    def test_roll_over(self):
        self.proto.file_handler.finished = False
        self.proto.counter = (2**16) - 1
        ack1 = ACK + b'\xff\xff'
        ack2 = ACK + b'\x00\x00'
        dat1 = DAT + b'\x00\x00AAAA'
        dat2 = DAT + b'\x00\x01AAAA'

        self.proto.datagram_received(ack1, self.addr)
        self.proto.datagram_received(ack2, self.addr)

        calls = [call(dat1, self.addr), call(dat2, self.addr)]
        self.proto.transport.sendto.assert_has_calls(calls)

    def test_err_received(self):
        err = ERR + b'\x00TFTP Aborted.\x00'
        self.proto.datagram_received(err, self.addr)
        self.assertTrue(self.proto.handle_err_pkt.called)
        self.assertFalse(self.proto.transport.sendto.called)