| 1 | //go:build linux && rootrunner |
| 2 | |
| 3 | package host |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/carlosframework/platform/internal/rootparticipation" |
| 14 | "github.com/carlosframework/platform/internal/rootstate" |
| 15 | ) |
| 16 | |
| 17 | func TestPlan14RootMigrationLedgerExclusionRootRunner(t *testing.T) { |
| 18 | dir := generationRootRunnerDir(t) |
| 19 | state, err := rootstate.Open(filepath.Join(dir, "exec"), true, false) |
| 20 | if err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | defer state.Close() |
| 24 | m, _, _ := migrationFixture(t) |
| 25 | m.execution = state |
| 26 | if err := m.run(t.Context()); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | c, err := (rootparticipation.Ledger{State: state}).Census() |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | c.Revision = 42 |
| 34 | c.LaunchRevision = 17 |
| 35 | raw, _ := json.Marshal(c) |
| 36 | if err := state.Write("census.json", raw); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | if err := m.run(t.Context()); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | after, _ := state.Read("census.json") |
| 43 | if string(raw) != string(after) { |
| 44 | t.Fatal("completed revision reset") |
| 45 | } |
| 46 | unlock, err := state.Lock(t.Context(), "transaction.lock") |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | done := make(chan error, 1) |
| 51 | go func() { done <- m.census(true) }() |
| 52 | select { |
| 53 | case err := <-done: |
| 54 | unlock() |
| 55 | t.Fatal("migration bypassed ledger exclusion", err) |
| 56 | case <-time.After(50 * time.Millisecond): |
| 57 | } |
| 58 | c.Pending = "acquisition" |
| 59 | raw, _ = json.Marshal(c) |
| 60 | if err := state.Write("census.json", raw); err != nil { |
| 61 | unlock() |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | unlock() |
| 65 | select { |
| 66 | case err := <-done: |
| 67 | if err == nil { |
| 68 | t.Fatal("migration erased pending acquisition") |
| 69 | } |
| 70 | case <-time.After(3 * time.Second): |
| 71 | t.Fatal("migration lock stuck") |
| 72 | } |
| 73 | after, _ = state.Read("census.json") |
| 74 | if string(raw) != string(after) { |
| 75 | t.Fatal("pending census changed") |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestPlan14RootMigrationUnarmedRootRunner(t *testing.T) { |
| 80 | opt := generationRootRunnerDir(t) |
| 81 | m, _, _ := migrationFixture(t) |
| 82 | m.guardReady = func(context.Context) error { |
| 83 | return verifyRootMigrationFloors(opt, rootFloorIdentity{"deploy", "box", "en"}) |
| 84 | } |
| 85 | if m.run(t.Context()) == nil { |
| 86 | t.Fatal("missing signed guard accepted") |
| 87 | } |
| 88 | if _, err := m.execution.Read("census.json"); !os.IsNotExist(err) { |
| 89 | t.Fatal("census preceded guard", err) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func TestPlan14ExecutionHostileCensusRootRunner(t *testing.T) { |
| 94 | dir := generationRootRunnerDir(t) |
| 95 | parent := filepath.Join(dir, "authority") |
| 96 | path := filepath.Join(parent, "exec") |
| 97 | s, err := rootstate.Open(path, true, false) |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | c := rootparticipation.Census{Schema: 1, Box: "box", Enrollment: "en", Migration: 1, Revision: 1, LaunchRevision: 1, Complete: true} |
| 102 | raw, _ := json.Marshal(c) |
| 103 | if err := s.Write("census.json", raw); err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | s.Close() |
| 107 | check := func() error { |
| 108 | s, e := rootstate.Open(path, false, false) |
| 109 | if e != nil { |
| 110 | return e |
| 111 | } |
| 112 | defer s.Close() |
| 113 | return (rootparticipation.Ledger{State: s, VerifyLaunches: func(context.Context, uint64) error { return nil }}).Idle(t.Context()) |
| 114 | } |
| 115 | if err := check(); err != nil { |
| 116 | t.Fatal("positive", err) |
| 117 | } |
| 118 | for _, hostile := range []string{parent, filepath.Join(path, "census.json")} { |
| 119 | if err := os.Chown(hostile, 65534, 65534); err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | if check() == nil { |
| 123 | t.Fatal("hostile authority granted idle") |
| 124 | } |
| 125 | if err := os.Chown(hostile, 0, 0); err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | if err := check(); err != nil { |
| 129 | t.Fatal("restored root authority refused", err) |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |