Home | History | Annotate | Download | only in exec
      1 // Copyright 2009 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 // Use an external test to avoid os/exec -> net/http -> crypto/x509 -> os/exec
      6 // circular dependency on non-cgo darwin.
      7 
      8 package exec_test
      9 
     10 import (
     11 	"bufio"
     12 	"bytes"
     13 	"context"
     14 	"fmt"
     15 	"internal/testenv"
     16 	"io"
     17 	"io/ioutil"
     18 	"log"
     19 	"net"
     20 	"net/http"
     21 	"net/http/httptest"
     22 	"os"
     23 	"os/exec"
     24 	"path/filepath"
     25 	"runtime"
     26 	"strconv"
     27 	"strings"
     28 	"testing"
     29 	"time"
     30 )
     31 
     32 func helperCommandContext(t *testing.T, ctx context.Context, s ...string) (cmd *exec.Cmd) {
     33 	testenv.MustHaveExec(t)
     34 
     35 	cs := []string{"-test.run=TestHelperProcess", "--"}
     36 	cs = append(cs, s...)
     37 	if ctx != nil {
     38 		cmd = exec.CommandContext(ctx, os.Args[0], cs...)
     39 	} else {
     40 		cmd = exec.Command(os.Args[0], cs...)
     41 	}
     42 	cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
     43 	return cmd
     44 }
     45 
     46 func helperCommand(t *testing.T, s ...string) *exec.Cmd {
     47 	return helperCommandContext(t, nil, s...)
     48 }
     49 
     50 func TestEcho(t *testing.T) {
     51 	bs, err := helperCommand(t, "echo", "foo bar", "baz").Output()
     52 	if err != nil {
     53 		t.Errorf("echo: %v", err)
     54 	}
     55 	if g, e := string(bs), "foo bar baz\n"; g != e {
     56 		t.Errorf("echo: want %q, got %q", e, g)
     57 	}
     58 }
     59 
     60 func TestCommandRelativeName(t *testing.T) {
     61 	testenv.MustHaveExec(t)
     62 
     63 	// Run our own binary as a relative path
     64 	// (e.g. "_test/exec.test") our parent directory.
     65 	base := filepath.Base(os.Args[0]) // "exec.test"
     66 	dir := filepath.Dir(os.Args[0])   // "/tmp/go-buildNNNN/os/exec/_test"
     67 	if dir == "." {
     68 		t.Skip("skipping; running test at root somehow")
     69 	}
     70 	parentDir := filepath.Dir(dir) // "/tmp/go-buildNNNN/os/exec"
     71 	dirBase := filepath.Base(dir)  // "_test"
     72 	if dirBase == "." {
     73 		t.Skipf("skipping; unexpected shallow dir of %q", dir)
     74 	}
     75 
     76 	cmd := exec.Command(filepath.Join(dirBase, base), "-test.run=TestHelperProcess", "--", "echo", "foo")
     77 	cmd.Dir = parentDir
     78 	cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
     79 
     80 	out, err := cmd.Output()
     81 	if err != nil {
     82 		t.Errorf("echo: %v", err)
     83 	}
     84 	if g, e := string(out), "foo\n"; g != e {
     85 		t.Errorf("echo: want %q, got %q", e, g)
     86 	}
     87 }
     88 
     89 func TestCatStdin(t *testing.T) {
     90 	// Cat, testing stdin and stdout.
     91 	input := "Input string\nLine 2"
     92 	p := helperCommand(t, "cat")
     93 	p.Stdin = strings.NewReader(input)
     94 	bs, err := p.Output()
     95 	if err != nil {
     96 		t.Errorf("cat: %v", err)
     97 	}
     98 	s := string(bs)
     99 	if s != input {
    100 		t.Errorf("cat: want %q, got %q", input, s)
    101 	}
    102 }
    103 
    104 func TestEchoFileRace(t *testing.T) {
    105 	cmd := helperCommand(t, "echo")
    106 	stdin, err := cmd.StdinPipe()
    107 	if err != nil {
    108 		t.Fatalf("StdinPipe: %v", err)
    109 	}
    110 	if err := cmd.Start(); err != nil {
    111 		t.Fatalf("Start: %v", err)
    112 	}
    113 	wrote := make(chan bool)
    114 	go func() {
    115 		defer close(wrote)
    116 		fmt.Fprint(stdin, "echo\n")
    117 	}()
    118 	if err := cmd.Wait(); err != nil {
    119 		t.Fatalf("Wait: %v", err)
    120 	}
    121 	<-wrote
    122 }
    123 
    124 func TestCatGoodAndBadFile(t *testing.T) {
    125 	// Testing combined output and error values.
    126 	bs, err := helperCommand(t, "cat", "/bogus/file.foo", "exec_test.go").CombinedOutput()
    127 	if _, ok := err.(*exec.ExitError); !ok {
    128 		t.Errorf("expected *exec.ExitError from cat combined; got %T: %v", err, err)
    129 	}
    130 	s := string(bs)
    131 	sp := strings.SplitN(s, "\n", 2)
    132 	if len(sp) != 2 {
    133 		t.Fatalf("expected two lines from cat; got %q", s)
    134 	}
    135 	errLine, body := sp[0], sp[1]
    136 	if !strings.HasPrefix(errLine, "Error: open /bogus/file.foo") {
    137 		t.Errorf("expected stderr to complain about file; got %q", errLine)
    138 	}
    139 	if !strings.Contains(body, "func TestHelperProcess(t *testing.T)") {
    140 		t.Errorf("expected test code; got %q (len %d)", body, len(body))
    141 	}
    142 }
    143 
    144 func TestNoExistBinary(t *testing.T) {
    145 	// Can't run a non-existent binary
    146 	err := exec.Command("/no-exist-binary").Run()
    147 	if err == nil {
    148 		t.Error("expected error from /no-exist-binary")
    149 	}
    150 }
    151 
    152 func TestExitStatus(t *testing.T) {
    153 	// Test that exit values are returned correctly
    154 	cmd := helperCommand(t, "exit", "42")
    155 	err := cmd.Run()
    156 	want := "exit status 42"
    157 	switch runtime.GOOS {
    158 	case "plan9":
    159 		want = fmt.Sprintf("exit status: '%s %d: 42'", filepath.Base(cmd.Path), cmd.ProcessState.Pid())
    160 	}
    161 	if werr, ok := err.(*exec.ExitError); ok {
    162 		if s := werr.Error(); s != want {
    163 			t.Errorf("from exit 42 got exit %q, want %q", s, want)
    164 		}
    165 	} else {
    166 		t.Fatalf("expected *exec.ExitError from exit 42; got %T: %v", err, err)
    167 	}
    168 }
    169 
    170 func TestPipes(t *testing.T) {
    171 	check := func(what string, err error) {
    172 		if err != nil {
    173 			t.Fatalf("%s: %v", what, err)
    174 		}
    175 	}
    176 	// Cat, testing stdin and stdout.
    177 	c := helperCommand(t, "pipetest")
    178 	stdin, err := c.StdinPipe()
    179 	check("StdinPipe", err)
    180 	stdout, err := c.StdoutPipe()
    181 	check("StdoutPipe", err)
    182 	stderr, err := c.StderrPipe()
    183 	check("StderrPipe", err)
    184 
    185 	outbr := bufio.NewReader(stdout)
    186 	errbr := bufio.NewReader(stderr)
    187 	line := func(what string, br *bufio.Reader) string {
    188 		line, _, err := br.ReadLine()
    189 		if err != nil {
    190 			t.Fatalf("%s: %v", what, err)
    191 		}
    192 		return string(line)
    193 	}
    194 
    195 	err = c.Start()
    196 	check("Start", err)
    197 
    198 	_, err = stdin.Write([]byte("O:I am output\n"))
    199 	check("first stdin Write", err)
    200 	if g, e := line("first output line", outbr), "O:I am output"; g != e {
    201 		t.Errorf("got %q, want %q", g, e)
    202 	}
    203 
    204 	_, err = stdin.Write([]byte("E:I am error\n"))
    205 	check("second stdin Write", err)
    206 	if g, e := line("first error line", errbr), "E:I am error"; g != e {
    207 		t.Errorf("got %q, want %q", g, e)
    208 	}
    209 
    210 	_, err = stdin.Write([]byte("O:I am output2\n"))
    211 	check("third stdin Write 3", err)
    212 	if g, e := line("second output line", outbr), "O:I am output2"; g != e {
    213 		t.Errorf("got %q, want %q", g, e)
    214 	}
    215 
    216 	stdin.Close()
    217 	err = c.Wait()
    218 	check("Wait", err)
    219 }
    220 
    221 const stdinCloseTestString = "Some test string."
    222 
    223 // Issue 6270.
    224 func TestStdinClose(t *testing.T) {
    225 	check := func(what string, err error) {
    226 		if err != nil {
    227 			t.Fatalf("%s: %v", what, err)
    228 		}
    229 	}
    230 	cmd := helperCommand(t, "stdinClose")
    231 	stdin, err := cmd.StdinPipe()
    232 	check("StdinPipe", err)
    233 	// Check that we can access methods of the underlying os.File.`
    234 	if _, ok := stdin.(interface {
    235 		Fd() uintptr
    236 	}); !ok {
    237 		t.Error("can't access methods of underlying *os.File")
    238 	}
    239 	check("Start", cmd.Start())
    240 	go func() {
    241 		_, err := io.Copy(stdin, strings.NewReader(stdinCloseTestString))
    242 		check("Copy", err)
    243 		// Before the fix, this next line would race with cmd.Wait.
    244 		check("Close", stdin.Close())
    245 	}()
    246 	check("Wait", cmd.Wait())
    247 }
    248 
    249 // Issue 17647.
    250 // It used to be the case that TestStdinClose, above, would fail when
    251 // run under the race detector. This test is a variant of TestStdinClose
    252 // that also used to fail when run under the race detector.
    253 // This test is run by cmd/dist under the race detector to verify that
    254 // the race detector no longer reports any problems.
    255 func TestStdinCloseRace(t *testing.T) {
    256 	cmd := helperCommand(t, "stdinClose")
    257 	stdin, err := cmd.StdinPipe()
    258 	if err != nil {
    259 		t.Fatalf("StdinPipe: %v", err)
    260 	}
    261 	if err := cmd.Start(); err != nil {
    262 		t.Fatalf("Start: %v", err)
    263 	}
    264 	go func() {
    265 		if err := cmd.Process.Kill(); err != nil {
    266 			t.Errorf("Kill: %v", err)
    267 		}
    268 	}()
    269 	go func() {
    270 		// Send the wrong string, so that the child fails even
    271 		// if the other goroutine doesn't manage to kill it first.
    272 		// This test is to check that the race detector does not
    273 		// falsely report an error, so it doesn't matter how the
    274 		// child process fails.
    275 		io.Copy(stdin, strings.NewReader("unexpected string"))
    276 		if err := stdin.Close(); err != nil {
    277 			t.Errorf("stdin.Close: %v", err)
    278 		}
    279 	}()
    280 	if err := cmd.Wait(); err == nil {
    281 		t.Fatalf("Wait: succeeded unexpectedly")
    282 	}
    283 }
    284 
    285 // Issue 5071
    286 func TestPipeLookPathLeak(t *testing.T) {
    287 	fd0, lsof0 := numOpenFDS(t)
    288 	for i := 0; i < 4; i++ {
    289 		cmd := exec.Command("something-that-does-not-exist-binary")
    290 		cmd.StdoutPipe()
    291 		cmd.StderrPipe()
    292 		cmd.StdinPipe()
    293 		if err := cmd.Run(); err == nil {
    294 			t.Fatal("unexpected success")
    295 		}
    296 	}
    297 	for triesLeft := 3; triesLeft >= 0; triesLeft-- {
    298 		open, lsof := numOpenFDS(t)
    299 		fdGrowth := open - fd0
    300 		if fdGrowth > 2 {
    301 			if triesLeft > 0 {
    302 				// Work around what appears to be a race with Linux's
    303 				// proc filesystem (as used by lsof). It seems to only
    304 				// be eventually consistent. Give it awhile to settle.
    305 				// See golang.org/issue/7808
    306 				time.Sleep(100 * time.Millisecond)
    307 				continue
    308 			}
    309 			t.Errorf("leaked %d fds; want ~0; have:\n%s\noriginally:\n%s", fdGrowth, lsof, lsof0)
    310 		}
    311 		break
    312 	}
    313 }
    314 
    315 func numOpenFDS(t *testing.T) (n int, lsof []byte) {
    316 	if runtime.GOOS == "android" {
    317 		// Android's stock lsof does not obey the -p option,
    318 		// so extra filtering is needed. (golang.org/issue/10206)
    319 		return numOpenFDsAndroid(t)
    320 	}
    321 
    322 	lsof, err := exec.Command("lsof", "-b", "-n", "-p", strconv.Itoa(os.Getpid())).Output()
    323 	if err != nil {
    324 		t.Skip("skipping test; error finding or running lsof")
    325 	}
    326 	return bytes.Count(lsof, []byte("\n")), lsof
    327 }
    328 
    329 func numOpenFDsAndroid(t *testing.T) (n int, lsof []byte) {
    330 	raw, err := exec.Command("lsof").Output()
    331 	if err != nil {
    332 		t.Skip("skipping test; error finding or running lsof")
    333 	}
    334 
    335 	// First find the PID column index by parsing the first line, and
    336 	// select lines containing pid in the column.
    337 	pid := []byte(strconv.Itoa(os.Getpid()))
    338 	pidCol := -1
    339 
    340 	s := bufio.NewScanner(bytes.NewReader(raw))
    341 	for s.Scan() {
    342 		line := s.Bytes()
    343 		fields := bytes.Fields(line)
    344 		if pidCol < 0 {
    345 			for i, v := range fields {
    346 				if bytes.Equal(v, []byte("PID")) {
    347 					pidCol = i
    348 					break
    349 				}
    350 			}
    351 			lsof = append(lsof, line...)
    352 			continue
    353 		}
    354 		if bytes.Equal(fields[pidCol], pid) {
    355 			lsof = append(lsof, '\n')
    356 			lsof = append(lsof, line...)
    357 		}
    358 	}
    359 	if pidCol < 0 {
    360 		t.Fatal("error processing lsof output: unexpected header format")
    361 	}
    362 	if err := s.Err(); err != nil {
    363 		t.Fatalf("error processing lsof output: %v", err)
    364 	}
    365 	return bytes.Count(lsof, []byte("\n")), lsof
    366 }
    367 
    368 var testedAlreadyLeaked = false
    369 
    370 // basefds returns the number of expected file descriptors
    371 // to be present in a process at start.
    372 func basefds() uintptr {
    373 	return os.Stderr.Fd() + 1
    374 }
    375 
    376 func closeUnexpectedFds(t *testing.T, m string) {
    377 	for fd := basefds(); fd <= 101; fd++ {
    378 		err := os.NewFile(fd, "").Close()
    379 		if err == nil {
    380 			t.Logf("%s: Something already leaked - closed fd %d", m, fd)
    381 		}
    382 	}
    383 }
    384 
    385 func TestExtraFilesFDShuffle(t *testing.T) {
    386 	t.Skip("flaky test; see https://golang.org/issue/5780")
    387 	switch runtime.GOOS {
    388 	case "darwin":
    389 		// TODO(cnicolaou): https://golang.org/issue/2603
    390 		// leads to leaked file descriptors in this test when it's
    391 		// run from a builder.
    392 		closeUnexpectedFds(t, "TestExtraFilesFDShuffle")
    393 	case "netbsd":
    394 		// https://golang.org/issue/3955
    395 		closeUnexpectedFds(t, "TestExtraFilesFDShuffle")
    396 	case "windows":
    397 		t.Skip("no operating system support; skipping")
    398 	}
    399 
    400 	// syscall.StartProcess maps all the FDs passed to it in
    401 	// ProcAttr.Files (the concatenation of stdin,stdout,stderr and
    402 	// ExtraFiles) into consecutive FDs in the child, that is:
    403 	// Files{11, 12, 6, 7, 9, 3} should result in the file
    404 	// represented by FD 11 in the parent being made available as 0
    405 	// in the child, 12 as 1, etc.
    406 	//
    407 	// We want to test that FDs in the child do not get overwritten
    408 	// by one another as this shuffle occurs. The original implementation
    409 	// was buggy in that in some data dependent cases it would overwrite
    410 	// stderr in the child with one of the ExtraFile members.
    411 	// Testing for this case is difficult because it relies on using
    412 	// the same FD values as that case. In particular, an FD of 3
    413 	// must be at an index of 4 or higher in ProcAttr.Files and
    414 	// the FD of the write end of the Stderr pipe (as obtained by
    415 	// StderrPipe()) must be the same as the size of ProcAttr.Files;
    416 	// therefore we test that the read end of this pipe (which is what
    417 	// is returned to the parent by StderrPipe() being one less than
    418 	// the size of ProcAttr.Files, i.e. 3+len(cmd.ExtraFiles).
    419 	//
    420 	// Moving this test case around within the overall tests may
    421 	// affect the FDs obtained and hence the checks to catch these cases.
    422 	npipes := 2
    423 	c := helperCommand(t, "extraFilesAndPipes", strconv.Itoa(npipes+1))
    424 	rd, wr, _ := os.Pipe()
    425 	defer rd.Close()
    426 	if rd.Fd() != 3 {
    427 		t.Errorf("bad test value for test pipe: fd %d", rd.Fd())
    428 	}
    429 	stderr, _ := c.StderrPipe()
    430 	wr.WriteString("_LAST")
    431 	wr.Close()
    432 
    433 	pipes := make([]struct {
    434 		r, w *os.File
    435 	}, npipes)
    436 	data := []string{"a", "b"}
    437 
    438 	for i := 0; i < npipes; i++ {
    439 		r, w, err := os.Pipe()
    440 		if err != nil {
    441 			t.Fatalf("unexpected error creating pipe: %s", err)
    442 		}
    443 		pipes[i].r = r
    444 		pipes[i].w = w
    445 		w.WriteString(data[i])
    446 		c.ExtraFiles = append(c.ExtraFiles, pipes[i].r)
    447 		defer func() {
    448 			r.Close()
    449 			w.Close()
    450 		}()
    451 	}
    452 	// Put fd 3 at the end.
    453 	c.ExtraFiles = append(c.ExtraFiles, rd)
    454 
    455 	stderrFd := int(stderr.(*os.File).Fd())
    456 	if stderrFd != ((len(c.ExtraFiles) + 3) - 1) {
    457 		t.Errorf("bad test value for stderr pipe")
    458 	}
    459 
    460 	expected := "child: " + strings.Join(data, "") + "_LAST"
    461 
    462 	err := c.Start()
    463 	if err != nil {
    464 		t.Fatalf("Run: %v", err)
    465 	}
    466 	ch := make(chan string, 1)
    467 	go func(ch chan string) {
    468 		buf := make([]byte, 512)
    469 		n, err := stderr.Read(buf)
    470 		if err != nil {
    471 			t.Errorf("Read: %s", err)
    472 			ch <- err.Error()
    473 		} else {
    474 			ch <- string(buf[:n])
    475 		}
    476 		close(ch)
    477 	}(ch)
    478 	select {
    479 	case m := <-ch:
    480 		if m != expected {
    481 			t.Errorf("Read: '%s' not '%s'", m, expected)
    482 		}
    483 	case <-time.After(5 * time.Second):
    484 		t.Errorf("Read timedout")
    485 	}
    486 	c.Wait()
    487 }
    488 
    489 func TestExtraFiles(t *testing.T) {
    490 	testenv.MustHaveExec(t)
    491 
    492 	if runtime.GOOS == "windows" {
    493 		t.Skipf("skipping test on %q", runtime.GOOS)
    494 	}
    495 
    496 	// Ensure that file descriptors have not already been leaked into
    497 	// our environment.
    498 	if !testedAlreadyLeaked {
    499 		testedAlreadyLeaked = true
    500 		closeUnexpectedFds(t, "TestExtraFiles")
    501 	}
    502 
    503 	// Force network usage, to verify the epoll (or whatever) fd
    504 	// doesn't leak to the child,
    505 	ln, err := net.Listen("tcp", "127.0.0.1:0")
    506 	if err != nil {
    507 		t.Fatal(err)
    508 	}
    509 	defer ln.Close()
    510 
    511 	// Make sure duplicated fds don't leak to the child.
    512 	f, err := ln.(*net.TCPListener).File()
    513 	if err != nil {
    514 		t.Fatal(err)
    515 	}
    516 	defer f.Close()
    517 	ln2, err := net.FileListener(f)
    518 	if err != nil {
    519 		t.Fatal(err)
    520 	}
    521 	defer ln2.Close()
    522 
    523 	// Force TLS root certs to be loaded (which might involve
    524 	// cgo), to make sure none of that potential C code leaks fds.
    525 	ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
    526 	// quiet expected TLS handshake error "remote error: bad certificate"
    527 	ts.Config.ErrorLog = log.New(ioutil.Discard, "", 0)
    528 	ts.StartTLS()
    529 	defer ts.Close()
    530 	_, err = http.Get(ts.URL)
    531 	if err == nil {
    532 		t.Errorf("success trying to fetch %s; want an error", ts.URL)
    533 	}
    534 
    535 	tf, err := ioutil.TempFile("", "")
    536 	if err != nil {
    537 		t.Fatalf("TempFile: %v", err)
    538 	}
    539 	defer os.Remove(tf.Name())
    540 	defer tf.Close()
    541 
    542 	const text = "Hello, fd 3!"
    543 	_, err = tf.Write([]byte(text))
    544 	if err != nil {
    545 		t.Fatalf("Write: %v", err)
    546 	}
    547 	_, err = tf.Seek(0, io.SeekStart)
    548 	if err != nil {
    549 		t.Fatalf("Seek: %v", err)
    550 	}
    551 
    552 	c := helperCommand(t, "read3")
    553 	var stdout, stderr bytes.Buffer
    554 	c.Stdout = &stdout
    555 	c.Stderr = &stderr
    556 	c.ExtraFiles = []*os.File{tf}
    557 	err = c.Run()
    558 	if err != nil {
    559 		t.Fatalf("Run: %v; stdout %q, stderr %q", err, stdout.Bytes(), stderr.Bytes())
    560 	}
    561 	if stdout.String() != text {
    562 		t.Errorf("got stdout %q, stderr %q; want %q on stdout", stdout.String(), stderr.String(), text)
    563 	}
    564 }
    565 
    566 func TestExtraFilesRace(t *testing.T) {
    567 	if runtime.GOOS == "windows" {
    568 		t.Skip("no operating system support; skipping")
    569 	}
    570 	listen := func() net.Listener {
    571 		ln, err := net.Listen("tcp", "127.0.0.1:0")
    572 		if err != nil {
    573 			t.Fatal(err)
    574 		}
    575 		return ln
    576 	}
    577 	listenerFile := func(ln net.Listener) *os.File {
    578 		f, err := ln.(*net.TCPListener).File()
    579 		if err != nil {
    580 			t.Fatal(err)
    581 		}
    582 		return f
    583 	}
    584 	runCommand := func(c *exec.Cmd, out chan<- string) {
    585 		bout, err := c.CombinedOutput()
    586 		if err != nil {
    587 			out <- "ERROR:" + err.Error()
    588 		} else {
    589 			out <- string(bout)
    590 		}
    591 	}
    592 
    593 	for i := 0; i < 10; i++ {
    594 		la := listen()
    595 		ca := helperCommand(t, "describefiles")
    596 		ca.ExtraFiles = []*os.File{listenerFile(la)}
    597 		lb := listen()
    598 		cb := helperCommand(t, "describefiles")
    599 		cb.ExtraFiles = []*os.File{listenerFile(lb)}
    600 		ares := make(chan string)
    601 		bres := make(chan string)
    602 		go runCommand(ca, ares)
    603 		go runCommand(cb, bres)
    604 		if got, want := <-ares, fmt.Sprintf("fd3: listener %s\n", la.Addr()); got != want {
    605 			t.Errorf("iteration %d, process A got:\n%s\nwant:\n%s\n", i, got, want)
    606 		}
    607 		if got, want := <-bres, fmt.Sprintf("fd3: listener %s\n", lb.Addr()); got != want {
    608 			t.Errorf("iteration %d, process B got:\n%s\nwant:\n%s\n", i, got, want)
    609 		}
    610 		la.Close()
    611 		lb.Close()
    612 		for _, f := range ca.ExtraFiles {
    613 			f.Close()
    614 		}
    615 		for _, f := range cb.ExtraFiles {
    616 			f.Close()
    617 		}
    618 
    619 	}
    620 }
    621 
    622 // TestHelperProcess isn't a real test. It's used as a helper process
    623 // for TestParameterRun.
    624 func TestHelperProcess(*testing.T) {
    625 	if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
    626 		return
    627 	}
    628 	defer os.Exit(0)
    629 
    630 	// Determine which command to use to display open files.
    631 	ofcmd := "lsof"
    632 	switch runtime.GOOS {
    633 	case "dragonfly", "freebsd", "netbsd", "openbsd":
    634 		ofcmd = "fstat"
    635 	case "plan9":
    636 		ofcmd = "/bin/cat"
    637 	}
    638 
    639 	args := os.Args
    640 	for len(args) > 0 {
    641 		if args[0] == "--" {
    642 			args = args[1:]
    643 			break
    644 		}
    645 		args = args[1:]
    646 	}
    647 	if len(args) == 0 {
    648 		fmt.Fprintf(os.Stderr, "No command\n")
    649 		os.Exit(2)
    650 	}
    651 
    652 	cmd, args := args[0], args[1:]
    653 	switch cmd {
    654 	case "echo":
    655 		iargs := []interface{}{}
    656 		for _, s := range args {
    657 			iargs = append(iargs, s)
    658 		}
    659 		fmt.Println(iargs...)
    660 	case "cat":
    661 		if len(args) == 0 {
    662 			io.Copy(os.Stdout, os.Stdin)
    663 			return
    664 		}
    665 		exit := 0
    666 		for _, fn := range args {
    667 			f, err := os.Open(fn)
    668 			if err != nil {
    669 				fmt.Fprintf(os.Stderr, "Error: %v\n", err)
    670 				exit = 2
    671 			} else {
    672 				defer f.Close()
    673 				io.Copy(os.Stdout, f)
    674 			}
    675 		}
    676 		os.Exit(exit)
    677 	case "pipetest":
    678 		bufr := bufio.NewReader(os.Stdin)
    679 		for {
    680 			line, _, err := bufr.ReadLine()
    681 			if err == io.EOF {
    682 				break
    683 			} else if err != nil {
    684 				os.Exit(1)
    685 			}
    686 			if bytes.HasPrefix(line, []byte("O:")) {
    687 				os.Stdout.Write(line)
    688 				os.Stdout.Write([]byte{'\n'})
    689 			} else if bytes.HasPrefix(line, []byte("E:")) {
    690 				os.Stderr.Write(line)
    691 				os.Stderr.Write([]byte{'\n'})
    692 			} else {
    693 				os.Exit(1)
    694 			}
    695 		}
    696 	case "stdinClose":
    697 		b, err := ioutil.ReadAll(os.Stdin)
    698 		if err != nil {
    699 			fmt.Fprintf(os.Stderr, "Error: %v\n", err)
    700 			os.Exit(1)
    701 		}
    702 		if s := string(b); s != stdinCloseTestString {
    703 			fmt.Fprintf(os.Stderr, "Error: Read %q, want %q", s, stdinCloseTestString)
    704 			os.Exit(1)
    705 		}
    706 		os.Exit(0)
    707 	case "read3": // read fd 3
    708 		fd3 := os.NewFile(3, "fd3")
    709 		bs, err := ioutil.ReadAll(fd3)
    710 		if err != nil {
    711 			fmt.Printf("ReadAll from fd 3: %v", err)
    712 			os.Exit(1)
    713 		}
    714 		switch runtime.GOOS {
    715 		case "dragonfly":
    716 			// TODO(jsing): Determine why DragonFly is leaking
    717 			// file descriptors...
    718 		case "darwin":
    719 			// TODO(bradfitz): broken? Sometimes.
    720 			// https://golang.org/issue/2603
    721 			// Skip this additional part of the test for now.
    722 		case "netbsd":
    723 			// TODO(jsing): This currently fails on NetBSD due to
    724 			// the cloned file descriptors that result from opening
    725 			// /dev/urandom.
    726 			// https://golang.org/issue/3955
    727 		case "solaris":
    728 			// TODO(aram): This fails on Solaris because libc opens
    729 			// its own files, as it sees fit. Darwin does the same,
    730 			// see: https://golang.org/issue/2603
    731 		default:
    732 			// Now verify that there are no other open fds.
    733 			var files []*os.File
    734 			for wantfd := basefds() + 1; wantfd <= 100; wantfd++ {
    735 				f, err := os.Open(os.Args[0])
    736 				if err != nil {
    737 					fmt.Printf("error opening file with expected fd %d: %v", wantfd, err)
    738 					os.Exit(1)
    739 				}
    740 				if got := f.Fd(); got != wantfd {
    741 					fmt.Printf("leaked parent file. fd = %d; want %d\n", got, wantfd)
    742 					var args []string
    743 					switch runtime.GOOS {
    744 					case "plan9":
    745 						args = []string{fmt.Sprintf("/proc/%d/fd", os.Getpid())}
    746 					default:
    747 						args = []string{"-p", fmt.Sprint(os.Getpid())}
    748 					}
    749 					out, _ := exec.Command(ofcmd, args...).CombinedOutput()
    750 					fmt.Print(string(out))
    751 					os.Exit(1)
    752 				}
    753 				files = append(files, f)
    754 			}
    755 			for _, f := range files {
    756 				f.Close()
    757 			}
    758 		}
    759 		// Referring to fd3 here ensures that it is not
    760 		// garbage collected, and therefore closed, while
    761 		// executing the wantfd loop above. It doesn't matter
    762 		// what we do with fd3 as long as we refer to it;
    763 		// closing it is the easy choice.
    764 		fd3.Close()
    765 		os.Stdout.Write(bs)
    766 	case "exit":
    767 		n, _ := strconv.Atoi(args[0])
    768 		os.Exit(n)
    769 	case "describefiles":
    770 		f := os.NewFile(3, fmt.Sprintf("fd3"))
    771 		ln, err := net.FileListener(f)
    772 		if err == nil {
    773 			fmt.Printf("fd3: listener %s\n", ln.Addr())
    774 			ln.Close()
    775 		}
    776 		os.Exit(0)
    777 	case "extraFilesAndPipes":
    778 		n, _ := strconv.Atoi(args[0])
    779 		pipes := make([]*os.File, n)
    780 		for i := 0; i < n; i++ {
    781 			pipes[i] = os.NewFile(uintptr(3+i), strconv.Itoa(i))
    782 		}
    783 		response := ""
    784 		for i, r := range pipes {
    785 			ch := make(chan string, 1)
    786 			go func(c chan string) {
    787 				buf := make([]byte, 10)
    788 				n, err := r.Read(buf)
    789 				if err != nil {
    790 					fmt.Fprintf(os.Stderr, "Child: read error: %v on pipe %d\n", err, i)
    791 					os.Exit(1)
    792 				}
    793 				c <- string(buf[:n])
    794 				close(c)
    795 			}(ch)
    796 			select {
    797 			case m := <-ch:
    798 				response = response + m
    799 			case <-time.After(5 * time.Second):
    800 				fmt.Fprintf(os.Stderr, "Child: Timeout reading from pipe: %d\n", i)
    801 				os.Exit(1)
    802 			}
    803 		}
    804 		fmt.Fprintf(os.Stderr, "child: %s", response)
    805 		os.Exit(0)
    806 	case "exec":
    807 		cmd := exec.Command(args[1])
    808 		cmd.Dir = args[0]
    809 		output, err := cmd.CombinedOutput()
    810 		if err != nil {
    811 			fmt.Fprintf(os.Stderr, "Child: %s %s", err, string(output))
    812 			os.Exit(1)
    813 		}
    814 		fmt.Printf("%s", string(output))
    815 		os.Exit(0)
    816 	case "lookpath":
    817 		p, err := exec.LookPath(args[0])
    818 		if err != nil {
    819 			fmt.Fprintf(os.Stderr, "LookPath failed: %v\n", err)
    820 			os.Exit(1)
    821 		}
    822 		fmt.Print(p)
    823 		os.Exit(0)
    824 	case "stderrfail":
    825 		fmt.Fprintf(os.Stderr, "some stderr text\n")
    826 		os.Exit(1)
    827 	default:
    828 		fmt.Fprintf(os.Stderr, "Unknown command %q\n", cmd)
    829 		os.Exit(2)
    830 	}
    831 }
    832 
    833 // Issue 9173: ignore stdin pipe writes if the program completes successfully.
    834 func TestIgnorePipeErrorOnSuccess(t *testing.T) {
    835 	testenv.MustHaveExec(t)
    836 
    837 	// We really only care about testing this on Unixy things.
    838 	if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
    839 		t.Skipf("skipping test on %q", runtime.GOOS)
    840 	}
    841 
    842 	cmd := helperCommand(t, "echo", "foo")
    843 	var out bytes.Buffer
    844 	cmd.Stdin = strings.NewReader(strings.Repeat("x", 10<<20))
    845 	cmd.Stdout = &out
    846 	if err := cmd.Run(); err != nil {
    847 		t.Fatal(err)
    848 	}
    849 	if got, want := out.String(), "foo\n"; got != want {
    850 		t.Errorf("output = %q; want %q", got, want)
    851 	}
    852 }
    853 
    854 type badWriter struct{}
    855 
    856 func (w *badWriter) Write(data []byte) (int, error) {
    857 	return 0, io.ErrUnexpectedEOF
    858 }
    859 
    860 func TestClosePipeOnCopyError(t *testing.T) {
    861 	testenv.MustHaveExec(t)
    862 
    863 	if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
    864 		t.Skipf("skipping test on %s - no yes command", runtime.GOOS)
    865 	}
    866 	cmd := exec.Command("yes")
    867 	cmd.Stdout = new(badWriter)
    868 	c := make(chan int, 1)
    869 	go func() {
    870 		err := cmd.Run()
    871 		if err == nil {
    872 			t.Errorf("yes completed successfully")
    873 		}
    874 		c <- 1
    875 	}()
    876 	select {
    877 	case <-c:
    878 		// ok
    879 	case <-time.After(5 * time.Second):
    880 		t.Fatalf("yes got stuck writing to bad writer")
    881 	}
    882 }
    883 
    884 func TestOutputStderrCapture(t *testing.T) {
    885 	testenv.MustHaveExec(t)
    886 
    887 	cmd := helperCommand(t, "stderrfail")
    888 	_, err := cmd.Output()
    889 	ee, ok := err.(*exec.ExitError)
    890 	if !ok {
    891 		t.Fatalf("Output error type = %T; want ExitError", err)
    892 	}
    893 	got := string(ee.Stderr)
    894 	want := "some stderr text\n"
    895 	if got != want {
    896 		t.Errorf("ExitError.Stderr = %q; want %q", got, want)
    897 	}
    898 }
    899 
    900 func TestContext(t *testing.T) {
    901 	ctx, cancel := context.WithCancel(context.Background())
    902 	c := helperCommandContext(t, ctx, "pipetest")
    903 	stdin, err := c.StdinPipe()
    904 	if err != nil {
    905 		t.Fatal(err)
    906 	}
    907 	stdout, err := c.StdoutPipe()
    908 	if err != nil {
    909 		t.Fatal(err)
    910 	}
    911 	if err := c.Start(); err != nil {
    912 		t.Fatal(err)
    913 	}
    914 
    915 	if _, err := stdin.Write([]byte("O:hi\n")); err != nil {
    916 		t.Fatal(err)
    917 	}
    918 	buf := make([]byte, 5)
    919 	n, err := io.ReadFull(stdout, buf)
    920 	if n != len(buf) || err != nil || string(buf) != "O:hi\n" {
    921 		t.Fatalf("ReadFull = %d, %v, %q", n, err, buf[:n])
    922 	}
    923 	waitErr := make(chan error, 1)
    924 	go func() {
    925 		waitErr <- c.Wait()
    926 	}()
    927 	cancel()
    928 	select {
    929 	case err := <-waitErr:
    930 		if err == nil {
    931 			t.Fatal("expected Wait failure")
    932 		}
    933 	case <-time.After(3 * time.Second):
    934 		t.Fatal("timeout waiting for child process death")
    935 	}
    936 }
    937 
    938 func TestContextCancel(t *testing.T) {
    939 	ctx, cancel := context.WithCancel(context.Background())
    940 	defer cancel()
    941 	c := helperCommandContext(t, ctx, "cat")
    942 
    943 	r, w, err := os.Pipe()
    944 	if err != nil {
    945 		t.Fatal(err)
    946 	}
    947 	c.Stdin = r
    948 
    949 	stdout, err := c.StdoutPipe()
    950 	if err != nil {
    951 		t.Fatal(err)
    952 	}
    953 	readDone := make(chan struct{})
    954 	go func() {
    955 		defer close(readDone)
    956 		var a [1024]byte
    957 		for {
    958 			n, err := stdout.Read(a[:])
    959 			if err != nil {
    960 				if err != io.EOF {
    961 					t.Errorf("unexpected read error: %v", err)
    962 				}
    963 				return
    964 			}
    965 			t.Logf("%s", a[:n])
    966 		}
    967 	}()
    968 
    969 	if err := c.Start(); err != nil {
    970 		t.Fatal(err)
    971 	}
    972 
    973 	if err := r.Close(); err != nil {
    974 		t.Fatal(err)
    975 	}
    976 
    977 	if _, err := io.WriteString(w, "echo"); err != nil {
    978 		t.Fatal(err)
    979 	}
    980 
    981 	cancel()
    982 
    983 	// Calling cancel should have killed the process, so writes
    984 	// should now fail.  Give the process a little while to die.
    985 	start := time.Now()
    986 	for {
    987 		if _, err := io.WriteString(w, "echo"); err != nil {
    988 			break
    989 		}
    990 		if time.Since(start) > time.Second {
    991 			t.Fatal("canceling context did not stop program")
    992 		}
    993 		time.Sleep(time.Millisecond)
    994 	}
    995 
    996 	if err := w.Close(); err != nil {
    997 		t.Errorf("error closing write end of pipe: %v", err)
    998 	}
    999 	<-readDone
   1000 
   1001 	if err := c.Wait(); err == nil {
   1002 		t.Error("program unexpectedly exited successfully")
   1003 	} else {
   1004 		t.Logf("exit status: %v", err)
   1005 	}
   1006 }
   1007