package host import ( "context" "encoding/json" "errors" "fmt" "os" "path/filepath" "strings" "sync" "testing" "time" "github.com/carlosframework/platform/internal/hostfloor" "github.com/carlosframework/platform/internal/registry" "github.com/carlosframework/platform/internal/release" "github.com/carlosframework/platform/internal/rootparticipation" "github.com/carlosframework/platform/internal/rootstate" ) func TestPlan14LaunchVerifierToken(t *testing.T) { lock := &plan14ReleaseStore{} x := &release.ReleaseExclusion{Open: func() (rootstate.Store, error) { return lock, nil }} disk := &rollbackDisk{dir: t.TempDir()} reads := 0 v := rootLaunchVerifier{exclude: x, open: func() (rootstate.Store, error) { reads++; return disk, nil }, reconcile: generationReconciler{ read: func(ctx context.Context) (generationInventory, error) { if ctx.Value(launchRecoveryCensusKey{}) == true { t.Fatal("ordinary verification admitted pending acquisition") } return generationInventory{Census: rootparticipation.Census{Schema: 1, Box: "box", Enrollment: "en", Migration: 1, Revision: 1, LaunchRevision: 7, Complete: true}}, nil }, kernel: func(context.Context) (string, string, error) { return "boot", strings.Repeat("a", 64), nil }, }} if !errors.Is(v.verify(t.Context(), 7), rootparticipation.ErrUnavailable) || reads != 0 { t.Fatal("missing token reached authority") } foreign := &release.ReleaseExclusion{Open: x.Open} if err := foreign.With(t.Context(), func(ctx context.Context) error { return v.verify(ctx, 7) }); !errors.Is(err, rootparticipation.ErrUnavailable) || reads != 0 { t.Fatal("foreign token reached authority", err) } var stale context.Context if err := x.With(t.Context(), func(ctx context.Context) error { stale = ctx; return v.verify(ctx, 7) }); err != nil { t.Fatal(err) } if !errors.Is(v.verify(stale, 7), rootparticipation.ErrUnavailable) { t.Fatal("stale token accepted") } if err := x.With(t.Context(), func(ctx context.Context) error { return v.verify(ctx, 8) }); err == nil { t.Fatal("wrong launch revision accepted") } } func TestPlan14LaunchVerifierCallerOrder(t *testing.T) { t.Parallel() for _, name := range []string{"idle", "begin", "end", "recover", "activation", "floor-begin", "floor-end", "system-begin", "system-clear", "system-recovery", "guard-close", "guard-rollback"} { t.Run(name, func(t *testing.T) { lock := &plan14ReleaseStore{} x := &release.ReleaseExclusion{Open: func() (rootstate.Store, error) { return lock, nil }} l := updateParticipationFixture(t) l.State = &d3ParticipationDisk{Store: l.State} l.ExcludeLaunches = x.With verified := 0 l.VerifyLaunches = func(ctx context.Context, _ uint64) error { verified++ if rootparticipation.IsAcquisitionRecovery(ctx) != (name == "recover") { t.Error("wrong acquisition recovery scope") } if err := x.Check(ctx); err != nil { return rootparticipation.ErrUnavailable } return nil } if name == "end" || name == "floor-end" || name == "system-clear" || name == "guard-close" || name == "guard-rollback" { if err := x.With(t.Context(), func(ctx context.Context) error { return l.BeginUpdate(ctx, "update") }); err != nil { t.Fatal(err) } } if name == "recover" { c, err := l.Census() if err != nil { t.Fatal(err) } c.Pending = "acquisition" raw, _ := json.Marshal(c) if err := l.State.Write("census.json", raw); err != nil { t.Fatal(err) } } floor := hostfloor.Disk{Dir: t.TempDir(), Participation: l} a := &systemApplier{participation: l, optDir: t.TempDir(), dataDir: t.TempDir()} if name == "guard-close" || name == "guard-rollback" { armLocalFloor(t, a.dataDir, "v0.30.0") a.openRegistry = func() (*registry.Registry, error) { return registry.Open(filepath.Join(a.dataDir, "registry.db")) } } run := func(ctx context.Context) error { switch name { case "idle": return floor.ControllersIdle(ctx) case "begin": return floor.BeginParticipation(ctx, "update", a.dataDir, nil, func() error { t.Error("legacy selected"); return nil }) case "end": return floor.EndParticipation(ctx, "update", nil) case "recover": return x.With(ctx, l.RecoverAcquisition) case "activation": return l.WithLaunchExclusion(ctx, func(ctx context.Context) error { return l.ActivationReady(ctx) }) case "floor-begin": return floor.Begin(ctx, "update", a.dataDir) case "floor-end": return floor.EndParticipation(ctx, "update", nil) case "system-begin": return a.beginExecutableChange(ctx, "new", time.Now()) case "system-clear": return a.clearUpdateBegun(ctx) case "guard-close", "guard-rollback": return a.activationGuard(ctx, "v0.32.0", "v0.31.0", func(ctx context.Context) error { return a.clearUpdateBegun(ctx) }) case "system-recovery": return a.controllersRecovered(ctx) } return nil } held, leave, holderDone := make(chan struct{}), make(chan struct{}), make(chan error, 1) go func() { holderDone <- x.With(t.Context(), func(ctx context.Context) error { close(held) <-leave if name == "guard-close" || name == "guard-rollback" { if err := withFloorLock(a.dataDir, true, func(*movesDir) error { return nil }); err != nil { return err } } unlock, err := l.State.Lock(ctx, "transaction.lock") if err == nil { unlock() } return err }) }() <-held done := make(chan error, 1) go func() { done <- run(t.Context()) }() select { case err := <-done: t.Fatal("caller escaped held release", err) case <-time.After(15 * time.Millisecond): } close(leave) select { case err := <-holderDone: if err != nil { t.Fatal(err) } case <-time.After(2 * time.Second): t.Fatal("release holder deadlocked") } select { case err := <-done: if err != nil { t.Fatal(err) } case <-time.After(2 * time.Second): t.Fatal("caller deadlocked") } if verified == 0 { t.Fatal("verifier not reached") } }) } } type d3ParticipationDisk struct { rootstate.Store mu sync.Mutex } func (d *d3ParticipationDisk) Lock(ctx context.Context, name string) (func(), error) { d.mu.Lock() return d.mu.Unlock, nil } func TestPlan14LaunchVerifierNoCensus(t *testing.T) { t.Setenv("CARLOS_HOST_CLAIM", "") l := updateParticipationFixture(t) if err := l.State.Remove("census.json"); err != nil { t.Fatal(err) } l.ExcludeLaunches = func(context.Context, func(context.Context) error) error { t.Fatal("no-census box acquired release exclusion") return nil } l.VerifyLaunches = func(context.Context, uint64) error { t.Fatal("no-census box consulted verifier"); return nil } floor := hostfloor.Disk{Dir: t.TempDir(), Participation: l} data := t.TempDir() if err := os.Mkdir(filepath.Join(data, ".hostexec"), 0700); err != nil { t.Fatal(err) } if err := floor.ControllersIdle(t.Context(), data); err != nil { t.Fatal(err) } if err := floor.Begin(t.Context(), "legacy", data); err != nil { t.Fatal(err) } if err := floor.EndParticipation(t.Context(), "legacy", nil); err != nil { t.Fatal(err) } a := &systemApplier{participation: l, optDir: t.TempDir(), dataDir: data} if err := a.beginExecutableChange(t.Context(), "new", time.Now()); err != nil { t.Fatal(err) } if err := a.clearUpdateBegun(t.Context()); err != nil { t.Fatal(err) } if err := a.checkUnarmedParticipationRecovery(t.Context()); err != nil { t.Fatal(err) } } func TestPlan14SystemDeadlineNoCensusUnchanged(t *testing.T) { for _, census := range []bool{false, true} { t.Run(fmt.Sprint(census), func(t *testing.T) { l := updateParticipationFixture(t) if !census { if err := l.State.Remove("census.json"); err != nil { t.Fatal(err) } } f := newApplyFixture(t, "stable", "old") f.a.participation = l f.a.floorHook = func(*systemApplier) error { return context.DeadlineExceeded } err := f.a.Run(t.Context(), false) if !errors.Is(err, context.DeadlineExceeded) || errors.Is(err, errSystemDeferred) != census { t.Fatal("wrong timeout disposition", err) } }) } }