1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
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)
}
}
|