CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/spec_helper.rb
Views: 11704
1
# -*- coding: binary -*-
2
3
# Enable legacy providers such as blowfish-cbc, cast128-cbc, arcfour, etc
4
$stderr.puts "Overriding user environment variable 'OPENSSL_CONF' to enable legacy functions." unless ENV['OPENSSL_CONF'].nil?
5
ENV['OPENSSL_CONF'] = File.expand_path(
6
File.join(File.dirname(__FILE__), '..', 'config', 'openssl.conf')
7
)
8
9
require 'stringio'
10
require 'factory_bot'
11
require 'rubocop'
12
require 'rubocop/rspec/support'
13
require 'faker'
14
15
ENV['RAILS_ENV'] = 'test'
16
17
load_metasploit = ENV.fetch('SPEC_HELPER_LOAD_METASPLOIT', 'true') == 'true'
18
19
if load_metasploit
20
# @note must be before loading config/environment because railtie needs to be loaded before
21
# `Metasploit::Framework::Application.initialize!` is called.
22
#
23
# Must be explicit as activerecord is optional dependency
24
require 'active_record/railtie'
25
require 'metasploit/framework/database'
26
# check if database.yml is present
27
unless Metasploit::Framework::Database.configurations_pathname.try(:to_path)
28
fail 'RSPEC currently needs a configured database'
29
end
30
31
require File.expand_path('../../config/environment', __FILE__)
32
33
# Don't `require 'rspec/rails'` as it includes support for pieces of rails that metasploit-framework doesn't use
34
require 'rspec/rails'
35
36
require 'metasploit/framework/spec'
37
38
FILE_FIXTURES_PATH = File.expand_path(File.dirname(__FILE__)) + '/file_fixtures/'
39
40
# Load the shared examples from the following engines
41
engines = [
42
Metasploit::Concern,
43
Rails
44
]
45
46
# Requires supporting ruby files with custom matchers and macros, etc,
47
# in spec/support/ and its subdirectories.
48
engines.each do |engine|
49
support_glob = engine.root.join('spec', 'support', '**', '*.rb')
50
Dir[support_glob].each { |f|
51
require f
52
}
53
end
54
55
# Fail the test suite if the test environment database has not been migrated
56
migration_manager = Class.new.extend(Msf::DBManager::Migration)
57
fail "Run `RAILS_ENV=test rake db:migrate` before running tests" if migration_manager.needs_migration?
58
end
59
60
RSpec.configure do |config|
61
config.raise_errors_for_deprecations!
62
config.include RuboCop::RSpec::ExpectOffense
63
config.expose_dsl_globally = false
64
65
# Don't run Acceptance tests by default
66
config.define_derived_metadata(file_path: %r{spec/acceptance/}) do |metadata|
67
metadata[:acceptance] ||= true
68
end
69
config.filter_run_excluding({ acceptance: true })
70
71
# These two settings work together to allow you to limit a spec run
72
# to individual examples or groups you care about by tagging them with
73
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
74
# get run.
75
if ENV['CI']
76
config.before(:example, :focus) { raise "Should not commit focused specs" }
77
else
78
config.filter_run focus: true
79
config.run_all_when_everything_filtered = true
80
end
81
82
# allow more verbose output when running an individual spec file.
83
if config.files_to_run.one?
84
# RSpec filters the backtrace by default so as not to be so noisy.
85
# This causes the full backtrace to be printed when running a single
86
# spec file (e.g. to troubleshoot a particular spec failure).
87
config.full_backtrace = true
88
end
89
90
# Print the 10 slowest examples and example groups at the
91
# end of the spec run, to help surface which specs are running
92
# particularly slow.
93
config.profile_examples = 10
94
95
# Run specs in random order to surface order dependencies. If you find an
96
# order dependency and want to debug it, you can fix the order by providing
97
# the seed, which is printed after each run.
98
# --seed 1234
99
config.order = :random
100
101
if load_metasploit
102
config.use_transactional_fixtures = true
103
104
# rspec-rails 3 will no longer automatically infer an example group's spec type
105
# from the file location. You can explicitly opt-in to the feature using this
106
# config option.
107
# To explicitly tag specs without using automatic inference, set the `:type`
108
# metadata manually:
109
#
110
# describe ThingsController, :type => :controller do
111
# # Equivalent to being in spec/controllers
112
# end
113
config.infer_spec_type_from_file_location!
114
end
115
116
# Seed global randomization in this process using the `--seed` CLI option.
117
# Setting this allows you to use `--seed` to deterministically reproduce
118
# test failures related to randomization by passing the same `--seed` value
119
# as the one that triggered the failure.
120
Kernel.srand config.seed
121
122
# Implemented to avoid regression issue with code calling Faker not being deterministic
123
# https://github.com/faker-ruby/faker/issues/2281
124
Faker::Config.random = Random.new(config.seed)
125
126
config.expect_with :rspec do |expectations|
127
# Enable only the newer, non-monkey-patching expect syntax.
128
expectations.syntax = :expect
129
end
130
131
# rspec-mocks config goes here. You can use an alternate test double
132
# library (such as bogus or mocha) by changing the `mock_with` option here.
133
config.mock_with :rspec do |mocks|
134
# Enable only the newer, non-monkey-patching expect syntax.
135
# For more details, see:
136
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
137
mocks.syntax = :expect
138
139
mocks.patch_marshal_to_support_partial_doubles = false
140
141
# Prevents you from mocking or stubbing a method that does not exist on
142
# a real object.
143
mocks.verify_partial_doubles = true
144
end
145
146
if ENV['REMOTE_DB']
147
require 'metasploit/framework/data_service/remote/managed_remote_data_service'
148
opts = {}
149
opts[:process_name] = File.join('tools', 'dev', 'msfdb_ws')
150
opts[:host] = 'localhost'
151
opts[:port] = '8080'
152
153
config.before(:suite) do
154
Metasploit::Framework::DataService::ManagedRemoteDataService.instance.start(opts)
155
end
156
157
config.after(:suite) do
158
Metasploit::Framework::DataService::ManagedRemoteDataService.instance.stop
159
end
160
end
161
162
if ENV['MSF_FEATURE_DATASTORE_FALLBACKS']
163
config.before(:suite) do
164
Msf::FeatureManager.instance.set(Msf::FeatureManager::DATASTORE_FALLBACKS, true)
165
end
166
end
167
168
if ENV['MSF_FEATURE_DEFER_MODULE_LOADS']
169
config.before(:suite) do
170
Msf::FeatureManager.instance.set(Msf::FeatureManager::DEFER_MODULE_LOADS, true)
171
end
172
end
173
174
# rex-text table performs word wrapping on msfconsole tables:
175
# https://github.com/rapid7/rex-text/blob/11e59416f7d8cce18b8b8b9893b3277e6ad0bea1/lib/rex/text/wrapped_table.rb#L74
176
# This can cause some integration tests to fail if the tests are run from smaller consoles
177
# This mock will ensure that the tests run without word-wrapping.
178
require 'bigdecimal'
179
config.before(:each) do
180
mock_io_console = double(:console, winsize: { rows: 30, columns: ::BigDecimal::INFINITY }.values)
181
allow(::IO).to receive(:console).and_return(mock_io_console)
182
end
183
end
184
185
if load_metasploit
186
Metasploit::Framework::Spec::Constants::Suite.configure!
187
Metasploit::Framework::Spec::Threads::Suite.configure!
188
end
189
190
def get_stdout(&block)
191
out = $stdout
192
$stdout = tmp = StringIO.new
193
begin
194
yield
195
ensure
196
$stdout = out
197
end
198
tmp.string
199
end
200
201
def get_stderr(&block)
202
out = $stderr
203
$stderr = tmp = StringIO.new
204
begin
205
yield
206
ensure
207
$stderr = out
208
end
209
tmp.string
210
end
211
212