| 1 |
// Copyright 2023 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 unix |
| 6 |
|
| 7 |
package runtime_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"internal/testenv" |
| 11 |
"os" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
) |
| 15 |
|
| 16 |
func TestCheckFDs(t *testing.T) { |
| 17 |
if *flagQuick { |
| 18 |
t.Skip("-quick") |
| 19 |
} |
| 20 |
|
| 21 |
testenv.MustHaveGoBuild(t) |
| 22 |
|
| 23 |
fdsBin, err := buildTestProg(t, "testfds") |
| 24 |
if err != nil { |
| 25 |
t.Fatal(err) |
| 26 |
} |
| 27 |
|
| 28 |
i, err := os.CreateTemp(t.TempDir(), "fds-input") |
| 29 |
if err != nil { |
| 30 |
t.Fatal(err) |
| 31 |
} |
| 32 |
if _, err := i.Write([]byte("stdin")); err != nil { |
| 33 |
t.Fatal(err) |
| 34 |
} |
| 35 |
if err := i.Close(); err != nil { |
| 36 |
t.Fatal(err) |
| 37 |
} |
| 38 |
|
| 39 |
o, err := os.CreateTemp(t.TempDir(), "fds-output") |
| 40 |
if err != nil { |
| 41 |
t.Fatal(err) |
| 42 |
} |
| 43 |
outputPath := o.Name() |
| 44 |
if err := o.Close(); err != nil { |
| 45 |
t.Fatal(err) |
| 46 |
} |
| 47 |
|
| 48 |
env := []string{"TEST_OUTPUT=" + outputPath} |
| 49 |
for _, e := range os.Environ() { |
| 50 |
if strings.HasPrefix(e, "GODEBUG=") || strings.HasPrefix(e, "GOTRACEBACK=") { |
| 51 |
continue |
| 52 |
} |
| 53 |
env = append(env, e) |
| 54 |
} |
| 55 |
|
| 56 |
proc, err := os.StartProcess(fdsBin, []string{fdsBin}, &os.ProcAttr{ |
| 57 |
Env: env, |
| 58 |
Files: []*os.File{}, |
| 59 |
}) |
| 60 |
if err != nil { |
| 61 |
t.Fatal(err) |
| 62 |
} |
| 63 |
ps, err := proc.Wait() |
| 64 |
if err != nil { |
| 65 |
t.Fatal(err) |
| 66 |
} |
| 67 |
if ps.ExitCode() != 0 { |
| 68 |
t.Fatalf("testfds failed: %d", ps.ExitCode()) |
| 69 |
} |
| 70 |
|
| 71 |
fc, err := os.ReadFile(outputPath) |
| 72 |
if err != nil { |
| 73 |
t.Fatal(err) |
| 74 |
} |
| 75 |
if string(fc) != "" { |
| 76 |
t.Errorf("unexpected file content, got: %q", string(fc)) |
| 77 |
} |
| 78 |
} |