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(:length) }
|
|
|
|
it { should respond_to(:valid?) }
|
2014-01-07 18:00:04 +00:00
|
|
|
it { should respond_to(:each) }
|
2014-01-06 23:49:27 +00:00
|
|
|
|
2014-01-07 18:00:04 +00:00
|
|
|
describe '.new' do
|
|
|
|
|
|
|
|
context "with a hostname" do
|
|
|
|
let(:args) { "localhost" }
|
|
|
|
it { should be_valid }
|
2014-09-02 16:08:26 +00:00
|
|
|
it { expect(subject.length).to be >= 1 }
|
2014-01-06 23:49:27 +00:00
|
|
|
end
|
|
|
|
|
2014-01-07 18:00:04 +00:00
|
|
|
context "with a hostname and CIDR" do
|
|
|
|
let(:args) { "localhost/24" }
|
|
|
|
it { should be_valid }
|
2014-09-02 16:08:26 +00:00
|
|
|
it { expect(subject.length).to eq(256) }
|
2014-01-06 23:49:27 +00:00
|
|
|
end
|
|
|
|
|
2014-01-07 18:00:04 +00:00
|
|
|
context "with an invalid hostname" do
|
2014-08-19 21:47:30 +00:00
|
|
|
let(:args) { "@!*^&.invalid-hostname-really." }
|
2014-01-07 18:00:04 +00:00
|
|
|
it { should_not be_valid }
|
|
|
|
end
|
2014-01-06 23:49:27 +00:00
|
|
|
|
2014-01-07 18:00:04 +00:00
|
|
|
context "with an invalid hostname and CIDR" do
|
2014-08-19 21:47:30 +00:00
|
|
|
let(:args) { "@!*^&.invalid-hostname-really./24" }
|
2014-01-07 18:00:04 +00:00
|
|
|
it { should_not be_valid }
|
|
|
|
end
|
2014-01-06 23:49:27 +00:00
|
|
|
|
2014-01-07 22:31:55 +00:00
|
|
|
context "with an IPv6 address range containing a scope" do
|
|
|
|
let(:args) { "fe80::1%lo-fe80::100%lo" }
|
|
|
|
it { should be_valid }
|
|
|
|
end
|
|
|
|
|
2014-01-07 18:00:04 +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
|
|
|
|
2014-01-07 18:00:04 +00:00
|
|
|
it "should handle longform ranges" do
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1.1.1-10.1.1.2")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == 2
|
2014-01-09 21:18:03 +00:00
|
|
|
walker.next.should == "10.1.1.1"
|
2014-01-07 18:00:04 +00:00
|
|
|
end
|
2009-12-04 20:17:28 +00:00
|
|
|
|
2014-01-07 22:31:55 +00:00
|
|
|
context "with mulitple ranges" do
|
|
|
|
let(:args) { "1.1.1.1-2 2.1-2.2.2 3.1-2.1-2.1 " }
|
|
|
|
it { should be_valid }
|
2014-09-02 16:08:26 +00:00
|
|
|
it { expect(subject.length).to eq(8) }
|
2014-01-07 22:31:55 +00:00
|
|
|
it { should include("1.1.1.1") }
|
|
|
|
end
|
2013-07-18 20:00:57 +00:00
|
|
|
|
2014-01-07 18:00:04 +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
|
2014-01-09 21:18:03 +00:00
|
|
|
walker.next.should == "10.1.1.1"
|
2014-01-07 18:00:04 +00:00
|
|
|
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
|
2013-07-18 20:00:57 +00:00
|
|
|
|
2014-01-07 18:00:04 +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
|
2009-12-04 20:17:28 +00:00
|
|
|
|
2014-01-07 18:00:04 +00:00
|
|
|
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
|
|
|
|
2014-01-07 18:00:04 +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
|
2012-10-17 19:50:25 +00:00
|
|
|
|
2014-01-07 18:00:04 +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
|
2012-10-17 19:50:25 +00:00
|
|
|
|
2014-01-07 18:00:04 +00:00
|
|
|
it "should take * to mean 0-255" do
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1.3.*")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == 256
|
2014-01-09 21:18:03 +00:00
|
|
|
walker.next.should == "10.1.3.0"
|
2014-01-07 18:00:04 +00:00
|
|
|
walker.should include("10.1.3.255")
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1.*.3")
|
|
|
|
walker.should be_valid
|
|
|
|
walker.length.should == 256
|
2014-01-09 21:18:03 +00:00
|
|
|
walker.next.should == "10.1.0.3"
|
2014-01-07 18:00:04 +00:00
|
|
|
walker.should include("10.1.255.3")
|
|
|
|
end
|
2012-10-17 19:50:25 +00:00
|
|
|
|
2014-01-07 18:00:04 +00:00
|
|
|
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
|
2012-10-17 19:50:25 +00:00
|
|
|
|
2014-01-07 18:00:04 +00:00
|
|
|
it "should handle ranges and lists together" do
|
|
|
|
walker = Rex::Socket::RangeWalker.new("10.1.1.1-2,3")
|
2012-10-17 19:50:25 +00:00
|
|
|
walker.should be_valid
|
2014-01-07 18:00:04 +00:00
|
|
|
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
|
2014-01-07 22:31:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#each' do
|
|
|
|
let(:args) { "10.1.1.1-2,2,3 10.2.2.2" }
|
2014-01-07 18:00:04 +00:00
|
|
|
|
|
|
|
it "should yield all ips" do
|
|
|
|
got = []
|
|
|
|
walker.each { |ip|
|
|
|
|
got.push ip
|
|
|
|
}
|
2014-01-07 22:31:55 +00:00
|
|
|
got.should == ["10.1.1.1", "10.1.1.2", "10.1.1.3", "10.2.2.2"]
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#include_range?' do
|
|
|
|
let(:args) { "10.1.1.*" }
|
|
|
|
|
|
|
|
it "returns true for a sub-range" do
|
|
|
|
other = described_class.new("10.1.1.1-255")
|
|
|
|
walker.should be_include_range(other)
|
2012-10-17 19:50:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2014-01-09 21:18:03 +00:00
|
|
|
|
|
|
|
describe '#next' do
|
|
|
|
let(:args) { "10.1.1.1-5" }
|
|
|
|
it "should return all addresses" do
|
|
|
|
all = []
|
|
|
|
while ip = walker.next
|
|
|
|
all << ip
|
|
|
|
end
|
|
|
|
all.should == [ "10.1.1.1", "10.1.1.2", "10.1.1.3", "10.1.1.4", "10.1.1.5", ]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not raise if called again after empty" do
|
|
|
|
expect {
|
|
|
|
(walker.length + 5).times { walker.next }
|
|
|
|
}.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-12-04 19:37:14 +00:00
|
|
|
end
|