From 4ba571346ec7e6c592fede527fae905dc81c3c52 Mon Sep 17 00:00:00 2001 From: Luke Imhoff Date: Fri, 24 May 2013 12:33:42 -0500 Subject: [PATCH] Spec Msf::Simple::Framework#init_module_paths [#47720609] --- lib/msf/base/simple/framework.rb | 29 +----- lib/msf/base/simple/framework/module_paths.rb | 33 +++++++ spec/lib/msf/base/simple/framework_spec.rb | 11 +++ .../msf/simple/framework/module_paths.rb | 93 +++++++++++++++++++ 4 files changed, 139 insertions(+), 27 deletions(-) create mode 100644 lib/msf/base/simple/framework/module_paths.rb create mode 100644 spec/lib/msf/base/simple/framework_spec.rb create mode 100644 spec/support/shared/examples/msf/simple/framework/module_paths.rb diff --git a/lib/msf/base/simple/framework.rb b/lib/msf/base/simple/framework.rb index c8ac7ab49a..88f92da9c3 100644 --- a/lib/msf/base/simple/framework.rb +++ b/lib/msf/base/simple/framework.rb @@ -1,5 +1,6 @@ # -*- coding: binary -*- require 'msf/base/simple' +require 'msf/base/simple/framework/module_paths' module Msf module Simple @@ -12,6 +13,7 @@ module Simple # ### module Framework + include Msf::Simple::Framework::ModulePaths ### # @@ -155,33 +157,6 @@ module Framework self.datastore.to_file(Msf::Config.config_file, 'framework/core') end - # - # Initialize the module paths - # - def init_module_paths - - # Ensure the module cache is accurate - self.modules.refresh_cache_from_database - - # Initialize the default module search paths - if (Msf::Config.module_directory) - self.modules.add_module_path(Msf::Config.module_directory) - end - - # Initialize the user module search path - if (Msf::Config.user_module_directory) - self.modules.add_module_path(Msf::Config.user_module_directory) - end - - # If additional module paths have been defined globally, then load them. - # They should be separated by semi-colons. - if self.datastore['MsfModulePaths'] - self.datastore['MsfModulePaths'].split(";").each { |path| - self.modules.add_module_path(path) - } - end - end - # # Statistics. # diff --git a/lib/msf/base/simple/framework/module_paths.rb b/lib/msf/base/simple/framework/module_paths.rb new file mode 100644 index 0000000000..b571459dbe --- /dev/null +++ b/lib/msf/base/simple/framework/module_paths.rb @@ -0,0 +1,33 @@ +module Msf + module Simple + module Framework + module ModulePaths + # Initialize the module paths + # + # @return [void] + def init_module_paths + # Ensure the module cache is accurate + self.modules.refresh_cache_from_database + + # Initialize the default module search paths + if (Msf::Config.module_directory) + self.modules.add_module_path(Msf::Config.module_directory) + end + + # Initialize the user module search path + if (Msf::Config.user_module_directory) + self.modules.add_module_path(Msf::Config.user_module_directory) + end + + # If additional module paths have been defined globally, then load them. + # They should be separated by semi-colons. + if self.datastore['MsfModulePaths'] + self.datastore['MsfModulePaths'].split(";").each { |path| + self.modules.add_module_path(path) + } + end + end + end + end + end +end \ No newline at end of file diff --git a/spec/lib/msf/base/simple/framework_spec.rb b/spec/lib/msf/base/simple/framework_spec.rb new file mode 100644 index 0000000000..8b37a4c48d --- /dev/null +++ b/spec/lib/msf/base/simple/framework_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe Msf::Simple::Framework do + include_context 'Msf::Simple::Framework' + + subject do + framework + end + + it_should_behave_like 'Msf::Simple::Framework::ModulePaths' +end \ No newline at end of file diff --git a/spec/support/shared/examples/msf/simple/framework/module_paths.rb b/spec/support/shared/examples/msf/simple/framework/module_paths.rb new file mode 100644 index 0000000000..c81b7825b2 --- /dev/null +++ b/spec/support/shared/examples/msf/simple/framework/module_paths.rb @@ -0,0 +1,93 @@ +shared_examples_for 'Msf::Simple::Framework::ModulePaths' do + it { should be_a Msf::Simple::Framework::ModulePaths } + + context '#init_module_paths' do + def init_module_paths + framework.init_module_paths + end + + let(:module_directory) do + nil + end + + let(:user_module_directory) do + nil + end + + before(:each) do + # create the framework first so that it's initialization's call + # to init_module_paths doesn't get captured. + framework + + Msf::Config.stub(:module_directory => module_directory) + Msf::Config.stub(:user_module_directory => user_module_directory) + end + + it 'should refresh module cache from database' do + framework.modules.should_receive(:refresh_cache_from_database) + + init_module_paths + end + + context 'Msf::Config' do + context 'module_directory' do + context 'without nil' do + let(:module_directory) do + 'modules' + end + + it 'should add Msf::Config.module_directory to module paths' do + framework.modules.should_receive(:add_module_path).with( + module_directory + ) + + init_module_paths + end + end + end + + context 'user_module_directory' do + context 'without nil' do + let(:user_module_directory) do + 'user/modules' + end + + it 'should add Msf::Config.user_module_directory to module paths' do + framework.modules.should_receive(:add_module_path).with( + user_module_directory + ) + + init_module_paths + end + end + end + end + + context 'datastore' do + context 'MsfModulePaths' do + let(:module_paths) do + module_paths = [] + + 1.upto(2) do |i| + module_paths << "msf/#{i}/modules" + end + + module_paths + end + + before(:each) do + msf_module_paths = module_paths.join(';') + framework.datastore['MsfModulePaths'] = msf_module_paths + end + + it 'should add each module path' do + module_paths.each do |module_path| + framework.modules.should_receive(:add_module_path).with(module_path) + end + + init_module_paths + end + end + end + end +end \ No newline at end of file