diff --git a/engine/jvm/processes.go b/engine/jvm/processes.go index 6160c4a..d87b66d 100644 --- a/engine/jvm/processes.go +++ b/engine/jvm/processes.go @@ -31,7 +31,7 @@ func findImposterJvmProcesses() ([]engine.ManagedMock, error) { logger.Tracef("found JVM Imposter process %d: %v", p.Pid, cmdline) port := determinePort(cmdline) if port == 0 { - if determineTlsEnabled(cmdline) { + if isTlsEnabled(cmdline) { port = 8443 } else { port = 8080 @@ -64,25 +64,21 @@ func isImposterProc(cmdline []string, procName string) bool { func determinePort(cmdline []string) int { portRaw := readArg(cmdline, "listenPort", "-l") if portRaw != "" { - port, err := strconv.Atoi(portRaw) - if err != nil { - return 0 + if port, err := strconv.Atoi(portRaw); err == nil { + return port } - return port } return 0 } -// determineTlsEnabled parses the command line arguments to the JVM process +// isTlsEnabled parses the command line arguments to the JVM process // to determine if TLS is enabled -func determineTlsEnabled(cmdline []string) bool { +func isTlsEnabled(cmdline []string) bool { tlsRaw := readArg(cmdline, "tlsEnabled", "-t") if tlsRaw != "" { - tls, err := strconv.ParseBool(tlsRaw) - if err != nil { - return false + if tls, err := strconv.ParseBool(tlsRaw); err == nil { + return tls } - return tls } return false } diff --git a/engine/jvm/processes_test.go b/engine/jvm/processes_test.go new file mode 100644 index 0000000..896beed --- /dev/null +++ b/engine/jvm/processes_test.go @@ -0,0 +1,51 @@ +package jvm + +import "testing" + +func Test_readArg(t *testing.T) { + type args struct { + cmdline []string + longArg string + shortArg string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "combined form", + args: args{ + cmdline: []string{"--arg=VAL"}, + longArg: "arg", + shortArg: "a", + }, + want: "VAL", + }, + { + name: "separate form", + args: args{ + cmdline: []string{"--arg", "VAL"}, + longArg: "arg", + shortArg: "a", + }, + want: "VAL", + }, + { + name: "separate form with short arg", + args: args{ + cmdline: []string{"-a", "VAL"}, + longArg: "arg", + shortArg: "a", + }, + want: "VAL", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := readArg(tt.args.cmdline, tt.args.longArg, tt.args.shortArg); got != tt.want { + t.Errorf("readArg() = %v, want %v", got, tt.want) + } + }) + } +}