Exemplo n.º 1
0
 def _test_search_1(self):
     """search()"""
     """
         Test with more regular expression where computed automaton
         has some NFA parts.
     """
     
     parser = pcre_parser()
     
     hyfa = JHybridFA()
     hyfa.load_file(aux_func.getPatternMatchDir() + "/algorithms/j_hybrid_fa/tests_data/test_search_1.re")
     hyfa.set_parser(parser)
   
     hyfa.compute()
     
     self.assertTrue(hyfa.get_compute())
     
     ret = hyfa.search("0123 uvwx")
     self.assertEqual(ret, [1,0,1,0])
     
     ret = hyfa.search("abcd abgggcd")
     self.assertEqual(ret, [0,1,0,1])
     
     ret = hyfa.search("aaaaa")
     self.assertEqual(ret, [0,0,0,0])
Exemplo n.º 2
0
    def _test_search_2(self):
        """search()"""
        """
            Test with more regular expression where are not blow up REs.
        """
        
        parser = pcre_parser()
        
        hyfa = JHybridFA()
        hyfa.load_file(aux_func.getPatternMatchDir() + "/algorithms/j_hybrid_fa/tests_data/test_search_2.re")
        hyfa.set_parser(parser)

        hyfa.compute()
        
        self.assertTrue(hyfa.get_compute())
        
        ret = hyfa.search("0123")
        self.assertEqual(ret, [1,0,0,0])
        
        ret = hyfa.search("uvwx")
        self.assertEqual(ret, [0,0,1,0])

        ret = hyfa.search("abcd uvwx")
        self.assertEqual(ret, [0,1,1,0])
        
        ret = hyfa.search("cdefgh")
        self.assertEqual(ret, [0,0,0,0])
Exemplo n.º 3
0
    def _test_search_4(self):
        """search()"""
        """
            Test with many regular expression where computed automaton
            has many NFA parts.
            Compares results of searching in computed Hybrid FA with
            regular NFA automaton searching.
        """
        parser = pcre_parser()
        parser.load_file(aux_func.getPatternMatchDir() + "/rules/Moduly/web-cgi.rules.pcre")

        nfa_aut = b_nfa()
        nfa_aut.create_by_parser(parser)
        nfa_aut.compute()
        
        parser = pcre_parser()
        
        hyfa = JHybridFA()
        hyfa.set_parser(parser)
        hyfa.load_file(aux_func.getPatternMatchDir() + "/rules/Moduly/web-cgi.rules.pcre")
        
        hyfa.compute()

        input_data = "/awstats.pl?---configdir=| /calendar-admin.pl /db4web_c.exe/aaaa:  /itemid=123f  /ShellExample.cgi?*"
        self.assertEqual(nfa_aut.search(input_data), hyfa.search(input_data))
Exemplo n.º 4
0
    def _test_search_4(self):
        """search()"""
        """
            Test with many regular expression where computed automaton
            has many NFA parts.
            Compares results of searching in computed Hybrid FA with
            regular NFA automaton searching.
        """
        parser = pcre_parser()
        parser.load_file(aux_func.getPatternMatchDir() +
                         "/rules/Moduly/web-cgi.rules.pcre")

        nfa_aut = b_nfa()
        nfa_aut.create_by_parser(parser)
        nfa_aut.compute()

        parser = pcre_parser()

        hyfa = JHybridFA()
        hyfa.set_parser(parser)
        hyfa.load_file(aux_func.getPatternMatchDir() +
                       "/rules/Moduly/web-cgi.rules.pcre")

        hyfa.compute()

        input_data = "/awstats.pl?---configdir=| /calendar-admin.pl /db4web_c.exe/aaaa:  /itemid=123f  /ShellExample.cgi?*"
        self.assertEqual(nfa_aut.search(input_data), hyfa.search(input_data))
