package store import ( "context" "encoding/json" "testing" ) func TestDomainEntrySignedManifest(t *testing.T) { controller, campus, domains := manifestFixture(t) node := manifestIntegrityStore(t) old := buildManifest(t, controller, campus, domains) if old.SchemaVersion != 1 { t.Fatal("legacy version changed") } e := DomainEntrySettings{Layout: "diary", DiaryEnabled: true, DiaryPrefix: "/", BlogPrefix: "/blog"} put := func(id string, e DomainEntrySettings) { t.Helper() raw, _ := json.Marshal(e) if _, err := controller.DB.Exec(`INSERT INTO domain_entry_settings VALUES(?,?) ON CONFLICT(domain_uuid) DO UPDATE SET settings_json=excluded.settings_json`, id, string(raw)); err != nil { t.Fatal(err) } } put(domains[0].UUID, e) put(domains[1].UUID, e) put("unrelated", e) m := buildManifest(t, controller, campus, domains) if m.SchemaVersion != 2 || m.Revision <= old.Revision || len(m.DomainEntries) != 2 { t.Fatalf("bad v2 %+v", m) } if err := applyManifest(node, m); err != nil { t.Fatal(err) } if layout, err := node.GetConfigInDomain(domains[0].UUID, "home_layout"); err != nil || layout != "diary" { t.Fatal(layout, err) } if on, err := node.PeriodicalEnabled(context.Background(), domains[0].UUID, "diary"); err != nil || !on { t.Fatal(on, err) } if prefix, err := node.PeriodicalPrefix(context.Background(), domains[0].UUID, "diary"); err != nil || prefix != "/" { t.Fatal(prefix, err) } if on, err := node.PeriodicalEnabled(context.Background(), domains[1].UUID, "diary"); err != nil || on { t.Fatal("remote projection activated", on, err) } if err := applyManifest(node, m); err != nil { t.Fatal("duplicate", err) } if err := applyManifest(node, old); err == nil { t.Fatal("replay accepted") } e.Layout = "blog" e.BlogEnabled = true put(domains[0].UUID, e) newer := buildManifest(t, controller, campus, domains) if newer.Revision <= m.Revision { t.Fatal("settings change did not advance revision") } if _, err := node.DB.Exec(`CREATE TRIGGER fail_entry BEFORE UPDATE ON periodical_prefixes BEGIN SELECT RAISE(ABORT,'fixture'); END`); err != nil { t.Fatal(err) } if err := applyManifest(node, newer); err == nil { t.Fatal("partial apply") } if layout, _ := node.GetConfigInDomain(domains[0].UUID, "home_layout"); layout != "diary" { t.Fatal("rollback", layout) } var revision int64 if err := node.DB.QueryRow(`SELECT applied_revision FROM federation_manifest_state WHERE campus_uuid=?`, campus.UUID).Scan(&revision); err != nil || revision != m.Revision { t.Fatal("checkpoint advanced", revision, err) } if _, err := node.DB.Exec(`DROP TRIGGER fail_entry`); err != nil { t.Fatal(err) } if err := applyManifest(node, newer); err != nil { t.Fatal(err) } newer.DomainEntries[domains[0].UUID] = DomainEntrySettings{Layout: "diary", DiaryEnabled: true, DiaryPrefix: "/../escape", BlogPrefix: "/blog"} tampered := newer if err := applyManifest(node, tampered); err == nil { t.Fatal("unsigned settings accepted") } newer = signTestManifest(t, controller, newer) if err := applyManifest(node, newer); err == nil { t.Fatal("invalid signed prefix accepted") } }