| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/hostguard" |
| 9 | "github.com/carlosframework/platform/internal/release" |
| 10 | "github.com/carlosframework/platform/internal/rootparticipation" |
| 11 | "github.com/carlosframework/platform/internal/rootstate" |
| 12 | ) |
| 13 | |
| 14 | type heldHostStore struct { |
| 15 | rootstate.Store |
| 16 | locks int |
| 17 | } |
| 18 | |
| 19 | func (s *heldHostStore) Lock(context.Context, string) (func(), error) { |
| 20 | s.locks++ |
| 21 | return nil, os.ErrPermission |
| 22 | } |
| 23 | |
| 24 | func TestPlan14LaunchSettingsUnderHostGuard(t *testing.T) { |
| 25 | disk := &rollbackMemory{files: map[string][]byte{}} |
| 26 | in := hostguard.RootInstallation{Host: "shop.example", Incarnation: "inc", Stamp: 2, Account: "bab", App: "app", Record: "record", Box: "box", Enrollment: "en", Migration: 1, InventoryRevision: 1} |
| 27 | if err := hostguard.NewRootAuthority(disk).RetainMigrationDeny(t.Context(), in, 1); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | held := &heldHostStore{Store: disk} |
| 31 | source := rootRestartRequestSource{state: held, census: func() (rootparticipation.Census, error) { |
| 32 | return rootparticipation.Census{Complete: true, Box: "box", Enrollment: "en", Migration: 1}, nil |
| 33 | }} |
| 34 | b := release.GenerationBinding{Binding: release.HistoryBinding{Host: in.Host, Account: in.Account, App: in.App}, Target: release.GenerationOwner{Host: in.Host, Incarnation: in.Incarnation, Stamp: in.Stamp, Box: in.Box, Enrollment: in.Enrollment}, Installation: rootHistoryInstallation(in), Record: in.Record} |
| 35 | if got, err := source.launchInstallation(t.Context(), in.Host, b); err != nil || got.Host != in.Host || held.locks != 0 { |
| 36 | t.Fatal("nested lock or wrong installation", err, held.locks) |
| 37 | } |
| 38 | for _, field := range []string{"host", "stamp", "installation", "account", "record", "box", "enrollment"} { |
| 39 | bad := b |
| 40 | switch field { |
| 41 | case "host": |
| 42 | bad.Target.Host = "other.example" |
| 43 | case "stamp": |
| 44 | bad.Target.Stamp++ |
| 45 | case "installation": |
| 46 | bad.Installation = "foreign" |
| 47 | case "account": |
| 48 | bad.Binding.Account = "foreign" |
| 49 | case "record": |
| 50 | bad.Record = "foreign" |
| 51 | case "box": |
| 52 | bad.Target.Box = "foreign" |
| 53 | case "enrollment": |
| 54 | bad.Target.Enrollment = "foreign" |
| 55 | } |
| 56 | if _, err := source.launchInstallation(t.Context(), in.Host, bad); err == nil { |
| 57 | t.Fatal("accepted changed", field) |
| 58 | } |
| 59 | } |
| 60 | if _, err := source.installation(t.Context(), in.Host); err == nil || held.locks == 0 { |
| 61 | t.Fatal("ordinary lookup stopped taking the host lock", err) |
| 62 | } |
| 63 | } |
| 64 | |