Exemplo n.º 5
0
    def _test_compute_1(self):
        """compute()"""
        """
            Test without blow up patterns
        """
        hyfa = JHybridFA()
        
        parser = pcre_parser()
        hyfa.set_parser(parser)
        hyfa.load_file(aux_func.getPatternMatchDir() + "/algorithms/j_hybrid_fa/tests_data/test_compute_1.re")
        
        hyfa.compute()
        
        # self.get_compute() has to be True
        self.assertTrue(hyfa.get_compute())

        parser = pcre_parser()
        parser.load_file(aux_func.getPatternMatchDir() + "/algorithms/j_hybrid_fa/tests_data/test_compute_1.re")
        dfa = b_dfa()
        dfa.create_by_parser(parser)
        dfa.compute()
        
        a = hyfa.dfa.get_automaton(False)
        b = dfa.get_automaton(False)
        
        # Test without blow up patterns
        self.assertEqual(a.states.keys(), b.states.keys())
        self.assertEqual(a.alphabet, b.alphabet)
        self.assertEqual(a.start, b.start)
        self.assertEqual(a.final, b.final)
        self.assertEqual(a.transitions, b.transitions)
        self.assertTrue(a.Flags['Hybrid FA - DFA part'])
        self.assertTrue(a.Flags['Deterministic'])
        self.assertEqual(len(hyfa.nfas), 0)
        self.assertEqual(hyfa.tran_aut, {})
Exemplo n.º 6
0
    def _test_search_3(self):
        """search()"""
        """
            Test with more REs where some have blow up patterns on his starts.
        """

        parser = pcre_parser()

        hyfa = JHybridFA()
        hyfa.load_file(aux_func.getPatternMatchDir() +
                       "/algorithms/j_hybrid_fa/tests_data/test_search_3.re")
        hyfa.set_parser(parser)

        hyfa.compute()

        self.assertTrue(hyfa.get_compute())

        ret = hyfa.search("0123")
        self.assertEqual(ret, [1, 0, 0, 0])

        ret = hyfa.search("uvwx")
        self.assertEqual(ret, [0, 0, 1, 0])

        ret = hyfa.search("abcd uvwx")
        self.assertEqual(ret, [0, 1, 1, 1])

        ret = hyfa.search("abcd agcd")
        self.assertEqual(ret, [0, 1, 0, 1])

        ret = hyfa.search("cdefgh")
        self.assertEqual(ret, [0, 0, 0, 0])
Exemplo n.º 7
0
    def _test_search_2(self):
        """search()"""
        """
            Test with more regular expression where are not blow up REs.
        """

        parser = pcre_parser()

        hyfa = JHybridFA()
        hyfa.load_file(aux_func.getPatternMatchDir() +
                       "/algorithms/j_hybrid_fa/tests_data/test_search_2.re")
        hyfa.set_parser(parser)

        hyfa.compute()

        self.assertTrue(hyfa.get_compute())

        ret = hyfa.search("0123")
        self.assertEqual(ret, [1, 0, 0, 0])

        ret = hyfa.search("uvwx")
        self.assertEqual(ret, [0, 0, 1, 0])

        ret = hyfa.search("abcd uvwx")
        self.assertEqual(ret, [0, 1, 1, 0])

        ret = hyfa.search("cdefgh")
        self.assertEqual(ret, [0, 0, 0, 0])
Exemplo n.º 8
0
    def _test_search_1(self):
        """search()"""
        """
            Test with more regular expression where computed automaton
            has some NFA parts.
        """

        parser = pcre_parser()

        hyfa = JHybridFA()
        hyfa.load_file(aux_func.getPatternMatchDir() +
                       "/algorithms/j_hybrid_fa/tests_data/test_search_1.re")
        hyfa.set_parser(parser)

        hyfa.compute()

        self.assertTrue(hyfa.get_compute())

        ret = hyfa.search("0123 uvwx")
        self.assertEqual(ret, [1, 0, 1, 0])

        ret = hyfa.search("abcd abgggcd")
        self.assertEqual(ret, [0, 1, 0, 1])

        ret = hyfa.search("aaaaa")
        self.assertEqual(ret, [0, 0, 0, 0])
