wgengine/wgcfg: fix validateEndpoints of empty string

Updates tailscale/corp#1238
pull/1244/head
Brad Fitzpatrick 3 years ago
parent 914a486af6
commit 761188e5d2

@ -22,10 +22,14 @@ type ParseError struct {
}
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 {
if s == "" {
// Otherwise strings.Split of the empty string produces [""].
return nil
}
vals := strings.Split(s, ",")
for _, val := range vals {
_, _, err := parseEndpoint(val)

@ -53,3 +53,21 @@ func TestParseEndpoint(t *testing.T) {
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)
}
}
}

Loading…
Cancel
Save