def test_non_zeros(self):
     for length in xrange(1, 1000):
         test_string = non_zero_bytes(length)
         encoded = cobs.encode(test_string)
         expected_encoded = self.simple_encode_non_zeros_only(test_string)
         self.assertEqual(
             encoded, expected_encoded,
             "encoded != expected_encoded for length %d\nencoded: %s\nexpected_encoded: %s"
             % (length, repr(encoded), repr(expected_encoded)))
 def test_zeros(self):
     for length in xrange(520):
         test_string = '\x00' * length
         encoded = cobs.encode(test_string)
         expected_encoded = '\x01' * (length + 1)
         self.assertEqual(encoded, expected_encoded,
                          "encoding zeros failed for length %d" % length)
         decoded = cobs.decode(encoded)
         self.assertEqual(decoded, test_string,
                          "decoding zeros failed for length %d" % length)
Esempio n. 3
0
    def sendpacket(self, device_id, packet_id, data_in):
        """Convert device_id, packet_id and data to BPL packet and send via serial. Returns the encoded packet."""
        if self.verbose:
            print('\npre-check', str(device_id), str(packet_id), data_in)
        txData = convert_to_bytes(data_in)
        if self.verbose:
            print(txData)

        datalength = len(txData)

        packet_length = datalength + 4

        txPacket = txData
        txPacket.append(packet_id)
        txPacket.append(device_id)
        txPacket.append(packet_length)

        if py_version == 2:
            txPacket_str = str(txPacket)
            crcValue = self.crc8_func(txPacket_str)
            txPacket.append(crcValue)
            txPacket_str = str(txPacket)
            encoded = cobs.encode(txPacket_str)
        else:
            crcValue = self.crc8_func(txPacket)
            txPacket.append(crcValue)
            encoded = cobs.encode(txPacket)

        if self.verbose: print('encoded packet check')
        if self.verbose: print(["0x%s" % b for b in encoded])

        ## ADD TERMINATOR BYTE
        packet = encoded + b"\x00"
        if self.serialPort != None:
            if self.serialPort.is_open:
                if self.verbose: print('send packet check')
                if self.verbose: print(["0x%s" % b for b in packet])
                self.serialPort.write(packet)
        else:
            print("Serial Port is not Open")
        return packet
 def test_non_zeros_and_trailing_zero(self):
     for length in xrange(1, 1000):
         non_zeros_string = non_zero_bytes(length)
         test_string = non_zeros_string + '\x00'
         encoded = cobs.encode(test_string)
         if (len(non_zeros_string) % 254) == 0:
             expected_encoded = self.simple_encode_non_zeros_only(
                 non_zeros_string) + '\x01\x01'
         else:
             expected_encoded = self.simple_encode_non_zeros_only(
                 non_zeros_string) + '\x01'
         self.assertEqual(
             encoded, expected_encoded,
             "encoded != expected_encoded for length %d\nencoded: %s\nexpected_encoded: %s"
             % (length, repr(encoded), repr(expected_encoded)))
 def test_random(self):
     try:
         for _test_num in xrange(self.NUM_TESTS):
             length = random.randint(0, self.MAX_LENGTH)
             test_string = ''.join(
                 chr(random.randint(0, 255)) for x in xrange(length))
             encoded = cobs.encode(test_string)
             self.assertTrue(
                 '\x00' not in encoded,
                 "encoding contains zero byte(s):\noriginal: %s\nencoded: %s"
                 % (repr(test_string), repr(encoded)))
             self.assertTrue(
                 len(encoded) <= len(test_string) + 1 +
                 (len(test_string) // 254),
                 "encoding too big:\noriginal: %s\nencoded: %s" %
                 (repr(test_string), repr(encoded)))
             decoded = cobs.decode(encoded)
             self.assertEqual(
                 decoded, test_string,
                 "encoding and decoding random data failed:\noriginal: %s\ndecoded: %s"
                 % (repr(test_string), repr(decoded)))
     except KeyboardInterrupt:
         pass
 def test_predefined_encodings(self):
     for (test_string,
          expected_encoded_string) in self.predefined_encodings:
         encoded = cobs.encode(test_string)
         self.assertEqual(encoded, expected_encoded_string)