Example #1
0
    def test_choose_candidate(self):

        candidates1 = {
            '78e289': 8,
            '207078e2891b6033000000': 1,
            '57': 1,
            '20701b603378e289000c62': 1,
            '1b6033fd57': 1,
            '1b603300': 3,
            '7078e2891b6033000000': 2,
            '78e289757e': 1,
            '1b6033': 14,
            '701b603378e289': 2
        }
        candidates2 = {
            '1b603300': 4,
            '701b603378e289': 2,
            '20701b603378e289000c62': 1,
            '000': 3,
            '0000': 19,
            '1b6033': 11,
            '78e2890000': 1,
            '00': 4,
            '7078e2891b6033000000': 2,
            '207078e2891b6033000000': 1,
            '78e289000': 1,
            '78e289': 7,
            '0': 7,
            '1b60330000': 3
        }

        self.assertEqual(next(Address.choose_candidate_pair(candidates1)),
                         ("1b6033", "78e289"))
        self.assertEqual(next(Address.choose_candidate_pair(candidates2)),
                         ("1b6033", "78e289"))
Example #2
0
    def test_address_candidate_finding(self):
        fh = CommonRange.from_hex

        candidates_participant_1 = [
            fh('1b6033'),
            fh('1b6033fd57'),
            fh('701b603378e289'),
            fh('20701b603378e289000c62')
        ]
        candidates_participant_2 = [
            fh('1b603300'),
            fh('78e289757e'),
            fh('7078e2891b6033000000'),
            fh('207078e2891b6033000000')
        ]

        expected_address1 = '1b6033'
        expected_address2 = '78e289'

        print(Address.find_candidates(candidates_participant_1))
        print(Address.find_candidates(candidates_participant_2))
        combined = candidates_participant_1 + candidates_participant_2
        combined.sort(key=len)
        score = Address.find_candidates(combined)
        print(score)
        print("=================")
        print(sorted(score, key=lambda k: score[k], reverse=True))
        print()

        highscored = sorted(score, key=lambda k: score[k], reverse=True)[:2]
        self.assertIn(expected_address1, highscored)
        self.assertIn(expected_address2, highscored)
Example #3
0
    def test_assign_participant_addresses(self):
        clusters = {"ack": {1, 17, 3, 20, 5, 7, 9, 11, 13, 15}, "default": {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 19}}
        com = Component(messagetypes=self.protocol.message_types)
        com.assign_messagetypes(self.protocol.messages, clusters)

        Address.assign_participant_addresses(self.protocol.messages, self.participants, ("78e289", "1b6033"))

        self.assertEqual(self.participants[0].address_hex, "78e289")
        self.assertEqual(self.participants[1].address_hex, "1b6033")
Example #4
0
    def test_choose_candidate(self):

        candidates1 = {'78e289': 8, '207078e2891b6033000000': 1, '57': 1, '20701b603378e289000c62': 1, '1b6033fd57': 1,
                       '1b603300': 3, '7078e2891b6033000000': 2, '78e289757e': 1, '1b6033': 14, '701b603378e289': 2}
        candidates2 = {'1b603300': 4, '701b603378e289': 2, '20701b603378e289000c62': 1, '000': 3, '0000': 19,
                       '1b6033': 11, '78e2890000': 1, '00': 4, '7078e2891b6033000000': 2, '207078e2891b6033000000': 1,
                       '78e289000': 1, '78e289': 7, '0': 7, '1b60330000': 3}

        self.assertEqual(next(Address.choose_candidate_pair(candidates1)), ("1b6033", "78e289"))
        self.assertEqual(next(Address.choose_candidate_pair(candidates2)), ("1b6033", "78e289"))
Example #5
0
    def __init__(self, protocol, participants=None, field_types=None):
        """

        :type protocol: urh.signalprocessing.ProtocolAnalyzer.ProtocolAnalyzer
        :param participants:
        """
        if participants is not None:
            protocol.auto_assign_participants(participants)

        self.protocol = protocol
        self.bitvectors = [np.array(msg.decoded_bits, dtype=np.int8) for msg in self.protocol.messages]
        self.len_cluster = self.cluster_lengths()
        self.xor_matrix = self.build_xor_matrix()


        mt = self.protocol.message_types

        field_types = FieldType.load_from_xml() if field_types is None else field_types

        self.preamble_component = Preamble(fieldtypes=field_types, priority=0, messagetypes=mt)
        self.length_component = Length(fieldtypes=field_types, length_cluster=self.len_cluster, priority=1,
                                       predecessors=[self.preamble_component], messagetypes=mt)
        self.address_component = Address(fieldtypes=field_types, xor_matrix=self.xor_matrix, priority=2,
                                         predecessors=[self.preamble_component], messagetypes=mt)
        self.sequence_number_component = SequenceNumber(fieldtypes=field_types, priority=3,
                                                        predecessors=[self.preamble_component])
        self.type_component = Type(priority=4, predecessors=[self.preamble_component])
        self.flags_component = Flags(priority=5, predecessors=[self.preamble_component])
Example #6
0
    def test_build_component_order(self):
        expected_default = [
            Preamble(fieldtypes=[]),
            Length(fieldtypes=[], length_cluster=None),
            Address(fieldtypes=[], xor_matrix=None),
            SequenceNumber(fieldtypes=[]),
            Type(),
            Flags()
        ]

        format_finder = FormatFinder(self.protocol)

        for expected, actual in zip(expected_default,
                                    format_finder.build_component_order()):
            assert type(expected) == type(actual)

        expected_swapped = [
            Preamble(fieldtypes=[]),
            Address(fieldtypes=[], xor_matrix=None),
            Length(fieldtypes=[], length_cluster=None),
            SequenceNumber(fieldtypes=[]),
            Type(),
            Flags()
        ]
        format_finder.length_component.priority = 2
        format_finder.address_component.priority = 1

        for expected, actual in zip(expected_swapped,
                                    format_finder.build_component_order()):
            assert type(expected) == type(actual)

        # Test duplicate Priority
        format_finder.sequence_number_component.priority = 4
        with self.assertRaises(ValueError) as context:
            format_finder.build_component_order()
            self.assertTrue('Duplicate priority' in context.exception)
        format_finder.sequence_number_component.priority = 3
        self.assertTrue(format_finder.build_component_order())
Example #7
0
    def test_address_candidate_finding(self):
        fh = CommonRange.from_hex

        candidates_participant_1 = [fh('1b6033'), fh('1b6033fd57'), fh('701b603378e289'), fh('20701b603378e289000c62')]
        candidates_participant_2 = [fh('1b603300'), fh('78e289757e'), fh('7078e2891b6033000000'),
                                    fh('207078e2891b6033000000')]

        expected_address1 = '1b6033'
        expected_address2 = '78e289'

        # print(Address.find_candidates(candidates_participant_1))
        # print(Address.find_candidates(candidates_participant_2))
        combined = candidates_participant_1 + candidates_participant_2
        combined.sort(key=len)
        score = Address.find_candidates(combined)
        # print(score)
        # print("=================")
        # print(sorted(score, key=lambda k: score[k], reverse=True))
        # print()

        highscored = sorted(score, key=lambda k: score[k], reverse=True)[:2]
        self.assertIn(expected_address1, highscored)
        self.assertIn(expected_address2, highscored)