1package gzip
2
3import (
4	"internal/testenv"
5	"os"
6	"path/filepath"
7	"runtime"
8	"strings"
9	"testing"
10)
11
12// TestGZIPFilesHaveZeroMTimes checks that every .gz file in the tree
13// has a zero MTIME. This is a requirement for the Debian maintainers
14// to be able to have deterministic packages.
15//
16// See https://golang.org/issue/14937.
17func TestGZIPFilesHaveZeroMTimes(t *testing.T) {
18	// To avoid spurious false positives due to untracked GZIP files that
19	// may be in the user's GOROOT (Issue 18604), we only run this test on
20	// the builders, which should have a clean checkout of the tree.
21	if testenv.Builder() == "" {
22		t.Skip("skipping test on non-builder")
23	}
24	if !testenv.HasSrc() {
25		t.Skip("skipping; no GOROOT available")
26	}
27
28	goroot, err := filepath.EvalSymlinks(runtime.GOROOT())
29	if err != nil {
30		t.Fatal("error evaluating GOROOT: ", err)
31	}
32	var files []string
33	err = filepath.Walk(goroot, func(path string, info os.FileInfo, err error) error {
34		if err != nil {
35			return err
36		}
37		if !info.IsDir() && strings.HasSuffix(path, ".gz") {
38			files = append(files, path)
39		}
40		return nil
41	})
42	if err != nil {
43		if os.IsNotExist(err) {
44			t.Skipf("skipping: GOROOT directory not found: %s", runtime.GOROOT())
45		}
46		t.Fatal("error collecting list of .gz files in GOROOT: ", err)
47	}
48	if len(files) == 0 {
49		t.Fatal("expected to find some .gz files under GOROOT")
50	}
51	for _, path := range files {
52		checkZeroMTime(t, path)
53	}
54}
55
56func checkZeroMTime(t *testing.T, path string) {
57	f, err := os.Open(path)
58	if err != nil {
59		t.Error(err)
60		return
61	}
62	defer f.Close()
63	gz, err := NewReader(f)
64	if err != nil {
65		t.Errorf("cannot read gzip file %s: %s", path, err)
66		return
67	}
68	defer gz.Close()
69	if !gz.ModTime.IsZero() {
70		t.Errorf("gzip file %s has non-zero mtime (%s)", path, gz.ModTime)
71	}
72}
73