70 lines
2.1 KiB
Go
70 lines
2.1 KiB
Go
|
|
package starssh
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestExistsFallsBackFromPOSIXToPowerShell(t *testing.T) {
|
||
|
|
oldExecRequestRunner := execRequestRunner
|
||
|
|
t.Cleanup(func() {
|
||
|
|
execRequestRunner = oldExecRequestRunner
|
||
|
|
})
|
||
|
|
|
||
|
|
var calls []ExecRequest
|
||
|
|
execRequestRunner = func(s *StarSSH, ctx context.Context, req ExecRequest) (*ExecResult, error) {
|
||
|
|
calls = append(calls, req)
|
||
|
|
switch len(calls) {
|
||
|
|
case 1:
|
||
|
|
return &ExecResult{ExitCode: 127, Stderr: []byte("sh not found")}, nil
|
||
|
|
case 2:
|
||
|
|
return &ExecResult{Stdout: []byte("1\n")}, nil
|
||
|
|
default:
|
||
|
|
t.Fatalf("unexpected extra probe request: %+v", req)
|
||
|
|
return nil, nil
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
star := &StarSSH{}
|
||
|
|
if !star.Exists(`C:\Windows\System32`) {
|
||
|
|
t.Fatal("expected helper to succeed after PowerShell fallback")
|
||
|
|
}
|
||
|
|
if len(calls) != 2 {
|
||
|
|
t.Fatalf("expected two probe attempts, got %d", len(calls))
|
||
|
|
}
|
||
|
|
if calls[0].ShellDialect != ExecShellDialectRaw || !strings.HasPrefix(calls[0].Command, "sh -lc ") {
|
||
|
|
t.Fatalf("unexpected first probe request: %+v", calls[0])
|
||
|
|
}
|
||
|
|
if calls[1].ShellDialect != ExecShellDialectRaw || !strings.HasPrefix(strings.ToLower(calls[1].Command), "powershell.exe ") {
|
||
|
|
t.Fatalf("unexpected second probe request: %+v", calls[1])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestGetUserFallsBackToCMDWhenPowerShellVariantsFail(t *testing.T) {
|
||
|
|
oldExecRequestRunner := execRequestRunner
|
||
|
|
t.Cleanup(func() {
|
||
|
|
execRequestRunner = oldExecRequestRunner
|
||
|
|
})
|
||
|
|
|
||
|
|
var calls []ExecRequest
|
||
|
|
execRequestRunner = func(s *StarSSH, ctx context.Context, req ExecRequest) (*ExecResult, error) {
|
||
|
|
calls = append(calls, req)
|
||
|
|
if len(calls) < 5 {
|
||
|
|
return &ExecResult{ExitCode: 127, Stderr: []byte("command not found")}, nil
|
||
|
|
}
|
||
|
|
return &ExecResult{Stdout: []byte("HOST\\tester\r\n")}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
star := &StarSSH{}
|
||
|
|
if got := star.GetUser(); got != `HOST\tester` {
|
||
|
|
t.Fatalf("unexpected user after fallback: %q", got)
|
||
|
|
}
|
||
|
|
if len(calls) != 5 {
|
||
|
|
t.Fatalf("expected five probe attempts, got %d", len(calls))
|
||
|
|
}
|
||
|
|
if got := strings.ToLower(calls[4].Command); got != "cmd.exe /q /d /c whoami" {
|
||
|
|
t.Fatalf("unexpected final fallback command: %q", calls[4].Command)
|
||
|
|
}
|
||
|
|
}
|