|
| 1 | +// Copyright 2015 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 | +// Test broken pipes on Unix systems. |
| 6 | +// +build !windows,!plan9,!nacl |
| 7 | + |
| 8 | +package os_test |
| 9 | + |
| 10 | +import ( |
| 11 | + "fmt" |
| 12 | + "internal/testenv" |
| 13 | + "os" |
| 14 | + osexec "os/exec" |
| 15 | + "os/signal" |
| 16 | + "runtime" |
| 17 | + "syscall" |
| 18 | + "testing" |
| 19 | +) |
| 20 | + |
| 21 | +func TestEPIPE(t *testing.T) { |
| 22 | + r, w, err := os.Pipe() |
| 23 | + if err != nil { |
| 24 | + t.Fatal(err) |
| 25 | + } |
| 26 | + if err := r.Close(); err != nil { |
| 27 | + t.Fatal(err) |
| 28 | + } |
| 29 | + |
| 30 | + // Every time we write to the pipe we should get an EPIPE. |
| 31 | + for i := 0; i < 20; i++ { |
| 32 | + _, err = w.Write([]byte("hi")) |
| 33 | + if err == nil { |
| 34 | + t.Fatal("unexpected success of Write to broken pipe") |
| 35 | + } |
| 36 | + if pe, ok := err.(*os.PathError); ok { |
| 37 | + err = pe.Err |
| 38 | + } |
| 39 | + if se, ok := err.(*os.SyscallError); ok { |
| 40 | + err = se.Err |
| 41 | + } |
| 42 | + if err != syscall.EPIPE { |
| 43 | + t.Errorf("iteration %d: got %v, expected EPIPE", i, err) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestStdPipe(t *testing.T) { |
| 49 | + testenv.MustHaveExec(t) |
| 50 | + r, w, err := os.Pipe() |
| 51 | + if err != nil { |
| 52 | + t.Fatal(err) |
| 53 | + } |
| 54 | + if err := r.Close(); err != nil { |
| 55 | + t.Fatal(err) |
| 56 | + } |
| 57 | + // Invoke the test program to run the test and write to a closed pipe. |
| 58 | + // If sig is false: |
| 59 | + // writing to stdout or stderr should cause an immediate SIGPIPE; |
| 60 | + // writing to descriptor 3 should fail with EPIPE and then exit 0. |
| 61 | + // If sig is true: |
| 62 | + // all writes should fail with EPIPE and then exit 0. |
| 63 | + for _, sig := range []bool{false, true} { |
| 64 | + for dest := 1; dest < 4; dest++ { |
| 65 | + cmd := osexec.Command(os.Args[0], "-test.run", "TestStdPipeHelper") |
| 66 | + cmd.Stdout = w |
| 67 | + cmd.Stderr = w |
| 68 | + cmd.ExtraFiles = []*os.File{w} |
| 69 | + cmd.Env = append(os.Environ(), fmt.Sprintf("GO_TEST_STD_PIPE_HELPER=%d", dest)) |
| 70 | + if sig { |
| 71 | + cmd.Env = append(cmd.Env, "GO_TEST_STD_PIPE_HELPER_SIGNAL=1") |
| 72 | + } |
| 73 | + if err := cmd.Run(); err == nil { |
| 74 | + if !sig && dest < 3 { |
| 75 | + t.Errorf("unexpected success of write to closed pipe %d sig %t in child", dest, sig) |
| 76 | + } |
| 77 | + } else if ee, ok := err.(*osexec.ExitError); !ok { |
| 78 | + t.Errorf("unexpected exec error type %T: %v", err, err) |
| 79 | + } else if ws, ok := ee.Sys().(syscall.WaitStatus); !ok { |
| 80 | + t.Errorf("unexpected wait status type %T: %v", ee.Sys(), ee.Sys()) |
| 81 | + } else if ws.Signaled() && ws.Signal() == syscall.SIGPIPE { |
| 82 | + if sig || dest > 2 { |
| 83 | + t.Errorf("unexpected SIGPIPE signal for descriptor %d sig %t", dest, sig) |
| 84 | + } |
| 85 | + } else { |
| 86 | + t.Errorf("unexpected exit status %v for descriptor %ds sig %t", err, dest, sig) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// This is a helper for TestStdPipe. It's not a test in itself. |
| 93 | +func TestStdPipeHelper(t *testing.T) { |
| 94 | + if os.Getenv("GO_TEST_STD_PIPE_HELPER_SIGNAL") != "" { |
| 95 | + signal.Notify(make(chan os.Signal, 1), syscall.SIGPIPE) |
| 96 | + } |
| 97 | + switch os.Getenv("GO_TEST_STD_PIPE_HELPER") { |
| 98 | + case "1": |
| 99 | + os.Stdout.Write([]byte("stdout")) |
| 100 | + case "2": |
| 101 | + os.Stderr.Write([]byte("stderr")) |
| 102 | + case "3": |
| 103 | + if _, err := os.NewFile(3, "3").Write([]byte("3")); err == nil { |
| 104 | + os.Exit(3) |
| 105 | + } |
| 106 | + default: |
| 107 | + t.Skip("skipping test helper") |
| 108 | + } |
| 109 | + // For stdout/stderr, we should have crashed with a broken pipe error. |
| 110 | + // The caller will be looking for that exit status, |
| 111 | + // so just exit normally here to cause a failure in the caller. |
| 112 | + // For descriptor 3, a normal exit is expected. |
| 113 | + os.Exit(0) |
| 114 | +} |
0 commit comments