Example #1
0
 def test_invalid_crc_raises_exception(self):
     """
     Checks that having a wrong crc raises an exception.
     """
     datagram = b'\x00\x00\x00\xDE\xAD\xDE\xAD\xC0'
     with self.assertRaises(serial_datagram.CRCMismatchError):
         serial_datagram.decode(datagram)
Example #2
0
    def test_that_too_short_sequence_raises_exception(self):
        """
        Checks that decoding a datagram smaller than 5 bytes (CRC + END) raises
        an exception.
        """

        with self.assertRaises(serial_datagram.FrameError):
            serial_datagram.decode(b'\x01\x02\x03\x03')
Example #3
0
 def test_escape_is_unescaped(self):
     """
     Checks that ESC + ESC_ESC is transformed to ESC.
     """
     datagram = b'\xDB\xDD\xC3\x03\xE4\xD1\xC0'
     datagram = serial_datagram.decode(datagram)
     self.assertEqual(b'\xDB', datagram)
Example #4
0
 def test_can_decode_simple_datagram(self):
     """
     Simply tries to decode a complete datagram.
     """
     datagram = b'\x00\x00\x00\xFF\x41\xD9\x12\xC0'
     datagram = bytes(datagram)
     datagram = serial_datagram.decode(datagram)
     self.assertEqual(b'\x00\x00\x00', datagram)
Example #5
0
 def test_that_weird_sequences_work(self):
     """
     Decode order matters. This can be revealed by making a packet
     containing ESC + ESC_END, which would cause a problem if the replace
     happens in the wrong order.
     """
     data = serial_datagram.ESC + serial_datagram.ESC_END
     datagram = serial_datagram.encode(data)
     self.assertEqual(data, serial_datagram.decode(datagram))
Example #6
0
    def test_float_single_precision(self):
        """
        Checks that floats are single precision.
        """
        data = service_call.encode_call("foo", [1.])
        data = serial_datagram.decode(data)

        # Check that we have the marker for single precision float
        self.assertEqual(data[1 + 3 + 1], 0xca)
Example #7
0
    def test_encoding_method_name(self):
        """
        Checks that encoding method works as expected.
        """
        data = service_call.encode_call("foo", [1, 2, 3])
        data = serial_datagram.decode(data)

        u = msgpack.Unpacker(encoding='ascii')
        u.feed(data)
        command = next(u)

        self.assertEqual(command, ['foo', [1, 2, 3]])
Example #8
0
    def test_encoding_method_name(self):
        """
        Checks that encoding method works as expected.
        """
        data = service_call.encode_call("foo", [1, 2, 3])
        data = serial_datagram.decode(data)

        u = msgpack.Unpacker(encoding='ascii')
        u.feed(data)
        command = next(u)

        self.assertEqual(command, ['foo', [1, 2, 3]])
Example #9
0
def decode_call(data):
    """
    Decodes the given method call.

    Returns a tuple containing the method name and the parameters.
    """
    data = serial_datagram.decode(data)
    u = msgpack.Unpacker(encoding='ascii')
    u.feed(data)

    command = next(u)

    return command[0], command[1]
Example #10
0
def decode_call(data):
    """
    Decodes the given method call.

    Returns a tuple containing the method name and the parameters.
    """
    data = serial_datagram.decode(data)
    u = msgpack.Unpacker(encoding='ascii')
    u.feed(data)

    command = next(u)

    return command[0], command[1]
Example #11
0
 def test_end_is_unsescaped(self):
     """
     Checks that ESC + ESC_END is transformed to END.
     """
     datagram = serial_datagram.decode(b'\xdb\xdcIf-=\xc0')
     self.assertEqual(b'\xC0', datagram)