diff options
author | Bryan C. Mills <bcmills@google.com> | 2022-04-22 23:33:16 -0400 |
---|---|---|
committer | Bryan Mills <bcmills@google.com> | 2022-04-26 14:49:07 +0000 |
commit | 7b31af0eae8ce369d5ffd16be1de0b2f0121e7c2 (patch) | |
tree | 7b7097d33aa1c4b0fbc23c85b1f95d4124c4662d /src/os/exec/exec_windows_test.go | |
parent | ad5eaa8c4cd952df0d4894f11ee0158a9a33a0f3 (diff) | |
download | go-7b31af0eae8ce369d5ffd16be1de0b2f0121e7c2.tar.gz go-7b31af0eae8ce369d5ffd16be1de0b2f0121e7c2.zip |
os/exec: use a TestMain to avoid hijacking stdout for helper commands
The previous implementation of helperCommand relied on running a
well-known Test function which implemented all known commands.
That not only added Skip noise in the test's output, but also (and
more importantly) meant that the commands could not write directly to
stdout in the usual way, since the testing package hijacks os.Stdout
for its own use.
The new implementation addresses the above issues, and also ensures
that all registered commands are actually used, reducing the risk of
an unused command sticking around after refactoring.
It also sets the subprocess environment variable directly in the test
process, instead of on each individual helper command's Env field,
allowing helper commands to be used without an explicit Env.
Updates #50599.
(Also for #50436.)
Change-Id: I189c7bed9a07cfe47a084b657b88575b1ee370b9
Reviewed-on: https://go-review.googlesource.com/c/go/+/401934
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/os/exec/exec_windows_test.go')
-rw-r--r-- | src/os/exec/exec_windows_test.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/os/exec/exec_windows_test.go b/src/os/exec/exec_windows_test.go index 8e31e47190..35ae0b0b8a 100644 --- a/src/os/exec/exec_windows_test.go +++ b/src/os/exec/exec_windows_test.go @@ -7,14 +7,31 @@ package exec_test import ( + "fmt" "io" "os" "os/exec" "strconv" + "strings" "syscall" "testing" ) +func init() { + registerHelperCommand("pipehandle", cmdPipeHandle) +} + +func cmdPipeHandle(args ...string) { + handle, _ := strconv.ParseUint(args[0], 16, 64) + pipe := os.NewFile(uintptr(handle), "") + _, err := fmt.Fprint(pipe, args[1]) + if err != nil { + fmt.Fprintf(os.Stderr, "writing to pipe failed: %v\n", err) + os.Exit(1) + } + pipe.Close() +} + func TestPipePassing(t *testing.T) { r, w, err := os.Pipe() if err != nil { @@ -54,3 +71,28 @@ func TestNoInheritHandles(t *testing.T) { t.Fatalf("got exit code %d; want 88", exitError.ExitCode()) } } + +// start a child process without the user code explicitly starting +// with a copy of the parent's SYSTEMROOT. +// (See issue 25210.) +func TestChildCriticalEnv(t *testing.T) { + cmd := helperCommand(t, "echoenv", "SYSTEMROOT") + + // Explicitly remove SYSTEMROOT from the command's environment. + var env []string + for _, kv := range cmd.Environ() { + k, _, ok := strings.Cut(kv, "=") + if !ok || !strings.EqualFold(k, "SYSTEMROOT") { + env = append(env, kv) + } + } + cmd.Env = env + + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatal(err) + } + if strings.TrimSpace(string(out)) == "" { + t.Error("no SYSTEMROOT found") + } +} |