github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/src/go/internal/gcimporter/gcimporter_test.go (about) 1 // Copyright 2011 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 gcimporter 6 7 import ( 8 "bytes" 9 "fmt" 10 "internal/testenv" 11 "io/ioutil" 12 "os" 13 "os/exec" 14 "path/filepath" 15 "runtime" 16 "strings" 17 "testing" 18 "time" 19 20 "go/types" 21 ) 22 23 // skipSpecialPlatforms causes the test to be skipped for platforms where 24 // builders (build.golang.org) don't have access to compiled packages for 25 // import. 26 func skipSpecialPlatforms(t *testing.T) { 27 switch platform := runtime.GOOS + "-" + runtime.GOARCH; platform { 28 case "nacl-amd64p32", 29 "nacl-386", 30 "nacl-arm", 31 "darwin-arm", 32 "darwin-arm64": 33 t.Skipf("no compiled packages available for import on %s", platform) 34 } 35 } 36 37 func compile(t *testing.T, dirname, filename string) string { 38 cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", filename) 39 cmd.Dir = dirname 40 out, err := cmd.CombinedOutput() 41 if err != nil { 42 t.Logf("%s", out) 43 t.Fatalf("go tool compile %s failed: %s", filename, err) 44 } 45 // filename should end with ".go" 46 return filepath.Join(dirname, filename[:len(filename)-2]+"o") 47 } 48 49 func testPath(t *testing.T, path, srcDir string) *types.Package { 50 t0 := time.Now() 51 pkg, err := Import(make(map[string]*types.Package), path, srcDir, nil) 52 if err != nil { 53 t.Errorf("testPath(%s): %s", path, err) 54 return nil 55 } 56 t.Logf("testPath(%s): %v", path, time.Since(t0)) 57 return pkg 58 } 59 60 const maxTime = 30 * time.Second 61 62 func testDir(t *testing.T, dir string, endTime time.Time) (nimports int) { 63 dirname := filepath.Join(runtime.GOROOT(), "pkg", runtime.GOOS+"_"+runtime.GOARCH, dir) 64 list, err := ioutil.ReadDir(dirname) 65 if err != nil { 66 t.Fatalf("testDir(%s): %s", dirname, err) 67 } 68 for _, f := range list { 69 if time.Now().After(endTime) { 70 t.Log("testing time used up") 71 return 72 } 73 switch { 74 case !f.IsDir(): 75 // try extensions 76 for _, ext := range pkgExts { 77 if strings.HasSuffix(f.Name(), ext) { 78 name := f.Name()[0 : len(f.Name())-len(ext)] // remove extension 79 if testPath(t, filepath.Join(dir, name), dir) != nil { 80 nimports++ 81 } 82 } 83 } 84 case f.IsDir(): 85 nimports += testDir(t, filepath.Join(dir, f.Name()), endTime) 86 } 87 } 88 return 89 } 90 91 func TestImportTestdata(t *testing.T) { 92 // This package only handles gc export data. 93 if runtime.Compiler != "gc" { 94 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler) 95 } 96 97 if outFn := compile(t, "testdata", "exports.go"); outFn != "" { 98 defer os.Remove(outFn) 99 } 100 101 if pkg := testPath(t, "./testdata/exports", "."); pkg != nil { 102 // The package's Imports list must include all packages 103 // explicitly imported by exports.go, plus all packages 104 // referenced indirectly via exported objects in exports.go. 105 // With the textual export format, the list may also include 106 // additional packages that are not strictly required for 107 // import processing alone (they are exported to err "on 108 // the safe side"). 109 // TODO(gri) update the want list to be precise, now that 110 // the textual export data is gone. 111 got := fmt.Sprint(pkg.Imports()) 112 for _, want := range []string{"go/ast", "go/token"} { 113 if !strings.Contains(got, want) { 114 t.Errorf(`Package("exports").Imports() = %s, does not contain %s`, got, want) 115 } 116 } 117 } 118 } 119 120 func TestVersionHandling(t *testing.T) { 121 skipSpecialPlatforms(t) // we really only need to exclude nacl platforms, but this is fine 122 123 // This package only handles gc export data. 124 if runtime.Compiler != "gc" { 125 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler) 126 } 127 128 const dir = "./testdata/versions" 129 list, err := ioutil.ReadDir(dir) 130 if err != nil { 131 t.Fatal(err) 132 } 133 134 for _, f := range list { 135 name := f.Name() 136 if !strings.HasSuffix(name, ".a") { 137 continue // not a package file 138 } 139 if strings.Contains(name, "corrupted") { 140 continue // don't process a leftover corrupted file 141 } 142 pkgpath := "./" + name[:len(name)-2] 143 144 if testing.Verbose() { 145 t.Logf("importing %s", name) 146 } 147 148 // test that export data can be imported 149 _, err := Import(make(map[string]*types.Package), pkgpath, dir, nil) 150 if err != nil { 151 // ok to fail if it fails with a newer version error for select files 152 if strings.Contains(err.Error(), "newer version") { 153 switch name { 154 case "test_go1.11_999b.a", "test_go1.11_999i.a": 155 continue 156 } 157 // fall through 158 } 159 t.Errorf("import %q failed: %v", pkgpath, err) 160 continue 161 } 162 163 // create file with corrupted export data 164 // 1) read file 165 data, err := ioutil.ReadFile(filepath.Join(dir, name)) 166 if err != nil { 167 t.Fatal(err) 168 } 169 // 2) find export data 170 i := bytes.Index(data, []byte("\n$$B\n")) + 5 171 j := bytes.Index(data[i:], []byte("\n$$\n")) + i 172 if i < 0 || j < 0 || i > j { 173 t.Fatalf("export data section not found (i = %d, j = %d)", i, j) 174 } 175 // 3) corrupt the data (increment every 7th byte) 176 for k := j - 13; k >= i; k -= 7 { 177 data[k]++ 178 } 179 // 4) write the file 180 pkgpath += "_corrupted" 181 filename := filepath.Join(dir, pkgpath) + ".a" 182 ioutil.WriteFile(filename, data, 0666) 183 defer os.Remove(filename) 184 185 // test that importing the corrupted file results in an error 186 _, err = Import(make(map[string]*types.Package), pkgpath, dir, nil) 187 if err == nil { 188 t.Errorf("import corrupted %q succeeded", pkgpath) 189 } else if msg := err.Error(); !strings.Contains(msg, "version skew") { 190 t.Errorf("import %q error incorrect (%s)", pkgpath, msg) 191 } 192 } 193 } 194 195 func TestImportStdLib(t *testing.T) { 196 skipSpecialPlatforms(t) 197 198 // This package only handles gc export data. 199 if runtime.Compiler != "gc" { 200 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler) 201 } 202 203 dt := maxTime 204 if testing.Short() && testenv.Builder() == "" { 205 dt = 10 * time.Millisecond 206 } 207 nimports := testDir(t, "", time.Now().Add(dt)) // installed packages 208 t.Logf("tested %d imports", nimports) 209 } 210 211 var importedObjectTests = []struct { 212 name string 213 want string 214 }{ 215 // non-interfaces 216 {"crypto.Hash", "type Hash uint"}, 217 {"go/ast.ObjKind", "type ObjKind int"}, 218 {"go/types.Qualifier", "type Qualifier func(*Package) string"}, 219 {"go/types.Comparable", "func Comparable(T Type) bool"}, 220 {"math.Pi", "const Pi untyped float"}, 221 {"math.Sin", "func Sin(x float64) float64"}, 222 {"go/ast.NotNilFilter", "func NotNilFilter(_ string, v reflect.Value) bool"}, 223 {"go/internal/gcimporter.BImportData", "func BImportData(fset *go/token.FileSet, imports map[string]*go/types.Package, data []byte, path string) (_ int, pkg *go/types.Package, err error)"}, 224 225 // interfaces 226 {"context.Context", "type Context interface{Deadline() (deadline time.Time, ok bool); Done() <-chan struct{}; Err() error; Value(key interface{}) interface{}}"}, 227 {"crypto.Decrypter", "type Decrypter interface{Decrypt(rand io.Reader, msg []byte, opts DecrypterOpts) (plaintext []byte, err error); Public() PublicKey}"}, 228 {"encoding.BinaryMarshaler", "type BinaryMarshaler interface{MarshalBinary() (data []byte, err error)}"}, 229 {"io.Reader", "type Reader interface{Read(p []byte) (n int, err error)}"}, 230 {"io.ReadWriter", "type ReadWriter interface{Reader; Writer}"}, 231 {"go/ast.Node", "type Node interface{End() go/token.Pos; Pos() go/token.Pos}"}, 232 {"go/types.Type", "type Type interface{String() string; Underlying() Type}"}, 233 } 234 235 func TestImportedTypes(t *testing.T) { 236 skipSpecialPlatforms(t) 237 238 // This package only handles gc export data. 239 if runtime.Compiler != "gc" { 240 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler) 241 } 242 243 for _, test := range importedObjectTests { 244 s := strings.Split(test.name, ".") 245 if len(s) != 2 { 246 t.Fatal("inconsistent test data") 247 } 248 importPath := s[0] 249 objName := s[1] 250 251 pkg, err := Import(make(map[string]*types.Package), importPath, ".", nil) 252 if err != nil { 253 t.Error(err) 254 continue 255 } 256 257 obj := pkg.Scope().Lookup(objName) 258 if obj == nil { 259 t.Errorf("%s: object not found", test.name) 260 continue 261 } 262 263 got := types.ObjectString(obj, types.RelativeTo(pkg)) 264 if got != test.want { 265 t.Errorf("%s: got %q; want %q", test.name, got, test.want) 266 } 267 268 if named, _ := obj.Type().(*types.Named); named != nil { 269 verifyInterfaceMethodRecvs(t, named, 0) 270 } 271 } 272 } 273 274 // verifyInterfaceMethodRecvs verifies that method receiver types 275 // are named if the methods belong to a named interface type. 276 func verifyInterfaceMethodRecvs(t *testing.T, named *types.Named, level int) { 277 // avoid endless recursion in case of an embedding bug that lead to a cycle 278 if level > 10 { 279 t.Errorf("%s: embeds itself", named) 280 return 281 } 282 283 iface, _ := named.Underlying().(*types.Interface) 284 if iface == nil { 285 return // not an interface 286 } 287 288 // check explicitly declared methods 289 for i := 0; i < iface.NumExplicitMethods(); i++ { 290 m := iface.ExplicitMethod(i) 291 recv := m.Type().(*types.Signature).Recv() 292 if recv == nil { 293 t.Errorf("%s: missing receiver type", m) 294 continue 295 } 296 if recv.Type() != named { 297 t.Errorf("%s: got recv type %s; want %s", m, recv.Type(), named) 298 } 299 } 300 301 // check embedded interfaces (if they are named, too) 302 for i := 0; i < iface.NumEmbeddeds(); i++ { 303 // embedding of interfaces cannot have cycles; recursion will terminate 304 if etype, _ := iface.EmbeddedType(i).(*types.Named); etype != nil { 305 verifyInterfaceMethodRecvs(t, etype, level+1) 306 } 307 } 308 } 309 310 func TestIssue5815(t *testing.T) { 311 skipSpecialPlatforms(t) 312 313 // This package only handles gc export data. 314 if runtime.Compiler != "gc" { 315 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler) 316 } 317 318 pkg := importPkg(t, "strings") 319 320 scope := pkg.Scope() 321 for _, name := range scope.Names() { 322 obj := scope.Lookup(name) 323 if obj.Pkg() == nil { 324 t.Errorf("no pkg for %s", obj) 325 } 326 if tname, _ := obj.(*types.TypeName); tname != nil { 327 named := tname.Type().(*types.Named) 328 for i := 0; i < named.NumMethods(); i++ { 329 m := named.Method(i) 330 if m.Pkg() == nil { 331 t.Errorf("no pkg for %s", m) 332 } 333 } 334 } 335 } 336 } 337 338 // Smoke test to ensure that imported methods get the correct package. 339 func TestCorrectMethodPackage(t *testing.T) { 340 skipSpecialPlatforms(t) 341 342 // This package only handles gc export data. 343 if runtime.Compiler != "gc" { 344 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler) 345 } 346 347 imports := make(map[string]*types.Package) 348 _, err := Import(imports, "net/http", ".", nil) 349 if err != nil { 350 t.Fatal(err) 351 } 352 353 mutex := imports["sync"].Scope().Lookup("Mutex").(*types.TypeName).Type() 354 mset := types.NewMethodSet(types.NewPointer(mutex)) // methods of *sync.Mutex 355 sel := mset.Lookup(nil, "Lock") 356 lock := sel.Obj().(*types.Func) 357 if got, want := lock.Pkg().Path(), "sync"; got != want { 358 t.Errorf("got package path %q; want %q", got, want) 359 } 360 } 361 362 func TestIssue13566(t *testing.T) { 363 skipSpecialPlatforms(t) 364 365 // This package only handles gc export data. 366 if runtime.Compiler != "gc" { 367 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler) 368 } 369 370 // On windows, we have to set the -D option for the compiler to avoid having a drive 371 // letter and an illegal ':' in the import path - just skip it (see also issue #3483). 372 if runtime.GOOS == "windows" { 373 t.Skip("avoid dealing with relative paths/drive letters on windows") 374 } 375 376 if f := compile(t, "testdata", "a.go"); f != "" { 377 defer os.Remove(f) 378 } 379 if f := compile(t, "testdata", "b.go"); f != "" { 380 defer os.Remove(f) 381 } 382 383 // import must succeed (test for issue at hand) 384 pkg := importPkg(t, "./testdata/b") 385 386 // make sure all indirectly imported packages have names 387 for _, imp := range pkg.Imports() { 388 if imp.Name() == "" { 389 t.Errorf("no name for %s package", imp.Path()) 390 } 391 } 392 } 393 394 func TestIssue13898(t *testing.T) { 395 skipSpecialPlatforms(t) 396 397 // This package only handles gc export data. 398 if runtime.Compiler != "gc" { 399 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler) 400 } 401 402 // import go/internal/gcimporter which imports go/types partially 403 imports := make(map[string]*types.Package) 404 _, err := Import(imports, "go/internal/gcimporter", ".", nil) 405 if err != nil { 406 t.Fatal(err) 407 } 408 409 // look for go/types package 410 var goTypesPkg *types.Package 411 for path, pkg := range imports { 412 if path == "go/types" { 413 goTypesPkg = pkg 414 break 415 } 416 } 417 if goTypesPkg == nil { 418 t.Fatal("go/types not found") 419 } 420 421 // look for go/types.Object type 422 obj := lookupObj(t, goTypesPkg.Scope(), "Object") 423 typ, ok := obj.Type().(*types.Named) 424 if !ok { 425 t.Fatalf("go/types.Object type is %v; wanted named type", typ) 426 } 427 428 // lookup go/types.Object.Pkg method 429 m, index, indirect := types.LookupFieldOrMethod(typ, false, nil, "Pkg") 430 if m == nil { 431 t.Fatalf("go/types.Object.Pkg not found (index = %v, indirect = %v)", index, indirect) 432 } 433 434 // the method must belong to go/types 435 if m.Pkg().Path() != "go/types" { 436 t.Fatalf("found %v; want go/types", m.Pkg()) 437 } 438 } 439 440 func TestIssue15517(t *testing.T) { 441 skipSpecialPlatforms(t) 442 443 // This package only handles gc export data. 444 if runtime.Compiler != "gc" { 445 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler) 446 } 447 448 // On windows, we have to set the -D option for the compiler to avoid having a drive 449 // letter and an illegal ':' in the import path - just skip it (see also issue #3483). 450 if runtime.GOOS == "windows" { 451 t.Skip("avoid dealing with relative paths/drive letters on windows") 452 } 453 454 if f := compile(t, "testdata", "p.go"); f != "" { 455 defer os.Remove(f) 456 } 457 458 // Multiple imports of p must succeed without redeclaration errors. 459 // We use an import path that's not cleaned up so that the eventual 460 // file path for the package is different from the package path; this 461 // will expose the error if it is present. 462 // 463 // (Issue: Both the textual and the binary importer used the file path 464 // of the package to be imported as key into the shared packages map. 465 // However, the binary importer then used the package path to identify 466 // the imported package to mark it as complete; effectively marking the 467 // wrong package as complete. By using an "unclean" package path, the 468 // file and package path are different, exposing the problem if present. 469 // The same issue occurs with vendoring.) 470 imports := make(map[string]*types.Package) 471 for i := 0; i < 3; i++ { 472 if _, err := Import(imports, "./././testdata/p", ".", nil); err != nil { 473 t.Fatal(err) 474 } 475 } 476 } 477 478 func TestIssue15920(t *testing.T) { 479 skipSpecialPlatforms(t) 480 481 // This package only handles gc export data. 482 if runtime.Compiler != "gc" { 483 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler) 484 } 485 486 // On windows, we have to set the -D option for the compiler to avoid having a drive 487 // letter and an illegal ':' in the import path - just skip it (see also issue #3483). 488 if runtime.GOOS == "windows" { 489 t.Skip("avoid dealing with relative paths/drive letters on windows") 490 } 491 492 if f := compile(t, "testdata", "issue15920.go"); f != "" { 493 defer os.Remove(f) 494 } 495 496 importPkg(t, "./testdata/issue15920") 497 } 498 499 func TestIssue20046(t *testing.T) { 500 skipSpecialPlatforms(t) 501 502 // This package only handles gc export data. 503 if runtime.Compiler != "gc" { 504 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler) 505 } 506 507 // On windows, we have to set the -D option for the compiler to avoid having a drive 508 // letter and an illegal ':' in the import path - just skip it (see also issue #3483). 509 if runtime.GOOS == "windows" { 510 t.Skip("avoid dealing with relative paths/drive letters on windows") 511 } 512 513 if f := compile(t, "testdata", "issue20046.go"); f != "" { 514 defer os.Remove(f) 515 } 516 517 // "./issue20046".V.M must exist 518 pkg := importPkg(t, "./testdata/issue20046") 519 obj := lookupObj(t, pkg.Scope(), "V") 520 if m, index, indirect := types.LookupFieldOrMethod(obj.Type(), false, nil, "M"); m == nil { 521 t.Fatalf("V.M not found (index = %v, indirect = %v)", index, indirect) 522 } 523 } 524 func TestIssue25301(t *testing.T) { 525 skipSpecialPlatforms(t) 526 527 // This package only handles gc export data. 528 if runtime.Compiler != "gc" { 529 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler) 530 } 531 532 // On windows, we have to set the -D option for the compiler to avoid having a drive 533 // letter and an illegal ':' in the import path - just skip it (see also issue #3483). 534 if runtime.GOOS == "windows" { 535 t.Skip("avoid dealing with relative paths/drive letters on windows") 536 } 537 538 if f := compile(t, "testdata", "issue25301.go"); f != "" { 539 defer os.Remove(f) 540 } 541 542 importPkg(t, "./testdata/issue25301") 543 } 544 545 func TestIssue25596(t *testing.T) { 546 skipSpecialPlatforms(t) 547 548 // This package only handles gc export data. 549 if runtime.Compiler != "gc" { 550 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler) 551 } 552 553 // On windows, we have to set the -D option for the compiler to avoid having a drive 554 // letter and an illegal ':' in the import path - just skip it (see also issue #3483). 555 if runtime.GOOS == "windows" { 556 t.Skip("avoid dealing with relative paths/drive letters on windows") 557 } 558 559 if f := compile(t, "testdata", "issue25596.go"); f != "" { 560 defer os.Remove(f) 561 } 562 563 importPkg(t, "./testdata/issue25596") 564 } 565 566 func importPkg(t *testing.T, path string) *types.Package { 567 pkg, err := Import(make(map[string]*types.Package), path, ".", nil) 568 if err != nil { 569 t.Fatal(err) 570 } 571 return pkg 572 } 573 574 func lookupObj(t *testing.T, scope *types.Scope, name string) types.Object { 575 if obj := scope.Lookup(name); obj != nil { 576 return obj 577 } 578 t.Fatalf("%s not found", name) 579 return nil 580 }