package metadata import ( "context" "crypto/sha256" "database/sql" "encoding/hex" "errors" "net/url" "os" "path/filepath" "strings" "sync" "testing" "time" "github.com/google/uuid" "pageswiki/internal/db" ) func testRepository(t *testing.T, backend Backend) *Repository { t.Helper() var database *sql.DB var err error if backend == SQLite { database, err = db.Open(filepath.Join(t.TempDir(), "metadata.db")) } else { dsn := os.Getenv("PAGESWIKI_TEST_POSTGRES_DSN") if dsn == "" { t.Skip("set PAGESWIKI_TEST_POSTGRES_DSN to a disposable PostgreSQL URL") } u, e := url.Parse(dsn) if e != nil || (u.Scheme != "postgres" && u.Scheme != "postgresql") { t.Fatal("test DSN must be PostgreSQL URL") } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() root, e := db.OpenPostgreSQL(ctx, db.PostgreSQLOptions{DSN: dsn, AllowInsecure: true}) if e != nil { t.Fatal(e) } schema := "pageswiki_metadata_test_" + strings.ReplaceAll(uuid.NewString(), "-", "") if _, e = root.ExecContext(ctx, `CREATE SCHEMA "`+schema+`"`); e != nil { root.Close() t.Fatal("create synthetic test schema failed") } t.Cleanup(func() { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if _, e := root.ExecContext(ctx, `DROP SCHEMA "`+schema+`" CASCADE`); e != nil { t.Error("drop synthetic test schema failed") } root.Close() }) query := u.Query() query.Set("search_path", schema) u.RawQuery = query.Encode() database, err = db.OpenPostgreSQL(ctx, db.PostgreSQLOptions{DSN: u.String(), AllowInsecure: true}) } if err != nil { t.Fatal(err) } t.Cleanup(func() { database.Close() }) r, err := New(database, backend) if err != nil { t.Fatal(err) } if err := r.InitializeFresh(context.Background()); err != nil { t.Fatal(err) } if err := r.InitializeFresh(context.Background()); err != nil { t.Fatal("repeated initialization", err) } return r } func hash(s string) string { v := sha256.Sum256([]byte(s)); return hex.EncodeToString(v[:]) } func TestMetadataContract(t *testing.T) { for _, backend := range []Backend{SQLite, PostgreSQL} { t.Run(string(backend), func(t *testing.T) { r := testRepository(t, backend) ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() mustPage := func(domain, path string) string { t.Helper() id, err := r.CreatePage(ctx, domain, path) if err != nil { t.Fatal(err) } return id } id := mustPage("domain-a", "/学区%_/通り/本") other := mustPage("domain-b", "/学区%_/通り/本") if id == other { t.Fatal("object UUID reused") } if got, err := r.ResolvePath(ctx, "domain-a", "/学区%_/通り/本"); err != nil || got != id { t.Fatal("path resolution", err) } if _, err := r.ResolvePath(ctx, "unknown", "/学区%_/通り/本"); !errors.Is(err, ErrNotFound) { t.Fatal("domain isolation", err) } if _, err := r.CreatePage(ctx, "domain-a", "/学区%_/通り/本"); !errors.Is(err, ErrConflict) { t.Fatal("duplicate path accepted", err) } var count int if err := r.database.QueryRowContext(ctx, `SELECT count(*) FROM objects`).Scan(&count); err != nil || count != 2 { t.Fatal("failed create left orphan object", err, count) } if err := r.BindPath(ctx, "domain-a", "/alias", id); err != nil { t.Fatal(err) } if err := r.BindPath(ctx, "domain-b", "/stolen", id); !errors.Is(err, ErrNotFound) { t.Fatal("foreign alias accepted", err) } mustPage("domain-a", "/学区XX/通り/本") mustPage("domain-a", "/学区%_suffix/本") if err := r.RenameTree(ctx, "domain-a", "/学区%_", "/新しい学区"); err != nil { t.Fatal("virtual folder rename", err) } if got, err := r.ResolvePath(ctx, "domain-a", "/新しい学区/通り/本"); err != nil || got != id { t.Fatal("UUID not preserved", err) } for _, path := range []string{"/学区XX/通り/本", "/学区%_suffix/本"} { if _, err := r.ResolvePath(ctx, "domain-a", path); err != nil { t.Fatal("wildcard/boundary changed unrelated path", err) } } if got, err := r.ResolvePath(ctx, "domain-b", "/学区%_/通り/本"); err != nil || got != other { t.Fatal("rename crossed domain", err) } mustPage("domain-a", "/occupied/something") if err := r.RenameTree(ctx, "domain-a", "/新しい学区", "/occupied"); !errors.Is(err, ErrConflict) { t.Fatal("destination merged", err) } if _, err := r.ResolvePath(ctx, "domain-a", "/新しい学区/通り/本"); err != nil { t.Fatal("failed move changed source", err) } if err := r.RenameTree(ctx, "domain-a", "/新しい学区", "/新しい学区/inside"); !errors.Is(err, ErrInvalid) { t.Fatal("recursive destination accepted") } mustPage("domain-a", "/Case") mustPage("domain-a", "/case") real := mustPage("domain-a", "/real") child := mustPage("domain-a", "/real/child") if err := r.RenameTree(ctx, "domain-a", "/real", "/moved-real"); err != nil { t.Fatal("real folder move", err) } for path, id := range map[string]string{"/moved-real": real, "/moved-real/child": child} { if got, err := r.ResolvePath(ctx, "domain-a", path); err != nil || got != id { t.Fatal("real subtree identity", err) } } v := Revision{ObjectUUID: id, DocumentHash: hash("first"), Language: "ja", ContentType: "text/html", Message: "first"} first, err := r.AppendRevision(ctx, "domain-a", v, "") if err != nil { t.Fatal(err) } if _, err := r.Head(ctx, "domain-b", id, "ja"); !errors.Is(err, ErrNotFound) { t.Fatal("foreign revision readable", err) } if _, err := r.Head(ctx, "domain-a", id, "en"); !errors.Is(err, ErrNotFound) { t.Fatal("language fallback leaked into exact head", err) } if _, err := r.AppendRevision(ctx, "domain-a", v, ""); !errors.Is(err, ErrConflict) { t.Fatal("stale revision accepted", err) } v.Language = "en" if _, err := r.AppendRevision(ctx, "domain-a", v, ""); err != nil { t.Fatal("translation creation", err) } v.Language = "ja" v.DocumentHash = hash("updated") // Independent Repository objects simulate writers that cannot share a Go mutex. wins := concurrent(t, 6, func() error { peer, _ := New(r.database, backend) _, err := peer.AppendRevision(ctx, "domain-a", v, first.UUID) return err }) if wins != 1 { t.Fatal("CAS winners", wins) } head, err := r.Head(ctx, "domain-a", id, "ja") if err != nil || head.UUID == first.UUID || head.CreatedAtMS <= first.CreatedAtMS { t.Fatal("head ordering", err) } var originalHash string if err := r.database.QueryRowContext(ctx, `SELECT document_hash FROM revisions WHERE uuid=`+r.p(1), first.UUID).Scan(&originalHash); err != nil || originalHash != first.DocumentHash { t.Fatal("immutable revision changed", err) } if _, err := r.GetRevision(ctx, "domain-b", first.UUID); !errors.Is(err, ErrNotFound) { t.Fatal("foreign revision UUID readable", err) } restored, err := r.RestoreRevision(ctx, "domain-a", first.UUID, head.UUID, "restore old document") if err != nil || restored.UUID == first.UUID || restored.DocumentHash != first.DocumentHash || restored.CreatedAtMS <= head.CreatedAtMS { t.Fatal("restore did not append immutable revision", err) } if _, err := r.RestoreRevision(ctx, "domain-a", first.UUID, head.UUID, "stale restore"); !errors.Is(err, ErrConflict) { t.Fatal("restore ignored head conflict", err) } a := Asset{DomainUUID: "domain-a", Address: "asset:images:cover.png", HeadDocumentHash: hash("image1"), ContentType: "image/png", SizeBytes: 10} initial, err := r.PutAsset(ctx, a, "", "actor") if err != nil { t.Fatal(err) } if _, err := r.GetAsset(ctx, "domain-b", a.Address); !errors.Is(err, ErrNotFound) { t.Fatal("foreign asset readable", err) } a.HeadDocumentHash = hash("image2") if wins := concurrent(t, 6, func() error { peer, _ := New(r.database, backend) _, err := peer.PutAsset(ctx, a, initial.HeadDocumentHash, "actor") return err }); wins != 1 { t.Fatal("asset CAS winners", wins) } current, err := r.GetAsset(ctx, "domain-a", a.Address) if err != nil || current.ObjectUUID != initial.ObjectUUID || current.UpdatedAtMS <= initial.UpdatedAtMS { t.Fatal("asset identity/order", err) } if err := r.database.QueryRowContext(ctx, `SELECT count(*) FROM asset_head_history`).Scan(&count); err != nil || count != 2 { t.Fatal("history not atomic", err, count) } a.DomainUUID = "domain-b" if _, err := r.PutAsset(ctx, a, "", "actor"); err != nil { t.Fatal("asset namespace not domain-scoped", err) } if _, err := r.AppendRevision(ctx, "domain-a", Revision{ObjectUUID: id, DocumentHash: "bad", ContentType: "text/html"}, head.UUID); !errors.Is(err, ErrInvalid) { t.Fatal("invalid hash accepted", err) } canceled, stop := context.WithCancel(ctx) stop() if _, err := r.CreatePage(canceled, "domain-a", "/canceled"); !errors.Is(err, context.Canceled) { t.Fatal("cancellation lost", err) } }) } } func TestMetadataInputValidation(t *testing.T) { for _, path := range []string{"", "relative", "//", "///", "/a//b", "/a/../b", "/a/./b", "/a\x00b", "/a\nb", "/a\xffb"} { if _, err := canonicalPath(path); !errors.Is(err, ErrInvalid) { t.Fatalf("invalid path accepted: %q", path) } } for _, path := range []string{"/", "/日本語/📚", "/name%_", "/a/b/"} { if _, err := canonicalPath(path); err != nil { t.Fatalf("valid path rejected: %q", path) } } if _, err := New(nil, SQLite); !errors.Is(err, ErrInvalid) { t.Fatal("nil database accepted") } // Do not expose data carried by a wrapped cancellation error. r := Repository{} err := r.failure(errors.Join(errors.New("secret"), context.Canceled)) if !errors.Is(err, context.Canceled) || strings.Contains(err.Error(), "secret") { t.Fatal("unsafe cancellation error") } } func concurrent(t *testing.T, n int, fn func() error) int { t.Helper() var wg sync.WaitGroup results := make(chan error, n) for i := 0; i < n; i++ { wg.Add(1) go func() { defer wg.Done(); results <- fn() }() } wg.Wait() close(results) wins := 0 for err := range results { if err == nil { wins++ } else if !errors.Is(err, ErrConflict) { t.Fatal("unexpected concurrent write error", err) } } return wins } func TestMetadataRefusesLegacyAdoption(t *testing.T) { for _, backend := range []Backend{SQLite, PostgreSQL} { t.Run(string(backend), func(t *testing.T) { r := testRepository(t, backend) ctx := context.Background() if _, err := r.database.ExecContext(ctx, `DELETE FROM schema_migration_steps WHERE component='core-metadata'`); err != nil { t.Fatal(err) } if _, err := r.CreatePage(ctx, "test", "/preserved"); err != nil { t.Fatal(err) } if err := r.InitializeFresh(ctx); err == nil { t.Fatal("unversioned schema silently adopted") } if _, err := r.ResolvePath(ctx, "test", "/preserved"); err != nil { t.Fatal("existing data changed", err) } }) } } func TestMetadataAssetHistoryFailureIsAtomic(t *testing.T) { for _, backend := range []Backend{SQLite, PostgreSQL} { t.Run(string(backend), func(t *testing.T) { r := testRepository(t, backend) ctx := context.Background() a := Asset{DomainUUID: "test", Address: "asset:images:original.png", HeadDocumentHash: hash("original"), ContentType: "image/png", SizeBytes: 8} original, err := r.PutAsset(ctx, a, "", "actor") if err != nil { t.Fatal(err) } // Force a history write failure after the asset HEAD write. The // synthetic unique index is solely fault injection, not the real schema. if _, err := r.database.ExecContext(ctx, `CREATE UNIQUE INDEX synthetic_history_failure ON asset_head_history(domain_uuid)`); err != nil { t.Fatal(err) } a.HeadDocumentHash = hash("changed") if _, err := r.PutAsset(ctx, a, original.HeadDocumentHash, "actor"); !errors.Is(err, ErrConflict) { t.Fatal("injected history failure ignored", err) } got, err := r.GetAsset(ctx, "test", a.Address) if err != nil || got.HeadDocumentHash != original.HeadDocumentHash || got.UpdatedAtMS != original.UpdatedAtMS { t.Fatal("partial HEAD update", err) } a.Address = "asset:images:new.png" if _, err := r.PutAsset(ctx, a, "", "actor"); !errors.Is(err, ErrConflict) { t.Fatal("injected creation failure ignored", err) } if _, err := r.GetAsset(ctx, "test", a.Address); !errors.Is(err, ErrNotFound) { t.Fatal("partial asset created", err) } for _, table := range []string{"objects", "object_origins", "assets", "asset_head_history"} { var count int if err := r.database.QueryRowContext(ctx, `SELECT count(*) FROM `+table).Scan(&count); err != nil || count != 1 { t.Fatal("rollback failed", table, count, err) } } }) } } func TestMetadataDetectsMissingColumnWithoutRepair(t *testing.T) { for _, backend := range []Backend{SQLite, PostgreSQL} { t.Run(string(backend), func(t *testing.T) { r := testRepository(t, backend) ctx := context.Background() if _, err := r.database.ExecContext(ctx, `ALTER TABLE revisions RENAME COLUMN filename TO misplaced_filename`); err != nil { t.Fatal(err) } if err := r.InitializeFresh(ctx); err == nil { t.Fatal("missing column ignored") } var value string if err := r.database.QueryRowContext(ctx, `SELECT misplaced_filename FROM revisions LIMIT 1`).Scan(&value); !errors.Is(err, sql.ErrNoRows) { t.Fatal("drift silently repaired", err) } }) } }