Beispiel #1
0
    def test_multiple_packets_from_buffer_with_junk(self):
        data_buffer = ''
        payload = 'abc123'
        packet_type = PacketType.FROM_INSTRUMENT
        junk = 'kj34jk3h45'

        for i in xrange(3):
            # create a packet
            packet = Packet.create(payload, packet_type)[0]
            # get the packet contents as a string
            data_buffer += packet.data + junk

        while True:
            packet, data_buffer = Packet.packet_from_buffer(data_buffer)
            if packet is None:
                break

            self.assertEqual(packet.payload, payload)
            self.assertEqual(packet.header.packet_type, packet_type)
            self.assertEqual(packet.header.payload_size, len(payload))
            self.assertTrue(packet.valid)
            self.assertNotEqual(packet.header.time, 0)
            self.assertIn(junk, data_buffer)

        # Verify there is no data left
        self.assertEqual(data_buffer, junk)
Beispiel #2
0
    def test_multiple_packets_from_buffer_with_junk(self):
        data_buffer = ''
        payload = 'abc123'
        packet_type = PacketType.FROM_INSTRUMENT
        junk = 'kj34jk3h45'

        for i in xrange(3):
            # create a packet
            packet = Packet.create(payload, packet_type)[0]
            # get the packet contents as a string
            data_buffer += packet.data + junk

        while True:
            packet, data_buffer = Packet.packet_from_buffer(data_buffer)
            if packet is None:
                break

            self.assertEqual(packet.payload, payload)
            self.assertEqual(packet.header.packet_type, packet_type)
            self.assertEqual(packet.header.payload_size, len(payload))
            self.assertTrue(packet.valid)
            self.assertNotEqual(packet.header.time, 0)
            self.assertIn(junk, data_buffer)

        # Verify there is no data left
        self.assertEqual(data_buffer, junk)
Beispiel #3
0
    def test_max_size_packet(self):
        payload = 'x' * Packet.max_payload
        packet_type = PacketType.FROM_INSTRUMENT
        packets = Packet.create(payload, packet_type)

        self.assertEqual(len(packets), 2)
        self.assertEqual(packets[0].payload, payload)
        self.assertEqual(packets[1].payload, '')
        self.assertEqual(packets[0].header.time, packets[1].header.time)
Beispiel #4
0
    def test_max_size_packet(self):
        payload = 'x' * Packet.max_payload
        packet_type = PacketType.FROM_INSTRUMENT
        packets = Packet.create(payload, packet_type)

        self.assertEqual(len(packets), 2)
        self.assertEqual(packets[0].payload, payload)
        self.assertEqual(packets[1].payload, '')
        self.assertEqual(packets[0].header.time, packets[1].header.time)
Beispiel #5
0
    def test_create_packet(self):
        payload = 'abc123'
        packet_type = PacketType.FROM_INSTRUMENT
        packet = Packet.create(payload, packet_type)[0]

        self.assertEqual(packet.payload, payload)
        self.assertEqual(packet.header.packet_type, packet_type)
        self.assertEqual(packet.header.payload_size, len(payload))
        self.assertTrue(packet.valid)
        self.assertNotEqual(packet.header.time, 0)
Beispiel #6
0
    def test_create_packet(self):
        payload = 'abc123'
        packet_type = PacketType.FROM_INSTRUMENT
        packet = Packet.create(payload, packet_type)[0]

        self.assertEqual(packet.payload, payload)
        self.assertEqual(packet.header.packet_type, packet_type)
        self.assertEqual(packet.header.payload_size, len(payload))
        self.assertTrue(packet.valid)
        self.assertNotEqual(packet.header.time, 0)
Beispiel #7
0
    def test_large_packet(self):
        payload1 = 'x' * Packet.max_payload
        payload2 = 'abcabc'
        payload = payload1 + payload2
        packet_type = PacketType.FROM_INSTRUMENT
        packets = Packet.create(payload, packet_type)

        self.assertEqual(len(packets), 2)
        self.assertEqual(packets[0].payload, payload1)
        self.assertEqual(packets[1].payload, payload2)
        self.assertEqual(packets[0].header.time, packets[1].header.time)
Beispiel #8
0
    def test_large_packet(self):
        payload1 = 'x' * Packet.max_payload
        payload2 = 'abcabc'
        payload = payload1 + payload2
        packet_type = PacketType.FROM_INSTRUMENT
        packets = Packet.create(payload, packet_type)

        self.assertEqual(len(packets), 2)
        self.assertEqual(packets[0].payload, payload1)
        self.assertEqual(packets[1].payload, payload2)
        self.assertEqual(packets[0].header.time, packets[1].header.time)
Beispiel #9
0
    def test_packet_from_fh(self):
        # create a packet
        payload = 'abc123'
        packet_type = PacketType.FROM_INSTRUMENT
        packet = Packet.create(payload, packet_type)[0]
        # get the packet contents as a string
        fh = StringIO(packet.data)

        packet = Packet.packet_from_fh(fh)

        self.assertEqual(packet.payload, payload)
        self.assertEqual(packet.header.packet_type, packet_type)
        self.assertEqual(packet.header.payload_size, len(payload))
        self.assertTrue(packet.valid)
        self.assertNotEqual(packet.header.time, 0)
