libgo: update to go1.9
[official-gcc.git] / libgo / go / os / exec / exec_test.go
bloba877d8ae232faa8a93a34068f69f5f1cc531edfc
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.
5 // Use an external test to avoid os/exec -> net/http -> crypto/x509 -> os/exec
6 // circular dependency on non-cgo darwin.
8 package exec_test
10 import (
11 "bufio"
12 "bytes"
13 "context"
14 "fmt"
15 "internal/poll"
16 "internal/testenv"
17 "io"
18 "io/ioutil"
19 "log"
20 "net"
21 "net/http"
22 "net/http/httptest"
23 "os"
24 "os/exec"
25 "path/filepath"
26 "runtime"
27 "strconv"
28 "strings"
29 "testing"
30 "time"
33 func helperCommandContext(t *testing.T, ctx context.Context, s ...string) (cmd *exec.Cmd) {
34 testenv.MustHaveExec(t)
36 cs := []string{"-test.run=TestHelperProcess", "--"}
37 cs = append(cs, s...)
38 if ctx != nil {
39 cmd = exec.CommandContext(ctx, os.Args[0], cs...)
40 } else {
41 cmd = exec.Command(os.Args[0], cs...)
43 cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
44 path := os.Getenv("LD_LIBRARY_PATH")
45 if path != "" {
46 cmd.Env = append(cmd.Env, "LD_LIBRARY_PATH="+path)
48 return cmd
51 func helperCommand(t *testing.T, s ...string) *exec.Cmd {
52 return helperCommandContext(t, nil, s...)
55 func TestEcho(t *testing.T) {
56 bs, err := helperCommand(t, "echo", "foo bar", "baz").Output()
57 if err != nil {
58 t.Errorf("echo: %v", err)
60 if g, e := string(bs), "foo bar baz\n"; g != e {
61 t.Errorf("echo: want %q, got %q", e, g)
65 func TestCommandRelativeName(t *testing.T) {
66 testenv.MustHaveExec(t)
68 // Run our own binary as a relative path
69 // (e.g. "_test/exec.test") our parent directory.
70 base := filepath.Base(os.Args[0]) // "exec.test"
71 dir := filepath.Dir(os.Args[0]) // "/tmp/go-buildNNNN/os/exec/_test"
72 if dir == "." {
73 t.Skip("skipping; running test at root somehow")
75 parentDir := filepath.Dir(dir) // "/tmp/go-buildNNNN/os/exec"
76 dirBase := filepath.Base(dir) // "_test"
77 if dirBase == "." {
78 t.Skipf("skipping; unexpected shallow dir of %q", dir)
81 cmd := exec.Command(filepath.Join(dirBase, base), "-test.run=TestHelperProcess", "--", "echo", "foo")
82 cmd.Dir = parentDir
83 cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
85 out, err := cmd.Output()
86 if err != nil {
87 t.Errorf("echo: %v", err)
89 if g, e := string(out), "foo\n"; g != e {
90 t.Errorf("echo: want %q, got %q", e, g)
94 func TestCatStdin(t *testing.T) {
95 // Cat, testing stdin and stdout.
96 input := "Input string\nLine 2"
97 p := helperCommand(t, "cat")
98 p.Stdin = strings.NewReader(input)
99 bs, err := p.Output()
100 if err != nil {
101 t.Errorf("cat: %v", err)
103 s := string(bs)
104 if s != input {
105 t.Errorf("cat: want %q, got %q", input, s)
109 func TestEchoFileRace(t *testing.T) {
110 cmd := helperCommand(t, "echo")
111 stdin, err := cmd.StdinPipe()
112 if err != nil {
113 t.Fatalf("StdinPipe: %v", err)
115 if err := cmd.Start(); err != nil {
116 t.Fatalf("Start: %v", err)
118 wrote := make(chan bool)
119 go func() {
120 defer close(wrote)
121 fmt.Fprint(stdin, "echo\n")
123 if err := cmd.Wait(); err != nil {
124 t.Fatalf("Wait: %v", err)
126 <-wrote
129 func TestCatGoodAndBadFile(t *testing.T) {
130 // Testing combined output and error values.
131 bs, err := helperCommand(t, "cat", "/bogus/file.foo", "exec_test.go").CombinedOutput()
132 if _, ok := err.(*exec.ExitError); !ok {
133 t.Errorf("expected *exec.ExitError from cat combined; got %T: %v", err, err)
135 s := string(bs)
136 sp := strings.SplitN(s, "\n", 2)
137 if len(sp) != 2 {
138 t.Fatalf("expected two lines from cat; got %q", s)
140 errLine, body := sp[0], sp[1]
141 if !strings.HasPrefix(errLine, "Error: open /bogus/file.foo") {
142 t.Errorf("expected stderr to complain about file; got %q", errLine)
144 if !strings.Contains(body, "func TestHelperProcess(t *testing.T)") {
145 t.Errorf("expected test code; got %q (len %d)", body, len(body))
149 func TestNoExistBinary(t *testing.T) {
150 // Can't run a non-existent binary
151 err := exec.Command("/no-exist-binary").Run()
152 if err == nil {
153 t.Error("expected error from /no-exist-binary")
157 func TestExitStatus(t *testing.T) {
158 // Test that exit values are returned correctly
159 cmd := helperCommand(t, "exit", "42")
160 err := cmd.Run()
161 want := "exit status 42"
162 switch runtime.GOOS {
163 case "plan9":
164 want = fmt.Sprintf("exit status: '%s %d: 42'", filepath.Base(cmd.Path), cmd.ProcessState.Pid())
166 if werr, ok := err.(*exec.ExitError); ok {
167 if s := werr.Error(); s != want {
168 t.Errorf("from exit 42 got exit %q, want %q", s, want)
170 } else {
171 t.Fatalf("expected *exec.ExitError from exit 42; got %T: %v", err, err)
175 func TestPipes(t *testing.T) {
176 check := func(what string, err error) {
177 if err != nil {
178 t.Fatalf("%s: %v", what, err)
181 // Cat, testing stdin and stdout.
182 c := helperCommand(t, "pipetest")
183 stdin, err := c.StdinPipe()
184 check("StdinPipe", err)
185 stdout, err := c.StdoutPipe()
186 check("StdoutPipe", err)
187 stderr, err := c.StderrPipe()
188 check("StderrPipe", err)
190 outbr := bufio.NewReader(stdout)
191 errbr := bufio.NewReader(stderr)
192 line := func(what string, br *bufio.Reader) string {
193 line, _, err := br.ReadLine()
194 if err != nil {
195 t.Fatalf("%s: %v", what, err)
197 return string(line)
200 err = c.Start()
201 check("Start", err)
203 _, err = stdin.Write([]byte("O:I am output\n"))
204 check("first stdin Write", err)
205 if g, e := line("first output line", outbr), "O:I am output"; g != e {
206 t.Errorf("got %q, want %q", g, e)
209 _, err = stdin.Write([]byte("E:I am error\n"))
210 check("second stdin Write", err)
211 if g, e := line("first error line", errbr), "E:I am error"; g != e {
212 t.Errorf("got %q, want %q", g, e)
215 _, err = stdin.Write([]byte("O:I am output2\n"))
216 check("third stdin Write 3", err)
217 if g, e := line("second output line", outbr), "O:I am output2"; g != e {
218 t.Errorf("got %q, want %q", g, e)
221 stdin.Close()
222 err = c.Wait()
223 check("Wait", err)
226 const stdinCloseTestString = "Some test string."
228 // Issue 6270.
229 func TestStdinClose(t *testing.T) {
230 check := func(what string, err error) {
231 if err != nil {
232 t.Fatalf("%s: %v", what, err)
235 cmd := helperCommand(t, "stdinClose")
236 stdin, err := cmd.StdinPipe()
237 check("StdinPipe", err)
238 // Check that we can access methods of the underlying os.File.`
239 if _, ok := stdin.(interface {
240 Fd() uintptr
241 }); !ok {
242 t.Error("can't access methods of underlying *os.File")
244 check("Start", cmd.Start())
245 go func() {
246 _, err := io.Copy(stdin, strings.NewReader(stdinCloseTestString))
247 check("Copy", err)
248 // Before the fix, this next line would race with cmd.Wait.
249 check("Close", stdin.Close())
251 check("Wait", cmd.Wait())
254 // Issue 17647.
255 // It used to be the case that TestStdinClose, above, would fail when
256 // run under the race detector. This test is a variant of TestStdinClose
257 // that also used to fail when run under the race detector.
258 // This test is run by cmd/dist under the race detector to verify that
259 // the race detector no longer reports any problems.
260 func TestStdinCloseRace(t *testing.T) {
261 cmd := helperCommand(t, "stdinClose")
262 stdin, err := cmd.StdinPipe()
263 if err != nil {
264 t.Fatalf("StdinPipe: %v", err)
266 if err := cmd.Start(); err != nil {
267 t.Fatalf("Start: %v", err)
269 go func() {
270 // We don't check the error return of Kill. It is
271 // possible that the process has already exited, in
272 // which case Kill will return an error "process
273 // already finished". The purpose of this test is to
274 // see whether the race detector reports an error; it
275 // doesn't matter whether this Kill succeeds or not.
276 cmd.Process.Kill()
278 go func() {
279 // Send the wrong string, so that the child fails even
280 // if the other goroutine doesn't manage to kill it first.
281 // This test is to check that the race detector does not
282 // falsely report an error, so it doesn't matter how the
283 // child process fails.
284 io.Copy(stdin, strings.NewReader("unexpected string"))
285 if err := stdin.Close(); err != nil {
286 t.Errorf("stdin.Close: %v", err)
289 if err := cmd.Wait(); err == nil {
290 t.Fatalf("Wait: succeeded unexpectedly")
294 // Issue 5071
295 func TestPipeLookPathLeak(t *testing.T) {
296 // If we are reading from /proc/self/fd we (should) get an exact result.
297 tolerance := 0
299 // Reading /proc/self/fd is more reliable than calling lsof, so try that
300 // first.
301 numOpenFDs := func() (int, []byte, error) {
302 fds, err := ioutil.ReadDir("/proc/self/fd")
303 if err != nil {
304 return 0, nil, err
306 return len(fds), nil, nil
308 want, before, err := numOpenFDs()
309 if err != nil {
310 // We encountered a problem reading /proc/self/fd (we might be on
311 // a platform that doesn't have it). Fall back onto lsof.
312 t.Logf("using lsof because: %v", err)
313 numOpenFDs = func() (int, []byte, error) {
314 // Android's stock lsof does not obey the -p option,
315 // so extra filtering is needed.
316 // https://golang.org/issue/10206
317 if runtime.GOOS == "android" {
318 // numOpenFDsAndroid handles errors itself and
319 // might skip or fail the test.
320 n, lsof := numOpenFDsAndroid(t)
321 return n, lsof, nil
323 lsof, err := exec.Command("lsof", "-b", "-n", "-p", strconv.Itoa(os.Getpid())).Output()
324 return bytes.Count(lsof, []byte("\n")), lsof, err
327 // lsof may see file descriptors associated with the fork itself,
328 // so we allow some extra margin if we have to use it.
329 // https://golang.org/issue/19243
330 tolerance = 5
332 // Retry reading the number of open file descriptors.
333 want, before, err = numOpenFDs()
334 if err != nil {
335 t.Log(err)
336 t.Skipf("skipping test; error finding or running lsof")
340 for i := 0; i < 6; i++ {
341 cmd := exec.Command("something-that-does-not-exist-binary")
342 cmd.StdoutPipe()
343 cmd.StderrPipe()
344 cmd.StdinPipe()
345 if err := cmd.Run(); err == nil {
346 t.Fatal("unexpected success")
349 got, after, err := numOpenFDs()
350 if err != nil {
351 // numOpenFDs has already succeeded once, it should work here.
352 t.Errorf("unexpected failure: %v", err)
354 if got-want > tolerance {
355 t.Errorf("number of open file descriptors changed: got %v, want %v", got, want)
356 if before != nil {
357 t.Errorf("before:\n%v\n", before)
359 if after != nil {
360 t.Errorf("after:\n%v\n", after)
365 func numOpenFDsAndroid(t *testing.T) (n int, lsof []byte) {
366 raw, err := exec.Command("lsof").Output()
367 if err != nil {
368 t.Skip("skipping test; error finding or running lsof")
371 // First find the PID column index by parsing the first line, and
372 // select lines containing pid in the column.
373 pid := []byte(strconv.Itoa(os.Getpid()))
374 pidCol := -1
376 s := bufio.NewScanner(bytes.NewReader(raw))
377 for s.Scan() {
378 line := s.Bytes()
379 fields := bytes.Fields(line)
380 if pidCol < 0 {
381 for i, v := range fields {
382 if bytes.Equal(v, []byte("PID")) {
383 pidCol = i
384 break
387 lsof = append(lsof, line...)
388 continue
390 if bytes.Equal(fields[pidCol], pid) {
391 lsof = append(lsof, '\n')
392 lsof = append(lsof, line...)
395 if pidCol < 0 {
396 t.Fatal("error processing lsof output: unexpected header format")
398 if err := s.Err(); err != nil {
399 t.Fatalf("error processing lsof output: %v", err)
401 return bytes.Count(lsof, []byte("\n")), lsof
404 var testedAlreadyLeaked = false
406 // basefds returns the number of expected file descriptors
407 // to be present in a process at start.
408 // stdin, stdout, stderr, epoll/kqueue
409 func basefds() uintptr {
410 return os.Stderr.Fd() + 1
413 func closeUnexpectedFds(t *testing.T, m string) {
414 for fd := basefds(); fd <= 101; fd++ {
415 if fd == poll.PollDescriptor() {
416 continue
418 err := os.NewFile(fd, "").Close()
419 if err == nil {
420 t.Logf("%s: Something already leaked - closed fd %d", m, fd)
425 func TestExtraFilesFDShuffle(t *testing.T) {
426 t.Skip("flaky test; see https://golang.org/issue/5780")
427 switch runtime.GOOS {
428 case "darwin":
429 // TODO(cnicolaou): https://golang.org/issue/2603
430 // leads to leaked file descriptors in this test when it's
431 // run from a builder.
432 closeUnexpectedFds(t, "TestExtraFilesFDShuffle")
433 case "netbsd":
434 // https://golang.org/issue/3955
435 closeUnexpectedFds(t, "TestExtraFilesFDShuffle")
436 case "windows":
437 t.Skip("no operating system support; skipping")
440 // syscall.StartProcess maps all the FDs passed to it in
441 // ProcAttr.Files (the concatenation of stdin,stdout,stderr and
442 // ExtraFiles) into consecutive FDs in the child, that is:
443 // Files{11, 12, 6, 7, 9, 3} should result in the file
444 // represented by FD 11 in the parent being made available as 0
445 // in the child, 12 as 1, etc.
447 // We want to test that FDs in the child do not get overwritten
448 // by one another as this shuffle occurs. The original implementation
449 // was buggy in that in some data dependent cases it would overwrite
450 // stderr in the child with one of the ExtraFile members.
451 // Testing for this case is difficult because it relies on using
452 // the same FD values as that case. In particular, an FD of 3
453 // must be at an index of 4 or higher in ProcAttr.Files and
454 // the FD of the write end of the Stderr pipe (as obtained by
455 // StderrPipe()) must be the same as the size of ProcAttr.Files;
456 // therefore we test that the read end of this pipe (which is what
457 // is returned to the parent by StderrPipe() being one less than
458 // the size of ProcAttr.Files, i.e. 3+len(cmd.ExtraFiles).
460 // Moving this test case around within the overall tests may
461 // affect the FDs obtained and hence the checks to catch these cases.
462 npipes := 2
463 c := helperCommand(t, "extraFilesAndPipes", strconv.Itoa(npipes+1))
464 rd, wr, _ := os.Pipe()
465 defer rd.Close()
466 if rd.Fd() != 3 {
467 t.Errorf("bad test value for test pipe: fd %d", rd.Fd())
469 stderr, _ := c.StderrPipe()
470 wr.WriteString("_LAST")
471 wr.Close()
473 pipes := make([]struct {
474 r, w *os.File
475 }, npipes)
476 data := []string{"a", "b"}
478 for i := 0; i < npipes; i++ {
479 r, w, err := os.Pipe()
480 if err != nil {
481 t.Fatalf("unexpected error creating pipe: %s", err)
483 pipes[i].r = r
484 pipes[i].w = w
485 w.WriteString(data[i])
486 c.ExtraFiles = append(c.ExtraFiles, pipes[i].r)
487 defer func() {
488 r.Close()
489 w.Close()
492 // Put fd 3 at the end.
493 c.ExtraFiles = append(c.ExtraFiles, rd)
495 stderrFd := int(stderr.(*os.File).Fd())
496 if stderrFd != ((len(c.ExtraFiles) + 3) - 1) {
497 t.Errorf("bad test value for stderr pipe")
500 expected := "child: " + strings.Join(data, "") + "_LAST"
502 err := c.Start()
503 if err != nil {
504 t.Fatalf("Run: %v", err)
506 ch := make(chan string, 1)
507 go func(ch chan string) {
508 buf := make([]byte, 512)
509 n, err := stderr.Read(buf)
510 if err != nil {
511 t.Errorf("Read: %s", err)
512 ch <- err.Error()
513 } else {
514 ch <- string(buf[:n])
516 close(ch)
517 }(ch)
518 select {
519 case m := <-ch:
520 if m != expected {
521 t.Errorf("Read: '%s' not '%s'", m, expected)
523 case <-time.After(5 * time.Second):
524 t.Errorf("Read timedout")
526 c.Wait()
529 func TestExtraFiles(t *testing.T) {
530 testenv.MustHaveExec(t)
532 if runtime.GOOS == "windows" {
533 t.Skipf("skipping test on %q", runtime.GOOS)
536 // Ensure that file descriptors have not already been leaked into
537 // our environment.
538 if !testedAlreadyLeaked {
539 testedAlreadyLeaked = true
540 closeUnexpectedFds(t, "TestExtraFiles")
543 // Force network usage, to verify the epoll (or whatever) fd
544 // doesn't leak to the child,
545 ln, err := net.Listen("tcp", "127.0.0.1:0")
546 if err != nil {
547 t.Fatal(err)
549 defer ln.Close()
551 // Make sure duplicated fds don't leak to the child.
552 f, err := ln.(*net.TCPListener).File()
553 if err != nil {
554 t.Fatal(err)
556 defer f.Close()
557 ln2, err := net.FileListener(f)
558 if err != nil {
559 t.Fatal(err)
561 defer ln2.Close()
563 // Force TLS root certs to be loaded (which might involve
564 // cgo), to make sure none of that potential C code leaks fds.
565 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
566 // quiet expected TLS handshake error "remote error: bad certificate"
567 ts.Config.ErrorLog = log.New(ioutil.Discard, "", 0)
568 ts.StartTLS()
569 defer ts.Close()
570 _, err = http.Get(ts.URL)
571 if err == nil {
572 t.Errorf("success trying to fetch %s; want an error", ts.URL)
575 tf, err := ioutil.TempFile("", "")
576 if err != nil {
577 t.Fatalf("TempFile: %v", err)
579 defer os.Remove(tf.Name())
580 defer tf.Close()
582 const text = "Hello, fd 3!"
583 _, err = tf.Write([]byte(text))
584 if err != nil {
585 t.Fatalf("Write: %v", err)
587 _, err = tf.Seek(0, io.SeekStart)
588 if err != nil {
589 t.Fatalf("Seek: %v", err)
592 c := helperCommand(t, "read3")
593 var stdout, stderr bytes.Buffer
594 c.Stdout = &stdout
595 c.Stderr = &stderr
596 c.ExtraFiles = []*os.File{tf}
597 err = c.Run()
598 if err != nil {
599 t.Fatalf("Run: %v; stdout %q, stderr %q", err, stdout.Bytes(), stderr.Bytes())
601 if stdout.String() != text {
602 t.Errorf("got stdout %q, stderr %q; want %q on stdout", stdout.String(), stderr.String(), text)
606 func TestExtraFilesRace(t *testing.T) {
607 if runtime.GOOS == "windows" {
608 t.Skip("no operating system support; skipping")
610 listen := func() net.Listener {
611 ln, err := net.Listen("tcp", "127.0.0.1:0")
612 if err != nil {
613 t.Fatal(err)
615 return ln
617 listenerFile := func(ln net.Listener) *os.File {
618 f, err := ln.(*net.TCPListener).File()
619 if err != nil {
620 t.Fatal(err)
622 return f
624 runCommand := func(c *exec.Cmd, out chan<- string) {
625 bout, err := c.CombinedOutput()
626 if err != nil {
627 out <- "ERROR:" + err.Error()
628 } else {
629 out <- string(bout)
633 for i := 0; i < 10; i++ {
634 la := listen()
635 ca := helperCommand(t, "describefiles")
636 ca.ExtraFiles = []*os.File{listenerFile(la)}
637 lb := listen()
638 cb := helperCommand(t, "describefiles")
639 cb.ExtraFiles = []*os.File{listenerFile(lb)}
640 ares := make(chan string)
641 bres := make(chan string)
642 go runCommand(ca, ares)
643 go runCommand(cb, bres)
644 if got, want := <-ares, fmt.Sprintf("fd3: listener %s\n", la.Addr()); got != want {
645 t.Errorf("iteration %d, process A got:\n%s\nwant:\n%s\n", i, got, want)
647 if got, want := <-bres, fmt.Sprintf("fd3: listener %s\n", lb.Addr()); got != want {
648 t.Errorf("iteration %d, process B got:\n%s\nwant:\n%s\n", i, got, want)
650 la.Close()
651 lb.Close()
652 for _, f := range ca.ExtraFiles {
653 f.Close()
655 for _, f := range cb.ExtraFiles {
656 f.Close()
662 // TestHelperProcess isn't a real test. It's used as a helper process
663 // for TestParameterRun.
664 func TestHelperProcess(*testing.T) {
665 if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
666 return
668 defer os.Exit(0)
670 // Determine which command to use to display open files.
671 ofcmd := "lsof"
672 switch runtime.GOOS {
673 case "dragonfly", "freebsd", "netbsd", "openbsd":
674 ofcmd = "fstat"
675 case "plan9":
676 ofcmd = "/bin/cat"
679 args := os.Args
680 for len(args) > 0 {
681 if args[0] == "--" {
682 args = args[1:]
683 break
685 args = args[1:]
687 if len(args) == 0 {
688 fmt.Fprintf(os.Stderr, "No command\n")
689 os.Exit(2)
692 cmd, args := args[0], args[1:]
693 switch cmd {
694 case "echo":
695 iargs := []interface{}{}
696 for _, s := range args {
697 iargs = append(iargs, s)
699 fmt.Println(iargs...)
700 case "echoenv":
701 for _, s := range args {
702 fmt.Println(os.Getenv(s))
704 os.Exit(0)
705 case "cat":
706 if len(args) == 0 {
707 io.Copy(os.Stdout, os.Stdin)
708 return
710 exit := 0
711 for _, fn := range args {
712 f, err := os.Open(fn)
713 if err != nil {
714 fmt.Fprintf(os.Stderr, "Error: %v\n", err)
715 exit = 2
716 } else {
717 defer f.Close()
718 io.Copy(os.Stdout, f)
721 os.Exit(exit)
722 case "pipetest":
723 bufr := bufio.NewReader(os.Stdin)
724 for {
725 line, _, err := bufr.ReadLine()
726 if err == io.EOF {
727 break
728 } else if err != nil {
729 os.Exit(1)
731 if bytes.HasPrefix(line, []byte("O:")) {
732 os.Stdout.Write(line)
733 os.Stdout.Write([]byte{'\n'})
734 } else if bytes.HasPrefix(line, []byte("E:")) {
735 os.Stderr.Write(line)
736 os.Stderr.Write([]byte{'\n'})
737 } else {
738 os.Exit(1)
741 case "stdinClose":
742 b, err := ioutil.ReadAll(os.Stdin)
743 if err != nil {
744 fmt.Fprintf(os.Stderr, "Error: %v\n", err)
745 os.Exit(1)
747 if s := string(b); s != stdinCloseTestString {
748 fmt.Fprintf(os.Stderr, "Error: Read %q, want %q", s, stdinCloseTestString)
749 os.Exit(1)
751 os.Exit(0)
752 case "read3": // read fd 3
753 fd3 := os.NewFile(3, "fd3")
754 bs, err := ioutil.ReadAll(fd3)
755 if err != nil {
756 fmt.Printf("ReadAll from fd 3: %v", err)
757 os.Exit(1)
759 switch runtime.GOOS {
760 case "dragonfly":
761 // TODO(jsing): Determine why DragonFly is leaking
762 // file descriptors...
763 case "darwin":
764 // TODO(bradfitz): broken? Sometimes.
765 // https://golang.org/issue/2603
766 // Skip this additional part of the test for now.
767 case "netbsd":
768 // TODO(jsing): This currently fails on NetBSD due to
769 // the cloned file descriptors that result from opening
770 // /dev/urandom.
771 // https://golang.org/issue/3955
772 case "solaris":
773 // TODO(aram): This fails on Solaris because libc opens
774 // its own files, as it sees fit. Darwin does the same,
775 // see: https://golang.org/issue/2603
776 default:
777 // Now verify that there are no other open fds.
778 var files []*os.File
779 for wantfd := basefds() + 1; wantfd <= 100; wantfd++ {
780 if wantfd == poll.PollDescriptor() {
781 continue
783 f, err := os.Open(os.Args[0])
784 if err != nil {
785 fmt.Printf("error opening file with expected fd %d: %v", wantfd, err)
786 os.Exit(1)
788 if got := f.Fd(); got != wantfd {
789 fmt.Printf("leaked parent file. fd = %d; want %d\n", got, wantfd)
790 var args []string
791 switch runtime.GOOS {
792 case "plan9":
793 args = []string{fmt.Sprintf("/proc/%d/fd", os.Getpid())}
794 default:
795 args = []string{"-p", fmt.Sprint(os.Getpid())}
797 out, _ := exec.Command(ofcmd, args...).CombinedOutput()
798 fmt.Print(string(out))
799 os.Exit(1)
801 files = append(files, f)
803 for _, f := range files {
804 f.Close()
807 // Referring to fd3 here ensures that it is not
808 // garbage collected, and therefore closed, while
809 // executing the wantfd loop above. It doesn't matter
810 // what we do with fd3 as long as we refer to it;
811 // closing it is the easy choice.
812 fd3.Close()
813 os.Stdout.Write(bs)
814 case "exit":
815 n, _ := strconv.Atoi(args[0])
816 os.Exit(n)
817 case "describefiles":
818 f := os.NewFile(3, fmt.Sprintf("fd3"))
819 ln, err := net.FileListener(f)
820 if err == nil {
821 fmt.Printf("fd3: listener %s\n", ln.Addr())
822 ln.Close()
824 os.Exit(0)
825 case "extraFilesAndPipes":
826 n, _ := strconv.Atoi(args[0])
827 pipes := make([]*os.File, n)
828 for i := 0; i < n; i++ {
829 pipes[i] = os.NewFile(uintptr(3+i), strconv.Itoa(i))
831 response := ""
832 for i, r := range pipes {
833 ch := make(chan string, 1)
834 go func(c chan string) {
835 buf := make([]byte, 10)
836 n, err := r.Read(buf)
837 if err != nil {
838 fmt.Fprintf(os.Stderr, "Child: read error: %v on pipe %d\n", err, i)
839 os.Exit(1)
841 c <- string(buf[:n])
842 close(c)
843 }(ch)
844 select {
845 case m := <-ch:
846 response = response + m
847 case <-time.After(5 * time.Second):
848 fmt.Fprintf(os.Stderr, "Child: Timeout reading from pipe: %d\n", i)
849 os.Exit(1)
852 fmt.Fprintf(os.Stderr, "child: %s", response)
853 os.Exit(0)
854 case "exec":
855 cmd := exec.Command(args[1])
856 cmd.Dir = args[0]
857 output, err := cmd.CombinedOutput()
858 if err != nil {
859 fmt.Fprintf(os.Stderr, "Child: %s %s", err, string(output))
860 os.Exit(1)
862 fmt.Printf("%s", string(output))
863 os.Exit(0)
864 case "lookpath":
865 p, err := exec.LookPath(args[0])
866 if err != nil {
867 fmt.Fprintf(os.Stderr, "LookPath failed: %v\n", err)
868 os.Exit(1)
870 fmt.Print(p)
871 os.Exit(0)
872 case "stderrfail":
873 fmt.Fprintf(os.Stderr, "some stderr text\n")
874 os.Exit(1)
875 case "sleep":
876 time.Sleep(3 * time.Second)
877 os.Exit(0)
878 default:
879 fmt.Fprintf(os.Stderr, "Unknown command %q\n", cmd)
880 os.Exit(2)
884 type delayedInfiniteReader struct{}
886 func (delayedInfiniteReader) Read(b []byte) (int, error) {
887 time.Sleep(100 * time.Millisecond)
888 for i := range b {
889 b[i] = 'x'
891 return len(b), nil
894 // Issue 9173: ignore stdin pipe writes if the program completes successfully.
895 func TestIgnorePipeErrorOnSuccess(t *testing.T) {
896 testenv.MustHaveExec(t)
898 // We really only care about testing this on Unixy and Windowsy things.
899 if runtime.GOOS == "plan9" {
900 t.Skipf("skipping test on %q", runtime.GOOS)
903 testWith := func(r io.Reader) func(*testing.T) {
904 return func(t *testing.T) {
905 cmd := helperCommand(t, "echo", "foo")
906 var out bytes.Buffer
907 cmd.Stdin = r
908 cmd.Stdout = &out
909 if err := cmd.Run(); err != nil {
910 t.Fatal(err)
912 if got, want := out.String(), "foo\n"; got != want {
913 t.Errorf("output = %q; want %q", got, want)
917 t.Run("10MB", testWith(strings.NewReader(strings.Repeat("x", 10<<20))))
918 t.Run("Infinite", testWith(delayedInfiniteReader{}))
921 type badWriter struct{}
923 func (w *badWriter) Write(data []byte) (int, error) {
924 return 0, io.ErrUnexpectedEOF
927 func TestClosePipeOnCopyError(t *testing.T) {
928 testenv.MustHaveExec(t)
930 if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
931 t.Skipf("skipping test on %s - no yes command", runtime.GOOS)
933 cmd := exec.Command("yes")
934 cmd.Stdout = new(badWriter)
935 c := make(chan int, 1)
936 go func() {
937 err := cmd.Run()
938 if err == nil {
939 t.Errorf("yes completed successfully")
941 c <- 1
943 select {
944 case <-c:
945 // ok
946 case <-time.After(5 * time.Second):
947 t.Fatalf("yes got stuck writing to bad writer")
951 func TestOutputStderrCapture(t *testing.T) {
952 testenv.MustHaveExec(t)
954 cmd := helperCommand(t, "stderrfail")
955 _, err := cmd.Output()
956 ee, ok := err.(*exec.ExitError)
957 if !ok {
958 t.Fatalf("Output error type = %T; want ExitError", err)
960 got := string(ee.Stderr)
961 want := "some stderr text\n"
962 if got != want {
963 t.Errorf("ExitError.Stderr = %q; want %q", got, want)
967 func TestContext(t *testing.T) {
968 ctx, cancel := context.WithCancel(context.Background())
969 c := helperCommandContext(t, ctx, "pipetest")
970 stdin, err := c.StdinPipe()
971 if err != nil {
972 t.Fatal(err)
974 stdout, err := c.StdoutPipe()
975 if err != nil {
976 t.Fatal(err)
978 if err := c.Start(); err != nil {
979 t.Fatal(err)
982 if _, err := stdin.Write([]byte("O:hi\n")); err != nil {
983 t.Fatal(err)
985 buf := make([]byte, 5)
986 n, err := io.ReadFull(stdout, buf)
987 if n != len(buf) || err != nil || string(buf) != "O:hi\n" {
988 t.Fatalf("ReadFull = %d, %v, %q", n, err, buf[:n])
990 waitErr := make(chan error, 1)
991 go func() {
992 waitErr <- c.Wait()
994 cancel()
995 select {
996 case err := <-waitErr:
997 if err == nil {
998 t.Fatal("expected Wait failure")
1000 case <-time.After(3 * time.Second):
1001 t.Fatal("timeout waiting for child process death")
1005 func TestContextCancel(t *testing.T) {
1006 ctx, cancel := context.WithCancel(context.Background())
1007 defer cancel()
1008 c := helperCommandContext(t, ctx, "cat")
1010 r, w, err := os.Pipe()
1011 if err != nil {
1012 t.Fatal(err)
1014 c.Stdin = r
1016 stdout, err := c.StdoutPipe()
1017 if err != nil {
1018 t.Fatal(err)
1020 readDone := make(chan struct{})
1021 go func() {
1022 defer close(readDone)
1023 var a [1024]byte
1024 for {
1025 n, err := stdout.Read(a[:])
1026 if err != nil {
1027 if err != io.EOF {
1028 t.Errorf("unexpected read error: %v", err)
1030 return
1032 t.Logf("%s", a[:n])
1036 if err := c.Start(); err != nil {
1037 t.Fatal(err)
1040 if err := r.Close(); err != nil {
1041 t.Fatal(err)
1044 if _, err := io.WriteString(w, "echo"); err != nil {
1045 t.Fatal(err)
1048 cancel()
1050 // Calling cancel should have killed the process, so writes
1051 // should now fail. Give the process a little while to die.
1052 start := time.Now()
1053 for {
1054 if _, err := io.WriteString(w, "echo"); err != nil {
1055 break
1057 if time.Since(start) > time.Second {
1058 t.Fatal("canceling context did not stop program")
1060 time.Sleep(time.Millisecond)
1063 if err := w.Close(); err != nil {
1064 t.Errorf("error closing write end of pipe: %v", err)
1066 <-readDone
1068 if err := c.Wait(); err == nil {
1069 t.Error("program unexpectedly exited successfully")
1070 } else {
1071 t.Logf("exit status: %v", err)
1075 // test that environment variables are de-duped.
1076 func TestDedupEnvEcho(t *testing.T) {
1077 testenv.MustHaveExec(t)
1079 cmd := helperCommand(t, "echoenv", "FOO")
1080 cmd.Env = append(cmd.Env, "FOO=bad", "FOO=good")
1081 out, err := cmd.CombinedOutput()
1082 if err != nil {
1083 t.Fatal(err)
1085 if got, want := strings.TrimSpace(string(out)), "good"; got != want {
1086 t.Errorf("output = %q; want %q", got, want)