| 1 | package vet |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/ed25519" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/carlosframework/platform/internal/release" |
| 15 | ) |
| 16 | |
| 17 | func testPub(t *testing.T) ed25519.PublicKey { |
| 18 | t.Helper() |
| 19 | _, pubB64, err := release.GenerateKey() |
| 20 | if err != nil { |
| 21 | t.Fatalf("generate key: %v", err) |
| 22 | } |
| 23 | pub, err := release.ParsePublicKey(pubB64, "test") |
| 24 | if err != nil { |
| 25 | t.Fatalf("parse public key: %v", err) |
| 26 | } |
| 27 | return pub |
| 28 | } |
| 29 | |
| 30 | // shipBinary ships bytes as a one-artifact binary release keyed for this |
| 31 | // platform (what Check's boot path selects). |
| 32 | func shipBinary(t *testing.T, store release.Store, app, version string, bin []byte) { |
| 33 | t.Helper() |
| 34 | key := runtime.GOOS + "-" + runtime.GOARCH |
| 35 | m := release.Manifest{ |
| 36 | Schema: 1, App: app, Version: version, Kind: "binary", |
| 37 | Artifacts: map[string]release.Artifact{key: {File: app}}, |
| 38 | } |
| 39 | if err := release.Ship(context.Background(), store, release.OpsAccount, m, map[string][]byte{key: bin}); err != nil { |
| 40 | t.Fatalf("Ship %s %s: %v", app, version, err) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestCheckPassesGoodRelease(t *testing.T) { |
| 45 | store := release.NewDirStore(filepath.Join(t.TempDir(), "store")) |
| 46 | shipBinary(t, store, "echoapp", "v1", []byte("not a real binary, hashes fine")) |
| 47 | if probs := Check(context.Background(), store, testPub(t), release.OpsAccount, "echoapp", "v1", false); len(probs) != 0 { |
| 48 | t.Fatalf("Check = %v, want no problems", probs) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestCheckReportsMissingManifest(t *testing.T) { |
| 53 | store := release.NewDirStore(filepath.Join(t.TempDir(), "store")) |
| 54 | probs := Check(context.Background(), store, testPub(t), release.OpsAccount, "ghost", "v9", false) |
| 55 | if len(probs) != 1 || !strings.Contains(probs[0], "manifest") { |
| 56 | t.Fatalf("Check = %v, want one manifest problem", probs) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestCheckReportsTamperedArtifact(t *testing.T) { |
| 61 | root := filepath.Join(t.TempDir(), "store") |
| 62 | store := release.NewDirStore(root) |
| 63 | shipBinary(t, store, "echoapp", "v1", []byte("original artifact bytes")) |
| 64 | |
| 65 | // Corrupt the stored bytes underneath the manifest's sha256. |
| 66 | artPath := filepath.Join(root, filepath.FromSlash(release.ArtifactKey(release.OpsAccount, "echoapp", "v1", "echoapp"))) |
| 67 | if err := os.WriteFile(artPath, []byte("EVIL artifact bytes!!!!"), 0o644); err != nil { |
| 68 | t.Fatalf("corrupt artifact: %v", err) |
| 69 | } |
| 70 | |
| 71 | probs := Check(context.Background(), store, testPub(t), release.OpsAccount, "echoapp", "v1", false) |
| 72 | if len(probs) != 1 || !strings.Contains(probs[0], "sha256 mismatch") { |
| 73 | t.Fatalf("Check = %v, want one sha256-mismatch problem", probs) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // buildEchoapp compiles testdata/echoapp (the module root's test tenant) or |
| 78 | // skips when no Go toolchain is around. |
| 79 | func buildEchoapp(t *testing.T) []byte { |
| 80 | t.Helper() |
| 81 | goBin, err := exec.LookPath("go") |
| 82 | if err != nil { |
| 83 | t.Skip("go toolchain not found; skipping boot check test") |
| 84 | } |
| 85 | out := filepath.Join(t.TempDir(), "echoapp") |
| 86 | cmd := exec.Command(goBin, "build", "-o", out, "./testdata/echoapp") |
| 87 | cmd.Dir = filepath.Join("..", "..") // module root, from internal/vet |
| 88 | cmd.Env = os.Environ() |
| 89 | if msg, err := cmd.CombinedOutput(); err != nil { |
| 90 | t.Fatalf("build echoapp: %v\n%s", err, msg) |
| 91 | } |
| 92 | bin, err := os.ReadFile(out) |
| 93 | if err != nil { |
| 94 | t.Fatalf("read built echoapp: %v", err) |
| 95 | } |
| 96 | return bin |
| 97 | } |
| 98 | |
| 99 | func TestCheckBootHealthz(t *testing.T) { |
| 100 | if !canListenUnix() { |
| 101 | t.Skip("unix-socket listeners not permitted in this environment (sandbox)") |
| 102 | } |
| 103 | bin := buildEchoapp(t) |
| 104 | store := release.NewDirStore(filepath.Join(t.TempDir(), "store")) |
| 105 | shipBinary(t, store, "echoapp", "v1", bin) |
| 106 | if probs := Check(context.Background(), store, testPub(t), release.OpsAccount, "echoapp", "v1", true); len(probs) != 0 { |
| 107 | t.Fatalf("Check with boot = %v, want no problems", probs) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func TestCheckBootFlagsBinaryThatNeverServes(t *testing.T) { |
| 112 | if !canListenUnix() { |
| 113 | t.Skip("unix-socket listeners not permitted in this environment (sandbox)") |
| 114 | } |
| 115 | falseBin, err := os.ReadFile("/bin/false") |
| 116 | if err != nil { |
| 117 | t.Skipf("no /bin/false to misbehave with: %v", err) |
| 118 | } |
| 119 | old := bootTimeout |
| 120 | bootTimeout = 1 * time.Second // don't sit through the production window |
| 121 | defer func() { bootTimeout = old }() |
| 122 | |
| 123 | store := release.NewDirStore(filepath.Join(t.TempDir(), "store")) |
| 124 | shipBinary(t, store, "deadapp", "v1", falseBin) |
| 125 | probs := Check(context.Background(), store, testPub(t), release.OpsAccount, "deadapp", "v1", true) |
| 126 | if len(probs) != 1 || !strings.Contains(probs[0], "boot:") { |
| 127 | t.Fatalf("Check = %v, want one boot problem", probs) |
| 128 | } |
| 129 | } |
| 130 | |