Skip to content

Commit 4ba5713

Browse files
committed
Spec Msf::Simple::Framework#init_module_paths
[#47720609]
1 parent c221787 commit 4ba5713

File tree

4 files changed

+139
-27
lines changed

4 files changed

+139
-27
lines changed

lib/msf/base/simple/framework.rb

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: binary -*-
22
require 'msf/base/simple'
3+
require 'msf/base/simple/framework/module_paths'
34

45
module Msf
56
module Simple
@@ -12,6 +13,7 @@ module Simple
1213
#
1314
###
1415
module Framework
16+
include Msf::Simple::Framework::ModulePaths
1517

1618
###
1719
#
@@ -155,33 +157,6 @@ def save_config
155157
self.datastore.to_file(Msf::Config.config_file, 'framework/core')
156158
end
157159

158-
#
159-
# Initialize the module paths
160-
#
161-
def init_module_paths
162-
163-
# Ensure the module cache is accurate
164-
self.modules.refresh_cache_from_database
165-
166-
# Initialize the default module search paths
167-
if (Msf::Config.module_directory)
168-
self.modules.add_module_path(Msf::Config.module_directory)
169-
end
170-
171-
# Initialize the user module search path
172-
if (Msf::Config.user_module_directory)
173-
self.modules.add_module_path(Msf::Config.user_module_directory)
174-
end
175-
176-
# If additional module paths have been defined globally, then load them.
177-
# They should be separated by semi-colons.
178-
if self.datastore['MsfModulePaths']
179-
self.datastore['MsfModulePaths'].split(";").each { |path|
180-
self.modules.add_module_path(path)
181-
}
182-
end
183-
end
184-
185160
#
186161
# Statistics.
187162
#
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module Msf
2+
module Simple
3+
module Framework
4+
module ModulePaths
5+
# Initialize the module paths
6+
#
7+
# @return [void]
8+
def init_module_paths
9+
# Ensure the module cache is accurate
10+
self.modules.refresh_cache_from_database
11+
12+
# Initialize the default module search paths
13+
if (Msf::Config.module_directory)
14+
self.modules.add_module_path(Msf::Config.module_directory)
15+
end
16+
17+
# Initialize the user module search path
18+
if (Msf::Config.user_module_directory)
19+
self.modules.add_module_path(Msf::Config.user_module_directory)
20+
end
21+
22+
# If additional module paths have been defined globally, then load them.
23+
# They should be separated by semi-colons.
24+
if self.datastore['MsfModulePaths']
25+
self.datastore['MsfModulePaths'].split(";").each { |path|
26+
self.modules.add_module_path(path)
27+
}
28+
end
29+
end
30+
end
31+
end
32+
end
33+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'spec_helper'
2+
3+
describe Msf::Simple::Framework do
4+
include_context 'Msf::Simple::Framework'
5+
6+
subject do
7+
framework
8+
end
9+
10+
it_should_behave_like 'Msf::Simple::Framework::ModulePaths'
11+
end
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
shared_examples_for 'Msf::Simple::Framework::ModulePaths' do
2+
it { should be_a Msf::Simple::Framework::ModulePaths }
3+
4+
context '#init_module_paths' do
5+
def init_module_paths
6+
framework.init_module_paths
7+
end
8+
9+
let(:module_directory) do
10+
nil
11+
end
12+
13+
let(:user_module_directory) do
14+
nil
15+
end
16+
17+
before(:each) do
18+
# create the framework first so that it's initialization's call
19+
# to init_module_paths doesn't get captured.
20+
framework
21+
22+
Msf::Config.stub(:module_directory => module_directory)
23+
Msf::Config.stub(:user_module_directory => user_module_directory)
24+
end
25+
26+
it 'should refresh module cache from database' do
27+
framework.modules.should_receive(:refresh_cache_from_database)
28+
29+
init_module_paths
30+
end
31+
32+
context 'Msf::Config' do
33+
context 'module_directory' do
34+
context 'without nil' do
35+
let(:module_directory) do
36+
'modules'
37+
end
38+
39+
it 'should add Msf::Config.module_directory to module paths' do
40+
framework.modules.should_receive(:add_module_path).with(
41+
module_directory
42+
)
43+
44+
init_module_paths
45+
end
46+
end
47+
end
48+
49+
context 'user_module_directory' do
50+
context 'without nil' do
51+
let(:user_module_directory) do
52+
'user/modules'
53+
end
54+
55+
it 'should add Msf::Config.user_module_directory to module paths' do
56+
framework.modules.should_receive(:add_module_path).with(
57+
user_module_directory
58+
)
59+
60+
init_module_paths
61+
end
62+
end
63+
end
64+
end
65+
66+
context 'datastore' do
67+
context 'MsfModulePaths' do
68+
let(:module_paths) do
69+
module_paths = []
70+
71+
1.upto(2) do |i|
72+
module_paths << "msf/#{i}/modules"
73+
end
74+
75+
module_paths
76+
end
77+
78+
before(:each) do
79+
msf_module_paths = module_paths.join(';')
80+
framework.datastore['MsfModulePaths'] = msf_module_paths
81+
end
82+
83+
it 'should add each module path' do
84+
module_paths.each do |module_path|
85+
framework.modules.should_receive(:add_module_path).with(module_path)
86+
end
87+
88+
init_module_paths
89+
end
90+
end
91+
end
92+
end
93+
end

0 commit comments

Comments
 (0)