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/spec/spec_helper.rb
Views: 11704
# -*- coding: binary -*-12# Enable legacy providers such as blowfish-cbc, cast128-cbc, arcfour, etc3$stderr.puts "Overriding user environment variable 'OPENSSL_CONF' to enable legacy functions." unless ENV['OPENSSL_CONF'].nil?4ENV['OPENSSL_CONF'] = File.expand_path(5File.join(File.dirname(__FILE__), '..', 'config', 'openssl.conf')6)78require 'stringio'9require 'factory_bot'10require 'rubocop'11require 'rubocop/rspec/support'12require 'faker'1314ENV['RAILS_ENV'] = 'test'1516load_metasploit = ENV.fetch('SPEC_HELPER_LOAD_METASPLOIT', 'true') == 'true'1718if load_metasploit19# @note must be before loading config/environment because railtie needs to be loaded before20# `Metasploit::Framework::Application.initialize!` is called.21#22# Must be explicit as activerecord is optional dependency23require 'active_record/railtie'24require 'metasploit/framework/database'25# check if database.yml is present26unless Metasploit::Framework::Database.configurations_pathname.try(:to_path)27fail 'RSPEC currently needs a configured database'28end2930require File.expand_path('../../config/environment', __FILE__)3132# Don't `require 'rspec/rails'` as it includes support for pieces of rails that metasploit-framework doesn't use33require 'rspec/rails'3435require 'metasploit/framework/spec'3637FILE_FIXTURES_PATH = File.expand_path(File.dirname(__FILE__)) + '/file_fixtures/'3839# Load the shared examples from the following engines40engines = [41Metasploit::Concern,42Rails43]4445# Requires supporting ruby files with custom matchers and macros, etc,46# in spec/support/ and its subdirectories.47engines.each do |engine|48support_glob = engine.root.join('spec', 'support', '**', '*.rb')49Dir[support_glob].each { |f|50require f51}52end5354# Fail the test suite if the test environment database has not been migrated55migration_manager = Class.new.extend(Msf::DBManager::Migration)56fail "Run `RAILS_ENV=test rake db:migrate` before running tests" if migration_manager.needs_migration?57end5859RSpec.configure do |config|60config.raise_errors_for_deprecations!61config.include RuboCop::RSpec::ExpectOffense62config.expose_dsl_globally = false6364# Don't run Acceptance tests by default65config.define_derived_metadata(file_path: %r{spec/acceptance/}) do |metadata|66metadata[:acceptance] ||= true67end68config.filter_run_excluding({ acceptance: true })6970# These two settings work together to allow you to limit a spec run71# to individual examples or groups you care about by tagging them with72# `:focus` metadata. When nothing is tagged with `:focus`, all examples73# get run.74if ENV['CI']75config.before(:example, :focus) { raise "Should not commit focused specs" }76else77config.filter_run focus: true78config.run_all_when_everything_filtered = true79end8081# allow more verbose output when running an individual spec file.82if config.files_to_run.one?83# RSpec filters the backtrace by default so as not to be so noisy.84# This causes the full backtrace to be printed when running a single85# spec file (e.g. to troubleshoot a particular spec failure).86config.full_backtrace = true87end8889# Print the 10 slowest examples and example groups at the90# end of the spec run, to help surface which specs are running91# particularly slow.92config.profile_examples = 109394# Run specs in random order to surface order dependencies. If you find an95# order dependency and want to debug it, you can fix the order by providing96# the seed, which is printed after each run.97# --seed 123498config.order = :random99100if load_metasploit101config.use_transactional_fixtures = true102103# rspec-rails 3 will no longer automatically infer an example group's spec type104# from the file location. You can explicitly opt-in to the feature using this105# config option.106# To explicitly tag specs without using automatic inference, set the `:type`107# metadata manually:108#109# describe ThingsController, :type => :controller do110# # Equivalent to being in spec/controllers111# end112config.infer_spec_type_from_file_location!113end114115# Seed global randomization in this process using the `--seed` CLI option.116# Setting this allows you to use `--seed` to deterministically reproduce117# test failures related to randomization by passing the same `--seed` value118# as the one that triggered the failure.119Kernel.srand config.seed120121# Implemented to avoid regression issue with code calling Faker not being deterministic122# https://github.com/faker-ruby/faker/issues/2281123Faker::Config.random = Random.new(config.seed)124125config.expect_with :rspec do |expectations|126# Enable only the newer, non-monkey-patching expect syntax.127expectations.syntax = :expect128end129130# rspec-mocks config goes here. You can use an alternate test double131# library (such as bogus or mocha) by changing the `mock_with` option here.132config.mock_with :rspec do |mocks|133# Enable only the newer, non-monkey-patching expect syntax.134# For more details, see:135# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/136mocks.syntax = :expect137138mocks.patch_marshal_to_support_partial_doubles = false139140# Prevents you from mocking or stubbing a method that does not exist on141# a real object.142mocks.verify_partial_doubles = true143end144145if ENV['REMOTE_DB']146require 'metasploit/framework/data_service/remote/managed_remote_data_service'147opts = {}148opts[:process_name] = File.join('tools', 'dev', 'msfdb_ws')149opts[:host] = 'localhost'150opts[:port] = '8080'151152config.before(:suite) do153Metasploit::Framework::DataService::ManagedRemoteDataService.instance.start(opts)154end155156config.after(:suite) do157Metasploit::Framework::DataService::ManagedRemoteDataService.instance.stop158end159end160161if ENV['MSF_FEATURE_DATASTORE_FALLBACKS']162config.before(:suite) do163Msf::FeatureManager.instance.set(Msf::FeatureManager::DATASTORE_FALLBACKS, true)164end165end166167if ENV['MSF_FEATURE_DEFER_MODULE_LOADS']168config.before(:suite) do169Msf::FeatureManager.instance.set(Msf::FeatureManager::DEFER_MODULE_LOADS, true)170end171end172173# rex-text table performs word wrapping on msfconsole tables:174# https://github.com/rapid7/rex-text/blob/11e59416f7d8cce18b8b8b9893b3277e6ad0bea1/lib/rex/text/wrapped_table.rb#L74175# This can cause some integration tests to fail if the tests are run from smaller consoles176# This mock will ensure that the tests run without word-wrapping.177require 'bigdecimal'178config.before(:each) do179mock_io_console = double(:console, winsize: { rows: 30, columns: ::BigDecimal::INFINITY }.values)180allow(::IO).to receive(:console).and_return(mock_io_console)181end182end183184if load_metasploit185Metasploit::Framework::Spec::Constants::Suite.configure!186Metasploit::Framework::Spec::Threads::Suite.configure!187end188189def get_stdout(&block)190out = $stdout191$stdout = tmp = StringIO.new192begin193yield194ensure195$stdout = out196end197tmp.string198end199200def get_stderr(&block)201out = $stderr202$stderr = tmp = StringIO.new203begin204yield205ensure206$stderr = out207end208tmp.string209end210211212