From 40eedcb2f0450c7a717660eb13b097fc839e1db3 Mon Sep 17 00:00:00 2001 From: puneeth_aditya_5656 Date: Mon, 4 May 2026 20:21:58 +0530 Subject: [PATCH] fix start command port validation Signed-off-by: puneeth_aditya_5656 --- cmd/start.go | 11 +++++++++++ cmd/start_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 cmd/start_test.go diff --git a/cmd/start.go b/cmd/start.go index f36c22c..92caac9 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "log" + "strconv" "github.com/microcks/microcks-cli/pkg/config" "github.com/microcks/microcks-cli/pkg/connectors" @@ -33,6 +34,8 @@ microcks start --driver [driver you wnat either 'docker' or 'podman'] # Define name of your microcks container/instance microcks start --name [name of you container/instance]`, Run: func(cmd *cobra.Command, args []string) { + err := validateStartPort(hostPort) + errors.CheckError(err) configFile := globalClientOpts.ConfigPath localConfig, err := config.ReadLocalConfig(configFile) @@ -150,3 +153,11 @@ microcks start --name [name of you container/instance]`, startCmd.Flags().StringVar(&driver, "driver", "docker", "use --driver to change driver from docker to podman") return startCmd } + +func validateStartPort(port string) error { + portNum, err := strconv.Atoi(port) + if err != nil || portNum < 1 || portNum > 65535 { + return fmt.Errorf("--port must be a number between 1 and 65535, got %q", port) + } + return nil +} diff --git a/cmd/start_test.go b/cmd/start_test.go new file mode 100644 index 0000000..ede8be1 --- /dev/null +++ b/cmd/start_test.go @@ -0,0 +1,35 @@ +package cmd + +import ( + "strconv" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestValidateStartPort(t *testing.T) { + tests := []struct { + name string + port string + wantErr bool + }{ + {name: "lowest valid port", port: "1"}, + {name: "default port", port: "8585"}, + {name: "highest valid port", port: "65535"}, + {name: "non numeric port", port: "abc", wantErr: true}, + {name: "zero port", port: "0", wantErr: true}, + {name: "negative port", port: "-1", wantErr: true}, + {name: "port above range", port: "65536", wantErr: true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := validateStartPort(tt.port) + if tt.wantErr { + require.EqualError(t, err, "--port must be a number between 1 and 65535, got "+strconv.Quote(tt.port)) + return + } + require.NoError(t, err) + }) + } +}