Skip to content

Commit 7f34586

Browse files
committed
Add specs for several Opt* classes
1 parent bf41d3d commit 7f34586

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed
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+
"192.0.2.0/24",
9+
"192.0.2.0-255",
10+
"192.0.2.0,1-255",
11+
"192.0.2.*",
12+
"192.0.2.0-192.0.2.255",
13+
].map{|a| [a, a]}
14+
invalid_values = [
15+
# Too many dots
16+
"192.0.2.0.0",
17+
"192.0.2.0.0,1",
18+
"192.0.2.0.0,1-2",
19+
# CIDR apparently doesn't validate before sending to addr_atoi
20+
#"192.0.2.0.0/24",
21+
# Not enough dots
22+
"192.0.2",
23+
"192.0.2,1",
24+
"192.0.2,1-2",
25+
# CIDR apparently doesn't validate before sending to addr_atoi
26+
#"192.0.2/24", # Not enough dots, cidr
27+
# Can't mix ranges and CIDR
28+
"192.0.2.0,1/24",
29+
"192.0.2.0-1/24",
30+
"192.0.2.0,1-2/24",
31+
]
32+
33+
it_behaves_like "an option", valid_values, invalid_values
34+
end
35+
36+
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::OptBool do
6+
valid_values =
7+
[
8+
["true", true ],
9+
["yes", true ],
10+
["1", true ],
11+
["false", false],
12+
["no", false],
13+
["0", false],
14+
]
15+
invalid_values =
16+
[ "yer mom", "123", "012" ]
17+
it_behaves_like "an option", valid_values, invalid_values
18+
end
19+
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::OptInt do
6+
valid_values = [
7+
"1", "10", "0",
8+
#"-1", # Negatives don't work
9+
].map{|v| [ v, v.to_i ] }
10+
valid_values.push([ "0x10", 16 ])
11+
invalid_values = [
12+
#"yer mom", # to_i makes this 0
13+
#"0.1", # to_i makes this 0
14+
]
15+
16+
it_behaves_like "an option", valid_values, invalid_values
17+
end
18+
19+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
require 'spec_helper'
3+
require 'msf/core/option_container'
4+
5+
describe Msf::OptPort do
6+
valid_values = [ "0", "1", "80", "65535" ].map{|v|
7+
# This is bogus, but OptPort doesn't implement #normalize, so it
8+
# falls back to just returning the original value
9+
[ v, v ]
10+
}
11+
invalid_values = [ "yer mom", "0.1", "-1" "65536" ]
12+
13+
it_behaves_like "an option", valid_values, invalid_values
14+
end
15+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 |valid_value, normalized_value|
9+
it "should be valid and normalize appropriately: #{valid_value}" do
10+
subject.valid?(valid_value).should be_true
11+
subject.normalize(valid_value).should == normalized_value
12+
end
13+
end
14+
end
15+
16+
context "with invalid values" do
17+
invalid_values.each do |invalid_value|
18+
it "should not be valid: #{invalid_value}" do
19+
subject.valid?(invalid_value).should be_false
20+
end
21+
end
22+
end
23+
24+
end
25+

0 commit comments

Comments
 (0)