1
mirror of https://github.com/rapid7/metasploit-framework synced 2024-11-05 14:57:30 +01:00

updated msftest assert functions and fixed a few bugs in the associated library

git-svn-id: file:///home/svn/framework3/trunk@10634 4d416f70-5f16-0410-b530-b9f4589650da
This commit is contained in:
Jonathan Cran 2010-10-11 01:57:33 +00:00
parent e7fe407106
commit 48af4d63c1
2 changed files with 71 additions and 20 deletions

View File

@ -1,27 +1,33 @@
## This class consists of assert helper methods for regexing logs
##
## $id$
$:.unshift(File.expand_path(File.dirname(__FILE__))
$:.unshift(File.expand_path(File.dirname(__FILE__)))
require 'regexr'
require 'test/unit'
class MSFTest < Test::Unit::TestCase
class MsfTest < Test::Unit::TestCase
def initialize
def setup
super
@case_insensitive = true
@regexr = Regexr.new
end
def assert_complete(data,thestart,theend)
assert_true @regexr.verify_start_and_end(data,thestart,theend), "The start or end did not match the expected string"
def assert_complete(data,first,last)
assert_not_nil @regexr.verify_start(data,first), "The start string " + data.split("\n").first + " did not match the expected string: " + first
assert_not_nil @regexr.verify_end(data,last), "The end string " + data.split("\n").last + " did not match the expected string: " + last
end
def assert_all_successes(data, regexes)
assert_true @regexr.scan_for_successes(data,regexes), "All strings were found in the data"
def assert_all_successes(data, regex_strings)
regex_strings.each { |regex_string|
assert_true @regexr.ensure_exists_in_data(data,regex_string), "The string " + regex_string + " was not found in the data."
}
end
def assert_no_failures(data, regexes, exceptions)
assert_true @regexr.scan_for_failures(data,regexes,exceptions), "A non-exccepted failure was found in the data"
def assert_no_failures(data, regex_strings, exception_strings)
regex_strings.each { |regex_string|
assert_true @regexr.ensure_doesnt_exist_in_data_unless(data,regex_string,exception_strings), "The string " + regex_string + " was found in the the data, and no exception was found."
}
end
end

View File

@ -6,22 +6,22 @@
class Regexr
def initialize
@verbose = true
def initialize(verbose=false)
@verbose = verbose
end
# Check for the beginning line. Handy when you need to ensure a log has started
def verify_start(data,the_start)
data_lines = data.split("\n")
regex_start = Regexp.new(the_start, @case_insensitive)
return regex_start.match(data_lines.first)
return regex_start =~ data_lines.first
end
# Check for end line. Handy when you need to ensure a log has completed.
def verify_end(data,the_end)
data_lines = data.split("\n")
regex_end = Regexp.new(the_end, @case_insensitive)
return regex_end.match(data_lines.last)
return regex_end =~ data_lines.last
end
# Check for the beginning and end lines. Handy when you need to ensure a log has started & completed
@ -32,26 +32,71 @@ class Regexr
## yuck, refactor this - TODO
if regex_start.match == data_lines.first
if regex_start =~ data_lines.first
return regex_endline == data_lines.last
end
return false
end
# Scan for a success line. In order to pass, the string must exist in the data
def ensure_exists_in_data(data,regex_string)
data_lines = data.split("\n")
re = Regexp.new(regex_string, @case_insensitive)
data_lines.each {|line|
if line =~ re
return true
end
}
return false
end
# Scan for a fail line. In order to pass, the string must not exist in the data
def ensure_doesnt_exist_in_data(data,regex_string)
data_lines = data.split("\n")
re = Regexp.new(regex_string, @case_insensitive)
data_lines.each {|line|
if line =~ re
return false
end
}
return true
end
# Scan for a fail line. In order to pass, the string must not exist in the data
def ensure_doesnt_exist_in_data_unless(data,regex_string, exceptions)
data_lines = data.split("\n")
re = Regexp.new(regex_string, @case_insensitive)
data_lines.each {|line|
if line =~ re
exceptions.each { |exception|
if line =~ exception
return true
end
}
return false
end
}
return true
end
# Scan for any number of success lines. In order to pass, all successes must match.
def ensure_all_exist_in_data(data,regexes=[])
data_lines = data.split("\n")
if regexes
success = false
target_successes = regexes.size
count = 0
success_count = 0
regexes.each { |condition|
matched = false
re = Regexp.new(condition, @case_insensitive)
data_lines.each {|line|
if line =~ re
count += 1
success_count += 1
matched = true
break
end
@ -66,7 +111,7 @@ class Regexr
end
}
if target_successes == count
if target_successes == success_count
if @verbose
puts "DEBUG: woot, got all successes"
end
@ -94,7 +139,7 @@ class Regexr
data_lines.each { |line|
if re.match(line)
if re =~ line
okay = false # oh, we found a match
if @verbose
@ -107,7 +152,7 @@ class Regexr
reg_exception = Regexp.new(exception, @case_insensitive)
# If the exception matches here, we'll spare it
if reg_exception.match(line)
if reg_exception =~ line
if @verbose
puts "\'" + line + "\' is an exception, we can ignore it."
end