241 lines
9.6 KiB
Go
241 lines
9.6 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func opmlDoc(outlines string) string {
|
|
return `<?xml version="1.0"?><opml version="2.0"><head><title>Test</title></head><body>` + outlines + `</body></opml>`
|
|
}
|
|
func opmlEntry(name, link string) string {
|
|
return `<outline text="` + name + `" type="rss" xmlUrl="` + link + `"/>`
|
|
}
|
|
|
|
func TestFeedOPMLParserAndExport(t *testing.T) {
|
|
for _, version := range []string{"1.0", "1.1", "2.0"} {
|
|
raw := strings.Replace(opmlDoc(`<outline text="技術"><outline text="Go">`+opmlEntry("A & B", "https://example.org/feed?a=1&b=2")+`</outline></outline>`), `version="2.0"`, `version="`+version+`"`, 1)
|
|
rows, err := parseFeedOPML(raw)
|
|
if err != nil || len(rows) != 1 || rows[0].Title != "A & B" || rows[0].Folder != "技術 / Go" || rows[0].URL != "https://example.org/feed?a=1&b=2" || rows[0].Status != "add" {
|
|
t.Fatal(rows, err)
|
|
}
|
|
}
|
|
for _, bad := range []string{
|
|
strings.Replace(opmlDoc(""), `version="1.0"?>`, `version="1.0" encoding="iso-8859-1"?>`, 1),
|
|
`<!DOCTYPE opml [<!ENTITY external SYSTEM "file:///etc/passwd">]>` + opmlDoc(""),
|
|
opmlDoc("") + opmlDoc(""), opmlDoc("") + "garbage", `<opml version="2.0"><body/></opml>`,
|
|
opmlDoc(`<outline text="x" text="y"/>`), opmlDoc(strings.Repeat(`<outline text="deep">`, 20) + strings.Repeat(`</outline>`, 20)),
|
|
opmlDoc(strings.Repeat(opmlEntry("x", "https://example.org"), 501)), strings.Repeat("x", feedOPMLMaxBytes+1),
|
|
opmlDoc(`<unexpected>` + opmlEntry("x", "https://example.org") + `</unexpected>`),
|
|
} {
|
|
if _, err := parseFeedOPML(bad); err == nil {
|
|
t.Fatal("accepted invalid OPML")
|
|
}
|
|
}
|
|
sources := []store.FeedSource{{Provider: "local", Title: "A & B", Folder: "技術 / Go", URL: "https://example.org/feed?a=1&b=2"}, {Provider: "local", Title: "Paused", Enabled: false, URL: "https://example.org/paused"}, {Provider: "freshrss", Title: "Remote", URL: "https://private.example.org/api/greader.php", Credential: "secret"}}
|
|
raw, err := encodeFeedOPML(sources)
|
|
if err != nil || strings.Contains(string(raw), "private.example.org") || strings.Contains(string(raw), "secret") {
|
|
t.Fatal("export leak", err)
|
|
}
|
|
rows, err := parseFeedOPML(string(raw))
|
|
if err != nil || len(rows) != 2 || rows[0].Folder != "技術 / Go" || rows[0].Title != "A & B" {
|
|
t.Fatal("roundtrip", string(raw), rows, err)
|
|
}
|
|
raw, err = encodeFeedOPML(nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rows, err = parseFeedOPML(string(raw)); err != nil || len(rows) != 0 {
|
|
t.Fatal("empty export", string(raw), err)
|
|
}
|
|
}
|
|
|
|
type opmlPreview struct {
|
|
Rows []feedOPMLRow `json:"rows"`
|
|
Expected string `json:"expected"`
|
|
Available int `json:"available"`
|
|
}
|
|
|
|
func previewOPML(t *testing.T, s *Server, token, raw string) opmlPreview {
|
|
t.Helper()
|
|
w := feedRequestTest(s, token, "POST", "opml/preview", map[string]string{"opml": raw})
|
|
var preview opmlPreview
|
|
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &preview) != nil {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
return preview
|
|
}
|
|
|
|
func TestFeedOPMLPreviewImportReplayAndOwnership(t *testing.T) {
|
|
s, token, domain, actor, source := feedFixture(t)
|
|
raw := opmlDoc(`<outline text="日本語">` + opmlEntry("New", "https://example.org/new") + opmlEntry("Existing", "https://EXAMPLE.org:443/feed.xml") + opmlEntry("Duplicate", "https://example.org/new") + opmlEntry("Private", "https://127.0.0.1/feed") + `<outline text="Skipped" isComment="true">` + opmlEntry("Hidden", "https://example.org/hidden") + `</outline><outline type="include" text="Include" url="https://example.org/external.opml"/>` + opmlEntry("", "https://example.org/empty-title") + `</outline>`)
|
|
p := previewOPML(t, s, token, raw)
|
|
if p.Available != 31 || len(p.Rows) != 7 {
|
|
t.Fatal(p)
|
|
}
|
|
for i, want := range []string{"add", "existing", "duplicate", "unsupported_url", "commented", "unsupported_type", "invalid_fields"} {
|
|
if p.Rows[i].Status != want {
|
|
t.Fatal(i, p.Rows[i])
|
|
}
|
|
}
|
|
all, _ := s.store.FeedSources(context.Background(), domain, actor)
|
|
if len(all) != 1 {
|
|
t.Fatal("preview wrote sources")
|
|
}
|
|
for _, index := range []int{2, 3, 4, 5, 6} {
|
|
w := feedRequestTest(s, token, "POST", "opml/import", map[string]any{"opml": raw, "expected": p.Expected, "selected": []int{index}, "request_id": uuid.NewString(), "interval_seconds": 1800})
|
|
if w.Code != 422 {
|
|
t.Fatal("ineligible row accepted", index, w.Code)
|
|
}
|
|
}
|
|
input := map[string]any{"opml": raw, "expected": p.Expected, "selected": []int{0}, "request_id": uuid.NewString(), "enabled": false, "interval_seconds": 1800}
|
|
w := feedRequestTest(s, token, "POST", "opml/import", input)
|
|
if w.Code != 201 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
original := w.Body.String()
|
|
if retry := feedRequestTest(s, token, "POST", "opml/import", input); retry.Code != 201 || retry.Body.String() != original {
|
|
t.Fatal("retry", retry.Code, retry.Body)
|
|
}
|
|
input["enabled"] = true
|
|
if retry := feedRequestTest(s, token, "POST", "opml/import", input); retry.Code != 412 {
|
|
t.Fatal("changed retry", retry.Code)
|
|
}
|
|
input["enabled"] = false
|
|
all, _ = s.store.FeedSources(context.Background(), domain, actor)
|
|
if len(all) != 2 {
|
|
t.Fatal("duplicate import", len(all))
|
|
}
|
|
for _, v := range all {
|
|
if v.UUID != source.UUID {
|
|
if v.Enabled || v.Title != "New" || v.Folder != "日本語" {
|
|
t.Fatal(v)
|
|
}
|
|
if err := s.store.FeedSourceAction(context.Background(), domain, actor, v.UUID, "delete", v.Version); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
if retry := feedRequestTest(s, token, "POST", "opml/import", input); retry.Code != 201 || retry.Body.String() != original {
|
|
t.Fatal("deleted retry", retry.Code, retry.Body)
|
|
}
|
|
all, _ = s.store.FeedSources(context.Background(), domain, actor)
|
|
if len(all) != 1 {
|
|
t.Fatal("resurrected source")
|
|
}
|
|
w = feedRequestTest(s, token, "GET", "opml/export", nil)
|
|
if w.Code != 200 || !strings.Contains(w.Header().Get("Content-Disposition"), "attachment") || !strings.Contains(w.Header().Get("Cache-Control"), "no-store") {
|
|
t.Fatal(w.Code, w.Header())
|
|
}
|
|
other, err := s.store.CreatePasswordAccount(domain, "opmlother", "long test password", "Other", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
otherToken, err := s.store.CreateAuthSession(domain, other.UUID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w = feedRequestTest(s, otherToken, "GET", "opml/export", nil)
|
|
if w.Code != 200 || strings.Contains(w.Body.String(), source.URL) {
|
|
t.Fatal("owner leak", w.Code, w.Body)
|
|
}
|
|
if w = feedRequestTest(s, "", "GET", "opml/export", nil); w.Code == 200 {
|
|
t.Fatal("anonymous export")
|
|
}
|
|
if err = s.store.SetPrincipalState(domain, actor, "readonly"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
token, err = s.store.CreateAuthSession(domain, actor)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w = feedRequestTest(s, token, "GET", "opml/export", nil); w.Code != 200 {
|
|
t.Fatal("readonly export", w.Code)
|
|
}
|
|
if w = feedRequestTest(s, token, "POST", "opml/import", input); w.Code != 403 {
|
|
t.Fatal("readonly mutation", w.Code)
|
|
}
|
|
if err = s.setSystemExtensionEnabled(domain, "organizer.feeds", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w = feedRequestTest(s, token, "GET", "opml/export", nil); w.Code != 404 {
|
|
t.Fatal("disabled export", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestFeedOPMLAtomicityLimitsAndConcurrentApply(t *testing.T) {
|
|
s, token, domain, actor, source := feedFixture(t)
|
|
raw := opmlDoc(opmlEntry("A", "https://example.org/a") + opmlEntry("B", "https://example.org/b"))
|
|
p := previewOPML(t, s, token, raw)
|
|
input := map[string]any{"opml": raw, "expected": p.Expected, "selected": []int{0, 1}, "request_id": uuid.NewString(), "interval_seconds": 1800}
|
|
if _, err := s.store.DB.Exec(`CREATE TRIGGER fail_feed_import BEFORE INSERT ON feed_import_receipts BEGIN SELECT RAISE(ABORT,'synthetic failure'); END`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w := feedRequestTest(s, token, "POST", "opml/import", input); w.Code != 500 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
all, _ := s.store.FeedSources(context.Background(), domain, actor)
|
|
if len(all) != 1 {
|
|
t.Fatal("partial source insert")
|
|
}
|
|
if _, err := s.store.DB.Exec(`DROP TRIGGER fail_feed_import`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// A configuration edit invalidates the preview; neither new source is applied.
|
|
source.Title = "Changed"
|
|
if _, err := s.store.SaveFeedSource(context.Background(), domain, actor, source); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w := feedRequestTest(s, token, "POST", "opml/import", input); w.Code != 412 {
|
|
t.Fatal("stale preview", w.Code)
|
|
}
|
|
p = previewOPML(t, s, token, raw)
|
|
input["expected"] = p.Expected
|
|
var wg sync.WaitGroup
|
|
codes := make(chan int, 2)
|
|
for range 2 {
|
|
wg.Add(1)
|
|
go func() { defer wg.Done(); codes <- feedRequestTest(s, token, "POST", "opml/import", input).Code }()
|
|
}
|
|
wg.Wait()
|
|
close(codes)
|
|
for code := range codes {
|
|
if code != 201 {
|
|
t.Fatal("concurrent retry", code)
|
|
}
|
|
}
|
|
all, _ = s.store.FeedSources(context.Background(), domain, actor)
|
|
if len(all) != 3 {
|
|
t.Fatal("duplicate concurrent import")
|
|
}
|
|
entries := ""
|
|
selected := []int{}
|
|
for i := 0; i < 32; i++ {
|
|
entries += opmlEntry(fmt.Sprint(i), fmt.Sprintf("https://example.org/large/%d", i))
|
|
selected = append(selected, i)
|
|
}
|
|
raw = opmlDoc(entries)
|
|
p = previewOPML(t, s, token, raw)
|
|
input = map[string]any{"opml": raw, "expected": p.Expected, "selected": selected, "request_id": uuid.NewString(), "interval_seconds": 1800}
|
|
if w := feedRequestTest(s, token, "POST", "opml/import", input); w.Code != 507 {
|
|
t.Fatal("capacity", w.Code, w.Body)
|
|
}
|
|
all, _ = s.store.FeedSources(context.Background(), domain, actor)
|
|
if len(all) != 3 {
|
|
t.Fatal("partial limit import")
|
|
}
|
|
for _, selection := range [][]int{{-1}, {99}, {0, 0}, {}} {
|
|
input["selected"] = selection
|
|
if w := feedRequestTest(s, token, "POST", "opml/import", input); w.Code != 422 {
|
|
t.Fatal("invalid selection", selection, w.Code)
|
|
}
|
|
}
|
|
}
|