| new file mode 100644 |
| index 0000000..5977ab0 |
| --- /dev/null |
| +++ b/adopt/gate_adverse_test.go |
| @@ -0,0 +1,624 @@ |
| +package adopt |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "log/slog" |
| + "net/http" |
| + "net/http/httptest" |
| + "path/filepath" |
| + "slices" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/alta/carlosop" |
| + "amadan.net/oficina/alta/console" |
| + "amadan.net/oficina/alta/homeop" |
| + "amadan.net/oficina/alta/homeop/fakehome" |
| + "amadan.net/oficina/alta/provision" |
| + family "amadan.net/rastrillo/rastrillo/crypto" |
| +) |
| + |
| +// The adverse suite drives the whole activation gate: the real Observer, |
| +// reading the fake console and the fake app, inside Alta's real engine, |
| +// talking to the in-memory Home. Every case ends where it matters: the |
| +// run is parked with the gate's own code, and Home never saw a |
| +// directory.activate-consumer for any app. |
| + |
| +const gateOwner = "owner@adverse.test" |
| + |
| +// gate is an initialized Home with Plan A done for the six canonical apps. |
| +type gate struct { |
| + t *testing.T |
| + now time.Time |
| + home *fakehome.Home |
| + store *provision.Store |
| + cfg provision.Config |
| + team string |
| +} |
| + |
| +func (g *gate) clock() time.Time { return g.now } |
| + |
| +func (g *gate) tick(d time.Duration) { g.now = g.now.Add(d) } |
| + |
| +func (g *gate) confirm(kind string) { |
| + g.t.Helper() |
| + if err := g.home.Confirm(homeop.Directory, ""); err != nil { |
| + g.t.Fatal(err) |
| + } |
| + if kind == homeop.KindInitialize { |
| + return |
| + } |
| + owner := "" |
| + if kind == homeop.KindCreateTeam { |
| + owner = gateOwner |
| + } |
| + if err := g.home.Confirm(homeop.Credential, owner); err != nil { |
| + g.t.Fatal(err) |
| + } |
| +} |
| + |
| +// drive advances run until it is done, failed or has stood still for |
| +// rounds passes. |
| +func (g *gate) drive(e *provision.Engine, run int64, rounds int) provision.Run { |
| + g.t.Helper() |
| + var r provision.Run |
| + for i := 0; i < rounds; i++ { |
| + if err := e.Advance(context.Background(), run); err != nil { |
| + var fl *provision.Fail |
| + if !errors.As(err, &fl) { |
| + g.t.Fatalf("advance: %v", err) |
| + } |
| + } |
| + var err error |
| + if r, err = g.store.Run(context.Background(), run); err != nil { |
| + g.t.Fatal(err) |
| + } |
| + if r.State == "done" || r.State == "failed" { |
| + return r |
| + } |
| + g.tick(5 * time.Second) |
| + } |
| + return r |
| +} |
| + |
| +type offlineNet struct{} |
| + |
| +func (offlineNet) RoundTrip(*http.Request) (*http.Response, error) { |
| + return nil, errors.New("offline: an adoption made a network call") |
| +} |
| + |
| +func (g *gate) engine(obs provision.Observer) *provision.Engine { |
| + cfg := g.cfg |
| + cfg.Observer = obs |
| + return provision.New(cfg) |
| +} |
| + |
| +func newGate(t *testing.T) *gate { |
| + t.Helper() |
| + g := &gate{t: t, now: time.Unix(2_000_000_000, 0)} |
| + key, err := family.Generate() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + pre := homeop.Token() |
| + const credOrigin, dirOrigin = "https://id.oficina.works", "https://oficina.works" |
| + g.home = fakehome.New(key.SignPub(), pre, credOrigin, dirOrigin, g.clock) |
| + g.home.AddPerson(gateOwner) |
| + roles := map[string]homeop.Role{} |
| + for r, origin := range map[string]string{homeop.Credential: credOrigin, homeop.Directory: dirOrigin} { |
| + srv := httptest.NewServer(g.home.Handler(r)) |
| + t.Cleanup(srv.Close) |
| + p := g.home.Pins(r) |
| + roles[r] = homeop.Role{Name: r, Origin: origin, URL: srv.URL, Installation: p.Installation, Registry: p.Registry, Device: p.Device} |
| + } |
| + db, err := provision.OpenDB(filepath.Join(t.TempDir(), "alta.db")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { db.Close() }) |
| + g.store, err = provision.NewStore(context.Background(), db, bytes.Repeat([]byte{4}, 32), g.clock) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + net := &http.Client{Transport: offlineNet{}} |
| + g.cfg = provision.Config{ |
| + Store: g.store, Home: &homeop.Client{Key: key, Prefilter: pre, Roles: roles, Now: g.clock}, |
| + Console: &console.Client{HTTP: net, Base: "https://console.invalid", Account: fixtureAccount, Tokens: map[string]string{}}, |
| + Public: net, |
| + HomeCfg: provision.HomeConfig{Issuer: credOrigin, Directory: dirOrigin, KeysBase64: "eyJrMSI6IkJBPT0ifQ==", Provisioner: g.home.ProvisioningPublic()}, |
| + Account: fixtureAccount, Now: g.clock, Logger: slog.New(slog.NewTextHandler(&bytes.Buffer{}, nil)), |
| + Debug: func(err error) { t.Logf("transient: %v", err) }, |
| + } |
| + |
| + ctx := context.Background() |
| + e := g.engine(nil) |
| + run, err := e.StartInitialize(ctx, provision.InitializeAttestation) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + g.confirm(homeop.KindInitialize) |
| + if r := g.drive(e, run, 3); r.State != "done" { |
| + t.Fatalf("initialize: %s %s", r.State, r.Code) |
| + } |
| + g.tick(homeop.WindowLifetime * time.Second) |
| + for _, a := range provision.Slugs() { |
| + g.home.AddEnvConsumer(a, homeop.CanonicalOrigin(a)) |
| + } |
| + if run, err = e.StartAdoption(ctx, "Oficina", gateOwner); err != nil { |
| + t.Fatal(err) |
| + } |
| + g.confirm(homeop.KindCreateTeam) |
| + if r := g.drive(e, run, 20); r.State != "done" || r.Code != "awaiting-configuration" { |
| + t.Fatalf("plan A: %s %s", r.State, r.Code) |
| + } |
| + team, err := g.store.Team(ctx, provision.AdoptionLabel) |
| + if err != nil || team.TeamSQID == "" { |
| + t.Fatalf("no seeded team: %v", err) |
| + } |
| + g.team = team.TeamSQID |
| + return g |
| +} |
| + |
| +// noActivation fails unless Home has never been asked to activate any |
| +// consumer. |
| +func (g *gate) noActivation() { |
| + g.t.Helper() |
| + for _, r := range g.home.Requests() { |
| + if strings.HasPrefix(r, homeop.DirectoryActivate) { |
| + g.t.Fatalf("Home committed %s", r) |
| + } |
| + } |
| + if g.home.Calls[homeop.DirectoryActivate] != 0 { |
| + g.t.Fatal("Home was sent an activation") |
| + } |
| +} |
| + |
| +// attempt is the add-apps run for app with obs: confirmed and driven. |
| +func (g *gate) attempt(app string, obs provision.Observer) provision.Run { |
| + g.t.Helper() |
| + e := g.engine(obs) |
| + g.tick(homeop.WindowLifetime * time.Second) |
| + run, err := e.ActivateAdopted(context.Background(), []string{app}) |
| + if err != nil { |
| + g.t.Fatalf("ActivateAdopted: %v", err) |
| + } |
| + g.confirm(homeop.KindAddApps) |
| + return g.drive(e, run, 3) |
| +} |
| + |
| +// refused is attempt that must end parked with each of want among the |
| +// gate's codes, and with Home never asked to activate. |
| +func (g *gate) refused(app string, obs provision.Observer, want ...string) []string { |
| + g.t.Helper() |
| + r := g.attempt(app, obs) |
| + g.noActivation() |
| + if r.State != "parked" || !strings.HasPrefix(r.Code, "observe-") { |
| + g.t.Fatalf("run %s %q, want parked by the gate", r.State, r.Code) |
| + } |
| + codes := strings.Split(strings.TrimPrefix(r.Code, "observe-"), ",") |
| + for _, c := range want { |
| + if !slices.Contains(codes, c) { |
| + g.t.Fatalf("codes = %q, want %q among them", codes, c) |
| + } |
| + } |
| + return codes |
| +} |
| + |
| +// gateFixture is an app through configure, as newObserverFixture builds |
| +// it, but for the team Home seeded. |
| +func (g *gate) fixture(slug string, before ...func(*writeFixture)) *observerFixture { |
| + g.t.Helper() |
| + t := g.t |
| + w := newWriteFixture(t, slug) |
| + for _, f := range before { |
| + f(w) |
| + } |
| + fastPolls(t, 5*time.Millisecond, 2*time.Second) |
| + w.process(false) |
| + ctx := context.Background() |
| + if _, err := WriteAgentKeys(ctx, w.c, w.h, w.s, w.app, g.team, true); err != nil { |
| + t.Fatalf("WriteAgentKeys: %v", err) |
| + } |
| + g.finish(w) |
| + return g.observer(w) |
| +} |
| + |
| +// finish restarts until delivered, captures the after snapshot and |
| +// records the witness and the pre-plan-a preflight. |
| +func (g *gate) finish(w *writeFixture) { |
| + g.t.Helper() |
| + ctx := context.Background() |
| + base := w.baseline(g.t) |
| + if _, err := RestartAndWait(ctx, w.c, w.h, w.app, base.Identity, func(d Delivery) error { return Delivered(base.Delivery, d) }, true); err != nil { |
| + g.t.Fatalf("RestartAndWait: %v", err) |
| + } |
| + if _, err := CaptureAfter(ctx, w.c, w.h, w.s, w.app); err != nil { |
| + g.t.Fatalf("CaptureAfter: %v", err) |
| + } |
| + if err := RecordWitness(ctx, w.s, w.c, w.h, w.app, WitnessSentence(w.app), true); err != nil { |
| + g.t.Fatalf("RecordWitness: %v", err) |
| + } |
| + if err := w.s.SaveHomePreflight(ctx, HomePreflight{Phase: PhasePrePlanA, ProposedID: 1, ProposedSQID: g.team, AttestedAt: at.Add(-time.Hour)}); err != nil { |
| + g.t.Fatal(err) |
| + } |
| +} |
| + |
| +func (g *gate) observer(w *writeFixture) *observerFixture { |
| + x := &observerFixture{writeFixture: w, now: at} |
| + x.o = &Observer{C: w.c, H: w.h, S: w.s, Artifacts: map[string]string{w.app.Slug: sha256Hex(w.site.header)}, Now: func() time.Time { return x.now }} |
| + return x |
| +} |
| + |
| +// passLiveChecks records every required live check as passed. |
| +func passLiveChecks(t *testing.T, s *Store) { |
| + t.Helper() |
| + for _, c := range RequiredLiveChecks { |
| + setupOK(t, s.SaveCheck(context.Background(), c, "passed")) |
| + } |
| +} |
| + |
| +// ---- The control: the harness reaches activation when nothing is wrong ---- |
| + |
| +func TestAdverseControlActivatesWhenEverythingHolds(t *testing.T) { |
| + g := newGate(t) |
| + x := g.fixture("calendar") |
| + r := g.attempt("calendar", x.o) |
| + if r.State != "done" || g.home.InstanceState("calendar") != "live" { |
| + t.Fatalf("run %s %q, instance %q", r.State, r.Code, g.home.InstanceState("calendar")) |
| + } |
| + if n := len(g.home.Requests()); n == 0 { |
| + t.Fatal("no requests") |
| + } |
| + found := false |
| + for _, q := range g.home.Requests() { |
| + found = found || q == homeop.DirectoryActivate+":calendar" |
| + } |
| + if !found { |
| + t.Fatalf("calendar was not activated: %v", g.home.Requests()) |
| + } |
| +} |
| + |
| +// adverse is one case: how the world or the journal is made wrong, and a |
| +// code the gate must refuse with. |
| +type adverse struct { |
| + name string |
| + app string |
| + before func(*writeFixture) |
| + edit func(t *testing.T, x *observerFixture) |
| + want []string |
| +} |
| + |
| +func (c adverse) run(t *testing.T) { |
| + t.Run(c.name, func(t *testing.T) { |
| + g := newGate(t) |
| + app := c.app |
| + if app == "" { |
| + app = "calendar" |
| + } |
| + var before []func(*writeFixture) |
| + if c.before != nil { |
| + before = append(before, c.before) |
| + } |
| + x := g.fixture(app, before...) |
| + if app != CanaryApp { |
| + passLiveChecks(t, x.s) |
| + } |
| + if c.edit != nil { |
| + c.edit(t, x) |
| + } |
| + g.refused(app, x.o, c.want...) |
| + }) |
| +} |
| + |
| +func runAdverse(t *testing.T, cases []adverse) { |
| + t.Helper() |
| + for _, c := range cases { |
| + c.run(t) |
| + } |
| +} |
| + |
| +// ---- Missing evidence ---- |
| + |
| +func TestAdverseMissingEvidence(t *testing.T) { |
| + ctx := context.Background() |
| + runAdverse(t, []adverse{ |
| + {name: "no pre-plan-a home preflight", edit: func(t *testing.T, x *observerFixture) { |
| + x.rejournal(t, func(e *evidence) { e.home = nil }) |
| + setupOK(t, x.s.SaveHomePreflight(ctx, HomePreflight{Phase: PhasePreInitialize})) |
| + }, want: []string{"missing-home-preflight"}}, |
| + {name: "no baseline", edit: func(t *testing.T, x *observerFixture) { x.rejournal(t, func(e *evidence) { e.base = nil }) }, |
| + want: []string{"missing-baseline"}}, |
| + {name: "write sent but not landed", edit: func(t *testing.T, x *observerFixture) { |
| + x.rejournal(t, func(e *evidence) { e.write.State = "sent" }) |
| + }, want: []string{"missing-write"}}, |
| + {name: "no after snapshot", edit: func(t *testing.T, x *observerFixture) { x.rejournal(t, func(e *evidence) { e.after = nil }) }, |
| + want: []string{"missing-after"}}, |
| + {name: "no witness", edit: func(t *testing.T, x *observerFixture) { x.rejournal(t, func(e *evidence) { e.witness = nil }) }, |
| + want: []string{"missing-witness"}}, |
| + {name: "rolled back", edit: func(t *testing.T, x *observerFixture) { |
| + setupOK(t, x.s.SaveRollback(ctx, RollbackRecord{App: x.app.Slug, State: "sent"})) |
| + }, want: []string{"rollback-recorded"}}, |
| + {name: "artifact not reviewed", edit: func(t *testing.T, x *observerFixture) { x.o.Artifacts = nil }, |
| + want: []string{"artifact-unreviewed"}}, |
| + }) |
| +} |
| + |
| +// A non-canary app is refused until every live check has passed, and a |
| +// failure refuses it for good. |
| +func TestAdversePlanCRefusedWithoutLiveSuccess(t *testing.T) { |
| + cases := []struct { |
| + name string |
| + checks func(t *testing.T, s *Store) |
| + }{ |
| + {name: "none recorded", checks: func(*testing.T, *Store) {}}, |
| + {name: "one missing", checks: func(t *testing.T, s *Store) { |
| + for _, c := range RequiredLiveChecks[1:] { |
| + setupOK(t, s.SaveCheck(context.Background(), c, "passed")) |
| + } |
| + }}, |
| + {name: "one inconclusive", checks: func(t *testing.T, s *Store) { |
| + for _, c := range RequiredLiveChecks[:len(RequiredLiveChecks)-1] { |
| + setupOK(t, s.SaveCheck(context.Background(), c, "passed")) |
| + } |
| + setupOK(t, s.SaveCheck(context.Background(), "owner-local-access-kept", "inconclusive")) |
| + }}, |
| + {name: "all inconclusive", checks: func(t *testing.T, s *Store) { |
| + for _, c := range RequiredLiveChecks { |
| + setupOK(t, s.SaveCheck(context.Background(), c, "inconclusive")) |
| + } |
| + }}, |
| + {name: "one failed", checks: func(t *testing.T, s *Store) { |
| + passLiveChecks(t, s) |
| + setupOK(t, s.SaveCheck(context.Background(), "revoked-membership-refused", "failed:allowed")) |
| + }}, |
| + {name: "failed, then passed again", checks: func(t *testing.T, s *Store) { |
| + setupOK(t, s.SaveCheck(context.Background(), "owner-allowed", "failed:denied")) |
| + passLiveChecks(t, s) |
| + }}, |
| + {name: "local permission check failed", checks: func(t *testing.T, s *Store) { |
| + passLiveChecks(t, s) |
| + setupOK(t, s.SaveCheck(context.Background(), "member-no-local-permission-refused", "failed:allowed")) |
| + }}, |
| + {name: "local permission check failed, rest passed after", checks: func(t *testing.T, s *Store) { |
| + setupOK(t, s.SaveCheck(context.Background(), "member-no-local-permission-refused", "failed:allowed")) |
| + passLiveChecks(t, s) |
| + }}, |
| + } |
| + for _, tc := range cases { |
| + t.Run(tc.name, func(t *testing.T) { |
| + g := newGate(t) |
| + x := g.fixture("docs") |
| + tc.checks(t, x.s) |
| + g.refused("docs", x.o, "live-checks-missing") |
| + }) |
| + } |
| + t.Run("canary needs none", func(t *testing.T) { |
| + g := newGate(t) |
| + x := g.fixture("calendar") |
| + if r := g.attempt("calendar", x.o); r.State != "done" { |
| + t.Fatalf("run %s %q", r.State, r.Code) |
| + } |
| + }) |
| +} |
| + |
| +// ---- Changed identity ---- |
| + |
| +func instanceOf(x *observerFixture) carlosop.Instance { |
| + return carlosop.Instance{Host: CanonicalHost(x.app), App: x.app.Slug, Slug: "s-" + x.app.Slug, Channel: "stable", Region: "eu", |
| + CreatedAt: time.Date(2026, 3, 1, 0, 0, 0, 0, time.UTC), RecordSeq: 4, ReplicaAssignment: json.RawMessage(`{"boxes":["b1"]}`)} |
| +} |
| + |
| +func TestAdverseChangedIdentity(t *testing.T) { |
| + changed := func(edit func(in *carlosop.Instance)) func(*testing.T, *observerFixture) { |
| + return func(t *testing.T, x *observerFixture) { |
| + in := instanceOf(x) |
| + edit(&in) |
| + x.f.AddInstance(in) |
| + } |
| + } |
| + runAdverse(t, []adverse{ |
| + {name: "slug", edit: changed(func(in *carlosop.Instance) { in.Slug = "s-another" }), want: []string{"identity-storage"}}, |
| + {name: "created_at", edit: changed(func(in *carlosop.Instance) { in.CreatedAt = in.CreatedAt.Add(time.Hour) }), want: []string{"identity-storage"}}, |
| + {name: "replica assignment", edit: changed(func(in *carlosop.Instance) { in.ReplicaAssignment = json.RawMessage(`{"boxes":["b2"]}`) }), |
| + want: []string{"identity-storage"}}, |
| + {name: "seed", edit: changed(func(in *carlosop.Instance) { in.Seed = json.RawMessage(`{"from":"another"}`) }), want: []string{"identity-storage"}}, |
| + {name: "public alias", edit: changed(func(in *carlosop.Instance) { in.PublicHost = "alias.oficina.works" }), want: []string{"identity-alias"}}, |
| + {name: "channel", edit: changed(func(in *carlosop.Instance) { in.Channel = "beta" }), want: []string{"identity-channel"}}, |
| + {name: "region", edit: changed(func(in *carlosop.Instance) { in.Region = "us" }), want: []string{"identity-region"}}, |
| + {name: "moving", edit: changed(func(in *carlosop.Instance) { in.Move = json.RawMessage(`{"to":"us"}`) }), want: []string{"identity-moving"}}, |
| + {name: "record_seq", edit: changed(func(in *carlosop.Instance) { in.RecordSeq++ }), want: []string{"identity-record"}}, |
| + // A second record answering for the host is an error in the read |
| + // (never a silent pick), so the record host is staged in the journal. |
| + {name: "record host", edit: func(t *testing.T, x *observerFixture) { |
| + x.rejournal(t, func(e *evidence) { e.witness.Identity.RecordHost = "calendar-record.example.test" }) |
| + }, want: []string{"identity-instance"}}, |
| + // The journal's own records describe another environment or |
| + // ownership than the one the host now serves. |
| + {name: "environment", edit: func(t *testing.T, x *observerFixture) { |
| + x.rejournal(t, func(e *evidence) { e.witness.Identity.Environment = "another" }) |
| + }, want: []string{"identity-environment"}}, |
| + {name: "owned", edit: func(t *testing.T, x *observerFixture) { |
| + x.rejournal(t, func(e *evidence) { e.witness.Identity.Owned = true }) |
| + }, want: []string{"identity-owned"}}, |
| + {name: "baseline's owner differs", edit: func(t *testing.T, x *observerFixture) { |
| + x.rejournal(t, func(e *evidence) { e.base.Identity.Owned = true }) |
| + }, want: []string{"identity-owned"}}, |
| + }) |
| +} |
| + |
| +// ---- Changed config ---- |
| + |
| +func TestAdverseChangedConfig(t *testing.T) { |
| + runAdverse(t, []adverse{ |
| + {name: "preserved secret resealed before the after snapshot", edit: func(t *testing.T, x *observerFixture) { |
| + // A hand-written after row, as CaptureAfter would have refused. |
| + x.rejournal(t, func(e *evidence) { |
| + for i, v := range e.after.Vars { |
| + if v.Key == "CALENDAR_INSTANCE_KEY" { |
| + e.after.Vars = withDigest(e.after.Vars, i, "vd1:"+hex64("7")) |
| + } |
| + } |
| + }) |
| + }, want: []string{"after-not-preserved"}}, |
| + {name: "preserved secret resealed after the after snapshot", edit: func(t *testing.T, x *observerFixture) { |
| + x.f.SetSecret("calendar", "", "CALENDAR_INSTANCE_KEY") |
| + }, want: []string{"drift-changed:CALENDAR_INSTANCE_KEY"}}, |
| + {name: "plain value changed", edit: func(t *testing.T, x *observerFixture) { |
| + x.f.SetVar("calendar", "", "CALENDAR_ORIGIN", "https://calendar.oficina.works/another-plain-value") |
| + }, want: []string{"drift-changed:CALENDAR_ORIGIN"}}, |
| + {name: "key removed", edit: func(t *testing.T, x *observerFixture) { |
| + x.f.Unset("calendar", "", "CALENDAR_ORIGIN") |
| + }, want: []string{"drift-gone:CALENDAR_ORIGIN"}}, |
| + {name: "new team-mode key", edit: func(t *testing.T, x *observerFixture) { |
| + x.f.SetVar("calendar", "", provision.TeamKeyNames(x.app)[0], "x") |
| + }, want: []string{"team-mode-" + provision.TeamKeyNames(appNamedStatic("calendar"))[0]}}, |
| + {name: "agent key with another digest", edit: func(t *testing.T, x *observerFixture) { |
| + x.f.SetVar("calendar", "", "CALENDAR_AGENT_TEAM", "otherTeam") |
| + }, want: []string{"agent-key-CALENDAR_AGENT_TEAM"}}, |
| + {name: "issuer key with another digest", edit: func(t *testing.T, x *observerFixture) { |
| + x.f.SetVar("calendar", "", "CALENDAR_AGENT_ISSUER", "https://elsewhere.example") |
| + }, want: []string{"agent-key-CALENDAR_AGENT_ISSUER"}}, |
| + {name: "overlay shadows an agent key", edit: func(t *testing.T, x *observerFixture) { |
| + // The same value, so the same digest: only its source differs. |
| + x.f.SetOverlay("calendar", CanonicalHost(x.app), "CALENDAR_AGENT_ISSUER", Issuer) |
| + }, want: []string{"agent-key-CALENDAR_AGENT_ISSUER"}}, |
| + {name: "one agent key unset", edit: func(t *testing.T, x *observerFixture) { |
| + x.f.Unset("calendar", "", "CALENDAR_AGENT_TEAM") |
| + }, want: []string{"agent-key-CALENDAR_AGENT_TEAM"}}, |
| + {name: "a key the runner did not write is added", edit: func(t *testing.T, x *observerFixture) { |
| + x.f.PlatformWrite("calendar", "", "CALENDAR_PLATFORM_NOTE", "n") |
| + }, want: []string{"config-added:CALENDAR_PLATFORM_NOTE"}}, |
| + {name: "written for another team", edit: func(t *testing.T, x *observerFixture) { |
| + x.rejournal(t, func(e *evidence) { e.write.Sent[AgentKeys(x.app)[1]] = "otherTeam" }) |
| + }, want: []string{"team-mismatch"}}, |
| + {name: "written for another issuer", edit: func(t *testing.T, x *observerFixture) { |
| + x.rejournal(t, func(e *evidence) { e.write.Sent[AgentKeys(x.app)[0]] = "https://elsewhere.example" }) |
| + }, want: []string{"issuer-mismatch"}}, |
| + }) |
| +} |
| + |
| +func appNamedStatic(slug string) provision.App { |
| + a, _ := provision.AppBySlug(slug) |
| + return a |
| +} |
| + |
| +// ---- Superseded witness ---- |
| + |
| +func TestAdverseSupersededWitness(t *testing.T) { |
| + runAdverse(t, []adverse{ |
| + {name: "after version changed", edit: func(t *testing.T, x *observerFixture) { |
| + // A platform writer adds a key and the after snapshot is |
| + // taken again: the witness saw the earlier version. |
| + x.f.PlatformWrite("calendar", "", "CALENDAR_PLATFORM_NOTE", "n") |
| + if _, err := CaptureAfter(context.Background(), x.c, x.h, x.s, x.app); err != nil { |
| + t.Fatalf("CaptureAfter: %v", err) |
| + } |
| + }, want: []string{"witness-superseded"}}, |
| + {name: "witness saw another after version", edit: func(t *testing.T, x *observerFixture) { |
| + x.rejournal(t, func(e *evidence) { e.witness.AfterVersion = "cv1:" + hex64("9") }) |
| + }, want: []string{"witness-superseded"}}, |
| + {name: "delivered version changed", edit: func(t *testing.T, x *observerFixture) { |
| + x.rejournal(t, func(e *evidence) { e.witness.Delivery.Version = "v1.3.9" }) |
| + }, want: []string{"witness-superseded"}}, |
| + {name: "delivered header changed", edit: func(t *testing.T, x *observerFixture) { |
| + x.rejournal(t, func(e *evidence) { e.witness.Delivery.Header = "v1.3.9" }) |
| + }, want: []string{"witness-superseded"}}, |
| + {name: "redeployed since", edit: func(t *testing.T, x *observerFixture) { |
| + x.site.version, x.site.header = "v1.5.0", "v1.5.0" |
| + }, want: []string{"witness-superseded"}}, |
| + }) |
| +} |
| + |
| +// ---- Changed delivery ---- |
| + |
| +func TestAdverseChangedDelivery(t *testing.T) { |
| + runAdverse(t, []adverse{ |
| + {name: "version body", edit: func(t *testing.T, x *observerFixture) { x.site.version = "v9.9.9" }, |
| + want: []string{"not-delivered", "version-header-mismatch", "witness-superseded"}}, |
| + {name: "header", edit: func(t *testing.T, x *observerFixture) { x.site.header = "v9.9.9" }, |
| + want: []string{"not-delivered", "version-header-mismatch", "witness-superseded"}}, |
| + {name: "redirect", edit: func(t *testing.T, x *observerFixture) { x.site.redirectVersion = true }, want: []string{"not-delivered"}}, |
| + {name: "healthz 503", edit: func(t *testing.T, x *observerFixture) { x.site.health = 503 }, want: []string{"not-delivered"}}, |
| + {name: "operations reverted to app-401", edit: func(t *testing.T, x *observerFixture) { |
| + x.site.ops = func(w http.ResponseWriter) { |
| + w.Header().Set("Content-Type", "application/json") |
| + w.WriteHeader(401) |
| + _, _ = w.Write([]byte(`{"error":"unauthorized"}`)) |
| + } |
| + }, want: []string{"not-delivered"}}, |
| + {name: "operations gone", edit: func(t *testing.T, x *observerFixture) { x.site.ops = nil }, want: []string{"not-delivered"}}, |
| + }) |
| +} |
| + |
| +// ---- Artifact ---- |
| + |
| +func TestAdverseArtifact(t *testing.T) { |
| + runAdverse(t, []adverse{ |
| + {name: "reviewed digest differs", edit: func(t *testing.T, x *observerFixture) { |
| + x.o.Artifacts = map[string]string{"calendar": hex64("0")} |
| + }, want: []string{"artifact-mismatch"}}, |
| + {name: "reviewed digest malformed", edit: func(t *testing.T, x *observerFixture) { |
| + x.o.Artifacts = map[string]string{"calendar": "AB" + sha256Hex(valueVersion)[2:]} |
| + }, want: []string{"artifact-unreviewed"}}, |
| + {name: "version malformed", before: func(w *writeFixture) { w.site.version, w.site.header = "-v1", "-v1" }, |
| + want: []string{"version-unacceptable"}}, |
| + }) |
| +} |
| + |
| +// ---- Malformed receipts ---- |
| + |
| +// Each malformed answer to the write leaves it stopped, never landed, so |
| +// the gate has no write for the app. |
| +func TestAdverseMalformedReceipts(t *testing.T) { |
| + other := "cv1:" + strings.Repeat("9", 64) |
| + cases := map[string]func(p string) func(rc map[string]any){ |
| + "extra set key": func(p string) func(rc map[string]any) { |
| + return func(rc map[string]any) { rc["set"] = append(rc["set"].([]any), p+"_ORIGIN") } |
| + }, |
| + "missing digest": func(p string) func(rc map[string]any) { |
| + return func(rc map[string]any) { delete(rc["digests"].(map[string]any), p+"_AGENT_TEAM") } |
| + }, |
| + "wrong digest": func(p string) func(rc map[string]any) { |
| + return func(rc map[string]any) { |
| + rc["digests"].(map[string]any)[p+"_AGENT_TEAM"] = carlosop.EnvDigest("tOther9") |
| + } |
| + }, |
| + "unset not empty": func(p string) func(rc map[string]any) { |
| + return func(rc map[string]any) { rc["unset"] = []any{p + "_ORIGIN"} } |
| + }, |
| + "unset_absent set": func(p string) func(rc map[string]any) { |
| + return func(rc map[string]any) { rc["unset_absent"] = []any{p + "_ORIGIN"} } |
| + }, |
| + "others moved": func(string) func(rc map[string]any) { |
| + return func(rc map[string]any) { |
| + rc["others"] = map[string]any{"default": map[string]any{"before": rc["version_before"], "after": other}} |
| + } |
| + }, |
| + "version before": func(string) func(rc map[string]any) { |
| + return func(rc map[string]any) { rc["version_before"] = other } |
| + }, |
| + } |
| + for name, edit := range cases { |
| + t.Run(name, func(t *testing.T) { |
| + g := newGate(t) |
| + w := newWriteFixture(t, "calendar") |
| + fastPolls(t, 5*time.Millisecond, 2*time.Second) |
| + w.process(false) |
| + w.putRewriter(t, edit(w.app.Prefix)) |
| + if _, err := WriteAgentKeys(context.Background(), w.c, w.h, w.s, w.app, g.team, true); !errors.Is(err, ErrStopConflict) { |
| + t.Fatalf("err = %v, want ErrStopConflict", err) |
| + } |
| + rec, ok := w.writeRecord(t) |
| + if !ok || rec.State == "landed" { |
| + t.Fatalf("write %q, want one that did not land", rec.State) |
| + } |
| + setupOK(t, w.s.SaveHomePreflight(context.Background(), HomePreflight{Phase: PhasePrePlanA, ProposedID: 1, ProposedSQID: g.team, AttestedAt: at})) |
| + x := g.observer(w) |
| + g.refused("calendar", x.o, "missing-write") |
| + }) |
| + } |
| +} |