Skip to content

Commit

Permalink
Fixed + Added Test Cases
Browse files Browse the repository at this point in the history
  • Loading branch information
brona committed Sep 2, 2013
1 parent f6d6822 commit 1a8ce18
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/ipaddress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions test/ipaddress/ipv6_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/ipaddress_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit 1a8ce18

Please sign in to comment.