Skip to content

Commit 1cb067e

Browse files
committed
Merge branch 'cleanup/option_specs' of git://github.com/jlee-r7/metasploit-framework into jlee-r7-cleanup/option_specs
2 parents 3d000ee + 2cbc15a commit 1cb067e

File tree

9 files changed

+282
-0
lines changed

9 files changed

+282
-0
lines changed

spec/lib/msf/core/data_store_spec.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
require 'spec_helper'
3+
require 'msf/core/data_store'
4+
5+
shared_examples "datastore" do
6+
it "should have options" do
7+
subject["foo"].should == "bar"
8+
subject["fizz"].should == "buzz"
9+
end
10+
it "should have case-insensitive keys" do
11+
# Sorted by gray code, just for fun
12+
subject["foo"].should == "bar"
13+
subject["Foo"].should == "bar"
14+
subject["FOo"].should == "bar"
15+
subject["fOo"].should == "bar"
16+
subject["fOO"].should == "bar"
17+
subject["FOO"].should == "bar"
18+
subject["FoO"].should == "bar"
19+
subject["foO"].should == "bar"
20+
end
21+
context "#to_h" do
22+
it "should return a Hash with correct values" do
23+
subject.to_h.should == { "foo" => "bar", "fizz" => "buzz" }
24+
end
25+
end
26+
end
27+
28+
describe Msf::DataStore do
29+
30+
describe "#import_option" do
31+
subject do
32+
s = described_class.new
33+
s.import_option("foo", "bar")
34+
s.import_option("fizz", "buzz")
35+
s
36+
end
37+
it_behaves_like "datastore"
38+
end
39+
40+
describe "#import_options_from_hash" do
41+
subject do
42+
hash = { "foo" => "bar", "fizz" => "buzz" }
43+
s = described_class.new
44+
s.import_options_from_hash(hash)
45+
s
46+
end
47+
it_behaves_like "datastore"
48+
end
49+
50+
describe "#import_options_from_s" do
51+
subject do
52+
str = "foo=bar fizz=buzz"
53+
s = described_class.new
54+
s.import_options_from_s(str)
55+
s
56+
end
57+
it_behaves_like "datastore"
58+
end
59+
60+
describe "#from_file" do
61+
subject do
62+
ini_instance = double
63+
ini_instance.stub(:group?).and_return(true)
64+
ini_instance.stub(:[]).and_return( { "foo" => "bar", "fizz" => "buzz" } )
65+
66+
ini = stub_const("Rex::Parser::Ini", Class.new)
67+
ini.stub(:from_file).and_return(ini_instance)
68+
69+
s = described_class.new
70+
s.from_file("path")
71+
s
72+
end
73+
74+
it_behaves_like "datastore"
75+
end
76+
77+
78+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
require 'spec_helper'
3+
require 'msf/core/option_container'
4+
5+
describe Msf::OptionContainer do
6+
it "should create new options for it's args" do
7+
foo_inst = mock("foo_inst")
8+
foo_inst.stub(:advanced=)
9+
foo_inst.stub(:evasion=)
10+
foo_inst.stub(:owner=)
11+
12+
foo_class = mock("opt_class")
13+
foo_class.should_receive(:new).and_return(foo_inst)
14+
15+
foo_inst.should_receive(:name).and_return("thing")
16+
17+
subject = described_class.new({
18+
'thing' => [ foo_class, true, nil, false ]
19+
})
20+
subject["thing"].should == foo_inst
21+
22+
end
23+
24+
25+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
require 'spec_helper'
3+
require 'msf/core/option_container'
4+
5+
describe Msf::OptAddressRange do
6+
# Normalized values are just the original value for OptAddressRange
7+
valid_values = [
8+
{ :value => "192.0.2.0/24", :normalized => "192.0.2.0/24" },
9+
{ :value => "192.0.2.0-255", :normalized => "192.0.2.0-255" },
10+
{ :value => "192.0.2.0,1-255", :normalized => "192.0.2.0,1-255" },
11+
{ :value => "192.0.2.*", :normalized => "192.0.2.*" },
12+
{ :value => "192.0.2.0-192.0.2.255", :normalized => "192.0.2.0-192.0.2.255" },
13+
]
14+
invalid_values = [
15+
# Too many dots
16+
{ :value => "192.0.2.0.0" },
17+
{ :value => "192.0.2.0.0,1" },
18+
{ :value => "192.0.2.0.0,1-2" },
19+
{ :pending => "Redmine #7536", :value => "192.0.2.0.0/24" },
20+
# Not enough dots
21+
{ :value => "192.0.2" },
22+
{ :value => "192.0.2,1" },
23+
{ :value => "192.0.2,1-2" },
24+
{ :pending => "Redmine #7536", :value => "192.0.2/24" },
25+
# Can't mix ranges and CIDR
26+
{ :value => "192.0.2.0,1/24" },
27+
{ :value => "192.0.2.0-1/24" },
28+
{ :value => "192.0.2.0,1-2/24" },
29+
{ :value => "192.0.2.0/1-24" },
30+
{ :value => "192.0.2.0-192.0.2.1-255", },
31+
]
32+
33+
it_behaves_like "an option", valid_values, invalid_values
34+
end
35+
36+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
require 'spec_helper'
3+
require 'msf/core/option_container'
4+
5+
describe Msf::OptAddress do
6+
valid_values = [
7+
"192.0.2.0", "127.0.0.1", "2001:db8::", "::1"
8+
# Normalized values are just the original value
9+
].map{|a| { :value => a, :normalized => a } }
10+
11+
invalid_values = [
12+
# Too many dots
13+
{ :value => "192.0.2.0.0" },
14+
# Not enough
15+
{ :pending => "Redmine #7537", :value => "192.0.2" }
16+
]
17+
18+
it_behaves_like "an option", valid_values, invalid_values
19+
end
20+
21+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
require 'spec_helper'
3+
require 'msf/core/option_container'
4+
5+
describe Msf::OptBool do
6+
valid_values = [
7+
{ :value => "true", :normalized => true },
8+
{ :value => "yes", :normalized => true },
9+
{ :value => "1", :normalized => true },
10+
{ :value => "false", :normalized => false },
11+
{ :value => "no", :normalized => false },
12+
{ :value => "0", :normalized => false },
13+
]
14+
invalid_values = [
15+
{ :value => "yer mom" },
16+
{ :value => "012" },
17+
{ :value => "123" },
18+
]
19+
it_behaves_like "an option", valid_values, invalid_values
20+
end
21+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
require 'spec_helper'
3+
require 'msf/core/option_container'
4+
5+
describe Msf::OptInt do
6+
valid_values = [
7+
{ :value => "1", :normalized => 1 },
8+
{ :value => "10", :normalized => 10 },
9+
{ :value => "0", :normalized => 0 },
10+
{ :value => "0x10", :normalized => 16 },
11+
{ :pending => "Redmine #7540", :value => "-1", :normalized => -1 }
12+
]
13+
invalid_values = [
14+
{ :pending => "Redmine #7539", :value => "yer mom", },
15+
{ :pending => "Redmine #7539", :value => "0.1", },
16+
]
17+
18+
it_behaves_like "an option", valid_values, invalid_values
19+
end
20+
21+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
require 'spec_helper'
3+
require 'msf/core/option_container'
4+
5+
describe Msf::OptPath do
6+
valid_values = [
7+
{ :value => __FILE__, :normalized => __FILE__ },
8+
]
9+
invalid_values = [
10+
{ :value => "yer mom", },
11+
{ :value => "0.1", },
12+
{ :value => "-1", },
13+
{ :value => "65536", },
14+
{ :value => "$", },
15+
]
16+
17+
it_behaves_like "an option", valid_values, invalid_values
18+
end
19+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
require 'spec_helper'
3+
require 'msf/core/option_container'
4+
5+
describe Msf::OptPort do
6+
valid_values = [
7+
{ :pending => "Redmine #7535", :value => "0", :normalized => 0 },
8+
{ :pending => "Redmine #7535", :value => "65536",:normalized => 65536 },
9+
{ :pending => "Redmine #7535", :value => "80", :normalized => 80 },
10+
]
11+
invalid_values = [
12+
{ :value => "yer mom", },
13+
{ :value => "0.1", },
14+
{ :value => "-1", },
15+
{ :value => "65536", },
16+
]
17+
18+
it_behaves_like "an option", valid_values, invalid_values
19+
end
20+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
shared_examples_for "an option" do |valid_values, invalid_values|
3+
subject do
4+
described_class.new("name")
5+
end
6+
7+
context "with valid values" do
8+
valid_values.each do |vhash|
9+
valid_value = vhash[:value]
10+
normalized_value = vhash[:normalized]
11+
12+
it "should be valid and normalize appropriately: #{valid_value}" do
13+
block = Proc.new {
14+
subject.normalize(valid_value).should == normalized_value
15+
subject.valid?(valid_value).should be_true
16+
}
17+
if vhash[:pending]
18+
pending(vhash[:pending], &block)
19+
else
20+
block.call
21+
end
22+
end
23+
end
24+
end
25+
26+
context "with invalid values" do
27+
invalid_values.each do |vhash|
28+
invalid_value = vhash[:value]
29+
it "should not be valid: #{invalid_value}" do
30+
block = Proc.new { subject.valid?(invalid_value).should be_false }
31+
if vhash[:pending]
32+
pending(vhash[:pending], &block)
33+
else
34+
block.call
35+
end
36+
end
37+
end
38+
end
39+
40+
end
41+

0 commit comments

Comments
 (0)