diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/ipaddress/ipv4_test.rb | 11 | ||||
-rw-r--r-- | test/ipaddress/ipv6_test.rb | 79 |
2 files changed, 73 insertions, 17 deletions
diff --git a/test/ipaddress/ipv4_test.rb b/test/ipaddress/ipv4_test.rb index e8b299f..70c9c9c 100644 --- a/test/ipaddress/ipv4_test.rb +++ b/test/ipaddress/ipv4_test.rb @@ -134,7 +134,7 @@ class IPv4Test < Test::Unit::TestCase def test_method_bits ip = @klass.new("127.0.0.1") - assert_equal ip.bits, "01111111000000000000000000000001" + assert_equal "01111111000000000000000000000001", ip.bits end def test_method_first @@ -255,6 +255,15 @@ class IPv4Test < Test::Unit::TestCase assert_equal 9, ip1 - ip2 end + def test_method_plus + ip1 = @klass.new("172.16.10.1/24") + ip2 = @klass.new("172.16.11.2/24") + assert_equal "172.16.10.0/23", (ip1 + ip2).to_s + ip2 = @klass.new("172.16.12.2/24") + assert_equal [ip1.network.to_s,ip2.network.to_s], (ip1 + ip2).map{|i| i.to_s} + end + + def test_method_netmask_equal ip = @klass.new("10.1.1.1/16") assert_equal 16, ip.prefix.to_i diff --git a/test/ipaddress/ipv6_test.rb b/test/ipaddress/ipv6_test.rb index afc3b45..6549442 100644 --- a/test/ipaddress/ipv6_test.rb +++ b/test/ipaddress/ipv6_test.rb @@ -11,8 +11,28 @@ class IPv6Test < Test::Unit::TestCase "ff01:0:0:0:0:0:0:101" => "ff01::101", "0:0:0:0:0:0:0:1" => "::1", "0:0:0:0:0:0:0:0" => "::"} + + @valid_ipv6 = { # Kindly taken from the python IPy library + "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210" => 338770000845734292534325025077361652240, + "1080:0000:0000:0000:0008:0800:200C:417A" => 21932261930451111902915077091070067066, + "1080:0:0:0:8:800:200C:417A" => 21932261930451111902915077091070067066, + "1080:0::8:800:200C:417A" => 21932261930451111902915077091070067066, + "1080::8:800:200C:417A" => 21932261930451111902915077091070067066, + "FF01:0:0:0:0:0:0:43" => 338958331222012082418099330867817087043, + "FF01:0:0::0:0:43" => 338958331222012082418099330867817087043, + "FF01::43" => 338958331222012082418099330867817087043, + "0:0:0:0:0:0:0:1" => 1, + "0:0:0::0:0:1" => 1, + "::1" => 1, + "0:0:0:0:0:0:0:0" => 0, + "0:0:0::0:0:0" => 0, + "::" => 0, + "1080:0:0:0:8:800:200C:417A" => 21932261930451111902915077091070067066, + "1080::8:800:200C:417A" => 21932261930451111902915077091070067066} + @ip = @klass.new "2001:db8::8:800:200c:417a/64" + @arr = [8193,3512,0,0,8,2048,8204,16762] end @@ -27,8 +47,7 @@ class IPv6Test < Test::Unit::TestCase end def test_attribute_groups - arr = [8193,3512,0,0,8,2048,8204,16762] - assert_equal arr, @ip.groups + assert_equal @arr, @ip.groups end def test_method_hexs @@ -37,8 +56,30 @@ class IPv6Test < Test::Unit::TestCase end def test_method_to_i - bigint = 42540766411282592856906245548098208122 - assert_equal bigint, @ip.to_i + @valid_ipv6.each do |ip,num| + assert_equal num, @klass.new(ip).to_i + end + end + + def test_method_bits + bits = "0010000000000001000011011011100000000000000000000" + + "000000000000000000000000000100000001000000000000010000" + + "0000011000100000101111010" + assert_equal bits, @ip.bits + end + + def test_method_prefix=() + ip = @klass.new "2001:db8::8:800:200c:417a" + assert_equal 128, ip.prefix + ip.prefix = 64 + assert_equal 64, ip.prefix + assert_equal "2001:db8::8:800:200c:417a/64", ip.to_s + end + + def test_method_group + @arr.each_with_index do |val,index| + assert_equal val, @ip[index] + end end def test_method_to_hex @@ -68,16 +109,16 @@ class IPv6Test < Test::Unit::TestCase assert_equal "1::1", @klass.new("1:0:0:0:0:0:0:1").compressed end - def test_method_unspecified? - assert_equal true, @klass.new("::").unspecified? - assert_equal false, @ip.unspecified? - end - - def test_method_loopback? - assert_equal true, @klass.new("::1").loopback? - assert_equal false, @ip.loopback? - end - + def test_method_unspecified? + assert_equal true, @klass.new("::").unspecified? + assert_equal false, @ip.unspecified? + end + + def test_method_loopback? + assert_equal true, @klass.new("::1").loopback? + assert_equal false, @ip.loopback? + end + def test_classmethod_expand compressed = "2001:db8:0:cd30::" expanded = "2001:0db8:0000:cd30:0000:0000:0000:0000" @@ -86,7 +127,7 @@ class IPv6Test < Test::Unit::TestCase assert_not_equal expanded, @klass.expand("2001:0db8::cd30") assert_not_equal expanded, @klass.expand("2001:0db8::cd3") end - + def test_classmethod_compress compressed = "2001:db8:0:cd30::" expanded = "2001:0db8:0000:cd30:0000:0000:0000:0000" @@ -119,7 +160,13 @@ class IPv6Test < Test::Unit::TestCase assert_equal "2001:0db8:0000:0000:0008:0800:200c:417a", ip.address assert_equal "2001:db8::8:800:200c:417a/128", ip.to_s end - + + def test_classhmethod_parse_u128 + @valid_ipv6.each do |ip,num| + assert_equal @klass.new(ip).to_s, @klass.parse_u128(num).to_s + end + end + end # class IPv4Test |