|
| 1 | +# -*- coding: binary -*- |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +RSpec.describe Msf::Simple::Auxiliary do |
| 6 | + let(:cleanup_calls) { [] } |
| 7 | + |
| 8 | + let(:events_double) do |
| 9 | + double('events').tap do |e| |
| 10 | + allow(e).to receive(:on_module_run) |
| 11 | + allow(e).to receive(:on_module_complete) |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + let(:framework_double) do |
| 16 | + double('framework', events: events_double) |
| 17 | + end |
| 18 | + |
| 19 | + let(:job_listener) { Msf::Simple::NoopJobListener.instance } |
| 20 | + |
| 21 | + let(:mod) do |
| 22 | + calls_ref = cleanup_calls |
| 23 | + fw_double = framework_double |
| 24 | + |
| 25 | + instance = Object.new |
| 26 | + instance.define_singleton_method(:framework) { fw_double } |
| 27 | + instance.define_singleton_method(:setup) {} |
| 28 | + instance.define_singleton_method(:print_error) { |_msg| } |
| 29 | + instance.define_singleton_method(:print_status) { |_msg| } |
| 30 | + instance.define_singleton_method(:elog) { |_msg, **_kw| } |
| 31 | + instance.define_singleton_method(:fail_reason) { Msf::Module::Failure::None } |
| 32 | + instance.define_singleton_method(:fail_reason=) { |_v| } |
| 33 | + instance.define_singleton_method(:fail_detail) { nil } |
| 34 | + instance.define_singleton_method(:fail_detail=) { |_v| } |
| 35 | + instance.define_singleton_method(:error=) { |_v| } |
| 36 | + instance.define_singleton_method(:report_failure) {} |
| 37 | + instance.define_singleton_method(:cleanup) { calls_ref << 1 } |
| 38 | + instance |
| 39 | + end |
| 40 | + |
| 41 | + let(:run_uuid) { 'test-run-uuid-1234' } |
| 42 | + let(:ctx) { [mod, run_uuid, job_listener] } |
| 43 | + |
| 44 | + def run_proc(ctx, &block) |
| 45 | + block ||= proc { |_m| } |
| 46 | + described_class.send(:job_run_proc, ctx, &block) |
| 47 | + end |
| 48 | + |
| 49 | + def cleanup_proc(ctx) |
| 50 | + described_class.send(:job_cleanup_proc, ctx) |
| 51 | + end |
| 52 | + |
| 53 | + describe '.job_run_proc' do |
| 54 | + context 'when the module raises Auxiliary::Failed' do |
| 55 | + it 'does not call cleanup' do |
| 56 | + run_proc(ctx) { raise Msf::Auxiliary::Failed, 'intentional failure' } |
| 57 | + expect(cleanup_calls.length).to eq(0) |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + context 'when the module raises Auxiliary::Complete' do |
| 62 | + it 'does not call cleanup' do |
| 63 | + run_proc(ctx) { raise Msf::Auxiliary::Complete } |
| 64 | + expect(cleanup_calls.length).to eq(0) |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + context 'when the module raises Timeout::Error' do |
| 69 | + it 'does not call cleanup' do |
| 70 | + run_proc(ctx) { raise ::Timeout::Error } |
| 71 | + expect(cleanup_calls.length).to eq(0) |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + context 'when the module raises a generic Exception' do |
| 76 | + it 'does not call cleanup' do |
| 77 | + run_proc(ctx) { raise 'unexpected error' } |
| 78 | + expect(cleanup_calls.length).to eq(0) |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + context 'when the module completes normally' do |
| 83 | + it 'does not call cleanup' do |
| 84 | + run_proc(ctx) { nil } |
| 85 | + expect(cleanup_calls.length).to eq(0) |
| 86 | + end |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + describe '.job_cleanup_proc' do |
| 91 | + it 'calls cleanup exactly once' do |
| 92 | + cleanup_proc(ctx) |
| 93 | + expect(cleanup_calls.length).to eq(1) |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + describe 'cleanup is called exactly once across job_run_proc + job_cleanup_proc' do |
| 98 | + def run_then_cleanup(ctx, &block) |
| 99 | + block ||= proc { |_m| } |
| 100 | + run_proc(ctx, &block) |
| 101 | + cleanup_proc(ctx) |
| 102 | + end |
| 103 | + |
| 104 | + context 'when the module raises Auxiliary::Failed' do |
| 105 | + it 'calls cleanup exactly once total' do |
| 106 | + run_then_cleanup(ctx) { raise Msf::Auxiliary::Failed, 'intentional failure' } |
| 107 | + expect(cleanup_calls.length).to eq(1) |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + context 'when the module raises Auxiliary::Complete' do |
| 112 | + it 'calls cleanup exactly once total' do |
| 113 | + run_then_cleanup(ctx) { raise Msf::Auxiliary::Complete } |
| 114 | + expect(cleanup_calls.length).to eq(1) |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + context 'when the module raises Timeout::Error' do |
| 119 | + it 'calls cleanup exactly once total' do |
| 120 | + run_then_cleanup(ctx) { raise ::Timeout::Error } |
| 121 | + expect(cleanup_calls.length).to eq(1) |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + context 'when the module raises a generic Exception' do |
| 126 | + it 'calls cleanup exactly once total' do |
| 127 | + run_then_cleanup(ctx) { raise 'unexpected error' } |
| 128 | + expect(cleanup_calls.length).to eq(1) |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + context 'when the module completes normally' do |
| 133 | + it 'calls cleanup exactly once total' do |
| 134 | + run_then_cleanup(ctx) { nil } |
| 135 | + expect(cleanup_calls.length).to eq(1) |
| 136 | + end |
| 137 | + end |
| 138 | + end |
| 139 | +end |
0 commit comments