summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Rodrigues <mikebrodrigues@gmail.com>2014-07-01 19:27:58 -0700
committerMichael Rodrigues <mikebrodrigues@gmail.com>2014-07-01 19:27:58 -0700
commitb085273a41659b5c919b78178066a1c7fcce19e6 (patch)
treeea93cc30baf692bec3a2d59346d9a2e937ed1f8d
parentc2704ae93df103bbd14a2daddd8a3fe5b2f9cf4b (diff)
downloadipaddress-b085273a41659b5c919b78178066a1c7fcce19e6.tar.gz
#first, #last, #network, and #broadcast properly handle /31 (RFC3021) and /32
-rw-r--r--lib/ipaddress/ipv4.rb31
-rw-r--r--test/ipaddress/ipv4_test.rb17
2 files changed, 40 insertions, 8 deletions
diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb
index a798a24..d68dfa9 100644
--- a/lib/ipaddress/ipv4.rb
+++ b/lib/ipaddress/ipv4.rb
@@ -294,9 +294,16 @@ module IPAddress;
# #=> "172.16.10.255"
#
def broadcast
- self.class.parse_u32(broadcast_u32, @prefix)
+ case
+ when prefix <= 30
+ self.class.parse_u32(broadcast_u32, @prefix)
+ when prefix == 31
+ self.class.parse_u32(-1, @prefix)
+ when prefix == 32
+ return nil
+ end
end
-
+
#
# Checks if the IP address is actually a network
#
@@ -348,7 +355,14 @@ module IPAddress;
# #=> "192.168.100.1"
#
def first
- self.class.parse_u32(network_u32+1, @prefix)
+ case
+ when prefix <= 30
+ self.class.parse_u32(network_u32+1, @prefix)
+ when prefix == 31
+ self.class.parse_u32(network_u32, @prefix)
+ when prefix == 32
+ return self
+ end
end
#
@@ -373,7 +387,14 @@ module IPAddress;
# #=> "192.168.100.254"
#
def last
- self.class.parse_u32(broadcast_u32-1, @prefix)
+ case
+ when prefix <= 30
+ self.class.parse_u32(broadcast_u32-1, @prefix)
+ when prefix == 31
+ self.class.parse_u32(broadcast_u32, @prefix)
+ when prefix == 32
+ return self
+ end
end
#
@@ -461,7 +482,7 @@ module IPAddress;
return prefix <=> oth.prefix if to_u32 == oth.to_u32
to_u32 <=> oth.to_u32
end
-
+
#
# Returns the number of IP addresses included
# in the network. It also counts the network
diff --git a/test/ipaddress/ipv4_test.rb b/test/ipaddress/ipv4_test.rb
index 38cac74..6518bf1 100644
--- a/test/ipaddress/ipv4_test.rb
+++ b/test/ipaddress/ipv4_test.rb
@@ -26,7 +26,8 @@ class IPv4Test < Test::Unit::TestCase
"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,
@@ -42,13 +43,17 @@ class IPv4Test < Test::Unit::TestCase
"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" => nil}
@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")
@@ -181,6 +186,9 @@ class IPv4Test < Test::Unit::TestCase
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
@@ -193,6 +201,9 @@ class IPv4Test < Test::Unit::TestCase
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