def test_crc(self): # http://depa.usst.edu.cn/chenjq/www2/software/crc/CRC_Javascript/CRCcalculation.htm # CRC-16: polynomial="16_standard", start_value = False, final_xor = False, reverse_polynomial=False, reverse_all=False # CRC-16-CCITT: polynomial="16_ccitt", start_value = False, final_xor = False, reverse_polynomial=False, reverse_all=False # http://www.lammertbies.nl/comm/info/crc-calculation.html <- Fehler # CRC-16: polynomial="16_standard", start_value = False, final_xor = False, reverse_polynomial=False, reverse_all=False c = crc_generic(polynomial="8_en") e = Encoder() bitstr = [ "010101010110100111011010111011101110111011100110001011101010001011101110110110101101", "010101010110101001101110111011101110111011100110001011101010001011101110110111100101", "010101010110100111010010111011101110111011100110001011101010001011101110110110100101" ] expected = ["0x78", "0xc9", "0xf2"] for value, expect in zip(bitstr, expected): nv = "" for i in range(0, len(value)): if value[i] == "1": nv += "0" else: nv += "1" self.assertEqual(e.bit2hex(c.crc(e.str2bit(value[4:-8]))), expect)
def saveas(self): # Ask for a name txt = ["Please enter a name:", self.e.chain[0]] ok, name = CustomDialog.dialog(self, txt, "input") if ok and name != "": self.e.chain[0] = name self.decoderchainUpdate() # If name is already there, overwrite existing for i in range(0, len(self.decodings)): if name == self.decodings[i].name: self.ui.combobox_decodings.setCurrentIndex(i) self.decodings[i] = Encoder(self.chainstr) self.set_e() self.ui.saveas.setVisible(False) self.save_to_file() return self.decodings.append(Encoder(self.chainstr)) self.ui.combobox_decodings.addItem(self.chainstr[0]) self.ui.combobox_decodings.setCurrentIndex( self.ui.combobox_decodings.count() - 1) self.set_e() self.save_to_file()
def setUp(self): filename = os.path.join(tempfile.gettempdir(), "test.fuzz") mod1 = Modulator("mod 1") mod2 = Modulator("mod 2") mod2.param_for_one = 42 decoders = [ Encoder(["NRZ"]), Encoder(["NRZ-I", constants.DECODING_INVERT]) ] pac = ProtocolAnalyzerContainer([mod1, mod2]) pac.messages.append( Message([True, False, False, True, "A"], 100, decoder=decoders[0], message_type=pac.default_message_type)) pac.messages.append( Message([False, False, False, False, "A"], 200, decoder=decoders[1], message_type=pac.default_message_type)) pac.used_symbols.add(Symbol("A", 1, 1, 100)) pac.create_fuzzing_label(1, 10, 0) pac.to_xml_file(filename)
def saveas(self): # Ask for a name name, ok = QInputDialog.getText(self, self.tr("Save decoding"), self.tr("Please enter a name:"), QLineEdit.Normal, self.e.chain[0]) if ok and name != "": self.e.chain[0] = name self.decoderchainUpdate() # If name is already there, overwrite existing for i in range(0, len(self.decodings)): if name == self.decodings[i].name: self.ui.combobox_decodings.setCurrentIndex(i) self.decodings[i] = Encoder(self.chainstr) self.set_e() self.ui.saveas.setVisible(False) self.save_to_file() return self.decodings.append(Encoder(self.chainstr)) self.ui.combobox_decodings.addItem(self.chainstr[0]) self.ui.combobox_decodings.setCurrentIndex( self.ui.combobox_decodings.count() - 1) self.set_e() self.save_to_file()
def test_enocean_switch_telegram(self): e = Encoder() received = "010101010110100111101010111011101110111011100110001011101010001011101110110111011101000" # First step is invert the received bits!!! #received = "10101010 1001 011000010101 000100010001 000100011001 110100010101 110100010001 0010001000 1011" preamble = "10101010" sof = "1001" eof = "1011" self.assertIn(preamble, received) self.assertIn(sof, received) self.assertIn(eof, received) # Preamble/SOF/EOF remain unchanged expected_result = preamble + sof + "01100001 00000000 00000010 11000001 11000000 00100100" + eof expected_result2 = preamble + sof + "01010000 00000000 00000010 11000001 11000000 00100010" + eof decoded, err, _ = e.code_enocean(True, e.str2bit(received.replace(" ",""))) self.assertEqual(err, 0) reencoded, err, _ = e.code_enocean(False, decoded) self.assertEqual(err, 0) self.assertEqual(decoded, e.str2bit(expected_result.replace(" ", ""))) self.assertEqual(reencoded, e.str2bit(received))
def read_decoders_from_xml_tag(root: ET.Element): try: decoders = [] for decoding_tag in root.find("decodings").findall("decoding"): conf = [d.strip().replace("'", "") for d in decoding_tag.text.split(",") if d.strip().replace("'", "")] decoders.append(Encoder(conf)) return decoders except AttributeError: logger.error("no decodings found in xml") return []
def __init__(self, plain_bits, pause: int, message_type: MessageType, rssi=0, modulator_indx=0, decoder=None, fuzz_created=False, bit_sample_pos=None, bit_len=100, participant=None): """ :param pause: pause AFTER the message in samples :type plain_bits: list[bool|Symbol] :type decoder: Encoder :type bit_alignment_positions: list of int :param bit_alignment_positions: Für Ausrichtung der Hex Darstellung (Leere Liste für Standardverhalten) :param bit_len: Für Übernahme der Bitlänge in Modulator Dialog :param fuzz_created: message was created through fuzzing :return: """ self.__plain_bits = plain_bits self.pause = pause self.modulator_indx = modulator_indx self.rssi = rssi self.participant = participant """:type: Participant """ self.message_type = message_type """:type: MessageType """ self.absolute_time = 0 # set in Compare Frame self.relative_time = 0 # set in Compare Frame self.__decoder = decoder if decoder else Encoder( ["Non Return To Zero (NRZ)"]) """:type: encoding """ self.align_labels = True self.fuzz_created = fuzz_created self.__decoded_bits = None self.__encoded_bits = None self.__bit_alignments = [] self.decoding_errors = 0 self.decoding_state = Encoder.ErrorState.SUCCESS self.bit_len = bit_len # Für Übernahme in Modulator if bit_sample_pos is None: self.bit_sample_pos = [] else: self.bit_sample_pos = bit_sample_pos """
def test_context_menu(self): self.dialog.ui.combobox_decodings.setCurrentIndex(4) decoding = Encoder(chain=[constants.DECODING_INVERT]) self.dialog.decodings[4] = decoding self.dialog.set_e() self.assertEqual(1, self.dialog.ui.decoderchain.count()) self.dialog.ui.decoderchain.context_menu_pos = QPoint(0, 0) menu = self.dialog.ui.decoderchain.create_context_menu() menu_actions = [action.text() for action in menu.actions() if action.text()] self.assertEqual(3, len(menu_actions))
def test_morse(self): e = Encoder() e.morse_low = 3 e.morse_high = 5 e.morse_wait = 1 msg1 = "1111111000111100011111100100001111111111111111111111011" msg2 = "0111110111011111011101111101110" encoded = e.str2bit(msg1) compare = e.str2bit(msg2) decoded, err, _ = e.code_morse(decoding=True, inpt=encoded) reencoded, _, _ = e.code_morse(decoding=False, inpt=decoded) self.assertEqual(err, 1) self.assertEqual(reencoded, compare)
def __init__(self, signal: Signal): self.messages = [] # type: list[Message] self.signal = signal self.filename = self.signal.filename if self.signal is not None else "" self.__name = "Blank" # Fallback if Signal has no Name self.show = Qt.Checked # Show in Compare Frame? self.qt_signals = ProtocolAnalyzerSignals() self.decoder = Encoder(["Non Return To Zero (NRZ)" ]) # For Default Encoding of Protocol self.message_types = [MessageType("default")]
def test_load_profile(self): filename = os.path.join(tempfile.gettempdir(), "test.fuzz") mod1 = Modulator("mod 1") mod2 = Modulator("mod 2") mod2.param_for_one = 42 decoders = [ Encoder(["NRZ"]), Encoder(["NRZ-I", constants.DECODING_INVERT]) ] pac = ProtocolAnalyzerContainer([mod1, mod2]) pac.messages.append( Message([True, False, False, True], 100, decoder=decoders[0], message_type=pac.default_message_type)) pac.messages.append( Message([False, False, False, False], 200, decoder=decoders[1], message_type=pac.default_message_type)) pac.create_fuzzing_label(1, 10, 0) pac.to_xml_file(filename) self.wait_before_new_file() self.form.add_files([os.path.join(tempfile.gettempdir(), "test.fuzz")]) self.assertEqual(self.form.ui.tabWidget.currentWidget(), self.form.ui.tab_generator) pac = self.form.generator_tab_controller.table_model.protocol self.assertEqual(len(pac.modulators), 2) self.assertEqual(len(pac.messages), 2) self.assertEqual(pac.messages[1][0], False) self.assertEqual(len(pac.protocol_labels), 1)
def test_whitening(self): e = Encoder() # Test 1 e.data_whitening_sync = e.hex2bit("67686768") original_inpt = e.hex2bit("aaaaaaaa67686768f9ca03909567ba76a8") + array.array("B", [False]) # Korrektes Signal, bitgenau inpt = copy.copy(original_inpt) #print (e.bit2hex(inpt)) output, err, _ = e.apply_data_whitening(True, inpt) #print (e.bit2hex(output), err) newinpt, err, _ = e.apply_data_whitening(False, output) #print (e.bit2hex(newinpt), newinpt, err) self.assertEqual(original_inpt, newinpt)
def test_crc(self): c = crc_generic(polynomial="8_en") e = Encoder() bitstr = [ "010101010110100111011010111011101110111011100110001011101010001011101110110110101101", "010101010110101001101110111011101110111011100110001011101010001011101110110111100101", "010101010110100111010010111011101110111011100110001011101010001011101110110110100101" ] print() for v in bitstr: nv = "" for i in range(0, len(v)): if v[i] == "1": nv += "0" else: nv += "1" xorsum = 0 for i in range(12, len(v) - 8, 4): bin = e.str2bit(v[0 + i:4 + i]) xorsum ^= (bin[0] * 8 + bin[1] * 4 + bin[2] * 2 + bin[3]) print(e.bit2hex(e.str2bit(v[1:])), hex(xorsum))
def setUp(self): self.protocol = ProtocolAnalyzer(None) with open(get_path_for_data_file("decoded_bits.txt")) as f: for line in f: self.protocol.messages.append( Message.from_plain_bits_str(line.replace("\n", ""), {})) self.protocol.messages[ -1].message_type = self.protocol.default_message_type # Assign participants alice = Participant("Alice", "A") bob = Participant("Bob", "B") alice_indices = { 1, 2, 5, 6, 9, 10, 13, 14, 17, 18, 20, 22, 23, 26, 27, 30, 31, 34, 35, 38, 39, 41 } for i, message in enumerate(self.protocol.messages): if i in alice_indices: message.participant = alice else: message.participant = bob self.assertEqual(self.protocol.num_messages, 42) self.assertEqual(self.protocol.plain_hex_str[0][16:18], "2d") self.decodings = [] self.decodings.append(Encoder(['Non Return To Zero (NRZ)'])) self.decodings.append( Encoder(['Non Return To Zero Inverted (NRZ-I)', 'Invert'])) self.decodings.append(Encoder(['Manchester I', 'Edge Trigger'])) self.decodings.append( Encoder(['Manchester II', 'Edge Trigger', 'Invert'])) self.decodings.append( Encoder([ 'Differential Manchester', 'Edge Trigger', 'Differential Encoding', ])) self.decodings.append( Encoder([ 'DeWhitening Special', 'Remove Data Whitening (CC1101)', '0x9a7d9a7d;0x21;0x8' ])) self.decodings.append( Encoder([ 'DeWhitening', 'Remove Data Whitening (CC1101)', '0x67686768;0x21;0x8' ]))
def test_substitution(self): e = Encoder() e.src = [ array.array("B", [True, True, True, False]), array.array("B", [True, False, False, False]) ] e.dst = [array.array("B", [True]), array.array("B", [False])] # encoded-string with 3 missing trailing zeroes encoded = e.str2bit( "1000111010001110111011101110111011101110100011101110111011101110111011101000100010001000100010001" ) compare = e.str2bit( "1000111010001110111011101110111011101110100011101110111011101110111011101000100010001000100010001000" ) decoded, err, _ = e.code_substitution(decoding=True, inpt=encoded) reencoded, _, _ = e.code_substitution(decoding=False, inpt=decoded) self.assertEqual(err, 3) self.assertEqual(reencoded, compare)
def test_enocean_crc_polynomial(self): e = Encoder() msg1 = "aa9a6d201006401009802019e411e8035b" msg2 = "aa9a6d2010000ffdaaf01019e411e8071b" # Remove Preamble + SOF + EOF for CRC calculation msg1 = e.hex2bit("a6d201006401009802019e411e8035") crc1 = e.hex2bit("35") msg2 = e.hex2bit("a6d2010000ffdaaf01019e411e8071") crc2 = e.hex2bit("71") calc_crc1 = e.enocean_hash(msg1) calc_crc2 = e.enocean_hash(msg2) self.assertTrue(calc_crc1 == crc1) self.assertTrue(calc_crc2 == crc2)
def test_build_decoding(self): self.dialog.ui.combobox_decodings.setCurrentIndex(4) chain = [constants.DECODING_INVERT, constants.DECODING_ENOCEAN, constants.DECODING_DIFFERENTIAL, constants.DECODING_REDUNDANCY, constants.DECODING_CARRIER, constants.DECODING_BITORDER, constants.DECODING_EDGE, constants.DECODING_DATAWHITENING, constants.DECODING_SUBSTITUTION, constants.DECODING_EXTERNAL, constants.DECODING_CUT] decoding = Encoder(chain=chain) self.dialog.decodings[4] = decoding self.dialog.set_e() self.assertEqual(len(chain), self.dialog.ui.decoderchain.count()) for i in range(0, self.dialog.ui.decoderchain.count()): self.dialog.ui.decoderchain.setCurrentRow(i) self.dialog.set_information(2) self.assertIn(chain[i], self.dialog.ui.info.text())
def test_enocean_crc8_message(self): e = Encoder() received = e.hex2bit("aacbac4cddd5ddd3bddd5ddcc5ddcddd4c2d5d5c2cdddab200000") preamble, sof, eof = "aa", "9", "b" decoded, err, state = e.code_enocean(decoding=True, inpt=received) self.assertEqual(err, 0) self.assertEqual(state, e.ErrorState.SUCCESS) self.assertIn(preamble, e.bit2hex(decoded)) self.assertIn(sof, e.bit2hex(decoded)) self.assertIn(eof, e.bit2hex(decoded)) reencoded, errors, state = e.code_enocean(decoding=False, inpt=decoded) self.assertEqual(errors, 0) self.assertEqual(state, e.ErrorState.SUCCESS) redecoded, errors, state = e.code_enocean(decoding=True, inpt=reencoded) self.assertEqual(errors, 0) self.assertEqual(state, e.ErrorState.SUCCESS) self.assertEqual(decoded, redecoded)
def test_carrier(self): e = Encoder() # Test 1 e.carrier = "----1....1**" # or "....1....101", ... original_inpt = e.str2bit("000010000100111111111100") inpt = original_inpt.copy() #print("\nOriginal:", inpt) output, err, _ = e.code_carrier(True, inpt) #print("Decoded: ", output, err) newinpt, err, _ = e.code_carrier(False, output) #print("Encoded: ", newinpt, err) self.assertEqual(original_inpt, newinpt)
def test_build_decoding(self): self.dialog.ui.combobox_decodings.setCurrentIndex(4) chain = [(constants.DECODING_INVERT, ), (constants.DECODING_ENOCEAN, ), (constants.DECODING_DIFFERENTIAL, ), (constants.DECODING_CARRIER, ), (constants.DECODING_BITORDER, ), (constants.DECODING_EDGE, ), (constants.DECODING_DATAWHITENING, ), (constants.DECODING_REDUNDANCY, "2"), (constants.DECODING_MORSE, "1;3;1"), (constants.DECODING_SUBSTITUTION, "0:1;1:0;"), (constants.DECODING_EXTERNAL, "./;./"), (constants.DECODING_CUT, "0;1010")] decoding = Encoder( chain=[c for chain_item in chain for c in chain_item]) self.dialog.decodings[4] = decoding self.dialog.set_e() self.assertEqual(len(chain), self.dialog.ui.decoderchain.count()) for i in range(0, self.dialog.ui.decoderchain.count()): self.dialog.ui.decoderchain.setCurrentRow(i) self.dialog.set_information(2) self.assertIn(chain[i][0], self.dialog.ui.info.text())
def setUp(self): self.old_sym_len = constants.SETTINGS.value('rel_symbol_length', type=int) constants.SETTINGS.setValue('rel_symbol_length', 0) # Disable Symbols for this Test self.form = MainController() self.form.add_signalfile( get_path_for_data_file("steckdose_anlernen.complex")) self.form.signal_tab_controller.signal_frames[ 0].ui.spinBoxNoiseTreshold.setValue(0.06) self.form.signal_tab_controller.signal_frames[ 0].ui.spinBoxNoiseTreshold.editingFinished.emit() self.form.signal_tab_controller.signal_frames[ 0].ui.spinBoxCenterOffset.setValue(-0.0127) self.form.signal_tab_controller.signal_frames[ 0].ui.spinBoxCenterOffset.editingFinished.emit() self.form.signal_tab_controller.signal_frames[ 0].ui.spinBoxInfoLen.setValue(100) self.form.signal_tab_controller.signal_frames[ 0].ui.spinBoxInfoLen.editingFinished.emit() self.sframe = self.form.signal_tab_controller.signal_frames[0] self.cframe = self.form.compare_frame_controller self.gframe = self.form.generator_tab_controller # Dewhitening mit SyncByte 0x9a7d9a7d, Data Whitening Poly 0x21, Compute and apply CRC16 via X0r, # Rest auf False anlegen und setzen self.form.ui.tabWidget.setCurrentIndex(1) self.cframe.ui.cbProtoView.setCurrentIndex(1) # Hex decoding = Encoder([ "Data Whitening", constants.DECODING_DATAWHITENING, "0x9a7d9a7d;0x21;0x8" ]) self.cframe.decodings.append(decoding) self.cframe.ui.cbDecoding.addItem(decoding.name) self.cframe.set_decoding(decoding) # CRC Check self.assertEqual(self.cframe.protocol_model.display_data[0][-4:], "0000") # Serial Part 1: Bits 207-226 (Dezimal: 91412) (20 Bits) self.cframe.add_protocol_label(start=206, end=225, messagenr=0, proto_view=0, edit_label_name=False) # Zeros: Bits 227-244 (18 Bits) self.cframe.add_protocol_label(start=226, end=243, messagenr=0, proto_view=0, edit_label_name=False) # Serial Part 2: Bit 245 - 264 (Dezimal: 1034678) (20 Bits) self.cframe.add_protocol_label(start=244, end=263, messagenr=0, proto_view=0, edit_label_name=False) self.form.ui.tabWidget.setCurrentIndex(2) item = self.gframe.tree_model.rootItem.children[0].children[0] index = self.gframe.tree_model.createIndex(0, 0, item) rect = self.gframe.ui.treeProtocols.visualRect(index) self.assertEqual(len(self.gframe.ui.treeProtocols.selectedIndexes()), 0) QTest.mousePress(self.gframe.ui.treeProtocols.viewport(), Qt.LeftButton, pos=rect.center()) self.assertEqual(self.gframe.ui.treeProtocols.selectedIndexes()[0], index) mimedata = self.gframe.tree_model.mimeData( self.gframe.ui.treeProtocols.selectedIndexes()) self.gframe.table_model.dropMimeData( mimedata, 1, -1, -1, self.gframe.table_model.createIndex(0, 0)) self.assertEqual(self.gframe.table_model.row_count, 1) self.assertEqual(len(self.gframe.table_model.protocol.protocol_labels), 3)
def setUp(self): super().setUp() self.add_signal_to_form("steckdose_anlernen.complex") self.form.signal_tab_controller.signal_frames[ 0].ui.spinBoxNoiseTreshold.setValue(0.06) self.form.signal_tab_controller.signal_frames[ 0].ui.spinBoxNoiseTreshold.editingFinished.emit() self.form.signal_tab_controller.signal_frames[ 0].ui.spinBoxCenterOffset.setValue(-0.0127) self.form.signal_tab_controller.signal_frames[ 0].ui.spinBoxCenterOffset.editingFinished.emit() self.form.signal_tab_controller.signal_frames[ 0].ui.spinBoxInfoLen.setValue(100) self.form.signal_tab_controller.signal_frames[ 0].ui.spinBoxInfoLen.editingFinished.emit() self.gframe = self.form.generator_tab_controller self.gframe.ui.cbViewType.setCurrentIndex(1) # hex view # Dewhitening mit SyncByte 0x9a7d9a7d, Data Whitening Poly 0x21, Compute and apply CRC16 via X0r, # Rest auf False anlegen und setzen self.form.ui.tabWidget.setCurrentIndex(1) self.form.compare_frame_controller.ui.cbProtoView.setCurrentIndex( 1) # Hex decoding = Encoder([ "Data Whitening", constants.DECODING_DATAWHITENING, "0x9a7d9a7d;0x21;0x8" ]) self.form.compare_frame_controller.decodings.append(decoding) self.form.compare_frame_controller.ui.cbDecoding.addItem(decoding.name) self.form.compare_frame_controller.set_decoding(decoding) # CRC Check self.assertEqual( self.form.compare_frame_controller.protocol_model.display_data[0] [-4:], array.array("B", [0, 0, 0, 0])) # Serial Part 1: Bits 207-226 (Dezimal: 91412) (20 Bits) self.form.compare_frame_controller.add_protocol_label( start=206, end=225, messagenr=0, proto_view=0, edit_label_name=False) # Zeros: Bits 227-244 (18 Bits) self.form.compare_frame_controller.add_protocol_label( start=226, end=243, messagenr=0, proto_view=0, edit_label_name=False) # Serial Part 2: Bit 245 - 264 (Dezimal: 1034678) (20 Bits) self.form.compare_frame_controller.add_protocol_label( start=244, end=263, messagenr=0, proto_view=0, edit_label_name=False) self.form.ui.tabWidget.setCurrentIndex(2) item = self.gframe.tree_model.rootItem.children[0].children[0] index = self.gframe.tree_model.createIndex(0, 0, item) rect = self.gframe.ui.treeProtocols.visualRect(index) self.assertEqual(len(self.gframe.ui.treeProtocols.selectedIndexes()), 0) QTest.mousePress(self.gframe.ui.treeProtocols.viewport(), Qt.LeftButton, pos=rect.center()) self.assertEqual(self.gframe.ui.treeProtocols.selectedIndexes()[0], index) mimedata = self.gframe.tree_model.mimeData( self.gframe.ui.treeProtocols.selectedIndexes()) self.gframe.table_model.dropMimeData( mimedata, 1, -1, -1, self.gframe.table_model.createIndex(0, 0)) self.assertEqual(self.gframe.table_model.row_count, 1) self.assertEqual(len(self.gframe.table_model.protocol.protocol_labels), 3) self.dialog = FuzzingDialogController( protocol=self.gframe.table_model.protocol, label_index=0, msg_index=0, proto_view=0, parent=self.gframe) self.dialog.finished.connect(self.gframe.refresh_label_list) self.dialog.finished.connect(self.gframe.refresh_table) self.dialog.finished.connect(self.gframe.set_fuzzing_ui_status) if self.SHOW: self.dialog.show()
def test_cut_decoding(self): e = Encoder() received = e.str2bit("00001010010101111111000") expected_result = e.str2bit("1010010101111111000") e.cutmode = 0 e.cutmark = [True, False, True, False] decoded, err, _ = e.code_cut(True, received) self.assertEqual(decoded, expected_result) received = e.str2bit("00001010010101111111000") expected_result = e.str2bit("00001010") e.cutmode = 1 e.cutmark = [True, False, True, False] decoded, err, _ = e.code_cut(True, received) self.assertEqual(decoded, expected_result) received = e.str2bit("00001010010101111111000") expected_result = e.str2bit("001010010101111111000") e.cutmode = 2 e.cutmark = 2 decoded, err, _ = e.code_cut(True, received) self.assertEqual(decoded, expected_result) received = e.str2bit("00001010010101111111000") expected_result = e.str2bit("000") e.cutmode = 3 e.cutmark = 2 decoded, err, _ = e.code_cut(True, received) self.assertEqual(decoded, expected_result) received = e.str2bit("00001010010101111111000") expected_result = e.str2bit("00001010010101111111000") e.cutmode = 0 e.cutmark = [True, False, True, False, True, False] decoded, err, _ = e.code_cut(True, received) self.assertEqual(decoded, expected_result)