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

Detect leaked constants under Msf::Modules

MSP-11130

Detect constants leaked under Msf::Modules after the suite completes.
This commit is contained in:
Luke Imhoff 2014-10-27 11:13:43 -05:00
parent 313c2407ad
commit 605f48e58d
No known key found for this signature in database
GPG Key ID: 5B1FB01FB33356F8
4 changed files with 54 additions and 0 deletions

View File

@ -32,6 +32,10 @@ module Metasploit
# works in compatible manner with activerecord's rake tasks and other
# railties.
module Framework
extend ActiveSupport::Autoload
autoload :Spec
# Returns the root of the metasploit-framework project. Use in place of
# `Rails.root`.
#

View File

@ -0,0 +1,5 @@
module Metasploit::Framework::Spec
extend ActiveSupport::Autoload
autoload :Constants
end

View File

@ -0,0 +1,43 @@
require 'msf/core/modules'
# Monitor constants created by module loading to ensure that the loads in one example don't interfere with the
# assertions in another example.
module Metasploit::Framework::Spec::Constants
# The parent namespace constant that can have children added when loading modules.
PARENT_CONSTANT = Msf::Modules
# Configures after(:suite) callback for RSpec to check for leaked constants.
def self.configure!
unless @configured
RSpec.configure do |config|
config.after(:suite) do
::Metasploit::Framework::Spec::Constants.each { |child_name|
$stderr.puts "#{child_name} not removed from #{PARENT_CONSTANT}"
}
end
end
@configured = true
end
end
# Yields each constant under {PARENT_CONSTANT}.
#
# @yield [child_name]
# @yieldparam child_name [String] name of constant relative to {PARENT_CONSTANT}.
# @yieldreturn [void]
# @return [Integer] count
def self.each
inherit = false
count = 0
child_constant_names = PARENT_CONSTANT.constants(inherit)
child_constant_names.each do |child_constant_name|
count += 1
yield child_constant_name
end
count
end
end

View File

@ -53,3 +53,5 @@ RSpec.configure do |config|
# instead of true.
config.use_transactional_fixtures = true
end
Metasploit::Framework::Spec::Constants.configure!