Exemplo n.º 9
0
    def _test_search_3(self):
        """search()"""
        """
            Test with more REs where some have blow up patterns on his starts.
        """
        
        parser = pcre_parser()
        
        hyfa = JHybridFA()
        hyfa.load_file(aux_func.getPatternMatchDir() + "/algorithms/j_hybrid_fa/tests_data/test_search_3.re")
        hyfa.set_parser(parser)

        hyfa.compute()
        
        self.assertTrue(hyfa.get_compute())
        
        ret = hyfa.search("0123")
        self.assertEqual(ret, [1,0,0,0])
        
        ret = hyfa.search("uvwx")
        self.assertEqual(ret, [0,0,1,0])

        ret = hyfa.search("abcd uvwx")
        self.assertEqual(ret, [0,1,1,1])
        
        ret = hyfa.search("abcd agcd")
        self.assertEqual(ret, [0,1,0,1])
        
        ret = hyfa.search("cdefgh")
        self.assertEqual(ret, [0,0,0,0])
Exemplo n.º 10
0
    def _test_compute_5(self):
        """compute()"""
        """
            Test where are more blow up REs
        """
        hyfa = JHybridFA()
        
        parser = pcre_parser()
        hyfa.set_parser(parser)
        hyfa.load_file(aux_func.getPatternMatchDir() + "/algorithms/j_hybrid_fa/tests_data/test_compute_5.re")
        
        hyfa.compute()
        
        # self.get_compute() has to be True
        self.assertTrue(hyfa.get_compute())

        # self.get_compute() has to be True
        self.assertTrue(hyfa.get_compute())

        hd = hyfa.dfa.get_automaton(False)
        hn0 = hyfa.nfas[0].get_automaton(False)
        hn1 = hyfa.nfas[1].get_automaton(False)
        d = nfa_data().load_from_file(aux_func.getPatternMatchDir() + "/algorithms/j_hybrid_fa/tests_data/test_compute_5_dfa.nfa_data")
        n0 = nfa_data().load_from_file(aux_func.getPatternMatchDir() + "/algorithms/j_hybrid_fa/tests_data/test_compute_5_nfa0.nfa_data")
        n1 = nfa_data().load_from_file(aux_func.getPatternMatchDir() + "/algorithms/j_hybrid_fa/tests_data/test_compute_5_nfa1.nfa_data")

        # test where are more blow up REs
        # test of DFA part
        self.assertEqual(hd.states.keys(), d.states.keys())
        self.assertEqual(hd.alphabet, d.alphabet)
        self.assertEqual(hd.start, d.start)
        self.assertEqual(len(hd.final), 3)
        self.assertEqual(hd.transitions, d.transitions)
        self.assertTrue(hd.Flags['Hybrid FA - DFA part'])
        self.assertTrue(hd.Flags['Deterministic'])
        
        self.assertEqual(len(hyfa.nfas), 2)
        self.assertEqual({0:0, 1: 10}, hyfa.tran_aut)
        
        # test of NFA part #0
        self.assertEqual(hn0.states.keys(), n0.states.keys())
        self.assertEqual(hn0.alphabet, n0.alphabet)
        self.assertEqual(hn0.start, n0.start)
        self.assertEqual(hn0.final, n0.final)
        self.assertEqual(hn0.transitions, n0.transitions)
        self.assertTrue(hn0.Flags['Hybrid FA - one NFA part'])

        # test of NFA part #1
        self.assertEqual(hn1.states.keys(), n1.states.keys())
        self.assertEqual(hn1.alphabet, n1.alphabet)
        self.assertEqual(hn1.start, n1.start)
        self.assertEqual(hn1.final, n1.final)
        self.assertEqual(hn1.transitions, n1.transitions)
        self.assertTrue(hn1.Flags['Hybrid FA - one NFA part'])
