summaryrefslogtreecommitdiff
path: root/spec/ruby/library/ipaddr
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/ipaddr')
-rw-r--r--spec/ruby/library/ipaddr/hton_spec.rb30
-rw-r--r--spec/ruby/library/ipaddr/ipv4_conversion_spec.rb44
-rw-r--r--spec/ruby/library/ipaddr/new_spec.rb92
-rw-r--r--spec/ruby/library/ipaddr/operator_spec.rb82
-rw-r--r--spec/ruby/library/ipaddr/reverse_spec.rb27
-rw-r--r--spec/ruby/library/ipaddr/to_s_spec.rb20
6 files changed, 295 insertions, 0 deletions
diff --git a/spec/ruby/library/ipaddr/hton_spec.rb b/spec/ruby/library/ipaddr/hton_spec.rb
new file mode 100644
index 0000000000..9c0b821abf
--- /dev/null
+++ b/spec/ruby/library/ipaddr/hton_spec.rb
@@ -0,0 +1,30 @@
+require_relative '../../spec_helper'
+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/ruby/library/ipaddr/ipv4_conversion_spec.rb b/spec/ruby/library/ipaddr/ipv4_conversion_spec.rb
new file mode 100644
index 0000000000..1128c16dd2
--- /dev/null
+++ b/spec/ruby/library/ipaddr/ipv4_conversion_spec.rb
@@ -0,0 +1,44 @@
+require_relative '../../spec_helper'
+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.should.ipv4_compat?
+ b = a.native
+ b.to_s.should == "192.168.1.2"
+ b.family.should == Socket::AF_INET
+ b.should_not.ipv4_compat?
+
+ 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.should.ipv4_mapped?
+ b = a.native
+ b.to_s.should == "192.168.1.2"
+ b.family.should == Socket::AF_INET
+ b.should_not.ipv4_mapped?
+
+ 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/ruby/library/ipaddr/new_spec.rb b/spec/ruby/library/ipaddr/new_spec.rb
new file mode 100644
index 0000000000..7fba2b372e
--- /dev/null
+++ b/spec/ruby/library/ipaddr/new_spec.rb
@@ -0,0 +1,92 @@
+require_relative '../../spec_helper'
+require 'ipaddr'
+
+describe "IPAddr#new" do
+ it "initializes IPAddr" do
+ ->{ IPAddr.new("3FFE:505:ffff::/48") }.should_not.raise
+ ->{ IPAddr.new("0:0:0:1::") }.should_not.raise
+ ->{ IPAddr.new("2001:200:300::/48") }.should_not.raise
+ 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.should_not.ipv4?
+ a.should.ipv6?
+ 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.should.ipv4?
+ a.should_not.ipv6?
+ 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
+ [
+ ["::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|
+ ->{
+ IPAddr.new(*args)
+ }.should.raise(ArgumentError)
+ }
+ end
+end
diff --git a/spec/ruby/library/ipaddr/operator_spec.rb b/spec/ruby/library/ipaddr/operator_spec.rb
new file mode 100644
index 0000000000..3337d22300
--- /dev/null
+++ b/spec/ruby/library/ipaddr/operator_spec.rb
@@ -0,0 +1,82 @@
+require_relative '../../spec_helper'
+require 'ipaddr'
+
+describe "IPAddr Operator" do
+ before do
+ @in6_addr_any = IPAddr.new()
+ @a = IPAddr.new("3ffe:505:2::/48")
+ @b = IPAddr.new("0:0:0:1::")
+ @c = IPAddr.new("ffff:ffff::")
+ 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 == "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
+ @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
+
+ # https://bugs.ruby-lang.org/issues/12799
+ it "tests for equality correctly if object cannot be converted to IPAddr" do
+ IPAddr.new("1.1.1.1").should_not == "sometext"
+ 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 address 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/ruby/library/ipaddr/reverse_spec.rb b/spec/ruby/library/ipaddr/reverse_spec.rb
new file mode 100644
index 0000000000..9bda60ca70
--- /dev/null
+++ b/spec/ruby/library/ipaddr/reverse_spec.rb
@@ -0,0 +1,27 @@
+require_relative '../../spec_helper'
+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"
+ ->{
+ IPAddr.new("192.168.2.1").ip6_arpa
+ }.should.raise(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"
+ ->{
+ IPAddr.new("192.168.2.1").ip6_int
+ }.should.raise(ArgumentError)
+ end
+end
diff --git a/spec/ruby/library/ipaddr/to_s_spec.rb b/spec/ruby/library/ipaddr/to_s_spec.rb
new file mode 100644
index 0000000000..2a9a027909
--- /dev/null
+++ b/spec/ruby/library/ipaddr/to_s_spec.rb
@@ -0,0 +1,20 @@
+require_relative '../../spec_helper'
+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