This commit is contained in:
+21
-1
@@ -5,6 +5,7 @@ package media
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
@@ -33,7 +34,8 @@ func TestPrepareReencodesRasterAndStripsTrailingData(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPreparePDFIsAttachment(t *testing.T) {
|
||||
prepared, err := Prepare(strings.NewReader("%PDF-1.7\nsmall fixture"), "guide.pdf", Limits{})
|
||||
pdf := minimalPDF()
|
||||
prepared, err := Prepare(bytes.NewReader(pdf), "guide.pdf", Limits{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -42,6 +44,18 @@ func TestPreparePDFIsAttachment(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareRejectsMalformedPDF(t *testing.T) {
|
||||
for _, source := range []string{
|
||||
"%PDF-1.7\nsmall fixture",
|
||||
"%PDF-9.9\nxref\ntrailer\nstartxref\n9\n%%EOF",
|
||||
"%PDF-1.7\nxref\ntrailer\nstartxref\n999999\n%%EOF",
|
||||
} {
|
||||
if _, err := Prepare(strings.NewReader(source), "broken.pdf", Limits{}); !errors.Is(err, ErrInvalidMedia) {
|
||||
t.Fatalf("malformed PDF error=%v source=%q", err, source)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareRejectsActiveAndOversizedInput(t *testing.T) {
|
||||
if _, err := Prepare(strings.NewReader("<svg><script/></svg>"), "bad.svg", Limits{}); !errors.Is(err, ErrInvalidMedia) {
|
||||
t.Fatalf("svg err=%v", err)
|
||||
@@ -50,3 +64,9 @@ func TestPrepareRejectsActiveAndOversizedInput(t *testing.T) {
|
||||
t.Fatalf("large err=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func minimalPDF() []byte {
|
||||
prefix := []byte("%PDF-1.7\n1 0 obj\n<< /Type /Catalog >>\nendobj\n")
|
||||
offset := len(prefix)
|
||||
return append(prefix, []byte(fmt.Sprintf("xref\n0 2\n0000000000 65535 f \n0000000009 00000 n \ntrailer\n<< /Size 2 /Root 1 0 R >>\nstartxref\n%d\n%%%%EOF\n", offset))...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user