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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
# typed: false
# coding: utf-8
# A set of integration specs that assert we raise expected errors when trying to parse PDFs that
# are invalid. Usually they have some form of corruption that we're unable to compensate for
describe PDF::Reader, "integration specs with invalid PDF files" do
context "Empty file" do
it "raises an exception" do
expect {
PDF::Reader.new(StringIO.new(""))
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "PDF has a content stream refers to a non-existant font" do
let(:filename) { pdf_spec_file("content_stream_refers_to_invalid_font") }
it "raises an exception" do
expect {
reader = PDF::Reader.new(filename)
reader.page(1).text
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "Malformed PDF" do
let(:filename) { pdf_spec_file("trailer_root_is_not_a_dict") }
it "raises an exception if trailer Root is not a dict" do
PDF::Reader.open(filename) do |reader|
expect { reader.page(1) }.to raise_error(PDF::Reader::MalformedPDFError)
end
end
end
context "PDF with missing page data" do
let(:filename) { pdf_spec_file("invalid_pages") }
it "raises a MalformedPDFError when an InvalidPageError is raised internally" do
PDF::Reader.open(filename) do |reader|
expect { reader.pages }.to raise_error(PDF::Reader::MalformedPDFError)
end
end
end
context "PDF that has a content stream with a broken string" do
let(:filename) { pdf_spec_file("broken_string") }
# this file used to get us into a hard, endless loop. Make sure that doesn't still happen
it "doesn't hang when extracting doc info" do
Timeout::timeout(3) do
expect {
reader = PDF::Reader.new(filename)
reader.info
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
end
context "gh-217" do
let(:filename) { pdf_spec_file("gh-217") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "gh-222" do
let(:filename) { pdf_spec_file("gh-222") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
# The top level Pages object is corrupted and has no Count or Type key
context "gh-223" do
let(:filename) { pdf_spec_file("gh-223") }
it "parses without error" do
expect {
parse_pdf(filename)
}.to_not raise_error
end
it "has zero pages" do
PDF::Reader.open(filename) do |reader|
expect(reader.page_count).to eq 0
expect(reader.pages).to eq []
end
end
end
context "gh-224" do
let(:filename) { pdf_spec_file("gh-224") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "gh-227" do
let(:filename) { pdf_spec_file("gh-227") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "gh-228" do
let(:filename) { pdf_spec_file("gh-228") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "gh-229" do
let(:filename) { pdf_spec_file("gh-229") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "gh-230" do
let(:filename) { pdf_spec_file("gh-230") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "gh-231" do
let(:filename) { pdf_spec_file("gh-231") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "gh-232" do
let(:filename) { pdf_spec_file("gh-232") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "gh-234" do
let(:filename) { pdf_spec_file("gh-234") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "gh-235" do
let(:filename) { pdf_spec_file("gh-235") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "gh-236" do
let(:filename) { pdf_spec_file("gh-236") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "gh-237" do
let(:filename) { pdf_spec_file("gh-237") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "gh-238" do
let(:filename) { pdf_spec_file("gh-238") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "gh-239" do
let(:filename) { pdf_spec_file("gh-239") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "gh-240" do
let(:filename) { pdf_spec_file("gh-240") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "gh-241" do
let(:filename) { pdf_spec_file("gh-241") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "gh-242" do
let(:filename) { pdf_spec_file("gh-242") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
# This one raised an unexpected exception in v2.0.0, but since v2.6.0 (and PR #372) it works
# without error
context "gh-243" do
let(:filename) { pdf_spec_file("gh-243") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to_not raise_error
end
end
# This one raised an unexpected exception in v2.0.0, but since v2.4.0 (and PR #309) it works
# without error
context "gh-244" do
let(:filename) { pdf_spec_file("gh-244") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to_not raise_error
end
end
# This encrypted PDF declares an invalid key length. It's not really that we don't support a
# feature - we'll never support an invalid key length. We raise an unsupported fature error
# anyway.
context "gh-245" do
let(:filename) { pdf_spec_file("gh-245") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::UnsupportedFeatureError)
end
end
context "negative-xref-offset.pdf" do
let(:filename) { pdf_spec_file("negative-xref-offset") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "content_stream_wrong_args.pdf" do
let(:filename) { pdf_spec_file("content_stream_wrong_args") }
it "raises MalformedPDFError when parsed" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError)
end
end
context "xref_offset_too_low.pdf" do
let(:filename) { pdf_spec_file("xref_offset_too_low") }
it "compensates for the error and can extract the paage text" do
expect {
parse_pdf(filename)
}.to_not raise_error
PDF::Reader.open(filename) do |pdf|
expect(pdf.page(1).text).to eql(
"The xref offset for the root object (obj 2) is a few bytes too low"
)
end
end
end
context "stream_missing_endobj.pdf" do
let(:filename) { pdf_spec_file("stream_missing_endobj") }
it "compensates for the error and can extract the paage text" do
expect {
parse_pdf(filename)
}.to_not raise_error
PDF::Reader.open(filename) do |pdf|
expect(pdf.page(1).text).to eql(
"Object 4 (content stream) is missing the endobj token"
)
end
end
end
context "loop-in-page-ancestors.pdf" do
let(:filename) { pdf_spec_file("loop-in-page-ancestors") }
it "detects the loop and raises an exception" do
expect {
parse_pdf(filename)
}.to raise_error(PDF::Reader::MalformedPDFError, "loop found in ancestor path")
end
end
# a very basic sanity check that we can open this file and extract interesting data
def parse_pdf(filename)
PDF::Reader.open(filename) do |reader|
reader.pdf_version
reader.info
reader.metadata
reader.objects
reader.page_count
reader.pages.each do |page|
page.fonts.to_s
page.text.to_s
page.raw_content.to_s
end
end
end
end
|