| 1 | //go:build linux |
| 2 | |
| 3 | package hostbox |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/carlosframework/platform/internal/registry" |
| 12 | ) |
| 13 | |
| 14 | func TestPlan9Fix4InventoryRefusesFlatRestartMarksArtifact(t *testing.T) { |
| 15 | data := t.TempDir() |
| 16 | host := "restart-marks.json" |
| 17 | owner := "owner" |
| 18 | victim := filepath.Join(data, host) |
| 19 | if err := os.WriteFile(victim, []byte("root-written restart state"), 0600); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | parker := TypedParker{Roots: map[string]string{"data": data}, ParkedRoot: data, Incarnation: "inc", Host: host, Retirement: "capture"} |
| 23 | artifact, err := parker.CaptureArtifact("data", host, owner, ArtifactPath) |
| 24 | if err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | inventory, err := OpenStopInventoryDB(filepath.Join(t.TempDir(), "inventory.db")) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | t.Cleanup(func() { inventory.Close() }) |
| 32 | in := StopInventory{Complete: true, Host: host, Incarnation: "inc", InstallationStamp: 1, Box: "box", Enrollment: "enrollment", DirectoryRevision: "1", Entries: []StopInventoryEntry{{ |
| 33 | Kind: "path", ArtifactID: "restart-marks", ManagedRoot: "data", RelativePath: artifact.Name, Ownership: artifact.Owner, |
| 34 | Materialization: "materialized", Identity: artifact.Identity, Device: artifact.Dev, Inode: artifact.Ino, Type: artifact.Type, |
| 35 | }}} |
| 36 | if err = inventory.WriteStopInventory(t.Context(), in); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | in, err = inventory.ReadInstallation(t.Context(), host, "inc", 1) |
| 40 | if err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | row := registry.Route{Host: host, DBPath: filepath.Join(data, host+".db"), ClaimOwnership: registry.ClaimOwnership{ClaimIncarnation: "inc", ClaimStamp: 1, ClaimOwner: owner, ClaimRowID: "row"}} |
| 44 | if err = ValidateAgentInventory(in, &row, data); !errors.Is(err, ErrTypedInventory) { |
| 45 | t.Fatalf("flat inventory accepted root restart marks with captured identity: %v", err) |
| 46 | } |
| 47 | if body, err := os.ReadFile(victim); err != nil || string(body) != "root-written restart state" { |
| 48 | t.Fatalf("refusal changed restart marks: %q %v", body, err) |
| 49 | } |
| 50 | } |
| 51 | |