CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/support/acceptance/session.rb
Views: 1904
1
module Acceptance::Session
2
# @return [Symbol] The current platform
3
def self.current_platform
4
host_os = RbConfig::CONFIG['host_os']
5
case host_os
6
when /darwin/
7
:osx
8
when /mingw/
9
:windows
10
when /linux/
11
:linux
12
else
13
raise "unknown host_os #{host_os.inspect}"
14
end
15
end
16
17
# Allows restricting the tests of a specific Meterpreter's test suite with the METERPRETER environment variable
18
# @return [TrueClass, FalseClass] True if the given Meterpreter should be run, false otherwise.
19
def self.run_meterpreter?(meterpreter_config)
20
return true if ENV['SESSION'].blank?
21
22
name = meterpreter_config[:name].to_s
23
ENV['SESSION'].include?(name)
24
end
25
26
# Allows restricting the tests of a specific Meterpreter's test suite with the METERPRETER environment variable
27
# @return [TrueClass, FalseClass] True if the given Meterpreter should be run, false otherwise.
28
def self.run_meterpreter_module_test?(module_test)
29
return true if ENV['SESSION_MODULE_TEST'].blank?
30
31
ENV['SESSION_MODULE_TEST'].include?(module_test)
32
end
33
34
# Allows restricting the tests of a specific session's test suite with the SESSION environment variable
35
# @return [TrueClass, FalseClass] True if the given session should be run, false otherwise.
36
def self.run_session?(session_config)
37
return true if ENV['SESSION'].blank?
38
39
name = session_config[:name].to_s
40
ENV['SESSION'].include?("command_shell/#{name}")
41
end
42
43
# Allows restricting the tests of a specific session's test suite with the SESSION environment variable
44
# @return [TrueClass, FalseClass] True if the given session should be run, false otherwise.
45
def self.run_session_module_test?(module_test)
46
return true if ENV['SESSION_MODULE_TEST'].blank?
47
48
ENV['SESSION_MODULE_TEST'].include?(module_test)
49
end
50
51
# @param [String] string A console string with ANSI escape codes present
52
# @return [String] A string with the ANSI escape codes removed
53
def self.uncolorize(string)
54
string.gsub(/\e\[\d+m/, '')
55
end
56
57
# @param [Hash] payload_config
58
# @return [Boolean]
59
def self.supported_platform?(payload_config)
60
payload_config[:platforms].include?(current_platform)
61
end
62
63
# @param [Hash] module_test
64
# @return [Boolean]
65
def self.skipped_module_test?(module_test, test_environment)
66
current_platform_requirements = Array(module_test[:platforms].find { |platform| Array(platform)[0] == current_platform })[1] || {}
67
module_test.fetch(:skip, false) ||
68
self.eval_predicate(current_platform_requirements.fetch(:skip, false), test_environment)
69
end
70
71
# @param [Hash] payload_config
72
# @return [String] The human readable name for the given payload configuration
73
def self.human_name_for_payload(payload_config)
74
is_stageless = payload_config[:name].include?('_reverse_tcp')
75
is_staged = payload_config[:name].include?('/reverse_tcp')
76
77
details = []
78
details << 'stageless' if is_stageless
79
details << 'staged' if is_staged
80
details << payload_config[:name]
81
82
details.join(' ')
83
end
84
85
# @param [Object] hash A hash of key => hash
86
# @return [Object] Returns a new hash with the 'key' merged into hash value and all payloads
87
def self.with_session_name_merged(hash)
88
hash.each_with_object({}) do |(name, config), acc|
89
acc[name] = config.merge({ name: name })
90
end
91
end
92
93
# Evaluates a simple predicate; Similar to Msf::OptCondition.eval_condition
94
# @param [TrueClass,FalseClass,Array] value
95
# @param [Hash] environment
96
# @return [TrueClass, FalseClass] True or false
97
def self.eval_predicate(value, environment)
98
case value
99
when Array
100
left_operand, operator, right_operand = value
101
# Map values such as `:session_name` to the runtime value
102
left_operand = environment[left_operand] if environment.key?(left_operand)
103
right_operand = environment[right_operand] if environment.key?(right_operand)
104
105
case operator.to_sym
106
when :==
107
evaluate_predicate(left_operand, environment) == evaluate_predicate(right_operand, environment)
108
when :!=
109
evaluate_predicate(left_operand, environment) != evaluate_predicate(right_operand, environment)
110
when :or
111
evaluate_predicate(left_operand, environment) || evaluate_predicate(right_operand, environment)
112
else
113
raise "unexpected operator #{operator.inspect}"
114
end
115
else
116
value
117
end
118
end
119
end
120
121