2013-04-17 19:26:16 +00:00
|
|
|
# -*- coding:binary -*-
|
2009-12-04 19:37:14 +00:00
|
|
|
require 'rex/socket/range_walker'
|
|
|
|
|
|
|
|
describe Rex::Socket::RangeWalker do
|
2014-01-06 23:49:27 +00:00
|
|
|
|
|
|
|
let(:args) { "::1" }
|
|
|
|
subject(:walker) { described_class.new(args) }
|
|
|
|
|
|
|
|
it { should respond_to(:num_ips) }
|
|
|
|
it { should respond_to(:length) }
|
|
|
|
it { should respond_to(:valid?) }
|
|
|
|
|
|
|
|
context "with a hostname" do
|
|
|
|
let(:args) { "localhost" }
|
|
|
|
it { should be_valid }
|
|
|
|
it "should have one address" do
|
|
|
|
walker.length.should == 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a hostname and CIDR" do
|
|
|
|
let(:args) { "localhost/24" }
|
|
|
|
it { should be_valid }
|
|
|
|
it "should have 256 addresses" do
|
|
|
|
walker.length.should == 256
|
|
|
|
end
|
2012-10-17 19:50:25 +00:00
|
|
|
end
|
2014-01-06 23:49:27 +00:00
|
|
|
|
|
|
|
context "with an invalid hostname" do
|
|
|
|
let(:args) { "asdf.foo." }
|
|
|
|
it { should_not be_valid }
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-10-17 19:50:25 +00:00
|
|
|
it "should handle single ipv6 addresses" do
|
|
|
|
walker = Rex::Socket::RangeWalker.new("::1")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == 1
|
|
|
|
end
|
2009-12-04 19:37:14 +00:00
|
|
|
|
2012-10-17 19:50:25 +00:00
|
|
|
it "should handle ranges" do
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1.1.1-2")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == 2
|
|
|
|
walker.next_ip.should == "10.1.1.1"
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1-2.1.1-2")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == 4
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1-2.3-4.5-6")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == 8
|
|
|
|
walker.should include("10.1.3.5")
|
|
|
|
end
|
2009-12-04 20:17:28 +00:00
|
|
|
|
2013-07-18 20:00:57 +00:00
|
|
|
it 'should reject CIDR ranges with missing octets' do
|
|
|
|
walker = Rex::Socket::RangeWalker.new('192.168/24')
|
|
|
|
walker.should_not be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should reject a CIDR range with too many octets' do
|
|
|
|
walker = Rex::Socket::RangeWalker.new('192.168.1.2.0/24')
|
|
|
|
walker.should_not be_valid
|
|
|
|
end
|
|
|
|
|
2012-10-17 19:50:25 +00:00
|
|
|
it "should default the lower bound of a range to 0" do
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1.3.-17")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == 18
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1.3.-255")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == 256
|
|
|
|
end
|
2009-12-04 20:17:28 +00:00
|
|
|
|
2012-10-17 19:50:25 +00:00
|
|
|
it "should default the upper bound of a range to 255" do
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1.3.254-")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == 2
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should take * to mean 0-255" do
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1.3.*")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == 256
|
|
|
|
walker.next_ip.should == "10.1.3.0"
|
|
|
|
walker.should include("10.1.3.255")
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1.*.3")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == 256
|
|
|
|
walker.next_ip.should == "10.1.0.3"
|
|
|
|
walker.should include("10.1.255.3")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should handle lists" do
|
|
|
|
#walker = Rex::Socket::RangeWalker.new("10.1.1.1,2")
|
|
|
|
#walker.should be_valid
|
|
|
|
#walker.length.should == 2
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1.1.1")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == 1
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1.1.1,3")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == 2
|
|
|
|
walker.should_not include("10.1.1.2")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should produce the same ranges with * and 0-255" do
|
|
|
|
a = Rex::Socket::RangeWalker.new("10.1.3.*")
|
|
|
|
b = Rex::Socket::RangeWalker.new("10.1.3.0-255")
|
|
|
|
a.ranges.should eq(b.ranges)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should handle ranges and lists together" do
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1.1.1-2,3")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == 3
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1-2.1.1,2")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == 4
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1,2.3,4.5,6")
|
|
|
|
walker.length.should == 8
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should handle cidr" do
|
|
|
|
31.downto 16 do |bits|
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1.1.1/#{bits}")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == (2**(32-bits))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should yield all ips" do
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1.1.1,2,3")
|
|
|
|
got = []
|
|
|
|
walker.each { |ip|
|
|
|
|
got.push ip
|
|
|
|
}
|
|
|
|
got.should == ["10.1.1.1", "10.1.1.2", "10.1.1.3"]
|
|
|
|
end
|
2009-12-04 19:37:14 +00:00
|
|
|
end
|