1 [GOOS:windows] skip 'filesystem normalizes / to \'
2 [GOOS:plan9] skip 'filesystem disallows \n in paths'
3
4 # If the directory path containing a package to be built includes a newline,
5 # the go command should refuse to even try to build the package.
6
7 env DIR=$WORK${/}${newline}'package main'${newline}'func main() { panic("uh-oh")'${newline}'/*'
8
9 mkdir $DIR
10 cd $DIR
11 exec pwd
12 cp $WORK/go.mod ./go.mod
13 cp $WORK/main.go ./main.go
14 cp $WORK/main_nocgo.go ./main_nocgo.go
15 cp $WORK/main_test.go ./main_test.go
16
17 ! go build -o $devnull .
18 stderr 'package example: invalid package directory .*uh-oh'
19
20 [cgo] ! go build -o $devnull main.go
21 [!cgo] ! go build -o $devnull main_nocgo.go
22 stderr 'package command-line-arguments: invalid package directory .*uh-oh'
23
24 ! go run .
25 stderr 'package example: invalid package directory .*uh-oh'
26
27 [cgo] ! go run main.go
28 [!cgo] ! go run main_nocgo.go
29 stderr 'package command-line-arguments: invalid package directory .*uh-oh'
30
31 ! go test .
32 stderr 'package example: invalid package directory .*uh-oh'
33
34 [cgo] ! go test -v main.go main_test.go
35 [!cgo] ! go test -v main_nocgo.go main_test.go
36 stderr 'package command-line-arguments: invalid package directory .*uh-oh'
37
38 go list -compiled -e -f '{{with .CompiledGoFiles}}{{.}}{{end}}' .
39 ! stdout .
40 ! stderr .
41 ! exists obj_
42
43
44 # The cgo tool should only accept the source file if the working directory
45 # is not written in line directives in the resulting files.
46
47 [cgo] ! go tool cgo main.go
48 [cgo] stderr 'cgo: input path contains newline character: .*uh-oh'
49 [cgo] ! exists _obj
50
51 [cgo] go tool cgo -trimpath=$PWD main.go
52 [cgo] grep '//line main\.go:1:1' _obj/main.cgo1.go
53 [cgo] ! grep 'uh-oh' _obj/main.cgo1.go
54 [cgo] rm _obj
55
56
57 # Since we do preserve $PWD (or set it appropriately) for commands, and we do
58 # not resolve symlinks unnecessarily, referring to the contents of the unsafe
59 # directory via a safe symlink should be ok, and should not inject the data from
60 # the symlink target path.
61
62 [!symlink] stop 'remainder of test checks symlink behavior'
63 [short] stop 'links and runs binaries'
64
65 symlink $WORK${/}link -> $DIR
66
67 [cgo] go run $WORK${/}link${/}main.go
68 [!cgo] go run $WORK${/}link${/}main_nocgo.go
69 ! stdout panic
70 ! stderr panic
71 stderr '^ok$'
72
73 [cgo] go test -v $WORK${/}link${/}main.go $WORK${/}link${/}main_test.go
74 [!cgo] go test -v $WORK${/}link${/}main_nocgo.go $WORK${/}link${/}main_test.go
75 ! stdout panic
76 ! stderr panic
77 stdout '^ok$' # 'go test' combines the test's stdout into stderr
78
79 cd $WORK/link
80
81 [cgo] ! go run $DIR${/}main.go
82 [!cgo] ! go run $DIR${/}main_nocgo.go
83 stderr 'package command-line-arguments: invalid package directory .*uh-oh'
84
85 go run .
86 ! stdout panic
87 ! stderr panic
88 stderr '^ok$'
89
90 [cgo] go run main.go
91 [!cgo] go run main_nocgo.go
92 ! stdout panic
93 ! stderr panic
94 stderr '^ok$'
95
96 go test -v
97 ! stdout panic
98 ! stderr panic
99 stdout '^ok$' # 'go test' combines the test's stdout into stderr
100
101 go test -v .
102 ! stdout panic
103 ! stderr panic
104 stdout '^ok$' # 'go test' combines the test's stdout into stderr
105
106 [cgo] go tool cgo main.go
107 [cgo] grep '//line .*'${/}'link'${/}'main\.go:1:1' _obj/main.cgo1.go
108 [cgo] ! grep 'uh-oh' _obj/main.cgo1.go
109
110 -- $WORK/go.mod --
111 module example
112 go 1.19
113 -- $WORK/main.go --
114 package main
115
116 import "C"
117
118 func main() {
119 /* nothing here */
120 println("ok")
121 }
122 -- $WORK/main_nocgo.go --
123 //go:build !cgo
124
125 package main
126
127 func main() {
128 /* nothing here */
129 println("ok")
130 }
131 -- $WORK/main_test.go --
132 package main
133
134 import "testing"
135
136 func TestMain(*testing.M) {
137 main()
138 }
139
View as plain text