//go:build linux package host import ( "os" "path/filepath" "testing" "github.com/carlosframework/platform/internal/hostbox" "github.com/carlosframework/platform/internal/rootstate" "golang.org/x/sys/unix" ) func TestPlan14InventoryCapturePhysicalTypes(t *testing.T) { root := t.TempDir() path := filepath.Join(root, "payload") if err := unix.Mkfifo(path, 0600); err != nil { t.Fatal(err) } if _, err := hostbox.CaptureInventoryIdentity(root, "payload"); err == nil { t.Fatal("captured fifo as inventory payload") } if err := os.Remove(path); err != nil { t.Fatal(err) } if err := os.WriteFile(path, []byte("payload"), 0600); err != nil { t.Fatal(err) } if err := os.Link(path, filepath.Join(root, "alias")); err != nil { t.Fatal(err) } if _, err := hostbox.CaptureInventoryIdentity(root, "payload"); err == nil { t.Fatal("captured multiply-linked inventory payload") } } func TestPlan14InventoryCaptureCannotClaimOldBoot(t *testing.T) { root := t.TempDir() if err := os.Mkdir(filepath.Join(root, "state"), 0700); err != nil { t.Fatal(err) } id := inventoryIdentityFixture(t, root, "state") 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} h := ClaimRootHTTP{Roots: map[string]string{"data": root}} for _, prior := range []hostbox.StopInventory{{}, {Entries: []hostbox.StopInventoryEntry{{ArtifactID: "state", Materialization: "intended"}}}} { if err := h.verifyNewInventoryIdentities(prior, hostbox.StopInventory{Entries: []hostbox.StopInventoryEntry{e}}); err != nil { t.Fatal(err) } forged := e forged.Identity.Boot = "earlier-boot" if err := h.verifyNewInventoryIdentities(prior, hostbox.StopInventory{Entries: []hostbox.StopInventoryEntry{forged}}); err == nil { t.Fatal("accepted caller's cross-boot capture") } } } func inventoryIdentityFixture(t *testing.T, root, relative string) rootstate.FileIdentity { t.Helper() id, err := hostbox.CaptureInventoryIdentity(root, relative) if err != nil { t.Fatal(err) } return id }