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