Make IsValidIP return error strings

Also treat 0.0.0.0 as special, like loopback and multicast.
This commit is contained in:
Tim Hockin
2016-01-04 09:49:39 -08:00
parent 14bece550f
commit 87c1fc50a8
4 changed files with 34 additions and 15 deletions

View File

@@ -210,8 +210,11 @@ func IsValidPortName(port string) []string {
}
// IsValidIP tests that the argument is a valid IP address.
func IsValidIP(value string) bool {
return net.ParseIP(value) != nil
func IsValidIP(value string) []string {
if net.ParseIP(value) == nil {
return []string{"must be a valid IP address, (e.g. 10.9.8.7)"}
}
return nil
}
const percentFmt string = "[0-9]+%"

View File

@@ -293,8 +293,8 @@ func TestIsValidIP(t *testing.T) {
"0.0.0.0",
}
for _, val := range goodValues {
if !IsValidIP(val) {
t.Errorf("expected true for %q", val)
if msgs := IsValidIP(val); len(msgs) != 0 {
t.Errorf("expected true for %q: %v", val, msgs)
}
}
@@ -306,7 +306,7 @@ func TestIsValidIP(t *testing.T) {
"a",
}
for _, val := range badValues {
if IsValidIP(val) {
if msgs := IsValidIP(val); len(msgs) == 0 {
t.Errorf("expected false for %q", val)
}
}