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