def test_fix_all_star_template(self): message1 = Message(['message', 'sent', 'A1']) message2 = Message(['message', 'sent', 'A2']) message3 = Message(['message', 'sent', 'A2', 'from', ':', 'B1']) message4 = Message(['message', 'sent', 'A2', 'from', ':', 'B2']) messages = {3: [message1, message2], 6: [message3, message4]} template = Template(['*', '*', '*', '*', '*', '*']) template.matched_lines = [0, 1] ch = Chromosome({6: [template]}) fix_all_star_template(ch, 6, 0, messages) self.assertFalse(is_all_star_template(ch.templates[6][0]))
def test_no_variable_parts(self): # create messages message1 = Message(['read', 'text', 'file1', 'from', 'ABC1']) message2 = Message(['read', 'text', 'file2', 'from', 'ABC2']) message3 = Message(['read', 'text', 'file3', 'from', 'ABC3']) messages = {5: [message1, message2, message3]} # create a chromosome template = Template(['read', 'text', '*', 'from', '*']) template.matched_lines = [0, 1, 2] ch = Chromosome({5: [template]}) # code to test check_variable_parts(ch, messages) self.assertEqual(ch.templates[5][0].to_string(), "[ read text * from * ]")
def test_check_variable_parts_2templates(self): # create messages message1 = Message(['read', 'text', 'file1', 'from', 'ABC1']) message2 = Message(['read', 'text', 'file2', 'from', 'ABC3']) message3 = Message(['read', 'text', 'file2', 'from', 'ABC4']) message4 = Message( ['read', 'text', 'file', 'ABC', 'from', 'DB', '98765']) message5 = Message( ['read', 'text', 'file', 'DSE', 'from', 'DB', '7654']) messages = {5: [message1, message2, message3], 7: [message4, message5]} # create a chromosome template1 = Template(['read', '*', '*', 'from', '*']) template1.matched_lines = [0, 1, 2] template2 = Template(['read', '*', 'file', '*', 'from', 'DB', '*']) template2.matched_lines = [0, 1] ch = Chromosome({5: [template1], 7: [template2]}) # code to test check_variable_parts(ch, messages) self.assertEqual(ch.templates[5][0].to_string(), "[ read text * from * ]") self.assertEqual(ch.templates[7][0].to_string(), "[ read text file * from DB * ]")
def compute_matched_lines(messages: dict, template: Template): # if the templates has not be changed # we don't need to recompute the list of matched lines if not template.is_changed(): return # Otherwise, we recompute the list of matched lines template.matched_lines = [] cluster_id = template.get_length() for index in range(0, len(messages[cluster_id])): message = messages[cluster_id][index] if match(message, template): template.matched_lines.append(index) # Now the template has to be set as NOT CHANGED template.set_changed(False)
def test_add_template_to_reach_100cov(self): logfile = ROOT_DIR + '/test/resources/File.log' chrom_gen = ChromosomeGenerator(logfile, 0, '\n', ["'[\w\d\$\-:,\./_ ><\|]*'"]) chrom_mutator_100 = ChromosomeMutator100cov(chrom_gen) template = Template(['Message', 'sent', 'by', 'EEE', ',', 'at', 'port', '1']) template.matched_lines = [0] chromosome = Chromosome({8: [template]}) chrom_mutator_100.add_template_to_reach_100cov(chromosome, 8) chromosome_matched_lines = [] for t in chromosome.templates[8]: for i in t.matched_lines: chromosome_matched_lines.append(i) self.assertEqual(len(set(chromosome_matched_lines)), len(chrom_gen.messages[8]))
def test_apply_mutation(self): logfile = ROOT_DIR + '/test/resources/File.log' chrom_gen = ChromosomeGenerator(logfile, 0, '\n', ["'[\w\d\$\-:,\./_ ><\|]*'"]) chrom_mutator_100 = ChromosomeMutator100cov(chrom_gen) template = Template(['Driver', ':', '*']) template.matched_lines = [5] chromosome = Chromosome({3: [template]}) chrom_mutator_100.apply_mutation(chromosome) chromosome_matched_lines = [] for t in chromosome.templates[3]: for i in t.matched_lines: chromosome_matched_lines.append(i) self.assertEqual(len(set(chromosome_matched_lines)), len(chrom_gen.messages[3]))
def test_derive_template(self): t1 = Template(['configuration', 'file', '*']) t1.matched_lines = [0, 1, 2, 3, 4, 5] t2 = Template(['configuration', 'file', 'f1']) t2.matched_lines = [1] t3 = Template(['configuration', 'file', '*']) t3.matched_lines= [0, 1, 2, 3, 4, 5] t4 = Template(['configuration', 'file', 'f3']) t4.matched_lines = [4] t5 = Template(['configuration', 'file', 'f5']) t5.matched_lines = [5] t6 = Template(['configuration', 'file', 'f6']) t6.matched_lines = [3] partition = [t1, t2, t3, t4, t5, t6] to_remove_sub = set() derive_sub_template(partition, to_remove_sub) self.assertEqual(len(to_remove_sub),5) to_remove_super = set() derive_super_template(partition, to_remove_super) self.assertEqual(len(to_remove_super), 2)
def test_remove_super_templates(self): t1 = Template(['read', 'text', 'file', 'ABC', 'from', 'DB', '*']) t1.matched_lines = [3] t2 = Template(['read', 'text', 'file', '*', 'from', 'DB', '*']) t2.matched_lines = [0, 1] t3 = Template(['read', 'text', 'file', '*', 'from', 'DB', '2323232']) t3.matched_lines = [0] t4 = Template(['read', 'text', 'file', '*', 'from', 'DB', '*']) t4.matched_lines = [0, 1] t5 = Template(['file', 'configuration', '*']) t5.matched_lines = [2, 3, 4] t6 = Template(['file', 'configuration', 'A']) t6.matched_lines = [2] ch = Chromosome({7: [t1, t2, t3, t4, t3, t4], 3: [t5, t6]}) remove_super_templates(ch, 7) self.assertEqual(ch.cluster_size(7), 2) remove_super_templates(ch, 3) self.assertEqual(ch.cluster_size(3), 1) self.assertEqual(ch.templates[3][0].token, ['file', 'configuration', 'A'])