Repository navigation
Expand file tree
/
Copy pathinstall_script_test.go
More file actions
334 lines (308 loc) · 12.4 KB
/
Copy pathinstall_script_test.go
File metadata and controls
334 lines (308 loc) · 12.4 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
const installStubBinaryContents = "fake-binary\n"
// installScriptAsset mirrors the OS/architecture mapping in install.sh for the
// platform the test runs on.
func installScriptAsset(t *testing.T, version, goos string) string {
t.Helper()
var osLabel string
switch goos {
case "darwin":
osLabel = "macOS"
case "linux":
osLabel = "linux"
default:
t.Skipf("install.sh does not support GOOS %q", goos)
}
var archLabel string
switch runtime.GOARCH {
case "amd64":
archLabel = "amd64"
case "arm64":
archLabel = "arm64"
default:
t.Skipf("install.sh does not support GOARCH %q", runtime.GOARCH)
}
return fmt.Sprintf("asc_%s_%s_%s", version, osLabel, archLabel)
}
// runInstallScript executes install.sh with a stubbed curl so no network
// access happens. checksumsMode controls what the stub serves for
// checksums.txt: valid, wrong, unlisted, or missing.
func runInstallScript(t *testing.T, checksumsMode string, extraEnv ...string) (output, installedBinary string, err error) {
t.Helper()
output, installedBinary, _, err = runInstallScriptPlatform(t, checksumsMode, runtime.GOOS, "13.0", "go 1.27.1", extraEnv...)
return output, installedBinary, err
}
func runInstallScriptPlatform(t *testing.T, checksumsMode, goos, macOSVersion, goMod string, extraEnv ...string) (output, installedBinary, stderr string, err error) {
t.Helper()
repoRoot, wdErr := os.Getwd()
if wdErr != nil {
t.Fatalf("getwd: %v", wdErr)
}
version := "0.0.1"
asset := installScriptAsset(t, version, goos)
sum := sha256.Sum256([]byte(installStubBinaryContents))
checksum := hex.EncodeToString(sum[:])
workDir := t.TempDir()
stubDir := filepath.Join(workDir, "stub-bin")
installDir := filepath.Join(workDir, "install-bin")
if mkErr := os.MkdirAll(stubDir, 0o755); mkErr != nil {
t.Fatalf("mkdir stub dir: %v", mkErr)
}
curlStub := `#!/usr/bin/env bash
set -euo pipefail
printf '%s\n' "$*" >> "${STUB_STATE_DIR}/curl-requests"
out=""
url=""
prev=""
for arg in "$@"; do
if [ "${prev}" = "-o" ]; then
out="${arg}"
fi
case "${arg}" in
http://*|https://*) url="${arg}" ;;
esac
prev="${arg}"
done
retry_target=""
case "${url}" in
*_checksums.txt) retry_target="checksums" ;;
*/releases/download/*) retry_target="binary" ;;
esac
if [ -n "${retry_target}" ] && [ "${STUB_FAIL_ONCE:-}" = "${retry_target}" ]; then
attempt_file="${STUB_STATE_DIR}/${retry_target}-attempted"
if [ ! -e "${attempt_file}" ]; then
touch "${attempt_file}"
exit 22
fi
fi
case "${url}" in
*/go.mod)
if [ "${STUB_GO_MOD}" = "missing" ]; then exit 22; fi
printf '%s\n' "${STUB_GO_MOD}"
;;
*/releases/latest)
printf '%s' "https://github.com/rorkai/App-Store-Connect-CLI/releases/tag/${STUB_VERSION}"
;;
*_checksums.txt)
case "${STUB_CHECKSUMS_MODE}" in
missing) exit 22 ;;
valid) printf '%s %s\n' "${STUB_SHA256}" "${STUB_ASSET}" > "${out}" ;;
unlisted) printf '%s %s\n' "${STUB_SHA256}" "${STUB_ASSET//./x}" > "${out}" ;;
wrong) printf '%s %s\n' "0000000000000000000000000000000000000000000000000000000000000000" "${STUB_ASSET}" > "${out}" ;;
*) echo "unknown STUB_CHECKSUMS_MODE" >&2; exit 1 ;;
esac
;;
*)
printf 'fake-binary\n' > "${out}"
;;
esac
`
if writeErr := os.WriteFile(filepath.Join(stubDir, "curl"), []byte(curlStub), 0o755); writeErr != nil {
t.Fatalf("write curl stub: %v", writeErr)
}
if writeErr := os.WriteFile(filepath.Join(stubDir, "sleep"), []byte("#!/usr/bin/env bash\nexit 0\n"), 0o755); writeErr != nil {
t.Fatalf("write sleep stub: %v", writeErr)
}
unameOS := "Linux"
if goos == "darwin" {
unameOS = "Darwin"
}
unameStub := fmt.Sprintf("#!/usr/bin/env bash\ncase \"$1\" in\n-s) echo %q ;;\n-m) echo %q ;;\nesac\n", unameOS, runtime.GOARCH)
if writeErr := os.WriteFile(filepath.Join(stubDir, "uname"), []byte(unameStub), 0o755); writeErr != nil {
t.Fatalf("write uname stub: %v", writeErr)
}
swVersStub := "#!/usr/bin/env bash\nprintf '%s\\n' \"$STUB_MACOS_VERSION\"\n"
if writeErr := os.WriteFile(filepath.Join(stubDir, "sw_vers"), []byte(swVersStub), 0o755); writeErr != nil {
t.Fatalf("write sw_vers stub: %v", writeErr)
}
cmd := exec.Command("bash", filepath.Join(repoRoot, "install.sh"))
cmd.Dir = workDir
cmd.Env = append(
os.Environ(),
"PATH="+stubDir+string(os.PathListSeparator)+os.Getenv("PATH"),
"INSTALL_DIR="+installDir,
"STUB_VERSION="+version,
"STUB_ASSET="+asset,
"STUB_SHA256="+checksum,
"STUB_CHECKSUMS_MODE="+checksumsMode,
"STUB_STATE_DIR="+workDir,
"STUB_MACOS_VERSION="+macOSVersion,
"STUB_GO_MOD="+goMod,
)
cmd.Env = append(cmd.Env, extraEnv...)
var stdout, stderrBuffer bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderrBuffer
runErr := cmd.Run()
return stdout.String() + stderrBuffer.String(), filepath.Join(installDir, "asc"), stderrBuffer.String(), runErr
}
func TestInstallScriptRetriesTransientBinaryDownloadFailure(t *testing.T) {
output, installedBinary, err := runInstallScript(t, "valid", "STUB_FAIL_ONCE=binary")
if err != nil {
t.Fatalf("expected transient binary download failure to recover: %v\n%s", err, output)
}
if !strings.Contains(output, "Download failed; retrying (2/3)") {
t.Fatalf("expected retry diagnostic, got:\n%s", output)
}
if _, statErr := os.Stat(installedBinary); statErr != nil {
t.Fatalf("expected binary installed at %s: %v\n%s", installedBinary, statErr, output)
}
}
func TestInstallScriptRetriesTransientChecksumDownloadFailure(t *testing.T) {
output, installedBinary, err := runInstallScript(t, "valid", "STUB_FAIL_ONCE=checksums")
if err != nil {
t.Fatalf("expected transient checksum download failure to recover: %v\n%s", err, output)
}
if !strings.Contains(output, "Download failed; retrying (2/3)") {
t.Fatalf("expected retry diagnostic, got:\n%s", output)
}
if _, statErr := os.Stat(installedBinary); statErr != nil {
t.Fatalf("expected binary installed at %s: %v\n%s", installedBinary, statErr, output)
}
}
func TestInstallScriptSyntaxIsValid(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("install.sh targets unix shells")
}
repoRoot, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %v", err)
}
cmd := exec.Command("bash", "-n", filepath.Join(repoRoot, "install.sh"))
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("bash -n install.sh failed: %v\n%s", err, output)
}
}
func TestInstallScriptVerifiesChecksumAndInstalls(t *testing.T) {
output, installedBinary, err := runInstallScript(t, "valid")
if err != nil {
t.Fatalf("install.sh failed: %v\n%s", err, output)
}
if !strings.Contains(output, "Checksum verified.") {
t.Fatalf("expected checksum verification confirmation, got:\n%s", output)
}
contents, readErr := os.ReadFile(installedBinary)
if readErr != nil {
t.Fatalf("expected installed binary at %s: %v\n%s", installedBinary, readErr, output)
}
if string(contents) != installStubBinaryContents {
t.Fatalf("installed binary contents mismatch: %q", contents)
}
}
func TestInstallScriptFailsClosedWhenChecksumsMissing(t *testing.T) {
output, installedBinary, err := runInstallScript(t, "missing")
if err == nil {
t.Fatalf("expected install.sh to fail without checksums.txt, got success:\n%s", output)
}
if !strings.Contains(output, "Refusing to install without SHA-256 checksum verification.") {
t.Fatalf("expected fail-closed error message, got:\n%s", output)
}
if !strings.Contains(output, "ASC_INSTALL_INSECURE=1") {
t.Fatalf("expected override hint in error output, got:\n%s", output)
}
if _, statErr := os.Stat(installedBinary); !os.IsNotExist(statErr) {
t.Fatalf("expected no binary installed, stat err=%v\n%s", statErr, output)
}
}
func TestInstallScriptFailsClosedWhenAssetUnlisted(t *testing.T) {
output, installedBinary, err := runInstallScript(t, "unlisted")
if err == nil {
t.Fatalf("expected install.sh to fail when asset is missing from checksums.txt, got success:\n%s", output)
}
if !strings.Contains(output, "Refusing to install without SHA-256 checksum verification.") {
t.Fatalf("expected fail-closed error message, got:\n%s", output)
}
if _, statErr := os.Stat(installedBinary); !os.IsNotExist(statErr) {
t.Fatalf("expected no binary installed, stat err=%v\n%s", statErr, output)
}
}
func TestInstallScriptInsecureOverrideAllowsMissingChecksums(t *testing.T) {
output, installedBinary, err := runInstallScript(t, "missing", "ASC_INSTALL_INSECURE=1")
if err != nil {
t.Fatalf("expected ASC_INSTALL_INSECURE=1 to allow install, got: %v\n%s", err, output)
}
if !strings.Contains(output, "installing WITHOUT checksum verification") {
t.Fatalf("expected loud insecure-install warning, got:\n%s", output)
}
if _, statErr := os.Stat(installedBinary); statErr != nil {
t.Fatalf("expected binary installed at %s: %v\n%s", installedBinary, statErr, output)
}
}
func TestInstallScriptChecksumMismatchFailsEvenWithInsecureOverride(t *testing.T) {
output, installedBinary, err := runInstallScript(t, "wrong", "ASC_INSTALL_INSECURE=1")
if err == nil {
t.Fatalf("expected install.sh to fail on checksum mismatch, got success:\n%s", output)
}
if !strings.Contains(output, "Checksum verification failed") {
t.Fatalf("expected checksum mismatch error, got:\n%s", output)
}
if _, statErr := os.Stat(installedBinary); !os.IsNotExist(statErr) {
t.Fatalf("expected no binary installed, stat err=%v\n%s", statErr, output)
}
}
func TestInstallScriptMacOSReleaseCompatibility(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("install.sh targets unix shells")
}
for _, tt := range []struct {
name, goos, macOSVersion, goMod, wantError string
wantMetadata bool
}{
{"monterey-compatible-release", "darwin", "12.7.6", "module example.com/asc\n\ngo 1.26.8\n", "", true},
{"monterey-new-release", "darwin", "12.7.6", "go 1.27.1", "requires macOS 13", true},
{"monterey-new-toolchain", "darwin", "12.7.6", "go 1.26.8\ntoolchain go1.27.1", "requires macOS 13", true},
{"monterey-future-go", "darwin", "12.7.6", "go 1.28.0", "requires macOS 13", true},
{"monterey-invalid-toolchain", "darwin", "12.7.6", "go 1.26.8\ntoolchain go1.banana", "Could not determine macOS compatibility", true},
{"monterey-missing-metadata", "darwin", "12.7.6", "missing", "Could not determine macOS compatibility", true},
{"monterey-invalid-metadata", "darwin", "12.7.6", "not a go.mod file", "Could not determine macOS compatibility", true},
{"monterey-default-go", "darwin", "12.7.6", "go default", "Could not determine macOS compatibility", true},
{"monterey-default-toolchain", "darwin", "12.7.6", "go 1.26.8\ntoolchain default", "", true},
{"monterey-invalid-version", "darwin", "12.7.6", "go banana", "Could not determine macOS compatibility", true},
{"ventura", "darwin", "13.0.1", "go 1.27.1", "", false},
{"newer-macos", "darwin", "26.0", "go 1.27.1", "", false},
{"linux", "linux", "", "go 1.27.1", "", false},
{"invalid-macos-version", "darwin", "unknown", "go 1.27.1", "Could not determine macOS version", false},
} {
t.Run(tt.name, func(t *testing.T) {
output, installedBinary, stderr, err := runInstallScriptPlatform(t, "valid", tt.goos, tt.macOSVersion, tt.goMod)
if tt.wantError == "" {
if err != nil {
t.Fatalf("install failed: %v\n%s", err, output)
}
if contents, readErr := os.ReadFile(installedBinary); readErr != nil || string(contents) != installStubBinaryContents {
t.Fatalf("expected installed binary, contents=%q err=%v\n%s", contents, readErr, output)
}
} else {
if err == nil || !strings.Contains(stderr, tt.wantError) {
t.Fatalf("expected %q, err=%v\n%s", tt.wantError, err, output)
}
if _, statErr := os.Stat(filepath.Dir(installedBinary)); !os.IsNotExist(statErr) {
t.Fatalf("unsupported release created install directory: %v", statErr)
}
}
requests, readErr := os.ReadFile(filepath.Join(filepath.Dir(filepath.Dir(installedBinary)), "curl-requests"))
if readErr != nil && !os.IsNotExist(readErr) {
t.Fatal(readErr)
}
metadataURL := "https://raw.githubusercontent.com/rorkai/App-Store-Connect-CLI/0.0.1/go.mod"
if got := strings.Contains(string(requests), metadataURL); got != tt.wantMetadata {
t.Fatalf("release metadata fetched=%v, want %v: %s", got, tt.wantMetadata, requests)
}
if tt.wantError != "" && strings.Contains(string(requests), "/releases/download/") {
t.Fatalf("downloaded unsupported release: %s", requests)
}
})
}
}