From 5ff984d66a67bad68be78059be81f444e0b121c8 Mon Sep 17 00:00:00 2001 From: Aditya Date: Sun, 24 May 2026 13:26:15 +0530 Subject: [PATCH] fix: validate numeric part of --waitFor flag in test command Previously --waitFor=sec or --waitFor=0sec would pass validation but cause unexpected behavior during execution. Now proper validation ensures: - Numeric part must be present (e.g. 5sec not sec) - Numeric part must be a valid integer - Value must be greater than zero Closes #430 Signed-off-by: Aditya --- cmd/test.go | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/cmd/test.go b/cmd/test.go index 70ecb4f2..c3a05b8e 100644 --- a/cmd/test.go +++ b/cmd/test.go @@ -75,10 +75,33 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command { } // Validate presence and values of flags. - if !strings.HasSuffix(waitFor, "milli") && !strings.HasSuffix(waitFor, "sec") && !strings.HasSuffix(waitFor, "min") { - fmt.Println("--waitFor format is wrong. Accepted units are: milli, sec, min (e.g. 500milli, 30sec, 5min)") - os.Exit(1) - } +if !strings.HasSuffix(waitFor, "milli") && !strings.HasSuffix(waitFor, "sec") && !strings.HasSuffix(waitFor, "min") { + fmt.Println("--waitFor format is wrong. Accepted units are: milli, sec, min (e.g. 500milli, 30sec, 5min)") + os.Exit(1) +} + +// Validate that numeric part of --waitFor is present and greater than zero. +var numericPart string +if strings.HasSuffix(waitFor, "milli") { + numericPart = waitFor[:len(waitFor)-5] +} else if strings.HasSuffix(waitFor, "sec") { + numericPart = waitFor[:len(waitFor)-3] +} else if strings.HasSuffix(waitFor, "min") { + numericPart = waitFor[:len(waitFor)-3] +} +if numericPart == "" { + fmt.Printf("--waitFor value %q is missing numeric part (e.g. 5sec, 500milli, 1min)\n", waitFor) + os.Exit(1) +} +numericVal, err := strconv.ParseInt(numericPart, 0, 64) +if err != nil { + fmt.Printf("--waitFor value %q has invalid numeric part: must be a valid integer\n", waitFor) + os.Exit(1) +} +if numericVal <= 0 { + fmt.Printf("--waitFor value %q must be greater than zero\n", waitFor) + os.Exit(1) +} // Collect optional HTTPS transport flags. config.InsecureTLS = globalClientOpts.InsecureTLS @@ -170,7 +193,7 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command { } var testResultID string - testResultID, err := mc.CreateTestResult(serviceRef, testEndpoint, runnerType, secretName, waitForMilliseconds, filteredOperations, operationsHeaders, oAuth2Context) + testResultID, err = mc.CreateTestResult(serviceRef, testEndpoint, runnerType, secretName, waitForMilliseconds, filteredOperations, operationsHeaders, oAuth2Context) if err != nil { fmt.Printf("Got error when invoking Microcks client creating Test: %s", err) os.Exit(1)