carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1//go:build unix
2
3package hostguard
4
5import (
6 "context"
7 "errors"
8 "testing"
9 "time"
10)
11
12func TestPlan14MigrationDenyCoversRootAndAgent(t *testing.T) {
13 f, target, captured := fenceFixture(t)
14 root := NewRootAuthority(&rootTestStore{dir: t.TempDir()})
15 f.g.Root = root
16 f.obs.Claim.Holder.Account, f.obs.Claim.Holder.App = "bab", "app"
17 in := RootInstallation{Host: target.Host, Incarnation: captured.Incarnation, Stamp: captured.Stamp, Account: "bab", App: "app", Record: "record", Box: target.Box, Enrollment: target.Enrollment, Migration: 1}
18 if err := f.g.EstablishRoot(t.Context(), in, allow); err != nil {
19 t.Fatal(err)
20 }
21 if err := f.g.With(t.Context(), target.Host, captured, allow, func() error { return nil }); err != nil {
22 t.Fatal("canonical", err)
23 }
24 if err := root.RetainMigrationDeny(t.Context(), in, target.Session); err != nil {
25 t.Fatal(err)
26 }
27 if err := f.g.With(t.Context(), target.Host, captured, allow, func() error { t.Fatal("old session ran"); return nil }); !errors.Is(err, ErrFenced) {
28 t.Fatal(err)
29 }
30 if err := f.g.EstablishRoot(t.Context(), in, allow); !errors.Is(err, ErrFenced) {
31 t.Fatal("re-establishment bypass", err)
32 }
33 view, err := root.View(t.Context(), target.Host, captured)
34 if err != nil {
35 t.Fatal(err)
36 }
37 if err := view.admit(target.Host, captured, target.Session); !errors.Is(err, ErrFenced) {
38 t.Fatal("agent view bypass", err)
39 }
40 target, err = f.g.BeginFence(t.Context(), target)
41 if err != nil {
42 t.Fatal(err)
43 }
44 if err = f.g.CompleteFence(t.Context(), target, []byte("verified predecessor"), time.Now(), func(context.Context, FenceTarget, []byte, time.Time) error { return nil }); err != nil {
45 t.Fatal(err)
46 }
47 view, err = root.View(t.Context(), target.Host, captured)
48 if err != nil {
49 t.Fatal(err)
50 }
51 f.obs.Claim.Holder.Session++
52 if err := f.g.With(t.Context(), target.Host, captured, allow, func() error { return nil }); err != nil {
53 t.Fatal("fresh authoritative session denied", err)
54 }
55 if err := view.admit(target.Host, captured, target.Session+1); err != nil {
56 t.Fatal("new session view", err)
57 }
58 if err := root.RetainMigrationDeny(t.Context(), in, target.Session-1); err != nil {
59 t.Fatal(err)
60 }
61 again, err := root.View(t.Context(), target.Host, captured)
62 if err != nil || again.MigrationDenies[0].Session != target.Session {
63 t.Fatal("deny regressed", again, err)
64 }
65}
66
67func TestPlan14MigrationReadmissionRequiresCompletedPredecessor(t *testing.T) {
68 f, target, captured := fenceFixture(t)
69 root := NewRootAuthority(&rootTestStore{dir: t.TempDir()})
70 f.g.Root = root
71 f.obs.Claim.Holder.Account, f.obs.Claim.Holder.App = "bab", "app"
72 in := RootInstallation{Host: target.Host, Incarnation: captured.Incarnation, Stamp: captured.Stamp, Account: "bab", App: "app", Record: "record", Box: target.Box, Enrollment: target.Enrollment, Migration: 1}
73 if err := root.RetainMigrationDeny(t.Context(), in, target.Session); err != nil {
74 t.Fatal(err)
75 }
76 f.obs.Claim.Holder.Session++
77 if f.g.With(t.Context(), target.Host, captured, allow, func() error { return nil }) == nil {
78 t.Fatal("session advance bypassed predecessor stop")
79 }
80 f.obs.Claim.Holder.Session--
81 target, err := f.g.BeginFence(t.Context(), target)
82 if err != nil {
83 t.Fatal(err)
84 }
85 if err := f.g.CompleteFence(t.Context(), target, []byte("verified exact root stop"), time.Now(), func(context.Context, FenceTarget, []byte, time.Time) error { return nil }); err != nil {
86 t.Fatal(err)
87 }
88 f.obs.Claim.Holder.Session++
89 effects := 0
90 if err := f.g.With(t.Context(), target.Host, captured, allow, func() error { effects++; return nil }); err != nil || effects != 1 {
91 t.Fatal("completed predecessor", err, effects)
92 }
93 f.obs.Claim.Holder.Session--
94 if f.g.With(t.Context(), target.Host, captured, allow, func() error { t.Fatal("old session ran"); return nil }) == nil {
95 t.Fatal("deny lost")
96 }
97}
98
99func TestPlan14MigrationInventoryRevisionAdvance(t *testing.T) {
100 f, target, captured := fenceFixture(t)
101 root := NewRootAuthority(&rootTestStore{dir: t.TempDir()})
102 f.g.Root = root
103 f.obs.Claim.Holder.Account, f.obs.Claim.Holder.App = "bab", "app"
104 in := RootInstallation{Host: target.Host, Incarnation: captured.Incarnation, Stamp: captured.Stamp, Account: "bab", App: "app", Record: "record", Box: target.Box, Enrollment: target.Enrollment, Migration: 1}
105 if err := root.RetainMigrationDeny(t.Context(), in, target.Session); err != nil {
106 t.Fatal(err)
107 }
108 artifacts := InstallationArtifacts{Schema: 1, Timers: "refresh"}
109 if root.CompleteMigrationInventory(t.Context(), target, 4, artifacts) == nil {
110 t.Fatal("revision without stopped receipt")
111 }
112 var err error
113 target, err = f.g.BeginFence(t.Context(), target)
114 if err != nil {
115 t.Fatal(err)
116 }
117 if err := f.g.CompleteFence(t.Context(), target, []byte("verified predecessor"), time.Now(), func(context.Context, FenceTarget, []byte, time.Time) error { return nil }); err != nil {
118 t.Fatal(err)
119 }
120 if err := root.CompleteMigrationInventory(t.Context(), target, 4, artifacts); err != nil {
121 t.Fatal(err)
122 }
123 for _, change := range [][2]uint64{{0, 1}, {3, 5}, {4, 6}, {5, 6}, {^uint64(0), 0}} {
124 if root.CompleteMigrationInventory(t.Context(), target, change[1], artifacts, change[0]) == nil {
125 t.Fatal("unbound revision advance", change)
126 }
127 }
128 if err := root.CompleteMigrationInventory(t.Context(), target, 5, artifacts, 4); err != nil {
129 t.Fatal("exact successor", err)
130 }
131 if err := root.CompleteMigrationInventory(t.Context(), target, 5, artifacts, 4); err != nil {
132 t.Fatal("retry", err)
133 }
134 if root.CompleteMigrationInventory(t.Context(), target, 4, artifacts) == nil {
135 t.Fatal("revision regressed")
136 }
137 view, err := root.View(t.Context(), target.Host, captured)
138 if err != nil || view.Installation.InventoryRevision != 5 || len(view.MigrationDenies) != 1 || view.MigrationDenies[0].Session != target.Session {
139 t.Fatal(view, err)
140 }
141}
142