1 // Copyright 2015 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <limits>
6 #include <string>
7 
8 #include "fpdfsdk/fpdfview_c_api_test.h"
9 #include "public/fpdfview.h"
10 #include "testing/embedder_test.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
TEST(fpdf,CApiTest)13 TEST(fpdf, CApiTest) {
14   EXPECT_TRUE(CheckPDFiumCApi());
15 }
16 
17 class FPDFViewEmbeddertest : public EmbedderTest {};
18 
TEST_F(FPDFViewEmbeddertest,Document)19 TEST_F(FPDFViewEmbeddertest, Document) {
20   EXPECT_TRUE(OpenDocument("about_blank.pdf"));
21   EXPECT_EQ(1, GetPageCount());
22   EXPECT_EQ(0, GetFirstPageNum());
23 
24   int version;
25   EXPECT_TRUE(FPDF_GetFileVersion(document(), &version));
26   EXPECT_EQ(14, version);
27 
28   EXPECT_EQ(0xFFFFFFFF, FPDF_GetDocPermissions(document()));
29   EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document()));
30 }
31 
32 // See bug 465.
TEST_F(FPDFViewEmbeddertest,EmptyDocument)33 TEST_F(FPDFViewEmbeddertest, EmptyDocument) {
34   EXPECT_TRUE(CreateEmptyDocument());
35 
36   {
37     int version = 42;
38     EXPECT_FALSE(FPDF_GetFileVersion(document(), &version));
39     EXPECT_EQ(0, version);
40   }
41 
42   {
43 #ifndef PDF_ENABLE_XFA
44     const unsigned long kExpected = 0;
45 #else
46     const unsigned long kExpected = static_cast<uint32_t>(-1);
47 #endif
48     EXPECT_EQ(kExpected, FPDF_GetDocPermissions(document()));
49   }
50 
51   EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document()));
52 
53   EXPECT_EQ(0, FPDF_GetPageCount(document()));
54 
55   EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
56   EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document()));
57   EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
58 
59   char buf[100];
60   EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
61   EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
62 
63   EXPECT_EQ(0u, FPDF_CountNamedDests(document()));
64 }
65 
TEST_F(FPDFViewEmbeddertest,Page)66 TEST_F(FPDFViewEmbeddertest, Page) {
67   EXPECT_TRUE(OpenDocument("about_blank.pdf"));
68   FPDF_PAGE page = LoadPage(0);
69   EXPECT_NE(nullptr, page);
70   EXPECT_EQ(612.0, FPDF_GetPageWidth(page));
71   EXPECT_EQ(792.0, FPDF_GetPageHeight(page));
72   UnloadPage(page);
73   EXPECT_EQ(nullptr, LoadPage(1));
74 }
75 
TEST_F(FPDFViewEmbeddertest,ViewerRefDummy)76 TEST_F(FPDFViewEmbeddertest, ViewerRefDummy) {
77   EXPECT_TRUE(OpenDocument("about_blank.pdf"));
78   EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
79   EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document()));
80   EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
81 
82   char buf[100];
83   EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
84   EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
85 }
86 
TEST_F(FPDFViewEmbeddertest,ViewerRef)87 TEST_F(FPDFViewEmbeddertest, ViewerRef) {
88   EXPECT_TRUE(OpenDocument("viewer_ref.pdf"));
89   EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
90   EXPECT_EQ(5, FPDF_VIEWERREF_GetNumCopies(document()));
91   EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
92 
93   // Test some corner cases.
94   char buf[100];
95   EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "", buf, sizeof(buf)));
96   EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
97   EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
98 
99   // Make sure |buf| does not get written into when it appears to be too small.
100   // NOLINTNEXTLINE(runtime/printf)
101   strcpy(buf, "ABCD");
102   EXPECT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, 1));
103   EXPECT_STREQ("ABCD", buf);
104 
105   // Note "Foo" is a different key from "foo".
106   EXPECT_EQ(4U,
107             FPDF_VIEWERREF_GetName(document(), "Foo", nullptr, sizeof(buf)));
108   ASSERT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, sizeof(buf)));
109   EXPECT_STREQ("foo", buf);
110 
111   // Try to retrieve a boolean and an integer.
112   EXPECT_EQ(
113       0U, FPDF_VIEWERREF_GetName(document(), "HideToolbar", buf, sizeof(buf)));
114   EXPECT_EQ(0U,
115             FPDF_VIEWERREF_GetName(document(), "NumCopies", buf, sizeof(buf)));
116 
117   // Try more valid cases.
118   ASSERT_EQ(4U,
119             FPDF_VIEWERREF_GetName(document(), "Direction", buf, sizeof(buf)));
120   EXPECT_STREQ("R2L", buf);
121   ASSERT_EQ(8U,
122             FPDF_VIEWERREF_GetName(document(), "ViewArea", buf, sizeof(buf)));
123   EXPECT_STREQ("CropBox", buf);
124 }
125 
TEST_F(FPDFViewEmbeddertest,NamedDests)126 TEST_F(FPDFViewEmbeddertest, NamedDests) {
127   EXPECT_TRUE(OpenDocument("named_dests.pdf"));
128   long buffer_size;
129   char fixed_buffer[512];
130   FPDF_DEST dest;
131 
132   // Query the size of the first item.
133   buffer_size = 2000000;  // Absurdly large, check not used for this case.
134   dest = FPDF_GetNamedDest(document(), 0, nullptr, &buffer_size);
135   EXPECT_NE(nullptr, dest);
136   EXPECT_EQ(12, buffer_size);
137 
138   // Try to retrieve the first item with too small a buffer.
139   buffer_size = 10;
140   dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size);
141   EXPECT_NE(nullptr, dest);
142   EXPECT_EQ(-1, buffer_size);
143 
144   // Try to retrieve the first item with correctly sized buffer. Item is
145   // taken from Dests NameTree in named_dests.pdf.
146   buffer_size = 12;
147   dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size);
148   EXPECT_NE(nullptr, dest);
149   EXPECT_EQ(12, buffer_size);
150   EXPECT_EQ(std::string("F\0i\0r\0s\0t\0\0\0", 12),
151             std::string(fixed_buffer, buffer_size));
152 
153   // Try to retrieve the second item with ample buffer. Item is taken
154   // from Dests NameTree but has a sub-dictionary in named_dests.pdf.
155   buffer_size = sizeof(fixed_buffer);
156   dest = FPDF_GetNamedDest(document(), 1, fixed_buffer, &buffer_size);
157   EXPECT_NE(nullptr, dest);
158   EXPECT_EQ(10, buffer_size);
159   EXPECT_EQ(std::string("N\0e\0x\0t\0\0\0", 10),
160             std::string(fixed_buffer, buffer_size));
161 
162   // Try to retrieve third item with ample buffer. Item is taken
163   // from Dests NameTree but has a bad sub-dictionary in named_dests.pdf.
164   // in named_dests.pdf).
165   buffer_size = sizeof(fixed_buffer);
166   dest = FPDF_GetNamedDest(document(), 2, fixed_buffer, &buffer_size);
167   EXPECT_EQ(nullptr, dest);
168   EXPECT_EQ(sizeof(fixed_buffer),
169             static_cast<size_t>(buffer_size));  // unmodified.
170 
171   // Try to retrieve the forth item with ample buffer. Item is taken
172   // from Dests NameTree but has a vale of the wrong type in named_dests.pdf.
173   buffer_size = sizeof(fixed_buffer);
174   dest = FPDF_GetNamedDest(document(), 3, fixed_buffer, &buffer_size);
175   EXPECT_EQ(nullptr, dest);
176   EXPECT_EQ(sizeof(fixed_buffer),
177             static_cast<size_t>(buffer_size));  // unmodified.
178 
179   // Try to retrieve fifth item with ample buffer. Item taken from the
180   // old-style Dests dictionary object in named_dests.pdf.
181   buffer_size = sizeof(fixed_buffer);
182   dest = FPDF_GetNamedDest(document(), 4, fixed_buffer, &buffer_size);
183   EXPECT_NE(nullptr, dest);
184   EXPECT_EQ(30, buffer_size);
185   EXPECT_EQ(std::string("F\0i\0r\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 30),
186             std::string(fixed_buffer, buffer_size));
187 
188   // Try to retrieve sixth item with ample buffer. Item istaken from the
189   // old-style Dests dictionary object but has a sub-dictionary in
190   // named_dests.pdf.
191   buffer_size = sizeof(fixed_buffer);
192   dest = FPDF_GetNamedDest(document(), 5, fixed_buffer, &buffer_size);
193   EXPECT_NE(nullptr, dest);
194   EXPECT_EQ(28, buffer_size);
195   EXPECT_EQ(std::string("L\0a\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 28),
196             std::string(fixed_buffer, buffer_size));
197 
198   // Try to retrieve non-existent item with ample buffer.
199   buffer_size = sizeof(fixed_buffer);
200   dest = FPDF_GetNamedDest(document(), 6, fixed_buffer, &buffer_size);
201   EXPECT_EQ(nullptr, dest);
202   EXPECT_EQ(sizeof(fixed_buffer),
203             static_cast<size_t>(buffer_size));  // unmodified.
204 
205   // Try to underflow/overflow the integer index.
206   buffer_size = sizeof(fixed_buffer);
207   dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::max(),
208                            fixed_buffer, &buffer_size);
209   EXPECT_EQ(nullptr, dest);
210   EXPECT_EQ(sizeof(fixed_buffer),
211             static_cast<size_t>(buffer_size));  // unmodified.
212 
213   buffer_size = sizeof(fixed_buffer);
214   dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::min(),
215                            fixed_buffer, &buffer_size);
216   EXPECT_EQ(nullptr, dest);
217   EXPECT_EQ(sizeof(fixed_buffer),
218             static_cast<size_t>(buffer_size));  // unmodified.
219 
220   buffer_size = sizeof(fixed_buffer);
221   dest = FPDF_GetNamedDest(document(), -1, fixed_buffer, &buffer_size);
222   EXPECT_EQ(nullptr, dest);
223   EXPECT_EQ(sizeof(fixed_buffer),
224             static_cast<size_t>(buffer_size));  // unmodified.
225 }
226 
TEST_F(FPDFViewEmbeddertest,NamedDestsByName)227 TEST_F(FPDFViewEmbeddertest, NamedDestsByName) {
228   EXPECT_TRUE(OpenDocument("named_dests.pdf"));
229 
230   // Null pointer returns nullptr.
231   FPDF_DEST dest = FPDF_GetNamedDestByName(document(), nullptr);
232   EXPECT_EQ(nullptr, dest);
233 
234   // Empty string returns nullptr.
235   dest = FPDF_GetNamedDestByName(document(), "");
236   EXPECT_EQ(nullptr, dest);
237 
238   // Item from Dests NameTree.
239   dest = FPDF_GetNamedDestByName(document(), "First");
240   EXPECT_NE(nullptr, dest);
241 
242   long ignore_len = 0;
243   FPDF_DEST dest_by_index =
244       FPDF_GetNamedDest(document(), 0, nullptr, &ignore_len);
245   EXPECT_EQ(dest_by_index, dest);
246 
247   // Item from Dests dictionary.
248   dest = FPDF_GetNamedDestByName(document(), "FirstAlternate");
249   EXPECT_NE(nullptr, dest);
250 
251   ignore_len = 0;
252   dest_by_index = FPDF_GetNamedDest(document(), 4, nullptr, &ignore_len);
253   EXPECT_EQ(dest_by_index, dest);
254 
255   // Bad value type for item from Dests NameTree array.
256   dest = FPDF_GetNamedDestByName(document(), "WrongType");
257   EXPECT_EQ(nullptr, dest);
258 
259   // No such destination in either Dest NameTree or dictionary.
260   dest = FPDF_GetNamedDestByName(document(), "Bogus");
261   EXPECT_EQ(nullptr, dest);
262 }
263 
264 // The following tests pass if the document opens without crashing.
TEST_F(FPDFViewEmbeddertest,Crasher_113)265 TEST_F(FPDFViewEmbeddertest, Crasher_113) {
266   EXPECT_TRUE(OpenDocument("bug_113.pdf"));
267 }
268 
TEST_F(FPDFViewEmbeddertest,Crasher_451830)269 TEST_F(FPDFViewEmbeddertest, Crasher_451830) {
270   // Document is damaged and can't be opened.
271   EXPECT_FALSE(OpenDocument("bug_451830.pdf"));
272 }
273 
TEST_F(FPDFViewEmbeddertest,Crasher_452455)274 TEST_F(FPDFViewEmbeddertest, Crasher_452455) {
275   EXPECT_TRUE(OpenDocument("bug_452455.pdf"));
276   FPDF_PAGE page = LoadPage(0);
277   EXPECT_NE(nullptr, page);
278   UnloadPage(page);
279 }
280 
TEST_F(FPDFViewEmbeddertest,Crasher_454695)281 TEST_F(FPDFViewEmbeddertest, Crasher_454695) {
282   // Document is damaged and can't be opened.
283   EXPECT_FALSE(OpenDocument("bug_454695.pdf"));
284 }
285 
TEST_F(FPDFViewEmbeddertest,Crasher_572871)286 TEST_F(FPDFViewEmbeddertest, Crasher_572871) {
287   EXPECT_TRUE(OpenDocument("bug_572871.pdf"));
288 }
289 
290 // It tests that document can still be loaded even the trailer has no 'Size'
291 // field if other information is right.
TEST_F(FPDFViewEmbeddertest,Failed_213)292 TEST_F(FPDFViewEmbeddertest, Failed_213) {
293   EXPECT_TRUE(OpenDocument("bug_213.pdf"));
294 }
295 
296 // The following tests pass if the document opens without infinite looping.
TEST_F(FPDFViewEmbeddertest,Hang_298)297 TEST_F(FPDFViewEmbeddertest, Hang_298) {
298   EXPECT_FALSE(OpenDocument("bug_298.pdf"));
299 }
300 
301 // Test if the document opens without infinite looping.
302 // Previously this test will hang in a loop inside LoadAllCrossRefV4. After
303 // the fix, LoadAllCrossRefV4 will return false after detecting a cross
304 // reference loop. Cross references will be rebuilt successfully.
TEST_F(FPDFViewEmbeddertest,CrossRefV4Loop)305 TEST_F(FPDFViewEmbeddertest, CrossRefV4Loop) {
306   EXPECT_TRUE(OpenDocument("bug_xrefv4_loop.pdf"));
307 }
308 
309 // The test should pass when circular references to ParseIndirectObject will not
310 // cause infinite loop.
TEST_F(FPDFViewEmbeddertest,Hang_343)311 TEST_F(FPDFViewEmbeddertest, Hang_343) {
312   EXPECT_FALSE(OpenDocument("bug_343.pdf"));
313 }
314 
315 // The test should pass when the absence of 'Contents' field in a signature
316 // dictionary will not cause an infinite loop in CPDF_SyntaxParser::GetObject().
TEST_F(FPDFViewEmbeddertest,Hang_344)317 TEST_F(FPDFViewEmbeddertest, Hang_344) {
318   EXPECT_FALSE(OpenDocument("bug_344.pdf"));
319 }
320 
321 // The test should pass when there is no infinite recursion in
322 // CPDF_SyntaxParser::GetString().
TEST_F(FPDFViewEmbeddertest,Hang_355)323 TEST_F(FPDFViewEmbeddertest, Hang_355) {
324   EXPECT_FALSE(OpenDocument("bug_355.pdf"));
325 }
326 // The test should pass even when the file has circular references to pages.
TEST_F(FPDFViewEmbeddertest,Hang_360)327 TEST_F(FPDFViewEmbeddertest, Hang_360) {
328   EXPECT_FALSE(OpenDocument("bug_360.pdf"));
329 }
330 
TEST_F(FPDFViewEmbeddertest,FPDF_RenderPageBitmapWithMatrix)331 TEST_F(FPDFViewEmbeddertest, FPDF_RenderPageBitmapWithMatrix) {
332   const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615";
333   const char kTopLeftQuarterBlackMd5sum[] = "24e4d1ec06fa0258af758cfc8b2ad50a";
334 
335   EXPECT_TRUE(OpenDocument("black.pdf"));
336   FPDF_PAGE page = LoadPage(0);
337   EXPECT_NE(nullptr, page);
338   const int width = static_cast<int>(FPDF_GetPageWidth(page));
339   const int height = static_cast<int>(FPDF_GetPageHeight(page));
340   EXPECT_EQ(612, width);
341   EXPECT_EQ(792, height);
342 
343   FPDF_BITMAP bitmap = RenderPage(page);
344   CompareBitmap(bitmap, width, height, kAllBlackMd5sum);
345   FPDFBitmap_Destroy(bitmap);
346 
347   // Try rendering with an identity matrix. The output should be the same as
348   // the RenderPage() output.
349   FS_MATRIX matrix;
350   matrix.a = 1;
351   matrix.b = 0;
352   matrix.c = 0;
353   matrix.d = 1;
354   matrix.e = 0;
355   matrix.f = 0;
356 
357   FS_RECTF rect;
358   rect.left = 0;
359   rect.top = 0;
360   rect.right = width;
361   rect.bottom = height;
362 
363   bitmap = FPDFBitmap_Create(width, height, 0);
364   FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
365   FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0);
366   CompareBitmap(bitmap, width, height, kAllBlackMd5sum);
367   FPDFBitmap_Destroy(bitmap);
368 
369   // Now render again with the image scaled.
370   matrix.a = 0.5;
371   matrix.d = 0.5;
372 
373   bitmap = FPDFBitmap_Create(width, height, 0);
374   FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
375   FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0);
376   CompareBitmap(bitmap, width, height, kTopLeftQuarterBlackMd5sum);
377   FPDFBitmap_Destroy(bitmap);
378 
379   UnloadPage(page);
380 }
381