100 lines
2.3 KiB
Ruby
100 lines
2.3 KiB
Ruby
|
#!/usr/bin/ruby
|
||
|
|
||
|
$:.unshift(File.join('..', '..', File.dirname(__FILE__)))
|
||
|
|
||
|
require 'test/unit'
|
||
|
require 'Msf/Core/OptionContainer'
|
||
|
|
||
|
module Msf
|
||
|
|
||
|
class OptionContainer::UnitTest < Test::Unit::TestCase
|
||
|
|
||
|
# Tests the initialization of the OptionContainer object
|
||
|
def test_initialize
|
||
|
# Make sure initialization works
|
||
|
options = nil
|
||
|
|
||
|
assert_block("initialize failed") {
|
||
|
options = OptionContainer.new(
|
||
|
'rhost' => [ OptAddress, true, nil, 'host.com' ],
|
||
|
'rport' => [ OptPort, true, nil, 1234 ])
|
||
|
|
||
|
if (options == nil)
|
||
|
false
|
||
|
end
|
||
|
|
||
|
true
|
||
|
}
|
||
|
|
||
|
# Make sure there are 2 options
|
||
|
assert_equal(2, options.length, "invalid number of options #{options.length}")
|
||
|
|
||
|
# Make sure that the constructor raises an argument error when
|
||
|
# an invalid option is supplied
|
||
|
assert_raise(ArgumentError, "initialize invalid failed") {
|
||
|
OptionContainer.new(
|
||
|
'rhost' => 'invalid');
|
||
|
}
|
||
|
end
|
||
|
|
||
|
# Tests getting the value of an option
|
||
|
def test_get
|
||
|
options = OptionContainer.new(
|
||
|
'rport' => [ OptPort, true, nil, 1234 ])
|
||
|
|
||
|
assert_equal(1234, options.get('rport').default,
|
||
|
"option default does not match")
|
||
|
assert_equal(true, options.get('rport').required?,
|
||
|
"option required does not match")
|
||
|
assert_equal('rport', options['rport'].name,
|
||
|
"option name does not match")
|
||
|
end
|
||
|
|
||
|
# Tests validation
|
||
|
def test_validate
|
||
|
# Test validating required options
|
||
|
options = OptionContainer.new(
|
||
|
'rhost' => [ OptAddress, true ],
|
||
|
'rport' => [ OptPort, true ],
|
||
|
'Lib' => [ OptString ])
|
||
|
|
||
|
ds = DataStore.new
|
||
|
|
||
|
assert_raise(OptionValidateError, "required validation failed") {
|
||
|
options.validate(ds)
|
||
|
}
|
||
|
|
||
|
ds['rhost'] = 'www.invalid.host.tldinvalid'
|
||
|
ds['rport'] = 1234
|
||
|
|
||
|
assert_raise(OptionValidateError, "host validation failed") {
|
||
|
options.validate(ds)
|
||
|
}
|
||
|
|
||
|
# Make sure address validation does work
|
||
|
ds['rhost'] = 'www.google.com'
|
||
|
|
||
|
assert_equal(true, options.validate(ds), "overall validation failed")
|
||
|
|
||
|
# Make sure port validation does work
|
||
|
ds['rport'] = 23423423
|
||
|
|
||
|
assert_raise(OptionValidateError, "port validation failed") {
|
||
|
options.validate(ds)
|
||
|
}
|
||
|
end
|
||
|
|
||
|
# Make sure advanced additions work
|
||
|
def test_advanced
|
||
|
options = OptionContainer.new
|
||
|
|
||
|
options.add_advanced_options(
|
||
|
'DONKEY' => [ OptString, false ])
|
||
|
|
||
|
assert_equal(true, options.get('DONKEY').advanced?,
|
||
|
"advanced option failed")
|
||
|
end
|
||
|
end
|
||
|
|
||
|
end
|