Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/spec/support/module_spec_helper.rb
4598 views
1
#
2
# Copyright (c) 2006-2026 Wade Alcorn - [email protected]
3
# Browser Exploitation Framework (BeEF) - https://beefproject.com
4
# See the file 'doc/COPYING' for copying permission
5
#
6
# Helpers for unit specs of command modules (modules/*/module.rb).
7
# Use with stubbed config; no REST/API or real server.
8
#
9
10
module ModuleSpecHelper
11
#
12
# Build a command instance for testing post_execute (or other instance methods).
13
# Uses allocate so we avoid Command#initialize reading from config.
14
# Set @datastore before calling post_execute; results are stored in @results via save().
15
#
16
# @param klass [Class] The command module class (e.g. Test_beef_debug)
17
# @param datastore [Hash] Data to set on @datastore (simulates callback data)
18
# @return [Object] Instance with @datastore set; call post_execute then read instance_variable_get(:@results)
19
#
20
def build_command_instance(klass, datastore = {})
21
instance = klass.allocate
22
instance.instance_variable_set(:@datastore, datastore)
23
instance
24
end
25
26
#
27
# Call post_execute on a command instance and return the saved results.
28
# Use after build_command_instance(klass, datastore).
29
#
30
# @param instance [Object] Command instance from build_command_instance
31
# @return [Hash, nil] The value passed to save() (stored in @results)
32
#
33
def run_post_execute(instance)
34
instance.post_execute
35
instance.instance_variable_get(:@results)
36
end
37
38
#
39
# Call pre_send on a command instance (e.g. before sending the command to the hooked browser).
40
# Use after build_command_instance(klass, datastore). Stub AssetHandler, Configuration, etc. before calling.
41
#
42
# @param instance [Object] Command instance from build_command_instance
43
#
44
def run_pre_send(instance)
45
instance.pre_send
46
end
47
end
48
49
RSpec.configure do |config|
50
config.include ModuleSpecHelper
51
52
# Stub Kernel.sleep for all module specs so modules that call sleep
53
# (e.g. Irc_nat_pinning's post_execute sleep 30) don't slow the suite.
54
config.before(:each) do |example|
55
if example.metadata[:file_path]&.include?('spec/beef/modules')
56
allow(Kernel).to receive(:sleep)
57
end
58
end
59
end
60
61