diff --git a/lib/msf/core.rb b/lib/msf/core.rb index 839147b94c..26e42b07a3 100644 --- a/lib/msf/core.rb +++ b/lib/msf/core.rb @@ -9,10 +9,6 @@ # ### -# Unit testing -#require 'test/unit' -require 'Msf/Core/UnitTestSuite' - # framework-core depends on framework-shared require 'Msf/Shared' diff --git a/lib/msf/core.rb.ts.rb b/lib/msf/core.rb.ts.rb new file mode 100644 index 0000000000..c097fb99f2 --- /dev/null +++ b/lib/msf/core.rb.ts.rb @@ -0,0 +1,15 @@ +#!/usr/bin/ruby + +require 'test/unit' +require 'Msf/Core' +require 'Msf/Core/OptionContainer.rb.ut' + +class Msf::TestSuite + def self.suite + suite = Test::Unit::TestSuite.new + + suite << Msf::OptionContainer::UnitTest.suite + + return suite; + end +end diff --git a/lib/msf/core/option_container.rb b/lib/msf/core/option_container.rb index 9a0e8c2428..ca7db1afe3 100644 --- a/lib/msf/core/option_container.rb +++ b/lib/msf/core/option_container.rb @@ -236,108 +236,4 @@ class OptionContainer < Hash end -module Test - -begin -### -# -# OptionContainerTestCase -# ----------------------- -# -# This class implements some testing routines for ensuring that the option -# container is operating correctly. -# -### -class OptionContainerTestCase < ::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 -rescue -end - -end - end diff --git a/lib/msf/core/option_container.rb.ut.rb b/lib/msf/core/option_container.rb.ut.rb new file mode 100644 index 0000000000..440878575f --- /dev/null +++ b/lib/msf/core/option_container.rb.ut.rb @@ -0,0 +1,99 @@ +#!/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 diff --git a/user_interfaces/tc.rb b/user_interfaces/tc.rb index c517e51386..a72bbfe3d0 100755 --- a/user_interfaces/tc.rb +++ b/user_interfaces/tc.rb @@ -1,6 +1,6 @@ #!/usr/bin/ruby -I../Lib require 'test/unit/ui/console/testrunner' -require 'Msf/Core' +require 'Msf/Core.rb.ts' -Test::Unit::UI::Console::TestRunner.run(Msf::Test::FrameworkCoreTestSuite) +Test::Unit::UI::Console::TestRunner.run(Msf::TestSuite)