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/msf/base/sessions/command_shell_spec.rb
Views: 11766
# frozen_string_literal: true12require 'spec_helper'34RSpec.describe Msf::Sessions::CommandShell do5let(:type) { 'shell' }67describe '.type' do8it 'should have the correct type' do9expect(described_class.type).to eq(type)10end11end1213describe '.can_cleanup_files' do14it 'should be able to cleanup files' do15expect(described_class.can_cleanup_files).to eq(true)16end17end1819context 'when we have a command shell session' do20subject(:command_shell) { described_class.new(nil) }21let(:command_functions) do22%i[help background sessions resource shell download upload source irb pry].map { |command| "cmd_#{command}" }23end24let(:command_help_functions) do25command_functions.map { |command| "#{command}_help" }26end27let(:description) { 'Command shell' }2829describe '#type' do30it 'should have the correct type' do31expect(subject.type).to eq(type)32end33end3435describe '#desc' do36it 'should have the correct description' do37expect(subject.desc).to eq(description)38end39end4041describe '#abort_foreground_supported' do42it 'should not support aborting the process running in the session' do43expect(subject.abort_foreground_supported).to be(true)44end45end4647describe '#shell_init' do48it 'should initialise the shell by default' do49expect(subject.shell_init).to be(true)50end51end5253describe 'Builtin commands' do54%i[background sessions resource shell download upload source irb pry].each do |command|55before(:each) do56allow(subject).to receive("cmd_#{command}_help")57end5859describe "#cmd_#{command}" do60context 'when called with the `-h` argument' do61it 'should call the corresponding help function' do62subject.send("cmd_#{command}", '-h')63expect(subject).to have_received("cmd_#{command}_help")64end65end6667context 'when called with the `--help` argument' do68it 'should call the corresponding help function' do69subject.send("cmd_#{command}", '--help')70expect(subject).to have_received("cmd_#{command}_help")71end72end73end74end75end7677describe '#run_builtin_cmd' do78%i[help background sessions resource shell download upload source irb pry].each do |command|79before(:each) do80allow(subject).to receive("cmd_#{command}")81end82context "when called with `#{command}`" do83it "should call cmd_#{command}" do84subject.run_builtin_cmd(command.to_s, nil)85expect(subject).to have_received("cmd_#{command}")86end87end88end89end9091describe '#run_single' do92before(:each) do93allow(subject).to receive(:run_builtin_cmd)94allow(subject).to receive(:shell_write)95end96%i[help background sessions resource shell download upload source irb pry].each do |command|97context "when called with builtin command `#{command}`" do98it 'should call the builtin function' do99subject.run_single(command.to_s)100expect(subject).to have_received(:run_builtin_cmd)101end102end103end104105context 'when called with a non-builtin command' do106let(:cmd) { 'some_command' }107it 'should write the command to the shell' do108subject.run_single(cmd)109expect(subject).to have_received(:shell_write).with("#{cmd}\n")110end111end112end113114describe '#process_autoruns' do115let(:initial_auto_run_script) { 'initial_auto_run_script' }116let(:auto_run_script) { 'auto_run_script' }117118before(:each) do119allow(subject).to receive(:execute_script)120end121122context 'The datastore is empty' do123let(:datastore) do124Msf::DataStore.new125end126it 'should not execute any script' do127subject.process_autoruns(datastore)128is_expected.not_to have_received(:execute_script)129end130end131132context 'The datastore contains an `InitialAutoRunScript`' do133let(:datastore) do134datastore = Msf::DataStore.new135datastore['InitialAutoRunScript'] = initial_auto_run_script136datastore137end138139it 'should execute the script' do140subject.process_autoruns(datastore)141is_expected.to have_received(:execute_script).with(initial_auto_run_script)142end143end144145context 'The datastore contains an `AutoRunScript`' do146let(:datastore) do147datastore = Msf::DataStore.new148datastore['AutoRunScript'] = auto_run_script149datastore150end151it 'should execute the script' do152subject.process_autoruns(datastore)153is_expected.to have_received(:execute_script).with(auto_run_script)154end155end156157context 'The datastore contains both `InitialAutoRunScript` and `AutoRunScript`' do158let(:datastore) do159datastore = Msf::DataStore.new160datastore['InitialAutoRunScript'] = initial_auto_run_script161datastore['AutoRunScript'] = auto_run_script162datastore163end164it 'should execute initial script before the auto run script' do165subject.process_autoruns(datastore)166is_expected.to have_received(:execute_script).ordered.with(initial_auto_run_script)167is_expected.to have_received(:execute_script).ordered.with(auto_run_script)168end169end170end171172context 'when the platform is windows' do173let(:platform) { 'windows' }174before(:each) do175subject.platform = platform176end177178it 'should not support aborting the process running in the session' do179expect(subject.abort_foreground_supported).to be(false)180end181end182end183end184185186