Module: Metasploit::Framework::Spec::Constants

Extended by:
ActiveSupport::Autoload
Defined in:
lib/metasploit/framework/spec/constants.rb

Overview

Monitor constants created by module loading to ensure that the loads in one example don’t interfere with the assertions in another example.

Defined Under Namespace

Modules: Each, Suite

Constant Summary collapse

LOADED_MODULE_CHILD_CONSTANT_REGEXP =

Regex parsing loaded module constants

/^Mod(?<unpacked_full_name>[0-9a-f]+)$/
PARENT_CONSTANT =

The parent namespace child_constant_name that can have children added when loading modules.

Msf::Modules
PERSISTENT_CHILD_CONSTANT_NAMES =

Constant names under PARENT_CONSTANT that can persist between specs because they are part of the loader library and not dynamically loaded code

%w{
  Error
  External
  Loader
  Metadata
  MetasploitClassCompatibilityError
  Namespace
  VersionCompatibilityError
}.map(&:to_sym)

Class Method Summary collapse

Class Method Details

.cleantrue, false

Cleans child constants from PARENT_CONSTANT.

Returns:

  • (true)

    if there were leaked constants that were cleaned.

  • (false)

    if there were no leaked constants.

See Also:



35
36
37
38
39
40
41
# File 'lib/metasploit/framework/spec/constants.rb', line 35

def self.clean
  count = each do |child_name|
    PARENT_CONSTANT.send(:remove_const, child_name)
  end

  count != 0
end

.define_taskvoid

This method returns an undefined value.

Adds actions to ‘spec` task so that `rake spec` fails if any of the following:

# ‘log/leaked-constants.log` exists after printing out the leaked constants. # Each.configured! is unnecessary in `spec/spec_helper.rb` and should be removed.



49
50
51
52
53
54
# File 'lib/metasploit/framework/spec/constants.rb', line 49

def self.define_task
  Suite.define_task
  # After Suite as Suite will kill for leaks before Each say it cleaned no leaks in case there are leaks in an
  # `after(:all)` that {Each} won't catch in its `after(:each)` checks.
  Each.define_task
end

.each {|child_name| ... } ⇒ Integer

Yields each child_constant_name under PARENT_CONSTANT.

Yields:

  • (child_name)

Yield Parameters:

  • child_name (Symbol)

    name of child_constant_name relative to PARENT_CONSTANT.

Yield Returns:

  • (void)

Returns:

  • (Integer)

    count



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/metasploit/framework/spec/constants.rb', line 62

def self.each
  inherit = false
  count = 0

  child_constant_names = PARENT_CONSTANT.constants(inherit)

  child_constant_names.each do |child_constant_name|
    unless PERSISTENT_CHILD_CONSTANT_NAMES.include? child_constant_name
      count += 1
      yield child_constant_name
    end
  end

  count
end

.full_name(child_constant_name) ⇒ String?

The module full name for ‘child_constant_name`

Parameters:

  • child_constant_name (String)

    the name of a child constant_name under PARENT_CONSTANT.

Returns:

  • (String)

    full module name used to load ‘child_constant_name`.

  • (nil)

    if ‘child_constant_name` does not correspond to a loaded module.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/metasploit/framework/spec/constants.rb', line 83

def self.full_name(child_constant_name)
  full_name = nil

  match = LOADED_MODULE_CHILD_CONSTANT_REGEXP.match(child_constant_name)

  if match
    potential_full_name = [match[:unpacked_full_name]].pack('H*')

    module_type, _reference_name = potential_full_name.split('/', 2)

    if Msf::MODULE_TYPES.include? module_type
      full_name = potential_full_name
    end
  end

  full_name
end