]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/build/deps_test.go
crypto/ecdsa: draw a fixed amount of entropy while signing
[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         "go/token"
14         "internal/testenv"
15         "io/fs"
16         "os"
17         "path/filepath"
18         "runtime"
19         "sort"
20         "strings"
21         "testing"
22 )
23
24 // depsRules defines the expected dependencies between packages in
25 // the Go source tree. It is a statement of policy.
26 //
27 // DO NOT CHANGE THIS DATA TO FIX BUILDS.
28 // Existing packages should not have their constraints relaxed
29 // without prior discussion.
30 // Negative assertions should almost never be removed.
31 //
32 // The general syntax of a rule is:
33 //
34 //              a, b < c, d;
35 //
36 // which means c and d come after a and b in the partial order
37 // (that is, c and d can import a and b),
38 // but doesn't provide a relative order between a vs b or c vs d.
39 //
40 // The rules can chain together, as in:
41 //
42 //              e < f, g < h;
43 //
44 // which is equivalent to
45 //
46 //              e < f, g;
47 //              f, g < h;
48 //
49 // Except for the special bottom element "NONE", each name
50 // must appear exactly once on the right-hand side of a rule.
51 // That rule serves as the definition of the allowed dependencies
52 // for that name. The definition must appear before any uses
53 // of the name on the left-hand side of a rule. (That is, the
54 // rules themselves must be ordered according to the partial
55 // order, for easier reading by people.)
56 //
57 // Negative assertions double-check the partial order:
58 //
59 //              i !< j
60 //
61 // means that it must NOT be the case that i < j.
62 // Negative assertions may appear anywhere in the rules,
63 // even before i and j have been defined.
64 //
65 // Comments begin with #.
66 //
67 // All-caps names are pseudo-names for specific points
68 // in the dependency lattice.
69 //
70 var depsRules = `
71         # No dependencies allowed for any of these packages.
72         NONE
73         < constraints, container/list, container/ring,
74           internal/cfg, internal/cpu, internal/goarch,
75           internal/goexperiment, internal/goos,
76           internal/goversion, internal/nettrace,
77           unicode/utf8, unicode/utf16, unicode,
78           unsafe;
79
80         # These packages depend only on internal/goarch and unsafe.
81         internal/goarch, unsafe
82         < internal/abi;
83
84         # RUNTIME is the core runtime group of packages, all of them very light-weight.
85         internal/abi, internal/cpu, internal/goarch,
86         internal/goexperiment, internal/goos, unsafe
87         < internal/bytealg
88         < internal/itoa
89         < internal/unsafeheader
90         < runtime/internal/sys
91         < runtime/internal/atomic
92         < runtime/internal/math
93         < runtime
94         < sync/atomic
95         < internal/race
96         < sync
97         < internal/reflectlite
98         < errors
99         < internal/oserror, math/bits
100         < RUNTIME;
101
102         RUNTIME
103         < sort
104         < container/heap;
105
106         RUNTIME
107         < io;
108
109         syscall !< io;
110         reflect !< sort;
111
112         RUNTIME, unicode/utf8
113         < path;
114
115         unicode !< path;
116
117         # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
118         RUNTIME, unicode/utf8, unicode/utf16
119         < internal/syscall/windows/sysdll, syscall/js
120         < syscall
121         < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
122         < internal/syscall/execenv
123         < SYSCALL;
124
125         # TIME is SYSCALL plus the core packages about time, including context.
126         SYSCALL
127         < time/tzdata
128         < time
129         < context
130         < TIME;
131
132         TIME, io, path, sort
133         < io/fs;
134
135         # MATH is RUNTIME plus the basic math packages.
136         RUNTIME
137         < math
138         < MATH;
139
140         unicode !< math;
141
142         MATH
143         < math/cmplx;
144
145         MATH
146         < math/rand;
147
148         MATH
149         < runtime/metrics;
150
151         MATH, unicode/utf8
152         < strconv;
153
154         unicode !< strconv;
155
156         # STR is basic string and buffer manipulation.
157         RUNTIME, io, unicode/utf8, unicode/utf16, unicode
158         < bytes, strings
159         < bufio;
160
161         bufio, path, strconv
162         < STR;
163
164         # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
165         # OS includes string routines, but those must be layered above package os.
166         # OS does not include reflection.
167         io/fs
168         < internal/testlog
169         < internal/poll
170         < os
171         < os/signal;
172
173         io/fs
174         < embed;
175
176         unicode, fmt !< net, os, os/signal;
177
178         os/signal, STR
179         < path/filepath
180         < io/ioutil, os/exec;
181
182         io/ioutil, os/exec, os/signal
183         < OS;
184
185         reflect !< OS;
186
187         OS
188         < golang.org/x/sys/cpu;
189
190         os < internal/godebug;
191
192         # FMT is OS (which includes string routines) plus reflect and fmt.
193         # It does not include package log, which should be avoided in core packages.
194         strconv, unicode
195         < reflect;
196
197         os, reflect
198         < internal/fmtsort
199         < fmt;
200
201         OS, fmt
202         < FMT;
203
204         log !< FMT;
205
206         OS, FMT
207         < internal/execabs;
208
209         OS, internal/execabs
210         < internal/goroot;
211
212         # Misc packages needing only FMT.
213         FMT
214         < flag,
215           html,
216           mime/quotedprintable,
217           net/internal/socktest,
218           net/url,
219           runtime/trace,
220           text/scanner,
221           text/tabwriter;
222
223         # encodings
224         # core ones do not use fmt.
225         io, strconv
226         < encoding;
227
228         encoding, reflect
229         < encoding/binary
230         < encoding/base32, encoding/base64;
231
232         fmt !< encoding/base32, encoding/base64;
233
234         FMT, encoding/base32, encoding/base64
235         < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
236           encoding/json, encoding/pem, encoding/xml, mime;
237
238         # hashes
239         io
240         < hash
241         < hash/adler32, hash/crc32, hash/crc64, hash/fnv, hash/maphash;
242
243         # math/big
244         FMT, encoding/binary, math/rand
245         < math/big;
246
247         # compression
248         FMT, encoding/binary, hash/adler32, hash/crc32
249         < compress/bzip2, compress/flate, compress/lzw
250         < archive/zip, compress/gzip, compress/zlib;
251
252         # templates
253         FMT
254         < text/template/parse;
255
256         net/url, text/template/parse
257         < text/template
258         < internal/lazytemplate;
259
260         encoding/json, html, text/template
261         < html/template;
262
263         # regexp
264         FMT
265         < regexp/syntax
266         < regexp
267         < internal/lazyregexp;
268
269         # suffix array
270         encoding/binary, regexp
271         < index/suffixarray;
272
273         # executable parsing
274         FMT, encoding/binary, compress/zlib
275         < runtime/debug
276         < debug/dwarf
277         < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
278         < debug/buildinfo
279         < DEBUG;
280
281         # go parser and friends.
282         FMT
283         < go/token
284         < go/scanner
285         < go/ast
286         < go/internal/typeparams
287         < go/parser;
288
289         FMT
290         < go/build/constraint;
291
292         go/build/constraint, go/parser, text/tabwriter
293         < go/printer
294         < go/format;
295
296         go/parser, internal/lazyregexp, text/template
297         < go/doc;
298
299         math/big, go/token
300         < go/constant;
301
302         container/heap, go/constant, go/parser, regexp
303         < go/types;
304
305         FMT, internal/goexperiment
306         < internal/buildcfg;
307
308         go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion
309         < go/build;
310
311         DEBUG, go/build, go/types, text/scanner
312         < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
313         < go/importer;
314
315         # databases
316         FMT
317         < database/sql/internal
318         < database/sql/driver
319         < database/sql;
320
321         # images
322         FMT, compress/lzw, compress/zlib
323         < image/color
324         < image, image/color/palette
325         < image/internal/imageutil
326         < image/draw
327         < image/gif, image/jpeg, image/png;
328
329         # cgo, delayed as long as possible.
330         # If you add a dependency on CGO, you must add the package
331         # to cgoPackages in cmd/dist/test.go as well.
332         RUNTIME
333         < C
334         < runtime/cgo
335         < CGO
336         < runtime/race, runtime/msan, runtime/asan;
337
338         # Bulk of the standard library must not use cgo.
339         # The prohibition stops at net and os/user.
340         C !< fmt, go/types, CRYPTO-MATH;
341
342         CGO, OS
343         < plugin;
344
345         CGO, FMT
346         < os/user
347         < archive/tar;
348
349         sync
350         < internal/singleflight;
351
352         os
353         < golang.org/x/net/dns/dnsmessage,
354           golang.org/x/net/lif,
355           golang.org/x/net/route;
356
357         os, runtime, strconv, sync, unsafe,
358         internal/godebug
359         < internal/intern;
360
361         internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
362         < net/netip;
363
364         # net is unavoidable when doing any networking,
365         # so large dependencies must be kept out.
366         # This is a long-looking list but most of these
367         # are small with few dependencies.
368         CGO,
369         golang.org/x/net/dns/dnsmessage,
370         golang.org/x/net/lif,
371         golang.org/x/net/route,
372         internal/godebug,
373         internal/nettrace,
374         internal/poll,
375         internal/singleflight,
376         internal/race,
377         net/netip,
378         os
379         < net;
380
381         fmt, unicode !< net;
382         math/rand !< net; # net uses runtime instead
383
384         # NET is net plus net-helper packages.
385         FMT, net
386         < net/textproto;
387
388         mime, net/textproto, net/url
389         < NET;
390
391         # logging - most packages should not import; http and up is allowed
392         FMT
393         < log;
394
395         log !< crypto/tls, database/sql, go/importer, testing;
396
397         FMT, log, net
398         < log/syslog;
399
400         NET, log
401         < net/mail;
402
403         # CRYPTO is core crypto algorithms - no cgo, fmt, net.
404         # Unfortunately, stuck with reflect via encoding/binary.
405         encoding/binary, golang.org/x/sys/cpu, hash
406         < crypto
407         < crypto/subtle
408         < crypto/internal/subtle
409         < crypto/elliptic/internal/fiat
410         < crypto/elliptic/internal/nistec
411         < crypto/ed25519/internal/edwards25519/field, golang.org/x/crypto/curve25519/internal/field
412         < crypto/ed25519/internal/edwards25519
413         < crypto/cipher
414         < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
415           crypto/sha1, crypto/sha256, crypto/sha512
416         < CRYPTO;
417
418         CGO, fmt, net !< CRYPTO;
419
420         # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
421         CRYPTO, FMT, math/big
422         < crypto/rand
423         < crypto/internal/randutil
424         < crypto/ed25519
425         < encoding/asn1
426         < golang.org/x/crypto/cryptobyte/asn1
427         < golang.org/x/crypto/cryptobyte
428         < golang.org/x/crypto/curve25519
429         < crypto/dsa, crypto/elliptic, crypto/rsa
430         < crypto/ecdsa
431         < CRYPTO-MATH;
432
433         CGO, net !< CRYPTO-MATH;
434
435         # TLS, Prince of Dependencies.
436         CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
437         < golang.org/x/crypto/internal/subtle
438         < golang.org/x/crypto/chacha20
439         < golang.org/x/crypto/poly1305
440         < golang.org/x/crypto/chacha20poly1305
441         < golang.org/x/crypto/hkdf
442         < crypto/x509/internal/macos
443         < crypto/x509/pkix
444         < crypto/x509
445         < crypto/tls;
446
447         # crypto-aware packages
448
449         NET, crypto/rand, mime/quotedprintable
450         < mime/multipart;
451
452         crypto/tls
453         < net/smtp;
454
455         # HTTP, King of Dependencies.
456
457         FMT
458         < golang.org/x/net/http2/hpack
459         < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
460
461         FMT, NET, container/list, encoding/binary, log
462         < golang.org/x/text/transform
463         < golang.org/x/text/unicode/norm
464         < golang.org/x/text/unicode/bidi
465         < golang.org/x/text/secure/bidirule
466         < golang.org/x/net/idna
467         < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
468
469         NET, crypto/tls
470         < net/http/httptrace;
471
472         compress/gzip,
473         golang.org/x/net/http/httpguts,
474         golang.org/x/net/http/httpproxy,
475         golang.org/x/net/http2/hpack,
476         net/http/internal,
477         net/http/internal/ascii,
478         net/http/internal/testcert,
479         net/http/httptrace,
480         mime/multipart,
481         log
482         < net/http;
483
484         # HTTP-aware packages
485
486         encoding/json, net/http
487         < expvar;
488
489         net/http, net/http/internal/ascii
490         < net/http/cookiejar, net/http/httputil;
491
492         net/http, flag
493         < net/http/httptest;
494
495         net/http, regexp
496         < net/http/cgi
497         < net/http/fcgi;
498
499         # Profiling
500         FMT, compress/gzip, encoding/binary, text/tabwriter
501         < runtime/pprof;
502
503         OS, compress/gzip, regexp
504         < internal/profile;
505
506         html, internal/profile, net/http, runtime/pprof, runtime/trace
507         < net/http/pprof;
508
509         # RPC
510         encoding/gob, encoding/json, go/token, html/template, net/http
511         < net/rpc
512         < net/rpc/jsonrpc;
513
514         # System Information
515         internal/cpu, sync
516         < internal/sysinfo;
517
518         # Test-only
519         log
520         < testing/iotest
521         < testing/fstest;
522
523         FMT, flag, math/rand
524         < testing/quick;
525
526         FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
527         < testing;
528
529         FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
530         internal/godebug, math/rand, encoding/hex, crypto/sha256
531         < internal/fuzz;
532
533         internal/fuzz, internal/testlog, runtime/pprof, regexp
534         < testing/internal/testdeps;
535
536         OS, flag, testing, internal/cfg
537         < internal/testenv;
538
539         OS, encoding/base64
540         < internal/obscuretestdata;
541
542         CGO, OS, fmt
543         < os/signal/internal/pty;
544
545         NET, testing, math/rand
546         < golang.org/x/net/nettest;
547
548         FMT, container/heap, math/rand
549         < internal/trace;
550 `
551
552 // listStdPkgs returns the same list of packages as "go list std".
553 func listStdPkgs(goroot string) ([]string, error) {
554         // Based on cmd/go's matchPackages function.
555         var pkgs []string
556
557         src := filepath.Join(goroot, "src") + string(filepath.Separator)
558         walkFn := func(path string, d fs.DirEntry, err error) error {
559                 if err != nil || !d.IsDir() || path == src {
560                         return nil
561                 }
562
563                 base := filepath.Base(path)
564                 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
565                         return filepath.SkipDir
566                 }
567
568                 name := filepath.ToSlash(path[len(src):])
569                 if name == "builtin" || name == "cmd" {
570                         return filepath.SkipDir
571                 }
572
573                 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
574                 return nil
575         }
576         if err := filepath.WalkDir(src, walkFn); err != nil {
577                 return nil, err
578         }
579         return pkgs, nil
580 }
581
582 func TestDependencies(t *testing.T) {
583         if !testenv.HasSrc() {
584                 // Tests run in a limited file system and we do not
585                 // provide access to every source file.
586                 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
587         }
588
589         ctxt := Default
590         all, err := listStdPkgs(ctxt.GOROOT)
591         if err != nil {
592                 t.Fatal(err)
593         }
594         sort.Strings(all)
595
596         sawImport := map[string]map[string]bool{} // from package => to package => true
597         policy := depsPolicy(t)
598
599         for _, pkg := range all {
600                 imports, err := findImports(pkg)
601                 if err != nil {
602                         t.Error(err)
603                         continue
604                 }
605                 if sawImport[pkg] == nil {
606                         sawImport[pkg] = map[string]bool{}
607                 }
608                 ok := policy[pkg]
609                 var bad []string
610                 for _, imp := range imports {
611                         sawImport[pkg][imp] = true
612                         if !ok[imp] {
613                                 bad = append(bad, imp)
614                         }
615                 }
616                 if bad != nil {
617                         t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
618                 }
619         }
620
621         // depPath returns the path between the given from and to packages.
622         // It returns the empty string if there's no dependency path.
623         var depPath func(string, string) string
624         depPath = func(from, to string) string {
625                 if sawImport[from][to] {
626                         return from + " => " + to
627                 }
628                 for pkg := range sawImport[from] {
629                         if p := depPath(pkg, to); p != "" {
630                                 return from + " => " + p
631                         }
632                 }
633                 return ""
634         }
635 }
636
637 var buildIgnore = []byte("\n//go:build ignore")
638
639 func findImports(pkg string) ([]string, error) {
640         vpkg := pkg
641         if strings.HasPrefix(pkg, "golang.org") {
642                 vpkg = "vendor/" + pkg
643         }
644         dir := filepath.Join(Default.GOROOT, "src", vpkg)
645         files, err := os.ReadDir(dir)
646         if err != nil {
647                 return nil, err
648         }
649         var imports []string
650         var haveImport = map[string]bool{}
651         fset := token.NewFileSet()
652         for _, file := range files {
653                 name := file.Name()
654                 if name == "slice_go14.go" || name == "slice_go18.go" {
655                         // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
656                         continue
657                 }
658                 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
659                         continue
660                 }
661                 info := fileInfo{
662                         name: filepath.Join(dir, name),
663                         fset: fset,
664                 }
665                 f, err := os.Open(info.name)
666                 if err != nil {
667                         return nil, err
668                 }
669                 err = readGoInfo(f, &info)
670                 f.Close()
671                 if err != nil {
672                         return nil, fmt.Errorf("reading %v: %v", name, err)
673                 }
674                 if info.parsed.Name.Name == "main" {
675                         continue
676                 }
677                 if bytes.Contains(info.header, buildIgnore) {
678                         continue
679                 }
680                 for _, imp := range info.imports {
681                         path := imp.path
682                         if !haveImport[path] {
683                                 haveImport[path] = true
684                                 imports = append(imports, path)
685                         }
686                 }
687         }
688         sort.Strings(imports)
689         return imports, nil
690 }
691
692 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
693 func depsPolicy(t *testing.T) map[string]map[string]bool {
694         allowed := map[string]map[string]bool{"NONE": {}}
695         disallowed := [][2][]string{}
696
697         parseDepsRules(t, func(deps []string, op string, users []string) {
698                 if op == "!<" {
699                         disallowed = append(disallowed, [2][]string{deps, users})
700                         return
701                 }
702                 for _, u := range users {
703                         if allowed[u] != nil {
704                                 t.Errorf("multiple deps lists for %s", u)
705                         }
706                         allowed[u] = make(map[string]bool)
707                         for _, d := range deps {
708                                 if allowed[d] == nil {
709                                         t.Errorf("use of %s before its deps list", d)
710                                 }
711                                 allowed[u][d] = true
712                         }
713                 }
714         })
715
716         // Check for missing deps info.
717         for _, deps := range allowed {
718                 for d := range deps {
719                         if allowed[d] == nil {
720                                 t.Errorf("missing deps list for %s", d)
721                         }
722                 }
723         }
724
725         // Complete transitive allowed deps.
726         for k := range allowed {
727                 for i := range allowed {
728                         for j := range allowed {
729                                 if i != k && k != j && allowed[i][k] && allowed[k][j] {
730                                         if i == j {
731                                                 // Can only happen along with a "use of X before deps" error above,
732                                                 // but this error is more specific - it makes clear that reordering the
733                                                 // rules will not be enough to fix the problem.
734                                                 t.Errorf("deps policy cycle: %s < %s < %s", j, k, i)
735                                         }
736                                         allowed[i][j] = true
737                                 }
738                         }
739                 }
740         }
741
742         // Check negative assertions against completed allowed deps.
743         for _, bad := range disallowed {
744                 deps, users := bad[0], bad[1]
745                 for _, d := range deps {
746                         for _, u := range users {
747                                 if allowed[u][d] {
748                                         t.Errorf("deps policy incorrect: assertion failed: %s !< %s", d, u)
749                                 }
750                         }
751                 }
752         }
753
754         if t.Failed() {
755                 t.FailNow()
756         }
757
758         return allowed
759 }
760
761 // parseDepsRules parses depsRules, calling save(deps, op, users)
762 // for each deps < users or deps !< users rule
763 // (op is "<" or "!<").
764 func parseDepsRules(t *testing.T, save func(deps []string, op string, users []string)) {
765         p := &depsParser{t: t, lineno: 1, text: depsRules}
766
767         var prev []string
768         var op string
769         for {
770                 list, tok := p.nextList()
771                 if tok == "" {
772                         if prev == nil {
773                                 break
774                         }
775                         p.syntaxError("unexpected EOF")
776                 }
777                 if prev != nil {
778                         save(prev, op, list)
779                 }
780                 prev = list
781                 if tok == ";" {
782                         prev = nil
783                         op = ""
784                         continue
785                 }
786                 if tok != "<" && tok != "!<" {
787                         p.syntaxError("missing <")
788                 }
789                 op = tok
790         }
791 }
792
793 // A depsParser parses the depsRules syntax described above.
794 type depsParser struct {
795         t        *testing.T
796         lineno   int
797         lastWord string
798         text     string
799 }
800
801 // syntaxError reports a parsing error.
802 func (p *depsParser) syntaxError(msg string) {
803         p.t.Fatalf("deps:%d: syntax error: %s near %s", p.lineno, msg, p.lastWord)
804 }
805
806 // nextList parses and returns a comma-separated list of names.
807 func (p *depsParser) nextList() (list []string, token string) {
808         for {
809                 tok := p.nextToken()
810                 switch tok {
811                 case "":
812                         if len(list) == 0 {
813                                 return nil, ""
814                         }
815                         fallthrough
816                 case ",", "<", "!<", ";":
817                         p.syntaxError("bad list syntax")
818                 }
819                 list = append(list, tok)
820
821                 tok = p.nextToken()
822                 if tok != "," {
823                         return list, tok
824                 }
825         }
826 }
827
828 // nextToken returns the next token in the deps rules,
829 // one of ";" "," "<" "!<" or a name.
830 func (p *depsParser) nextToken() string {
831         for {
832                 if p.text == "" {
833                         return ""
834                 }
835                 switch p.text[0] {
836                 case ';', ',', '<':
837                         t := p.text[:1]
838                         p.text = p.text[1:]
839                         return t
840
841                 case '!':
842                         if len(p.text) < 2 || p.text[1] != '<' {
843                                 p.syntaxError("unexpected token !")
844                         }
845                         p.text = p.text[2:]
846                         return "!<"
847
848                 case '#':
849                         i := strings.Index(p.text, "\n")
850                         if i < 0 {
851                                 i = len(p.text)
852                         }
853                         p.text = p.text[i:]
854                         continue
855
856                 case '\n':
857                         p.lineno++
858                         fallthrough
859                 case ' ', '\t':
860                         p.text = p.text[1:]
861                         continue
862
863                 default:
864                         i := strings.IndexAny(p.text, "!;,<#\n \t")
865                         if i < 0 {
866                                 i = len(p.text)
867                         }
868                         t := p.text[:i]
869                         p.text = p.text[i:]
870                         p.lastWord = t
871                         return t
872                 }
873         }
874 }
875
876 // TestStdlibLowercase tests that all standard library package names are
877 // lowercase. See Issue 40065.
878 func TestStdlibLowercase(t *testing.T) {
879         if !testenv.HasSrc() {
880                 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
881         }
882
883         ctxt := Default
884         all, err := listStdPkgs(ctxt.GOROOT)
885         if err != nil {
886                 t.Fatal(err)
887         }
888
889         for _, pkgname := range all {
890                 if strings.ToLower(pkgname) != pkgname {
891                         t.Errorf("package %q should not use upper-case path", pkgname)
892                 }
893         }
894 }
895
896 // TestFindImports tests that findImports works.  See #43249.
897 func TestFindImports(t *testing.T) {
898         imports, err := findImports("go/build")
899         if err != nil {
900                 t.Fatal(err)
901         }
902         t.Logf("go/build imports %q", imports)
903         want := []string{"bytes", "os", "path/filepath", "strings"}
904 wantLoop:
905         for _, w := range want {
906                 for _, imp := range imports {
907                         if imp == w {
908                                 continue wantLoop
909                         }
910                 }
911                 t.Errorf("expected to find %q in import list", w)
912         }
913 }