Files
pages/internal/calendar/dav_test.go
T
2026-11-15 05:47:14 +09:00

129 lines
4.3 KiB
Go

package calendar
import (
"context"
"encoding/base64"
"errors"
"sync"
"testing"
"github.com/google/uuid"
)
func TestDAVUIDRejectsBinaryFallback(t *testing.T) {
for _, raw := range []string{"\xff\xfe", "uid\x00value", "uid\nvalue"} {
name := base64.RawURLEncoding.EncodeToString([]byte(raw)) + ".ics"
if _, err := DAVUID(Calendar{}, name); !errors.Is(err, ErrMissing) {
t.Fatalf("binary UID accepted: %v", err)
}
}
name := "client.ics"
if _, err := DAVUID(Calendar{}, name); !errors.Is(err, ErrMissing) {
t.Fatal(err)
}
if uid, err := DAVUID(Calendar{DAVNames: map[string]string{"日本語@example.invalid": name}}, name); err != nil || uid != "日本語@example.invalid" {
t.Fatal(uid, err)
}
}
func TestCalendarDAVIdentityAtomicAndRebuild(t *testing.T) {
r, domain, actor, c := fixture(t)
ctx := context.Background()
item := sample(t, "client@example.test", "2026-10-01T09:00:00Z", "2026-10-01T10:00:00Z", false)
var wg sync.WaitGroup
results := make(chan error, 2)
for i := 0; i < 2; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, _, err := r.ChangeDAV(ctx, domain, c.ID, actor, "arbitrary.ics", "", &item, nil)
results <- err
}()
}
wg.Wait()
close(results)
success := 0
for err := range results {
if err == nil {
success++
} else if !errors.Is(err, ErrPreconditionRequired) {
t.Fatal(err)
}
}
if success != 1 {
t.Fatal(success)
}
// Filename metadata survives an index rebuild and a settings change.
c, err := r.Get(domain, c.ID, actor)
if err != nil {
t.Fatal(err)
}
if _, err = r.Settings(ctx, domain, c.ID, actor, c.Version, "Renamed", false, c.Members); err != nil {
t.Fatal(err)
}
for _, table := range []string{"calendar_event_cache", "calendar_month_cache", "calendar_index_state"} {
if _, err = r.DB.Exec("DELETE FROM " + table); err != nil {
t.Fatal(err)
}
}
c, items, err := r.DAVSnapshot(ctx, domain, c.ID, actor, true)
if err != nil || len(items) != 1 || DAVFilename(c, items[0].UID) != "arbitrary.ics" || items[0].ETag != item.ETag {
t.Fatal(err, items, c.DAVNames)
}
if _, _, err = r.ChangeDAV(ctx, domain, c.ID, actor, "other.ics", "", &item, nil); err != ErrConflict {
t.Fatal("duplicate UID", err)
}
other := sample(t, "other@example.test", "2026-10-01T09:00:00Z", "2026-10-01T10:00:00Z", false)
if _, _, err = r.ChangeDAV(ctx, domain, c.ID, actor, "arbitrary.ics", item.ETag, &other, nil); err != ErrConflict {
t.Fatal("identity changed", err)
}
member := uuid.NewString()
c, err = r.Settings(ctx, domain, c.ID, actor, c.Version, c.Name, false, map[string]string{member: "read"})
if err != nil {
t.Fatal(err)
}
if _, _, err = r.ChangeDAV(ctx, domain, c.ID, member, "arbitrary.ics", item.ETag, &item, nil); err != ErrForbidden {
t.Fatal("read member wrote", err)
}
c, err = r.Settings(ctx, domain, c.ID, actor, c.Version, c.Name, false, map[string]string{member: "write"})
if err != nil {
t.Fatal(err)
}
if _, _, err = r.ChangeDAV(ctx, domain, c.ID, member, "arbitrary.ics", item.ETag, &item, nil); err != nil {
t.Fatal(err)
}
c, err = r.Get(domain, c.ID, actor)
if err != nil {
t.Fatal(err)
}
if _, err = r.Settings(ctx, domain, c.ID, actor, c.Version, c.Name, false, map[string]string{}); err != nil {
t.Fatal(err)
}
if _, _, err = r.DAVSnapshot(ctx, domain, c.ID, member, true); err != ErrMissing {
t.Fatal("revoked ACL leaked", err)
}
if _, _, err = r.ChangeDAV(ctx, domain, c.ID, actor, "arbitrary.ics", item.ETag, nil, nil); err != nil {
t.Fatal(err)
}
c, err = r.Get(domain, c.ID, actor)
if err != nil || len(c.DAVNames) != 0 {
t.Fatal("alias retained after delete", err, c.DAVNames)
}
}
func TestCalendarDAVSnapshotLimit(t *testing.T) {
r, domain, actor, c := fixture(t)
ctx := context.Background()
if _, _, err := r.DAVSnapshot(ctx, domain, c.ID, actor, false); err != nil {
t.Fatal(err)
}
// Synthetic disposable cache only; exceed the collection bound deliberately.
_, err := r.DB.Exec(`WITH RECURSIVE n(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM n WHERE x<10001) INSERT INTO calendar_event_cache(domain,calendar,uid,month,start,end,etag,data) SELECT ?,?,CAST(x AS TEXT),'2026-10',0,0,'"test"','' FROM n`, domain, c.ID)
if err != nil {
t.Fatal(err)
}
if _, items, err := r.DAVSnapshot(ctx, domain, c.ID, actor, false); err != ErrLimit || items != nil {
t.Fatal("returned truncated collection", len(items), err)
}
}