File: xref_spec.rb

package info (click to toggle)
ruby-pdf-reader 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 12,908 kB
  • ctags: 569
  • sloc: ruby: 8,330; makefile: 10
file content (132 lines) | stat: -rw-r--r-- 4,722 bytes parent folder | download
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
# coding: utf-8

require File.dirname(__FILE__) + "/spec_helper"

# expose the xrefs hash inside the XRef class so we can ensure it's built correctly
class PDF::Reader::XRef
  attr_reader :xref
end

describe PDF::Reader::XRef, "initilisation" do
  context "with cairo-basic.pdf" do
    it "should load all xrefs correctly from a File" do
      filename = File.new(pdf_spec_file("cairo-basic"))
      tbl      = PDF::Reader::XRef.new(filename)
      tbl.xref.keys.size.should eql(15) # 1 xref table with 16 items (ignore the first)
    end
    it "should load all xrefs correctly from a StringIO" do
      data = StringIO.new(binread(pdf_spec_file("cairo-basic")))
      tbl  = PDF::Reader::XRef.new(data)
      tbl.xref.keys.size.should eql(15) # 1 xref table with 16 items (ignore the first)
    end
  end
  context "with cairo-unicode.pdf" do
    it "should load all xrefs correctly" do
      file = File.new(pdf_spec_file("cairo-unicode"))
      tbl  = PDF::Reader::XRef.new(file)
      tbl.xref.keys.size.should eql(57) # 1 xref table with 58 items (ignore the first)
    end
    it "should load all xrefs correctly from a StringIO" do
      data = StringIO.new(binread(pdf_spec_file("cairo-unicode")))
      tbl  = PDF::Reader::XRef.new(data)
      tbl.xref.keys.size.should eql(57) # 1 xref table with 58 items (ignore the first)
    end
  end

  context "with openoffice-2.2.pdf" do
    it "should load all xrefs correctly" do
      @file = File.new(pdf_spec_file("openoffice-2.2"))
      @tbl = PDF::Reader::XRef.new(@file)
      @tbl.xref.keys.size.should eql(28) # 1 xref table with 29 items (ignore the first)
    end
  end

  context "with pdflatex.pdf" do
    it "should load all xrefs correctly" do
      @file = File.new(pdf_spec_file("pdflatex"))
      @tbl = PDF::Reader::XRef.new(@file)
      @tbl.xref.keys.size.should eql(353) # 1 xref table with 360 items (but a bunch are ignored)
    end
  end

  context "with xref_subsecetions.pdf" do
    it "should load all xrefs correctly from a PDF that has multiple xref sections with subsections and xref streams" do
      @file = File.new(pdf_spec_file("xref_subsections"))
      @tbl = PDF::Reader::XRef.new(@file)
      @tbl.xref.keys.size.should eql(539)
    end
  end

  context "with no_trailer.pdf" do
    it "should raise an error when attempting to locate the xref table" do
      @file = File.new(pdf_spec_file("no_trailer"))
      lambda {
        PDF::Reader::XRef.new(@file)
      }.should raise_error(PDF::Reader::MalformedPDFError)
    end
  end

  context "with trailer_is_not_a_dict.pdf" do
    it "should raise an error when attempting to locate the xref table" do
      @file = File.new(pdf_spec_file("trailer_is_not_a_dict"))
      lambda {
        PDF::Reader::XRef.new(@file)
      }.should raise_error(PDF::Reader::MalformedPDFError)
    end
  end

  context "with cross_ref_stream.pdf" do
    let!(:file) { File.new(pdf_spec_file("cross_ref_stream"))}
    subject     { PDF::Reader::XRef.new(file)}

    it "should correctly load all object locations" do
      subject.xref.keys.size.should eql(327) # 1 xref stream with 344 items (ignore the 17 free objects)
    end

    it "should load type 1 objects references" do
      subject.xref[66][0].should eql(298219)
    end

    it "should load type 2 objects references" do
      subject.xref[281][0].should eql(PDF::Reader::Reference.new(341,0))
    end
  end

  context "with cross_ref_stream.pdf" do
    let!(:file) { File.new(pdf_spec_file("cross_ref_stream"))}
    subject     { PDF::Reader::XRef.new(file)}

    it "should raise an error when attempting to load an invalid xref stream" do
      lambda do
        subject.send(:load_xref_stream, {:Subject=>"\xFE\xFF"})
      end.should raise_exception(PDF::Reader::MalformedPDFError)
    end
  end

  context "with zeroed_xref_entry.pdf" do
    let!(:file) { File.new(pdf_spec_file("zeroed_xref_entry"))}
    subject     { PDF::Reader::XRef.new(file)}

    it "should ignore non-free entries in the xref stream that point to offset 0" do
      subject.size.should eql(6)
      subject.xref.keys.should_not include(7)
    end
  end

  context "with junk_prefix.pdf" do
    it "should load all xrefs correctly from a File" do
      File.open(pdf_spec_file("junk_prefix")) do |file|
        tbl      = PDF::Reader::XRef.new(file)
        tbl.xref.keys.size.should eql(6) # 1 xref table with 6 items (ignore the first)
      end
    end

    it "should load all xrefs with an offset to skip junk at the beginning of the file" do
      File.open(pdf_spec_file("junk_prefix")) do |file|
        tbl      = PDF::Reader::XRef.new(file)
        tbl.xref[1][0].should == 36
        tbl.xref[2][0].should == 130
      end
    end
  end
end