CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/support/shared/contexts/untested_payloads.rb
Views: 11784
1
# Use along with `it_should_behave_like 'payload can be instantiated'` to detect if a payload under `:modules_pathname`
2
# was not tested. If any payloads are untested, an error will be written to stderr and the names of untested payloads
3
# will be logged to `log/untested-payloads.log`. This log is reset for run of context, so if there were previously
4
# untested payloads and there aren't anymore, then `log/untested-payloads.log` will be deleted. Can be used with
5
# {Metasploit::Framework::Spec::UntestedPayloads.define_task} so that the `spec` task fails if there are untested
6
# payloads.
7
#
8
# @example Using 'untested payloads' with `Metasploit::Framework::Spec::UntestedPayloads.define_task` and 'payloads can be instantiated' shared examples
9
# # Rakefile
10
# require 'metasploit/framework/spec/untested_payloads'
11
#
12
# # defined spec task with rspec-rails
13
# My::Application.load_tasks
14
# # extends spec task to fail when there are untested payloads
15
# Metasploit::Framework::Spec::UntestedPayloads.define_task
16
#
17
# # spec/modules/payloads_spec.rb
18
# require 'spec_helper'
19
#
20
# describe 'modules/payloads' do
21
# modules_pathname = Pathname.new(__FILE__).parent.parent.parent.join('modules')
22
#
23
# include_context 'untested payloads', modules_pathname: modules_pathname
24
#
25
# context 'my/staged/payload/handler' do
26
# it_should_behave_like 'payload can be instantiated',
27
# ancestor_reference_names: [
28
# 'stages/my/payload',
29
# 'stagers/my/payload/handler',
30
# modules_pathname: modules_pathname,
31
# reference_name: 'my/staged/payload/handler'
32
# ]
33
# end
34
# end
35
#
36
# @param options [Hash{Symbol => Pathname}]
37
# @option options [Pathname] :modules_pathname Pathname of `modules` directory underwhich payloads are defined on the
38
# file system.
39
RSpec.shared_context 'untested payloads' do |options={}|
40
options.assert_valid_keys(:modules_pathname)
41
42
modules_pathname = options.fetch(:modules_pathname)
43
44
before(:context) do
45
@expected_ancestor_reference_name_set = Set.new
46
@actual_ancestor_reference_name_set = Set.new
47
48
payloads_pathname = modules_pathname.join('payloads')
49
50
Dir.glob(payloads_pathname.join('**', '*.rb')) do |expected_ancestor_path|
51
expected_ancestor_pathname = Pathname.new(expected_ancestor_path)
52
expected_ancestor_reference_pathname = expected_ancestor_pathname.relative_path_from(payloads_pathname)
53
expected_ancestor_reference_name = expected_ancestor_reference_pathname.to_path.gsub(/.rb$/, '')
54
55
@expected_ancestor_reference_name_set.add(expected_ancestor_reference_name)
56
end
57
end
58
59
after(:context) do
60
missing_ancestor_reference_name_set = @expected_ancestor_reference_name_set - @actual_ancestor_reference_name_set
61
62
expect(missing_ancestor_reference_name_set).to be_empty, "Some payloads are untested: #{missing_ancestor_reference_name_set.join(', ')}. Please update `modules/payloads_spec.rb` with the payload definitions"
63
end
64
end
65
66