github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/not-internal/work/security_test.go (about) 1 // Copyright 2018 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 work 6 7 import ( 8 "os" 9 "testing" 10 ) 11 12 var goodCompilerFlags = [][]string{ 13 {"-DFOO"}, 14 {"-Dfoo=bar"}, 15 {"-Ufoo"}, 16 {"-F/Qt"}, 17 {"-I/"}, 18 {"-I/etc/passwd"}, 19 {"-I."}, 20 {"-O"}, 21 {"-O2"}, 22 {"-Osmall"}, 23 {"-W"}, 24 {"-Wall"}, 25 {"-Wp,-Dfoo=bar"}, 26 {"-Wp,-Ufoo"}, 27 {"-fobjc-arc"}, 28 {"-fno-objc-arc"}, 29 {"-fomit-frame-pointer"}, 30 {"-fno-omit-frame-pointer"}, 31 {"-fpic"}, 32 {"-fno-pic"}, 33 {"-fPIC"}, 34 {"-fno-PIC"}, 35 {"-fpie"}, 36 {"-fno-pie"}, 37 {"-fPIE"}, 38 {"-fno-PIE"}, 39 {"-fsplit-stack"}, 40 {"-fno-split-stack"}, 41 {"-fstack-xxx"}, 42 {"-fno-stack-xxx"}, 43 {"-fsanitize=hands"}, 44 {"-g"}, 45 {"-ggdb"}, 46 {"-march=souza"}, 47 {"-mcpu=123"}, 48 {"-mfpu=123"}, 49 {"-mtune=happybirthday"}, 50 {"-mstack-overflow"}, 51 {"-mno-stack-overflow"}, 52 {"-mmacosx-version"}, 53 {"-mnop-fun-dllimport"}, 54 {"-pthread"}, 55 {"-std=c99"}, 56 {"-xc"}, 57 {"-D", "FOO"}, 58 {"-D", "foo=bar"}, 59 {"-I", "."}, 60 {"-I", "/etc/passwd"}, 61 {"-I", "世界"}, 62 {"-I", "=/usr/include/libxml2"}, 63 {"-I", "dir"}, 64 {"-I", "$SYSROOT/dir"}, 65 {"-framework", "Chocolate"}, 66 {"-x", "c"}, 67 {"-v"}, 68 } 69 70 var badCompilerFlags = [][]string{ 71 {"-D@X"}, 72 {"-D-X"}, 73 {"-Ufoo=bar"}, 74 {"-F@dir"}, 75 {"-F-dir"}, 76 {"-I@dir"}, 77 {"-I-dir"}, 78 {"-O@1"}, 79 {"-Wa,-foo"}, 80 {"-W@foo"}, 81 {"-g@gdb"}, 82 {"-g-gdb"}, 83 {"-march=@dawn"}, 84 {"-march=-dawn"}, 85 {"-std=@c99"}, 86 {"-std=-c99"}, 87 {"-x@c"}, 88 {"-x-c"}, 89 {"-D", "@foo"}, 90 {"-D", "-foo"}, 91 {"-I", "@foo"}, 92 {"-I", "-foo"}, 93 {"-I", "=@obj"}, 94 {"-framework", "-Caffeine"}, 95 {"-framework", "@Home"}, 96 {"-x", "--c"}, 97 {"-x", "@obj"}, 98 } 99 100 func TestCheckCompilerFlags(t *testing.T) { 101 for _, f := range goodCompilerFlags { 102 if err := checkCompilerFlags("test", "test", f); err != nil { 103 t.Errorf("unexpected error for %q: %v", f, err) 104 } 105 } 106 for _, f := range badCompilerFlags { 107 if err := checkCompilerFlags("test", "test", f); err == nil { 108 t.Errorf("missing error for %q", f) 109 } 110 } 111 } 112 113 var goodLinkerFlags = [][]string{ 114 {"-Fbar"}, 115 {"-lbar"}, 116 {"-Lbar"}, 117 {"-fpic"}, 118 {"-fno-pic"}, 119 {"-fPIC"}, 120 {"-fno-PIC"}, 121 {"-fpie"}, 122 {"-fno-pie"}, 123 {"-fPIE"}, 124 {"-fno-PIE"}, 125 {"-fsanitize=hands"}, 126 {"-g"}, 127 {"-ggdb"}, 128 {"-march=souza"}, 129 {"-mcpu=123"}, 130 {"-mfpu=123"}, 131 {"-mtune=happybirthday"}, 132 {"-pic"}, 133 {"-pthread"}, 134 {"-Wl,-rpath,foo"}, 135 {"-Wl,-rpath,$ORIGIN/foo"}, 136 {"-Wl,-R", "/foo"}, 137 {"-Wl,-R", "foo"}, 138 {"-Wl,-R,foo"}, 139 {"-Wl,--just-symbols=foo"}, 140 {"-Wl,--just-symbols,foo"}, 141 {"-Wl,--warn-error"}, 142 {"-Wl,--no-warn-error"}, 143 {"foo.so"}, 144 {"_世界.dll"}, 145 {"./x.o"}, 146 {"libcgosotest.dylib"}, 147 {"-F", "framework"}, 148 {"-l", "."}, 149 {"-l", "/etc/passwd"}, 150 {"-l", "世界"}, 151 {"-L", "framework"}, 152 {"-framework", "Chocolate"}, 153 {"-v"}, 154 {"-Wl,-sectcreate,__TEXT,__info_plist,${SRCDIR}/Info.plist"}, 155 {"-Wl,-framework", "-Wl,Chocolate"}, 156 {"-Wl,-framework,Chocolate"}, 157 {"-Wl,-unresolved-symbols=ignore-all"}, 158 } 159 160 var badLinkerFlags = [][]string{ 161 {"-DFOO"}, 162 {"-Dfoo=bar"}, 163 {"-W"}, 164 {"-Wall"}, 165 {"-fobjc-arc"}, 166 {"-fno-objc-arc"}, 167 {"-fomit-frame-pointer"}, 168 {"-fno-omit-frame-pointer"}, 169 {"-fsplit-stack"}, 170 {"-fno-split-stack"}, 171 {"-fstack-xxx"}, 172 {"-fno-stack-xxx"}, 173 {"-mstack-overflow"}, 174 {"-mno-stack-overflow"}, 175 {"-mnop-fun-dllimport"}, 176 {"-std=c99"}, 177 {"-xc"}, 178 {"-D", "FOO"}, 179 {"-D", "foo=bar"}, 180 {"-I", "FOO"}, 181 {"-L", "@foo"}, 182 {"-L", "-foo"}, 183 {"-x", "c"}, 184 {"-D@X"}, 185 {"-D-X"}, 186 {"-I@dir"}, 187 {"-I-dir"}, 188 {"-O@1"}, 189 {"-Wa,-foo"}, 190 {"-W@foo"}, 191 {"-g@gdb"}, 192 {"-g-gdb"}, 193 {"-march=@dawn"}, 194 {"-march=-dawn"}, 195 {"-std=@c99"}, 196 {"-std=-c99"}, 197 {"-x@c"}, 198 {"-x-c"}, 199 {"-D", "@foo"}, 200 {"-D", "-foo"}, 201 {"-I", "@foo"}, 202 {"-I", "-foo"}, 203 {"-l", "@foo"}, 204 {"-l", "-foo"}, 205 {"-framework", "-Caffeine"}, 206 {"-framework", "@Home"}, 207 {"-Wl,-framework,-Caffeine"}, 208 {"-Wl,-framework", "-Wl,@Home"}, 209 {"-Wl,-framework", "@Home"}, 210 {"-Wl,-framework,Chocolate,@Home"}, 211 {"-x", "--c"}, 212 {"-x", "@obj"}, 213 {"-Wl,-rpath,@foo"}, 214 {"-Wl,-R,foo,bar"}, 215 {"-Wl,-R,@foo"}, 216 {"-Wl,--just-symbols,@foo"}, 217 {"../x.o"}, 218 } 219 220 func TestCheckLinkerFlags(t *testing.T) { 221 for _, f := range goodLinkerFlags { 222 if err := checkLinkerFlags("test", "test", f); err != nil { 223 t.Errorf("unexpected error for %q: %v", f, err) 224 } 225 } 226 for _, f := range badLinkerFlags { 227 if err := checkLinkerFlags("test", "test", f); err == nil { 228 t.Errorf("missing error for %q", f) 229 } 230 } 231 } 232 233 func TestCheckFlagAllowDisallow(t *testing.T) { 234 if err := checkCompilerFlags("TEST", "test", []string{"-disallow"}); err == nil { 235 t.Fatalf("missing error for -disallow") 236 } 237 os.Setenv("CGO_TEST_ALLOW", "-disallo") 238 if err := checkCompilerFlags("TEST", "test", []string{"-disallow"}); err == nil { 239 t.Fatalf("missing error for -disallow with CGO_TEST_ALLOW=-disallo") 240 } 241 os.Setenv("CGO_TEST_ALLOW", "-disallow") 242 if err := checkCompilerFlags("TEST", "test", []string{"-disallow"}); err != nil { 243 t.Fatalf("unexpected error for -disallow with CGO_TEST_ALLOW=-disallow: %v", err) 244 } 245 os.Unsetenv("CGO_TEST_ALLOW") 246 247 if err := checkCompilerFlags("TEST", "test", []string{"-Wall"}); err != nil { 248 t.Fatalf("unexpected error for -Wall: %v", err) 249 } 250 os.Setenv("CGO_TEST_DISALLOW", "-Wall") 251 if err := checkCompilerFlags("TEST", "test", []string{"-Wall"}); err == nil { 252 t.Fatalf("missing error for -Wall with CGO_TEST_DISALLOW=-Wall") 253 } 254 os.Setenv("CGO_TEST_ALLOW", "-Wall") // disallow wins 255 if err := checkCompilerFlags("TEST", "test", []string{"-Wall"}); err == nil { 256 t.Fatalf("missing error for -Wall with CGO_TEST_DISALLOW=-Wall and CGO_TEST_ALLOW=-Wall") 257 } 258 259 os.Setenv("CGO_TEST_ALLOW", "-fplugin.*") 260 os.Setenv("CGO_TEST_DISALLOW", "-fplugin=lint.so") 261 if err := checkCompilerFlags("TEST", "test", []string{"-fplugin=faster.so"}); err != nil { 262 t.Fatalf("unexpected error for -fplugin=faster.so: %v", err) 263 } 264 if err := checkCompilerFlags("TEST", "test", []string{"-fplugin=lint.so"}); err == nil { 265 t.Fatalf("missing error for -fplugin=lint.so: %v", err) 266 } 267 }