Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/metasploit/framework/spec/constants.rb
Views: 11784
1# Monitor constants created by module loading to ensure that the loads in one example don't interfere with the2# assertions in another example.3module Metasploit::Framework::Spec::Constants4extend ActiveSupport::Autoload56autoload :Each7autoload :Suite89#10# CONSTANTS11#1213# Regex parsing loaded module constants14LOADED_MODULE_CHILD_CONSTANT_REGEXP = /^Mod(?<unpacked_full_name>[0-9a-f]+)$/15# The parent namespace child_constant_name that can have children added when loading modules.16PARENT_CONSTANT = Msf::Modules17# Constant names under {PARENT_CONSTANT} that can persist between specs because they are part of the loader library18# and not dynamically loaded code19PERSISTENT_CHILD_CONSTANT_NAMES = %w{20Error21External22Loader23Metadata24MetasploitClassCompatibilityError25Namespace26VersionCompatibilityError27}.map(&:to_sym)2829# Cleans child constants from {PARENT_CONSTANT}.30#31# @return [true] if there were leaked constants that were cleaned.32# @return [false] if there were no leaked constants.33# @see each34def self.clean35count = each do |child_name|36PARENT_CONSTANT.send(:remove_const, child_name)37end3839count != 040end4142# Adds actions to `spec` task so that `rake spec` fails if any of the following:43#44# # `log/leaked-constants.log` exists after printing out the leaked constants.45# # Each.configured! is unnecessary in `spec/spec_helper.rb` and should be removed.46#47# @return [void]48def self.define_task49Suite.define_task50# After Suite as Suite will kill for leaks before Each say it cleaned no leaks in case there are leaks in an51# `after(:all)` that {Each} won't catch in its `after(:each)` checks.52Each.define_task53end5455# Yields each child_constant_name under {PARENT_CONSTANT}.56#57# @yield [child_name]58# @yieldparam child_name [Symbol] name of child_constant_name relative to {PARENT_CONSTANT}.59# @yieldreturn [void]60# @return [Integer] count61def self.each62inherit = false63count = 06465child_constant_names = PARENT_CONSTANT.constants(inherit)6667child_constant_names.each do |child_constant_name|68unless PERSISTENT_CHILD_CONSTANT_NAMES.include? child_constant_name69count += 170yield child_constant_name71end72end7374count75end7677# The module full name for `child_constant_name`78#79# @param child_constant_name [String] the name of a child constant_name under {PARENT_CONSTANT}.80# @return [String] full module name used to load `child_constant_name`.81# @return [nil] if `child_constant_name` does not correspond to a loaded module.82def self.full_name(child_constant_name)83full_name = nil8485match = LOADED_MODULE_CHILD_CONSTANT_REGEXP.match(child_constant_name)8687if match88potential_full_name = [match[:unpacked_full_name]].pack('H*')8990module_type, _reference_name = potential_full_name.split('/', 2)9192if Msf::MODULE_TYPES.include? module_type93full_name = potential_full_name94end95end9697full_name98end99end100101102