]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/build/deps_test.go
crypto/elliptic: use go:embed for the precomputed p256 table
[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, embed
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/internal/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         syscall
549         < os/exec/internal/fdtest;
550
551         FMT, container/heap, math/rand
552         < internal/trace;
553 `
554
555 // listStdPkgs returns the same list of packages as "go list std".
556 func listStdPkgs(goroot string) ([]string, error) {
557         // Based on cmd/go's matchPackages function.
558         var pkgs []string
559
560         src := filepath.Join(goroot, "src") + string(filepath.Separator)
561         walkFn := func(path string, d fs.DirEntry, err error) error {
562                 if err != nil || !d.IsDir() || path == src {
563                         return nil
564                 }
565
566                 base := filepath.Base(path)
567                 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
568                         return filepath.SkipDir
569                 }
570
571                 name := filepath.ToSlash(path[len(src):])
572                 if name == "builtin" || name == "cmd" {
573                         return filepath.SkipDir
574                 }
575
576                 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
577                 return nil
578         }
579         if err := filepath.WalkDir(src, walkFn); err != nil {
580                 return nil, err
581         }
582         return pkgs, nil
583 }
584
585 func TestDependencies(t *testing.T) {
586         if !testenv.HasSrc() {
587                 // Tests run in a limited file system and we do not
588                 // provide access to every source file.
589                 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
590         }
591
592         ctxt := Default
593         all, err := listStdPkgs(ctxt.GOROOT)
594         if err != nil {
595                 t.Fatal(err)
596         }
597         sort.Strings(all)
598
599         sawImport := map[string]map[string]bool{} // from package => to package => true
600         policy := depsPolicy(t)
601
602         for _, pkg := range all {
603                 imports, err := findImports(pkg)
604                 if err != nil {
605                         t.Error(err)
606                         continue
607                 }
608                 if sawImport[pkg] == nil {
609                         sawImport[pkg] = map[string]bool{}
610                 }
611                 ok := policy[pkg]
612                 var bad []string
613                 for _, imp := range imports {
614                         sawImport[pkg][imp] = true
615                         if !ok[imp] {
616                                 bad = append(bad, imp)
617                         }
618                 }
619                 if bad != nil {
620                         t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
621                 }
622         }
623
624         // depPath returns the path between the given from and to packages.
625         // It returns the empty string if there's no dependency path.
626         var depPath func(string, string) string
627         depPath = func(from, to string) string {
628                 if sawImport[from][to] {
629                         return from + " => " + to
630                 }
631                 for pkg := range sawImport[from] {
632                         if p := depPath(pkg, to); p != "" {
633                                 return from + " => " + p
634                         }
635                 }
636                 return ""
637         }
638 }
639
640 var buildIgnore = []byte("\n//go:build ignore")
641
642 func findImports(pkg string) ([]string, error) {
643         vpkg := pkg
644         if strings.HasPrefix(pkg, "golang.org") {
645                 vpkg = "vendor/" + pkg
646         }
647         dir := filepath.Join(Default.GOROOT, "src", vpkg)
648         files, err := os.ReadDir(dir)
649         if err != nil {
650                 return nil, err
651         }
652         var imports []string
653         var haveImport = map[string]bool{}
654         fset := token.NewFileSet()
655         for _, file := range files {
656                 name := file.Name()
657                 if name == "slice_go14.go" || name == "slice_go18.go" {
658                         // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
659                         continue
660                 }
661                 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
662                         continue
663                 }
664                 info := fileInfo{
665                         name: filepath.Join(dir, name),
666                         fset: fset,
667                 }
668                 f, err := os.Open(info.name)
669                 if err != nil {
670                         return nil, err
671                 }
672                 err = readGoInfo(f, &info)
673                 f.Close()
674                 if err != nil {
675                         return nil, fmt.Errorf("reading %v: %v", name, err)
676                 }
677                 if info.parsed.Name.Name == "main" {
678                         continue
679                 }
680                 if bytes.Contains(info.header, buildIgnore) {
681                         continue
682                 }
683                 for _, imp := range info.imports {
684                         path := imp.path
685                         if !haveImport[path] {
686                                 haveImport[path] = true
687                                 imports = append(imports, path)
688                         }
689                 }
690         }
691         sort.Strings(imports)
692         return imports, nil
693 }
694
695 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
696 func depsPolicy(t *testing.T) map[string]map[string]bool {
697         allowed := map[string]map[string]bool{"NONE": {}}
698         disallowed := [][2][]string{}
699
700         parseDepsRules(t, func(deps []string, op string, users []string) {
701                 if op == "!<" {
702                         disallowed = append(disallowed, [2][]string{deps, users})
703                         return
704                 }
705                 for _, u := range users {
706                         if allowed[u] != nil {
707                                 t.Errorf("multiple deps lists for %s", u)
708                         }
709                         allowed[u] = make(map[string]bool)
710                         for _, d := range deps {
711                                 if allowed[d] == nil {
712                                         t.Errorf("use of %s before its deps list", d)
713                                 }
714                                 allowed[u][d] = true
715                         }
716                 }
717         })
718
719         // Check for missing deps info.
720         for _, deps := range allowed {
721                 for d := range deps {
722                         if allowed[d] == nil {
723                                 t.Errorf("missing deps list for %s", d)
724                         }
725                 }
726         }
727
728         // Complete transitive allowed deps.
729         for k := range allowed {
730                 for i := range allowed {
731                         for j := range allowed {
732                                 if i != k && k != j && allowed[i][k] && allowed[k][j] {
733                                         if i == j {
734                                                 // Can only happen along with a "use of X before deps" error above,
735                                                 // but this error is more specific - it makes clear that reordering the
736                                                 // rules will not be enough to fix the problem.
737                                                 t.Errorf("deps policy cycle: %s < %s < %s", j, k, i)
738                                         }
739                                         allowed[i][j] = true
740                                 }
741                         }
742                 }
743         }
744
745         // Check negative assertions against completed allowed deps.
746         for _, bad := range disallowed {
747                 deps, users := bad[0], bad[1]
748                 for _, d := range deps {
749                         for _, u := range users {
750                                 if allowed[u][d] {
751                                         t.Errorf("deps policy incorrect: assertion failed: %s !< %s", d, u)
752                                 }
753                         }
754                 }
755         }
756
757         if t.Failed() {
758                 t.FailNow()
759         }
760
761         return allowed
762 }
763
764 // parseDepsRules parses depsRules, calling save(deps, op, users)
765 // for each deps < users or deps !< users rule
766 // (op is "<" or "!<").
767 func parseDepsRules(t *testing.T, save func(deps []string, op string, users []string)) {
768         p := &depsParser{t: t, lineno: 1, text: depsRules}
769
770         var prev []string
771         var op string
772         for {
773                 list, tok := p.nextList()
774                 if tok == "" {
775                         if prev == nil {
776                                 break
777                         }
778                         p.syntaxError("unexpected EOF")
779                 }
780                 if prev != nil {
781                         save(prev, op, list)
782                 }
783                 prev = list
784                 if tok == ";" {
785                         prev = nil
786                         op = ""
787                         continue
788                 }
789                 if tok != "<" && tok != "!<" {
790                         p.syntaxError("missing <")
791                 }
792                 op = tok
793         }
794 }
795
796 // A depsParser parses the depsRules syntax described above.
797 type depsParser struct {
798         t        *testing.T
799         lineno   int
800         lastWord string
801         text     string
802 }
803
804 // syntaxError reports a parsing error.
805 func (p *depsParser) syntaxError(msg string) {
806         p.t.Fatalf("deps:%d: syntax error: %s near %s", p.lineno, msg, p.lastWord)
807 }
808
809 // nextList parses and returns a comma-separated list of names.
810 func (p *depsParser) nextList() (list []string, token string) {
811         for {
812                 tok := p.nextToken()
813                 switch tok {
814                 case "":
815                         if len(list) == 0 {
816                                 return nil, ""
817                         }
818                         fallthrough
819                 case ",", "<", "!<", ";":
820                         p.syntaxError("bad list syntax")
821                 }
822                 list = append(list, tok)
823
824                 tok = p.nextToken()
825                 if tok != "," {
826                         return list, tok
827                 }
828         }
829 }
830
831 // nextToken returns the next token in the deps rules,
832 // one of ";" "," "<" "!<" or a name.
833 func (p *depsParser) nextToken() string {
834         for {
835                 if p.text == "" {
836                         return ""
837                 }
838                 switch p.text[0] {
839                 case ';', ',', '<':
840                         t := p.text[:1]
841                         p.text = p.text[1:]
842                         return t
843
844                 case '!':
845                         if len(p.text) < 2 || p.text[1] != '<' {
846                                 p.syntaxError("unexpected token !")
847                         }
848                         p.text = p.text[2:]
849                         return "!<"
850
851                 case '#':
852                         i := strings.Index(p.text, "\n")
853                         if i < 0 {
854                                 i = len(p.text)
855                         }
856                         p.text = p.text[i:]
857                         continue
858
859                 case '\n':
860                         p.lineno++
861                         fallthrough
862                 case ' ', '\t':
863                         p.text = p.text[1:]
864                         continue
865
866                 default:
867                         i := strings.IndexAny(p.text, "!;,<#\n \t")
868                         if i < 0 {
869                                 i = len(p.text)
870                         }
871                         t := p.text[:i]
872                         p.text = p.text[i:]
873                         p.lastWord = t
874                         return t
875                 }
876         }
877 }
878
879 // TestStdlibLowercase tests that all standard library package names are
880 // lowercase. See Issue 40065.
881 func TestStdlibLowercase(t *testing.T) {
882         if !testenv.HasSrc() {
883                 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
884         }
885
886         ctxt := Default
887         all, err := listStdPkgs(ctxt.GOROOT)
888         if err != nil {
889                 t.Fatal(err)
890         }
891
892         for _, pkgname := range all {
893                 if strings.ToLower(pkgname) != pkgname {
894                         t.Errorf("package %q should not use upper-case path", pkgname)
895                 }
896         }
897 }
898
899 // TestFindImports tests that findImports works.  See #43249.
900 func TestFindImports(t *testing.T) {
901         imports, err := findImports("go/build")
902         if err != nil {
903                 t.Fatal(err)
904         }
905         t.Logf("go/build imports %q", imports)
906         want := []string{"bytes", "os", "path/filepath", "strings"}
907 wantLoop:
908         for _, w := range want {
909                 for _, imp := range imports {
910                         if imp == w {
911                                 continue wantLoop
912                         }
913                 }
914                 t.Errorf("expected to find %q in import list", w)
915         }
916 }