opts: ValidateIPAddress: improve error, godoc, and tests
- document accepted values - add test-coverage for the function's behavior (including whitespace handling), and use sub-tests. - improve error-message to use uppercase for "IP", and to use a common prefix. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
3b423ea778
commit
1eadfb0e28
3 changed files with 76 additions and 20 deletions
|
@ -247,7 +247,7 @@ func TestValidateConfigurationErrors(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedErr: "1.1.1.1o is not an ip address",
|
expectedErr: "IP address is not correctly formatted: 1.1.1.1o",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "multiple DNS, invalid IP-address",
|
name: "multiple DNS, invalid IP-address",
|
||||||
|
@ -258,7 +258,7 @@ func TestValidateConfigurationErrors(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedErr: "1.1.1.1o is not an ip address",
|
expectedErr: "IP address is not correctly formatted: 1.1.1.1o",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "single DNSSearch",
|
name: "single DNSSearch",
|
||||||
|
|
|
@ -298,13 +298,18 @@ type ValidatorFctType func(val string) (string, error)
|
||||||
// ValidatorFctListType defines a validator function that returns a validated list of string and/or an error
|
// ValidatorFctListType defines a validator function that returns a validated list of string and/or an error
|
||||||
type ValidatorFctListType func(val string) ([]string, error)
|
type ValidatorFctListType func(val string) ([]string, error)
|
||||||
|
|
||||||
// ValidateIPAddress validates an Ip address.
|
// ValidateIPAddress validates if the given value is a correctly formatted
|
||||||
|
// IP address, and returns the value in normalized form. Leading and trailing
|
||||||
|
// whitespace is allowed, but it does not allow IPv6 addresses surrounded by
|
||||||
|
// square brackets ("[::1]").
|
||||||
|
//
|
||||||
|
// Refer to [net.ParseIP] for accepted formats.
|
||||||
func ValidateIPAddress(val string) (string, error) {
|
func ValidateIPAddress(val string) (string, error) {
|
||||||
ip := net.ParseIP(strings.TrimSpace(val))
|
ip := net.ParseIP(strings.TrimSpace(val))
|
||||||
if ip != nil {
|
if ip != nil {
|
||||||
return ip.String(), nil
|
return ip.String(), nil
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("%s is not an ip address", val)
|
return "", fmt.Errorf("IP address is not correctly formatted: %s", val)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidateDNSSearch validates domain for resolvconf search configuration.
|
// ValidateDNSSearch validates domain for resolvconf search configuration.
|
||||||
|
|
|
@ -10,24 +10,75 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestValidateIPAddress(t *testing.T) {
|
func TestValidateIPAddress(t *testing.T) {
|
||||||
if ret, err := ValidateIPAddress(`1.2.3.4`); err != nil || ret == "" {
|
tests := []struct {
|
||||||
t.Fatalf("ValidateIPAddress(`1.2.3.4`) got %s %s", ret, err)
|
doc string
|
||||||
|
input string
|
||||||
|
expectedOut string
|
||||||
|
expectedErr string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
doc: "IPv4 loopback",
|
||||||
|
input: `127.0.0.1`,
|
||||||
|
expectedOut: `127.0.0.1`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: "IPv4 loopback with whitespace",
|
||||||
|
input: ` 127.0.0.1 `,
|
||||||
|
expectedOut: `127.0.0.1`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: "IPv6 loopback long form",
|
||||||
|
input: `0:0:0:0:0:0:0:1`,
|
||||||
|
expectedOut: `::1`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: "IPv6 loopback",
|
||||||
|
input: `::1`,
|
||||||
|
expectedOut: `::1`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: "IPv6 loopback with whitespace",
|
||||||
|
input: ` ::1 `,
|
||||||
|
expectedOut: `::1`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: "IPv6 lowercase",
|
||||||
|
input: `2001:db8::68`,
|
||||||
|
expectedOut: `2001:db8::68`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: "IPv6 uppercase",
|
||||||
|
input: `2001:DB8::68`,
|
||||||
|
expectedOut: `2001:db8::68`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: "IPv6 with brackets",
|
||||||
|
input: `[::1]`,
|
||||||
|
expectedErr: `IP address is not correctly formatted: [::1]`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: "IPv4 partial",
|
||||||
|
input: `127`,
|
||||||
|
expectedErr: `IP address is not correctly formatted: 127`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: "random invalid string",
|
||||||
|
input: `random invalid string`,
|
||||||
|
expectedErr: `IP address is not correctly formatted: random invalid string`,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if ret, err := ValidateIPAddress(`127.0.0.1`); err != nil || ret == "" {
|
for _, tc := range tests {
|
||||||
t.Fatalf("ValidateIPAddress(`127.0.0.1`) got %s %s", ret, err)
|
tc := tc
|
||||||
}
|
t.Run(tc.input, func(t *testing.T) {
|
||||||
|
actualOut, actualErr := ValidateIPAddress(tc.input)
|
||||||
if ret, err := ValidateIPAddress(`::1`); err != nil || ret == "" {
|
assert.Check(t, is.Equal(tc.expectedOut, actualOut))
|
||||||
t.Fatalf("ValidateIPAddress(`::1`) got %s %s", ret, err)
|
if tc.expectedErr == "" {
|
||||||
}
|
assert.Check(t, actualErr)
|
||||||
|
} else {
|
||||||
if ret, err := ValidateIPAddress(`127`); err == nil || ret != "" {
|
assert.Check(t, is.Error(actualErr, tc.expectedErr))
|
||||||
t.Fatalf("ValidateIPAddress(`127`) got %s %s", ret, err)
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
if ret, err := ValidateIPAddress(`random invalid string`); err == nil || ret != "" {
|
|
||||||
t.Fatalf("ValidateIPAddress(`random invalid string`) got %s %s", ret, err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue