Skip to content

Commit 7d848c7

Browse files
committed
Merge remote branch 'origin/bug/fastlib-nested-pathnames'
2 parents b8e880b + 9346960 commit 7d848c7

File tree

9 files changed

+203
-19
lines changed

9 files changed

+203
-19
lines changed

.rspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--format documentation

Gemfile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,26 @@ source 'http://rubygems.org'
22

33
# Need 3+ for ActiveSupport::Concern
44
gem 'activesupport', '>= 3.0.0'
5+
# Needed for Msf::DbManager
6+
gem 'activerecord'
7+
# Database models shared between framework and Pro.
8+
gem 'metasploit_data_models', :git => 'git://github.com/rapid7/metasploit_data_models.git'
59
# Needed for module caching in Mdm::ModuleDetails
610
gem 'pg', '>= 0.11'
711

812
group :development do
9-
# running documentation generation tasks
10-
gem 'rake'
1113
# Markdown formatting for yard
1214
gem 'redcarpet'
1315
# generating documentation
1416
gem 'yard'
1517
end
18+
19+
group :development, :test do
20+
# running documentation generation tasks and rspec tasks
21+
gem 'rake'
22+
end
23+
24+
group :test do
25+
# testing framework
26+
gem 'rspec'
27+
end

Gemfile.lock

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,62 @@
1+
GIT
2+
remote: git://github.com/rapid7/metasploit_data_models.git
3+
revision: dd6c3a31c5ad8b55f4913b5ba20307178ba9c7bf
4+
specs:
5+
metasploit_data_models (0.0.2)
6+
activerecord
7+
activesupport
8+
pg
9+
pry
10+
111
GEM
212
remote: http://rubygems.org/
313
specs:
14+
activemodel (3.2.8)
15+
activesupport (= 3.2.8)
16+
builder (~> 3.0.0)
17+
activerecord (3.2.8)
18+
activemodel (= 3.2.8)
19+
activesupport (= 3.2.8)
20+
arel (~> 3.0.2)
21+
tzinfo (~> 0.3.29)
422
activesupport (3.2.8)
523
i18n (~> 0.6)
624
multi_json (~> 1.0)
25+
arel (3.0.2)
26+
builder (3.0.3)
27+
coderay (1.0.8)
28+
diff-lcs (1.1.3)
729
i18n (0.6.1)
30+
method_source (0.8)
831
multi_json (1.3.6)
932
pg (0.14.1)
33+
pry (0.9.10)
34+
coderay (~> 1.0.5)
35+
method_source (~> 0.8)
36+
slop (~> 3.3.1)
1037
rake (0.9.2.2)
1138
redcarpet (2.1.1)
39+
rspec (2.11.0)
40+
rspec-core (~> 2.11.0)
41+
rspec-expectations (~> 2.11.0)
42+
rspec-mocks (~> 2.11.0)
43+
rspec-core (2.11.1)
44+
rspec-expectations (2.11.3)
45+
diff-lcs (~> 1.1.3)
46+
rspec-mocks (2.11.3)
47+
slop (3.3.3)
48+
tzinfo (0.3.33)
1249
yard (0.8.2.1)
1350

1451
PLATFORMS
1552
ruby
1653

1754
DEPENDENCIES
55+
activerecord
1856
activesupport (>= 3.0.0)
57+
metasploit_data_models!
1958
pg (>= 0.11)
2059
rake
2160
redcarpet
61+
rspec
2262
yard

Rakefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
require 'bundler/setup'
22

3+
require 'rspec/core/rake_task'
34
require 'yard'
45

