package hostbox import ( "context" "github.com/carlosframework/platform/internal/hostclaim" "github.com/carlosframework/platform/internal/registry" "os" "path/filepath" "testing" ) func TestEmptyCensusRefusesUnclassifiedArtifactsAndNeverWritesInPrecheck(t *testing.T) { ctx := context.Background() base := t.TempDir() db, err := OpenStopInventoryDB(filepath.Join(base, "inventory.db")) if err != nil { t.Fatal(err) } defer db.Close() reg, err := registry.Open(filepath.Join(base, "registry.db")) if err != nil { t.Fatal(err) } defer reg.Close() launches, err := OpenLaunchJournal(filepath.Join(base, "launch.db")) if err != nil { t.Fatal(err) } defer launches.Close() roots := map[string]string{} for _, name := range []string{"data", "opt", "units"} { roots[name] = filepath.Join(base, name) if err = os.Mkdir(roots[name], 0700); err != nil { t.Fatal(err) } } c := EmptyInstallationCensus{Inventory: db, Registry: reg, Launches: launches, Box: "box", Enrollment: "enrollment", DirectoryRevision: "revision", Roots: roots} obs := hostclaim.Observed{Host: "shop.example", Claim: &hostclaim.Claim{Holder: &hostclaim.Holder{Incarnation: "inc", Stamp: 1}}} selected := RecordSelection{Host: obs.Host, Incarnation: "inc"} if err = c.CheckLocal(ctx, obs, selected); err != nil { t.Fatal(err) } if _, err = db.ReadInstallation(ctx, obs.Host, "inc", 1); err == nil { t.Fatal("precheck wrote inventory") } if err = os.WriteFile(filepath.Join(roots["opt"], "unexplained"), []byte("old"), 0600); err != nil { t.Fatal(err) } if err = c.Initialize(ctx, obs, selected); err == nil { t.Fatal("assigned unknown artifact to new installation") } if err = os.Remove(filepath.Join(roots["opt"], "unexplained")); err != nil { t.Fatal(err) } if err = c.Initialize(ctx, obs, selected); err != nil { t.Fatal(err) } in, err := db.ReadInstallation(ctx, obs.Host, "inc", 1) if err != nil || !in.Complete || len(in.Entries) != 0 { t.Fatalf("missing explicit empty census: %+v %v", in, err) } obs.Claim.Holder.Stamp = 2 if err = c.Initialize(ctx, obs, selected); err == nil { t.Fatal("old ownership silently reassigned") } }