Пример #1
0
    def test_encode_decode(self):
        """ Test basic functionality of cobs_encode/decode. """
        def assert_encode_decode_equal(data):
            """
            Check to make sure encode(decode) is idempotent.
            """
            encoded = hibike_message.cobs_encode(data)
            decoded = hibike_message.cobs_decode(encoded)
            self.assertEqual(data, decoded,
                             "{} encoded incorrectly".format(data))

        run_with_random_data(assert_encode_decode_equal,
                             CobsTests.gen_cobs_data,
                             times=100)
Пример #2
0
    def test_parse_valid_packets(self):
        """
        Check that `hibike_message.parse_device_data` doesn't choke on valid
        packets.
        """
        def assert_parse_is_not_none(valid_packet):
            """ Assert that parsing a valid packet does not result in None. """
            parse_result = hibike_message.parse_bytes(valid_packet)
            self.assertIsNotNone(
                parse_result,
                "valid packet {} parsed as None".format(valid_packet))

        for func in self.MESSAGE_GENERATORS.values():
            if func is not None:
                run_with_random_data(assert_parse_is_not_none, func, times=100)
Пример #3
0
    def test_parse_idempotence(self):
        """ Check that parsing an encoded packet is idempotent. """
        def assert_parse_idempotent(valid_packet):
            """
            Check that parsing and encoding VALID_PACKET results in
            the same packet.
            """
            parse_result = hibike_message.parse_bytes(valid_packet)
            encoded = self.encode_packet(parse_result)
            self.assertEqual(
                valid_packet, encoded,
                "parse not idempotent for {}".format(valid_packet))

        for func in self.MESSAGE_GENERATORS.values():
            if func is not None:
                run_with_random_data(assert_parse_idempotent, func, times=100)
Пример #4
0
    def test_idempotence(self):
        """
        Check that encoding and decoding a random set of parameters
        is idempotent.
        """
        def assert_params_same(device_id, param_list):
            """
            Check that encoding and decoding PARAM_LIST produces
            the same result.
            """
            encoded = hibike_message.encode_params(device_id, param_list)
            decoded = hibike_message.decode_params(device_id, encoded)
            self.assertEqual(
                sorted(decoded), sorted(param_list),
                "encoded/decoded param list {} not identical".format(
                    param_list))

        run_with_random_data(assert_params_same,
                             self.gen_random_device_id_and_params,
                             times=1000)
Пример #5
0
    def test_parse_bad_checksum(self):
        """ Packets with bad checksums should not be parsed. """
        def screw_up_checksum(valid_packet):
            """ Make the checksum of a valid packet wrong. """
            if valid_packet[-2] < 255:
                valid_packet[-2] += 1
            else:
                valid_packet[-2] -= 1
            return valid_packet

        def assert_parse_is_none(invalid_packet):
            """ Assert that parsing an invalid packet results in None. """
            parse_result = hibike_message.parse_bytes(invalid_packet)
            self.assertIsNone(
                parse_result,
                "packet with bad checksum parsed: {}".format(invalid_packet))

        for func in self.MESSAGE_GENERATORS.values():
            if func is not None:
                # pylint: disable=cell-var-from-loop
                wrapped_func = lambda: (screw_up_checksum(*func()), )
                run_with_random_data(assert_parse_is_none,
                                     wrapped_func,
                                     times=100)