Exemple #1
0
    def test_normalize_parse_tree(self):
        num_matches = 0
        for parse_tree, s, e in CiscoIOSParser.lineRegion.scanString(self.ios_data1):
            num_matches = num_matches + 1
            #pprint.pprint(parse_tree.asList())
            nparse_tree = CiscoIOSParser.normalizeParseTree(parse_tree.asList())
            #pprint.pprint(nparse_tree)
        self.assertEqual(num_matches, 16)

        num_matches = 0
        for parse_tree, s, e in CiscoIOSParser.indentedLineRegion.scanString(self.ios_data1):
            num_matches = num_matches + 1
            #pprint.pprint(parse_tree.asList())
            nparse_tree = CiscoIOSParser.normalizeParseTree(parse_tree.asList())
            #pprint.pprint(nparse_tree)
        self.assertEqual(num_matches, 12)

        num_matches = 0
        for parse_tree, s, e in CiscoIOSParser.interfaceRegion.scanString(self.ios_data1):
            num_matches = num_matches + 1
            nparse_tree = CiscoIOSParser.normalizeParseTree(parse_tree.asList())
            #pprint.pprint(parse_tree.asList())
            #pprint.pprint(nparse_tree)
            self.assertTrue(nparse_tree['type'] == 'interface')
            self.assertTrue(nparse_tree['id'] != None)
        self.assertEqual(num_matches, 2)

        num_matches = 0
        for parse_tree, s, e in CiscoIOSParser.cryptoRegion.scanString(self.ios_data1):
            num_matches = num_matches + 1
            #pprint.pprint(parse_tree.asList())
            nparse_tree = CiscoIOSParser.normalizeParseTree(parse_tree.asList())
            #pprint.pprint(nparse_tree)
            self.assertTrue(nparse_tree['type'] == 'crypto')
            self.assertTrue(nparse_tree['id'] != None)
        self.assertEqual(num_matches, 0)

        num_matches = 0
        for parse_tree, s, e in CiscoIOSParser.configFileRegion.scanString(self.ios_data1):
            num_matches = num_matches + 1
            #pprint.pprint(parse_tree.asList())
            nparse_tree = CiscoIOSParser.normalizeParseTree(parse_tree.asList())
            #pprint.pprint(nparse_tree)
            self.assertTrue(nparse_tree['type'] == None)
            self.assertTrue(nparse_tree['id'] == None)
        self.assertEqual(num_matches, 1)
Exemple #2
0
    def test_get_region_id(self):
        # Write Cisco IOS tests for interface, and line
        parser = CiscoIOSParser()

        line_nos = range(1, 45)
        line_nos = map(str, line_nos)
        expected_ids = {'line': line_nos,
                         'interface': ['Loopback0', 'GigabitEthernet4/2']}

        for region_type in expected_ids.keys():
            production = parser.getGrammarForUnit(region_type)

            match_forest = production.scanString(self.ios_data1)
            ids = []
            subtree_idx = 0
            for subtree, s, e in match_forest:
                new_id = parser.getRegionID(subtree, region_type, subtree_idx)
                ids.append(new_id)
                subtree_idx = subtree_idx + 1

            i = 0
            for id in ids:
                self.assertEqual(id, expected_ids[region_type][i])
                i = i + 1