1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package zip_sum_test
17
18 import (
19 "context"
20 "crypto/sha256"
21 "encoding/csv"
22 "encoding/hex"
23 "flag"
24 "fmt"
25 "internal/testenv"
26 "io"
27 "os"
28 "path/filepath"
29 "strings"
30 "testing"
31
32 "cmd/go/internal/cfg"
33 "cmd/go/internal/modfetch"
34
35 "golang.org/x/mod/module"
36 )
37
38 var (
39 updateTestData = flag.Bool("u", false, "when set, tests may update files in testdata instead of failing")
40 enableZipSum = flag.Bool("zipsum", false, "enable TestZipSums")
41 debugZipSum = flag.Bool("testwork", false, "when set, TestZipSums will preserve its test directory")
42 modCacheDir = flag.String("zipsumcache", "", "module cache to use instead of temp directory")
43 shardCount = flag.Int("zipsumshardcount", 1, "number of shards to divide TestZipSums into")
44 shardIndex = flag.Int("zipsumshard", 0, "index of TestZipSums shard to test (0 <= zipsumshard < zipsumshardcount)")
45 )
46
47 const zipSumsPath = "testdata/zip_sums.csv"
48
49 type zipSumTest struct {
50 m module.Version
51 wantSum, wantFileHash string
52 }
53
54 func TestZipSums(t *testing.T) {
55 if !*enableZipSum {
56
57
58 t.Skip("TestZipSum not enabled with -zipsum")
59 }
60 if *shardCount < 1 {
61 t.Fatal("-zipsumshardcount must be a positive integer")
62 }
63 if *shardIndex < 0 || *shardCount <= *shardIndex {
64 t.Fatal("-zipsumshard must be between 0 and -zipsumshardcount")
65 }
66
67 testenv.MustHaveGoBuild(t)
68 testenv.MustHaveExternalNetwork(t)
69 testenv.MustHaveExecPath(t, "bzr")
70 testenv.MustHaveExecPath(t, "git")
71
72
73
74 tests, err := readZipSumTests()
75 if err != nil {
76 t.Fatal(err)
77 }
78
79 if *modCacheDir != "" {
80 cfg.BuildContext.GOPATH = *modCacheDir
81 } else {
82 tmpDir, err := os.MkdirTemp("", "TestZipSums")
83 if err != nil {
84 t.Fatal(err)
85 }
86 if *debugZipSum {
87 fmt.Fprintf(os.Stderr, "TestZipSums: modCacheDir: %s\n", tmpDir)
88 } else {
89 defer os.RemoveAll(tmpDir)
90 }
91 cfg.BuildContext.GOPATH = tmpDir
92 }
93
94 cfg.GOPROXY = "direct"
95 cfg.GOSUMDB = "off"
96
97
98
99
100
101 if *shardCount > 1 {
102 r := *shardIndex
103 w := 0
104 for r < len(tests) {
105 tests[w] = tests[r]
106 w++
107 r += *shardCount
108 }
109 tests = tests[:w]
110 }
111
112
113
114 needUpdate := false
115 for i := range tests {
116 test := &tests[i]
117 name := fmt.Sprintf("%s@%s", strings.ReplaceAll(test.m.Path, "/", "_"), test.m.Version)
118 t.Run(name, func(t *testing.T) {
119 t.Parallel()
120 ctx := context.Background()
121
122 zipPath, err := modfetch.DownloadZip(ctx, test.m)
123 if err != nil {
124 if *updateTestData {
125 t.Logf("%s: could not download module: %s (will remove from testdata)", test.m, err)
126 test.m.Path = ""
127 needUpdate = true
128 } else {
129 t.Errorf("%s: could not download module: %s", test.m, err)
130 }
131 return
132 }
133
134 sum := modfetch.Sum(ctx, test.m)
135 if sum != test.wantSum {
136 if *updateTestData {
137 t.Logf("%s: updating content sum to %s", test.m, sum)
138 test.wantSum = sum
139 needUpdate = true
140 } else {
141 t.Errorf("%s: got content sum %s; want sum %s", test.m, sum, test.wantSum)
142 return
143 }
144 }
145
146 h := sha256.New()
147 f, err := os.Open(zipPath)
148 if err != nil {
149 t.Errorf("%s: %v", test.m, err)
150 }
151 defer f.Close()
152 if _, err := io.Copy(h, f); err != nil {
153 t.Errorf("%s: %v", test.m, err)
154 }
155 zipHash := hex.EncodeToString(h.Sum(nil))
156 if zipHash != test.wantFileHash {
157 if *updateTestData {
158 t.Logf("%s: updating zip file hash to %s", test.m, zipHash)
159 test.wantFileHash = zipHash
160 needUpdate = true
161 } else {
162 t.Errorf("%s: got zip file hash %s; want hash %s (but content sum matches)", test.m, zipHash, test.wantFileHash)
163 }
164 }
165 })
166 }
167
168 if needUpdate {
169
170 r, w := 0, 0
171 for r < len(tests) {
172 if tests[r].m.Path != "" {
173 tests[w] = tests[r]
174 w++
175 }
176 r++
177 }
178 tests = tests[:w]
179
180 if err := writeZipSumTests(tests); err != nil {
181 t.Error(err)
182 }
183 }
184 }
185
186 func readZipSumTests() ([]zipSumTest, error) {
187 f, err := os.Open(filepath.FromSlash(zipSumsPath))
188 if err != nil {
189 return nil, err
190 }
191 defer f.Close()
192 r := csv.NewReader(f)
193
194 var tests []zipSumTest
195 for {
196 line, err := r.Read()
197 if err == io.EOF {
198 break
199 } else if err != nil {
200 return nil, err
201 } else if len(line) != 4 {
202 return nil, fmt.Errorf("%s:%d: malformed line", f.Name(), len(tests)+1)
203 }
204 test := zipSumTest{m: module.Version{Path: line[0], Version: line[1]}, wantSum: line[2], wantFileHash: line[3]}
205 tests = append(tests, test)
206 }
207 return tests, nil
208 }
209
210 func writeZipSumTests(tests []zipSumTest) (err error) {
211 f, err := os.Create(filepath.FromSlash(zipSumsPath))
212 if err != nil {
213 return err
214 }
215 defer func() {
216 if cerr := f.Close(); err == nil && cerr != nil {
217 err = cerr
218 }
219 }()
220 w := csv.NewWriter(f)
221 line := make([]string, 0, 4)
222 for _, test := range tests {
223 line = append(line[:0], test.m.Path, test.m.Version, test.wantSum, test.wantFileHash)
224 if err := w.Write(line); err != nil {
225 return err
226 }
227 }
228 w.Flush()
229 return nil
230 }
231
View as plain text