Exemplo n.º 11
0
    def _test_compute_4(self):
        """compute()"""
        """
            Test with more patterns where some are blow up
        """
        hyfa = JHybridFA()

        parser = pcre_parser()
        hyfa.set_parser(parser)
        hyfa.load_file(aux_func.getPatternMatchDir() +
                       "/algorithms/j_hybrid_fa/tests_data/test_compute_4.re")

        hyfa.compute()

        # self.get_compute() has to be True
        self.assertTrue(hyfa.get_compute())

        # self.get_compute() has to be True
        self.assertTrue(hyfa.get_compute())

        hd = hyfa.dfa.get_automaton(False)
        hn0 = hyfa.nfas[0].get_automaton(False)
        d = nfa_data().load_from_file(
            aux_func.getPatternMatchDir() +
            "/algorithms/j_hybrid_fa/tests_data/test_compute_4_dfa.nfa_data")
        n = nfa_data().load_from_file(
            aux_func.getPatternMatchDir() +
            "/algorithms/j_hybrid_fa/tests_data/test_compute_4_nfa0.nfa_data")

        # Test with more patterns where some are blow up
        # test of DFA part
        self.assertEqual(hd.states.keys(), d.states.keys())
        self.assertEqual(hd.alphabet, d.alphabet)
        self.assertEqual(hd.start, d.start)
        self.assertEqual(len(hd.final), 2)
        self.assertEqual(hd.transitions, d.transitions)
        self.assertTrue(hd.Flags['Hybrid FA - DFA part'])
        self.assertTrue(hd.Flags['Deterministic'])

        self.assertEqual(len(hyfa.nfas), 1)
        self.assertEqual({0: 0}, hyfa.tran_aut)

        # test of NFA part #0
        self.assertEqual(hn0.states.keys(), n.states.keys())
        self.assertEqual(hn0.alphabet, n.alphabet)
        self.assertEqual(hn0.start, n.start)
        self.assertEqual(hn0.final, n.final)
        self.assertEqual(hn0.transitions, n.transitions)
        self.assertTrue(hn0.Flags['Hybrid FA - one NFA part'])
Exemplo n.º 12
0
 def _test_get_trans_num_1(self):
     """get_state_num()"""
     """
         Test with more regular expression where computed automaton
         has only DFA part.
     """
     hyfa = JHybridFA()
     
     parser = pcre_parser()
     hyfa.set_parser(parser)
     hyfa.load_file(aux_func.getPatternMatchDir() + "/algorithms/j_hybrid_fa/tests_data/test_get_xxx_num_1.re")
     
     hyfa.compute()
     
     self.assertEqual(hyfa.get_trans_num(), 102)
Exemplo n.º 13
0
    def _test_report_memory_naive_2(self):
        """report_memory_naive()"""
        """
            Test with more regular expression where computed automaton
            has some NFA parts.
        """
        hyfa = JHybridFA()
        
        parser = pcre_parser()
        hyfa.set_parser(parser)
        hyfa.load_file(aux_func.getPatternMatchDir() + "/algorithms/j_hybrid_fa/tests_data/test_get_xxx_num_2.re")
        
        hyfa.compute()

        self.assertEqual(hyfa.report_memory_naive(), 390)
Exemplo n.º 14
0
    def _test_report_memory_naive_2(self):
        """report_memory_naive()"""
        """
            Test with more regular expression where computed automaton
            has some NFA parts.
        """
        hyfa = JHybridFA()

        parser = pcre_parser()
        hyfa.set_parser(parser)
        hyfa.load_file(
            aux_func.getPatternMatchDir() +
            "/algorithms/j_hybrid_fa/tests_data/test_get_xxx_num_2.re")

        hyfa.compute()

        self.assertEqual(hyfa.report_memory_naive(), 390)
Exemplo n.º 15
0
    def _test_get_trans_num_1(self):
        """get_state_num()"""
        """
            Test with more regular expression where computed automaton
            has only DFA part.
        """
        hyfa = JHybridFA()

        parser = pcre_parser()
        hyfa.set_parser(parser)
        hyfa.load_file(
            aux_func.getPatternMatchDir() +
            "/algorithms/j_hybrid_fa/tests_data/test_get_xxx_num_1.re")

        hyfa.compute()

        self.assertEqual(hyfa.get_trans_num(), 102)
Exemplo n.º 16
0
def get_hybfas(ruleset):
    """
        Generate number of states, transitions and consumed memory for        \
        History FA (Suchodol).
    """
    # Create parser - use default parser
    po = parser.parser()
    # Create Hybrid FA object
    hyb_fa = JHybridFA()
    # Set parser
    hyb_fa.set_parser(po)
    # Parse input file
    hyb_fa.load_file(ruleset)
    # Create Hybrid FA
    hyb_fa.compute()
    # Return experimental results
    return ["Hybrid FA (Suchodol)", hyb_fa.get_state_num(), hyb_fa.get_trans_num(), hyb_fa.report_memory_optimal(), hyb_fa.report_memory_naive()]
