Example #1
0
    def test_sync_word_finding_common_prefix(self):
        """
        Messages are very similar (odd and even ones are the same)
        However, they do not have two different sync words!
        The algorithm needs to check for a common prefix of the two found sync words

        :return:
        """
        sync = "0x1337"
        num_messages = 10

        alice = Participant("Alice", address_hex="dead01")
        bob = Participant("Bob", address_hex="beef24")

        mb = MessageTypeBuilder("protocol_with_one_message_type")
        mb.add_label(FieldType.Function.PREAMBLE, 72)
        mb.add_label(FieldType.Function.SYNC, 16)
        mb.add_label(FieldType.Function.LENGTH, 8)
        mb.add_label(FieldType.Function.SRC_ADDRESS, 24)
        mb.add_label(FieldType.Function.DST_ADDRESS, 24)
        mb.add_label(FieldType.Function.SEQUENCE_NUMBER, 16)

        pg = ProtocolGenerator([mb.message_type],
                               syncs_by_mt={mb.message_type: "0x1337"},
                               preambles_by_mt={mb.message_type: "10" * 36},
                               participants=[alice, bob])

        random.seed(0)
        for i in range(num_messages):
            if i % 2 == 0:
                source, destination = alice, bob
                data_length = 8
            else:
                source, destination = bob, alice
                data_length = 16
            pg.generate_message(data=pg.decimal_to_bits(
                random.randint(0, 2**(data_length - 1)), data_length),
                                source=source,
                                destination=destination)

        preprocessor = Preprocessor([
            np.array(msg.plain_bits, dtype=np.uint8)
            for msg in pg.protocol.messages
        ])
        possible_syncs = preprocessor.find_possible_syncs()
        #self.save_protocol("sync_by_common_prefix", pg)
        self.assertEqual(len(possible_syncs), 1)

        # +0000 is okay, because this will get fixed by correction in FormatFinder
        self.assertIn(possible_syncs[0], [
            ProtocolGenerator.to_bits(sync),
            ProtocolGenerator.to_bits(sync) + "0000"
        ])
Example #2
0
    def test_format_finding_rwe(self):
        ff, messages = self.get_format_finder_from_protocol_file("rwe.proto.xml", return_messages=True)
        ff.run()

        sync1, sync2 = "0x9a7d9a7d", "0x67686768"

        preprocessor = Preprocessor([np.array(msg.plain_bits, dtype=np.uint8) for msg in messages])
        possible_syncs = preprocessor.find_possible_syncs()
        self.assertIn(ProtocolGenerator.to_bits(sync1), possible_syncs)
        self.assertIn(ProtocolGenerator.to_bits(sync2), possible_syncs)

        ack_messages = (3, 5, 7, 9, 11, 13, 15, 17, 20)
        ack_message_type = next(mt for mt, messages in ff.existing_message_types.items() if ack_messages[0] in messages)
        self.assertTrue(all(ack_msg in ff.existing_message_types[ack_message_type] for ack_msg in ack_messages))

        for mt in ff.message_types:
            preamble = mt.get_first_label_with_type(FieldType.Function.PREAMBLE)
            self.assertEqual(preamble.start, 0)
            self.assertEqual(preamble.length, 32)

            sync = mt.get_first_label_with_type(FieldType.Function.SYNC)
            self.assertEqual(sync.start, 32)
            self.assertEqual(sync.length, 32)

            length = mt.get_first_label_with_type(FieldType.Function.LENGTH)
            self.assertEqual(length.start, 64)
            self.assertEqual(length.length, 8)

            dst = mt.get_first_label_with_type(FieldType.Function.DST_ADDRESS)
            self.assertEqual(dst.length, 24)

            if mt == ack_message_type or 1 in ff.existing_message_types[mt]:
                self.assertEqual(dst.start, 72)
            else:
                self.assertEqual(dst.start, 88)

            if mt != ack_message_type and 1 not in ff.existing_message_types[mt]:
                src = mt.get_first_label_with_type(FieldType.Function.SRC_ADDRESS)
                self.assertEqual(src.start, 112)
                self.assertEqual(src.length, 24)
            elif 1 in ff.existing_message_types[mt]:
                # long ack
                src = mt.get_first_label_with_type(FieldType.Function.SRC_ADDRESS)
                self.assertEqual(src.start, 96)
                self.assertEqual(src.length, 24)

            crc = mt.get_first_label_with_type(FieldType.Function.CHECKSUM)
            self.assertIsNotNone(crc)
Example #3
0
 def test_sync_word_finding_with_two_sync_words(self):
     preamble = "0xaaaa"
     sync1, sync2 = "0x1234", "0xcafe"
     pg = self.build_protocol_generator(preamble_syncs=[(preamble, sync1),
                                                        (preamble, sync2)],
                                        num_messages=(15, 10),
                                        data=(lambda i: 12 * i,
                                              lambda i: 16 * i))
     preprocessor = Preprocessor([
         np.array(msg.plain_bits, dtype=np.uint8)
         for msg in pg.protocol.messages
     ])
     possible_syncs = preprocessor.find_possible_syncs()
     #self.save_protocol("two_syncs", pg)
     self.assertGreaterEqual(len(possible_syncs), 2)
     self.assertIn(ProtocolGenerator.to_bits(sync1), possible_syncs)
     self.assertIn(ProtocolGenerator.to_bits(sync2), possible_syncs)