1
mirror of https://github.com/rapid7/metasploit-framework synced 2024-09-11 17:08:02 +02:00

Clean up leaked constants

MSP-11130
This commit is contained in:
Luke Imhoff 2014-10-29 15:50:47 -05:00
parent 7e2897277b
commit c2bd75b587
No known key found for this signature in database
GPG Key ID: 5B1FB01FB33356F8
9 changed files with 52 additions and 7 deletions

View File

@ -11,6 +11,11 @@ module Metasploit::Framework::Spec::Constants::Each
def self.configure!
unless @configured
RSpec.configure do |config|
config.before(:each) do
# clean so that leaks from earlier example aren't attributed to this example
Metasploit::Framework::Spec::Constants.clean
end
config.after(:each) do
child_names = Metasploit::Framework::Spec::Constants.to_enum(:each).to_a
@ -26,10 +31,9 @@ module Metasploit::Framework::Spec::Constants::Each
message = lines.join("\n")
# clean so that leaks from one example aren't attributed to later examples
Metasploit::Framework::Spec::Constants.clean
fail message
# use caller metadata so that Jump to Source in the Rubymine RSpec running jumps to the example instead of
# here
fail RuntimeError, message, example.metadata[:caller]
end
end
end

View File

@ -100,6 +100,8 @@ describe Msf::Modules::Loader::Base do
context 'NAMESPACE_MODULE_CONTENT' do
context 'derived module' do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
let(:namespace_module_names) do
['Msf', 'Modules', 'Mod617578696c696172792f72737065632f6d6f636b']
end
@ -278,6 +280,8 @@ describe Msf::Modules::Loader::Base do
end
context 'with file changed' do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
let(:module_full_name) do
File.join('auxiliary', module_reference_name)
end
@ -689,6 +693,8 @@ describe Msf::Modules::Loader::Base do
end
context '#create_namespace_module' do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
let(:namespace_module_names) do
[
'Msf',
@ -778,6 +784,8 @@ describe Msf::Modules::Loader::Base do
end
context '#current_module' do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
let(:module_names) do
[
'Msf',
@ -918,6 +926,8 @@ describe Msf::Modules::Loader::Base do
end
context '#namespace_module_transaction' do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
let(:relative_name) do
'Mod617578696c696172792f72737065632f6d6f636b'
end
@ -948,7 +958,8 @@ describe Msf::Modules::Loader::Base do
end
it 'should remove the pre-existing namespace module' do
Msf::Modules.should_receive(:remove_const).with(relative_name)
expect(Msf::Modules).to receive(:remove_const).with(relative_name.to_sym).and_call_original
expect(Msf::Modules).to receive(:remove_const).with(relative_name).and_call_original
subject.send(:namespace_module_transaction, module_full_name) do |namespace_module|
true
@ -1177,6 +1188,8 @@ describe Msf::Modules::Loader::Base do
end
context 'with namespace_module nil' do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
#
# lets
#
@ -1194,7 +1207,7 @@ describe Msf::Modules::Loader::Base do
end
it 'should remove relative_name' do
parent_module.should_receive(:remove_const).with(relative_name)
expect(parent_module).to receive(:remove_const).with(relative_name).and_call_original
subject.send(:restore_namespace_module, parent_module, relative_name, namespace_module)
end
@ -1239,6 +1252,8 @@ describe Msf::Modules::Loader::Base do
end
context 'with the current constant being the namespace_module' do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
it 'should not change the constant' do
parent_module.const_defined?(relative_name).should be_truthy
@ -1254,6 +1269,9 @@ describe Msf::Modules::Loader::Base do
end
it 'should not remove the constant and then set it' do
# Allow 'Metasploit::Framework::Spec::Constants cleaner' removal
expect(parent_module).to receive(:remove_const).with(relative_name.to_sym).and_call_original
parent_module.should_not_receive(:remove_const).with(relative_name)
parent_module.should_not_receive(:const_set).with(relative_name, @current_namespace_module)
@ -1262,9 +1280,13 @@ describe Msf::Modules::Loader::Base do
end
context 'without the current constant being the namespace_module' do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
it 'should remove relative_name from parent_module' do
parent_module.const_defined?(relative_name).should be_truthy
parent_module.should_receive(:remove_const).with(relative_name)
expect(parent_module).to receive(:remove_const).with(relative_name).and_call_original
expect(parent_module).to receive(:remove_const).with(relative_name.to_sym).and_call_original
subject.send(:restore_namespace_module, parent_module, relative_name, @original_namespace_module)
end
@ -1280,6 +1302,8 @@ describe Msf::Modules::Loader::Base do
end
context 'without relative_name being a defined constant' do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
it 'should set relative_name on parent_module to namespace_module' do
parent_module.const_defined?(relative_name).should be_falsey

View File

@ -27,6 +27,8 @@ describe Msf::Modules::Loader::Directory do
context '#load_module' do
context 'with existent module_path' do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
let(:framework) do
framework = double('Msf::Framework', :datastore => {})

View File

@ -81,6 +81,8 @@ describe Msfcli do
# This one is slow because we're loading all modules
#
context ".dump_module_list" do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
it "it should dump a list of modules" do
tbl = ''
stdout = get_stdout {
@ -221,6 +223,7 @@ describe Msfcli do
end
context ".init_modules" do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
it "should inititalize an exploit module" do
args = 'exploit/windows/smb/psexec S'
@ -298,6 +301,8 @@ describe Msfcli do
end
context ".engage_mode" do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
it "should show me the summary of module auxiliary/scanner/http/http_version" do
args = 'auxiliary/scanner/http/http_version s'
stdout = get_stdout {

View File

@ -710,6 +710,8 @@ shared_examples_for 'Msf::DBManager::ModuleCache' do
end
context '#update_module_details' do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
def update_module_details
db_manager.update_module_details(module_instance)
end

View File

@ -21,6 +21,8 @@ shared_examples_for 'Msf::DBManager::Session' do
end
context 'with Msf::Session' do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
let(:exploit_datastore) do
Msf::ModuleDataStore.new(module_instance).tap do |datastore|
datastore['ParentModule'] = parent_module_fullname

View File

@ -7,6 +7,8 @@ shared_examples_for 'Msf::DBManager#update_all_module_details refresh' do
end
context 'with cached module in Msf::ModuleSet' do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
let(:module_set) do
framework.exploits
end

View File

@ -153,6 +153,8 @@ shared_examples_for 'Msf::ModuleManager::Cache' do
end
context 'with module info in cache' do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
let(:module_info_by_path) do
{
'path/to/module' => {

View File

@ -2,6 +2,8 @@ shared_examples_for 'Msf::Simple::Framework::ModulePaths' do
it { should be_a Msf::Simple::Framework::ModulePaths }
context '#init_module_paths' do
include_context 'Metasploit::Framework::Spec::Constants cleaner'
def init_module_paths
framework.init_module_paths(options)
end