| 1 | package agentvm |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func setupFunction(t *testing.T, name string, end string) string { |
| 12 | t.Helper() |
| 13 | start := strings.Index(SetupScript, name+"() ") |
| 14 | if start < 0 { |
| 15 | t.Fatal("missing function", name) |
| 16 | } |
| 17 | body := SetupScript[start:] |
| 18 | last := strings.Index(body, "\n"+end+"\n") |
| 19 | if last < 0 { |
| 20 | t.Fatal("missing function end", name) |
| 21 | } |
| 22 | return body[:last+len(end)+2] |
| 23 | } |
| 24 | |
| 25 | func TestRepositoryKeyFailurePreservesTrustedFile(t *testing.T) { |
| 26 | for _, mode := range []string{"eof", "empty", "invalid", "success"} { |
| 27 | t.Run(mode, func(t *testing.T) { |
| 28 | dir := t.TempDir() |
| 29 | key := filepath.Join(dir, "nodesource.asc") |
| 30 | if err := os.WriteFile(key, []byte("trusted"), 0644); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | commands := map[string]string{ |
| 34 | "curl": `#!/bin/sh |
| 35 | while [ "$#" -gt 0 ]; do |
| 36 | if [ "$1" = -o ]; then shift; target="$1"; fi |
| 37 | shift |
| 38 | done |
| 39 | case "$KEY_MODE" in |
| 40 | eof) printf partial > "$target"; exit 56;; |
| 41 | empty) : > "$target";; |
| 42 | invalid) printf '<html>bad gateway</html>' > "$target";; |
| 43 | success) printf '%s\n' '-----BEGIN PGP PUBLIC KEY BLOCK-----' new '-----END PGP PUBLIC KEY BLOCK-----' > "$target";; |
| 44 | esac |
| 45 | `, |
| 46 | |
| 47 | "sudo": `#!/bin/sh |
| 48 | if [ "$1" = install ]; then |
| 49 | shift |
| 50 | while [ "$#" -gt 2 ]; do shift; done |
| 51 | cp "$1" "$2" |
| 52 | else |
| 53 | exec "$@" |
| 54 | fi |
| 55 | `, |
| 56 | } |
| 57 | for name, body := range commands { |
| 58 | if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0755); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | } |
| 62 | cmd := exec.Command("bash", "-c", setupFunction(t, "install_repository_key", ")")+`install_repository_key https://example.invalid/key "$KEY_TARGET"`) |
| 63 | cmd.Env = append(os.Environ(), "HOME="+dir, "PATH="+dir+":"+os.Getenv("PATH"), "KEY_MODE="+mode, "KEY_TARGET="+key) |
| 64 | out, err := cmd.CombinedOutput() |
| 65 | data, readErr := os.ReadFile(key) |
| 66 | if readErr != nil { |
| 67 | t.Fatal(readErr) |
| 68 | } |
| 69 | if mode == "success" { |
| 70 | if err != nil || !strings.Contains(string(data), "\nnew\n") { |
| 71 | t.Fatalf("%v %s %q", err, out, data) |
| 72 | } |
| 73 | } else if err == nil || string(data) != "trusted" { |
| 74 | t.Fatalf("failed download replaced key: %v %q", err, data) |
| 75 | } |
| 76 | left, _ := filepath.Glob(key + ".*") |
| 77 | if len(left) != 0 { |
| 78 | t.Fatal("staging key leaked", left) |
| 79 | } |
| 80 | }) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestAPTFailureRecordsOOMWithoutChangingExitCode(t *testing.T) { |
| 85 | dir := t.TempDir() |
| 86 | commands := map[string]string{ |
| 87 | "sudo": `#!/bin/sh |
| 88 | if [ "$1" = env ]; then echo apt-failed; exit 137; fi |
| 89 | if [ "$1" = timeout ]; then echo 'Out of memory: Killed process 2926 (apt-get)'; exit 0; fi |
| 90 | exit 99 |
| 91 | `, |
| 92 | "awk": "#!/bin/sh\necho 3\n", |
| 93 | "ps": "#!/bin/sh\necho 'PID RSS COMMAND'\n", |
| 94 | } |
| 95 | for name, body := range commands { |
| 96 | if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0755); err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | } |
| 100 | cmd := exec.Command("bash", "-c", "set -euo pipefail\n"+setupFunction(t, "apt_get", "}")+"apt_get install package") |
| 101 | cmd.Env = append(os.Environ(), "HOME="+dir, "PATH="+dir+":"+os.Getenv("PATH"), "CLAUDE_CAGE=1") |
| 102 | out, err := cmd.CombinedOutput() |
| 103 | if err == nil || cmd.ProcessState.ExitCode() != 137 || !strings.Contains(string(out), "oom_kill before=3") || !strings.Contains(string(out), "Killed process") { |
| 104 | t.Fatalf("lost failure evidence: %v %s", err, out) |
| 105 | } |
| 106 | } |