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/msfupdate_spec.rb
Views: 11704
require 'spec_helper'12load Metasploit::Framework.root.join('msfupdate').to_path34RSpec.describe Msfupdate do56def dummy_pathname7Pathname.new(File.dirname(__FILE__)).join('dummy')8end910def dummy_git_pathname11dummy_pathname.join('gitbase')12end1314def dummy_install_pathname15dummy_pathname.join('installbase').join('msf3')16end1718def dummy_apt_pathname19dummy_pathname.join('aptbase')20end2122let(:msfbase_dir) do23dummy_git_pathname24end2526let(:stdin) { StringIO.new("", "rb") }27let(:stdout) { StringIO.new("", "wb") }28let(:stderr) { StringIO.new("", "wb") }2930subject do31Msfupdate.new(msfbase_dir, stdin, stdout, stderr)32end3334before(:context) do35# Create some fake directories to mock our different install environments36dummy_pathname.mkpath37dummy_apt_pathname.join('.apt').mkpath38dummy_git_pathname.join('.git').mkpath39dummy_install_pathname.mkpath40dummy_install_pathname.join('..', 'engine').mkpath41FileUtils.touch(dummy_install_pathname.join('..', 'engine', 'update.rb'))42end4344after(:context) do45dummy_pathname.rmtree46end4748before(:example) do49# By default, we want to ensure tests never actually try to execute any50# of the update methods unless we are explicitly testing them51allow(subject).to receive(:update_binary_install!)52allow(subject).to receive(:update_git!)53end5455context "#parse_args" do56it "doesn't alter ARGV" do57ARGV.clear58ARGV << 'foo'59subject.parse_args(['x', 'y'])60expect(ARGV).to eq ['foo']61end6263context "with --help" do64let(:args) { ['--help'] }6566it "calls usage" do67expect(subject).to receive(:usage)68begin69subject.parse_args(args)70rescue SystemExit71end72end7374it "exits before updating" do75expect {subject.parse_args(args)}.to raise_error(SystemExit)76end77end7879context "with --git-branch" do80let(:git_branch) { 'foo' }81let(:args) { ['--git-branch', git_branch] }8283it "sets @git_branch" do84subject.parse_args(args)85expect(subject.instance_variable_get(:@git_branch)).to eq git_branch86end8788context "without a space" do89let(:args) { ["--git-branch=#{git_branch}"] }9091it "sets @git_branch" do92subject.parse_args(args)93expect(subject.instance_variable_get(:@git_branch)).to eq git_branch94end95end96end9798context "with --git-remote" do99let(:git_remote) { 'foo' }100let(:args) { ['--git-remote', git_remote] }101102it "sets @git_remote" do103subject.parse_args(args)104expect(subject.instance_variable_get(:@git_remote)).to eq git_remote105end106107context "without a space" do108let(:args) { ["--git-remote=#{git_remote}"] }109110it "sets @git_remote" do111subject.parse_args(args)112expect(subject.instance_variable_get(:@git_remote)).to eq git_remote113end114end115end116117context "with --offline-file" do118let(:offline_file) { 'foo' }119let(:args) { ['--offline-file', offline_file] }120121it "sets @offline_file" do122subject.parse_args(args)123expect(subject.instance_variable_get(:@offline_file)).to match Regexp.new(Regexp.escape(offline_file))124end125126context "with relative path" do127it "transforms argument into an absolute path" do128subject.parse_args(args)129expect(subject.instance_variable_get(:@offline_file)).to eq File.join(Dir.pwd, offline_file)130end131end132133context "with absolute path" do134let(:offline_file) { '/tmp/foo' }135it "accepts an absolute path" do136subject.parse_args(args)137expect(subject.instance_variable_get(:@offline_file)).to eq offline_file138end139end140141context "without a space" do142let(:args) { ["--offline-file=#{offline_file}"] }143144it "sets @offline_file" do145subject.parse_args(["--offline-file=#{offline_file}"])146expect(subject.instance_variable_get(:@offline_file)).to match Regexp.new(Regexp.escape(offline_file))147end148end149end150151context "with wait" do152let(:args) { ['wait'] }153it "sets @actually_wait" do154subject.parse_args(args)155expect(subject.instance_variable_get(:@actually_wait)).to eq true156end157end158159context "with nowait" do160let(:args) { ['nowait'] }161it "sets @actually_wait" do162subject.parse_args(args)163expect(subject.instance_variable_get(:@actually_wait)).to eq false164end165end166end167168context "#run!" do169before(:example) do170subject.parse_args(args)171end172let(:args) { [] }173174it "calls validate_args" do175expect(subject).to receive(:validate_args) { true }176subject.run!177end178179it "exits if arguments are invalid" do180allow(subject).to receive(:validate_args).and_return(false)181expect(subject).to receive(:maybe_wait_and_exit).and_raise(SystemExit)182expect { subject.run! }.to raise_error(SystemExit)183end184end185186context "in an apt installation" do187let(:msfbase_dir) { dummy_apt_pathname }188189it { expect(subject.apt?).to be_truthy }190it { expect(subject.binary_install?).to be_falsey }191it { expect(subject.git?).to be_falsey }192193context "#validate_args" do194before(:example) do195subject.parse_args(args)196end197198context "with no args" do199let(:args) { [] }200it { expect(subject.validate_args).to be_truthy }201end202203context "with --git-remote" do204let(:args) { ['--git-remote', 'foo'] }205it { expect(subject.validate_args).to be_falsey }206end207208context "with --git-branch" do209let(:args) { ['--git-branch', 'foo'] }210it { expect(subject.validate_args).to be_falsey }211end212213context "with --offline-file" do214let(:args) { ['--offline-file', 'foo'] }215it { expect(subject.validate_args).to be_falsey }216end217end218219context "#run!" do220it "does not call update_binary_install!" do221expect(subject).not_to receive(:update_binary_install!)222subject.run!223end224it "does not call update_git!" do225expect(subject).not_to receive(:update_git!)226subject.run!227end228end229end230231context "in a binary installation" do232let(:msfbase_dir) { dummy_install_pathname }233234it { expect(subject.apt?).to be_falsey }235it { expect(subject.binary_install?).to be_truthy }236it { expect(subject.git?).to be_falsey }237238context "#validate_args" do239before(:example) do240subject.parse_args(args)241end242243context "with no args" do244let(:args) { [] }245it { expect(subject.validate_args).to be_truthy }246end247248context "with --git-remote" do249let(:args) { ['--git-remote', 'foo'] }250it { expect(subject.validate_args).to be_falsey }251end252253context "with --git-branch" do254let(:args) { ['--git-branch', 'foo'] }255it { expect(subject.validate_args).to be_falsey }256end257258context "with --offline-file" do259let(:args) { ['--offline-file', 'foo'] }260it { expect(subject.validate_args).to be_truthy }261end262end263264context "#run!" do265it "calls update_binary_install!" do266expect(subject).to receive(:update_binary_install!)267subject.run!268end269it "does not call update_git!" do270expect(subject).not_to receive(:update_git!)271subject.run!272end273end274275context "#update_binary_install!" do276# TODO: Add more tests!277end278end279280context "in a git installation" do281let(:msfbase_dir) { dummy_git_pathname }282283it { expect(subject.apt?).to be_falsey }284it { expect(subject.binary_install?).to be_falsey }285it { expect(subject.git?).to be_truthy }286287288context "#validate_args" do289before(:example) do290subject.parse_args(args)291end292293context "with no args" do294let(:args) { [] }295it { expect(subject.validate_args).to be_truthy }296end297298context "with --git-remote" do299let(:args) { ['--git-remote', 'foo'] }300it { expect(subject.validate_args).to be_truthy }301end302303context "with --git-branch" do304let(:args) { ['--git-branch', 'foo'] }305it { expect(subject.validate_args).to be_truthy }306end307308context "with --offline-file" do309let(:args) { ['--offline-file', 'foo'] }310it { expect(subject.validate_args).to be_falsey }311end312end313314context "#run!" do315it "does not call update_binary_install!" do316expect(subject).not_to receive(:update_binary_install!)317subject.run!318end319it "calls update_git!" do320expect(subject).to receive(:update_git!)321subject.run!322end323end324325context "#update_git!" do326# TODO: Add more tests!327end328end329330end331332333