package main import ( "math" "os" "path/filepath" "strings" "testing" ) func TestPlanValidate(t *testing.T) { base := func(s stamp) *plan { return &plan{Version: planVersion, Input: "x.pdf", Stamps: []stamp{s}} } good := stamp{ID: "s", Type: typeText, Content: "hi", Pages: []int{1}, Rect: rect{W: 10, H: 10}, Opacity: 0.5} if err := base(good).validate(); err != nil { t.Fatalf("good plan rejected: %v", err) } bad := map[string]stamp{ "empty text": {Type: typeText, Content: "", Pages: []int{1}, Rect: rect{W: 1, H: 1}}, "non-ascii text": {Type: typeText, Content: "héllo", Pages: []int{1}, Rect: rect{W: 1, H: 1}, Opacity: 1}, "control char": {Type: typeText, Content: "a\nb", Pages: []int{1}, Rect: rect{W: 1, H: 1}, Opacity: 1}, "empty src": {Type: typeImage, Src: "", Pages: []int{1}, Rect: rect{W: 1, H: 1}, Opacity: 1}, "unknown type": {Type: "frob", Pages: []int{1}, Rect: rect{W: 1, H: 1}}, "no pages": {Type: typeText, Content: "x", Rect: rect{W: 1, H: 1}}, "page below one": {Type: typeText, Content: "x", Pages: []int{0}, Rect: rect{W: 1, H: 1}}, "duplicate page": {Type: typeText, Content: "x", Pages: []int{1, 1}, Rect: rect{W: 1, H: 1}}, "zero width": {Type: typeText, Content: "x", Pages: []int{1}, Rect: rect{W: 0, H: 1}}, "opacity high": {Type: typeText, Content: "x", Pages: []int{1}, Rect: rect{W: 1, H: 1}, Opacity: 1.5}, "opacity neg": {Type: typeText, Content: "x", Pages: []int{1}, Rect: rect{W: 1, H: 1}, Opacity: -0.1}, "nan x": {Type: typeText, Content: "x", Pages: []int{1}, Rect: rect{X: math.NaN(), W: 1, H: 1}}, "inf w": {Type: typeText, Content: "x", Pages: []int{1}, Rect: rect{W: math.Inf(1), H: 1}}, } for name, s := range bad { if err := base(s).validate(); err == nil { t.Errorf("%s: expected validation error, got nil", name) } } // plan-level invariants. plans := map[string]*plan{ "older version": {Version: planVersion - 1, Input: "x"}, "newer version": {Version: planVersion + 1, Input: "x"}, "empty input": {Version: planVersion, Input: ""}, } for name, p := range plans { if err := p.validate(); err == nil { t.Errorf("%s: expected validation error, got nil", name) } } } func TestParsePlanStrict(t *testing.T) { valid := `{"version":2,"input":"x.pdf","stamps":[]}` for name, data := range map[string]string{ "unknown field": strings.Replace(valid, `"stamps"`, `"opactiy":1,"stamps"`, 1), "dropped field": strings.Replace(valid, `"stamps"`, `"unit":"pt","stamps"`, 1), "second value": valid + `{}`, "trailing junk": valid + `x`, } { if _, err := parsePlan([]byte(data), name); err == nil { t.Errorf("%s: expected error, got nil", name) } } } func TestPlanLimits(t *testing.T) { if _, err := parsePlan([]byte(strings.Repeat(" ", maxPlanBytes+1)), "large"); err == nil { t.Error("oversized plan: expected error, got nil") } p := newPlan("x.pdf") p.Stamps = make([]stamp, maxStamps+1) if err := p.validate(); err == nil { t.Error("too many stamps: expected error, got nil") } p.Stamps = []stamp{{Type: typeText, Content: strings.Repeat("x", maxTextBytes+1), Pages: []int{1}, Rect: rect{W: 1, H: 1}}} if err := p.validate(); err == nil { t.Error("oversized text: expected error, got nil") } pages := make([]int, maxPageRefs+1) for i := range pages { pages[i] = i + 1 } p.Stamps = []stamp{{Type: typeText, Content: "x", Pages: pages, Rect: rect{W: 1, H: 1}}} if err := p.validate(); err == nil { t.Error("too many page references: expected error, got nil") } } func TestPlanSaveRejectsOversizedOutput(t *testing.T) { p := newPlan(strings.Repeat("x", maxPlanBytes)) path := filepath.Join(t.TempDir(), "plan.json") if err := p.save(path); err == nil { t.Fatal("expected oversized encoded plan to fail") } if _, err := os.Stat(path); !os.IsNotExist(err) { t.Fatalf("oversized plan created an output: %v", err) } }