OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [os/] [exec/] [exec_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
package exec
6
 
7
import (
8
        "bufio"
9
        "bytes"
10
        "fmt"
11
        "io"
12
        "io/ioutil"
13
        "net"
14
        "net/http"
15
        "net/http/httptest"
16
        "os"
17
        "runtime"
18
        "strconv"
19
        "strings"
20
        "syscall"
21
        "testing"
22
)
23
 
24
func helperCommand(s ...string) *Cmd {
25
        cs := []string{"-test.run=TestHelperProcess", "--"}
26
        cs = append(cs, s...)
27
        cmd := Command(os.Args[0], cs...)
28
        cmd.Env = append([]string{"GO_WANT_HELPER_PROCESS=1"}, os.Environ()...)
29
        return cmd
30
}
31
 
32
func TestEcho(t *testing.T) {
33
        bs, err := helperCommand("echo", "foo bar", "baz").Output()
34
        if err != nil {
35
                t.Errorf("echo: %v", err)
36
        }
37
        if g, e := string(bs), "foo bar baz\n"; g != e {
38
                t.Errorf("echo: want %q, got %q", e, g)
39
        }
40
}
41
 
42
func TestCatStdin(t *testing.T) {
43
        // Cat, testing stdin and stdout.
44
        input := "Input string\nLine 2"
45
        p := helperCommand("cat")
46
        p.Stdin = strings.NewReader(input)
47
        bs, err := p.Output()
48
        if err != nil {
49
                t.Errorf("cat: %v", err)
50
        }
51
        s := string(bs)
52
        if s != input {
53
                t.Errorf("cat: want %q, got %q", input, s)
54
        }
55
}
56
 
57
func TestCatGoodAndBadFile(t *testing.T) {
58
        // Testing combined output and error values.
59
        bs, err := helperCommand("cat", "/bogus/file.foo", "exec_test.go").CombinedOutput()
60
        if _, ok := err.(*ExitError); !ok {
61
                t.Errorf("expected *ExitError from cat combined; got %T: %v", err, err)
62
        }
63
        s := string(bs)
64
        sp := strings.SplitN(s, "\n", 2)
65
        if len(sp) != 2 {
66
                t.Fatalf("expected two lines from cat; got %q", s)
67
        }
68
        errLine, body := sp[0], sp[1]
69
        if !strings.HasPrefix(errLine, "Error: open /bogus/file.foo") {
70
                t.Errorf("expected stderr to complain about file; got %q", errLine)
71
        }
72
        if !strings.Contains(body, "func TestHelperProcess(t *testing.T)") {
73
                t.Errorf("expected test code; got %q (len %d)", body, len(body))
74
        }
75
}
76
 
77
func TestNoExistBinary(t *testing.T) {
78
        // Can't run a non-existent binary
79
        err := Command("/no-exist-binary").Run()
80
        if err == nil {
81
                t.Error("expected error from /no-exist-binary")
82
        }
83
}
84
 
85
func TestExitStatus(t *testing.T) {
86
        // Test that exit values are returned correctly
87
        err := helperCommand("exit", "42").Run()
88
        if werr, ok := err.(*ExitError); ok {
89
                if s, e := werr.Error(), "exit status 42"; s != e {
90
                        t.Errorf("from exit 42 got exit %q, want %q", s, e)
91
                }
92
        } else {
93
                t.Fatalf("expected *ExitError from exit 42; got %T: %v", err, err)
94
        }
95
}
96
 
97
func TestPipes(t *testing.T) {
98
        check := func(what string, err error) {
99
                if err != nil {
100
                        t.Fatalf("%s: %v", what, err)
101
                }
102
        }
103
        // Cat, testing stdin and stdout.
104
        c := helperCommand("pipetest")
105
        stdin, err := c.StdinPipe()
106
        check("StdinPipe", err)
107
        stdout, err := c.StdoutPipe()
108
        check("StdoutPipe", err)
109
        stderr, err := c.StderrPipe()
110
        check("StderrPipe", err)
111
 
112
        outbr := bufio.NewReader(stdout)
113
        errbr := bufio.NewReader(stderr)
114
        line := func(what string, br *bufio.Reader) string {
115
                line, _, err := br.ReadLine()
116
                if err != nil {
117
                        t.Fatalf("%s: %v", what, err)
118
                }
119
                return string(line)
120
        }
121
 
122
        err = c.Start()
123
        check("Start", err)
124
 
125
        _, err = stdin.Write([]byte("O:I am output\n"))
126
        check("first stdin Write", err)
127
        if g, e := line("first output line", outbr), "O:I am output"; g != e {
128
                t.Errorf("got %q, want %q", g, e)
129
        }
130
 
131
        _, err = stdin.Write([]byte("E:I am error\n"))
132
        check("second stdin Write", err)
133
        if g, e := line("first error line", errbr), "E:I am error"; g != e {
134
                t.Errorf("got %q, want %q", g, e)
135
        }
136
 
137
        _, err = stdin.Write([]byte("O:I am output2\n"))
138
        check("third stdin Write 3", err)
139
        if g, e := line("second output line", outbr), "O:I am output2"; g != e {
140
                t.Errorf("got %q, want %q", g, e)
141
        }
142
 
143
        stdin.Close()
144
        err = c.Wait()
145
        check("Wait", err)
146
}
147
 
148
func TestExtraFiles(t *testing.T) {
149
        if runtime.GOOS == "windows" {
150
                t.Logf("no operating system support; skipping")
151
                return
152
        }
153
 
154
        // Ensure that file descriptors have not already been leaked into
155
        // our environment.
156
        for fd := os.Stderr.Fd() + 1; fd <= 101; fd++ {
157
                err := syscall.Close(fd)
158
                if err == nil {
159
                        t.Logf("Something already leaked - closed fd %d", fd)
160
                }
161
        }
162
 
163
        // Force network usage, to verify the epoll (or whatever) fd
164
        // doesn't leak to the child,
165
        ln, err := net.Listen("tcp", "127.0.0.1:0")
166
        if err != nil {
167
                t.Fatal(err)
168
        }
169
        defer ln.Close()
170
 
171
        // Force TLS root certs to be loaded (which might involve
172
        // cgo), to make sure none of that potential C code leaks fds.
173
        ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
174
                w.Write([]byte("Hello"))
175
        }))
