| 1 | package release |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestOnboardingInstallShell(t *testing.T) { |
| 12 | script, err := os.ReadFile("../../packaging/install/install-v1.sh") |
| 13 | if err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | for _, platform := range []string{"Linux/x86_64", "Darwin/x86_64", "Linux/aarch64", "truncated"} { |
| 17 | t.Run(platform, func(t *testing.T) { |
| 18 | root := t.TempDir() |
| 19 | parts := strings.Split(platform+"/unused", "/") |
| 20 | write := func(name, body string) { |
| 21 | t.Helper() |
| 22 | if err := os.WriteFile(filepath.Join(root, name), []byte(body), 0700); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | } |
| 26 | write("uname", "#!/bin/sh\necho uname >> \"$TMPDIR/started\"\ncase \"$1\" in -s) echo "+parts[0]+";; -m) echo "+parts[1]+";; esac\n") |
| 27 | write("curl", `#!/bin/sh |
| 28 | echo download >> "$TMPDIR/downloads" |
| 29 | while [ "$1" != "-o" ]; do shift; done |
| 30 | case "$2" in |
| 31 | */manifest.json) printf '%s' '{"version":"fixture-16","target":"linux/amd64","runner_sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","targets":{"linux/amd64":{"runner_sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","tool_proxy_sha256":"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"}},"release":{"schema":3,"runner_target":"linux/amd64","binaries":[{"name":"equipo-tool-proxy","sha256":"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"}]}}' > "$2" ;; |
| 32 | *) printf '#!/bin/sh\nexit 99\n' > "$2" ;; |
| 33 | esac |
| 34 | `) |
| 35 | write("sha256sum", `#!/bin/sh |
| 36 | cat > "$TMPDIR/checksum-input" |
| 37 | echo checked > "$TMPDIR/checked" |
| 38 | exit 1 |
| 39 | `) |
| 40 | input := string(script) |
| 41 | if platform == "truncated" { |
| 42 | input = input[:strings.Index(input, "curl --fail")] |
| 43 | } |
| 44 | cmd := exec.Command("sh", "-s", "--", "https://equipo.example", "fixture-16", "team") |
| 45 | cmd.Stdin = strings.NewReader(input) |
| 46 | cmd.Env = append(os.Environ(), "PATH="+root+":"+os.Getenv("PATH"), "TMPDIR="+root) |
| 47 | out, err := cmd.CombinedOutput() |
| 48 | if err == nil { |
| 49 | t.Fatalf("expected refusal or fake download failure: %s", out) |
| 50 | } |
| 51 | b, _ := os.ReadFile(filepath.Join(root, "downloads")) |
| 52 | if platform == "Linux/x86_64" { |
| 53 | if string(b) != "download\ndownload\n" { |
| 54 | t.Fatalf("supported host did not reach download: %s %s", b, out) |
| 55 | } |
| 56 | if _, e := os.Stat(filepath.Join(root, "checked")); e != nil { |
| 57 | t.Fatal("supported host skipped checksum") |
| 58 | } |
| 59 | } else if len(b) != 0 { |
| 60 | t.Fatalf("refused host downloaded: %s", b) |
| 61 | } |
| 62 | if platform == "truncated" { |
| 63 | if _, e := os.Stat(filepath.Join(root, "started")); !os.IsNotExist(e) { |
| 64 | t.Fatal("truncated stream executed commands") |
| 65 | } |
| 66 | } |
| 67 | entries, _ := os.ReadDir(root) |
| 68 | for _, e := range entries { |
| 69 | if strings.HasPrefix(e.Name(), "equipo-bootstrap-") { |
| 70 | t.Fatal("scratch leaked") |
| 71 | } |
| 72 | } |
| 73 | }) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // The same bootstrap on a Mac with Apple silicon: refuses root before |
| 78 | // downloading, reads the darwin target from the manifest with plutil, |
| 79 | // verifies the runner's digest with shasum and hands over to the |
| 80 | // runner's own installer (no prompts: the label is the Mac's name). |
| 81 | // macShell runs the bootstrap on a fake Apple silicon Mac whose tools |
| 82 | // write what they saw under log. home is the user's own home as the user |
| 83 | // directory (dscl) says; usertmp is what getconf says is its temporary |
| 84 | // directory ("" when getconf has none). |
| 85 | type macShell struct { |
| 86 | root, log, home, usertmp string |
| 87 | uid, version, digest string |
| 88 | noMktempDefault bool |
| 89 | } |
| 90 | |
| 91 | func newMacShell(t *testing.T) *macShell { |
| 92 | t.Helper() |
| 93 | root := t.TempDir() |
| 94 | m := &macShell{root: root, log: filepath.Join(root, "log"), home: filepath.Join(root, "Users", "paulequipo"), usertmp: filepath.Join(root, "var", "T"), uid: "502", version: "fixture-16", digest: strings.Repeat("c", 64)} |
| 95 | for _, d := range []string{m.log, m.home, m.usertmp, filepath.Join(root, "bin")} { |
| 96 | if err := os.MkdirAll(d, 0700); err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | } |
| 100 | return m |
| 101 | } |
| 102 | |
| 103 | func (m *macShell) run(t *testing.T, env ...string) ([]byte, error) { |
| 104 | t.Helper() |
| 105 | script, err := os.ReadFile("../../packaging/install/install-v1.sh") |
| 106 | if err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | bin := filepath.Join(m.root, "bin") |
| 110 | write := func(name, body string) { |
| 111 | t.Helper() |
| 112 | if err := os.WriteFile(filepath.Join(bin, name), []byte(body), 0700); err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | } |
| 116 | write("uname", "#!/bin/sh\ncase \"$1\" in -s) echo Darwin;; -m) echo arm64;; esac\n") |
| 117 | write("id", "#!/bin/sh\ncase \"$1\" in -un) echo paulequipo;; *) echo "+m.uid+";; esac\n") |
| 118 | write("dscl", "#!/bin/sh\n[ \"$3\" = /Users/paulequipo ] && echo 'NFSHomeDirectory: "+m.home+"'\n") |
| 119 | if m.usertmp != "" { |
| 120 | write("getconf", "#!/bin/sh\n[ \"$1\" = DARWIN_USER_TEMP_DIR ] && echo "+m.usertmp+"\n") |
| 121 | } else { |
| 122 | write("getconf", "#!/bin/sh\nexit 1\n") |
| 123 | } |
| 124 | if m.noMktempDefault { |
| 125 | write("mktemp", "#!/bin/sh\n[ $# -eq 1 ] && [ \"$1\" = -d ] && exit 1\nexec /usr/bin/mktemp \"$@\"\n") |
| 126 | } |
| 127 | write("plutil", "#!/bin/sh\ncase \"$2\" in version) echo "+m.version+";; 'targets.darwin/arm64.runner_sha256'|targets.darwin/arm64.runner_sha256) echo "+m.digest+";; *) exit 1;; esac\n") |
| 128 | write("curl", `#!/bin/sh |
| 129 | echo download >> "$FAKE_LOG/downloads" |
| 130 | for a in "$@"; do case "$a" in */darwin-arm64/equipo-runner) echo "$a" > "$FAKE_LOG/runner-url";; esac; done |
| 131 | while [ "$1" != "-o" ]; do shift; done |
| 132 | dirname "$2" >> "$FAKE_LOG/scratch" |
| 133 | case "$2" in |
| 134 | */manifest.json) echo '{}' > "$2" ;; |
| 135 | *) printf '#!/bin/sh\necho "$@" > "$FAKE_LOG/installed"\necho "HOME=$HOME" >> "$FAKE_LOG/installed"\n' > "$2" ;; |
| 136 | esac |
| 137 | `) |
| 138 | write("shasum", "#!/bin/sh\n[ \"$1 $2 $3 $4\" = '-a 256 --check --status' ] || exit 2\ngrep -q '^"+strings.Repeat("c", 64)+" ' || exit 1\n") |
| 139 | cmd := exec.Command("sh", "-s", "--", "https://equipo.example", "fixture-16", "team") |
| 140 | cmd.Stdin = strings.NewReader(string(script)) |
| 141 | cmd.Env = []string{"PATH=" + bin + ":/usr/bin:/bin", "FAKE_LOG=" + m.log} |
| 142 | cmd.Env = append(cmd.Env, env...) |
| 143 | return cmd.CombinedOutput() |
| 144 | } |
| 145 | |
| 146 | func (m *macShell) read(name string) string { |
| 147 | b, _ := os.ReadFile(filepath.Join(m.log, name)) |
| 148 | return string(b) |
| 149 | } |
| 150 | |
| 151 | // The same bootstrap on a Mac with Apple silicon: refuses root before |
| 152 | // downloading, reads the darwin target from the manifest with plutil, |
| 153 | // verifies the runner's digest with shasum and hands over to the |
| 154 | // runner's own installer (no prompts: the label is the Mac's name). |
| 155 | func TestMacInstallShell(t *testing.T) { |
| 156 | digest := strings.Repeat("c", 64) |
| 157 | for name, c := range map[string]struct { |
| 158 | uid, version, digest string |
| 159 | installed bool |
| 160 | downloads string |
| 161 | }{ |
| 162 | "installs": {uid: "502", version: "fixture-16", digest: digest, installed: true, downloads: "download\ndownload\n"}, |
| 163 | "root": {uid: "0", version: "fixture-16", digest: digest}, |
| 164 | "other release": {uid: "502", version: "fixture-15", digest: digest, downloads: "download\n"}, |
| 165 | "bad digest": {uid: "502", version: "fixture-16", digest: "not-a-digest", downloads: "download\n"}, |
| 166 | "short digest": {uid: "502", version: "fixture-16", digest: "abc123", downloads: "download\n"}, |
| 167 | } { |
| 168 | t.Run(name, func(t *testing.T) { |
| 169 | m := newMacShell(t) |
| 170 | m.uid, m.version, m.digest = c.uid, c.version, c.digest |
| 171 | out, err := m.run(t, "TMPDIR="+m.usertmp, "HOME="+m.home) |
| 172 | got := m.read("installed") |
| 173 | if c.installed { |
| 174 | if err != nil || !strings.HasPrefix(got, "install --release fixture-16 --origin https://equipo.example --instance team\n") { |
| 175 | t.Fatalf("not handed over: %v %s %q", err, out, got) |
| 176 | } |
| 177 | if url := m.read("runner-url"); url != "https://equipo.example/releases/fixture-16/darwin-arm64/equipo-runner\n" { |
| 178 | t.Fatalf("runner from %q", url) |
| 179 | } |
| 180 | } else if err == nil || got != "" { |
| 181 | t.Fatalf("%s: not refused: %s", name, out) |
| 182 | } |
| 183 | if b := m.read("downloads"); b != c.downloads { |
| 184 | t.Fatalf("%s: downloads %q", name, b) |
| 185 | } |
| 186 | entries, _ := os.ReadDir(m.usertmp) |
| 187 | for _, e := range entries { |
| 188 | if strings.HasPrefix(e.Name(), "equipo-bootstrap-") { |
| 189 | t.Fatal("scratch leaked") |
| 190 | } |
| 191 | } |
| 192 | }) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Under su, TMPDIR is unset and HOME is the caller's: the bootstrap needs |
| 197 | // neither. It works in the user's own temporary directory (getconf), or |
| 198 | // mktemp's, or a private folder in the user's own home, which it finds |
| 199 | // in the user directory; the runner's installer is given that home, and |
| 200 | // the caller's home is never touched. |
| 201 | func TestMacInstallShellUnderSu(t *testing.T) { |
| 202 | for name, set := range map[string]func(*macShell) string{ |
| 203 | "getconf": func(m *macShell) string { return m.usertmp }, |
| 204 | "home cache": func(m *macShell) string { |
| 205 | m.usertmp, m.noMktempDefault = "", true |
| 206 | return filepath.Join(m.home, ".cache", "equipo-bootstrap") |
| 207 | }, |
| 208 | } { |
| 209 | t.Run(name, func(t *testing.T) { |
| 210 | m := newMacShell(t) |
| 211 | want := set(m) |
| 212 | caller := filepath.Join(m.root, "Users", "paul") |
| 213 | if err := os.MkdirAll(caller, 0700); err != nil { |
| 214 | t.Fatal(err) |
| 215 | } |
| 216 | out, err := m.run(t, "HOME="+caller, "USER=paul", "SHELL=/bin/zsh") |
| 217 | if err != nil || !strings.HasSuffix(m.read("installed"), "HOME="+m.home+"\n") { |
| 218 | t.Fatalf("%v %s installed %q", err, out, m.read("installed")) |
| 219 | } |
| 220 | if dir := strings.TrimSpace(strings.Split(m.read("scratch"), "\n")[0]); filepath.Dir(dir) != want { |
| 221 | t.Fatalf("scratch in %s, want under %s", dir, want) |
| 222 | } |
| 223 | if entries, _ := os.ReadDir(caller); len(entries) != 0 { |
| 224 | t.Fatalf("the caller's home was used: %v", entries) |
| 225 | } |
| 226 | }) |
| 227 | } |
| 228 | } |
| 229 | |