| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "github.com/carlosframework/platform/internal/moves" |
| 6 | "github.com/carlosframework/platform/internal/rootstate" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestPlan14FloorNoDeadEndCurrentExecutor(t *testing.T) { |
| 13 | for _, source := range []string{"root-authorized bound", "agent-written moves", "unpublished seeds"} { |
| 14 | t.Run(source, func(t *testing.T) { |
| 15 | f := rootMovesFloors{private: &rollbackMemory{files: map[string][]byte{}}, public: &rollbackMemory{files: map[string][]byte{}}, identity: rootFloorIdentity{"d", "b", "e"}} |
| 16 | high := &moves.Floor{Min: 1, MinRelease: "v9.0.0"} |
| 17 | if source == "root-authorized bound" { |
| 18 | if err := f.reconstruct(t.Context(), rootFloorReconstruction{Identity: f.identity, Floor: high, Source: "root observation", Digest: floorTestDigest, Authorization: floorTestAuthorization(t, f)}); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | } else if err := f.refresh(t.Context(), nil, []*moves.Floor{high}, nil); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | binary := rootFloorBinary{Version: "v1.0.0", Protocol: 1, Digest: floorTestDigest} |
| 25 | if source == "unpublished seeds" { |
| 26 | binary = binary.forSeeds() |
| 27 | } |
| 28 | j := rootFloorJudge{floors: f, current: "current", prev: "prev", binary: func(string) (rootFloorBinary, error) { return binary, nil }} |
| 29 | got, err := rootFloorBootCandidate(t.Context(), j, false, nil, nil) |
| 30 | if err != nil || got != "current" { |
| 31 | t.Fatalf("R6 stranded certified executor: source=%s selected=%q err=%v", source, got, err) |
| 32 | } |
| 33 | }) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestPlan14FloorNoDeadEndUnknownPrev(t *testing.T) { |
| 38 | f := rootMovesFloors{private: &rollbackMemory{files: map[string][]byte{}}, public: &rollbackMemory{files: map[string][]byte{}}, identity: rootFloorIdentity{"d", "b", "e"}} |
| 39 | j := rootFloorJudge{floors: f, current: "current", prev: "prev", binary: func(string) (rootFloorBinary, error) { |
| 40 | return rootFloorBinary{Version: "v1.0.0", Protocol: 1, Digest: floorTestDigest}, nil |
| 41 | }} |
| 42 | got, err := rootFloorBootCandidate(t.Context(), j, true, func(context.Context, string) error { return nil }, func(context.Context) error { return nil }) |
| 43 | if err != nil || got != "prev" { |
| 44 | t.Fatalf("R6 unknown history lost eligible signed prev: selected=%q err=%v", got, err) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestPlan14FloorNoDeadEndNotePrev(t *testing.T) { |
| 49 | f := rootMovesFloors{private: &rollbackMemory{files: map[string][]byte{}}, public: &rollbackMemory{files: map[string][]byte{}}, identity: rootFloorIdentity{"d", "b", "e"}} |
| 50 | if err := f.refresh(t.Context(), nil, []*moves.Floor{{Min: 1, MinRelease: "v9.0.0"}}, nil); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | j := rootFloorJudge{floors: f, current: "current", prev: "prev", binary: func(string) (rootFloorBinary, error) { |
| 54 | return rootFloorBinary{Version: "v1.0.0", Protocol: 1, Digest: floorTestDigest}, nil |
| 55 | }, idle: func(context.Context) error { return nil }} |
| 56 | if err := j.judge(t.Context()); err != nil { |
| 57 | t.Fatalf("R6 notePrev judge refuses rescue install: %v", err) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestPlan14FloorNoDeadEndLegacyCapAndHealth(t *testing.T) { |
| 62 | f := rootMovesFloors{private: &rollbackMemory{files: map[string][]byte{}}, public: &rollbackMemory{files: map[string][]byte{}}, identity: rootFloorIdentity{"d", "b", "e"}} |
| 63 | high := &moves.Floor{Min: 9, MinRelease: "v9.0.0"} |
| 64 | if err := f.refresh(t.Context(), nil, []*moves.Floor{high}, nil); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | if b, err := f.localBound(); err != nil || b != nil { |
| 68 | t.Fatal("agent initialized floor", b, err) |
| 69 | } |
| 70 | b := rootFloorBinary{Version: "v2.0.0", Protocol: 2, Digest: floorTestDigest} |
| 71 | if err := f.acceptHealthy(t.Context(), b); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | if _, err := f.projection(); err != nil { |
| 75 | t.Fatal("root health did not establish accepted history", err) |
| 76 | } |
| 77 | _ = f.refresh(t.Context(), nil, []*moves.Floor{high}, nil) |
| 78 | got, err := f.localBound() |
| 79 | if err != nil || got == nil || got.Min != 2 || got.MinRelease != "v2.0.0" { |
| 80 | t.Fatal("agent escaped health cap", got, err) |
| 81 | } |
| 82 | // A lower healthy recovery cannot erase the highest root observation. |
| 83 | b.Version = "v1.0.0" |
| 84 | if err := f.acceptHealthy(t.Context(), b); err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | h, err := f.healthy() |
| 88 | if err != nil || h.Version != "v2.0.0" { |
| 89 | t.Fatal("health cap regressed", h, err) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | type floorOrderStore struct { |
| 94 | rootstate.Store |
| 95 | held *bool |
| 96 | } |
| 97 | |
| 98 | func (s floorOrderStore) Lock(ctx context.Context, name string) (func(), error) { |
| 99 | unlock, err := s.Store.Lock(ctx, name) |
| 100 | if err != nil { |
| 101 | return nil, err |
| 102 | } |
| 103 | *s.held = true |
| 104 | return func() { *s.held = false; unlock() }, nil |
| 105 | } |
| 106 | func TestPlan14RootFloorParticipationBeforeFloor(t *testing.T) { |
| 107 | held := false |
| 108 | f := rootMovesFloors{private: floorOrderStore{Store: &rollbackMemory{files: map[string][]byte{}}, held: &held}, public: &rollbackMemory{files: map[string][]byte{}}, identity: rootFloorIdentity{"d", "b", "e"}} |
| 109 | calls := 0 |
| 110 | j := rootFloorJudge{floors: f, current: "current", prev: "prev", binary: func(string) (rootFloorBinary, error) { |
| 111 | return rootFloorBinary{Version: "v1.0.0", Protocol: 1, Digest: floorTestDigest}, nil |
| 112 | }, idle: func(context.Context) error { |
| 113 | calls++ |
| 114 | if held { |
| 115 | t.Fatal("participation acquired under floor lock") |
| 116 | } |
| 117 | return nil |
| 118 | }} |
| 119 | if err := j.judge(t.Context()); err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | if calls != 1 { |
| 123 | t.Fatal("participation check omitted") |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | func TestPlan14FloorAvailableStagedCandidate(t *testing.T) { |
| 128 | low := rootFloorBinary{Version: "v1.0.0", Protocol: 1, Digest: floorTestDigest} |
| 129 | high := low |
| 130 | high.Version = "v9.0.0" |
| 131 | opt := t.TempDir() |
| 132 | staged := filepath.Join(systemRootDir(opt), "staging", "carlos") |
| 133 | present := false |
| 134 | j := rootFloorJudge{current: systemBinaryPath(opt), prev: systemPrevPath(opt), binary: func(path string) (rootFloorBinary, error) { |
| 135 | if path == staged { |
| 136 | if present { |
| 137 | return high, nil |
| 138 | } |
| 139 | return rootFloorBinary{}, os.ErrNotExist |
| 140 | } |
| 141 | return low, nil |
| 142 | }} |
| 143 | bound := &moves.Floor{Min: 1, MinRelease: "v9.0.0"} |
| 144 | if !j.permits(low, bound) { |
| 145 | t.Fatal("no available build must permit recovery") |
| 146 | } |
| 147 | present = true |
| 148 | if j.permits(low, bound) { |
| 149 | t.Fatal("staged certified candidate must preserve floor refusal") |
| 150 | } |
| 151 | } |
| 152 | |