Debian Codegraph @

src/compress/gzip/issue14937_test.go

raw
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
5 package gzip
6
7 import (
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.
26 func 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 testenv.MustHaveSource(t)
34
35 goroot, err := filepath.EvalSymlinks(runtime.GOROOT())
36 if err != nil {
37 t.Fatal("error evaluating GOROOT: ", err)
38 }
39 var files []string
40 err = filepath.WalkDir(goroot, func(path string, info fs.DirEntry, err error) error {
41 if err != nil {
42 return err
43 }
44 if !info.IsDir() && strings.HasSuffix(path, ".gz") {
45 files = append(files, path)
46 }
47 return nil
48 })
49 if err != nil {
50 if os.IsNotExist(err) {
51 t.Skipf("skipping: GOROOT directory not found: %s", runtime.GOROOT())
52 }
53 t.Fatal("error collecting list of .gz files in GOROOT: ", err)
54 }
55 if len(files) == 0 {
56 t.Fatal("expected to find some .gz files under GOROOT")
57 }
58 for _, path := range files {
59 checkZeroMTime(t, path)
60 }
61 }
62
63 func checkZeroMTime(t *testing.T, path string) {
64 f, err := os.Open(path)
65 if err != nil {
66 t.Error(err)
67 return
68 }
69 defer f.Close()
70 gz, err := NewReader(f)
71 if err != nil {
72 t.Errorf("cannot read gzip file %s: %s", path, err)
73 return
74 }
75 defer gz.Close()
76 if !gz.ModTime.IsZero() {
77 t.Errorf("gzip file %s has non-zero mtime (%s)", path, gz.ModTime)
78 }
79 }