package vet import ( "context" "crypto/ed25519" "os" "os/exec" "path/filepath" "runtime" "strings" "testing" "time" "github.com/carlosframework/platform/internal/release" ) func testPub(t *testing.T) ed25519.PublicKey { t.Helper() _, pubB64, err := release.GenerateKey() if err != nil { t.Fatalf("generate key: %v", err) } pub, err := release.ParsePublicKey(pubB64, "test") if err != nil { t.Fatalf("parse public key: %v", err) } return pub } // shipBinary ships bytes as a one-artifact binary release keyed for this // platform (what Check's boot path selects). func shipBinary(t *testing.T, store release.Store, app, version string, bin []byte) { t.Helper() key := runtime.GOOS + "-" + runtime.GOARCH m := release.Manifest{ Schema: 1, App: app, Version: version, Kind: "binary", Artifacts: map[string]release.Artifact{key: {File: app}}, } if err := release.Ship(context.Background(), store, release.OpsAccount, m, map[string][]byte{key: bin}); err != nil { t.Fatalf("Ship %s %s: %v", app, version, err) } } func TestCheckPassesGoodRelease(t *testing.T) { store := release.NewDirStore(filepath.Join(t.TempDir(), "store")) shipBinary(t, store, "echoapp", "v1", []byte("not a real binary, hashes fine")) if probs := Check(context.Background(), store, testPub(t), release.OpsAccount, "echoapp", "v1", false); len(probs) != 0 { t.Fatalf("Check = %v, want no problems", probs) } } func TestCheckReportsMissingManifest(t *testing.T) { store := release.NewDirStore(filepath.Join(t.TempDir(), "store")) probs := Check(context.Background(), store, testPub(t), release.OpsAccount, "ghost", "v9", false) if len(probs) != 1 || !strings.Contains(probs[0], "manifest") { t.Fatalf("Check = %v, want one manifest problem", probs) } } func TestCheckReportsTamperedArtifact(t *testing.T) { root := filepath.Join(t.TempDir(), "store") store := release.NewDirStore(root) shipBinary(t, store, "echoapp", "v1", []byte("original artifact bytes")) // Corrupt the stored bytes underneath the manifest's sha256. artPath := filepath.Join(root, filepath.FromSlash(release.ArtifactKey(release.OpsAccount, "echoapp", "v1", "echoapp"))) if err := os.WriteFile(artPath, []byte("EVIL artifact bytes!!!!"), 0o644); err != nil { t.Fatalf("corrupt artifact: %v", err) } probs := Check(context.Background(), store, testPub(t), release.OpsAccount, "echoapp", "v1", false) if len(probs) != 1 || !strings.Contains(probs[0], "sha256 mismatch") { t.Fatalf("Check = %v, want one sha256-mismatch problem", probs) } } // buildEchoapp compiles testdata/echoapp (the module root's test tenant) or // skips when no Go toolchain is around. func buildEchoapp(t *testing.T) []byte { t.Helper() goBin, err := exec.LookPath("go") if err != nil { t.Skip("go toolchain not found; skipping boot check test") } out := filepath.Join(t.TempDir(), "echoapp") cmd := exec.Command(goBin, "build", "-o", out, "./testdata/echoapp") cmd.Dir = filepath.Join("..", "..") // module root, from internal/vet cmd.Env = os.Environ() if msg, err := cmd.CombinedOutput(); err != nil { t.Fatalf("build echoapp: %v\n%s", err, msg) } bin, err := os.ReadFile(out) if err != nil { t.Fatalf("read built echoapp: %v", err) } return bin } func TestCheckBootHealthz(t *testing.T) { if !canListenUnix() { t.Skip("unix-socket listeners not permitted in this environment (sandbox)") } bin := buildEchoapp(t) store := release.NewDirStore(filepath.Join(t.TempDir(), "store")) shipBinary(t, store, "echoapp", "v1", bin) if probs := Check(context.Background(), store, testPub(t), release.OpsAccount, "echoapp", "v1", true); len(probs) != 0 { t.Fatalf("Check with boot = %v, want no problems", probs) } } func TestCheckBootFlagsBinaryThatNeverServes(t *testing.T) { if !canListenUnix() { t.Skip("unix-socket listeners not permitted in this environment (sandbox)") } falseBin, err := os.ReadFile("/bin/false") if err != nil { t.Skipf("no /bin/false to misbehave with: %v", err) } old := bootTimeout bootTimeout = 1 * time.Second // don't sit through the production window defer func() { bootTimeout = old }() store := release.NewDirStore(filepath.Join(t.TempDir(), "store")) shipBinary(t, store, "deadapp", "v1", falseBin) probs := Check(context.Background(), store, testPub(t), release.OpsAccount, "deadapp", "v1", true) if len(probs) != 1 || !strings.Contains(probs[0], "boot:") { t.Fatalf("Check = %v, want one boot problem", probs) } }