wgengine/wgcfg: fix validateEndpoints of empty string

Updates tailscale/corp#1238
This commit is contained in:
Brad Fitzpatrick 2021-01-30 11:17:55 -08:00
parent 914a486af6
commit 761188e5d2
2 changed files with 23 additions and 1 deletions

View File

@ -22,10 +22,14 @@ type ParseError struct {
} }
func (e *ParseError) Error() string { func (e *ParseError) Error() string {
return fmt.Sprintf("%s: %s", e.why, e.offender) return fmt.Sprintf("%s: %q", e.why, e.offender)
} }
func validateEndpoints(s string) error { func validateEndpoints(s string) error {
if s == "" {
// Otherwise strings.Split of the empty string produces [""].
return nil
}
vals := strings.Split(s, ",") vals := strings.Split(s, ",")
for _, val := range vals { for _, val := range vals {
_, _, err := parseEndpoint(val) _, _, err := parseEndpoint(val)

View File

@ -53,3 +53,21 @@ func TestParseEndpoint(t *testing.T) {
t.Error("Error was expected") t.Error("Error was expected")
} }
} }
func TestValidateEndpoints(t *testing.T) {
tests := []struct {
in string
want error
}{
{"", nil},
{"1.2.3.4:5", nil},
{"1.2.3.4:5,6.7.8.9:10", nil},
{",", &ParseError{why: "Missing port from endpoint", offender: ""}},
}
for _, tt := range tests {
got := validateEndpoints(tt.in)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("%q = %#v (%s); want %#v (%s)", tt.in, got, got, tt.want, tt.want)
}
}
}