-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Fixes calling cleanup method when post/auxiliary modules fail #21036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EclipseAditya
wants to merge
1
commit into
rapid7:master
Choose a base branch
from
EclipseAditya:fix/18138-cleanup-called-twice
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| # -*- coding: binary -*- | ||
|
|
||
| require 'spec_helper' | ||
|
|
||
| RSpec.describe Msf::Simple::Auxiliary do | ||
| let(:cleanup_calls) { [] } | ||
|
|
||
| let(:events_double) do | ||
| double('events').tap do |e| | ||
| allow(e).to receive(:on_module_run) | ||
| allow(e).to receive(:on_module_complete) | ||
| end | ||
| end | ||
|
|
||
| let(:framework_double) do | ||
| double('framework', events: events_double) | ||
| end | ||
|
|
||
| let(:job_listener) { Msf::Simple::NoopJobListener.instance } | ||
|
|
||
| let(:mod) do | ||
| calls_ref = cleanup_calls | ||
| fw_double = framework_double | ||
|
|
||
| instance = Object.new | ||
| instance.define_singleton_method(:framework) { fw_double } | ||
| instance.define_singleton_method(:setup) {} | ||
| instance.define_singleton_method(:print_error) { |_msg| } | ||
| instance.define_singleton_method(:print_status) { |_msg| } | ||
| instance.define_singleton_method(:elog) { |_msg, **_kw| } | ||
| instance.define_singleton_method(:fail_reason) { Msf::Module::Failure::None } | ||
| instance.define_singleton_method(:fail_reason=) { |_v| } | ||
| instance.define_singleton_method(:fail_detail) { nil } | ||
| instance.define_singleton_method(:fail_detail=) { |_v| } | ||
| instance.define_singleton_method(:error=) { |_v| } | ||
| instance.define_singleton_method(:report_failure) {} | ||
| instance.define_singleton_method(:cleanup) { calls_ref << 1 } | ||
| instance | ||
| end | ||
|
|
||
| let(:run_uuid) { 'test-run-uuid-1234' } | ||
| let(:ctx) { [mod, run_uuid, job_listener] } | ||
|
|
||
| def run_proc(ctx, &block) | ||
| block ||= proc { |_m| } | ||
| described_class.send(:job_run_proc, ctx, &block) | ||
| end | ||
|
|
||
| def cleanup_proc(ctx) | ||
| described_class.send(:job_cleanup_proc, ctx) | ||
| end | ||
|
|
||
| describe '.job_run_proc' do | ||
| context 'when the module raises Auxiliary::Failed' do | ||
| it 'does not call cleanup' do | ||
| run_proc(ctx) { raise Msf::Auxiliary::Failed, 'intentional failure' } | ||
| expect(cleanup_calls.length).to eq(0) | ||
| end | ||
| end | ||
|
|
||
| context 'when the module raises Auxiliary::Complete' do | ||
| it 'does not call cleanup' do | ||
| run_proc(ctx) { raise Msf::Auxiliary::Complete } | ||
| expect(cleanup_calls.length).to eq(0) | ||
| end | ||
| end | ||
|
|
||
| context 'when the module raises Timeout::Error' do | ||
| it 'does not call cleanup' do | ||
| run_proc(ctx) { raise ::Timeout::Error } | ||
| expect(cleanup_calls.length).to eq(0) | ||
| end | ||
| end | ||
|
|
||
| context 'when the module raises a generic Exception' do | ||
| it 'does not call cleanup' do | ||
| run_proc(ctx) { raise 'unexpected error' } | ||
| expect(cleanup_calls.length).to eq(0) | ||
| end | ||
| end | ||
|
|
||
| context 'when the module completes normally' do | ||
| it 'does not call cleanup' do | ||
| run_proc(ctx) { nil } | ||
| expect(cleanup_calls.length).to eq(0) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe '.job_cleanup_proc' do | ||
| it 'calls cleanup exactly once' do | ||
| cleanup_proc(ctx) | ||
| expect(cleanup_calls.length).to eq(1) | ||
| end | ||
| end | ||
|
|
||
| describe 'cleanup is called exactly once across job_run_proc + job_cleanup_proc' do | ||
| def run_then_cleanup(ctx, &block) | ||
| block ||= proc { |_m| } | ||
| run_proc(ctx, &block) | ||
| cleanup_proc(ctx) | ||
| end | ||
|
|
||
| context 'when the module raises Auxiliary::Failed' do | ||
| it 'calls cleanup exactly once total' do | ||
| run_then_cleanup(ctx) { raise Msf::Auxiliary::Failed, 'intentional failure' } | ||
| expect(cleanup_calls.length).to eq(1) | ||
| end | ||
| end | ||
|
|
||
| context 'when the module raises Auxiliary::Complete' do | ||
| it 'calls cleanup exactly once total' do | ||
| run_then_cleanup(ctx) { raise Msf::Auxiliary::Complete } | ||
| expect(cleanup_calls.length).to eq(1) | ||
| end | ||
| end | ||
|
|
||
| context 'when the module raises Timeout::Error' do | ||
| it 'calls cleanup exactly once total' do | ||
| run_then_cleanup(ctx) { raise ::Timeout::Error } | ||
| expect(cleanup_calls.length).to eq(1) | ||
| end | ||
| end | ||
|
|
||
| context 'when the module raises a generic Exception' do | ||
| it 'calls cleanup exactly once total' do | ||
| run_then_cleanup(ctx) { raise 'unexpected error' } | ||
| expect(cleanup_calls.length).to eq(1) | ||
| end | ||
| end | ||
|
|
||
| context 'when the module completes normally' do | ||
| it 'calls cleanup exactly once total' do | ||
| run_then_cleanup(ctx) { nil } | ||
| expect(cleanup_calls.length).to eq(1) | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.