diff --git a/checker/definition.go b/checker/definition.go index ecd4e6c..7d26d4a 100644 --- a/checker/definition.go +++ b/checker/definition.go @@ -34,8 +34,8 @@ func Definition() *sdk.CheckerDefinition { Id: "mode", Type: "string", Label: "Mode", - Default: "both", - Choices: []string{"c2s", "s2s", "both"}, + Default: string(ModeBoth), + Choices: validModes, }, { Id: "timeout", diff --git a/checker/interactive.go b/checker/interactive.go index 02c44b4..561ee91 100644 --- a/checker/interactive.go +++ b/checker/interactive.go @@ -3,6 +3,7 @@ package checker import ( "errors" "net/http" + "slices" "strconv" "strings" @@ -23,8 +24,8 @@ func (p *xmppProvider) RenderForm() []sdk.CheckerOptionField { Id: "mode", Type: "string", Label: "Mode", - Default: "both", - Choices: []string{"c2s", "s2s", "both"}, + Default: string(ModeBoth), + Choices: validModes, }, { Id: "timeout", @@ -46,12 +47,10 @@ func (p *xmppProvider) ParseForm(r *http.Request) (sdk.CheckerOptions, error) { opts := sdk.CheckerOptions{"domain": domain} if mode := strings.TrimSpace(r.FormValue("mode")); mode != "" { - switch mode { - case "c2s", "s2s", "both": - opts["mode"] = mode - default: + if !slices.Contains(validModes, mode) { return nil, errors.New("mode must be one of: c2s, s2s, both") } + opts["mode"] = mode } if to := strings.TrimSpace(r.FormValue("timeout")); to != "" { diff --git a/checker/rule.go b/checker/rule.go index 8c5853a..3b0886c 100644 --- a/checker/rule.go +++ b/checker/rule.go @@ -3,6 +3,7 @@ package checker import ( "context" "fmt" + "slices" "strings" sdk "git.happydns.org/checker-sdk-go/checker" @@ -24,7 +25,7 @@ func (r *xmppRule) Description() string { func (r *xmppRule) ValidateOptions(opts sdk.CheckerOptions) error { if v, ok := opts["mode"]; ok { - if s, ok := v.(string); ok && s != "" && s != "c2s" && s != "s2s" && s != "both" { + if s, ok := v.(string); ok && s != "" && !slices.Contains(validModes, s) { return fmt.Errorf(`mode must be "c2s", "s2s", or "both"`) } } diff --git a/checker/tls_related_test.go b/checker/tls_related_test.go index 13e13c0..dbd3a46 100644 --- a/checker/tls_related_test.go +++ b/checker/tls_related_test.go @@ -57,7 +57,8 @@ func TestRule_FoldsTLSCritIntoAggregate(t *testing.T) { }), }, } - state := (&xmppRule{}).Evaluate(context.Background(), obs, sdk.CheckerOptions{"domain": "example.com", "mode": "both"}) + states := (&xmppRule{}).Evaluate(context.Background(), obs, sdk.CheckerOptions{"domain": "example.com", "mode": "both"}) + state := states[0] if state.Status != sdk.StatusCrit { t.Fatalf("expected StatusCrit due to TLS chain invalid, got %s (%s)", state.Status, state.Message) } @@ -71,7 +72,8 @@ func TestRule_IgnoresUnrelatedTLSObs(t *testing.T) { xmpp: healthyXMPPData(), related: nil, } - state := (&xmppRule{}).Evaluate(context.Background(), obs, sdk.CheckerOptions{"domain": "example.com", "mode": "both"}) + states := (&xmppRule{}).Evaluate(context.Background(), obs, sdk.CheckerOptions{"domain": "example.com", "mode": "both"}) + state := states[0] if state.Status != sdk.StatusOK { t.Fatalf("expected StatusOK without related TLS issues, got %s (%s)", state.Status, state.Message) } diff --git a/checker/types.go b/checker/types.go index 062717c..94474b0 100644 --- a/checker/types.go +++ b/checker/types.go @@ -19,8 +19,11 @@ type XMPPMode string const ( ModeClient XMPPMode = "c2s" ModeServer XMPPMode = "s2s" + ModeBoth XMPPMode = "both" ) +var validModes = []string{string(ModeClient), string(ModeServer), string(ModeBoth)} + // XMPPData is the full observation stored per run. type XMPPData struct { Domain string `json:"domain"`