def test_MSB_LSB(self): data = NumberMask(random.randint(1, 0xFF), 8) crcSize = 8 crcMax = 2**crcSize - 1 inputPoly = NumberMask(random.randint(1, crcMax), crcSize) regInit = random.randint(0, crcMax) xorOut = random.randint(0, crcMax) crcProc = HwCRC() crcProc.setRegisterInitValue(regInit) crcProc.setXorOutValue(xorOut) crc = crcProc.calculateMSBClassic(data, inputPoly) revData = data.reversed() revPoly = inputPoly.reversed() revRegInit = reverse_number(regInit, crcSize) revXorOut = reverse_number(xorOut, crcSize) revCrcProc = HwCRC() revCrcProc.setReversed() revCrcProc.setRegisterInitValue(revRegInit) revCrcProc.setXorOutValue(revXorOut) crc2 = revCrcProc.calculateLSBClassic(revData, revPoly) revCrc = reverse_number(crc2, crcSize) # print "values: data:{} poly:{:X} init:{:X} xorOut:{:08b} crc:{:08b} revcrc:{:08b}".format( data, inputPoly, regInit, xorOut, crc, revCrc ) self.assertEqual(crc, revCrc)
def test_xor_hwcrc16_random(self): dataSize = 24 data1 = random.randint(1, 0xFFFFFF) data2 = random.randint(1, 0xFFFFFF) crcSize = 16 inputPoly = 0x10000 | random.randint(1, 0xFFFF) regInit = random.randint(1, 0xFFFF) xorOut = random.randint(1, 0xFFFF) crcFun = HwCRC() crcFun.setReversed(False) crcFun.setXorOutValue(xorOut) crcFun.setRegisterInitValue(regInit) crc1 = crcFun.calculate2(data1, dataSize, inputPoly, crcSize) crc2 = crcFun.calculate2(data2, dataSize, inputPoly, crcSize) xdata = data1 ^ data2 crcFunXor = HwCRC() crcFunXor.setReversed(False) xcrc = crcFunXor.calculate2(xdata, dataSize, inputPoly, crcSize) xorcrc = crc1 ^ crc2 # print "xor: d1:{:X} {:X} d2:{:X} {:X} r:{:X} xo:{:X} xor:{:X} {:X} {:X}".format( data1, crc1, data2, crc2, regInit, xorOut, xdata, xcrc, xorcrc ) self.assertEquals(xcrc, xorcrc)
def test_calculate3_8rev_init_xor(self): data = 0xF0 dataSize = 8 inputPoly = 0x107 crcSize = 8 regInit = 0x0F xorOut = 0xF0 crcProc = HwCRC() crcProc.setRegisterInitValue(regInit) crcProc.setXorOutValue(xorOut) crcProc.setReversed() crc = crcProc.calculate2(data, dataSize, inputPoly, crcSize) self.assertEqual(crc, 240)
def test_CRC_random(self): data = NumberMask(random.randint(1, 0xFFFFFFFFFFFFFFFF), 64) crcSize = 8 crcMax = 2**8 - 1 inputPoly = NumberMask(0x100 | random.randint(1, crcMax), crcSize) regInit = random.randint(0, crcMax) xorOut = random.randint(0, crcMax) reverse = bool(random.randint(0, 1)) crc_func = HwCRC() crc_func.setReversed(reverse) crc_func.setRegisterInitValue(regInit) crc_func.setXorOutValue(xorOut) crcLib = crc_func.calculate3(data, inputPoly) crcProc = DivisionCRC() crcProc.setReversed(reverse) crcProc.setRegisterInitValue(regInit) crcProc.setXorOutValue(xorOut) crc = crcProc.calculate3(data, inputPoly) # print "values: {} poly:{:X} init:{:X} xorOut:{:08b} rev:{} crc:{:08b} crcmod:{:08b} crcxor:{:08b}".format( data, inputPoly, regInit, xorOut, reverse, crc, crcLib, crc^crcLib ) # self.assertEqual( crc, crcLib, "Data: {} 0x{:X} 0x{:X} 0x{:X} {}".format(data, inputPoly, regInit, xorOut, reverse ) ) self.assertEqual(crc, crcLib)
def test_CRC(self): data = NumberMask(0xBF, 8) inputPoly = NumberMask(0x130, 8) regInit = 0x0 xorOut = 0x0 reverse = True crcProc = HwCRC() crcProc.setReversed(reverse) crcProc.setRegisterInitValue(regInit) crcProc.setXorOutValue(xorOut) crc = crcProc.calculate3(data, inputPoly) dcrcProc = DivisionCRC() dcrcProc.setReversed(reverse) dcrcProc.setRegisterInitValue(regInit) dcrcProc.setXorOutValue(xorOut) dcrc = dcrcProc.calculate3(data, inputPoly) # crc_func = crcmod.mkCrcFun(inputPoly, rev=reverse, initCrc=regInit, xorOut=xorOut) # crcLib = crc_func( data.toASCII() ) # print "crc: {:X} {:X} {:X} {:X}".format( dcrc, crc, crcLib, inputPoly ) self.assertEqual(dcrc, crc)
def test_crcmod_c16d64_random(self): data = NumberMask(random.randint(1, 0xFFFFFFFFFFFFFFFF), 64) crcSize = 16 crcMax = 2**crcSize - 1 inputPoly = NumberMask((0x1 << crcSize) | random.randint(1, crcMax), crcSize) # regInit = random.randint(0, crcMax) # xorOut = random.randint(0, crcMax) regInit = 0x0 xorOut = 0x0 reverse = bool(random.randint(0, 1)) crc_func = crcmod.mkCrcFun(inputPoly.masterData(), rev=reverse, initCrc=regInit, xorOut=xorOut) crcLib = crc_func(data.toASCII()) # print "crc: {:X} {:X}".format( crc, crc2 ) crcProc = HwCRC() crcProc.setReversed(reverse) crcProc.setXorOutValue(xorOut) if reverse: data.reorderBytes() inputPoly.reverse() crcInit = reverse_number(regInit ^ xorOut, crcSize) crcProc.setRegisterInitValue(crcInit) else: crcInit = regInit ^ xorOut crcProc.setRegisterInitValue(crcInit) crc = crcProc.calculate3(data, inputPoly) # print "values: {} poly:{:X} init:{:X} xorOut:{:08b} rev:{} crc:{:08b} crcmod:{:08b} crcxor:{:08b}".format( data, inputPoly, regInit, xorOut, reverse, crc, crcLib, crc^crcLib ) self.assertEqual(crc, crcLib)
def test_calculate_8_regInit(self): crcProc = HwCRC() crcProc.setRegisterInitValue(0xA5) ## 0b10100101 crc = crcProc.calculate1(0xA53937CF, 0x11D) self.assertEqual(crc, 0x1D)