1// Copyright 2019 The Bazel Authors. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package custom_test 16 17import ( 18 "bytes" 19 "fmt" 20 "io/ioutil" 21 "regexp" 22 "testing" 23 24 "github.com/bazelbuild/rules_go/go/tools/bazel_testing" 25) 26 27const origConfig = `# config = "",` 28 29func TestMain(m *testing.M) { 30 bazel_testing.TestMain(m, bazel_testing.Args{ 31 Nogo: "@//:nogo", 32 Main: ` 33-- BUILD.bazel -- 34load("@io_bazel_rules_go//go:def.bzl", "go_library", "nogo") 35 36nogo( 37 name = "nogo", 38 deps = [ 39 ":foofuncname", 40 ":importfmt", 41 ":visibility", 42 ], 43 # config = "", 44 visibility = ["//visibility:public"], 45) 46 47go_library( 48 name = "importfmt", 49 srcs = ["importfmt.go"], 50 importpath = "importfmtanalyzer", 51 deps = ["@org_golang_x_tools//go/analysis"], 52 visibility = ["//visibility:public"], 53) 54 55go_library( 56 name = "foofuncname", 57 srcs = ["foofuncname.go"], 58 importpath = "foofuncanalyzer", 59 deps = ["@org_golang_x_tools//go/analysis"], 60 visibility = ["//visibility:public"], 61) 62 63go_library( 64 name = "visibility", 65 srcs = ["visibility.go"], 66 importpath = "visibilityanalyzer", 67 deps = [ 68 "@org_golang_x_tools//go/analysis", 69 "@org_golang_x_tools//go/ast/inspector", 70 ], 71 visibility = ["//visibility:public"], 72) 73 74go_library( 75 name = "has_errors", 76 srcs = ["has_errors.go"], 77 importpath = "haserrors", 78 deps = [":dep"], 79) 80 81go_library( 82 name = "has_errors_linedirective", 83 srcs = ["has_errors_linedirective.go"], 84 importpath = "haserrors_linedirective", 85 deps = [":dep"], 86) 87 88go_library( 89 name = "uses_cgo_with_errors", 90 srcs = [ 91 "examplepkg/uses_cgo_clean.go", 92 "examplepkg/pure_src_with_err_calling_native.go", 93 ], 94 importpath = "examplepkg", 95 cgo = True, 96) 97 98go_library( 99 name = "no_errors", 100 srcs = ["no_errors.go"], 101 importpath = "noerrors", 102 deps = [":dep"], 103) 104 105go_library( 106 name = "dep", 107 srcs = ["dep.go"], 108 importpath = "dep", 109) 110 111-- foofuncname.go -- 112// importfmt checks for functions named "Foo". 113// It has the same package name as another check to test the checks with 114// the same package name do not conflict. 115package importfmt 116 117import ( 118 "go/ast" 119 120 "golang.org/x/tools/go/analysis" 121) 122 123const doc = "report calls of functions named \"Foo\"\n\nThe foofuncname analyzer reports calls to functions that are\nnamed \"Foo\"." 124 125var Analyzer = &analysis.Analyzer{ 126 Name: "foofuncname", 127 Run: run, 128 Doc: doc, 129} 130 131func run(pass *analysis.Pass) (interface{}, error) { 132 for _, f := range pass.Files { 133 // TODO(samueltan): use package inspector once the latest golang.org/x/tools 134 // changes are pulled into this branch (see #1755). 135 ast.Inspect(f, func(n ast.Node) bool { 136 switch n := n.(type) { 137 case *ast.FuncDecl: 138 if n.Name.Name == "Foo" { 139 pass.Reportf(n.Pos(), "function must not be named Foo") 140 } 141 return true 142 } 143 return true 144 }) 145 } 146 return nil, nil 147} 148 149-- importfmt.go -- 150// importfmt checks for the import of package fmt. 151package importfmt 152 153import ( 154 "go/ast" 155 "strconv" 156 157 "golang.org/x/tools/go/analysis" 158) 159 160const doc = "report imports of package fmt\n\nThe importfmt analyzer reports imports of package fmt." 161 162var Analyzer = &analysis.Analyzer{ 163 Name: "importfmt", 164 Run: run, 165 Doc: doc, 166} 167 168func run(pass *analysis.Pass) (interface{}, error) { 169 for _, f := range pass.Files { 170 // TODO(samueltan): use package inspector once the latest golang.org/x/tools 171 // changes are pulled into this branch (see #1755). 172 ast.Inspect(f, func(n ast.Node) bool { 173 switch n := n.(type) { 174 case *ast.ImportSpec: 175 if path, _ := strconv.Unquote(n.Path.Value); path == "fmt" { 176 pass.Reportf(n.Pos(), "package fmt must not be imported") 177 } 178 return true 179 } 180 return true 181 }) 182 } 183 return nil, nil 184} 185 186-- visibility.go -- 187// visibility looks for visibility annotations on functions and 188// checks they are only called from packages allowed to call them. 189package visibility 190 191import ( 192 "encoding/gob" 193 "go/ast" 194 "regexp" 195 196 "golang.org/x/tools/go/analysis" 197 "golang.org/x/tools/go/ast/inspector" 198) 199 200var Analyzer = &analysis.Analyzer{ 201 Name: "visibility", 202 Run: run, 203 Doc: "enforce visibility requirements for functions\n\nThe visibility analyzer reads visibility annotations on functions and\nchecks that packages that call those functions are allowed to do so.", 204 FactTypes: []analysis.Fact{(*VisibilityFact)(nil)}, 205} 206 207type VisibilityFact struct { 208 Paths []string 209} 210 211func (_ *VisibilityFact) AFact() {} // dummy method to satisfy interface 212 213func init() { gob.Register((*VisibilityFact)(nil)) } 214 215var visibilityRegexp = regexp.MustCompile("visibility:([^\\s]+)") 216 217func run(pass *analysis.Pass) (interface{}, error) { 218 in := inspector.New(pass.Files) 219 220 // Find visibility annotations on function declarations. 221 in.Nodes([]ast.Node{(*ast.FuncDecl)(nil)}, func(n ast.Node, push bool) (prune bool) { 222 if !push { 223 return false 224 } 225 226 fn := n.(*ast.FuncDecl) 227 228 if fn.Doc == nil { 229 return true 230 } 231 obj := pass.TypesInfo.ObjectOf(fn.Name) 232 if obj == nil { 233 return true 234 } 235 doc := fn.Doc.Text() 236 237 if matches := visibilityRegexp.FindAllStringSubmatch(doc, -1); matches != nil { 238 fact := &VisibilityFact{Paths: make([]string, len(matches))} 239 for i, m := range matches { 240 fact.Paths[i] = m[1] 241 } 242 pass.ExportObjectFact(obj, fact) 243 } 244 245 return true 246 }) 247 248 // Find calls that may be affected by visibility declarations. 249 in.Nodes([]ast.Node{(*ast.CallExpr)(nil)}, func(n ast.Node, push bool) (prune bool) { 250 if !push { 251 return false 252 } 253 254 callee, ok := n.(*ast.CallExpr).Fun.(*ast.SelectorExpr) 255 if !ok { 256 return false 257 } 258 obj := pass.TypesInfo.ObjectOf(callee.Sel) 259 if obj == nil { 260 return false 261 } 262 var fact VisibilityFact 263 if ok := pass.ImportObjectFact(obj, &fact); !ok { 264 return false 265 } 266 visible := false 267 for _, path := range fact.Paths { 268 if path == pass.Pkg.Path() { 269 visible = true 270 break 271 } 272 } 273 if !visible { 274 pass.Reportf(callee.Pos(), "function %s is not visible in this package", callee.Sel.Name) 275 } 276 277 return false 278 }) 279 280 return nil, nil 281} 282 283-- config.json -- 284{ 285 "importfmt": { 286 "only_files": { 287 "has_errors\\.go": "" 288 } 289 }, 290 "foofuncname": { 291 "description": "no exemptions since we know this check is 100% accurate" 292 }, 293 "visibility": { 294 "exclude_files": { 295 "has_.*\\.go": "special exception to visibility rules" 296 } 297 } 298} 299 300-- baseconfig.json -- 301{ 302 "_base": { 303 "exclude_files": { 304 "has_.*\\.go": "Visibility analyzer not specified. Still inherits this special exception." 305 } 306 }, 307 "importfmt": { 308 "only_files": { 309 "has_errors\\.go": "" 310 } 311 }, 312 "foofuncname": { 313 "description": "no exemptions since we know this check is 100% accurate, so override base config", 314 "exclude_files": {} 315 } 316} 317 318-- has_errors.go -- 319package haserrors 320 321import ( 322 _ "fmt" // This should fail importfmt 323 324 "dep" 325) 326 327func Foo() bool { // This should fail foofuncname 328 dep.D() // This should fail visibility 329 return true 330} 331 332-- has_errors_linedirective.go -- 333//line linedirective.go:1 334package haserrors_linedirective 335 336import ( 337 /*line linedirective_importfmt.go:4*/ _ "fmt" // This should fail importfmt 338 339 "dep" 340) 341 342//line linedirective_foofuncname.go:9 343func Foo() bool { // This should fail foofuncname 344//line linedirective_visibility.go:10 345 dep.D() // This should fail visibility 346 return true 347} 348 349-- no_errors.go -- 350// package noerrors contains no analyzer errors. 351package noerrors 352 353import "dep" 354 355func Baz() int { 356 dep.D() 357 return 1 358} 359 360-- dep.go -- 361package dep 362 363// visibility:noerrors 364func D() { 365} 366 367-- examplepkg/uses_cgo_clean.go -- 368package examplepkg 369 370// #include <stdlib.h> 371import "C" 372 373func Bar() bool { 374 if C.rand() > 10 { 375 return true 376 } 377 return false 378} 379 380-- examplepkg/pure_src_with_err_calling_native.go -- 381package examplepkg 382 383func Foo() bool { // This should fail foofuncname 384 return Bar() 385} 386 387`, 388 }) 389} 390 391func Test(t *testing.T) { 392 for _, test := range []struct { 393 desc, config, target string 394 wantSuccess bool 395 includes, excludes []string 396 }{ 397 { 398 desc: "default_config", 399 target: "//:has_errors", 400 wantSuccess: false, 401 includes: []string{ 402 `has_errors.go:.*package fmt must not be imported \(importfmt\)`, 403 `has_errors.go:.*function must not be named Foo \(foofuncname\)`, 404 `has_errors.go:.*function D is not visible in this package \(visibility\)`, 405 }, 406 }, { 407 desc: "default_config_linedirective", 408 target: "//:has_errors_linedirective", 409 wantSuccess: false, 410 includes: []string{ 411 `linedirective_importfmt.go:.*package fmt must not be imported \(importfmt\)`, 412 `linedirective_foofuncname.go:.*function must not be named Foo \(foofuncname\)`, 413 `linedirective_visibility.go:.*function D is not visible in this package \(visibility\)`, 414 }, 415 }, { 416 desc: "custom_config", 417 config: "config.json", 418 target: "//:has_errors", 419 wantSuccess: false, 420 includes: []string{ 421 `has_errors.go:.*package fmt must not be imported \(importfmt\)`, 422 `has_errors.go:.*function must not be named Foo \(foofuncname\)`, 423 }, 424 excludes: []string{ 425 `visib`, 426 }, 427 }, { 428 desc: "custom_config_linedirective", 429 config: "config.json", 430 target: "//:has_errors_linedirective", 431 wantSuccess: false, 432 includes: []string{ 433 `linedirective_foofuncname.go:.*function must not be named Foo \(foofuncname\)`, 434 `linedirective_visibility.go:.*function D is not visible in this package \(visibility\)`, 435 }, 436 excludes: []string{ 437 `importfmt`, 438 }, 439 }, { 440 desc: "custom_config_with_base_linedirective", 441 config: "baseconfig.json", 442 target: "//:has_errors_linedirective", 443 wantSuccess: false, 444 includes: []string{ 445 `linedirective_foofuncname.go:.*function must not be named Foo \(foofuncname\)`, 446 `linedirective_visibility.go:.*function D is not visible in this package \(visibility\)`, 447 }, 448 excludes: []string{ 449 `importfmt`, 450 }, 451 }, { 452 desc: "uses_cgo_with_errors", 453 config: "config.json", 454 target: "//:uses_cgo_with_errors", 455 wantSuccess: false, 456 includes: []string{ 457 // note the cross platform regex :) 458 `.*[\\/]cgo[\\/]examplepkg[\\/]pure_src_with_err_calling_native.go:.*function must not be named Foo \(foofuncname\)`, 459 }, 460 }, { 461 desc: "no_errors", 462 target: "//:no_errors", 463 wantSuccess: true, 464 excludes: []string{"no_errors.go"}, 465 }, 466 } { 467 t.Run(test.desc, func(t *testing.T) { 468 if test.config != "" { 469 customConfig := fmt.Sprintf("config = %q,", test.config) 470 if err := replaceInFile("BUILD.bazel", origConfig, customConfig); err != nil { 471 t.Fatal(err) 472 } 473 defer replaceInFile("BUILD.bazel", customConfig, origConfig) 474 } 475 476 cmd := bazel_testing.BazelCmd("build", test.target) 477 stderr := &bytes.Buffer{} 478 cmd.Stderr = stderr 479 if err := cmd.Run(); err == nil && !test.wantSuccess { 480 t.Fatal("unexpected success") 481 } else if err != nil && test.wantSuccess { 482 t.Fatalf("unexpected error: %v", err) 483 } 484 485 for _, pattern := range test.includes { 486 if matched, err := regexp.Match(pattern, stderr.Bytes()); err != nil { 487 t.Fatal(err) 488 } else if !matched { 489 t.Errorf("got output:\n %s\n which does not contain pattern: %s", string(stderr.Bytes()), pattern) 490 } 491 } 492 for _, pattern := range test.excludes { 493 if matched, err := regexp.Match(pattern, stderr.Bytes()); err != nil { 494 t.Fatal(err) 495 } else if matched { 496 t.Errorf("output contained pattern: %s", pattern) 497 } 498 } 499 }) 500 } 501} 502 503func replaceInFile(path, old, new string) error { 504 data, err := ioutil.ReadFile(path) 505 if err != nil { 506 return err 507 } 508 data = bytes.ReplaceAll(data, []byte(old), []byte(new)) 509 return ioutil.WriteFile(path, data, 0666) 510} 511