176
        defer ts.Close()
177
        http.Get(ts.URL) // ignore result; just calling to force root cert loading
178
 
179
        tf, err := ioutil.TempFile("", "")
180
        if err != nil {
181
                t.Fatalf("TempFile: %v", err)
182
        }
183
        defer os.Remove(tf.Name())
184
        defer tf.Close()
185
 
186
        const text = "Hello, fd 3!"
187
        _, err = tf.Write([]byte(text))
188
        if err != nil {
189
                t.Fatalf("Write: %v", err)
190
        }
191
        _, err = tf.Seek(0, os.SEEK_SET)
192
        if err != nil {
193
                t.Fatalf("Seek: %v", err)
194
        }
195
 
196
        c := helperCommand("read3")
197
        c.ExtraFiles = []*os.File{tf}
198
        bs, err := c.CombinedOutput()
199
        if err != nil {
200
                t.Fatalf("CombinedOutput: %v; output %q", err, bs)
201
        }
202
        if string(bs) != text {
203
                t.Errorf("got %q; want %q", string(bs), text)
204
        }
205
}
206
 
207
// TestHelperProcess isn't a real test. It's used as a helper process
208
// for TestParameterRun.
209
func TestHelperProcess(*testing.T) {
210
        if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
211
                return
212
        }
213
        defer os.Exit(0)
214
 
215
        // Determine which command to use to display open files.
216
        ofcmd := "lsof"
217
        switch runtime.GOOS {
218
        case "freebsd", "netbsd", "openbsd":
219
                ofcmd = "fstat"
220
        }
