1// Copyright 2019 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// +build ignore
6
7package main
8
9import (
10	"archive/tar"
11	"bytes"
12	"compress/gzip"
13	"crypto/sha256"
14	"flag"
15	"fmt"
16	"io"
17	"io/ioutil"
18	"net/http"
19	"os"
20	"os/exec"
21	"path/filepath"
22	"regexp"
23	"runtime"
24	"strings"
25	"sync"
26	"testing"
27	"time"
28
29	"google.golang.org/protobuf/internal/version"
30)
31
32var (
33	regenerate   = flag.Bool("regenerate", false, "regenerate files")
34	buildRelease = flag.Bool("buildRelease", false, "build release binaries")
35
36	protobufVersion = "3.15.3"
37	protobufSHA256  = "" // ignored if protobufVersion is a git hash
38
39	golangVersions = []string{"1.9.7", "1.10.8", "1.11.13", "1.12.17", "1.13.15", "1.14.15", "1.15.9", "1.16.1"}
40	golangLatest   = golangVersions[len(golangVersions)-1]
41
42	staticcheckVersion = "2020.1.4"
43	staticcheckSHA256s = map[string]string{
44		"darwin/amd64": "5706d101426c025e8f165309e0cb2932e54809eb035ff23ebe19df0f810699d8",
45		"linux/386":    "e4dbf94e940678ae7108f0d22c7c2992339bc10a8fb384e7e734b1531a429a1c",
46		"linux/amd64":  "09d2c2002236296de2c757df111fe3ae858b89f9e183f645ad01f8135c83c519",
47	}
48
49	// purgeTimeout determines the maximum age of unused sub-directories.
50	purgeTimeout = 30 * 24 * time.Hour // 1 month
51
52	// Variables initialized by mustInitDeps.
53	goPath       string
54	modulePath   string
55	protobufPath string
56)
57
58func Test(t *testing.T) {
59	mustInitDeps(t)
60	mustHandleFlags(t)
61
62	// Report dirt in the working tree quickly, rather than after
63	// going through all the presubmits.
64	//
65	// Fail the test late, so we can test uncommitted changes with -failfast.
66	gitDiff := mustRunCommand(t, "git", "diff", "HEAD")
67	if strings.TrimSpace(gitDiff) != "" {
68		fmt.Printf("WARNING: working tree contains uncommitted changes:\n%v\n", gitDiff)
69	}
70	gitUntracked := mustRunCommand(t, "git", "ls-files", "--others", "--exclude-standard")
71	if strings.TrimSpace(gitUntracked) != "" {
72		fmt.Printf("WARNING: working tree contains untracked files:\n%v\n", gitUntracked)
73	}
74
75	// Do the relatively fast checks up-front.
76	t.Run("GeneratedGoFiles", func(t *testing.T) {
77		diff := mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-types")
78		if strings.TrimSpace(diff) != "" {
79			t.Fatalf("stale generated files:\n%v", diff)
80		}
81		diff = mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-protos")
82		if strings.TrimSpace(diff) != "" {
83			t.Fatalf("stale generated files:\n%v", diff)
84		}
85	})
86	t.Run("FormattedGoFiles", func(t *testing.T) {
87		files := strings.Split(strings.TrimSpace(mustRunCommand(t, "git", "ls-files", "*.go")), "\n")
88		diff := mustRunCommand(t, append([]string{"gofmt", "-d"}, files...)...)
89		if strings.TrimSpace(diff) != "" {
90			t.Fatalf("unformatted source files:\n%v", diff)
91		}
92	})
93	t.Run("CopyrightHeaders", func(t *testing.T) {
94		files := strings.Split(strings.TrimSpace(mustRunCommand(t, "git", "ls-files", "*.go", "*.proto")), "\n")
95		mustHaveCopyrightHeader(t, files)
96	})
97
98	var wg sync.WaitGroup
99	sema := make(chan bool, (runtime.NumCPU()+1)/2)
100	for i := range golangVersions {
101		goVersion := golangVersions[i]
102		goLabel := "Go" + goVersion
103		runGo := func(label string, cmd command, args ...string) {
104			wg.Add(1)
105			sema <- true
106			go func() {
107				defer wg.Done()
108				defer func() { <-sema }()
109				t.Run(goLabel+"/"+label, func(t *testing.T) {
110					args[0] += goVersion
111					cmd.mustRun(t, args...)
112				})
113			}()
114		}
115
116		workDir := filepath.Join(goPath, "src", modulePath)
117		runGo("Normal", command{Dir: workDir}, "go", "test", "-race", "./...")
118		runGo("PureGo", command{Dir: workDir}, "go", "test", "-race", "-tags", "purego", "./...")
119		runGo("Reflect", command{Dir: workDir}, "go", "test", "-race", "-tags", "protoreflect", "./...")
120		if goVersion == golangLatest {
121			runGo("ProtoLegacy", command{Dir: workDir}, "go", "test", "-race", "-tags", "protolegacy", "./...")
122			runGo("ProtocGenGo", command{Dir: "cmd/protoc-gen-go/testdata"}, "go", "test")
123			runGo("Conformance", command{Dir: "internal/conformance"}, "go", "test", "-execute")
124
125			// Only run the 32-bit compatability tests for Linux;
126			// avoid Darwin since 10.15 dropped support i386 code execution.
127			if runtime.GOOS == "linux" {
128				runGo("Arch32Bit", command{Dir: workDir, Env: append(os.Environ(), "GOARCH=386")}, "go", "test", "./...")
129			}
130		}
131	}
132	wg.Wait()
133
134	t.Run("GoStaticCheck", func(t *testing.T) {
135		checks := []string{
136			"all",     // start with all checks enabled
137			"-SA1019", // disable deprecated usage check
138			"-S*",     // disable code simplication checks
139			"-ST*",    // disable coding style checks
140			"-U*",     // disable unused declaration checks
141		}
142		out := mustRunCommand(t, "staticcheck", "-checks="+strings.Join(checks, ","), "-fail=none", "./...")
143
144		// Filter out findings from certain paths.
145		var findings []string
146		for _, finding := range strings.Split(strings.TrimSpace(out), "\n") {
147			switch {
148			case strings.HasPrefix(finding, "internal/testprotos/legacy/"):
149			default:
150				findings = append(findings, finding)
151			}
152		}
153		if len(findings) > 0 {
154			t.Fatalf("staticcheck findings:\n%v", strings.Join(findings, "\n"))
155		}
156	})
157	t.Run("CommittedGitChanges", func(t *testing.T) {
158		if strings.TrimSpace(gitDiff) != "" {
159			t.Fatalf("uncommitted changes")
160		}
161	})
162	t.Run("TrackedGitFiles", func(t *testing.T) {
163		if strings.TrimSpace(gitUntracked) != "" {
164			t.Fatalf("untracked files")
165		}
166	})
167}
168
169func mustInitDeps(t *testing.T) {
170	check := func(err error) {
171		t.Helper()
172		if err != nil {
173			t.Fatal(err)
174		}
175	}
176
177	// Determine the directory to place the test directory.
178	repoRoot, err := os.Getwd()
179	check(err)
180	testDir := filepath.Join(repoRoot, ".cache")
181	check(os.MkdirAll(testDir, 0775))
182
183	// Delete the current directory if non-empty,
184	// which only occurs if a dependency failed to initialize properly.
185	var workingDir string
186	finishedDirs := map[string]bool{}
187	defer func() {
188		if workingDir != "" {
189			os.RemoveAll(workingDir) // best-effort
190		}
191	}()
192	startWork := func(name string) string {
193		workingDir = filepath.Join(testDir, name)
194		return workingDir
195	}
196	finishWork := func() {
197		finishedDirs[workingDir] = true
198		workingDir = ""
199	}
200
201	// Delete other sub-directories that are no longer relevant.
202	defer func() {
203		now := time.Now()
204		fis, _ := ioutil.ReadDir(testDir)
205		for _, fi := range fis {
206			dir := filepath.Join(testDir, fi.Name())
207			if finishedDirs[dir] {
208				os.Chtimes(dir, now, now) // best-effort
209				continue
210			}
211			if now.Sub(fi.ModTime()) < purgeTimeout {
212				continue
213			}
214			fmt.Printf("delete %v\n", fi.Name())
215			os.RemoveAll(dir) // best-effort
216		}
217	}()
218
219	// The bin directory contains symlinks to each tool by version.
220	// It is safe to delete this directory and run the test script from scratch.
221	binPath := startWork("bin")
222	check(os.RemoveAll(binPath))
223	check(os.Mkdir(binPath, 0775))
224	check(os.Setenv("PATH", binPath+":"+os.Getenv("PATH")))
225	registerBinary := func(name, path string) {
226		check(os.Symlink(path, filepath.Join(binPath, name)))
227	}
228	finishWork()
229
230	// Download and build the protobuf toolchain.
231	// We avoid downloading the pre-compiled binaries since they do not contain
232	// the conformance test runner.
233	protobufPath = startWork("protobuf-" + protobufVersion)
234	if _, err := os.Stat(protobufPath); err != nil {
235		fmt.Printf("download %v\n", filepath.Base(protobufPath))
236		if isCommit := strings.Trim(protobufVersion, "0123456789abcdef") == ""; isCommit {
237			command{Dir: testDir}.mustRun(t, "git", "clone", "https://github.com/protocolbuffers/protobuf", "protobuf-"+protobufVersion)
238			command{Dir: protobufPath}.mustRun(t, "git", "checkout", protobufVersion)
239		} else {
240			url := fmt.Sprintf("https://github.com/google/protobuf/releases/download/v%v/protobuf-all-%v.tar.gz", protobufVersion, protobufVersion)
241			downloadArchive(check, protobufPath, url, "protobuf-"+protobufVersion, protobufSHA256)
242		}
243
244		fmt.Printf("build %v\n", filepath.Base(protobufPath))
245		command{Dir: protobufPath}.mustRun(t, "./autogen.sh")
246		command{Dir: protobufPath}.mustRun(t, "./configure")
247		command{Dir: protobufPath}.mustRun(t, "make")
248		command{Dir: filepath.Join(protobufPath, "conformance")}.mustRun(t, "make")
249	}
250	check(os.Setenv("PROTOBUF_ROOT", protobufPath)) // for generate-protos
251	registerBinary("conform-test-runner", filepath.Join(protobufPath, "conformance", "conformance-test-runner"))
252	registerBinary("protoc", filepath.Join(protobufPath, "src", "protoc"))
253	finishWork()
254
255	// Download each Go toolchain version.
256	for _, v := range golangVersions {
257		goDir := startWork("go" + v)
258		if _, err := os.Stat(goDir); err != nil {
259			fmt.Printf("download %v\n", filepath.Base(goDir))
260			url := fmt.Sprintf("https://dl.google.com/go/go%v.%v-%v.tar.gz", v, runtime.GOOS, runtime.GOARCH)
261			downloadArchive(check, goDir, url, "go", "") // skip SHA256 check as we fetch over https from a trusted domain
262		}
263		registerBinary("go"+v, filepath.Join(goDir, "bin", "go"))
264		finishWork()
265	}
266	registerBinary("go", filepath.Join(testDir, "go"+golangLatest, "bin", "go"))
267	registerBinary("gofmt", filepath.Join(testDir, "go"+golangLatest, "bin", "gofmt"))
268
269	// Download the staticcheck tool.
270	checkDir := startWork("staticcheck-" + staticcheckVersion)
271	if _, err := os.Stat(checkDir); err != nil {
272		fmt.Printf("download %v\n", filepath.Base(checkDir))
273		url := fmt.Sprintf("https://github.com/dominikh/go-tools/releases/download/%v/staticcheck_%v_%v.tar.gz", staticcheckVersion, runtime.GOOS, runtime.GOARCH)
274		downloadArchive(check, checkDir, url, "staticcheck", staticcheckSHA256s[runtime.GOOS+"/"+runtime.GOARCH])
275	}
276	registerBinary("staticcheck", filepath.Join(checkDir, "staticcheck"))
277	finishWork()
278
279	// GitHub actions sets GOROOT, which confuses invocations of the Go toolchain.
280	// Explicitly clear GOROOT, so each toolchain uses their default GOROOT.
281	check(os.Unsetenv("GOROOT"))
282
283	// Set a cache directory outside the test directory.
284	check(os.Setenv("GOCACHE", filepath.Join(repoRoot, ".gocache")))
285
286	// Setup GOPATH for pre-module support (i.e., go1.10 and earlier).
287	goPath = startWork("gopath")
288	modulePath = strings.TrimSpace(command{Dir: testDir}.mustRun(t, "go", "list", "-m", "-f", "{{.Path}}"))
289	check(os.RemoveAll(filepath.Join(goPath, "src")))
290	check(os.MkdirAll(filepath.Join(goPath, "src", filepath.Dir(modulePath)), 0775))
291	check(os.Symlink(repoRoot, filepath.Join(goPath, "src", modulePath)))
292	command{Dir: repoRoot}.mustRun(t, "go", "mod", "tidy")
293	command{Dir: repoRoot}.mustRun(t, "go", "mod", "vendor")
294	check(os.Setenv("GOPATH", goPath))
295	finishWork()
296}
297
298func downloadFile(check func(error), dstPath, srcURL string) {
299	resp, err := http.Get(srcURL)
300	check(err)
301	defer resp.Body.Close()
302
303	check(os.MkdirAll(filepath.Dir(dstPath), 0775))
304	f, err := os.Create(dstPath)
305	check(err)
306
307	_, err = io.Copy(f, resp.Body)
308	check(err)
309}
310
311func downloadArchive(check func(error), dstPath, srcURL, skipPrefix, wantSHA256 string) {
312	check(os.RemoveAll(dstPath))
313
314	resp, err := http.Get(srcURL)
315	check(err)
316	defer resp.Body.Close()
317
318	var r io.Reader = resp.Body
319	if wantSHA256 != "" {
320		b, err := ioutil.ReadAll(resp.Body)
321		check(err)
322		r = bytes.NewReader(b)
323
324		if gotSHA256 := fmt.Sprintf("%x", sha256.Sum256(b)); gotSHA256 != wantSHA256 {
325			check(fmt.Errorf("checksum validation error:\ngot  %v\nwant %v", gotSHA256, wantSHA256))
326		}
327	}
328
329	zr, err := gzip.NewReader(r)
330	check(err)
331
332	tr := tar.NewReader(zr)
333	for {
334		h, err := tr.Next()
335		if err == io.EOF {
336			return
337		}
338		check(err)
339
340		// Skip directories or files outside the prefix directory.
341		if len(skipPrefix) > 0 {
342			if !strings.HasPrefix(h.Name, skipPrefix) {
343				continue
344			}
345			if len(h.Name) > len(skipPrefix) && h.Name[len(skipPrefix)] != '/' {
346				continue
347			}
348		}
349
350		path := strings.TrimPrefix(strings.TrimPrefix(h.Name, skipPrefix), "/")
351		path = filepath.Join(dstPath, filepath.FromSlash(path))
352		mode := os.FileMode(h.Mode & 0777)
353		switch h.Typeflag {
354		case tar.TypeReg:
355			b, err := ioutil.ReadAll(tr)
356			check(err)
357			check(ioutil.WriteFile(path, b, mode))
358		case tar.TypeDir:
359			check(os.Mkdir(path, mode))
360		}
361	}
362}
363
364func mustHandleFlags(t *testing.T) {
365	if *regenerate {
366		t.Run("Generate", func(t *testing.T) {
367			fmt.Print(mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-types", "-execute"))
368			fmt.Print(mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-protos", "-execute"))
369			files := strings.Split(strings.TrimSpace(mustRunCommand(t, "git", "ls-files", "*.go")), "\n")
370			mustRunCommand(t, append([]string{"gofmt", "-w"}, files...)...)
371		})
372	}
373	if *buildRelease {
374		t.Run("BuildRelease", func(t *testing.T) {
375			v := version.String()
376			for _, goos := range []string{"linux", "darwin", "windows"} {
377				for _, goarch := range []string{"386", "amd64"} {
378					// Avoid Darwin since 10.15 dropped support for i386.
379					if goos == "darwin" && goarch == "386" {
380						continue
381					}
382
383					binPath := filepath.Join("bin", fmt.Sprintf("protoc-gen-go.%v.%v.%v", v, goos, goarch))
384
385					// Build the binary.
386					cmd := command{Env: append(os.Environ(), "GOOS="+goos, "GOARCH="+goarch)}
387					cmd.mustRun(t, "go", "build", "-trimpath", "-ldflags", "-s -w -buildid=", "-o", binPath, "./cmd/protoc-gen-go")
388
389					// Archive and compress the binary.
390					in, err := ioutil.ReadFile(binPath)
391					if err != nil {
392						t.Fatal(err)
393					}
394					out := new(bytes.Buffer)
395					gz, _ := gzip.NewWriterLevel(out, gzip.BestCompression)
396					gz.Comment = fmt.Sprintf("protoc-gen-go VERSION=%v GOOS=%v GOARCH=%v", v, goos, goarch)
397					tw := tar.NewWriter(gz)
398					tw.WriteHeader(&tar.Header{
399						Name: "protoc-gen-go",
400						Mode: int64(0775),
401						Size: int64(len(in)),
402					})
403					tw.Write(in)
404					tw.Close()
405					gz.Close()
406					if err := ioutil.WriteFile(binPath+".tar.gz", out.Bytes(), 0664); err != nil {
407						t.Fatal(err)
408					}
409				}
410			}
411		})
412	}
413	if *regenerate || *buildRelease {
414		t.SkipNow()
415	}
416}
417
418var copyrightRegex = []*regexp.Regexp{
419	regexp.MustCompile(`^// Copyright \d\d\d\d The Go Authors\. All rights reserved.
420// Use of this source code is governed by a BSD-style
421// license that can be found in the LICENSE file\.
422`),
423	// Generated .pb.go files from main protobuf repo.
424	regexp.MustCompile(`^// Protocol Buffers - Google's data interchange format
425// Copyright \d\d\d\d Google Inc\.  All rights reserved\.
426`),
427}
428
429func mustHaveCopyrightHeader(t *testing.T, files []string) {
430	var bad []string
431File:
432	for _, file := range files {
433		b, err := ioutil.ReadFile(file)
434		if err != nil {
435			t.Fatal(err)
436		}
437		for _, re := range copyrightRegex {
438			if loc := re.FindIndex(b); loc != nil && loc[0] == 0 {
439				continue File
440			}
441		}
442		bad = append(bad, file)
443	}
444	if len(bad) > 0 {
445		t.Fatalf("files with missing/bad copyright headers:\n  %v", strings.Join(bad, "\n  "))
446	}
447}
448
449type command struct {
450	Dir string
451	Env []string
452}
453
454func (c command) mustRun(t *testing.T, args ...string) string {
455	t.Helper()
456	stdout := new(bytes.Buffer)
457	stderr := new(bytes.Buffer)
458	cmd := exec.Command(args[0], args[1:]...)
459	cmd.Dir = "."
460	if c.Dir != "" {
461		cmd.Dir = c.Dir
462	}
463	cmd.Env = os.Environ()
464	if c.Env != nil {
465		cmd.Env = c.Env
466	}
467	cmd.Env = append(cmd.Env, "PWD="+cmd.Dir)
468	cmd.Stdout = stdout
469	cmd.Stderr = stderr
470	if err := cmd.Run(); err != nil {
471		t.Fatalf("executing (%v): %v\n%s%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
472	}
473	return stdout.String()
474}
475
476func mustRunCommand(t *testing.T, args ...string) string {
477	t.Helper()
478	return command{}.mustRun(t, args...)
479}
480