-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_test.go
More file actions
278 lines (253 loc) · 7.64 KB
/
Copy pathupdate_test.go
File metadata and controls
278 lines (253 loc) · 7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
import (
"archive/tar"
"archive/zip"
"compress/gzip"
"crypto/sha256"
"encoding/hex"
"os"
"path/filepath"
"testing"
)
func TestParseSemver(t *testing.T) {
cases := []struct {
in string
want [3]int
ok bool
}{
{"v1.2.3", [3]int{1, 2, 3}, true},
{"1.2.3", [3]int{1, 2, 3}, true},
{"v1.2", [3]int{1, 2, 0}, true},
{"v2", [3]int{2, 0, 0}, true},
{"v1.2.3-rc1", [3]int{1, 2, 3}, true},
{"v1.2.3+build5", [3]int{1, 2, 3}, true},
{"dev", [3]int{}, false},
{"", [3]int{}, false},
{"v1.2.3.4", [3]int{}, false},
{"vx.y.z", [3]int{}, false},
}
for _, c := range cases {
got, ok := parseSemver(c.in)
if ok != c.ok || (ok && got != c.want) {
t.Errorf("parseSemver(%q) = %v,%v; want %v,%v", c.in, got, ok, c.want, c.ok)
}
}
}
func TestIsNewer(t *testing.T) {
cases := []struct {
current, candidate string
want bool
}{
{"v1.0.0", "v1.0.1", true},
{"v1.0.0", "v1.1.0", true},
{"v1.0.0", "v2.0.0", true},
{"v1.2.0", "v1.2.0", false},
{"v1.2.0", "v1.1.9", false},
{"v2.0.0", "v1.9.9", false},
{"dev", "v1.0.0", true}, // dev always upgrades to a real release
{"dev", "dev", false}, // identical strings never "newer"
{"v1.0.0", "garbage", false},
}
for _, c := range cases {
if got := isNewer(c.current, c.candidate); got != c.want {
t.Errorf("isNewer(%q,%q) = %v; want %v", c.current, c.candidate, got, c.want)
}
}
}
func TestAssetNameFor(t *testing.T) {
if name, zipped := assetNameFor("darwin", "arm64"); name != "hammer-darwin-arm64.tar.gz" || zipped {
t.Errorf("darwin/arm64 = %q,%v", name, zipped)
}
if name, zipped := assetNameFor("linux", "amd64"); name != "hammer-linux-amd64.tar.gz" || zipped {
t.Errorf("linux/amd64 = %q,%v", name, zipped)
}
if name, zipped := assetNameFor("windows", "amd64"); name != "hammer-windows-amd64.zip" || !zipped {
t.Errorf("windows/amd64 = %q,%v", name, zipped)
}
}
func TestCandidateURLs(t *testing.T) {
const gh = "https://github.com/o/r/releases/download/v1/hammer-linux-amd64.tar.gz"
if got := candidateURLs(gh, "github"); len(got) != 1 || got[0] != gh {
t.Errorf("github mode = %v", got)
}
auto := candidateURLs(gh, "auto")
if len(auto) != len(mirrorProxies)+1 || auto[0] != gh {
t.Fatalf("auto mode = %v", auto)
}
if auto[1] != mirrorProxies[0]+gh {
t.Errorf("auto mode first mirror = %q", auto[1])
}
ghproxy := candidateURLs(gh, "ghproxy")
if len(ghproxy) != len(mirrorProxies) || ghproxy[0] != mirrorProxies[0]+gh {
t.Errorf("ghproxy mode = %v", ghproxy)
}
custom := candidateURLs(gh, "https://mirror.example.com/")
if len(custom) != 2 || custom[0] != gh || custom[1] != "https://mirror.example.com/"+gh {
t.Errorf("custom mode = %v", custom)
}
// Trailing slash should be normalized, not doubled.
custom2 := candidateURLs(gh, "https://mirror.example.com")
if custom2[1] != "https://mirror.example.com/"+gh {
t.Errorf("custom mode (no slash) = %q", custom2[1])
}
}
func TestVerifyChecksum(t *testing.T) {
dir := t.TempDir()
archive := filepath.Join(dir, "hammer-linux-amd64.tar.gz")
if err := os.WriteFile(archive, []byte("payload"), 0o644); err != nil {
t.Fatal(err)
}
sum := sha256.Sum256([]byte("payload"))
hexSum := hex.EncodeToString(sum[:])
sums := filepath.Join(dir, "SHA256SUMS")
body := hexSum + " hammer-linux-amd64.tar.gz\n" +
"0000000000000000000000000000000000000000000000000000000000000000 other.tar.gz\n"
if err := os.WriteFile(sums, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
if err := verifyChecksum(archive, "hammer-linux-amd64.tar.gz", sums); err != nil {
t.Errorf("valid checksum rejected: %v", err)
}
if err := verifyChecksum(archive, "missing.tar.gz", sums); err == nil {
t.Error("expected error for asset missing from SHA256SUMS")
}
// Corrupt the archive: checksum must now mismatch.
if err := os.WriteFile(archive, []byte("tampered"), 0o644); err != nil {
t.Fatal(err)
}
if err := verifyChecksum(archive, "hammer-linux-amd64.tar.gz", sums); err == nil {
t.Error("expected checksum mismatch for tampered archive")
}
}
func TestExtractBinaryTarGz(t *testing.T) {
dir := t.TempDir()
archive := filepath.Join(dir, "hammer.tar.gz")
writeTarGz(t, archive, "hammer", []byte("#!binary\n"))
out, err := extractBinary(archive, false)
if err != nil {
t.Fatalf("extractBinary: %v", err)
}
defer os.Remove(out)
got, err := os.ReadFile(out)
if err != nil {
t.Fatal(err)
}
if string(got) != "#!binary\n" {
t.Errorf("extracted content = %q", got)
}
if fi, _ := os.Stat(out); fi.Mode().Perm()&0o111 == 0 {
t.Error("extracted binary is not executable")
}
}
func TestExtractBinaryTarGzMissing(t *testing.T) {
dir := t.TempDir()
archive := filepath.Join(dir, "empty.tar.gz")
writeTarGz(t, archive, "README.md", []byte("not a binary"))
if _, err := extractBinary(archive, false); err == nil {
t.Error("expected error when archive has no hammer binary")
}
}
func TestExtractBinaryZip(t *testing.T) {
dir := t.TempDir()
archive := filepath.Join(dir, "hammer.zip")
writeZip(t, archive, "hammer.exe", []byte("MZbinary"))
out, err := extractBinary(archive, true)
if err != nil {
t.Fatalf("extractBinary zip: %v", err)
}
defer os.Remove(out)
got, err := os.ReadFile(out)
if err != nil {
t.Fatal(err)
}
if string(got) != "MZbinary" {
t.Errorf("extracted content = %q", got)
}
}
func TestReplaceExecutable(t *testing.T) {
dir := t.TempDir()
exe := filepath.Join(dir, "hammer")
if err := os.WriteFile(exe, []byte("old"), 0o755); err != nil {
t.Fatal(err)
}
// Stage the replacement in a separate temp dir to exercise the
// cross-filesystem copy fallback path in moveFile.
newBin := filepath.Join(t.TempDir(), "newbin")
if err := os.WriteFile(newBin, []byte("new"), 0o755); err != nil {
t.Fatal(err)
}
if err := replaceExecutable(exe, newBin); err != nil {
t.Fatalf("replaceExecutable: %v", err)
}
got, err := os.ReadFile(exe)
if err != nil {
t.Fatal(err)
}
if string(got) != "new" {
t.Errorf("after replace, exe = %q; want %q", got, "new")
}
if fi, _ := os.Stat(exe); fi.Mode().Perm()&0o111 == 0 {
t.Error("replaced binary lost its executable bit")
}
}
func TestMirrorHost(t *testing.T) {
cases := map[string]string{
"https://ghfast.top/https://github.com/x": "ghfast.top",
"http://gh-proxy.com/path": "gh-proxy.com",
"ghproxy.net": "ghproxy.net",
}
for in, want := range cases {
if got := mirrorHost(in); got != want {
t.Errorf("mirrorHost(%q) = %q; want %q", in, got, want)
}
}
}
func TestIsHammerBinary(t *testing.T) {
for _, name := range []string{"hammer", "hammer.exe", "hammer-linux-amd64"} {
if !isHammerBinary(name) {
t.Errorf("isHammerBinary(%q) = false; want true", name)
}
}
for _, name := range []string{"README.md", "install.sh", "SHA256SUMS"} {
if isHammerBinary(name) {
t.Errorf("isHammerBinary(%q) = true; want false", name)
}
}
}
// --- test helpers ---------------------------------------------------------
func writeTarGz(t *testing.T, path, name string, content []byte) {
t.Helper()
f, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
gz := gzip.NewWriter(f)
defer gz.Close()
tw := tar.NewWriter(gz)
defer tw.Close()
hdr := &tar.Header{Name: name, Mode: 0o755, Size: int64(len(content)), Typeflag: tar.TypeReg}
if err := tw.WriteHeader(hdr); err != nil {
t.Fatal(err)
}
if _, err := tw.Write(content); err != nil {
t.Fatal(err)
}
}
func writeZip(t *testing.T, path, name string, content []byte) {
t.Helper()
f, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
zw := zip.NewWriter(f)
defer zw.Close()
w, err := zw.Create(name)
if err != nil {
t.Fatal(err)
}
if _, err := w.Write(content); err != nil {
t.Fatal(err)
}
}