Skip to content

Commit d7e7028

Browse files
committed
Merge branch 'cleanup/specs' of git://github.com/jlee-r7/metasploit-framework into jlee-r7-cleanup/specs
2 parents 291ad27 + 5f1ec45 commit d7e7028

File tree

7 files changed

+305
-238
lines changed

7 files changed

+305
-238
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace :yard do
3737
task :stats => :environment do
3838
stats = YARD::CLI::Stats.new
3939
yard_arguments = yard_options + ['--compact', '--list-undoc'] + yard_files
40-
stats.run *yard_arguments
40+
stats.run(*yard_arguments)
4141
end
4242
end
4343

spec/msf/core/task_manager_spec.rb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
require 'msf/core/task_manager'
3+
4+
describe Msf::TaskManager do
5+
6+
let(:framework) do
7+
Msf::Framework.new
8+
end
9+
10+
let(:tm) do
11+
Msf::TaskManager.new(framework)
12+
end
13+
14+
it "should have attributes" do
15+
tm.should respond_to("processing")
16+
tm.should respond_to("queue")
17+
tm.should respond_to("thread")
18+
tm.should respond_to("framework")
19+
tm.should respond_to("processing=")
20+
tm.should respond_to("queue=")
21+
tm.should respond_to("thread=")
22+
tm.should respond_to("framework=")
23+
end
24+
25+
it "should initialize with an empty queue" do
26+
tm.queue.length.should == 0
27+
tm.backlog.should == 0
28+
tm.backlog.should == tm.queue.length
29+
end
30+
31+
it "should add items to the queue and process them" do
32+
tm.queue_proc(Proc.new{ })
33+
tm.backlog.should == 1
34+
t = Msf::TaskManager::Task.new(Proc.new { })
35+
tm.queue_task(t)
36+
tm.backlog.should == 2
37+
tm.start
38+
t.wait
39+
tm.backlog.should == 0
40+
end
41+
42+
it "should add items to the queue and flush them" do
43+
tm.queue_proc(Proc.new{ })
44+
tm.backlog.should == 1
45+
tm.queue_proc(Proc.new{ })
46+
tm.backlog.should == 2
47+
tm.flush
48+
tm.backlog.should == 0
49+
end
50+
51+
it "should start and stop" do
52+
t = Msf::TaskManager::Task.new(Proc.new { })
53+
tm.queue_task(t)
54+
tm.backlog.should == 1
55+
tm.start
56+
t.wait
57+
tm.backlog.should == 0
58+
tm.stop
59+
1.upto 100 do |cnt|
60+
tm.queue_proc(Proc.new{ })
61+
tm.backlog.should == cnt
62+
end
63+
t = Msf::TaskManager::Task.new(Proc.new { })
64+
tm.queue_task(t)
65+
tm.start
66+
t.wait
67+
tm.backlog.should == 0
68+
end
69+
70+
it "should handle task timeouts" do
71+
t = Msf::TaskManager::Task.new(Proc.new { sleep(30) })
72+
t.timeout = 0.1
73+
74+
tm.start
75+
tm.queue_task(t)
76+
t.wait
77+
78+
t.status.should == :timeout
79+
t.duration.should <= 1.0
80+
end
81+
82+
it "should handle task exceptions" do
83+
t = Msf::TaskManager::Task.new(Proc.new { asdf1234() })
84+
tm.start
85+
tm.queue_task(t)
86+
t.wait
87+
t.status.should == :dropped
88+
t.exception.class.should == ::NoMethodError
89+
90+
t = Msf::TaskManager::Task.new(Proc.new { return 12345 })
91+
tm.queue_task(t)
92+
t.wait
93+
t.status.should == :dropped
94+
t.exception.should be_a ::LocalJumpError
95+
end
96+
end
97+

