CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/test/lib/regexr.rb
Views: 11766
1
## This class consists of helper methods for regexing logs
2
##
3
## TODO - clean up the style. looks like it was written in the early 90s
4
##
5
## $Id$
6
7
class Regexr
8
9
def initialize(verbose = false, case_insensitive = true)
10
@verbose = verbose
11
@case_insensitive = case_insensitive
12
end
13
14
# Check for the beginning and end lines. Handy when you need to ensure a log has started & completed
15
def verify_start_and_end(data, the_start, the_end)
16
return false unless data
17
18
data_lines = data.split("\n")
19
regex_start = Regexp.new(the_start, @case_insensitive)
20
regex_end = Regexp.new(the_end, @case_insensitive)
21
22
if regex_start =~ data_lines.first
23
return regex_end =~ data_lines.last
24
end
25
26
return false
27
end
28
29
# Scan for any number of success lines. In order to pass, all successes must match.
30
def find_strings_that_dont_exist_in_data(data, regexes = [])
31
return false unless data
32
33
data_lines = data.split("\n")
34
35
return nil unless regexes ## count as a pass
36
37
if regexes
38
target_successes = regexes.size
39
success_count = 0
40
regexes.each { |condition|
41
## assume we haven't got it
42
found = false
43
44
re = Regexp.new(condition, @case_insensitive)
45
46
## for each of our data lines
47
data_lines.each { |line|
48
## if it's a match
49
if line =~ re
50
found = true
51
break ## success!
52
end
53
}
54
55
if !found
56
return condition ## return this string, it wasn't found.
57
end
58
}
59
end
60
61
nil ## got all successes, woot!
62
end
63
64
# Scan for failures -- if any single failure matches, the test returns true.
65
def find_strings_that_exist_in_data_except(data, regexes = [], exceptions = [])
66
return false unless data
67
68
data_lines = data.split("\n")
69
70
return nil unless regexes ## count as a pass
71
72
regexes.each { |condition|
73
## for each failure condition that we've been passed
74
re = Regexp.new(condition, @case_insensitive)
75
76
## assume we're okay
77
found = false
78
79
data_lines.each { |line|
80
if re =~ line
81
found = true # oh, we found a match
82
83
# but let's check the exceptions
84
exceptions.map { |exception|
85
reg_exception = Regexp.new(exception, @case_insensitive)
86
87
# If the exception matches here, we'll spare it
88
if reg_exception =~ line
89
found = false
90
break
91
end
92
}
93
94
# If we didn't find an exception, we have to fail it. do not pass go.
95
return condition if found
96
end
97
}
98
}
99
100
nil ## no failures found!
101
end
102
end
103
104