• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cmath>
6 #include <limits>
7 #include <string>
8 
9 #include "fpdfsdk/fpdfview_c_api_test.h"
10 #include "public/fpdfview.h"
11 #include "testing/embedder_test.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/utils/path_service.h"
14 
15 namespace {
16 
17 class MockDownloadHints : public FX_DOWNLOADHINTS {
18  public:
SAddSegment(FX_DOWNLOADHINTS * pThis,size_t offset,size_t size)19   static void SAddSegment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
20   }
21 
MockDownloadHints()22   MockDownloadHints() {
23     FX_DOWNLOADHINTS::version = 1;
24     FX_DOWNLOADHINTS::AddSegment = SAddSegment;
25   }
26 
~MockDownloadHints()27   ~MockDownloadHints() {}
28 };
29 
30 }  // namespace
31 
TEST(fpdf,CApiTest)32 TEST(fpdf, CApiTest) {
33   EXPECT_TRUE(CheckPDFiumCApi());
34 }
35 
36 class FPDFViewEmbeddertest : public EmbedderTest {
37  protected:
38   void TestRenderPageBitmapWithMatrix(FPDF_PAGE page,
39                                       const int bitmap_width,
40                                       const int bitmap_height,
41                                       const FS_MATRIX& matrix,
42                                       const FS_RECTF& rect,
43                                       const char* expected_md5);
44 };
45 
TEST_F(FPDFViewEmbeddertest,Document)46 TEST_F(FPDFViewEmbeddertest, Document) {
47   EXPECT_TRUE(OpenDocument("about_blank.pdf"));
48   EXPECT_EQ(1, GetPageCount());
49   EXPECT_EQ(0, GetFirstPageNum());
50 
51   int version;
52   EXPECT_TRUE(FPDF_GetFileVersion(document(), &version));
53   EXPECT_EQ(14, version);
54 
55   EXPECT_EQ(0xFFFFFFFF, FPDF_GetDocPermissions(document()));
56   EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document()));
57 }
58 
TEST_F(FPDFViewEmbeddertest,LoadNonexistentDocument)59 TEST_F(FPDFViewEmbeddertest, LoadNonexistentDocument) {
60   FPDF_DOCUMENT doc = FPDF_LoadDocument("nonexistent_document.pdf", "");
61   ASSERT_FALSE(doc);
62   EXPECT_EQ(static_cast<int>(FPDF_GetLastError()), FPDF_ERR_FILE);
63 }
64 
65 // See bug 465.
TEST_F(FPDFViewEmbeddertest,EmptyDocument)66 TEST_F(FPDFViewEmbeddertest, EmptyDocument) {
67   EXPECT_TRUE(CreateEmptyDocument());
68 
69   {
70     int version = 42;
71     EXPECT_FALSE(FPDF_GetFileVersion(document(), &version));
72     EXPECT_EQ(0, version);
73   }
74 
75   {
76 #ifndef PDF_ENABLE_XFA
77     const unsigned long kExpected = 0;
78 #else
79     const unsigned long kExpected = static_cast<uint32_t>(-1);
80 #endif
81     EXPECT_EQ(kExpected, FPDF_GetDocPermissions(document()));
82   }
83 
84   EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document()));
85 
86   EXPECT_EQ(0, FPDF_GetPageCount(document()));
87 
88   EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
89   EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document()));
90   EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
91 
92   char buf[100];
93   EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
94   EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
95 
96   EXPECT_EQ(0u, FPDF_CountNamedDests(document()));
97 }
98 
TEST_F(FPDFViewEmbeddertest,LinearizedDocument)99 TEST_F(FPDFViewEmbeddertest, LinearizedDocument) {
100   EXPECT_TRUE(OpenDocumentLinearized("feature_linearized_loading.pdf"));
101   int version;
102   EXPECT_TRUE(FPDF_GetFileVersion(document(), &version));
103   EXPECT_EQ(16, version);
104 }
105 
TEST_F(FPDFViewEmbeddertest,Page)106 TEST_F(FPDFViewEmbeddertest, Page) {
107   EXPECT_TRUE(OpenDocument("about_blank.pdf"));
108   FPDF_PAGE page = LoadPage(0);
109   EXPECT_NE(nullptr, page);
110 
111   EXPECT_EQ(612.0, FPDF_GetPageWidth(page));
112   EXPECT_EQ(792.0, FPDF_GetPageHeight(page));
113 
114   FS_RECTF rect;
115   EXPECT_TRUE(FPDF_GetPageBoundingBox(page, &rect));
116   EXPECT_EQ(0.0, rect.left);
117   EXPECT_EQ(0.0, rect.bottom);
118   EXPECT_EQ(612.0, rect.right);
119   EXPECT_EQ(792.0, rect.top);
120 
121   UnloadPage(page);
122   EXPECT_EQ(nullptr, LoadPage(1));
123 }
124 
TEST_F(FPDFViewEmbeddertest,ViewerRefDummy)125 TEST_F(FPDFViewEmbeddertest, ViewerRefDummy) {
126   EXPECT_TRUE(OpenDocument("about_blank.pdf"));
127   EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
128   EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document()));
129   EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
130 
131   char buf[100];
132   EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
133   EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
134 }
135 
TEST_F(FPDFViewEmbeddertest,ViewerRef)136 TEST_F(FPDFViewEmbeddertest, ViewerRef) {
137   EXPECT_TRUE(OpenDocument("viewer_ref.pdf"));
138   EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
139   EXPECT_EQ(5, FPDF_VIEWERREF_GetNumCopies(document()));
140   EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
141 
142   // Test some corner cases.
143   char buf[100];
144   EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "", buf, sizeof(buf)));
145   EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
146   EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
147 
148   // Make sure |buf| does not get written into when it appears to be too small.
149   // NOLINTNEXTLINE(runtime/printf)
150   strcpy(buf, "ABCD");
151   EXPECT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, 1));
152   EXPECT_STREQ("ABCD", buf);
153 
154   // Note "Foo" is a different key from "foo".
155   EXPECT_EQ(4U,
156             FPDF_VIEWERREF_GetName(document(), "Foo", nullptr, sizeof(buf)));
157   ASSERT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, sizeof(buf)));
158   EXPECT_STREQ("foo", buf);
159 
160   // Try to retrieve a boolean and an integer.
161   EXPECT_EQ(
162       0U, FPDF_VIEWERREF_GetName(document(), "HideToolbar", buf, sizeof(buf)));
163   EXPECT_EQ(0U,
164             FPDF_VIEWERREF_GetName(document(), "NumCopies", buf, sizeof(buf)));
165 
166   // Try more valid cases.
167   ASSERT_EQ(4U,
168             FPDF_VIEWERREF_GetName(document(), "Direction", buf, sizeof(buf)));
169   EXPECT_STREQ("R2L", buf);
170   ASSERT_EQ(8U,
171             FPDF_VIEWERREF_GetName(document(), "ViewArea", buf, sizeof(buf)));
172   EXPECT_STREQ("CropBox", buf);
173 }
174 
TEST_F(FPDFViewEmbeddertest,NamedDests)175 TEST_F(FPDFViewEmbeddertest, NamedDests) {
176   EXPECT_TRUE(OpenDocument("named_dests.pdf"));
177   long buffer_size;
178   char fixed_buffer[512];
179   FPDF_DEST dest;
180 
181   // Query the size of the first item.
182   buffer_size = 2000000;  // Absurdly large, check not used for this case.
183   dest = FPDF_GetNamedDest(document(), 0, nullptr, &buffer_size);
184   EXPECT_NE(nullptr, dest);
185   EXPECT_EQ(12, buffer_size);
186 
187   // Try to retrieve the first item with too small a buffer.
188   buffer_size = 10;
189   dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size);
190   EXPECT_NE(nullptr, dest);
191   EXPECT_EQ(-1, buffer_size);
192 
193   // Try to retrieve the first item with correctly sized buffer. Item is
194   // taken from Dests NameTree in named_dests.pdf.
195   buffer_size = 12;
196   dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size);
197   EXPECT_NE(nullptr, dest);
198   EXPECT_EQ(12, buffer_size);
199   EXPECT_EQ(std::string("F\0i\0r\0s\0t\0\0\0", 12),
200             std::string(fixed_buffer, buffer_size));
201 
202   // Try to retrieve the second item with ample buffer. Item is taken
203   // from Dests NameTree but has a sub-dictionary in named_dests.pdf.
204   buffer_size = sizeof(fixed_buffer);
205   dest = FPDF_GetNamedDest(document(), 1, fixed_buffer, &buffer_size);
206   EXPECT_NE(nullptr, dest);
207   EXPECT_EQ(10, buffer_size);
208   EXPECT_EQ(std::string("N\0e\0x\0t\0\0\0", 10),
209             std::string(fixed_buffer, buffer_size));
210 
211   // Try to retrieve third item with ample buffer. Item is taken
212   // from Dests NameTree but has a bad sub-dictionary in named_dests.pdf.
213   // in named_dests.pdf).
214   buffer_size = sizeof(fixed_buffer);
215   dest = FPDF_GetNamedDest(document(), 2, 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   // Try to retrieve the forth item with ample buffer. Item is taken
221   // from Dests NameTree but has a vale of the wrong type in named_dests.pdf.
222   buffer_size = sizeof(fixed_buffer);
223   dest = FPDF_GetNamedDest(document(), 3, fixed_buffer, &buffer_size);
224   EXPECT_EQ(nullptr, dest);
225   EXPECT_EQ(sizeof(fixed_buffer),
226             static_cast<size_t>(buffer_size));  // unmodified.
227 
228   // Try to retrieve fifth item with ample buffer. Item taken from the
229   // old-style Dests dictionary object in named_dests.pdf.
230   buffer_size = sizeof(fixed_buffer);
231   dest = FPDF_GetNamedDest(document(), 4, fixed_buffer, &buffer_size);
232   EXPECT_NE(nullptr, dest);
233   EXPECT_EQ(30, buffer_size);
234   EXPECT_EQ(std::string("F\0i\0r\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 30),
235             std::string(fixed_buffer, buffer_size));
236 
237   // Try to retrieve sixth item with ample buffer. Item istaken from the
238   // old-style Dests dictionary object but has a sub-dictionary in
239   // named_dests.pdf.
240   buffer_size = sizeof(fixed_buffer);
241   dest = FPDF_GetNamedDest(document(), 5, fixed_buffer, &buffer_size);
242   EXPECT_NE(nullptr, dest);
243   EXPECT_EQ(28, buffer_size);
244   EXPECT_EQ(std::string("L\0a\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 28),
245             std::string(fixed_buffer, buffer_size));
246 
247   // Try to retrieve non-existent item with ample buffer.
248   buffer_size = sizeof(fixed_buffer);
249   dest = FPDF_GetNamedDest(document(), 6, fixed_buffer, &buffer_size);
250   EXPECT_EQ(nullptr, dest);
251   EXPECT_EQ(sizeof(fixed_buffer),
252             static_cast<size_t>(buffer_size));  // unmodified.
253 
254   // Try to underflow/overflow the integer index.
255   buffer_size = sizeof(fixed_buffer);
256   dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::max(),
257                            fixed_buffer, &buffer_size);
258   EXPECT_EQ(nullptr, dest);
259   EXPECT_EQ(sizeof(fixed_buffer),
260             static_cast<size_t>(buffer_size));  // unmodified.
261 
262   buffer_size = sizeof(fixed_buffer);
263   dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::min(),
264                            fixed_buffer, &buffer_size);
265   EXPECT_EQ(nullptr, dest);
266   EXPECT_EQ(sizeof(fixed_buffer),
267             static_cast<size_t>(buffer_size));  // unmodified.
268 
269   buffer_size = sizeof(fixed_buffer);
270   dest = FPDF_GetNamedDest(document(), -1, fixed_buffer, &buffer_size);
271   EXPECT_EQ(nullptr, dest);
272   EXPECT_EQ(sizeof(fixed_buffer),
273             static_cast<size_t>(buffer_size));  // unmodified.
274 }
275 
TEST_F(FPDFViewEmbeddertest,NamedDestsByName)276 TEST_F(FPDFViewEmbeddertest, NamedDestsByName) {
277   EXPECT_TRUE(OpenDocument("named_dests.pdf"));
278 
279   // Null pointer returns nullptr.
280   FPDF_DEST dest = FPDF_GetNamedDestByName(document(), nullptr);
281   EXPECT_EQ(nullptr, dest);
282 
283   // Empty string returns nullptr.
284   dest = FPDF_GetNamedDestByName(document(), "");
285   EXPECT_EQ(nullptr, dest);
286 
287   // Item from Dests NameTree.
288   dest = FPDF_GetNamedDestByName(document(), "First");
289   EXPECT_NE(nullptr, dest);
290 
291   long ignore_len = 0;
292   FPDF_DEST dest_by_index =
293       FPDF_GetNamedDest(document(), 0, nullptr, &ignore_len);
294   EXPECT_EQ(dest_by_index, dest);
295 
296   // Item from Dests dictionary.
297   dest = FPDF_GetNamedDestByName(document(), "FirstAlternate");
298   EXPECT_NE(nullptr, dest);
299 
300   ignore_len = 0;
301   dest_by_index = FPDF_GetNamedDest(document(), 4, nullptr, &ignore_len);
302   EXPECT_EQ(dest_by_index, dest);
303 
304   // Bad value type for item from Dests NameTree array.
305   dest = FPDF_GetNamedDestByName(document(), "WrongType");
306   EXPECT_EQ(nullptr, dest);
307 
308   // No such destination in either Dest NameTree or dictionary.
309   dest = FPDF_GetNamedDestByName(document(), "Bogus");
310   EXPECT_EQ(nullptr, dest);
311 }
312 
313 // The following tests pass if the document opens without crashing.
TEST_F(FPDFViewEmbeddertest,Crasher_113)314 TEST_F(FPDFViewEmbeddertest, Crasher_113) {
315   EXPECT_TRUE(OpenDocument("bug_113.pdf"));
316 }
317 
TEST_F(FPDFViewEmbeddertest,Crasher_451830)318 TEST_F(FPDFViewEmbeddertest, Crasher_451830) {
319   // Document is damaged and can't be opened.
320   EXPECT_FALSE(OpenDocument("bug_451830.pdf"));
321 }
322 
TEST_F(FPDFViewEmbeddertest,Crasher_452455)323 TEST_F(FPDFViewEmbeddertest, Crasher_452455) {
324   EXPECT_TRUE(OpenDocument("bug_452455.pdf"));
325   FPDF_PAGE page = LoadPage(0);
326   EXPECT_NE(nullptr, page);
327   UnloadPage(page);
328 }
329 
TEST_F(FPDFViewEmbeddertest,Crasher_454695)330 TEST_F(FPDFViewEmbeddertest, Crasher_454695) {
331   // Document is damaged and can't be opened.
332   EXPECT_FALSE(OpenDocument("bug_454695.pdf"));
333 }
334 
TEST_F(FPDFViewEmbeddertest,Crasher_572871)335 TEST_F(FPDFViewEmbeddertest, Crasher_572871) {
336   EXPECT_TRUE(OpenDocument("bug_572871.pdf"));
337 }
338 
339 // It tests that document can still be loaded even the trailer has no 'Size'
340 // field if other information is right.
TEST_F(FPDFViewEmbeddertest,Failed_213)341 TEST_F(FPDFViewEmbeddertest, Failed_213) {
342   EXPECT_TRUE(OpenDocument("bug_213.pdf"));
343 }
344 
345 // The following tests pass if the document opens without infinite looping.
TEST_F(FPDFViewEmbeddertest,Hang_298)346 TEST_F(FPDFViewEmbeddertest, Hang_298) {
347   EXPECT_FALSE(OpenDocument("bug_298.pdf"));
348 }
349 
350 // Test if the document opens without infinite looping.
351 // Previously this test will hang in a loop inside LoadAllCrossRefV4. After
352 // the fix, LoadAllCrossRefV4 will return false after detecting a cross
353 // reference loop. Cross references will be rebuilt successfully.
TEST_F(FPDFViewEmbeddertest,CrossRefV4Loop)354 TEST_F(FPDFViewEmbeddertest, CrossRefV4Loop) {
355   EXPECT_TRUE(OpenDocument("bug_xrefv4_loop.pdf"));
356   MockDownloadHints hints;
357 
358   // Make sure calling FPDFAvail_IsDocAvail() on this file does not infinite
359   // loop either. See bug 875.
360   int ret = PDF_DATA_NOTAVAIL;
361   while (ret == PDF_DATA_NOTAVAIL)
362     ret = FPDFAvail_IsDocAvail(avail_, &hints);
363   EXPECT_EQ(PDF_DATA_AVAIL, ret);
364 }
365 
366 // The test should pass when circular references to ParseIndirectObject will not
367 // cause infinite loop.
TEST_F(FPDFViewEmbeddertest,Hang_343)368 TEST_F(FPDFViewEmbeddertest, Hang_343) {
369   EXPECT_FALSE(OpenDocument("bug_343.pdf"));
370 }
371 
372 // The test should pass when the absence of 'Contents' field in a signature
373 // dictionary will not cause an infinite loop in CPDF_SyntaxParser::GetObject().
TEST_F(FPDFViewEmbeddertest,Hang_344)374 TEST_F(FPDFViewEmbeddertest, Hang_344) {
375   EXPECT_FALSE(OpenDocument("bug_344.pdf"));
376 }
377 
378 // The test should pass when there is no infinite recursion in
379 // CPDF_SyntaxParser::GetString().
TEST_F(FPDFViewEmbeddertest,Hang_355)380 TEST_F(FPDFViewEmbeddertest, Hang_355) {
381   EXPECT_FALSE(OpenDocument("bug_355.pdf"));
382 }
383 // The test should pass even when the file has circular references to pages.
TEST_F(FPDFViewEmbeddertest,Hang_360)384 TEST_F(FPDFViewEmbeddertest, Hang_360) {
385   EXPECT_FALSE(OpenDocument("bug_360.pdf"));
386 }
387 
TestRenderPageBitmapWithMatrix(FPDF_PAGE page,const int bitmap_width,const int bitmap_height,const FS_MATRIX & matrix,const FS_RECTF & rect,const char * expected_md5)388 void FPDFViewEmbeddertest::TestRenderPageBitmapWithMatrix(
389     FPDF_PAGE page,
390     const int bitmap_width,
391     const int bitmap_height,
392     const FS_MATRIX& matrix,
393     const FS_RECTF& rect,
394     const char* expected_md5) {
395   FPDF_BITMAP bitmap = FPDFBitmap_Create(bitmap_width, bitmap_height, 0);
396   FPDFBitmap_FillRect(bitmap, 0, 0, bitmap_width, bitmap_height, 0xFFFFFFFF);
397   FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0);
398   CompareBitmap(bitmap, bitmap_width, bitmap_height, expected_md5);
399   FPDFBitmap_Destroy(bitmap);
400 }
401 
TEST_F(FPDFViewEmbeddertest,FPDF_RenderPageBitmapWithMatrix)402 TEST_F(FPDFViewEmbeddertest, FPDF_RenderPageBitmapWithMatrix) {
403   const char kOriginalMD5[] = "0a90de37f52127619c3dfb642b5fa2fe";
404   const char kClippedMD5[] = "a84cab93c102b9b9290fba3047ba702c";
405   const char kTopLeftQuarterMD5[] = "f11a11137c8834389e31cf555a4a6979";
406   const char kHoriStretchedMD5[] = "48ef9205941ed19691ccfa00d717187e";
407   const char kRotated90ClockwiseMD5[] = "d8da2c7bf77521550d0f2752b9cf3482";
408   const char kRotated180ClockwiseMD5[] = "0113386bb0bd45125bacc6dee78bfe78";
409   const char kRotated270ClockwiseMD5[] = "a287e0f74ce203699cda89f9cc97a240";
410   const char kMirrorHoriMD5[] = "6e8d7a6fde39d8e720fb9e620102918c";
411   const char kMirrorVertMD5[] = "8f3a555ef9c0d5031831ae3715273707";
412   const char kLargerTopLeftQuarterMD5[] = "172a2f4adafbadbe98017b1c025b9e27";
413   const char kLargerMD5[] = "c806145641c3e6fc4e022c7065343749";
414   const char kLargerClippedMD5[] = "091d3b1c7933c8f6945eb2cb41e588e9";
415   const char kLargerRotatedMD5[] = "115f13353ebfc82ddb392d1f0059eb12";
416   const char kLargerRotatedLandscapeMD5[] = "c901239d17d84ac84cb6f2124da71b0d";
417   const char kLargerRotatedDiagonalMD5[] = "3d62417468bdaff0eb14391a0c30a3b1";
418   const char kTileMD5[] = "0a190003c97220bf8877684c8d7e89cf";
419 
420   EXPECT_TRUE(OpenDocument("rectangles.pdf"));
421   FPDF_PAGE page = LoadPage(0);
422   EXPECT_NE(nullptr, page);
423   const int page_width = static_cast<int>(FPDF_GetPageWidth(page));
424   const int page_height = static_cast<int>(FPDF_GetPageHeight(page));
425   EXPECT_EQ(200, page_width);
426   EXPECT_EQ(300, page_height);
427 
428   FPDF_BITMAP bitmap = RenderPage(page);
429   CompareBitmap(bitmap, page_width, page_height, kOriginalMD5);
430   FPDFBitmap_Destroy(bitmap);
431 
432   FS_RECTF page_rect{0, 0, page_width, page_height};
433 
434   // Try rendering with an identity matrix. The output should be the same as
435   // the RenderPage() output.
436   FS_MATRIX identity_matrix{1, 0, 0, 1, 0, 0};
437   TestRenderPageBitmapWithMatrix(page, page_width, page_height, identity_matrix,
438                                  page_rect, kOriginalMD5);
439 
440   // Again render with an identity matrix but with a smaller clipping rect.
441   FS_RECTF middle_of_page_rect{page_width / 4, page_height / 4,
442                                page_width * 3 / 4, page_height * 3 / 4};
443   TestRenderPageBitmapWithMatrix(page, page_width, page_height, identity_matrix,
444                                  middle_of_page_rect, kClippedMD5);
445 
446   // Now render again with the image scaled smaller.
447   FS_MATRIX half_scale_matrix{0.5, 0, 0, 0.5, 0, 0};
448   TestRenderPageBitmapWithMatrix(page, page_width, page_height,
449                                  half_scale_matrix, page_rect,
450                                  kTopLeftQuarterMD5);
451 
452   // Now render again with the image scaled larger horizontally (the right half
453   // will be clipped).
454   FS_MATRIX stretch_x_matrix{2, 0, 0, 1, 0, 0};
455   TestRenderPageBitmapWithMatrix(page, page_width, page_height,
456                                  stretch_x_matrix, page_rect,
457                                  kHoriStretchedMD5);
458 
459   // Try a 90 degree rotation clockwise but with the same bitmap size, so part
460   // will be clipped.
461   FS_MATRIX rotate_90_matrix{0, 1, -1, 0, page_width, 0};
462   TestRenderPageBitmapWithMatrix(page, page_width, page_height,
463                                  rotate_90_matrix, page_rect,
464                                  kRotated90ClockwiseMD5);
465 
466   // 180 degree rotation clockwise.
467   FS_MATRIX rotate_180_matrix{-1, 0, 0, -1, page_width, page_height};
468   TestRenderPageBitmapWithMatrix(page, page_width, page_height,
469                                  rotate_180_matrix, page_rect,
470                                  kRotated180ClockwiseMD5);
471 
472   // 270 degree rotation clockwise.
473   FS_MATRIX rotate_270_matrix{0, -1, 1, 0, 0, page_width};
474   TestRenderPageBitmapWithMatrix(page, page_width, page_height,
475                                  rotate_270_matrix, page_rect,
476                                  kRotated270ClockwiseMD5);
477 
478   // Mirror horizontally.
479   FS_MATRIX mirror_hori_matrix{-1, 0, 0, 1, page_width, 0};
480   TestRenderPageBitmapWithMatrix(page, page_width, page_height,
481                                  mirror_hori_matrix, page_rect, kMirrorHoriMD5);
482 
483   // Mirror vertically.
484   FS_MATRIX mirror_vert_matrix{1, 0, 0, -1, 0, page_height};
485   TestRenderPageBitmapWithMatrix(page, page_width, page_height,
486                                  mirror_vert_matrix, page_rect, kMirrorVertMD5);
487 
488   // Tests rendering to a larger bitmap
489   const int bitmap_width = page_width * 2;
490   const int bitmap_height = page_height * 2;
491 
492   // Render using an identity matrix and the whole bitmap area as clipping rect.
493   FS_RECTF bitmap_rect{0, 0, bitmap_width, bitmap_height};
494   TestRenderPageBitmapWithMatrix(page, bitmap_width, bitmap_height,
495                                  identity_matrix, bitmap_rect,
496                                  kLargerTopLeftQuarterMD5);
497 
498   // Render using a scaling matrix to fill the larger bitmap.
499   FS_MATRIX double_scale_matrix{2, 0, 0, 2, 0, 0};
500   TestRenderPageBitmapWithMatrix(page, bitmap_width, bitmap_height,
501                                  double_scale_matrix, bitmap_rect, kLargerMD5);
502 
503   // Render the larger image again but with clipping.
504   FS_RECTF middle_of_bitmap_rect{bitmap_width / 4, bitmap_height / 4,
505                                  bitmap_width * 3 / 4, bitmap_height * 3 / 4};
506   TestRenderPageBitmapWithMatrix(page, bitmap_width, bitmap_height,
507                                  double_scale_matrix, middle_of_bitmap_rect,
508                                  kLargerClippedMD5);
509 
510   // On the larger bitmap, try a 90 degree rotation but with the same bitmap
511   // size, so part will be clipped.
512   FS_MATRIX rotate_90_scale_2_matrix{0, 2, -2, 0, bitmap_width, 0};
513   TestRenderPageBitmapWithMatrix(page, bitmap_width, bitmap_height,
514                                  rotate_90_scale_2_matrix, bitmap_rect,
515                                  kLargerRotatedMD5);
516 
517   // On the larger bitmap, apply 90 degree rotation to a bitmap with the
518   // appropriate dimensions.
519   const int landscape_bitmap_width = bitmap_height;
520   const int landscape_bitmap_height = bitmap_width;
521   FS_RECTF landscape_bitmap_rect{0, 0, landscape_bitmap_width,
522                                  landscape_bitmap_height};
523   FS_MATRIX landscape_rotate_90_scale_2_matrix{
524       0, 2, -2, 0, landscape_bitmap_width, 0};
525   TestRenderPageBitmapWithMatrix(
526       page, landscape_bitmap_width, landscape_bitmap_height,
527       landscape_rotate_90_scale_2_matrix, landscape_bitmap_rect,
528       kLargerRotatedLandscapeMD5);
529 
530   // On the larger bitmap, apply 45 degree rotation to a bitmap with the
531   // appropriate dimensions.
532   const float sqrt2 = 1.41421356f;
533   const int diagonal_bitmap_size = ceil((bitmap_width + bitmap_height) / sqrt2);
534   FS_RECTF diagonal_bitmap_rect{0, 0, diagonal_bitmap_size,
535                                 diagonal_bitmap_size};
536   FS_MATRIX rotate_45_scale_2_matrix{
537       sqrt2, sqrt2, -sqrt2, sqrt2, bitmap_height / sqrt2, 0};
538   TestRenderPageBitmapWithMatrix(page, diagonal_bitmap_size,
539                                  diagonal_bitmap_size, rotate_45_scale_2_matrix,
540                                  diagonal_bitmap_rect,
541                                  kLargerRotatedDiagonalMD5);
542 
543   // Render the (2, 1) tile of the page (third column, second row) when the page
544   // is divided in 50x50 pixel tiles. The tile is scaled by a factor of 7.
545   const float scale = 7.0;
546   const int tile_size = 50;
547   const int tile_x = 2;
548   const int tile_y = 1;
549   int tile_bitmap_size = scale * tile_size;
550   FS_RECTF tile_bitmap_rect{0, 0, tile_bitmap_size, tile_bitmap_size};
551   FS_MATRIX tile_2_1_matrix{scale,
552                             0,
553                             0,
554                             scale,
555                             -tile_x * tile_bitmap_size,
556                             -tile_y * tile_bitmap_size};
557   TestRenderPageBitmapWithMatrix(page, tile_bitmap_size, tile_bitmap_size,
558                                  tile_2_1_matrix, tile_bitmap_rect, kTileMD5);
559 
560   UnloadPage(page);
561 }
562 
563 class UnSupRecordDelegate : public EmbedderTest::Delegate {
564  public:
UnSupRecordDelegate()565   UnSupRecordDelegate() : type_(-1) {}
~UnSupRecordDelegate()566   ~UnSupRecordDelegate() override {}
567 
UnsupportedHandler(int type)568   void UnsupportedHandler(int type) override { type_ = type; }
569 
570   int type_;
571 };
572 
TEST_F(FPDFViewEmbeddertest,UnSupportedOperations_NotFound)573 TEST_F(FPDFViewEmbeddertest, UnSupportedOperations_NotFound) {
574   UnSupRecordDelegate delegate;
575   SetDelegate(&delegate);
576   ASSERT_TRUE(OpenDocument("hello_world.pdf"));
577   EXPECT_EQ(delegate.type_, -1);
578   SetDelegate(nullptr);
579 }
580 
TEST_F(FPDFViewEmbeddertest,UnSupportedOperations_LoadCustomDocument)581 TEST_F(FPDFViewEmbeddertest, UnSupportedOperations_LoadCustomDocument) {
582   UnSupRecordDelegate delegate;
583   SetDelegate(&delegate);
584   ASSERT_TRUE(OpenDocument("unsupported_feature.pdf"));
585   EXPECT_EQ(FPDF_UNSP_DOC_PORTABLECOLLECTION, delegate.type_);
586   SetDelegate(nullptr);
587 }
588 
TEST_F(FPDFViewEmbeddertest,UnSupportedOperations_LoadDocument)589 TEST_F(FPDFViewEmbeddertest, UnSupportedOperations_LoadDocument) {
590   std::string file_path;
591   ASSERT_TRUE(
592       PathService::GetTestFilePath("unsupported_feature.pdf", &file_path));
593 
594   UnSupRecordDelegate delegate;
595   SetDelegate(&delegate);
596   FPDF_DOCUMENT doc = FPDF_LoadDocument(file_path.c_str(), "");
597   EXPECT_TRUE(doc != nullptr);
598   EXPECT_EQ(FPDF_UNSP_DOC_PORTABLECOLLECTION, delegate.type_);
599   FPDF_CloseDocument(doc);
600   SetDelegate(nullptr);
601 }
602