spec/msf/util/exe_spec.rb

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
2+
require 'msf/core'
3+
require 'msf/base/simple'
4+
5+
describe Msf::Util::EXE do
6+
7+
subject do
8+
described_class
9+
end
10+
11+
$framework = Msf::Simple::Framework.create(
12+
:module_types => [ Msf::MODULE_NOP ],
13+
'DisableDatabase' => true
14+
)
15+
16+
context '.to_executable_fmt' do
17+
it "should output nil when given a bogus format" do
18+
bin = subject.to_executable_fmt($framework, "", "", "", "does not exist", {})
19+
20+
bin.should == nil
21+
end
22+
23+
platform_format_map = {
24+
"windows" => [
25+
{ :format => "dll", :arch => "x86", :file_fp => /PE32 .*DLL/ },
26+
{ :format => "dll", :arch => "x64", :file_fp => /PE32\+.*DLL/ },
27+
{ :format => "exe", :arch => "x86", :file_fp => /PE32 / },
28+
{ :format => "exe", :arch => "x64", :file_fp => /PE32\+/ },
29+
{ :format => "exe-small", :arch => "x86", :file_fp => /PE32 / },
30+
# No template for 64-bit exe-small. That's fine, we probably
31+
# don't need one.
32+
#{ :format => "exe-small", :arch => "x64", :file_fp => /PE32\+/ },
33+
],
34+
"linux" => [
35+
{ :format => "elf", :arch => "x86", :file_fp => /ELF 32.*SYSV/ },
36+
{ :format => "elf", :arch => "x64", :file_fp => /ELF 64.*SYSV/ },
37+
{ :format => "elf", :arch => "armle",:file_fp => /ELF 32.*ARM/, :pending => true },
38+
],
39+
"bsd" => [
40+
{ :format => "elf", :arch => "x86", :file_fp => /ELF 32.*BSD/ },
41+
],
42+
"solaris" => [
43+
{ :format => "elf", :arch => "x86", :file_fp => /ELF 32/ },
44+
],
45+
"osx" => [
46+
{ :format => "macho", :arch => "x86", :file_fp => /Mach-O.*i386/ },
47+
{ :format => "macho", :arch => "x64", :file_fp => /Mach-O 64/ },
48+
{ :format => "macho", :arch => "armle", :file_fp => /Mach-O.*acorn/, :pending => true },
49+
{ :format => "macho", :arch => "ppc", :file_fp => /Mach-O.*ppc/, :pending => true },
50+
]
51+
}
52+
53+
platform_format_map.each do |plat, formats|
54+
context "with platform=#{plat}" do
55+
let(:platform) do
56+
Msf::Module::PlatformList.transform(plat)
57+
end
58+
59+
it "should output nil when given bogus format" do
60+
bin = subject.to_executable_fmt($framework, formats.first[:arch], platform, "\xcc", "asdf", {})
61+
bin.should == nil
62+
end
63+
it "should output nil when given bogus arch" do
64+
bin = subject.to_executable_fmt($framework, "asdf", platform, "\xcc", formats.first[:format], {})
65+
bin.should == nil
66+
end
67+
68+
formats.each do |format_hash|
69+
fmt = format_hash[:format]
70+
arch = format_hash[:arch]
71+
72+
if format_hash[:pending]
73+
pending "returns an executable when given arch=#{arch}, fmt=#{fmt}"
74+
next
75+
end
76+
77+
it "returns an executable when given arch=#{arch}, fmt=#{fmt}" do
78+
bin = subject.to_executable_fmt($framework, arch, platform, "\xcc", fmt, {})
79+
bin.should be_a String
80+
81+
f = IO.popen("file -","w+")
82+
f.write(bin)
83+
f.close_write
84+
fp = f.read
85+
f.close
86+
fp.should =~ format_hash[:file_fp] if format_hash[:file_fp]
87+
end
88+
89+
it "returns nil when given bogus format for arch=#{arch}" do
90+
bin = subject.to_executable_fmt($framework, arch, platform, "\xcc", "asdf", {})
91+
bin.should == nil
92+
end
93+
94+
end
95+
96+
end
97+
end
98+
99+
end
100+
101+
end
102+

test/tests/02_nmap_import_test.rb renamed to spec/rex/parser/nmap_xml_spec.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11

2-
require 'testbase'
3-
require 'rexml/document'
42
require 'rex/parser/nmap_xml'
5-
require 'spec'
63

74
xml = '
85
<?xml version="1.0" ?>
@@ -46,9 +43,9 @@
4643
host["addrs"]["ipv4"].should == "192.168.0.1"
4744
end
4845
}
46+
REXML::Document.parse_stream(StringIO.new(xml), parser)
4947
it "should have found exactly one host" do
5048
total_hosts.should == 1
5149
end
52-
REXML::Document.parse_stream(StringIO.new(xml), parser)
5350
end
5451

