diff options
Diffstat (limited to 'spec/rubyspec/library/ipaddr')
-rw-r--r-- | spec/rubyspec/library/ipaddr/hton_spec.rb | 30 | ||||
-rw-r--r-- | spec/rubyspec/library/ipaddr/ipv4_conversion_spec.rb | 46 | ||||
-rw-r--r-- | spec/rubyspec/library/ipaddr/new_spec.rb | 93 | ||||
-rw-r--r-- | spec/rubyspec/library/ipaddr/operator_spec.rb | 80 | ||||
-rw-r--r-- | spec/rubyspec/library/ipaddr/reverse_spec.rb | 27 | ||||
-rw-r--r-- | spec/rubyspec/library/ipaddr/to_s_spec.rb | 20 |
6 files changed, 296 insertions, 0 deletions
diff --git a/spec/rubyspec/library/ipaddr/hton_spec.rb b/spec/rubyspec/library/ipaddr/hton_spec.rb new file mode 100644 index 0000000000..037bb3d328 --- /dev/null +++ b/spec/rubyspec/library/ipaddr/hton_spec.rb @@ -0,0 +1,30 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'ipaddr' + +describe "IPAddr#hton" do + + it "converts IPAddr to network byte order" do + addr = '' + IPAddr.new("1234:5678:9abc:def0:1234:5678:9abc:def0").hton.each_byte do |c| + addr += sprintf("%02x", c) + end + addr.should == "123456789abcdef0123456789abcdef0" + addr = '' + IPAddr.new("123.45.67.89").hton.each_byte do |c| + addr += sprintf("%02x", c) + end + addr.should == sprintf("%02x%02x%02x%02x", 123, 45, 67, 89) + end + +end + +describe "IPAddr#new_ntoh" do + + it "creates a new IPAddr using hton notation" do + a = IPAddr.new("3ffe:505:2::") + IPAddr.new_ntoh(a.hton).to_s.should == "3ffe:505:2::" + a = IPAddr.new("192.168.2.1") + IPAddr.new_ntoh(a.hton).to_s.should == "192.168.2.1" + end + +end diff --git a/spec/rubyspec/library/ipaddr/ipv4_conversion_spec.rb b/spec/rubyspec/library/ipaddr/ipv4_conversion_spec.rb new file mode 100644 index 0000000000..b69be82c13 --- /dev/null +++ b/spec/rubyspec/library/ipaddr/ipv4_conversion_spec.rb @@ -0,0 +1,46 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'ipaddr' + +describe "IPAddr#ipv4_compat" do + + it "should ipv4_compat?" do + a = IPAddr.new("::192.168.1.2") + a.to_s.should == "::192.168.1.2" + a.to_string.should == "0000:0000:0000:0000:0000:0000:c0a8:0102" + a.family.should == Socket::AF_INET6 + a.ipv4_compat?.should == true + b = a.native + b.to_s.should == "192.168.1.2" + b.family.should == Socket::AF_INET + b.ipv4_compat?.should == false + + a = IPAddr.new("192.168.1.2") + b = a.ipv4_compat + b.to_s.should == "::192.168.1.2" + b.family.should == Socket::AF_INET6 + end + +end + +describe "IPAddr#ipv4_mapped" do + + it "should ipv4_mapped" do + a = IPAddr.new("::ffff:192.168.1.2") + a.to_s.should == "::ffff:192.168.1.2" + a.to_string.should == "0000:0000:0000:0000:0000:ffff:c0a8:0102" + a.family.should == Socket::AF_INET6 + a.ipv4_mapped?.should == true + b = a.native + b.to_s.should == "192.168.1.2" + b.family.should == Socket::AF_INET + b.ipv4_mapped?.should == false + + a = IPAddr.new("192.168.1.2") + b = a.ipv4_mapped + b.to_s.should == "::ffff:192.168.1.2" + b.family.should == Socket::AF_INET6 + end + +end + + diff --git a/spec/rubyspec/library/ipaddr/new_spec.rb b/spec/rubyspec/library/ipaddr/new_spec.rb new file mode 100644 index 0000000000..d0b91af87d --- /dev/null +++ b/spec/rubyspec/library/ipaddr/new_spec.rb @@ -0,0 +1,93 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'ipaddr' + +describe "IPAddr#new" do + it "initializes IPAddr" do + lambda{ IPAddr.new("3FFE:505:ffff::/48") }.should_not raise_error + lambda{ IPAddr.new("0:0:0:1::") }.should_not raise_error + lambda{ IPAddr.new("2001:200:300::/48") }.should_not raise_error + end + + it "initializes IPAddr ipv6 address with short notation" do + a = IPAddr.new + a.to_s.should == "::" + a.to_string.should == "0000:0000:0000:0000:0000:0000:0000:0000" + a.family.should == Socket::AF_INET6 + end + + it "initializes IPAddr ipv6 address with long notation" do + a = IPAddr.new("0123:4567:89ab:cdef:0ABC:DEF0:1234:5678") + a.to_s.should == "123:4567:89ab:cdef:abc:def0:1234:5678" + a.to_string.should == "0123:4567:89ab:cdef:0abc:def0:1234:5678" + a.family.should == Socket::AF_INET6 + end + + it "initializes IPAddr ipv6 address with / subnet notation" do + a = IPAddr.new("3ffe:505:2::/48") + a.to_s.should == "3ffe:505:2::" + a.to_string.should == "3ffe:0505:0002:0000:0000:0000:0000:0000" + a.family.should == Socket::AF_INET6 + a.ipv4?.should == false + a.ipv6?.should == true + a.inspect.should == "#<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0000/ffff:ffff:ffff:0000:0000:0000:0000:0000>" + end + + it "initializes IPAddr ipv6 address with mask subnet notation" do + a = IPAddr.new("3ffe:505:2::/ffff:ffff:ffff::") + a.to_s.should == "3ffe:505:2::" + a.to_string.should == "3ffe:0505:0002:0000:0000:0000:0000:0000" + a.family.should == Socket::AF_INET6 + end + + it "initializes IPAddr ipv4 address with all zeroes" do + a = IPAddr.new("0.0.0.0") + a.to_s.should == "0.0.0.0" + a.to_string.should == "0.0.0.0" + a.family.should == Socket::AF_INET + end + + it "initializes IPAddr ipv4 address" do + a = IPAddr.new("192.168.1.2") + a.to_s.should == "192.168.1.2" + a.to_string.should == "192.168.1.2" + a.family.should == Socket::AF_INET + a.ipv4?.should == true + a.ipv6?.should == false + end + + it "initializes IPAddr ipv4 address with / subnet notation" do + a = IPAddr.new("192.168.1.2/24") + a.to_s.should == "192.168.1.0" + a.to_string.should == "192.168.1.0" + a.family.should == Socket::AF_INET + a.inspect.should == "#<IPAddr: IPv4:192.168.1.0/255.255.255.0>" + end + + it "initializes IPAddr ipv4 address with subnet mask" do + a = IPAddr.new("192.168.1.2/255.255.255.0") + a.to_s.should == "192.168.1.0" + a.to_string.should == "192.168.1.0" + a.family.should == Socket::AF_INET + end + + it "initializes IPAddr ipv4 mapped address with subnet mask" do + a = IPAddr.new("::1:192.168.1.2/120") + a.to_s.should == "::1:c0a8:100" + a.to_string.should == "0000:0000:0000:0000:0000:0001:c0a8:0100" + a.family.should == Socket::AF_INET6 + end + + it "raises on incorrect IPAddr strings" do + [ + ["fe80::1%fxp0"], + ["::1/255.255.255.0"], + [IPAddr.new("::1").to_i], + ["::ffff:192.168.1.2/120", Socket::AF_INET], + ["[192.168.1.2]/120"], + ].each { |args| + lambda{ + IPAddr.new(*args) + }.should raise_error(ArgumentError) + } + end +end diff --git a/spec/rubyspec/library/ipaddr/operator_spec.rb b/spec/rubyspec/library/ipaddr/operator_spec.rb new file mode 100644 index 0000000000..3f54efd486 --- /dev/null +++ b/spec/rubyspec/library/ipaddr/operator_spec.rb @@ -0,0 +1,80 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'ipaddr' + +describe "IPAddr Operator" do + IN6MASK32 = "ffff:ffff::" + IN6MASK128 = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" + + before do + @in6_addr_any = IPAddr.new() + @a = IPAddr.new("3ffe:505:2::/48") + @b = IPAddr.new("0:0:0:1::") + @c = IPAddr.new(IN6MASK32) + end + + it "bitwises or" do + (@a | @b).to_s.should == "3ffe:505:2:1::" + a = @a + a |= @b + a.to_s.should == "3ffe:505:2:1::" + @a.to_s.should == "3ffe:505:2::" + (@a | 0x00000000000000010000000000000000).to_s.should == "3ffe:505:2:1::" + end + + it "bitwises and" do + (@a & @c).to_s.should == "3ffe:505::" + a = @a + a &= @c + a.to_s.should == "3ffe:505::" + @a.to_s.should == "3ffe:505:2::" + (@a & 0xffffffff000000000000000000000000).to_s.should == "3ffe:505::" + end + + it "bitshifts right" do + (@a >> 16).to_s.should == "0:3ffe:505:2::" + a = @a + a >>= 16 + a.to_s.should == "0:3ffe:505:2::" + @a.to_s.should == "3ffe:505:2::" + end + + it "bitshifts left" do + (@a << 16).to_s.should == "505:2::" + a = @a + a <<= 16 + a.to_s.should == "505:2::" + @a.to_s.should == "3ffe:505:2::" + end + + it "inverts" do + a = ~@in6_addr_any + a.to_s.should == IN6MASK128 + @in6_addr_any.to_s.should == "::" + end + + it "tests for equality" do + @a.should == IPAddr.new("3ffe:505:2::") + @a.should_not == IPAddr.new("3ffe:505:3::") + end + + it "sets a mask" do + a = @a.mask(32) + a.to_s.should == "3ffe:505::" + @a.to_s.should == "3ffe:505:2::" + end + + it "checks whether an addres is included in a range" do + @a.should include(IPAddr.new("3ffe:505:2::")) + @a.should include(IPAddr.new("3ffe:505:2::1")) + @a.should_not include(IPAddr.new("3ffe:505:3::")) + net1 = IPAddr.new("192.168.2.0/24") + net1.should include(IPAddr.new("192.168.2.0")) + net1.should include(IPAddr.new("192.168.2.255")) + net1.should_not include(IPAddr.new("192.168.3.0")) + # test with integer parameter + int = (192 << 24) + (168 << 16) + (2 << 8) + 13 + + net1.should include(int) + net1.should_not include(int+255) + end +end diff --git a/spec/rubyspec/library/ipaddr/reverse_spec.rb b/spec/rubyspec/library/ipaddr/reverse_spec.rb new file mode 100644 index 0000000000..dec5c65178 --- /dev/null +++ b/spec/rubyspec/library/ipaddr/reverse_spec.rb @@ -0,0 +1,27 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'ipaddr' + +describe "IPAddr#reverse" do + it "generates the reverse DNS lookup entry" do + IPAddr.new("3ffe:505:2::f").reverse.should == "f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.5.0.5.0.e.f.f.3.ip6.arpa" + IPAddr.new("192.168.2.1").reverse.should == "1.2.168.192.in-addr.arpa" + end +end + +describe "IPAddr#ip6_arpa" do + it "converts an IPv6 address into the reverse DNS lookup representation according to RFC3172" do + IPAddr.new("3ffe:505:2::f").ip6_arpa.should == "f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.5.0.5.0.e.f.f.3.ip6.arpa" + lambda{ + IPAddr.new("192.168.2.1").ip6_arpa + }.should raise_error(ArgumentError) + end +end + +describe "IPAddr#ip6_int" do + it "converts an IPv6 address into the reverse DNS lookup representation according to RFC1886" do + IPAddr.new("3ffe:505:2::f").ip6_int.should == "f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.5.0.5.0.e.f.f.3.ip6.int" + lambda{ + IPAddr.new("192.168.2.1").ip6_int + }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/ipaddr/to_s_spec.rb b/spec/rubyspec/library/ipaddr/to_s_spec.rb new file mode 100644 index 0000000000..30e5237436 --- /dev/null +++ b/spec/rubyspec/library/ipaddr/to_s_spec.rb @@ -0,0 +1,20 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'ipaddr' + +describe "IPAddr#to_s" do + + it "displays IPAddr using short notation" do + IPAddr.new("0:0:0:1::").to_s.should == "0:0:0:1::" + IPAddr.new("2001:200:300::/48").to_s.should == "2001:200:300::" + IPAddr.new("[2001:200:300::]/48").to_s.should == "2001:200:300::" + IPAddr.new("3ffe:505:2::1").to_s.should == "3ffe:505:2::1" + end + +end + +describe "IPAddr#to_string" do + it "displays an IPAddr using full notation" do + IPAddr.new("3ffe:505:2::1").to_string.should == "3ffe:0505:0002:0000:0000:0000:0000:0001" + end + +end |