github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/go/build/deps_test.go (about)

     1  // Copyright 2012 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  // This file exercises the import parser but also checks that
     6  // some low-level packages do not have new dependencies added.
     7  
     8  package build
     9  
    10  import (
    11  	"bytes"
    12  	"fmt"
    13  	"io/ioutil"
    14  	"os"
    15  	"path/filepath"
    16  	"runtime"
    17  	"sort"
    18  	"strconv"
    19  	"strings"
    20  	"testing"
    21  )
    22  
    23  // pkgDeps defines the expected dependencies between packages in
    24  // the Go source tree. It is a statement of policy.
    25  // Changes should not be made to this map without prior discussion.
    26  //
    27  // The map contains two kinds of entries:
    28  // 1) Lower-case keys are standard import paths and list the
    29  // allowed imports in that package.
    30  // 2) Upper-case keys define aliases for package sets, which can then
    31  // be used as dependencies by other rules.
    32  //
    33  // DO NOT CHANGE THIS DATA TO FIX BUILDS.
    34  //
    35  var pkgDeps = map[string][]string{
    36  	// L0 is the lowest level, core, nearly unavoidable packages.
    37  	"errors":                  {},
    38  	"io":                      {"errors", "sync"},
    39  	"runtime":                 {"unsafe", "runtime/internal/atomic", "runtime/internal/sys"},
    40  	"runtime/internal/sys":    {},
    41  	"runtime/internal/atomic": {"unsafe", "runtime/internal/sys"},
    42  	"internal/race":           {"runtime", "unsafe"},
    43  	"sync":                    {"internal/race", "runtime", "sync/atomic", "unsafe"},
    44  	"sync/atomic":             {"unsafe"},
    45  	"unsafe":                  {},
    46  
    47  	"L0": {
    48  		"errors",
    49  		"io",
    50  		"runtime",
    51  		"runtime/internal/atomic",
    52  		"sync",
    53  		"sync/atomic",
    54  		"unsafe",
    55  	},
    56  
    57  	// L1 adds simple functions and strings processing,
    58  	// but not Unicode tables.
    59  	"math":          {"unsafe"},
    60  	"math/cmplx":    {"math"},
    61  	"math/rand":     {"L0", "math"},
    62  	"sort":          {},
    63  	"strconv":       {"L0", "unicode/utf8", "math"},
    64  	"unicode/utf16": {},
    65  	"unicode/utf8":  {},
    66  
    67  	"L1": {
    68  		"L0",
    69  		"math",
    70  		"math/cmplx",
    71  		"math/rand",
    72  		"sort",
    73  		"strconv",
    74  		"unicode/utf16",
    75  		"unicode/utf8",
    76  	},
    77  
    78  	// L2 adds Unicode and strings processing.
    79  	"bufio":   {"L0", "unicode/utf8", "bytes"},
    80  	"bytes":   {"L0", "unicode", "unicode/utf8"},
    81  	"path":    {"L0", "unicode/utf8", "strings"},
    82  	"strings": {"L0", "unicode", "unicode/utf8"},
    83  	"unicode": {},
    84  
    85  	"L2": {
    86  		"L1",
    87  		"bufio",
    88  		"bytes",
    89  		"path",
    90  		"strings",
    91  		"unicode",
    92  	},
    93  
    94  	// L3 adds reflection and some basic utility packages
    95  	// and interface definitions, but nothing that makes
    96  	// system calls.
    97  	"crypto":              {"L2", "hash"},          // interfaces
    98  	"crypto/cipher":       {"L2", "crypto/subtle"}, // interfaces
    99  	"crypto/subtle":       {},
   100  	"encoding/base32":     {"L2"},
   101  	"encoding/base64":     {"L2"},
   102  	"encoding/binary":     {"L2", "reflect"},
   103  	"hash":                {"L2"}, // interfaces
   104  	"hash/adler32":        {"L2", "hash"},
   105  	"hash/crc32":          {"L2", "hash"},
   106  	"hash/crc64":          {"L2", "hash"},
   107  	"hash/fnv":            {"L2", "hash"},
   108  	"image":               {"L2", "image/color"}, // interfaces
   109  	"image/color":         {"L2"},                // interfaces
   110  	"image/color/palette": {"L2", "image/color"},
   111  	"reflect":             {"L2"},
   112  
   113  	"L3": {
   114  		"L2",
   115  		"crypto",
   116  		"crypto/cipher",
   117  		"crypto/subtle",
   118  		"encoding/base32",
   119  		"encoding/base64",
   120  		"encoding/binary",
   121  		"hash",
   122  		"hash/adler32",
   123  		"hash/crc32",
   124  		"hash/crc64",
   125  		"hash/fnv",
   126  		"image",
   127  		"image/color",
   128  		"image/color/palette",
   129  		"reflect",
   130  	},
   131  
   132  	// End of linear dependency definitions.
   133  
   134  	// Operating system access.
   135  	"syscall":                           {"L0", "internal/race", "internal/syscall/windows/sysdll", "unicode/utf16"},
   136  	"internal/syscall/unix":             {"L0", "syscall"},
   137  	"internal/syscall/windows":          {"L0", "syscall", "internal/syscall/windows/sysdll"},
   138  	"internal/syscall/windows/registry": {"L0", "syscall", "internal/syscall/windows/sysdll", "unicode/utf16"},
   139  	"time":          {"L0", "syscall", "internal/syscall/windows/registry"},
   140  	"os":            {"L1", "os", "syscall", "time", "internal/syscall/windows"},
   141  	"path/filepath": {"L2", "os", "syscall"},
   142  	"io/ioutil":     {"L2", "os", "path/filepath", "time"},
   143  	"os/exec":       {"L2", "os", "path/filepath", "syscall"},
   144  	"os/signal":     {"L2", "os", "syscall"},
   145  
   146  	// OS enables basic operating system functionality,
   147  	// but not direct use of package syscall, nor os/signal.
   148  	"OS": {
   149  		"io/ioutil",
   150  		"os",
   151  		"os/exec",
   152  		"path/filepath",
   153  		"time",
   154  	},
   155  
   156  	// Formatted I/O: few dependencies (L1) but we must add reflect.
   157  	"fmt": {"L1", "os", "reflect"},
   158  	"log": {"L1", "os", "fmt", "time"},
   159  
   160  	// Packages used by testing must be low-level (L2+fmt).
   161  	"regexp":         {"L2", "regexp/syntax"},
   162  	"regexp/syntax":  {"L2"},
   163  	"runtime/debug":  {"L2", "fmt", "io/ioutil", "os", "time"},
   164  	"runtime/pprof":  {"L2", "fmt", "text/tabwriter"},
   165  	"runtime/trace":  {"L0"},
   166  	"text/tabwriter": {"L2"},
   167  
   168  	"testing":          {"L2", "flag", "fmt", "os", "runtime/debug", "runtime/pprof", "runtime/trace", "time"},
   169  	"testing/iotest":   {"L2", "log"},
   170  	"testing/quick":    {"L2", "flag", "fmt", "reflect"},
   171  	"internal/testenv": {"L2", "OS", "testing"},
   172  
   173  	// L4 is defined as L3+fmt+log+time, because in general once
   174  	// you're using L3 packages, use of fmt, log, or time is not a big deal.
   175  	"L4": {
   176  		"L3",
   177  		"fmt",
   178  		"log",
   179  		"time",
   180  	},
   181  
   182  	// Go parser.
   183  	"go/ast":     {"L4", "OS", "go/scanner", "go/token"},
   184  	"go/doc":     {"L4", "go/ast", "go/token", "regexp", "text/template"},
   185  	"go/parser":  {"L4", "OS", "go/ast", "go/scanner", "go/token"},
   186  	"go/printer": {"L4", "OS", "go/ast", "go/scanner", "go/token", "text/tabwriter"},
   187  	"go/scanner": {"L4", "OS", "go/token"},
   188  	"go/token":   {"L4"},
   189  
   190  	"GOPARSER": {
   191  		"go/ast",
   192  		"go/doc",
   193  		"go/parser",
   194  		"go/printer",
   195  		"go/scanner",
   196  		"go/token",
   197  	},
   198  
   199  	"go/format":       {"L4", "GOPARSER", "internal/format"},
   200  	"internal/format": {"L4", "GOPARSER"},
   201  
   202  	// Go type checking.
   203  	"go/constant":               {"L4", "go/token", "math/big"},
   204  	"go/importer":               {"L4", "go/internal/gcimporter", "go/internal/gccgoimporter", "go/types"},
   205  	"go/internal/gcimporter":    {"L4", "OS", "go/build", "go/constant", "go/token", "go/types", "text/scanner"},
   206  	"go/internal/gccgoimporter": {"L4", "OS", "debug/elf", "go/constant", "go/token", "go/types", "text/scanner"},
   207  	"go/types":                  {"L4", "GOPARSER", "container/heap", "go/constant"},
   208  
   209  	// One of a kind.
   210  	"archive/tar":              {"L4", "OS", "syscall"},
   211  	"archive/zip":              {"L4", "OS", "compress/flate"},
   212  	"container/heap":           {"sort"},
   213  	"compress/bzip2":           {"L4"},
   214  	"compress/flate":           {"L4"},
   215  	"compress/gzip":            {"L4", "compress/flate"},
   216  	"compress/lzw":             {"L4"},
   217  	"compress/zlib":            {"L4", "compress/flate"},
   218  	"context":                  {"errors", "fmt", "sync", "time"},
   219  	"database/sql":             {"L4", "container/list", "database/sql/driver"},
   220  	"database/sql/driver":      {"L4", "time"},
   221  	"debug/dwarf":              {"L4"},
   222  	"debug/elf":                {"L4", "OS", "debug/dwarf", "compress/zlib"},
   223  	"debug/gosym":              {"L4"},
   224  	"debug/macho":              {"L4", "OS", "debug/dwarf"},
   225  	"debug/pe":                 {"L4", "OS", "debug/dwarf"},
   226  	"debug/plan9obj":           {"L4", "OS"},
   227  	"encoding":                 {"L4"},
   228  	"encoding/ascii85":         {"L4"},
   229  	"encoding/asn1":            {"L4", "math/big"},
   230  	"encoding/csv":             {"L4"},
   231  	"encoding/gob":             {"L4", "OS", "encoding"},
   232  	"encoding/hex":             {"L4"},
   233  	"encoding/json":            {"L4", "encoding"},
   234  	"encoding/pem":             {"L4"},
   235  	"encoding/xml":             {"L4", "encoding"},
   236  	"flag":                     {"L4", "OS"},
   237  	"go/build":                 {"L4", "OS", "GOPARSER"},
   238  	"html":                     {"L4"},
   239  	"image/draw":               {"L4", "image/internal/imageutil"},
   240  	"image/gif":                {"L4", "compress/lzw", "image/color/palette", "image/draw"},
   241  	"image/internal/imageutil": {"L4"},
   242  	"image/jpeg":               {"L4", "image/internal/imageutil"},
   243  	"image/png":                {"L4", "compress/zlib"},
   244  	"index/suffixarray":        {"L4", "regexp"},
   245  	"internal/singleflight":    {"sync"},
   246  	"internal/trace":           {"L4", "OS"},
   247  	"math/big":                 {"L4"},
   248  	"mime":                     {"L4", "OS", "syscall", "internal/syscall/windows/registry"},
   249  	"mime/quotedprintable":     {"L4"},
   250  	"net/internal/socktest":    {"L4", "OS", "syscall"},
   251  	"net/url":                  {"L4"},
   252  	"text/scanner":             {"L4", "OS"},
   253  	"text/template/parse":      {"L4"},
   254  
   255  	"html/template": {
   256  		"L4", "OS", "encoding/json", "html", "text/template",
   257  		"text/template/parse",
   258  	},
   259  	"text/template": {
   260  		"L4", "OS", "net/url", "text/template/parse",
   261  	},
   262  
   263  	// Cgo.
   264  	// If you add a dependency on CGO, you must add the package to
   265  	// cgoPackages in cmd/dist/test.go.
   266  	"runtime/cgo": {"L0", "C"},
   267  	"CGO":         {"C", "runtime/cgo"},
   268  
   269  	// Fake entry to satisfy the pseudo-import "C"
   270  	// that shows up in programs that use cgo.
   271  	"C": {},
   272  
   273  	// Race detector/MSan uses cgo.
   274  	"runtime/race": {"C"},
   275  	"runtime/msan": {"C"},
   276  
   277  	// Plan 9 alone needs io/ioutil and os.
   278  	"os/user": {"L4", "CGO", "io/ioutil", "os", "syscall"},
   279  
   280  	// Basic networking.
   281  	// Because net must be used by any package that wants to
   282  	// do networking portably, it must have a small dependency set: just L0+basic os.
   283  	"net": {"L0", "CGO", "math/rand", "os", "sort", "syscall", "time", "internal/syscall/windows", "internal/singleflight", "internal/race"},
   284  
   285  	// NET enables use of basic network-related packages.
   286  	"NET": {
   287  		"net",
   288  		"mime",
   289  		"net/textproto",
   290  		"net/url",
   291  	},
   292  
   293  	// Uses of networking.
   294  	"log/syslog":    {"L4", "OS", "net"},
   295  	"net/mail":      {"L4", "NET", "OS", "mime"},
   296  	"net/textproto": {"L4", "OS", "net"},
   297  
   298  	// Core crypto.
   299  	"crypto/aes":    {"L3"},
   300  	"crypto/des":    {"L3"},
   301  	"crypto/hmac":   {"L3"},
   302  	"crypto/md5":    {"L3"},
   303  	"crypto/rc4":    {"L3"},
   304  	"crypto/sha1":   {"L3"},
   305  	"crypto/sha256": {"L3"},
   306  	"crypto/sha512": {"L3"},
   307  
   308  	"CRYPTO": {
   309  		"crypto/aes",
   310  		"crypto/des",
   311  		"crypto/hmac",
   312  		"crypto/md5",
   313  		"crypto/rc4",
   314  		"crypto/sha1",
   315  		"crypto/sha256",
   316  		"crypto/sha512",
   317  	},
   318  
   319  	// Random byte, number generation.
   320  	// This would be part of core crypto except that it imports
   321  	// math/big, which imports fmt.
   322  	"crypto/rand": {"L4", "CRYPTO", "OS", "math/big", "syscall", "internal/syscall/unix"},
   323  
   324  	// Mathematical crypto: dependencies on fmt (L4) and math/big.
   325  	// We could avoid some of the fmt, but math/big imports fmt anyway.
   326  	"crypto/dsa":      {"L4", "CRYPTO", "math/big"},
   327  	"crypto/ecdsa":    {"L4", "CRYPTO", "crypto/elliptic", "math/big", "encoding/asn1"},
   328  	"crypto/elliptic": {"L4", "CRYPTO", "math/big"},
   329  	"crypto/rsa":      {"L4", "CRYPTO", "crypto/rand", "math/big"},
   330  
   331  	"CRYPTO-MATH": {
   332  		"CRYPTO",
   333  		"crypto/dsa",
   334  		"crypto/ecdsa",
   335  		"crypto/elliptic",
   336  		"crypto/rand",
   337  		"crypto/rsa",
   338  		"encoding/asn1",
   339  		"math/big",
   340  	},
   341  
   342  	// SSL/TLS.
   343  	"crypto/tls": {
   344  		"L4", "CRYPTO-MATH", "OS",
   345  		"container/list", "crypto/x509", "encoding/pem", "net", "syscall",
   346  	},
   347  	"crypto/x509": {
   348  		"L4", "CRYPTO-MATH", "OS", "CGO",
   349  		"crypto/x509/pkix", "encoding/pem", "encoding/hex", "net", "syscall",
   350  	},
   351  	"crypto/x509/pkix": {"L4", "CRYPTO-MATH"},
   352  
   353  	// Simple net+crypto-aware packages.
   354  	"mime/multipart": {"L4", "OS", "mime", "crypto/rand", "net/textproto", "mime/quotedprintable"},
   355  	"net/smtp":       {"L4", "CRYPTO", "NET", "crypto/tls"},
   356  
   357  	// HTTP, kingpin of dependencies.
   358  	"net/http": {
   359  		"L4", "NET", "OS",
   360  		"context", "compress/gzip", "crypto/tls",
   361  		"mime/multipart", "runtime/debug",
   362  		"net/http/internal",
   363  		"golang.org/x/net/http2/hpack",
   364  	},
   365  	"net/http/internal": {"L4"},
   366  
   367  	// HTTP-using packages.
   368  	"expvar":             {"L4", "OS", "encoding/json", "net/http"},
   369  	"net/http/cgi":       {"L4", "NET", "OS", "crypto/tls", "net/http", "regexp"},
   370  	"net/http/cookiejar": {"L4", "NET", "net/http"},
   371  	"net/http/fcgi":      {"L4", "NET", "OS", "net/http", "net/http/cgi"},
   372  	"net/http/httptest":  {"L4", "NET", "OS", "crypto/tls", "flag", "net/http", "net/http/internal"},
   373  	"net/http/httputil":  {"L4", "NET", "OS", "net/http", "net/http/internal"},
   374  	"net/http/pprof":     {"L4", "OS", "html/template", "net/http", "runtime/pprof", "runtime/trace"},
   375  	"net/rpc":            {"L4", "NET", "encoding/gob", "html/template", "net/http"},
   376  	"net/rpc/jsonrpc":    {"L4", "NET", "encoding/json", "net/rpc"},
   377  }
   378  
   379  // isMacro reports whether p is a package dependency macro
   380  // (uppercase name).
   381  func isMacro(p string) bool {
   382  	return 'A' <= p[0] && p[0] <= 'Z'
   383  }
   384  
   385  func allowed(pkg string) map[string]bool {
   386  	m := map[string]bool{}
   387  	var allow func(string)
   388  	allow = func(p string) {
   389  		if m[p] {
   390  			return
   391  		}
   392  		m[p] = true // set even for macros, to avoid loop on cycle
   393  
   394  		// Upper-case names are macro-expanded.
   395  		if isMacro(p) {
   396  			for _, pp := range pkgDeps[p] {
   397  				allow(pp)
   398  			}
   399  		}
   400  	}
   401  	for _, pp := range pkgDeps[pkg] {
   402  		allow(pp)
   403  	}
   404  	return m
   405  }
   406  
   407  // listStdPkgs returns the same list of packages as "go list std".
   408  func listStdPkgs(goroot string) ([]string, error) {
   409  	// Based on cmd/go's matchPackages function.
   410  	var pkgs []string
   411  
   412  	src := filepath.Join(goroot, "src") + string(filepath.Separator)
   413  	walkFn := func(path string, fi os.FileInfo, err error) error {
   414  		if err != nil || !fi.IsDir() || path == src {
   415  			return nil
   416  		}
   417  
   418  		base := filepath.Base(path)
   419  		if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
   420  			return filepath.SkipDir
   421  		}
   422  
   423  		name := filepath.ToSlash(path[len(src):])
   424  		if name == "builtin" || name == "cmd" || strings.Contains(name, ".") {
   425  			return filepath.SkipDir
   426  		}
   427  
   428  		pkgs = append(pkgs, name)
   429  		return nil
   430  	}
   431  	if err := filepath.Walk(src, walkFn); err != nil {
   432  		return nil, err
   433  	}
   434  	return pkgs, nil
   435  }
   436  
   437  func TestDependencies(t *testing.T) {
   438  	iOS := runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64")
   439  	if runtime.GOOS == "nacl" || iOS {
   440  		// Tests run in a limited file system and we do not
   441  		// provide access to every source file.
   442  		t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
   443  	}
   444  
   445  	ctxt := Default
   446  	all, err := listStdPkgs(ctxt.GOROOT)
   447  	if err != nil {
   448  		t.Fatal(err)
   449  	}
   450  	sort.Strings(all)
   451  
   452  	for _, pkg := range all {
   453  		imports, err := findImports(pkg)
   454  		if err != nil {
   455  			t.Error(err)
   456  			continue
   457  		}
   458  		ok := allowed(pkg)
   459  		var bad []string
   460  		for _, imp := range imports {
   461  			if !ok[imp] {
   462  				bad = append(bad, imp)
   463  			}
   464  		}
   465  		if bad != nil {
   466  			t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
   467  		}
   468  	}
   469  }
   470  
   471  var buildIgnore = []byte("\n// +build ignore")
   472  
   473  func findImports(pkg string) ([]string, error) {
   474  	dir := filepath.Join(Default.GOROOT, "src", pkg)
   475  	files, err := ioutil.ReadDir(dir)
   476  	if err != nil {
   477  		return nil, err
   478  	}
   479  	var imports []string
   480  	var haveImport = map[string]bool{}
   481  	for _, file := range files {
   482  		name := file.Name()
   483  		if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
   484  			continue
   485  		}
   486  		f, err := os.Open(filepath.Join(dir, name))
   487  		if err != nil {
   488  			return nil, err
   489  		}
   490  		var imp []string
   491  		data, err := readImports(f, false, &imp)
   492  		f.Close()
   493  		if err != nil {
   494  			return nil, fmt.Errorf("reading %v: %v", name, err)
   495  		}
   496  		if bytes.Contains(data, buildIgnore) {
   497  			continue
   498  		}
   499  		for _, quoted := range imp {
   500  			path, err := strconv.Unquote(quoted)
   501  			if err != nil {
   502  				continue
   503  			}
   504  			if !haveImport[path] {
   505  				haveImport[path] = true
   506  				imports = append(imports, path)
   507  			}
   508  		}
   509  	}
   510  	sort.Strings(imports)
   511  	return imports, nil
   512  }