From 1410f5ffa5a51f5e09f349662744d5621d6caf18 Mon Sep 17 00:00:00 2001 From: Bronislav Robenek Date: Mon, 2 Sep 2013 17:09:44 +0200 Subject: [PATCH 1/5] Fixed IPv6 Validation according to https://gist.github.com/cpetschnig/294476 --- lib/ipaddress.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/ipaddress.rb b/lib/ipaddress.rb index 0b7bd00..c58b9c8 100644 --- a/lib/ipaddress.rb +++ b/lib/ipaddress.rb @@ -136,14 +136,8 @@ def self.valid_ipv4_netmask?(addr) # #=> false # def self.valid_ipv6?(addr) - # IPv6 (normal) - return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*\Z/ =~ addr - return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr - return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr - # IPv6 (IPv4 compat) - return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:/ =~ addr && valid_ipv4?($') - return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_ipv4?($') - return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_ipv4?($') + # https://gist.github.com/cpetschnig/294476 + return true if /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/ =~ addr false end From f6d6822a5d7b95c6b2994e291b267fc820049be6 Mon Sep 17 00:00:00 2001 From: Bronislav Robenek Date: Mon, 2 Sep 2013 17:30:47 +0200 Subject: [PATCH 2/5] Fixed ERROR: 'rake/rdoctask' is obsolete and no longer supported. Use 'rdoc/task' (available in RDoc 2.4.2+) instead. /ipaddress/Rakefile:47:in `' --- Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index b668d77..b7dbf64 100644 --- a/Rakefile +++ b/Rakefile @@ -44,7 +44,7 @@ end task :default => :test -require 'rake/rdoctask' +require 'rdoc/task' Rake::RDocTask.new do |rdoc| if File.exist?('VERSION.yml') config = YAML.load(File.read('VERSION.yml')) From 1a8ce1826be6b91f6a634a47a308272d2f72c957 Mon Sep 17 00:00:00 2001 From: Bronislav Robenek Date: Mon, 2 Sep 2013 17:31:46 +0200 Subject: [PATCH 3/5] Fixed + Added Test Cases --- lib/ipaddress.rb | 5 +++-- test/ipaddress/ipv6_test.rb | 7 ++++--- test/ipaddress_test.rb | 2 ++ 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/ipaddress.rb b/lib/ipaddress.rb index c58b9c8..7631952 100644 --- a/lib/ipaddress.rb +++ b/lib/ipaddress.rb @@ -135,9 +135,10 @@ def self.valid_ipv4_netmask?(addr) # IPAddress::valid_ipv6? "2002::DEAD::BEEF" # #=> false # - def self.valid_ipv6?(addr) + def self.valid_ipv6?(addr) # https://gist.github.com/cpetschnig/294476 - return true if /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/ =~ addr + # http://forums.intermapper.com/viewtopic.php?t=452 + return true if /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/ =~ addr false end diff --git a/test/ipaddress/ipv6_test.rb b/test/ipaddress/ipv6_test.rb index 9d9ff3e..d4c2344 100644 --- a/test/ipaddress/ipv6_test.rb +++ b/test/ipaddress/ipv6_test.rb @@ -31,7 +31,8 @@ def setup "1080::8:800:200C:417A" => 21932261930451111902915077091070067066} @invalid_ipv6 = [":1:2:3:4:5:6:7", - ":1:2:3:4:5:6:7"] + ":1:2:3:4:5:6:7", + "2002:516:2:200"] @networks = { "2001:db8:1:1:1:1:1:1/32" => "2001:db8::/32", @@ -257,7 +258,7 @@ def test_classmethod_expand compressed = "2001:db8:0:cd30::" expanded = "2001:0db8:0000:cd30:0000:0000:0000:0000" assert_equal expanded, @klass.expand(compressed) - assert_not_equal expanded, @klass.expand("2001:0db8:0:cd3") + assert_not_equal expanded, @klass.expand("2001:0db8:0::cd3") assert_not_equal expanded, @klass.expand("2001:0db8::cd30") assert_not_equal expanded, @klass.expand("2001:0db8::cd3") end @@ -266,7 +267,7 @@ def test_classmethod_compress compressed = "2001:db8:0:cd30::" expanded = "2001:0db8:0000:cd30:0000:0000:0000:0000" assert_equal compressed, @klass.compress(expanded) - assert_not_equal compressed, @klass.compress("2001:0db8:0:cd3") + assert_not_equal compressed, @klass.compress("2001:0db8:0::cd3") assert_not_equal compressed, @klass.compress("2001:0db8::cd30") assert_not_equal compressed, @klass.compress("2001:0db8::cd3") end diff --git a/test/ipaddress_test.rb b/test/ipaddress_test.rb index ed72aed..8e2bb66 100644 --- a/test/ipaddress_test.rb +++ b/test/ipaddress_test.rb @@ -43,6 +43,8 @@ def test_module_method_valid? assert_equal false, IPAddress::valid?("10.0.0") assert_equal false, IPAddress::valid?("10.0") assert_equal false, IPAddress::valid?("2002:::1") + assert_equal false, IPAddress::valid?("2002:516:2:200") + end def test_module_method_valid_ipv4_netmark? From 94f8e44e2049bec7ba8702d15d6a1d91a95f5db9 Mon Sep 17 00:00:00 2001 From: Steven Schmid Date: Fri, 13 Sep 2013 14:47:06 +0200 Subject: [PATCH 4/5] Update README.rdoc Small fix --- README.rdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index bcb66f7..2146384 100644 --- a/README.rdoc +++ b/README.rdoc @@ -489,7 +489,7 @@ instance the following two networks: These two networks can be expressed using only one IP address network if we change the prefix. Let Ruby do the work: - IPAddress::IPv4::summarize(ip1,ip2).to_string + IPAddress::IPv4::summarize(ip1,ip2).map(&:to_string) #=> "172.16.10.0/23" We note how the network "172.16.10.0/23" includes all the From 8da374226a780a1ed5bd458ef27c6c3fd4602f4d Mon Sep 17 00:00:00 2001 From: bluemonk Date: Mon, 7 Oct 2013 06:11:50 +0200 Subject: [PATCH 5/5] Updates Rakefile from obsolete rake/rdoctask to rdoc/task --- Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index b668d77..b7dbf64 100644 --- a/Rakefile +++ b/Rakefile @@ -44,7 +44,7 @@ end task :default => :test -require 'rake/rdoctask' +require 'rdoc/task' Rake::RDocTask.new do |rdoc| if File.exist?('VERSION.yml') config = YAML.load(File.read('VERSION.yml'))