initial commit
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package ipparser
|
||||
|
||||
import (
|
||||
"net"
|
||||
)
|
||||
|
||||
func ValidIP4(ipAddress string) bool {
|
||||
testInput := net.ParseIP(ipAddress)
|
||||
if testInput == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return (testInput.To4() != nil)
|
||||
}
|
||||
|
||||
func ValidIP6(ip6Address string) bool {
|
||||
testInputIP6 := net.ParseIP(ip6Address)
|
||||
if testInputIP6 == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return (testInputIP6.To16() != nil)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package ipparser
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidIP4ToReturnTrueOnValidAddress(t *testing.T) {
|
||||
result := ValidIP4("1.2.3.4")
|
||||
|
||||
if result != true {
|
||||
t.Fatalf("Expected ValidIP(1.2.3.4) to be true but got false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidIP4ToReturnFalseOnInvalidAddress(t *testing.T) {
|
||||
result := ValidIP4("abcd")
|
||||
|
||||
if result == true {
|
||||
t.Fatalf("Expected ValidIP(abcd) to be false but got true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidIP4ToReturnFalseOnEmptyAddress(t *testing.T) {
|
||||
result := ValidIP4("")
|
||||
|
||||
if result == true {
|
||||
t.Fatalf("Expected ValidIP() to be false but got true")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user