Repository navigation
Expand file tree
/
Copy pathopen_test.go
More file actions
166 lines (153 loc) · 4 KB
/
Copy pathopen_test.go
File metadata and controls
166 lines (153 loc) · 4 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package pdfast55_test
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"github.com/hazyhaar/pdfast"
)
func TestFixturesIntegrity(t *testing.T) {
sumsPath := filepath.Join("testdata", "SHA256SUMS")
b, err := os.ReadFile(sumsPath)
if err != nil {
t.Fatal(err)
}
for _, line := range strings.Split(string(b), "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
parts := strings.Fields(line)
if len(parts) < 2 {
t.Fatalf("bad line: %q", line)
}
want, name := parts[0], parts[len(parts)-1]
raw, err := os.ReadFile(filepath.Join("testdata", name))
if err != nil {
t.Fatal(err)
}
sum := sha256.Sum256(raw)
got := hex.EncodeToString(sum[:])
if got != want {
t.Errorf("%s: hash mismatch", name)
}
}
}
func TestOpenMinimal(t *testing.T) {
doc, err := pdfast55.OpenFile("testdata/minimal_ok.pdf")
if err != nil {
t.Fatal(err)
}
if doc.Pages() < 1 {
t.Fatalf("pages=%d", doc.Pages())
}
pages, err := pdfast55.ExtractText(context.Background(), doc)
if err != nil {
t.Fatal(err)
}
var all string
for _, p := range pages {
all += p.Text
}
if !strings.Contains(all, "Hello") && !strings.Contains(all, "pdfast") {
t.Logf("extracted %q (font encoding may omit string; pages=%d)", all, len(pages))
}
}
func TestOpenInvalidMagic(t *testing.T) {
_, err := pdfast55.OpenFile("testdata/invalid_magic.pdf")
if err == nil {
t.Fatal("expected error")
}
if !errors.Is(err, pdfast55.ErrInvalidMagic) {
t.Fatalf("want ErrInvalidMagic got %v", err)
}
}
func TestOpenHostileJS_RejectPolicy(t *testing.T) {
_, err := pdfast55.OpenFile("testdata/hostile_js.pdf", pdfast55.WithActivePolicy(pdfast55.ActiveReject))
if err == nil {
t.Fatal("expected reject under ActiveReject")
}
if !errors.Is(err, pdfast55.ErrActiveContent) {
t.Fatalf("want ErrActiveContent got %v", err)
}
}
func TestOpenHostileJS_CoreOpens(t *testing.T) {
// Core engine (default): no active scan — hostile tokens do not block Open.
doc, err := pdfast55.OpenFile("testdata/hostile_js.pdf")
if err != nil {
t.Fatal(err)
}
_ = doc
}
func TestOpenHostileJS_SecurityObliterateOrOpen(t *testing.T) {
// Fixture only plants name tokens in a comment — no catalog OpenAction dict.
doc, err := pdfast55.OpenFile("testdata/hostile_js.pdf", pdfast55.WithSecurity(true))
if err != nil {
t.Fatal(err)
}
_ = doc
}
func TestOpenEncrypted(t *testing.T) {
_, err := pdfast55.OpenFile("testdata/encrypted.pdf")
if err == nil {
t.Fatal("expected encrypted error")
}
if !errors.Is(err, pdfast55.ErrEncrypted) {
t.Fatalf("want ErrEncrypted got %v", err)
}
}
func TestOpenHostileFixtures_RejectPolicy(t *testing.T) {
files := []string{
"testdata/hostile_aa.pdf",
"testdata/hostile_launch.pdf",
"testdata/hostile_embedded.pdf",
"testdata/hostile_uri_js.pdf",
"testdata/hostile_openaction.pdf",
}
for _, f := range files {
_, err := pdfast55.OpenFile(f, pdfast55.WithActivePolicy(pdfast55.ActiveReject))
if err == nil {
t.Errorf("%s: expected reject", f)
}
}
}
func TestOpenHostileOpenAction_SecurityObliterate(t *testing.T) {
doc, err := pdfast55.OpenFile("testdata/hostile_openaction.pdf", pdfast55.WithSecurity(true))
if err != nil {
t.Fatal(err)
}
if len(doc.Obliterated()) == 0 {
t.Fatal("expected obliteration")
}
res, err := pdfast55.Saniscribe(doc)
if err != nil {
t.Fatal(err)
}
if bytes.Contains(res.PDF, []byte("/OpenAction")) {
t.Fatal("saniscribe left OpenAction")
}
}
func TestOpenOversizedBeforeAlloc(t *testing.T) {
raw, err := os.ReadFile("testdata/minimal_ok.pdf")
if err != nil {
t.Fatal(err)
}
_, err = pdfast55.Open(bytes.NewReader(raw), int64(len(raw)), pdfast55.WithMaxFileSize(10))
if err == nil {
t.Fatal("expected oversized")
}
if !errors.Is(err, pdfast55.ErrOversized) {
t.Fatalf("want ErrOversized got %v", err)
}
}
func TestOpenHostileAllowed(t *testing.T) {
_, err := pdfast55.OpenFile("testdata/hostile_js.pdf", pdfast55.WithAllowActiveContent(true))
if err != nil {
t.Fatal(err)
}
}