Beispiel #10
0
    def test_packet_from_fh(self):
        # create a packet
        payload = 'abc123'
        packet_type = PacketType.FROM_INSTRUMENT
        packet = Packet.create(payload, packet_type)[0]
        # get the packet contents as a string
        fh = StringIO(packet.data)

        packet = Packet.packet_from_fh(fh)

        self.assertEqual(packet.payload, payload)
        self.assertEqual(packet.header.packet_type, packet_type)
        self.assertEqual(packet.header.payload_size, len(payload))
        self.assertTrue(packet.valid)
        self.assertNotEqual(packet.header.time, 0)
Beispiel #11
0
    def test_packet_from_buffer(self):
        # create a packet
        payload = 'abc123'
        packet_type = PacketType.FROM_INSTRUMENT
        packet = Packet.create(payload, packet_type)[0]
        # get the packet contents as a string
        data_buffer = packet.data
        # create a new packet from the buffer
        packet, data_buffer = Packet.packet_from_buffer(data_buffer)

        self.assertEqual(packet.payload, payload)
        self.assertEqual(packet.header.packet_type, packet_type)
        self.assertEqual(packet.header.payload_size, len(payload))
        self.assertTrue(packet.valid)
        self.assertNotEqual(packet.header.time, 0)
        # Verify there is no data left
        self.assertEqual(data_buffer, '')
Beispiel #12
0
    def test_bad_crc(self):
        # create a packet
        payload = 'abc123'
        packet_type = PacketType.FROM_INSTRUMENT
        packet = Packet.create(payload, packet_type)[0]
        # get the packet contents as a string
        data_buffer = packet.data

        # corrupt the data
        data_buffer = data_buffer[:-2] + 'ZZ'

        # create a new packet from the buffer
        packet, _ = Packet.packet_from_buffer(data_buffer)

        # make sure the packet is marked invalid and contains our corrupted payload
        self.assertFalse(packet.valid)
        self.assertEqual(packet.payload, payload[:-2] + 'ZZ')
Beispiel #13
0
    def test_packet_from_buffer(self):
        # create a packet
        payload = 'abc123'
        packet_type = PacketType.FROM_INSTRUMENT
        packet = Packet.create(payload, packet_type)[0]
        # get the packet contents as a string
        data_buffer = packet.data
        # create a new packet from the buffer
        packet, data_buffer = Packet.packet_from_buffer(data_buffer)

        self.assertEqual(packet.payload, payload)
        self.assertEqual(packet.header.packet_type, packet_type)
        self.assertEqual(packet.header.payload_size, len(payload))
        self.assertTrue(packet.valid)
        self.assertNotEqual(packet.header.time, 0)
        # Verify there is no data left
        self.assertEqual(data_buffer, '')
Beispiel #14
0
    def test_bad_crc(self):
        # create a packet
        payload = 'abc123'
        packet_type = PacketType.FROM_INSTRUMENT
        packet = Packet.create(payload, packet_type)[0]
        # get the packet contents as a string
        data_buffer = packet.data

        # corrupt the data
        data_buffer = data_buffer[:-2] + 'ZZ'

        # create a new packet from the buffer
        packet, _ = Packet.packet_from_buffer(data_buffer)

        # make sure the packet is marked invalid and contains our corrupted payload
        self.assertFalse(packet.valid)
        self.assertEqual(packet.payload, payload[:-2] + 'ZZ')
Beispiel #15
0
    def test_packet_from_fh_with_junk(self):
        data_buffer = ''
        payload = 'abc123'
        packet_type = PacketType.FROM_INSTRUMENT
        junk = 'kj34jk3h45'

        for i in xrange(3):
            # create a packet
            packet = Packet.create(payload, packet_type)[0]
            # get the packet contents as a string
            data_buffer += packet.data + junk

        fh = StringIO(data_buffer)
        while True:
            packet = Packet.packet_from_fh(fh)
            if packet is None:
                break

            self.assertEqual(packet.payload, payload)
            self.assertEqual(packet.header.packet_type, packet_type)
            self.assertEqual(packet.header.payload_size, len(payload))
            self.assertTrue(packet.valid)
            self.assertNotEqual(packet.header.time, 0)
            self.assertIn(junk, data_buffer)
Beispiel #16
0
    def test_packet_from_fh_with_junk(self):
        data_buffer = ''
        payload = 'abc123'
        packet_type = PacketType.FROM_INSTRUMENT
        junk = 'kj34jk3h45'

        for i in xrange(3):
            # create a packet
            packet = Packet.create(payload, packet_type)[0]
            # get the packet contents as a string
            data_buffer += packet.data + junk

        fh = StringIO(data_buffer)
        while True:
            packet = Packet.packet_from_fh(fh)
            if packet is None:
                break

            self.assertEqual(packet.payload, payload)
            self.assertEqual(packet.header.packet_type, packet_type)
            self.assertEqual(packet.header.payload_size, len(payload))
            self.assertTrue(packet.valid)
            self.assertNotEqual(packet.header.time, 0)
            self.assertIn(junk, data_buffer)
 def echo(self, data):
     packets = Packet.create('%s - %r\n' % (self.label, data), PacketType.FROM_INSTRUMENT)
     for client in self.clients:
         for packet in packets:
             client.transport.write(packet.data)
 def send_sample(self):
     reactor.callLater(self.delay, self.send_sample)
     packets = Packet.create(self.label + '\n', PacketType.FROM_INSTRUMENT)
     for client in self.clients:
         for packet in packets:
             client.transport.write(packet.data)