spec/rex/socket/range_walker_spec.rb

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
require 'rex/socket/range_walker'
2+
3+
describe Rex::Socket::RangeWalker do
4+
it "should have a num_ips attribute" do
5+
walker = Rex::Socket::RangeWalker.new("")
6+
walker.should respond_to("num_ips")
7+
walker.should respond_to("length")
8+
walker.num_ips.should == walker.length
9+
end
10+
it "should handle single ipv6 addresses" do
11+
walker = Rex::Socket::RangeWalker.new("::1")
12+
walker.should be_valid
13+
walker.length.should == 1
14+
end
15+
16+
it "should handle ranges" do
17+
walker = Rex::Socket::RangeWalker.new("10.1.1.1-2")
18+
walker.should be_valid
19+
walker.length.should == 2
20+
walker.next_ip.should == "10.1.1.1"
21+
walker = Rex::Socket::RangeWalker.new("10.1-2.1.1-2")
22+
walker.should be_valid
23+
walker.length.should == 4
24+
walker = Rex::Socket::RangeWalker.new("10.1-2.3-4.5-6")
25+
walker.should be_valid
26+
walker.length.should == 8
27+
walker.should include("10.1.3.5")
28+
end
29+
30+
it "should default the lower bound of a range to 0" do
31+
walker = Rex::Socket::RangeWalker.new("10.1.3.-17")
32+
walker.should be_valid
33+
walker.length.should == 18
34+
walker = Rex::Socket::RangeWalker.new("10.1.3.-255")
35+
walker.should be_valid
36+
walker.length.should == 256
37+
end
38+
39+
it "should default the upper bound of a range to 255" do
40+
walker = Rex::Socket::RangeWalker.new("10.1.3.254-")
41+
walker.should be_valid
42+
walker.length.should == 2
43+
end
44+
45+
it "should take * to mean 0-255" do
46+
walker = Rex::Socket::RangeWalker.new("10.1.3.*")
47+
walker.should be_valid
48+
walker.length.should == 256
49+
walker.next_ip.should == "10.1.3.0"
50+
walker.should include("10.1.3.255")
51+
walker = Rex::Socket::RangeWalker.new("10.1.*.3")
52+
walker.should be_valid
53+
walker.length.should == 256
54+
walker.next_ip.should == "10.1.0.3"
55+
walker.should include("10.1.255.3")
56+
end
57+
58+
it "should handle lists" do
59+
#walker = Rex::Socket::RangeWalker.new("10.1.1.1,2")
60+
#walker.should be_valid
61+
#walker.length.should == 2
62+
walker = Rex::Socket::RangeWalker.new("10.1.1.1")
63+
walker.should be_valid
64+
walker.length.should == 1
65+
walker = Rex::Socket::RangeWalker.new("10.1.1.1,3")
66+
walker.should be_valid
67+
walker.length.should == 2
68+
walker.should_not include("10.1.1.2")
69+
end
70+
71+
it "should produce the same ranges with * and 0-255" do
72+
a = Rex::Socket::RangeWalker.new("10.1.3.*")
73+
b = Rex::Socket::RangeWalker.new("10.1.3.0-255")
74+
a.ranges.should eq(b.ranges)
75+
end
76+
77+
it "should handle ranges and lists together" do
78+
walker = Rex::Socket::RangeWalker.new("10.1.1.1-2,3")
79+
walker.should be_valid
80+
walker.length.should == 3
81+
walker = Rex::Socket::RangeWalker.new("10.1-2.1.1,2")
82+
walker.should be_valid
83+
walker.length.should == 4
84+
walker = Rex::Socket::RangeWalker.new("10.1,2.3,4.5,6")
85+
walker.length.should == 8
86+
end
87+
88+
it "should handle cidr" do
89+
31.downto 16 do |bits|
90+
walker = Rex::Socket::RangeWalker.new("10.1.1.1/#{bits}")
91+
walker.should be_valid
92+
walker.length.should == (2**(32-bits))
93+
end
94+
end
95+
96+
it "should yield all ips" do
97+
walker = Rex::Socket::RangeWalker.new("10.1.1.1,2,3")
98+
got = []
99+
walker.each { |ip|
100+
got.push ip
101+
}
102+
got.should == ["10.1.1.1", "10.1.1.2", "10.1.1.3"]
103+
end
104+
end

0 commit comments

Comments
 (0)