Skip to content

Commit

Permalink
::1 should be detected as a valid loopback ip.
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusreza committed Sep 27, 2014
1 parent b05f1f7 commit be71e50
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/geocoder/ip_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Geocoder
class IpAddress < String

def loopback?
valid? and (self == "0.0.0.0" or self.match(/\A127\./))
valid? and (self == "0.0.0.0" or self.match(/\A127\./) or self == "::1")
end

def valid?
Expand All @@ -11,6 +11,8 @@ def valid?
((::ffff:)?((\d{1,3})\.){3}\d{1,3}) # Check for IPv4
| # .... Or
(\S+?(:\S+?){6}\S+) # Check for IPv6
| # .... Or
(::1) # IPv6 loopback
)\z
}x
!!self.match(ipregex)
Expand Down
2 changes: 2 additions & 0 deletions test/unit/ip_address_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def test_valid
assert Geocoder::IpAddress.new("666.65.123.94").valid? # technically invalid
assert Geocoder::IpAddress.new("::ffff:12.34.56.78").valid?
assert Geocoder::IpAddress.new("3ffe:0b00:0000:0000:0001:0000:0000:000a").valid?
assert Geocoder::IpAddress.new("::1").valid?
assert !Geocoder::IpAddress.new("232.65.123.94.43").valid?
assert !Geocoder::IpAddress.new("232.65.123").valid?
assert !Geocoder::IpAddress.new("::ffff:123.456.789").valid?
Expand All @@ -18,6 +19,7 @@ def test_valid
def test_loopback
assert Geocoder::IpAddress.new("0.0.0.0").loopback?
assert Geocoder::IpAddress.new("127.0.0.1").loopback?
assert Geocoder::IpAddress.new("::1").loopback?
assert !Geocoder::IpAddress.new("232.65.123.234").loopback?
assert !Geocoder::IpAddress.new("127 Main St.").loopback?
assert !Geocoder::IpAddress.new("John Doe\n127 Main St.\nAnywhere, USA").loopback?
Expand Down

0 comments on commit be71e50

Please sign in to comment.