|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/speedata/pdfdisassembler" |
| 10 | +) |
| 11 | + |
| 12 | +// buildObjPDF assembles 1-based object bodies into a PDF with a classical xref |
| 13 | +// and the given trailer dictionary body (without the surrounding << >>). |
| 14 | +func buildObjPDF(t *testing.T, objs []string, trailer string) []byte { |
| 15 | + t.Helper() |
| 16 | + var buf bytes.Buffer |
| 17 | + off := func() int { return buf.Len() } |
| 18 | + fmt.Fprint(&buf, "%PDF-1.7\n%\xE2\xE3\xCF\xD3\n") |
| 19 | + offsets := make([]int, len(objs)+1) |
| 20 | + for i, body := range objs { |
| 21 | + offsets[i+1] = off() |
| 22 | + fmt.Fprintf(&buf, "%d 0 obj\n%s\nendobj\n", i+1, body) |
| 23 | + } |
| 24 | + xrefOff := off() |
| 25 | + fmt.Fprintf(&buf, "xref\n0 %d\n%010d %05d f \n", len(objs)+1, 0, 65535) |
| 26 | + for i := 1; i <= len(objs); i++ { |
| 27 | + fmt.Fprintf(&buf, "%010d %05d n \n", offsets[i], 0) |
| 28 | + } |
| 29 | + fmt.Fprintf(&buf, "trailer\n<< /Size %d %s >>\nstartxref\n%d\n%%%%EOF\n", |
| 30 | + len(objs)+1, trailer, xrefOff) |
| 31 | + return buf.Bytes() |
| 32 | +} |
| 33 | + |
| 34 | +// buildPagesPDF builds a two-page document: page 1 inherits its MediaBox and |
| 35 | +// Resources from the /Pages root and carries a content stream; page 2 overrides |
| 36 | +// MediaBox, adds a CropBox and /Rotate, and has no content. |
| 37 | +func buildPagesPDF(t *testing.T) []byte { |
| 38 | + const content = "BT (Hi) Tj ET" // 13 bytes |
| 39 | + return buildObjPDF(t, []string{ |
| 40 | + "<< /Type /Catalog /Pages 2 0 R >>", |
| 41 | + "<< /Type /Pages /Count 2 /Kids [ 3 0 R 4 0 R ] /MediaBox [0 0 612 792] /Resources << /Font << /F1 5 0 R >> >> >>", |
| 42 | + "<< /Type /Page /Parent 2 0 R /Contents 6 0 R >>", |
| 43 | + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /CropBox [5 5 195 195] /Rotate 90 >>", |
| 44 | + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>", |
| 45 | + fmt.Sprintf("<< /Length %d >>\nstream\n%s\nendstream", len(content), content), |
| 46 | + }, "/Root 1 0 R") |
| 47 | +} |
| 48 | + |
| 49 | +func TestReport(t *testing.T) { |
| 50 | + r, err := pdfdisassembler.Open(bytes.NewReader(buildPagesPDF(t))) |
| 51 | + if err != nil { |
| 52 | + t.Fatalf("Open: %v", err) |
| 53 | + } |
| 54 | + defer r.Close() |
| 55 | + |
| 56 | + var out bytes.Buffer |
| 57 | + if err := report(&out, r); err != nil { |
| 58 | + t.Fatalf("report: %v", err) |
| 59 | + } |
| 60 | + got := out.String() |
| 61 | + |
| 62 | + wants := []string{ |
| 63 | + "Pages: 2", |
| 64 | + "Page 1:", |
| 65 | + "MediaBox: 612 x 792 pt", // inherited from /Pages root |
| 66 | + "Resources: [Font]", // inherited |
| 67 | + "Content: 13 bytes decoded", |
| 68 | + "Page 2:", |
| 69 | + "MediaBox: 200 x 200 pt", // overridden locally |
| 70 | + "CropBox: [5 5 195 195]", |
| 71 | + "Rotation: 90", |
| 72 | + "Content: 0 bytes decoded", // no /Contents |
| 73 | + } |
| 74 | + for _, w := range wants { |
| 75 | + if !strings.Contains(got, w) { |
| 76 | + t.Errorf("output missing %q; got:\n%s", w, got) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Page 1 has no rotation, so no Rotation line should appear before "Page 2". |
| 81 | + page1 := got[strings.Index(got, "Page 1:"):strings.Index(got, "Page 2:")] |
| 82 | + if strings.Contains(page1, "Rotation:") { |
| 83 | + t.Errorf("page 1 should not print a Rotation line; got:\n%s", page1) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// TestReportCyclicKidsTerminates feeds a page tree whose /Kids cycles back on |
| 88 | +// itself; report must return rather than recurse until the stack overflows. |
| 89 | +func TestReportCyclicKidsTerminates(t *testing.T) { |
| 90 | + data := buildObjPDF(t, []string{ |
| 91 | + "<< /Type /Catalog /Pages 2 0 R >>", |
| 92 | + "<< /Type /Pages /Count 1 /Kids [ 3 0 R ] >>", |
| 93 | + "<< /Type /Pages /Kids [ 2 0 R ] >>", // cycle back to obj 2 |
| 94 | + }, "/Root 1 0 R") |
| 95 | + r, err := pdfdisassembler.Open(bytes.NewReader(data)) |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("Open: %v", err) |
| 98 | + } |
| 99 | + defer r.Close() |
| 100 | + |
| 101 | + var out bytes.Buffer |
| 102 | + if err := report(&out, r); err != nil { |
| 103 | + t.Fatalf("report: %v", err) |
| 104 | + } |
| 105 | + if got := out.String(); !strings.Contains(got, "Pages: 0") { |
| 106 | + t.Errorf("want \"Pages: 0\", got:\n%s", got) |
| 107 | + } |
| 108 | +} |
0 commit comments