IPv4#summarize bug

17 views
Skip to first unread message

Paweł Krześniak

unread,
Oct 29, 2010, 3:47:05 AM10/29/10
to Ruby IPAddress
hi

IPv4#summarize doesn't work for in following corner case:

--8<--
diff --git a/test/ipaddress/ipv4_test.rb b/test/ipaddress/ipv4_test.rb
index c81ee8c..13423f2 100644
--- a/test/ipaddress/ipv4_test.rb
+++ b/test/ipaddress/ipv4_test.rb
@@ -453,6 +453,11 @@ 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}

+ ip1 = @klass.new("172.16.0.0/31")
+ ip2 = @klass.new("10.10.2.1/32")
+ result = ["10.10.2.1/32","172.16.0.0/31"]
+ assert_equal result, @klass.summarize(ip1,ip2).map{|i|
i.to_string}
+ assert_equal result, @klass.summarize(ip2,ip1).map{|i|
i.to_string}
end

def test_classmethod_parse_data
[imo@label ipaddress]$ rake test
(in /home/imo/src/ipaddress)
/usr/bin/ruby -I"lib:lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/
lib/rake/rake_test_loader.rb" "test/ipaddress_test.rb" "test/ipaddress/
prefix_test.rb" "test/ipaddress/extensions/extensions_test.rb" "test/
ipaddress/ipv4_test.rb" "test/ipaddress/ipv6_test.rb"
Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/
rake_test_loader
Started
..........E......................................................................................
Finished in 0.094536 seconds.

1) Error:
test_classmethod_summarize(IPv4Test):
ArgumentError: Value 2 out of range
./test/../lib/ipaddress/ipv4.rb:604:in `subnet'
./test/../lib/ipaddress/ipv4.rb:950:in `aggregate'
./test/../lib/ipaddress/ipv4.rb:677:in `+'
./test/../lib/ipaddress/ipv4.rb:866:in `summarize'
./test/ipaddress/ipv4_test.rb:459:in `test_classmethod_summarize'

97 tests, 401 assertions, 0 failures, 1 errors
rake aborted!
Command failed with status (1): [/usr/bin/ruby -I"lib:lib:test" "/usr/
lib/r...]

(See full trace by running task with --trace)
--8<--

solution is to sort only by prefix (not by logic defined by <=>
method) in IPv4#aggregate:

--8<--
diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb
index dba33ac..97d237d 100644
--- a/lib/ipaddress/ipv4.rb
+++ b/lib/ipaddress/ipv4.rb
@@ -674,7 +674,7 @@ module IPAddress;
# #=> ["10.0.0.0/24","10.0.2.0/24"]
#
def +(oth)
- aggregate(*[self,oth].sort.map{|i| i.network})
+ aggregate(*[self,oth].sort {|x,y| x.prefix <=> y.prefix}.map{|
i| i.network})
end

#
diff --git a/test/ipaddress/ipv4_test.rb b/test/ipaddress/ipv4_test.rb
index c81ee8c..3c97c76 100644
--- a/test/ipaddress/ipv4_test.rb
+++ b/test/ipaddress/ipv4_test.rb
@@ -453,6 +453,17 @@ 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}

+ ip1 = @klass.new("172.16.0.0/31")
+ ip2 = @klass.new("10.10.2.1/32")
+ result = ["10.10.2.1/32","172.16.0.0/31"]
+ assert_equal result, @klass.summarize(ip1,ip2).map{|i|
i.to_string}
+ assert_equal result, @klass.summarize(ip2,ip1).map{|i|
i.to_string}
+
+ ip1 = @klass.new("172.16.0.0/32")
+ ip2 = @klass.new("10.10.2.1/32")
+ result = ["10.10.2.1/32","172.16.0.0/32"]
+ assert_equal result, @klass.summarize(ip1,ip2).map{|i|
i.to_string}
+ assert_equal result, @klass.summarize(ip2,ip1).map{|i|
i.to_string}
end

def test_classmethod_parse_data
[imo@label ipaddress]$ rake test
(in /home/imo/src/ipaddress)
/usr/bin/ruby -I"lib:lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/
lib/rake/rake_test_loader.rb" "test/ipaddress_test.rb" "test/ipaddress/
prefix_test.rb" "test/ipaddress/extensions/extensions_test.rb" "test/
ipaddress/ipv4_test.rb" "test/ipaddress/ipv6_test.rb"
Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/
rake_test_loader
Started
.................................................................................................
Finished in 0.095278 seconds.

97 tests, 405 assertions, 0 failures, 0 errors
--8<--


cheers,

Marco Ceresa

unread,
Mar 29, 2011, 2:25:12 AM3/29/11
to ruby-ip...@googlegroups.com, Paweł Krześniak
2010/10/29 Paweł Krześniak <pawel.k...@gmail.com>:

> hi
>
> IPv4#summarize doesn't work for in following corner case:

Hello Pawel,

thanks very much for you bug report, and apologies for the late
answer. I'm back to 100% ipaddress developing now.

IPAddress 0.7.5 (to be released soon) fixes this issue.

Regards,
Marco

Reply all
Reply to author
Forward
0 new messages