221
 
222
        args := os.Args
223
        for len(args) > 0 {
224
                if args[0] == "--" {
225
                        args = args[1:]
226
                        break
227
                }
228
                args = args[1:]
229
        }
230
        if len(args) == 0 {
231
                fmt.Fprintf(os.Stderr, "No command\n")
232
                os.Exit(2)
233
        }
234
 
235
        cmd, args := args[0], args[1:]
236
        switch cmd {
237
        case "echo":
238
                iargs := []interface{}{}
239
                for _, s := range args {
240
                        iargs = append(iargs, s)
241
                }
242
                fmt.Println(iargs...)
243
        case "cat":
244
                if len(args) == 0 {
245
                        io.Copy(os.Stdout, os.Stdin)
246
                        return
247
                }
248
                exit := 0
249
                for _, fn := range args {
250
                        f, err := os.Open(fn)
251
                        if err != nil {
252
                                fmt.Fprintf(os.Stderr, "Error: %v\n", err)
253
                                exit = 2
254
                        } else {
255
                                defer f.Close()
256
                                io.Copy(os.Stdout, f)
257
                        }
258
                }
259
                os.Exit(exit)
260
        case "pipetest":
261
                bufr := bufio.NewReader(os.Stdin)
262
                for {
263
                        line, _, err := bufr.ReadLine()
264
                        if err == io.EOF {
265
                                break
266
                        } else if err != nil {
267
                                os.Exit(1)
268
                        }
269
                        if bytes.HasPrefix(line, []byte("O:")) {
270
                                os.Stdout.Write(line)
271
                                os.Stdout.Write([]byte{'\n'})
272
                        } else if bytes.HasPrefix(line, []byte("E:")) {
273
                                os.Stderr.Write(line)
274
                                os.Stderr.Write([]byte{'\n'})
275
                        } else {
276
                                os.Exit(1)
277
                        }
278
                }
279
        case "read3": // read fd 3
280
                fd3 := os.NewFile(3, "fd3")
281
                bs, err := ioutil.ReadAll(fd3)
282
                if err != nil {
283
                        fmt.Printf("ReadAll from fd 3: %v", err)
284
                        os.Exit(1)
285
                }
286
                switch runtime.GOOS {
287
                case "darwin":
288
                        // TODO(bradfitz): broken? Sometimes.
289
                        // http://golang.org/issue/2603
290
                        // Skip this additional part of the test for now.
291
                default:
292
                        // Now verify that there are no other open fds.
293
                        var files []*os.File
294
                        for wantfd := os.Stderr.Fd() + 2; wantfd <= 100; wantfd++ {
295
                                f, err := os.Open(os.Args[0])
296
                                if err != nil {
297
                                        fmt.Printf("error opening file with expected fd %d: %v", wantfd, err)
298
                                        os.Exit(1)
299
                                }
300
                                if got := f.Fd(); got != wantfd {
301
                                        fmt.Printf("leaked parent file. fd = %d; want %d\n", got, wantfd)
302
                                        out, _ := Command(ofcmd, "-p", fmt.Sprint(os.Getpid())).CombinedOutput()
303
                                        fmt.Print(string(out))
304
                                        os.Exit(1)
305
                                }
306
                                files = append(files, f)
307
                        }
308
                        for _, f := range files {
309
                                f.Close()
310
                        }
311
                }
312
                // Referring to fd3 here ensures that it is not
313
                // garbage collected, and therefore closed, while
314
                // executing the wantfd loop above.  It doesn't matter
315
                // what we do with fd3 as long as we refer to it;
316
                // closing it is the easy choice.
317
                fd3.Close()
318
                os.Stderr.Write(bs)
319
        case "exit":
320
                n, _ := strconv.Atoi(args[0])
321
                os.Exit(n)
322
        default:
323
                fmt.Fprintf(os.Stderr, "Unknown command %q\n", cmd)
324
                os.Exit(2)
325
        }
326
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.