github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/os/exec/exec_windows_test.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build windows
     6  
     7  package exec_test
     8  
     9  import (
    10  	"fmt"
    11  	"io"
    12  	"os"
    13  	"os/exec"
    14  	"strconv"
    15  	"strings"
    16  	"syscall"
    17  	"testing"
    18  )
    19  
    20  func init() {
    21  	registerHelperCommand("pipehandle", cmdPipeHandle)
    22  }
    23  
    24  func cmdPipeHandle(args ...string) {
    25  	handle, _ := strconv.ParseUint(args[0], 16, 64)
    26  	pipe := os.NewFile(uintptr(handle), "")
    27  	_, err := fmt.Fprint(pipe, args[1])
    28  	if err != nil {
    29  		fmt.Fprintf(os.Stderr, "writing to pipe failed: %v\n", err)
    30  		os.Exit(1)
    31  	}
    32  	pipe.Close()
    33  }
    34  
    35  func TestPipePassing(t *testing.T) {
    36  	t.Parallel()
    37  
    38  	r, w, err := os.Pipe()
    39  	if err != nil {
    40  		t.Error(err)
    41  	}
    42  	const marker = "arrakis, dune, desert planet"
    43  	childProc := helperCommand(t, "pipehandle", strconv.FormatUint(uint64(w.Fd()), 16), marker)
    44  	childProc.SysProcAttr = &syscall.SysProcAttr{AdditionalInheritedHandles: []syscall.Handle{syscall.Handle(w.Fd())}}
    45  	err = childProc.Start()
    46  	if err != nil {
    47  		t.Error(err)
    48  	}
    49  	w.Close()
    50  	response, err := io.ReadAll(r)
    51  	if err != nil {
    52  		t.Error(err)
    53  	}
    54  	r.Close()
    55  	if string(response) != marker {
    56  		t.Errorf("got %q; want %q", string(response), marker)
    57  	}
    58  	err = childProc.Wait()
    59  	if err != nil {
    60  		t.Error(err)
    61  	}
    62  }
    63  
    64  func TestNoInheritHandles(t *testing.T) {
    65  	t.Parallel()
    66  
    67  	cmd := exec.Command("cmd", "/c exit 88")
    68  	cmd.SysProcAttr = &syscall.SysProcAttr{NoInheritHandles: true}
    69  	err := cmd.Run()
    70  	exitError, ok := err.(*exec.ExitError)
    71  	if !ok {
    72  		t.Fatalf("got error %v; want ExitError", err)
    73  	}
    74  	if exitError.ExitCode() != 88 {
    75  		t.Fatalf("got exit code %d; want 88", exitError.ExitCode())
    76  	}
    77  }
    78  
    79  // start a child process without the user code explicitly starting
    80  // with a copy of the parent's SYSTEMROOT.
    81  // (See issue 25210.)
    82  func TestChildCriticalEnv(t *testing.T) {
    83  	t.Parallel()
    84  	cmd := helperCommand(t, "echoenv", "SYSTEMROOT")
    85  
    86  	// Explicitly remove SYSTEMROOT from the command's environment.
    87  	var env []string
    88  	for _, kv := range cmd.Environ() {
    89  		k, _, ok := strings.Cut(kv, "=")
    90  		if !ok || !strings.EqualFold(k, "SYSTEMROOT") {
    91  			env = append(env, kv)
    92  		}
    93  	}
    94  	cmd.Env = env
    95  
    96  	out, err := cmd.CombinedOutput()
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	if strings.TrimSpace(string(out)) == "" {
   101  		t.Error("no SYSTEMROOT found")
   102  	}
   103  }