summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreahlstrom <erik@negahok.se>2010-12-16 00:00:41 +0100
committereahlstrom <erik@negahok.se>2011-02-24 21:13:15 +0100
commita8ab168210bc294a442ba90e881175fb1308a093 (patch)
tree862ab27650a052b990ec918892e3d6a34ab9eed4
parent120a5093a115c730bb6941285f32b5b20eeb6b93 (diff)
downloadipaddress-a8ab168210bc294a442ba90e881175fb1308a093.tar.gz
bugfix: summarizing a small net with a large outside it's supernet raised error
-rw-r--r--lib/ipaddress/ipv4.rb24
-rw-r--r--test/ipaddress/ipv4_test.rb6
2 files changed, 19 insertions, 11 deletions
diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb
index dba33ac..bdfba97 100644
--- a/lib/ipaddress/ipv4.rb
+++ b/lib/ipaddress/ipv4.rb
@@ -538,6 +538,13 @@ module IPAddress;
end
#
+ # Checks that all other IPAddress::IPv4 is included in this object
+ #
+ def include_all?(*others)
+ others.find_all{|oth| include?(oth)}.length == others.length
+ end
+
+ #
# Checks if an IPv4 address objects belongs
# to a private network RFC1918
#
@@ -943,20 +950,15 @@ module IPAddress;
end
def aggregate(ip1,ip2)
- if ip1.include? ip2
- return [ip1]
+ return [ip1] if ip1.include? ip2
+
+ snet = ip1.supernet(ip1.prefix-1)
+ if snet.include_all?(ip1, ip2) && ((ip1.size + ip2.size) == snet.size)
+ return [snet]
else
- snet = ip1.supernet(ip1.prefix-1)
- arr1 = ip1.subnet(2**(ip2.prefix-ip1.prefix)).map{|i| i.to_string}
- arr2 = snet.subnet(2**(ip2.prefix-snet.prefix)).map{|i| i.to_string}
- if (arr2 - [ip2.to_string] - arr1).empty?
- return [snet]
- else
- return [ip1, ip2]
- end
+ return [ip1, ip2]
end
end
-
end # class IPv4
end # module IPAddress
diff --git a/test/ipaddress/ipv4_test.rb b/test/ipaddress/ipv4_test.rb
index 475e678..968ff70 100644
--- a/test/ipaddress/ipv4_test.rb
+++ b/test/ipaddress/ipv4_test.rb
@@ -456,6 +456,12 @@ class IPv4Test < Test::Unit::TestCase
result = ["10.0.1.0/24","10.10.2.0/24","172.16.0.0/23"]
assert_equal result, @klass.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
+ ips = [
+ @klass.new("10.0.0.12/30"),
+ @klass.new("10.0.100.0/24")
+ ]
+ result = ["10.0.0.12/30", "10.0.100.0/24"]
+ assert_equal result, @klass.summarize(*ips).map{|i| i.to_string}
end
def test_classmethod_parse_data