def test_configure_crc_parameters(self): crc_label = ChecksumLabel( "crc_label", 25, 120, 0, FieldType("crc", FieldType.Function.CHECKSUM)) crc_widget_controller = ChecksumWidgetController( crc_label, Message([0] * 150, 0, MessageType("test")), 0) crc = GenericCRC( polynomial=list(GenericCRC.DEFAULT_POLYNOMIALS.keys())[0]) self.assertEqual(crc_widget_controller.ui.lineEditCRCPolynomial.text(), crc.polynomial_as_hex_str) self.assertEqual(crc_widget_controller.ui.lineEditStartValue.text(), util.bit2hex(crc.start_value)) self.assertEqual(crc_widget_controller.ui.lineEditFinalXOR.text(), util.bit2hex(crc.final_xor)) crc_widget_controller.ui.comboBoxCRCFunction.setCurrentIndex(2) crc.polynomial = crc.choose_polynomial(2) self.assertEqual(crc_widget_controller.ui.lineEditCRCPolynomial.text(), crc.polynomial_as_hex_str) crc_widget_controller.ui.lineEditCRCPolynomial.setText("abcde") crc_widget_controller.ui.lineEditCRCPolynomial.editingFinished.emit() self.assertEqual(crc_label.checksum.polynomial, array.array("B", [1]) + util.hex2bit("abcde")) crc_widget_controller.ui.lineEditStartValue.setText("12345") crc_widget_controller.ui.lineEditStartValue.editingFinished.emit() self.assertEqual(util.bit2hex(crc_label.checksum.start_value), "12345") crc_widget_controller.ui.lineEditFinalXOR.setText("cccaa") crc_widget_controller.ui.lineEditFinalXOR.editingFinished.emit() self.assertEqual(util.bit2hex(crc_label.checksum.final_xor), "cccaa")
def test_configure_crc_parameters(self): crc_label = ChecksumLabel("crc_label", 25, 120, 0, FieldType("crc", FieldType.Function.CHECKSUM)) crc_widget_controller = ChecksumWidgetController(crc_label, Message([0] * 150, 0, MessageType("test")), 0) crc = GenericCRC(polynomial=list(GenericCRC.DEFAULT_POLYNOMIALS.keys())[0]) self.assertEqual(crc_widget_controller.ui.lineEditCRCPolynomial.text(), crc.polynomial_as_hex_str) self.assertEqual(crc_widget_controller.ui.lineEditStartValue.text(), util.bit2hex(crc.start_value)) self.assertEqual(crc_widget_controller.ui.lineEditFinalXOR.text(), util.bit2hex(crc.final_xor)) crc_widget_controller.ui.comboBoxCRCFunction.setCurrentIndex(2) crc.polynomial = crc.choose_polynomial(2) self.assertEqual(crc_widget_controller.ui.lineEditCRCPolynomial.text(), crc.polynomial_as_hex_str) crc_widget_controller.ui.lineEditCRCPolynomial.setText("abcde") crc_widget_controller.ui.lineEditCRCPolynomial.editingFinished.emit() self.assertEqual(crc_label.checksum.polynomial, array.array("B", [1]) + util.hex2bit("abcde")) crc_widget_controller.ui.lineEditStartValue.setText("12345") crc_widget_controller.ui.lineEditStartValue.editingFinished.emit() self.assertEqual(util.bit2hex(crc_label.checksum.start_value), "12345") crc_widget_controller.ui.lineEditFinalXOR.setText("cccaa") crc_widget_controller.ui.lineEditFinalXOR.editingFinished.emit() self.assertEqual(util.bit2hex(crc_label.checksum.final_xor), "cccaa")
def test_different_crcs(self): c = GenericCRC(polynomial="16_standard", start_value=False, final_xor=False, reverse_polynomial=False, reverse_all=False, lsb_first=False, little_endian=False) bitstring_set = [ "101001001010101010101011101111111000000000000111101010011101011", "101001001010101101111010110111101010010110111010", "00000000000000000000000000000000100000000000000000000000000000000001111111111111", "1111111111111111111111111111111110111111111111111111110111111111111111110000000000" "1"] for j in c.DEFAULT_POLYNOMIALS: c.polynomial = c.choose_polynomial(j) for i in bitstring_set: # Standard crc_new = c.crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_new, crc_old) # Special final xor c.final_xor = c.str2bit("0000111100001111") crc_new = c.crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_new, crc_old) c.final_xor = [False] * 16 # Special start value c.start_value = c.str2bit("1010101010101010") crc_new = c.crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_new, crc_old) c.start_value = [False] * 16 # reverse_polynomial c.reverse_polynomial = True crc_new = c.crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_new, crc_old) c.reverse_polynomial = False # lsb_first c.lsb_first = True crc_new = c.crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_new, crc_old) c.lsb_first = False # little_endian c.little_endian = True crc_new = c.crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_new, crc_old) c.little_endian = False # reverse all c.reverse_all = True crc_new = c.crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_new, crc_old) c.reverse_all = False
def test_not_aligned_data_len(self): c = GenericCRC(polynomial="16_standard", start_value=False, final_xor=False, reverse_polynomial=False, reverse_all=False, lsb_first=False, little_endian=False) polynomials = ["8_standard", "16_standard", "16_ccitt", "16_dnp"] crcs = {"8_standard": 0xd5, "16_standard": 0x8005, "16_ccitt": 0x1021, "16_dnp": 0x3d65} for j in polynomials: c.polynomial = c.choose_polynomial(j) inpt = "1" for i in range(0, 32): val = c.bit2int(c.crc(c.str2bit(inpt))) self.assertEqual(val, crcs[j]) inpt = "0" + inpt
def test_different_crcs_fast(self): c = GenericCRC(polynomial="16_standard", start_value=False, final_xor=False, reverse_polynomial=False, reverse_all=False, lsb_first=False, little_endian=False) bitstring_set = [ "10101010", "00000001", "000000010", "000000011", "0000000100000001", "101001001010101010101011101111111000000000000111101010011101011", "101001001010101101111010110111101010010110111010", "00000000000000000000000000000000100000000000000000000000000000000001111111111111", "1111111111111111111111111111111110111111111111111111110111111111111111110000000000" "1" ] for j in c.DEFAULT_POLYNOMIALS: c.polynomial = c.choose_polynomial(j) for i in bitstring_set: for cache in [8, 4, 7, 12, 16]: c.calculate_cache(cache) # Standard crc_new = c.cached_crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_old, crc_new) # Special final xor c.final_xor = c.str2bit("0000111100001111") crc_new = c.cached_crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_old, crc_new) c.final_xor = [False] * 16 # Special start value c.start_value = c.str2bit("1010101010101010") crc_new = c.cached_crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_old, crc_new) c.start_value = [False] * 16 # little_endian c.little_endian = True crc_new = c.cached_crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_old, crc_new) c.little_endian = False # reverse all c.reverse_all = True crc_new = c.cached_crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_old, crc_new) c.reverse_all = False # reverse_polynomial # We need to clear the cache before and after c.cache = [] # c.reverse_polynomial = True crc_new = c.cached_crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_old, crc_new) c.reverse_polynomial = False # c.cache = [] # TODO: Does only work for cachesize = 8 # lsb_first c.calculate_cache(8) c.lsb_first = True crc_new = c.cached_crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_old, crc_new) c.lsb_first = False