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/lib/metasploit/framework/database_spec.rb
Views: 11784
require 'spec_helper'12RSpec.describe Metasploit::Framework::Database do3context 'CONSTANTS' do4context 'CONFIGURATIONS_PATHNAME_PRECEDENCE' do5subject(:configurations_pathname_precedence) {6described_class::CONFIGURATIONS_PATHNAME_PRECEDENCE7}89it { is_expected.to match_array(10[11:environment_configurations_pathname,12:user_configurations_pathname,13:project_configurations_pathname14]15) }16end17end1819context '.configurations_pathname' do20subject(:configurations_pathname) {21described_class.configurations_pathname(*arguments)22}2324context 'with options' do25let(:arguments) {26[27{28path: path29}30]31}3233context 'with :path' do34context 'that exists' do35let(:path) {36tempfile.path37}3839let(:tempfile) {40Tempfile.new(['database', '.yml'])41}4243it 'returns Pathname(path)' do44expect(configurations_pathname).to eq(Pathname.new(path))45end46end4748context 'that does not exist' do49let(:path) {50'/a/configurations/path/that/does/not/exist/database.yml'51}525354it { is_expected.to be_nil }55end56end5758context 'without :path' do59let(:path) {60''61}6263it 'calls configurations_pathnames' do64expect(described_class).to receive(:configurations_pathnames).and_call_original6566configurations_pathname67end6869it 'returns first pathname from configurations_pathnames' do70expect(configurations_pathname).to eq(described_class.configurations_pathnames.first)71end72end73end7475context 'without options' do76let(:arguments) {77[]78}7980it 'calls configurations_pathnames' do81expect(described_class).to receive(:configurations_pathnames).and_call_original8283configurations_pathname84end8586it 'returns first pathname from configurations_pathnames' do87expect(configurations_pathname).to eq(described_class.configurations_pathnames.first)88end89end90end9192context '.configurations_pathnames' do93subject(:configurations_pathnames) {94described_class.configurations_pathnames95}9697before(:example) do98allow(described_class).to receive(:environment_configurations_pathname).and_return(99environment_configurations_pathname100)101end102103context 'with environment_configurations_pathname' do104context 'that exists' do105#106# lets107#108109let(:environment_configurations_pathname) {110Pathname.new(environment_configurations_tempfile.path)111}112113let(:environment_configurations_tempfile) {114Tempfile.new(['environment_configurations', '.database.yml'])115}116117#118# Callbacks119#120121before(:example) do122allow(described_class).to receive(:user_configurations_pathname).and_return(123user_configurations_pathname124)125end126127context 'with user_configurations_pathname' do128context 'that exists' do129#130# lets131#132133let(:user_configurations_pathname) {134Pathname.new(user_configurations_tempfile.path)135}136137let(:user_configurations_tempfile) {138Tempfile.new(['user_configurations', '.database.yml'])139}140141#142# Callbacks143#144145before(:example) do146allow(described_class).to receive(:project_configurations_pathname).and_return(147project_configurations_pathname148)149end150151context 'with project_configurations_pathname' do152context 'that exists' do153let(:project_configurations_pathname) {154Pathname.new(project_configurations_tempfile.path)155}156157let(:project_configurations_tempfile) {158Tempfile.new(['project_configurations', '.database.yml'])159}160161it 'is [environment_configurations_pathname, user_configurations_pathname, project_configurations_pathname]' do162expect(project_configurations_pathname).to exist163expect(configurations_pathnames).to match_array(164[165environment_configurations_pathname,166user_configurations_pathname,167project_configurations_pathname168]169)170end171end172173context 'that does not exist' do174let(:project_configurations_pathname) {175Pathname.new('/metasploit-framework/does/not/exist/here/config/database.yml')176}177178it 'is [environment_configurations_pathname, user_configurations_pathname]' do179expect(environment_configurations_pathname).to exist180expect(user_configurations_pathname).to exist181expect(project_configurations_pathname).not_to exist182183expect(project_configurations_pathname).not_to exist184expect(configurations_pathnames).to match_array(185[186environment_configurations_pathname,187user_configurations_pathname188]189)190end191end192end193194context 'without project_configurations_pathname' do195let(:project_configurations_pathname) {196nil197}198199it 'is [environment_configuration_pathname, user_configurations_pathname]' do200expect(environment_configurations_pathname).to exist201expect(user_configurations_pathname).to exist202203expect(configurations_pathnames).to match_array(204[205environment_configurations_pathname,206user_configurations_pathname207]208)209end210end211end212213context 'with does not exist' do214#215# lets216#217218let(:user_configurations_pathname) {219Pathname.new('/user/configuration/that/does/not/exist/.msf4/database.yml')220}221222#223# Callbacks224#225226before(:example) do227allow(described_class).to receive(:project_configurations_pathname).and_return(228project_configurations_pathname229)230end231232context 'with project_configurations_pathname' do233context 'that exists' do234let(:project_configurations_pathname) {235Pathname.new(project_configurations_tempfile.path)236}237238let(:project_configurations_tempfile) {239Tempfile.new(['project_configurations', '.database.yml'])240}241242it 'is [environment_configurations_pathname, project_configurations_pathname]' do243expect(environment_configurations_pathname).to exist244expect(user_configurations_pathname).not_to exist245expect(project_configurations_pathname).to exist246247expect(configurations_pathnames).to match_array(248[249environment_configurations_pathname,250project_configurations_pathname251]252)253end254end255256context 'that does not exist' do257let(:project_configurations_pathname) {258Pathname.new('/metasploit-framework/that/does/not/exist/config/database.yml')259}260261it 'is [environment_configurations_pathname]' do262expect(environment_configurations_pathname).to exist263expect(user_configurations_pathname).not_to exist264expect(project_configurations_pathname).not_to exist265266expect(configurations_pathnames).to match_array(267[268environment_configurations_pathname269]270)271end272end273end274275context 'without project_configurations_pathname' do276let(:project_configurations_pathname) {277nil278}279280it 'is [environment_configurations_pathname]' do281expect(environment_configurations_pathname).to exist282expect(user_configurations_pathname).not_to exist283expect(project_configurations_pathname).to be_nil284285expect(configurations_pathnames).to match_array(286[287environment_configurations_pathname288]289)290end291end292end293end294295context 'without user_configurations_pathname' do296#297# lets298#299300let(:user_configurations_pathname) {301nil302}303304#305# Callbacks306#307308before(:example) do309allow(described_class).to receive(:project_configurations_pathname).and_return(310project_configurations_pathname311)312end313314context 'with project_configurations_pathname' do315316end317318context 'without project_configurations_pathname' do319let(:project_configurations_pathname) {320nil321}322323it 'contains only the environment_configuration_pathname' do324expect(configurations_pathnames).to match_array([environment_configurations_pathname])325end326end327end328end329330context 'that does not exist' do331332end333end334335context 'without environment_configurations_pathname' do336#337# lets338#339340let(:environment_configurations_pathname) {341nil342}343344#345# Callbacks346#347348before(:example) do349allow(described_class).to receive(:user_configurations_pathname).and_return(350user_configurations_pathname351)352end353354context 'with user_configurations_pathname' do355context 'that exists' do356#357# lets358#359360let(:user_configurations_pathname) {361Pathname.new(user_configurations_tempfile.path)362}363364let(:user_configurations_tempfile) {365Tempfile.new(['user_configurations', '.database.yml'])366}367368#369# Callbacks370#371372before(:example) do373allow(described_class).to receive(:project_configurations_pathname).and_return(374project_configurations_pathname375)376end377378context 'with project_configurations_pathname' do379context 'that exists' do380let(:project_configurations_pathname) {381Pathname.new(project_configurations_tempfile.path)382}383384let(:project_configurations_tempfile) {385Tempfile.new(['project_configurations', '.database.yml'])386}387388it 'is [user_configurations_pathname, project_configurations_pathname]' do389expect(environment_configurations_pathname).to be_nil390expect(user_configurations_pathname).to exist391expect(project_configurations_pathname).to exist392393expect(configurations_pathnames).to match_array(394[395user_configurations_pathname,396project_configurations_pathname397]398)399end400end401402context 'that does not exist' do403let(:project_configurations_pathname) {404Pathname.new('/metasploit-framework/that/does/not/exist/config/database.yml')405}406407it 'is [user_configurations_pathname]' do408expect(environment_configurations_pathname).to be_nil409expect(user_configurations_pathname).to exist410expect(project_configurations_pathname).not_to exist411412expect(configurations_pathnames).to match_array(413[414user_configurations_pathname415]416)417end418end419end420421context 'without project_configurations_pathname' do422let(:project_configurations_pathname) {423nil424}425426it 'is [user_configurations_pathname]' do427expect(environment_configurations_pathname).to be_nil428expect(user_configurations_pathname).to exist429expect(project_configurations_pathname).to be_nil430431expect(configurations_pathnames).to match_array(432[433user_configurations_pathname434]435)436end437end438end439440context 'that does not exist' do441#442# lets443#444445let(:user_configurations_pathname) {446Pathname.new('/user/configuration/that/does/not/exist/.msf4/database.yml')447}448449#450# Callbacks451#452453before(:example) do454allow(described_class).to receive(:project_configurations_pathname).and_return(455project_configurations_pathname456)457end458459context 'with project_configurations_pathname' do460context 'that exists' do461let(:project_configurations_pathname) {462Pathname.new(project_configurations_tempfile.path)463}464465let(:project_configurations_tempfile) {466Tempfile.new(['project_configurations', '.database.yml'])467}468469it 'is [project_configurations_pathname]' do470expect(environment_configurations_pathname).to be_nil471expect(user_configurations_pathname).not_to exist472expect(project_configurations_pathname).to exist473474expect(configurations_pathnames).to match_array(475[476project_configurations_pathname477]478)479end480end481482context 'that does not exist' do483let(:project_configurations_pathname) {484Pathname.new('/metasploit-framework/that/does/not/exist/config/database.yml')485}486487it 'is []' do488expect(environment_configurations_pathname).to be_nil489expect(user_configurations_pathname).not_to exist490expect(project_configurations_pathname).not_to exist491492expect(configurations_pathnames).to eq([])493end494end495end496497context 'without project_configurations_pathname' do498let(:project_configurations_pathname) {499nil500}501502it 'is []' do503expect(environment_configurations_pathname).to be_nil504expect(user_configurations_pathname).not_to exist505expect(project_configurations_pathname).to be_nil506507expect(configurations_pathnames).to eq([])508end509end510end511end512513context 'without user_configurations_pathname' do514#515# lets516#517518let(:user_configurations_pathname) {519nil520}521522#523# Callbacks524#525526before(:example) do527allow(described_class).to receive(:project_configurations_pathname).and_return(528project_configurations_pathname529)530end531532context 'with project_configurations_pathname' do533context 'that exists' do534let(:project_configurations_pathname) {535Pathname.new(project_configurations_tempfile.path)536}537538let(:project_configurations_tempfile) {539Tempfile.new(['project_configurations', '.database.yml'])540}541542it 'is [project_configurations_pathname]' do543expect(environment_configurations_pathname).to be_nil544expect(user_configurations_pathname).to be_nil545expect(project_configurations_pathname).to exist546547expect(configurations_pathnames).to match_array(548[549project_configurations_pathname550]551)552end553end554555context 'that does not exist' do556let(:project_configurations_pathname) {557Pathname.new('/metasploit-framework/that/does/not/exist/config/database.yml')558}559560it 'is []' do561expect(environment_configurations_pathname).to be_nil562expect(user_configurations_pathname).to be_nil563expect(project_configurations_pathname).not_to exist564565expect(configurations_pathnames).to eq([])566end567end568end569570context 'without project_configurations_pathname' do571let(:project_configurations_pathname) {572nil573}574575it { is_expected.to eq([]) }576end577end578end579end580581context '.environment_configurations_pathname' do582subject(:environment_configurations_pathname) {583described_class.environment_configurations_pathname584}585586around(:example) do |example|587env_before = ENV.to_hash588589begin590example.run591ensure592ENV.update(env_before)593end594end595596context 'with MSF_DATABASE_CONFIG' do597before(:example) do598ENV['MSF_DATABASE_CONFIG'] = msf_database_config599end600601context 'with blank' do602let(:msf_database_config) {603''604}605606it { is_expected.to be_nil }607end608609context 'without blank' do610let(:msf_database_config) {611'msf/database/config/database.yml'612}613614it 'is Pathname of MSF_DATABASE_CONFIG' do615expect(environment_configurations_pathname).to eq(Pathname.new(msf_database_config))616end617end618end619620context 'without MSF_DATABASE_CONFIG' do621before(:example) do622ENV.delete('MSF_DATABASE_CONFIG')623end624625it { is_expected.to be_nil }626end627end628629context '.project_configurations_pathname' do630subject(:project_configurations_pathname) {631described_class.project_configurations_pathname632}633634it 'is <metasploit-framework>/config/database.yml' do635root = Pathname.new(__FILE__).realpath.parent.parent.parent.parent.parent636expect(project_configurations_pathname).to eq(root.join('config', 'database.yml'))637end638end639640context '.user_configurations_pathname' do641subject(:user_configurations_pathname) {642described_class.user_configurations_pathname643}644645#646# lets647#648649let(:config_root) {650Dir.mktmpdir651}652653#654# Callbacks655#656657around(:example) do |example|658begin659example.run660ensure661FileUtils.remove_entry_secure config_root662end663end664665before(:example) do666allow(Msf::Config).to receive(:config_directory).and_return(config_root)667end668669it 'is database.yml under the user config root' do670expect(user_configurations_pathname).to eq(Pathname.new(config_root).join('database.yml'))671end672end673end674675676