test/ipaddress/ipv4_test.rb in ipaddress-0.8.0 vs test/ipaddress/ipv4_test.rb in ipaddress-0.8.2
- old
+ new
@@ -1,8 +1,8 @@
require 'test_helper'
-class IPv4Test < Test::Unit::TestCase
+class IPv4Test < Minitest::Test
def setup
@klass = IPAddress::IPv4
@valid_ipv4 = {
@@ -24,70 +24,80 @@
@netmask_values = {
"0.0.0.0/0" => "0.0.0.0",
"10.0.0.0/8" => "255.0.0.0",
"172.16.0.0/16" => "255.255.0.0",
"192.168.0.0/24" => "255.255.255.0",
- "192.168.100.4/30" => "255.255.255.252"}
+ "192.168.100.4/30" => "255.255.255.252",
+ "192.168.12.4/32" => "255.255.255.255"}
@decimal_values ={
"0.0.0.0/0" => 0,
"10.0.0.0/8" => 167772160,
"172.16.0.0/16" => 2886729728,
"192.168.0.0/24" => 3232235520,
"192.168.100.4/30" => 3232261124}
-
+
+ @hex_values = {
+ "10.0.0.0" => "0a000000",
+ "172.16.5.4" => "ac100504",
+ "192.168.100.4" => "c0a86404",
+ }
+
@ip = @klass.new("172.16.10.1/24")
@network = @klass.new("172.16.10.0/24")
@broadcast = {
"10.0.0.0/8" => "10.255.255.255/8",
"172.16.0.0/16" => "172.16.255.255/16",
"192.168.0.0/24" => "192.168.0.255/24",
- "192.168.100.4/30" => "192.168.100.7/30"}
+ "192.168.100.4/30" => "192.168.100.7/30",
+ "192.168.12.3/31" => "255.255.255.255/31",
+ "10.0.0.1/32" => "10.0.0.1/32"}
@networks = {
"10.5.4.3/8" => "10.0.0.0/8",
"172.16.5.4/16" => "172.16.0.0/16",
"192.168.4.3/24" => "192.168.4.0/24",
- "192.168.100.5/30" => "192.168.100.4/30"}
+ "192.168.100.5/30" => "192.168.100.4/30",
+ "192.168.1.3/31" => "192.168.1.2/31",
+ "192.168.2.5/32" => "192.168.2.5/32"}
@class_a = @klass.new("10.0.0.1/8")
@class_b = @klass.new("172.16.0.1/16")
@class_c = @klass.new("192.168.0.1/24")
@classful = {
"10.1.1.1" => 8,
"150.1.1.1" => 16,
"200.1.1.1" => 24 }
+
+ @in_range = {
+ "10.32.0.1" => ["10.32.0.253", 253],
+ "192.0.0.0" => ["192.1.255.255", 131072]
+ }
end
def test_initialize
@valid_ipv4.keys.each do |i|
ip = @klass.new(i)
assert_instance_of @klass, ip
end
assert_instance_of IPAddress::Prefix32, @ip.prefix
- assert_raise (ArgumentError) do
+ assert_raises (ArgumentError) do
@klass.new
end
- assert_nothing_raised do
- @klass.new "10.0.0.0/8"
- end
end
def test_initialize_format_error
@invalid_ipv4.each do |i|
- assert_raise(ArgumentError) {@klass.new(i)}
+ assert_raises(ArgumentError) {@klass.new(i)}
end
- assert_raise (ArgumentError) {@klass.new("10.0.0.0/asd")}
+ assert_raises (ArgumentError) {@klass.new("10.0.0.0/asd")}
end
def test_initialize_without_prefix
- assert_nothing_raised do
- @klass.new("10.10.0.0")
- end
ip = @klass.new("10.10.0.0")
assert_instance_of IPAddress::Prefix32, ip.prefix
assert_equal 32, ip.prefix.to_i
end
@@ -103,15 +113,19 @@
ip = @klass.new("10.1.2.3/8")
assert_equal ip.octets, [10,1,2,3]
end
def test_initialize_should_require_ip
- assert_raise(ArgumentError) { @klass.new }
+ assert_raises(ArgumentError) { @klass.new }
end
def test_method_data
- assert_equal "\254\020\n\001", @ip.data
+ if RUBY_VERSION < "2.0"
+ assert_equal "\254\020\n\001", @ip.data
+ else
+ assert_equal "\xAC\x10\n\x01".b, @ip.data
+ end
end
def test_method_to_string
@valid_ipv4.each do |arg,attr|
ip = @klass.new(arg)
@@ -138,14 +152,26 @@
ip = @klass.new(addr)
assert_equal int, ip.to_u32
end
end
+ def test_method_to_hex
+ @hex_values.each do |addr,hex|
+ ip = @klass.new(addr)
+ assert_equal hex, ip.to_hex
+ end
+ end
+
def test_method_network?
assert_equal true, @network.network?
assert_equal false, @ip.network?
end
+
+ def test_one_address_network
+ network = @klass.new("172.16.10.1/32")
+ assert_equal false, network.network?
+ end
def test_method_broadcast
@broadcast.each do |addr,bcast|
ip = @klass.new(addr)
assert_instance_of @klass, ip.broadcast
@@ -171,19 +197,31 @@
assert_instance_of @klass, ip.first
assert_equal "192.168.100.1", ip.first.to_s
ip = @klass.new("192.168.100.50/24")
assert_instance_of @klass, ip.first
assert_equal "192.168.100.1", ip.first.to_s
+ ip = @klass.new("192.168.100.50/32")
+ assert_instance_of @klass, ip.first
+ assert_equal "192.168.100.50", ip.first.to_s
+ ip = @klass.new("192.168.100.50/31")
+ assert_instance_of @klass, ip.first
+ assert_equal "192.168.100.50", ip.first.to_s
end
def test_method_last
ip = @klass.new("192.168.100.0/24")
assert_instance_of @klass, ip.last
assert_equal "192.168.100.254", ip.last.to_s
ip = @klass.new("192.168.100.50/24")
assert_instance_of @klass, ip.last
assert_equal "192.168.100.254", ip.last.to_s
+ ip = @klass.new("192.168.100.50/32")
+ assert_instance_of @klass, ip.last
+ assert_equal "192.168.100.50", ip.last.to_s
+ ip = @klass.new("192.168.100.50/31")
+ assert_instance_of @klass, ip.last
+ assert_equal "192.168.100.51", ip.last.to_s
end
def test_method_each_host
ip = @klass.new("10.0.0.1/29")
arr = []
@@ -376,12 +414,12 @@
ip.netmask = "255.255.255.0"
assert_equal 24, ip.prefix.to_i
end
def test_method_split
- assert_raise(ArgumentError) {@ip.split(0)}
- assert_raise(ArgumentError) {@ip.split(257)}
+ assert_raises(ArgumentError) {@ip.split(0)}
+ assert_raises(ArgumentError) {@ip.split(257)}
assert_equal @ip.network, @ip.split(1).first
arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27",
"172.16.10.96/27", "172.16.10.128/27", "172.16.10.160/27",
@@ -407,24 +445,23 @@
arr = ["172.16.10.0/24"]
assert_equal arr, @network.split(1).map {|s| s.to_string}
end
def test_method_subnet
- assert_raise(ArgumentError) {@network.subnet(23)}
- assert_raise(ArgumentError) {@network.subnet(33)}
- assert_nothing_raised {@ip.subnet(30)}
+ assert_raises(ArgumentError) {@network.subnet(23)}
+ assert_raises(ArgumentError) {@network.subnet(33)}
arr = ["172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/26",
"172.16.10.192/26"]
assert_equal arr, @network.subnet(26).map {|s| s.to_string}
arr = ["172.16.10.0/25", "172.16.10.128/25"]
assert_equal arr, @network.subnet(25).map {|s| s.to_string}
arr = ["172.16.10.0/24"]
assert_equal arr, @network.subnet(24).map {|s| s.to_string}
end
def test_method_supernet
- assert_raise(ArgumentError) {@ip.supernet(24)}
+ assert_raises(ArgumentError) {@ip.supernet(24)}
assert_equal "0.0.0.0/0", @ip.supernet(0).to_string
assert_equal "0.0.0.0/0", @ip.supernet(-2).to_string
assert_equal "172.16.10.0/23", @ip.supernet(23).to_string
assert_equal "172.16.8.0/22", @ip.supernet(22).to_string
end
@@ -522,11 +559,42 @@
@classful.each do |ip,prefix|
res = @klass.parse_classful(ip)
assert_equal prefix, res.prefix
assert_equal "#{ip}/#{prefix}", res.to_string
end
- assert_raise(ArgumentError){ @klass.parse_classful("192.168.256.257") }
+ assert_raises(ArgumentError){ @klass.parse_classful("192.168.256.257") }
end
+ def test_network_split
+ @classful.each do |ip,net|
+ x = @klass.new("#{ip}/#{net}")
+ assert_equal x.split(1).length, 1
+ assert_equal x.split(2).length, 2
+ assert_equal x.split(32).length, 32
+ assert_equal x.split(256).length, 256
+ end
+ end
+
+ def test_in_range
+ @in_range.each do |s,d|
+ ip = @klass.new(s)
+ assert_equal ip.to(d[0]).length, d[1]
+ end
+ end
+
+ def test_octect_updates
+ ip = @klass.new("10.0.1.15/32")
+ ip[1] = 15
+ assert_equal "10.15.1.15/32", ip.to_string
+
+ ip = @klass.new("172.16.100.1")
+ ip[3] = 200
+ assert_equal "172.16.100.200/32", ip.to_string
+
+ ip = @klass.new("192.168.199.0/24")
+ ip[2] = 200
+ assert_equal "192.168.200.0/24", ip.to_string
+ end
+
end # class IPv4Test