Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
308 lines
12 KiB
Go
308 lines
12 KiB
Go
package server
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func stubMetadataCommands(t *testing.T, pdf, archive string) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
marker := filepath.Join(dir, "command-was-run")
|
|
t.Setenv("PAGESWIKI_METADATA_TEST_MARKER", marker)
|
|
for name, output := range map[string]string{"pdfinfo": pdf, "7z": archive} {
|
|
script := "#!/bin/sh\nprintf called >> \"$PAGESWIKI_METADATA_TEST_MARKER\"\nprintf '%s' '" + strings.ReplaceAll(output, "'", "'\"'\"'") + "'\n"
|
|
if err := os.WriteFile(filepath.Join(dir, name), []byte(script), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
return marker
|
|
}
|
|
|
|
func saveMetadataDocument(t *testing.T, s *Server, domain, path, contentType string) *store.Revision {
|
|
t.Helper()
|
|
object, err := s.store.CreatePageAtPathInDomain(domain, path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
revision, err := s.store.SaveRevisionContent(object, []byte("metadata fixture"), "fixture", "und", contentType, filepath.Base(path))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return revision
|
|
}
|
|
|
|
func runMetadataJob(t *testing.T, s *Server) {
|
|
t.Helper()
|
|
job, err := s.store.ClaimDueEvent()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if job.Type != metadataProjectionEventType || job.Priority >= 0 {
|
|
t.Fatalf("unexpected metadata job: %+v", job)
|
|
}
|
|
runErr := s.runEvent(job)
|
|
if err := s.store.FinishEvent(job.UUID, runErr); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if runErr != nil {
|
|
t.Fatal(runErr)
|
|
}
|
|
}
|
|
|
|
func TestMetadataProjectionForegroundOnlyQueuesAndReusesPersistentCache(t *testing.T) {
|
|
for _, kind := range []string{"pdf", "archive"} {
|
|
t.Run(kind, func(t *testing.T) {
|
|
s, token, domain := configFixture(t)
|
|
marker := stubMetadataCommands(t, "Pages: 3\n", "Path = chapter\nFolder = +\nSize = 0\n\nPath = chapter/page 1.png\nSize = 42\n\n")
|
|
path, contentType := "/document.pdf", "application/pdf"
|
|
if kind == "archive" {
|
|
path, contentType = "/document.7z", "application/x-7z-compressed"
|
|
}
|
|
revision := saveMetadataDocument(t, s, domain, path, contentType)
|
|
s.mux.HandleFunc("/v/pdf-page", s.handlePDFPage)
|
|
s.mux.HandleFunc("/v/archive", s.handleArchiveEntry)
|
|
endpoint := metadataProjectionURL(kind, revision.UUID, path)
|
|
for range 3 {
|
|
recorder := configRequest(s, token, http.MethodGet, endpoint, nil)
|
|
if recorder.Code != http.StatusAccepted || !strings.Contains(recorder.Body.String(), `"status":"pending"`) {
|
|
t.Fatalf("cold metadata response: %d %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
request := httptest.NewRequest(http.MethodGet, "http://localhost"+path, nil)
|
|
request.Header.Set("Authorization", "Bearer "+token)
|
|
view, selected, err := s.windowedContentViewForRequest(request, revision.ObjectUUID, path)
|
|
if err != nil || !selected || view.Kind != kind || view.MetadataStatus != "pending" || view.Text != "" {
|
|
t.Fatalf("metadata-only Viewer: %+v selected=%v err=%v", view, selected, err)
|
|
}
|
|
metadata, err := s.store.GetRevisionMetadata(revision.UUID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.printContentView(metadata, path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := os.Stat(marker); !os.IsNotExist(err) {
|
|
t.Fatalf("foreground request executed a metadata decoder: %v", err)
|
|
}
|
|
var count int
|
|
if err := s.store.DB.QueryRow(`SELECT COUNT(*) FROM event_jobs WHERE event_type=?`, metadataProjectionEventType).Scan(&count); err != nil || count != 1 {
|
|
t.Fatalf("duplicate metadata jobs: %d %v", count, err)
|
|
}
|
|
runMetadataJob(t, s)
|
|
if _, err := os.Stat(marker); err != nil {
|
|
t.Fatalf("scheduler did not inspect metadata: %v", err)
|
|
}
|
|
// A new Server has no in-memory state: the result comes from SQLite.
|
|
restarted := &Server{store: s.store}
|
|
result, err := restarted.requestMetadataProjection(revision.DocumentHash, kind)
|
|
if err != nil || result.Status != "ready" {
|
|
t.Fatalf("persistent result: %+v %v", result, err)
|
|
}
|
|
if kind == "pdf" && result.PDFPages != 3 {
|
|
t.Fatalf("PDF count = %d", result.PDFPages)
|
|
}
|
|
if kind == "archive" && (len(result.Entries) != 2 || !result.Entries[0].IsDir || !result.Entries[1].IsImage) {
|
|
t.Fatalf("archive metadata = %+v", result.Entries)
|
|
}
|
|
recorder := configRequest(s, token, http.MethodGet, endpoint, nil)
|
|
if recorder.Code != http.StatusOK || recorder.Header().Get("Vary") != "Authorization, Cookie" {
|
|
t.Fatalf("warm metadata response: %d %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
cached, err := s.store.GetProjectionMetadata(revision.DocumentHash, kind, metadataProjectionVersion)
|
|
if err != nil || bytes.Contains(cached.Payload, []byte("/v/archive")) || bytes.Contains(cached.Payload, []byte(revision.UUID)) {
|
|
t.Fatalf("cache contains request-specific URLs/Revision: %s %v", cached.Payload, err)
|
|
}
|
|
if err := s.store.DB.QueryRow(`SELECT COUNT(*) FROM event_jobs WHERE event_type=?`, metadataProjectionEventType).Scan(&count); err != nil || count != 1 {
|
|
t.Fatalf("warm result enqueued another job: %d %v", count, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMetadataProjectionFailuresAreCachedAndUnauthorizedRequestsDoNotQueue(t *testing.T) {
|
|
s, token, domain := configFixture(t)
|
|
stubMetadataCommands(t, "Pages: 10001\n", "invalid")
|
|
revision := saveMetadataDocument(t, s, domain, "/secret.pdf", "application/pdf")
|
|
s.mux.HandleFunc("/v/pdf-page", s.handlePDFPage)
|
|
endpoint := metadataProjectionURL("pdf", revision.UUID, "/secret.pdf")
|
|
if got := configRequest(s, token, http.MethodGet, endpoint, nil); got.Code != http.StatusAccepted {
|
|
t.Fatalf("queue: %d %s", got.Code, got.Body.String())
|
|
}
|
|
job, err := s.store.ClaimDueEvent()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runErr := s.runEvent(job)
|
|
if runErr == nil {
|
|
t.Fatal("unbounded PDF page count accepted")
|
|
}
|
|
if err := s.store.FinishEvent(job.UUID, runErr); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for range 2 {
|
|
got := configRequest(s, token, http.MethodGet, endpoint, nil)
|
|
if got.Code != http.StatusUnprocessableEntity || got.Header().Get("Retry-After") == "" || !strings.Contains(got.Body.String(), `"status":"failed"`) {
|
|
t.Fatalf("failure result: %d %s", got.Code, got.Body.String())
|
|
}
|
|
}
|
|
member, err := s.store.CreatePasswordAccount(domain, "limited", "a long test password", "Limited", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.store.DB.Exec(`DELETE FROM auth_role_bindings WHERE membership_uuid IN (SELECT uuid FROM domain_memberships WHERE principal_uuid=?)`, member.UUID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
limitedToken, err := s.store.CreateAuthSession(domain, member.UUID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := configRequest(s, limitedToken, http.MethodGet, endpoint, nil)
|
|
if got.Code != http.StatusForbidden || strings.Contains(got.Body.String(), "metadata inspection") {
|
|
t.Fatalf("cached metadata bypassed authorization: %d %s", got.Code, got.Body.String())
|
|
}
|
|
var count int
|
|
if err := s.store.DB.QueryRow(`SELECT COUNT(*) FROM event_jobs WHERE event_type=?`, metadataProjectionEventType).Scan(&count); err != nil || count != 1 {
|
|
t.Fatalf("failure/cached denial enqueued another job: %d %v", count, err)
|
|
}
|
|
}
|
|
|
|
func TestArchiveMetadataLimitsAndLiteralRevisionLinks(t *testing.T) {
|
|
output := strings.Repeat("Path = comic/page.png\nSize = 1\n\n", maxArchiveMetadataEntries+5)
|
|
result, err := parseArchiveMetadata([]byte(output), false)
|
|
if err != nil || !result.Truncated || len(result.Entries) != maxArchiveMetadataEntries {
|
|
t.Fatalf("entry cap: entries=%d truncated=%v err=%v", len(result.Entries), result.Truncated, err)
|
|
}
|
|
output = strings.Repeat("Path = "+strings.Repeat("x", 2000)+".png\nSize = 1\n\n", 200)
|
|
result, err = parseArchiveMetadata([]byte(output), false)
|
|
if err != nil || !result.Truncated || len(result.Entries) >= 200 {
|
|
t.Fatalf("name-byte cap: entries=%d truncated=%v err=%v", len(result.Entries), result.Truncated, err)
|
|
}
|
|
for _, bad := range []string{"Path = bad\x00name\nSize = 1\n", "Path = " + strings.Repeat("x", 4097) + "\nSize = 1\n", "Path = entry\nSize = -1\n"} {
|
|
if _, err := parseArchiveMetadata([]byte(bad), false); err == nil {
|
|
t.Fatalf("accepted malformed metadata: %.80q", bad)
|
|
}
|
|
}
|
|
entry := archiveEntryView{Name: "<img>&+[test].png", Size: 8, IsImage: true}
|
|
links := archiveMetadataLinks([]archiveEntryView{entry}, "/folder/a&b.7z", "revision-1")
|
|
parsed, err := url.Parse(links[0].URL)
|
|
if err != nil || parsed.Path != "/v/archive" || parsed.Query().Get("entry") != entry.Name || parsed.Query().Get("rev") != "revision-1" || parsed.Query().Get("path") != "/folder/a&b.7z" {
|
|
t.Fatalf("unsafe/mutable entry URL: %s %v", links[0].URL, err)
|
|
}
|
|
payload, _ := json.Marshal(result)
|
|
if len(payload) > store.MaxProjectionMetadataBytes {
|
|
t.Fatalf("bounded archive metadata cannot fit in cache: %d", len(payload))
|
|
}
|
|
}
|
|
|
|
func TestArchiveMetadataRejectsRevisionFromAnotherObject(t *testing.T) {
|
|
s, token, domain := configFixture(t)
|
|
first := saveMetadataDocument(t, s, domain, "/first.7z", "application/x-7z-compressed")
|
|
second := saveMetadataDocument(t, s, domain, "/second.7z", "application/x-7z-compressed")
|
|
s.mux.HandleFunc("/v/archive", s.handleArchiveEntry)
|
|
if first.ObjectUUID == second.ObjectUUID {
|
|
t.Fatal("fixture objects must differ")
|
|
}
|
|
got := configRequest(s, token, http.MethodGet, metadataProjectionURL("archive", second.UUID, "/first.7z"), nil)
|
|
if got.Code != http.StatusNotFound {
|
|
t.Fatalf("cross-object Revision accepted for Path: %d %s", got.Code, got.Body.String())
|
|
}
|
|
var count int
|
|
if err := s.store.DB.QueryRow(`SELECT COUNT(*) FROM event_jobs WHERE event_type=?`, metadataProjectionEventType).Scan(&count); err != nil || count != 0 {
|
|
t.Fatalf("invalid reference queued metadata: %d %v", count, err)
|
|
}
|
|
}
|
|
|
|
func TestMetadataCommandBoundsOutputAndDeadline(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
output, truncated, _ := runMetadataCommand(ctx, 32, "sh", "-c", "printf '%0100d' 0")
|
|
if !truncated || len(output) != 32 {
|
|
t.Fatalf("command output limit: len=%d truncated=%v", len(output), truncated)
|
|
}
|
|
ctx, cancel = context.WithTimeout(context.Background(), 20*time.Millisecond)
|
|
defer cancel()
|
|
if _, _, err := runMetadataCommand(ctx, 128, "sleep", "10"); err == nil {
|
|
t.Fatal("command deadline did not terminate the child")
|
|
}
|
|
}
|
|
|
|
func TestZIPArchiveEntriesRemainInlineAndPinRevisions(t *testing.T) {
|
|
var buffer bytes.Buffer
|
|
writer := zip.NewWriter(&buffer)
|
|
for _, name := range []string{"page-1.png", "page-2.png"} {
|
|
file, err := writer.Create(name)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, _ = fmt.Fprint(file, "image")
|
|
}
|
|
if err := writer.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
view := revisionContentView(&store.Revision{UUID: "old-revision", Filename: "comic.zip", ContentType: "application/zip", Body: buffer.String()}, "/comic.zip")
|
|
if view.Kind != "archive" || len(view.ArchiveEntries) != 2 || view.MetadataURL != "" {
|
|
t.Fatalf("ZIP no longer inline: %+v", view)
|
|
}
|
|
for _, entry := range view.ArchiveEntries {
|
|
if !entry.IsImage || !strings.Contains(entry.URL, "rev=old-revision") {
|
|
t.Fatalf("ZIP comic entry lost immutable binding: %+v", entry)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPDFMetadataUsesInstalledPDFInfo(t *testing.T) {
|
|
if _, err := exec.LookPath("pdfinfo"); err != nil {
|
|
t.Skip("pdfinfo not installed")
|
|
}
|
|
s, _, domain := configFixture(t)
|
|
var pdf bytes.Buffer
|
|
pdf.WriteString("%PDF-1.4\n")
|
|
offsets := []int{0}
|
|
objects := []string{
|
|
"<< /Type /Catalog /Pages 2 0 R >>",
|
|
"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
|
|
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] /Resources << >> >>",
|
|
}
|
|
for index, object := range objects {
|
|
offsets = append(offsets, pdf.Len())
|
|
fmt.Fprintf(&pdf, "%d 0 obj\n%s\nendobj\n", index+1, object)
|
|
}
|
|
xref := pdf.Len()
|
|
fmt.Fprintf(&pdf, "xref\n0 %d\n0000000000 65535 f \n", len(offsets))
|
|
for _, offset := range offsets[1:] {
|
|
fmt.Fprintf(&pdf, "%010d 00000 n \n", offset)
|
|
}
|
|
fmt.Fprintf(&pdf, "trailer\n<< /Size %d /Root 1 0 R >>\nstartxref\n%d\n%%%%EOF\n", len(offsets), xref)
|
|
object, err := s.store.CreatePageAtPathInDomain(domain, "/real.pdf")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
revision, err := s.store.SaveRevisionContent(object, pdf.Bytes(), "test", "und", "application/pdf", "real.pdf")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.generateMetadataProjection(metadataProjectionPayload{revision.DocumentHash, "pdf", metadataProjectionVersion}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, err := s.requestMetadataProjection(revision.DocumentHash, "pdf")
|
|
if err != nil || result.Status != "ready" || result.PDFPages != 1 {
|
|
t.Fatalf("actual PDF result: %+v %v", result, err)
|
|
}
|
|
}
|