-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from boris1993/ipv6
[bug fix] correctly comparing 2 IP addresses
- Loading branch information
Showing
12 changed files
with
140 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package common | ||
|
||
import "net" | ||
|
||
// CompareAddresses compares 2 given IP address string and see if they are the same IP address | ||
func CompareAddresses(address1 string, address2 string) bool { | ||
ipAddr1 := net.ParseIP(address1) | ||
ipAddr2 := net.ParseIP(address2) | ||
|
||
if ipAddr1 == nil || ipAddr2 == nil { | ||
return false | ||
} | ||
|
||
return ipAddr1.Equal(ipAddr2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package common | ||
|
||
import "testing" | ||
|
||
func TestCompareAddresses(t *testing.T) { | ||
var sameAddress bool | ||
var failTestMessage = "%s and %s should be the same address" | ||
|
||
sameAddress = CompareAddresses("192.168.1.1", "192.168.001.001") | ||
if !sameAddress { | ||
t.Errorf(failTestMessage, "192.168.1.1", "192.168.001.001") | ||
} | ||
|
||
sameAddress = CompareAddresses( | ||
"2001:0db8:0000:0000:0000:ff00:0042:8329", "2001:db8:0:0:0:ff00:42:8329") | ||
if !sameAddress { | ||
t.Errorf(failTestMessage, "2001:0db8:0000:0000:0000:ff00:0042:8329", "2001:db8:0:0:0:ff00:42:8329") | ||
} | ||
|
||
sameAddress = CompareAddresses( | ||
"2001:0db8:0000:0000:0000:ff00:0042:8329", "2001:db8::ff00:42:8329") | ||
if !sameAddress { | ||
t.Errorf(failTestMessage, "2001:0db8:0000:0000:0000:ff00:0042:8329", "2001:db8::ff00:42:8329") | ||
} | ||
|
||
sameAddress = CompareAddresses( | ||
"2001:db8:0:0:0:ff00:42:8329", "2001:db8::ff00:42:8329") | ||
if !sameAddress { | ||
t.Errorf(failTestMessage, "2001:db8:0:0:0:ff00:42:8329", "2001:db8::ff00:42:8329") | ||
} | ||
|
||
sameAddress = CompareAddresses("not valid address", "not valid address") | ||
if sameAddress { | ||
t.Error("Should return false when comparing invalid IP addresses") | ||
} | ||
} |
Oops, something went wrong.