| 1 | //go:build windows |
| 2 | |
| 3 | package builtin |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "os/exec" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "reasonix/internal/sandbox" |
| 14 | "reasonix/internal/tool" |
| 15 | ) |
| 16 | |
| 17 | func powerShellCommandAtUnits(t *testing.T, sh sandbox.Shell, units int) string { |
| 18 | t.Helper() |
| 19 | command := func(n int) string { return "Write-Output '" + strings.Repeat("中", n) + "'" } |
| 20 | base, _ := commandLineUnits(unconfinedShellArgv(sh, command(0))) |
| 21 | if units < base { |
| 22 | t.Fatalf("target %d is below the empty command's %d units", units, base) |
| 23 | } |
| 24 | c := command(units - base) |
| 25 | if got, _ := commandLineUnits(unconfinedShellArgv(sh, c)); got != units { |
| 26 | t.Fatalf("command line is %d units, want %d", got, units) |
| 27 | } |
| 28 | return c |
| 29 | } |
| 30 | |
| 31 | func TestBashRefusesCommandLineOverWindowsLimit(t *testing.T) { |
| 32 | powershell, err := exec.LookPath("powershell") |
| 33 | if err != nil { |
| 34 | t.Skip("powershell not found") |
| 35 | } |
| 36 | sh := sandbox.Shell{Kind: sandbox.ShellPowerShell, Path: powershell} |
| 37 | ctx := fullAccessBashTestContext(context.Background()) |
| 38 | |
| 39 | fits := powerShellCommandAtUnits(t, sh, windowsCommandLineMax) |
| 40 | args, _ := json.Marshal(map[string]any{"command": fits}) |
| 41 | res, err := (bash{shell: sh}).ExecuteDetailed(ctx, args) |
| 42 | if err != nil { |
| 43 | t.Fatalf("command at the limit failed: %v", err) |
| 44 | } |
| 45 | if !strings.Contains(res.Output, "中中中") { |
| 46 | t.Fatalf("command at the limit output = %.80q", res.Output) |
| 47 | } |
| 48 | |
| 49 | over := powerShellCommandAtUnits(t, sh, windowsCommandLineMax+1) |
| 50 | args, _ = json.Marshal(map[string]any{"command": over}) |
| 51 | res, err = (bash{shell: sh}).ExecuteDetailed(ctx, args) |
| 52 | if !errors.Is(err, errCommandLineTooLong) { |
| 53 | t.Fatalf("command over the limit error = %v, want errCommandLineTooLong", err) |
| 54 | } |
| 55 | if res.Execution == nil || res.Execution.State != tool.ShellStateNotRun || res.Execution.MutationRisk != tool.ShellMutationNotStarted { |
| 56 | t.Fatalf("execution = %+v, want not run and not started", res.Execution) |
| 57 | } |
| 58 | } |
| 59 |