Exemplo n.º 17
0
    def _test_compute_3(self):
        """compute()"""
        """
            Test with more patterns and one with blow up on start on RE
        """
        hyfa = JHybridFA()
        
        parser = pcre_parser()
        hyfa.set_parser(parser)
        hyfa.load_file(aux_func.getPatternMatchDir() + "/algorithms/j_hybrid_fa/tests_data/test_compute_3.re")
        
        hyfa.compute()
        
        # self.get_compute() has to be True
        self.assertTrue(hyfa.get_compute())

        # self.get_compute() has to be True
        self.assertTrue(hyfa.get_compute())

        hd = hyfa.dfa.get_automaton(False)
        hn0 = hyfa.nfas[0].get_automaton(False)
        d = nfa_data().load_from_file(aux_func.getPatternMatchDir() + "/algorithms/j_hybrid_fa/tests_data/test_compute_3_dfa.nfa_data")
        n = nfa_data().load_from_file(aux_func.getPatternMatchDir() + "/algorithms/j_hybrid_fa/tests_data/test_compute_3_nfa0.nfa_data")

        # Test with more patterns and one with blow up on start on RE
        # test of DFA part
        self.assertEqual(hd.states.keys().sort(), d.states.keys().sort())
        self.assertEqual(hd.alphabet, d.alphabet)
        self.assertEqual(hd.start, d.start)
        self.assertEqual(len(hd.final), 3)
        self.assertEqual(hd.transitions, d.transitions)
        self.assertTrue(hd.Flags['Hybrid FA - DFA part'])
        self.assertTrue(hd.Flags['Deterministic'])
        
        self.assertEqual(len(hyfa.nfas), 1)
        self.assertEqual(hyfa.tran_aut, {0: 8})
        
        # test of NFA part #0
        self.assertEqual(hn0.states.keys().sort(), n.states.keys().sort())
        self.assertEqual(hn0.alphabet, n.alphabet)
        self.assertEqual(hn0.start, n.start)
        self.assertEqual(hn0.final, n.final)
        self.assertEqual(hn0.transitions, n.transitions)
        self.assertTrue(hn0.Flags['Hybrid FA - one NFA part'])
Exemplo n.º 18
0
    def _test_compute_1(self):
        """compute()"""
        """
            Test without blow up patterns
        """
        hyfa = JHybridFA()

        parser = pcre_parser()
        hyfa.set_parser(parser)
        hyfa.load_file(aux_func.getPatternMatchDir() +
                       "/algorithms/j_hybrid_fa/tests_data/test_compute_1.re")

        hyfa.compute()

        # self.get_compute() has to be True
        self.assertTrue(hyfa.get_compute())

        parser = pcre_parser()
        parser.load_file(
            aux_func.getPatternMatchDir() +
            "/algorithms/j_hybrid_fa/tests_data/test_compute_1.re")
        dfa = b_dfa()
        dfa.create_by_parser(parser)
        dfa.compute()

        a = hyfa.dfa.get_automaton(False)
        b = dfa.get_automaton(False)

        # Test without blow up patterns
        self.assertEqual(a.states.keys(), b.states.keys())
        self.assertEqual(a.alphabet, b.alphabet)
        self.assertEqual(a.start, b.start)
        self.assertEqual(a.final, b.final)
        self.assertEqual(a.transitions, b.transitions)
        self.assertTrue(a.Flags['Hybrid FA - DFA part'])
        self.assertTrue(a.Flags['Deterministic'])
        self.assertEqual(len(hyfa.nfas), 0)
        self.assertEqual(hyfa.tran_aut, {})
Exemplo n.º 19
0
def get_hybfas(ruleset):
    """
        Generate number of states, transitions and consumed memory for        \
        History FA (Suchodol).
    """
    # Create parser - use default parser
    po = parser.parser()
    # Create Hybrid FA object
    hyb_fa = JHybridFA()
    # Set parser
    hyb_fa.set_parser(po)
    # Parse input file
    hyb_fa.load_file(ruleset)
    # Create Hybrid FA
    hyb_fa.compute()
    # Return experimental results
    return [
        "Hybrid FA (Suchodol)",
        hyb_fa.get_state_num(),
        hyb_fa.get_trans_num(),
        hyb_fa.report_memory_optimal(),
        hyb_fa.report_memory_naive()
    ]