6+
RSpec::Core::RakeTask.new(:spec)
7+
8+
task :default => :spec
9+
510
namespace :yard do
611
yard_files = [
712
# Ruby source files first

lib/msf/core/db_manager.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: binary -*-
22

3+
require 'msf/base/config'
34
require 'msf/core'
45
require 'msf/core/db'
56
require 'msf/core/task_manager'

lib/msf/core/module_manager/module_paths.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,22 @@ def add_module_path(path)
1717
nested_paths = []
1818

1919
# remove trailing file separator
20-
path.sub!(/#{File::SEPARATOR}$/, '')
20+
path_without_trailing_file_separator = path.sub(/#{File::SEPARATOR}$/, '')
2121

22-
pathname = Pathname.new(path)
22+
# Make the path completely canonical
23+
pathname = Pathname.new(path_without_trailing_file_separator).expand_path
2324
extension = pathname.extname
2425

2526
if extension == Msf::Modules::Loader::Archive::ARCHIVE_EXTENSION
2627
unless pathname.exist?
27-
raise RuntimeError, "The path supplied does not exist", caller
28+
raise ArgumentError, "The path supplied does not exist", caller
2829
end
2930

30-
nested_paths << pathname.expand_path.to_s
31+
nested_paths << pathname.to_s
3132
else
32-
# Make the path completely canonical
33-
pathname = Pathname.new(path).expand_path
34-
3533
# Make sure the path is a valid directory
3634
unless pathname.directory?
37-
raise RuntimeError, "The path supplied is not a valid directory.", caller
35+
raise ArgumentError, "The path supplied is not a valid directory.", caller
3836
end
3937

4038
nested_paths << pathname.to_s
@@ -43,7 +41,7 @@ def add_module_path(path)
4341
fastlib_glob = pathname.join('**', "*#{Msf::Modules::Loader::Archive::ARCHIVE_EXTENSION}")
4442

4543
Dir.glob(fastlib_glob).each do |fastlib_path|
46-
nested_pathnames << fastlib_path
44+
nested_paths << fastlib_path
4745
end
4846
end
4947

spec/msf/core/module_manager_spec.rb

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
require 'spec_helper'
2+
3+
#
4+
# Core
5+
#
6+
7+
# Temporary files
8+
require 'tempfile'
9+
# add mktmpdir to Dir
10+
require 'tmpdir'
11+
12+
#
13+
# Project
14+
#
15+
16+
require 'msf/core'
17+
18+
describe Msf::ModuleManager do
19+
let(:archive_basename) do
20+
[basename_prefix, archive_extension]
21+
end
22+
23+
let(:archive_extension) do
24+
'.fastlib'
25+
end
26+
27+
let(:basename_prefix) do
28+
'rspec'
29+
end
30+
31+
let(:framework) do
32+
Msf::Framework.new
33+
end
34+
35+
subject do
36+
described_class.new(framework)
37+
end
38+
39+
context '#add_module_path' do
40+
it 'should strip trailing File::SEPARATOR from the path' do
41+
Dir.mktmpdir do |path|
42+
path_with_trailing_separator = path + File::SEPARATOR
43+
subject.add_module_path(path_with_trailing_separator)
44+
45+
subject.send(:module_paths).should_not include(path_with_trailing_separator)
46+
subject.send(:module_paths).should include(path)
47+
end
48+
end
49+
50+
context 'with Fastlib archive' do
51+
it 'should raise an ArgumentError unless the File exists' do
52+
file = Tempfile.new(archive_basename)
53+
# unlink will clear path, so copy it to a variable
54+
path = file.path
55+
file.unlink
56+
57+
File.exist?(path).should be_false
58+
59+
expect {
60+
subject.add_module_path(path)
61+
}.to raise_error(ArgumentError, "The path supplied does not exist")
62+
end
63+
64+
it 'should add the path to #module_paths if the File exists' do
65+
Tempfile.open(archive_basename) do |temporary_file|
66+
path = temporary_file.path
67+
68+
File.exist?(path).should be_true
69+
70+
subject.add_module_path(path)
71+
72+
subject.send(:module_paths).should include(path)
73+
end
74+
end
75+
end
76+
77+
context 'with directory' do
78+
it 'should add path to #module_paths' do
79+
Dir.mktmpdir do |path|
80+
subject.add_module_path(path)
81+
82+
subject.send(:module_paths).should include(path)
83+
end
84+
end
85+
86+
context 'containing Fastlib archives' do
87+
it 'should add each Fastlib archive to #module_paths' do
88+
Dir.mktmpdir do |directory|
89+
Tempfile.open(archive_basename, directory) do |file|
90+
subject.add_module_path(directory)
91+
92+
subject.send(:module_paths).should include(directory)
93+
subject.send(:module_paths).should include(file.path)
94+
end
95+
end
96+
end
97+
end
98+
end
99+
100+
context 'with other file' do
101+
it 'should raise ArgumentError' do
102+
Tempfile.open(basename_prefix) do |file|
103+
expect {
104+
subject.add_module_path(file.path)
105+
}.to raise_error(ArgumentError, 'The path supplied is not a valid directory.')
106+
end
107+
end
108+
end
109+
end
110+
end

spec/msf/database_manager_spec.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.

spec/spec_helper.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,24 @@
1-
require "../lib/msf/core.rb"
1+
require 'rubygems'
2+
require 'bundler'
3+
Bundler.setup(:default, :test)
4+
5+
# add project lib directory to load path
6+
spec_pathname = Pathname.new(__FILE__).dirname
7+
root_pathname = spec_pathname.join('..').expand_path
8+
lib_pathname = root_pathname.join('lib')
9+
$LOAD_PATH.unshift(lib_pathname.to_s)
10+
11+
require 'rspec/core'
12+
13+
# Requires supporting ruby files with custom matchers and macros, etc,
14+
# in spec/support/ and its subdirectories.
15+
support_glob = root_pathname.join('spec', 'support', '**', '*.rb')
16+
17+
Dir.glob(support_glob) do |path|
18+
require path
19+
end
20+
21+
RSpec.configure do |config|
22+
config.mock_with :rspec
23+
end
24+

0 commit comments

Comments
 (0)