| 1 | //go:build linux |
| 2 | |
| 3 | package host |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/carlosframework/platform/internal/hostbox" |
| 11 | "github.com/carlosframework/platform/internal/rootstate" |
| 12 | "golang.org/x/sys/unix" |
| 13 | ) |
| 14 | |
| 15 | func TestPlan14InventoryCapturePhysicalTypes(t *testing.T) { |
| 16 | root := t.TempDir() |
| 17 | path := filepath.Join(root, "payload") |
| 18 | if err := unix.Mkfifo(path, 0600); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | if _, err := hostbox.CaptureInventoryIdentity(root, "payload"); err == nil { |
| 22 | t.Fatal("captured fifo as inventory payload") |
| 23 | } |
| 24 | if err := os.Remove(path); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | if err := os.WriteFile(path, []byte("payload"), 0600); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if err := os.Link(path, filepath.Join(root, "alias")); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | if _, err := hostbox.CaptureInventoryIdentity(root, "payload"); err == nil { |
| 34 | t.Fatal("captured multiply-linked inventory payload") |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestPlan14InventoryCaptureCannotClaimOldBoot(t *testing.T) { |
| 39 | root := t.TempDir() |
| 40 | if err := os.Mkdir(filepath.Join(root, "state"), 0700); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | id := inventoryIdentityFixture(t, root, "state") |
| 44 | e := hostbox.StopInventoryEntry{ArtifactID: "state", Kind: "path", ManagedRoot: "data", RelativePath: "state", Materialization: "materialized", Identity: id, Device: id.Device, Inode: id.Inode, Type: id.Mode & 0170000} |
| 45 | h := ClaimRootHTTP{Roots: map[string]string{"data": root}} |
| 46 | for _, prior := range []hostbox.StopInventory{{}, {Entries: []hostbox.StopInventoryEntry{{ArtifactID: "state", Materialization: "intended"}}}} { |
| 47 | if err := h.verifyNewInventoryIdentities(prior, hostbox.StopInventory{Entries: []hostbox.StopInventoryEntry{e}}); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | forged := e |
| 51 | forged.Identity.Boot = "earlier-boot" |
| 52 | if err := h.verifyNewInventoryIdentities(prior, hostbox.StopInventory{Entries: []hostbox.StopInventoryEntry{forged}}); err == nil { |
| 53 | t.Fatal("accepted caller's cross-boot capture") |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func inventoryIdentityFixture(t *testing.T, root, relative string) rootstate.FileIdentity { |
| 59 | t.Helper() |
| 60 | id, err := hostbox.CaptureInventoryIdentity(root, relative) |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | return id |
| 65 | } |
| 66 | |