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