]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/build/deps_test.go
[dev.boringcrypto] misc/boring: add new releases to RELEASES file
[gostls13.git] / src / go / build / deps_test.go
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":                  {"runtime", "internal/reflectlite"},
38         "io":                      {"errors", "sync", "sync/atomic"},
39         "runtime":                 {"unsafe", "runtime/internal/atomic", "runtime/internal/sys", "runtime/internal/math", "internal/cpu", "internal/bytealg"},
40         "runtime/internal/sys":    {},
41         "runtime/internal/atomic": {"unsafe", "internal/cpu"},
42         "runtime/internal/math":   {"runtime/internal/sys"},
43         "internal/race":           {"runtime", "unsafe"},
44         "sync":                    {"internal/race", "runtime", "sync/atomic", "unsafe"},
45         "sync/atomic":             {"unsafe"},
46         "unsafe":                  {},
47         "internal/cpu":            {},
48         "internal/bytealg":        {"unsafe", "internal/cpu"},
49         "internal/reflectlite":    {"runtime", "unsafe"},
50
51         "L0": {
52                 "errors",
53                 "io",
54                 "runtime",
55                 "runtime/internal/atomic",
56                 "sync",
57                 "sync/atomic",
58                 "unsafe",
59                 "internal/cpu",
60                 "internal/bytealg",
61                 "internal/reflectlite",
62         },
63
64         // L1 adds simple functions and strings processing,
65         // but not Unicode tables.
66         "math":          {"internal/cpu", "unsafe", "math/bits"},
67         "math/bits":     {"unsafe"},
68         "math/cmplx":    {"math", "math/bits"},
69         "math/rand":     {"L0", "math"},
70         "strconv":       {"L0", "unicode/utf8", "math", "math/bits"},
71         "unicode/utf16": {},
72         "unicode/utf8":  {},
73
74         "L1": {
75                 "L0",
76                 "math",
77                 "math/bits",
78                 "math/cmplx",
79                 "math/rand",
80                 "sort",
81                 "strconv",
82                 "unicode/utf16",
83                 "unicode/utf8",
84         },
85
86         // L2 adds Unicode and strings processing.
87         "bufio":   {"L0", "unicode/utf8", "bytes"},
88         "bytes":   {"L0", "unicode", "unicode/utf8"},
89         "path":    {"L0", "unicode/utf8", "strings"},
90         "strings": {"L0", "unicode", "unicode/utf8"},
91         "unicode": {},
92
93         "L2": {
94                 "L1",
95                 "bufio",
96                 "bytes",
97                 "path",
98                 "strings",
99                 "unicode",
100         },
101
102         // L3 adds reflection and some basic utility packages
103         // and interface definitions, but nothing that makes
104         // system calls.
105         "crypto":                 {"L2", "hash"}, // interfaces
106         "crypto/cipher":          {"L2", "crypto/subtle", "crypto/internal/subtle", "encoding/binary"},
107         "crypto/internal/subtle": {"unsafe", "reflect"}, // reflect behind a appengine tag
108         "crypto/subtle":          {},
109         "encoding/base32":        {"L2"},
110         "encoding/base64":        {"L2", "encoding/binary"},
111         "encoding/binary":        {"L2", "reflect"},
112         "hash":                   {"L2"}, // interfaces
113         "hash/adler32":           {"L2", "hash"},
114         "hash/crc32":             {"L2", "hash"},
115         "hash/crc64":             {"L2", "hash"},
116         "hash/fnv":               {"L2", "hash"},
117         "hash/maphash":           {"L2", "hash"},
118         "image":                  {"L2", "image/color"}, // interfaces
119         "image/color":            {"L2"},                // interfaces
120         "image/color/palette":    {"L2", "image/color"},
121         "internal/fmtsort":       {"reflect", "sort"},
122         "reflect":                {"L2"},
123         "sort":                   {"internal/reflectlite"},
124
125         "crypto/internal/boring":         {"L2", "C", "crypto", "crypto/cipher", "crypto/internal/boring/sig", "crypto/subtle", "encoding/asn1", "hash", "math/big"},
126         "crypto/internal/boring/fipstls": {"sync/atomic"},
127         "crypto/internal/cipherhw":       {"crypto/internal/boring"},
128         "crypto/tls/fipsonly":            {"crypto/internal/boring/fipstls", "crypto/internal/boring/sig"},
129
130         "L3": {
131                 "L2",
132                 "crypto",
133                 "crypto/cipher",
134                 "crypto/internal/boring",
135                 "crypto/internal/boring/fipstls",
136                 "crypto/internal/subtle",
137                 "crypto/subtle",
138                 "encoding/base32",
139                 "encoding/base64",
140                 "encoding/binary",
141                 "hash",
142                 "hash/adler32",
143                 "hash/crc32",
144                 "hash/crc64",
145                 "hash/fnv",
146                 "image",
147                 "image/color",
148                 "image/color/palette",
149                 "internal/fmtsort",
150                 "internal/oserror",
151                 "reflect",
152         },
153
154         // End of linear dependency definitions.
155
156         // Operating system access.
157         "syscall":                           {"L0", "internal/oserror", "internal/race", "internal/syscall/windows/sysdll", "syscall/js", "unicode/utf16"},
158         "syscall/js":                        {"L0"},
159         "internal/oserror":                  {"L0"},
160         "internal/syscall/unix":             {"L0", "syscall"},
161         "internal/syscall/windows":          {"L0", "syscall", "internal/syscall/windows/sysdll", "unicode/utf16"},
162         "internal/syscall/windows/registry": {"L0", "syscall", "internal/syscall/windows/sysdll", "unicode/utf16"},
163         "internal/syscall/execenv":          {"L0", "syscall", "internal/syscall/windows", "unicode/utf16"},
164         "time": {
165                 // "L0" without the "io" package:
166                 "errors",
167                 "runtime",
168                 "runtime/internal/atomic",
169                 "sync",
170                 "sync/atomic",
171                 "unsafe",
172                 // Other time dependencies:
173                 "internal/syscall/windows/registry",
174                 "syscall",
175                 "syscall/js",
176         },
177
178         "internal/cfg":     {"L0"},
179         "internal/poll":    {"L0", "internal/oserror", "internal/race", "syscall", "time", "unicode/utf16", "unicode/utf8", "internal/syscall/windows", "internal/syscall/unix"},
180         "internal/testlog": {"L0"},
181         "os":               {"L1", "os", "syscall", "time", "internal/oserror", "internal/poll", "internal/syscall/windows", "internal/syscall/unix", "internal/syscall/execenv", "internal/testlog"},
182         "path/filepath":    {"L2", "os", "syscall", "internal/syscall/windows"},
183         "io/ioutil":        {"L2", "os", "path/filepath", "time"},
184         "os/exec":          {"L2", "os", "context", "path/filepath", "syscall", "internal/syscall/execenv"},
185         "os/signal":        {"L2", "os", "syscall"},
186
187         // OS enables basic operating system functionality,
188         // but not direct use of package syscall, nor os/signal.
189         "OS": {
190                 "io/ioutil",
191                 "os",
192                 "os/exec",
193                 "path/filepath",
194                 "time",
195         },
196
197         // Formatted I/O: few dependencies (L1) but we must add reflect and internal/fmtsort.
198         "fmt": {"L1", "os", "reflect", "internal/fmtsort"},
199         "log": {"L1", "os", "fmt", "time"},
200
201         // Packages used by testing must be low-level (L2+fmt).
202         "regexp":         {"L2", "regexp/syntax"},
203         "regexp/syntax":  {"L2"},
204         "runtime/debug":  {"L2", "fmt", "io/ioutil", "os", "time"},
205         "runtime/pprof":  {"L2", "compress/gzip", "context", "encoding/binary", "fmt", "io/ioutil", "os", "syscall", "text/tabwriter", "time"},
206         "runtime/trace":  {"L0", "context", "fmt"},
207         "text/tabwriter": {"L2"},
208
209         "testing":                  {"L2", "flag", "fmt", "internal/race", "os", "runtime/debug", "runtime/pprof", "runtime/trace", "time"},
210         "testing/iotest":           {"L2", "log"},
211         "testing/quick":            {"L2", "flag", "fmt", "reflect", "time"},
212         "internal/obscuretestdata": {"L2", "OS", "encoding/base64"},
213         "internal/testenv":         {"L2", "OS", "flag", "testing", "syscall", "internal/cfg"},
214         "internal/lazyregexp":      {"L2", "OS", "regexp"},
215         "internal/lazytemplate":    {"L2", "OS", "text/template"},
216
217         // L4 is defined as L3+fmt+log+time, because in general once
218         // you're using L3 packages, use of fmt, log, or time is not a big deal.
219         "L4": {
220                 "L3",
221                 "fmt",
222                 "log",
223                 "time",
224         },
225
226         // Go parser.
227         "go/ast":     {"L4", "OS", "go/scanner", "go/token"},
228         "go/doc":     {"L4", "OS", "go/ast", "go/token", "regexp", "internal/lazyregexp", "text/template"},
229         "go/parser":  {"L4", "OS", "go/ast", "go/scanner", "go/token"},
230         "go/printer": {"L4", "OS", "go/ast", "go/scanner", "go/token", "text/tabwriter"},
231         "go/scanner": {"L4", "OS", "go/token"},
232         "go/token":   {"L4"},
233
234         "GOPARSER": {
235                 "go/ast",
236                 "go/doc",
237                 "go/parser",
238                 "go/printer",
239                 "go/scanner",
240                 "go/token",
241         },
242
243         "go/format":       {"L4", "GOPARSER", "internal/format"},
244         "internal/format": {"L4", "GOPARSER"},
245
246         // Go type checking.
247         "go/constant":               {"L4", "go/token", "math/big"},
248         "go/importer":               {"L4", "go/build", "go/internal/gccgoimporter", "go/internal/gcimporter", "go/internal/srcimporter", "go/token", "go/types"},
249         "go/internal/gcimporter":    {"L4", "OS", "go/build", "go/constant", "go/token", "go/types", "text/scanner"},
250         "go/internal/gccgoimporter": {"L4", "OS", "debug/elf", "go/constant", "go/token", "go/types", "internal/xcoff", "text/scanner"},
251         "go/internal/srcimporter":   {"L4", "OS", "fmt", "go/ast", "go/build", "go/parser", "go/token", "go/types", "path/filepath"},
252         "go/types":                  {"L4", "GOPARSER", "container/heap", "go/constant"},
253
254         // One of a kind.
255         "archive/tar":                    {"L4", "OS", "syscall", "os/user"},
256         "archive/zip":                    {"L4", "OS", "compress/flate"},
257         "container/heap":                 {"sort"},
258         "compress/bzip2":                 {"L4"},
259         "compress/flate":                 {"L4"},
260         "compress/gzip":                  {"L4", "compress/flate"},
261         "compress/lzw":                   {"L4"},
262         "compress/zlib":                  {"L4", "compress/flate"},
263         "context":                        {"errors", "internal/reflectlite", "sync", "sync/atomic", "time"},
264         "database/sql":                   {"L4", "container/list", "context", "database/sql/driver", "database/sql/internal"},
265         "database/sql/driver":            {"L4", "context", "time", "database/sql/internal"},
266         "debug/dwarf":                    {"L4"},
267         "debug/elf":                      {"L4", "OS", "debug/dwarf", "compress/zlib"},
268         "debug/gosym":                    {"L4"},
269         "debug/macho":                    {"L4", "OS", "debug/dwarf", "compress/zlib"},
270         "debug/pe":                       {"L4", "OS", "debug/dwarf", "compress/zlib"},
271         "debug/plan9obj":                 {"L4", "OS"},
272         "encoding":                       {"L4"},
273         "encoding/ascii85":               {"L4"},
274         "encoding/asn1":                  {"L4", "math/big"},
275         "encoding/csv":                   {"L4"},
276         "encoding/gob":                   {"L4", "OS", "encoding"},
277         "encoding/hex":                   {"L4"},
278         "encoding/json":                  {"L4", "encoding"},
279         "encoding/pem":                   {"L4"},
280         "encoding/xml":                   {"L4", "encoding"},
281         "flag":                           {"L4", "OS"},
282         "go/build":                       {"L4", "OS", "GOPARSER", "internal/goroot", "internal/goversion"},
283         "html":                           {"L4"},
284         "image/draw":                     {"L4", "image/internal/imageutil"},
285         "image/gif":                      {"L4", "compress/lzw", "image/color/palette", "image/draw"},
286         "image/internal/imageutil":       {"L4"},
287         "image/jpeg":                     {"L4", "image/internal/imageutil"},
288         "image/png":                      {"L4", "compress/zlib"},
289         "index/suffixarray":              {"L4", "regexp"},
290         "internal/goroot":                {"L4", "OS"},
291         "internal/singleflight":          {"sync"},
292         "internal/trace":                 {"L4", "OS", "container/heap"},
293         "internal/xcoff":                 {"L4", "OS", "debug/dwarf"},
294         "math/big":                       {"L4"},
295         "mime":                           {"L4", "OS", "syscall", "internal/syscall/windows/registry"},
296         "mime/quotedprintable":           {"L4"},
297         "net/internal/socktest":          {"L4", "OS", "syscall", "internal/syscall/windows"},
298         "net/url":                        {"L4"},
299         "plugin":                         {"L0", "OS", "CGO"},
300         "runtime/pprof/internal/profile": {"L4", "OS", "compress/gzip", "regexp"},
301         "testing/internal/testdeps":      {"L4", "internal/testlog", "runtime/pprof", "regexp"},
302         "text/scanner":                   {"L4", "OS"},
303         "text/template/parse":            {"L4"},
304
305         "html/template": {
306                 "L4", "OS", "encoding/json", "html", "text/template",
307                 "text/template/parse",
308         },
309         "text/template": {
310                 "L4", "OS", "net/url", "text/template/parse",
311         },
312
313         // Cgo.
314         // If you add a dependency on CGO, you must add the package to
315         // cgoPackages in cmd/dist/test.go.
316         "runtime/cgo": {"L0", "C"},
317         "CGO":         {"C", "runtime/cgo"},
318
319         // Fake entry to satisfy the pseudo-import "C"
320         // that shows up in programs that use cgo.
321         "C": {},
322
323         // Race detector/MSan uses cgo.
324         "runtime/race": {"C"},
325         "runtime/msan": {"C"},
326
327         // Plan 9 alone needs io/ioutil and os.
328         "os/user": {"L4", "CGO", "io/ioutil", "os", "syscall", "internal/syscall/windows", "internal/syscall/windows/registry"},
329
330         // Internal package used only for testing.
331         "os/signal/internal/pty": {"CGO", "fmt", "os", "syscall"},
332
333         // Basic networking.
334         // Because net must be used by any package that wants to
335         // do networking portably, it must have a small dependency set: just L0+basic os.
336         "net": {
337                 "L0", "CGO",
338                 "context", "math/rand", "os", "sort", "syscall", "time",
339                 "internal/nettrace", "internal/poll", "internal/syscall/unix",
340                 "internal/syscall/windows", "internal/singleflight", "internal/race",
341                 "golang.org/x/net/dns/dnsmessage", "golang.org/x/net/lif", "golang.org/x/net/route",
342         },
343
344         // NET enables use of basic network-related packages.
345         "NET": {
346                 "net",
347                 "mime",
348                 "net/textproto",
349                 "net/url",
350         },
351
352         // Uses of networking.
353         "log/syslog":    {"L4", "OS", "net"},
354         "net/mail":      {"L4", "NET", "OS", "mime"},
355         "net/textproto": {"L4", "OS", "net"},
356
357         // Core crypto.
358         "crypto/aes":               {"L3"},
359         "crypto/des":               {"L3"},
360         "crypto/hmac":              {"L3"},
361         "crypto/internal/randutil": {"io", "sync"},
362         "crypto/md5":               {"L3"},
363         "crypto/rc4":               {"L3"},
364         "crypto/sha1":              {"L3"},
365         "crypto/sha256":            {"L3"},
366         "crypto/sha512":            {"L3"},
367
368         "CRYPTO": {
369                 "crypto/aes",
370                 "crypto/des",
371                 "crypto/hmac",
372                 "crypto/internal/randutil",
373                 "crypto/md5",
374                 "crypto/rc4",
375                 "crypto/sha1",
376                 "crypto/sha256",
377                 "crypto/sha512",
378                 "golang.org/x/crypto/chacha20poly1305",
379                 "golang.org/x/crypto/curve25519",
380                 "golang.org/x/crypto/poly1305",
381         },
382
383         // Random byte, number generation.
384         // This would be part of core crypto except that it imports
385         // math/big, which imports fmt.
386         "crypto/rand": {"L4", "CRYPTO", "OS", "math/big", "syscall", "syscall/js", "internal/syscall/unix"},
387
388         // Not part of CRYPTO because it imports crypto/rand and crypto/sha512.
389         "crypto/ed25519":                       {"L3", "CRYPTO", "crypto/rand", "crypto/ed25519/internal/edwards25519"},
390         "crypto/ed25519/internal/edwards25519": {"encoding/binary"},
391
392         // Mathematical crypto: dependencies on fmt (L4) and math/big.
393         // We could avoid some of the fmt, but math/big imports fmt anyway.
394         "crypto/dsa": {"L4", "CRYPTO", "math/big"},
395         "crypto/ecdsa": {
396                 "L4", "CRYPTO", "crypto/elliptic", "math/big",
397                 "golang.org/x/crypto/cryptobyte", "golang.org/x/crypto/cryptobyte/asn1",
398         },
399         "crypto/elliptic": {"L4", "CRYPTO", "math/big"},
400         "crypto/rsa":      {"L4", "CRYPTO", "crypto/rand", "math/big"},
401
402         "CRYPTO-MATH": {
403                 "CRYPTO",
404                 "crypto/dsa",
405                 "crypto/ecdsa",
406                 "crypto/elliptic",
407                 "crypto/rand",
408                 "crypto/rsa",
409                 "encoding/asn1",
410                 "math/big",
411         },
412
413         // SSL/TLS.
414         "crypto/tls": {
415                 "L4", "CRYPTO-MATH", "OS", "golang.org/x/crypto/cryptobyte", "golang.org/x/crypto/hkdf",
416                 "container/list", "crypto/x509", "encoding/pem", "net", "syscall", "crypto/ed25519",
417         },
418         "crypto/x509": {
419                 "L4", "CRYPTO-MATH", "OS", "CGO", "crypto/ed25519",
420                 "crypto/x509/pkix", "encoding/pem", "encoding/hex", "net", "os/user", "syscall", "net/url",
421                 "golang.org/x/crypto/cryptobyte", "golang.org/x/crypto/cryptobyte/asn1",
422         },
423         "crypto/x509/pkix": {"L4", "CRYPTO-MATH", "encoding/hex"},
424
425         // Simple net+crypto-aware packages.
426         "mime/multipart": {"L4", "OS", "mime", "crypto/rand", "net/textproto", "mime/quotedprintable"},
427         "net/smtp":       {"L4", "CRYPTO", "NET", "crypto/tls"},
428
429         // HTTP, kingpin of dependencies.
430         "net/http": {
431                 "L4", "NET", "OS",
432                 "compress/gzip",
433                 "container/list",
434                 "context",
435                 "crypto/rand",
436                 "crypto/tls",
437                 "golang.org/x/net/http/httpguts",
438                 "golang.org/x/net/http/httpproxy",
439                 "golang.org/x/net/http2/hpack",
440                 "golang.org/x/net/idna",
441                 "golang.org/x/text/unicode/norm",
442                 "golang.org/x/text/width",
443                 "internal/nettrace",
444                 "mime/multipart",
445                 "net/http/httptrace",
446                 "net/http/internal",
447                 "runtime/debug",
448                 "syscall/js",
449         },
450         "net/http/internal":  {"L4"},
451         "net/http/httptrace": {"context", "crypto/tls", "internal/nettrace", "net", "net/textproto", "reflect", "time"},
452
453         // HTTP-using packages.
454         "expvar":             {"L4", "OS", "encoding/json", "net/http"},
455         "net/http/cgi":       {"L4", "NET", "OS", "crypto/tls", "net/http", "regexp"},
456         "net/http/cookiejar": {"L4", "NET", "net/http"},
457         "net/http/fcgi":      {"L4", "NET", "OS", "context", "net/http", "net/http/cgi"},
458         "net/http/httptest": {
459                 "L4", "NET", "OS", "crypto/tls", "flag", "net/http", "net/http/internal", "crypto/x509",
460                 "golang.org/x/net/http/httpguts",
461         },
462         "net/http/httputil": {"L4", "NET", "OS", "context", "net/http", "net/http/internal", "golang.org/x/net/http/httpguts"},
463         "net/http/pprof":    {"L4", "OS", "html/template", "net/http", "runtime/pprof", "runtime/trace"},
464         "net/rpc":           {"L4", "NET", "encoding/gob", "html/template", "net/http", "go/token"},
465         "net/rpc/jsonrpc":   {"L4", "NET", "encoding/json", "net/rpc"},
466 }
467
468 // isMacro reports whether p is a package dependency macro
469 // (uppercase name).
470 func isMacro(p string) bool {
471         return 'A' <= p[0] && p[0] <= 'Z'
472 }
473
474 func allowed(pkg string) map[string]bool {
475         m := map[string]bool{}
476         var allow func(string)
477         allow = func(p string) {
478                 if m[p] {
479                         return
480                 }
481                 m[p] = true // set even for macros, to avoid loop on cycle
482
483                 // Upper-case names are macro-expanded.
484                 if isMacro(p) {
485                         for _, pp := range pkgDeps[p] {
486                                 allow(pp)
487                         }
488                 }
489         }
490         for _, pp := range pkgDeps[pkg] {
491                 allow(pp)
492         }
493         return m
494 }
495
496 // listStdPkgs returns the same list of packages as "go list std".
497 func listStdPkgs(goroot string) ([]string, error) {
498         // Based on cmd/go's matchPackages function.
499         var pkgs []string
500
501         src := filepath.Join(goroot, "src") + string(filepath.Separator)
502         walkFn := func(path string, fi os.FileInfo, err error) error {
503                 if err != nil || !fi.IsDir() || path == src {
504                         return nil
505                 }
506
507                 base := filepath.Base(path)
508                 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
509                         return filepath.SkipDir
510                 }
511
512                 name := filepath.ToSlash(path[len(src):])
513                 if name == "builtin" || name == "cmd" || strings.Contains(name, "golang.org/x/") {
514                         return filepath.SkipDir
515                 }
516
517                 pkgs = append(pkgs, name)
518                 return nil
519         }
520         if err := filepath.Walk(src, walkFn); err != nil {
521                 return nil, err
522         }
523         return pkgs, nil
524 }
525
526 func TestDependencies(t *testing.T) {
527         iOS := runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64")
528         if iOS {
529                 // Tests run in a limited file system and we do not
530                 // provide access to every source file.
531                 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
532         }
533
534         ctxt := Default
535         all, err := listStdPkgs(ctxt.GOROOT)
536         if err != nil {
537                 t.Fatal(err)
538         }
539         sort.Strings(all)
540
541         sawImport := map[string]map[string]bool{} // from package => to package => true
542
543         for _, pkg := range all {
544                 imports, err := findImports(pkg)
545                 if err != nil {
546                         t.Error(err)
547                         continue
548                 }
549                 if sawImport[pkg] == nil {
550                         sawImport[pkg] = map[string]bool{}
551                 }
552                 ok := allowed(pkg)
553                 var bad []string
554                 for _, imp := range imports {
555                         sawImport[pkg][imp] = true
556                         if !ok[imp] {
557                                 bad = append(bad, imp)
558                         }
559                 }
560                 if bad != nil {
561                         t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
562                 }
563         }
564
565         // depPath returns the path between the given from and to packages.
566         // It returns the empty string if there's no dependency path.
567         var depPath func(string, string) string
568         depPath = func(from, to string) string {
569                 if sawImport[from][to] {
570                         return from + " => " + to
571                 }
572                 for pkg := range sawImport[from] {
573                         if p := depPath(pkg, to); p != "" {
574                                 return from + " => " + p
575                         }
576                 }
577                 return ""
578         }
579
580         // Also test some high-level policy goals are being met by not finding
581         // these dependency paths:
582         badPaths := []struct{ from, to string }{
583                 {"net", "unicode"},
584                 {"os", "unicode"},
585         }
586
587         for _, path := range badPaths {
588                 if how := depPath(path.from, path.to); how != "" {
589                         t.Errorf("policy violation: %s", how)
590                 }
591         }
592
593 }
594
595 var buildIgnore = []byte("\n// +build ignore")
596
597 func findImports(pkg string) ([]string, error) {
598         dir := filepath.Join(Default.GOROOT, "src", pkg)
599         files, err := ioutil.ReadDir(dir)
600         if err != nil {
601                 return nil, err
602         }
603         var imports []string
604         var haveImport = map[string]bool{}
605         for _, file := range files {
606                 name := file.Name()
607                 if name == "slice_go14.go" || name == "slice_go18.go" {
608                         // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
609                         continue
610                 }
611                 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
612                         continue
613                 }
614                 f, err := os.Open(filepath.Join(dir, name))
615                 if err != nil {
616                         return nil, err
617                 }
618                 var imp []string
619                 data, err := readImports(f, false, &imp)
620                 f.Close()
621                 if err != nil {
622                         return nil, fmt.Errorf("reading %v: %v", name, err)
623                 }
624                 if bytes.Contains(data, buildIgnore) {
625                         continue
626                 }
627                 for _, quoted := range imp {
628                         path, err := strconv.Unquote(quoted)
629                         if err != nil {
630                                 continue
631                         }
632                         if !haveImport[path] {
633                                 haveImport[path] = true
634                                 imports = append(imports, path)
635                         }
636                 }
637         }
638         sort.Strings(imports)
639         return imports, nil
640 }