|
5 | 5 | package syscall_test
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "fmt" |
| 9 | + "internal/testenv" |
8 | 10 | "os"
|
| 11 | + "os/exec" |
9 | 12 | "path/filepath"
|
| 13 | + "strings" |
10 | 14 | "syscall"
|
11 | 15 | "testing"
|
12 | 16 | )
|
@@ -71,3 +75,64 @@ func TestTOKEN_ALL_ACCESS(t *testing.T) {
|
71 | 75 | t.Errorf("TOKEN_ALL_ACCESS = %x, want 0xF01FF", syscall.TOKEN_ALL_ACCESS)
|
72 | 76 | }
|
73 | 77 | }
|
| 78 | + |
| 79 | +func TestStdioAreInheritable(t *testing.T) { |
| 80 | + testenv.MustHaveGoBuild(t) |
| 81 | + testenv.MustHaveExecPath(t, "gcc") |
| 82 | + |
| 83 | + tmpdir := t.TempDir() |
| 84 | + |
| 85 | + // build go dll |
| 86 | + const dlltext = ` |
| 87 | +package main |
| 88 | +
|
| 89 | +import "C" |
| 90 | +import ( |
| 91 | + "fmt" |
| 92 | +) |
| 93 | +
|
| 94 | +//export HelloWorld |
| 95 | +func HelloWorld() { |
| 96 | + fmt.Println("Hello World") |
| 97 | +} |
| 98 | +
|
| 99 | +func main() {} |
| 100 | +` |
| 101 | + dllsrc := filepath.Join(tmpdir, "helloworld.go") |
| 102 | + err := os.WriteFile(dllsrc, []byte(dlltext), 0644) |
| 103 | + if err != nil { |
| 104 | + t.Fatal(err) |
| 105 | + } |
| 106 | + dll := filepath.Join(tmpdir, "helloworld.dll") |
| 107 | + cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", dll, "-buildmode", "c-shared", dllsrc) |
| 108 | + out, err := testenv.CleanCmdEnv(cmd).CombinedOutput() |
| 109 | + if err != nil { |
| 110 | + t.Fatalf("failed to build go library: %s\n%s", err, out) |
| 111 | + } |
| 112 | + |
| 113 | + // run powershell script |
| 114 | + psscript := fmt.Sprintf(` |
| 115 | +hostname; |
| 116 | +$signature = " [DllImport("%q")] public static extern void HelloWorld(); "; |
| 117 | +Add-Type -MemberDefinition $signature -Name World -Namespace Hello; |
| 118 | +[Hello.World]::HelloWorld(); |
| 119 | +hostname; |
| 120 | +`, dll) |
| 121 | + psscript = strings.ReplaceAll(psscript, "\n", "") |
| 122 | + out, err = exec.Command("powershell", "-Command", psscript).CombinedOutput() |
| 123 | + if err != nil { |
| 124 | + t.Fatalf("Powershell command failed: %v: %v", err, string(out)) |
| 125 | + } |
| 126 | + |
| 127 | + hostname, err := os.Hostname() |
| 128 | + if err != nil { |
| 129 | + t.Fatal(err) |
| 130 | + } |
| 131 | + |
| 132 | + have := strings.ReplaceAll(string(out), "\n", "") |
| 133 | + have = strings.ReplaceAll(have, "\r", "") |
| 134 | + want := fmt.Sprintf("%sHello World%s", hostname, hostname) |
| 135 | + if have != want { |
| 136 | + t.Fatalf("Powershell command output is wrong: got %q, want %q", have, want) |
| 137 | + } |
| 138 | +} |
0 commit comments