1 // Copyright 2016 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 <memory>
6 #include <string>
7 
8 #include "public/cpp/fpdf_scopers.h"
9 #include "public/fpdf_edit.h"
10 #include "public/fpdf_ppo.h"
11 #include "public/fpdf_save.h"
12 #include "public/fpdfview.h"
13 #include "testing/embedder_test.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 namespace {
17 
18 class FPDFPPOEmbedderTest : public EmbedderTest {};
19 
FakeBlockWriter(FPDF_FILEWRITE * pThis,const void * pData,unsigned long size)20 int FakeBlockWriter(FPDF_FILEWRITE* pThis,
21                     const void* pData,
22                     unsigned long size) {
23   return size;
24 }
25 
26 }  // namespace
27 
TEST_F(FPDFPPOEmbedderTest,NoViewerPreferences)28 TEST_F(FPDFPPOEmbedderTest, NoViewerPreferences) {
29   EXPECT_TRUE(OpenDocument("hello_world.pdf"));
30 
31   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
32   EXPECT_TRUE(output_doc);
33   EXPECT_FALSE(FPDF_CopyViewerPreferences(output_doc, document()));
34   FPDF_CloseDocument(output_doc);
35 }
36 
TEST_F(FPDFPPOEmbedderTest,ViewerPreferences)37 TEST_F(FPDFPPOEmbedderTest, ViewerPreferences) {
38   EXPECT_TRUE(OpenDocument("viewer_ref.pdf"));
39 
40   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
41   EXPECT_TRUE(output_doc);
42   EXPECT_TRUE(FPDF_CopyViewerPreferences(output_doc, document()));
43   FPDF_CloseDocument(output_doc);
44 }
45 
TEST_F(FPDFPPOEmbedderTest,ImportPages)46 TEST_F(FPDFPPOEmbedderTest, ImportPages) {
47   ASSERT_TRUE(OpenDocument("viewer_ref.pdf"));
48 
49   FPDF_PAGE page = LoadPage(0);
50   EXPECT_TRUE(page);
51 
52   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
53   ASSERT_TRUE(output_doc);
54   EXPECT_TRUE(FPDF_CopyViewerPreferences(output_doc, document()));
55   EXPECT_TRUE(FPDF_ImportPages(output_doc, document(), "1", 0));
56   EXPECT_EQ(1, FPDF_GetPageCount(output_doc));
57   FPDF_CloseDocument(output_doc);
58 
59   UnloadPage(page);
60 }
61 
TEST_F(FPDFPPOEmbedderTest,ImportNPages)62 TEST_F(FPDFPPOEmbedderTest, ImportNPages) {
63   ASSERT_TRUE(OpenDocument("rectangles_multi_pages.pdf"));
64 
65   ScopedFPDFDocument output_doc_2up(
66       FPDF_ImportNPagesToOne(document(), 612, 792, 2, 1));
67   ASSERT_TRUE(output_doc_2up);
68   EXPECT_EQ(3, FPDF_GetPageCount(output_doc_2up.get()));
69   ScopedFPDFDocument output_doc_5up(
70       FPDF_ImportNPagesToOne(document(), 612, 792, 5, 1));
71   ASSERT_TRUE(output_doc_5up);
72   EXPECT_EQ(1, FPDF_GetPageCount(output_doc_5up.get()));
73   ScopedFPDFDocument output_doc_8up(
74       FPDF_ImportNPagesToOne(document(), 792, 612, 8, 1));
75   ASSERT_TRUE(output_doc_8up);
76   EXPECT_EQ(1, FPDF_GetPageCount(output_doc_8up.get()));
77   ScopedFPDFDocument output_doc_128up(
78       FPDF_ImportNPagesToOne(document(), 792, 612, 128, 1));
79   ASSERT_TRUE(output_doc_128up);
80   EXPECT_EQ(1, FPDF_GetPageCount(output_doc_128up.get()));
81 }
82 
TEST_F(FPDFPPOEmbedderTest,BadNupParams)83 TEST_F(FPDFPPOEmbedderTest, BadNupParams) {
84   ASSERT_TRUE(OpenDocument("rectangles_multi_pages.pdf"));
85 
86   FPDF_DOCUMENT output_doc_zero_row =
87       FPDF_ImportNPagesToOne(document(), 612, 792, 0, 3);
88   ASSERT_FALSE(output_doc_zero_row);
89   FPDF_DOCUMENT output_doc_zero_col =
90       FPDF_ImportNPagesToOne(document(), 612, 792, 2, 0);
91   ASSERT_FALSE(output_doc_zero_col);
92   FPDF_DOCUMENT output_doc_zero_width =
93       FPDF_ImportNPagesToOne(document(), 0, 792, 2, 1);
94   ASSERT_FALSE(output_doc_zero_width);
95   FPDF_DOCUMENT output_doc_zero_height =
96       FPDF_ImportNPagesToOne(document(), 612, 0, 7, 1);
97   ASSERT_FALSE(output_doc_zero_height);
98 }
99 
100 // TODO(Xlou): Add more tests to check output doc content of
101 // FPDF_ImportNPagesToOne()
102 // TODO(crbug.com/pdfium/11): Fix this test and enable.
103 #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
104 #define MAYBE_NupRenderImage DISABLED_NupRenderImage
105 #else
106 #define MAYBE_NupRenderImage NupRenderImage
107 #endif
TEST_F(FPDFPPOEmbedderTest,MAYBE_NupRenderImage)108 TEST_F(FPDFPPOEmbedderTest, MAYBE_NupRenderImage) {
109   ASSERT_TRUE(OpenDocument("rectangles_multi_pages.pdf"));
110   const int kPageCount = 2;
111   static constexpr const char* kExpectedMD5s[kPageCount] = {
112       "4d225b961da0f1bced7c83273e64c9b6", "fb18142190d770cfbc329d2b071aee4d"};
113   ScopedFPDFDocument output_doc_3up(
114       FPDF_ImportNPagesToOne(document(), 792, 612, 3, 1));
115   ASSERT_TRUE(output_doc_3up);
116   ASSERT_EQ(kPageCount, FPDF_GetPageCount(output_doc_3up.get()));
117   for (int i = 0; i < kPageCount; ++i) {
118     ScopedFPDFPage page(FPDF_LoadPage(output_doc_3up.get(), i));
119     ASSERT_TRUE(page);
120     ScopedFPDFBitmap bitmap = RenderPage(page.get());
121     EXPECT_EQ(792, FPDFBitmap_GetWidth(bitmap.get()));
122     EXPECT_EQ(612, FPDFBitmap_GetHeight(bitmap.get()));
123     EXPECT_EQ(kExpectedMD5s[i], HashBitmap(bitmap.get()));
124   }
125 }
126 
TEST_F(FPDFPPOEmbedderTest,BUG_925981)127 TEST_F(FPDFPPOEmbedderTest, BUG_925981) {
128   ASSERT_TRUE(OpenDocument("bug_925981.pdf"));
129   ScopedFPDFDocument output_doc_2up(
130       FPDF_ImportNPagesToOne(document(), 612, 792, 2, 1));
131   EXPECT_EQ(1, FPDF_GetPageCount(output_doc_2up.get()));
132 }
133 
TEST_F(FPDFPPOEmbedderTest,BadRepeatViewerPref)134 TEST_F(FPDFPPOEmbedderTest, BadRepeatViewerPref) {
135   ASSERT_TRUE(OpenDocument("repeat_viewer_ref.pdf"));
136 
137   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
138   EXPECT_TRUE(output_doc);
139   EXPECT_TRUE(FPDF_CopyViewerPreferences(output_doc, document()));
140 
141   FPDF_FILEWRITE writer;
142   writer.version = 1;
143   writer.WriteBlock = FakeBlockWriter;
144 
145   EXPECT_TRUE(FPDF_SaveAsCopy(output_doc, &writer, 0));
146   FPDF_CloseDocument(output_doc);
147 }
148 
TEST_F(FPDFPPOEmbedderTest,BadCircularViewerPref)149 TEST_F(FPDFPPOEmbedderTest, BadCircularViewerPref) {
150   ASSERT_TRUE(OpenDocument("circular_viewer_ref.pdf"));
151 
152   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
153   EXPECT_TRUE(output_doc);
154   EXPECT_TRUE(FPDF_CopyViewerPreferences(output_doc, document()));
155 
156   FPDF_FILEWRITE writer;
157   writer.version = 1;
158   writer.WriteBlock = FakeBlockWriter;
159 
160   EXPECT_TRUE(FPDF_SaveAsCopy(output_doc, &writer, 0));
161   FPDF_CloseDocument(output_doc);
162 }
163 
TEST_F(FPDFPPOEmbedderTest,BadRanges)164 TEST_F(FPDFPPOEmbedderTest, BadRanges) {
165   EXPECT_TRUE(OpenDocument("hello_world.pdf"));
166 
167   FPDF_PAGE page = LoadPage(0);
168   EXPECT_TRUE(page);
169 
170   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
171   EXPECT_TRUE(output_doc);
172   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "clams", 0));
173   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "0", 0));
174   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "42", 0));
175   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "1,2", 0));
176   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "1-2", 0));
177   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), ",1", 0));
178   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "1,", 0));
179   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "1-", 0));
180   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "-1", 0));
181   EXPECT_FALSE(FPDF_ImportPages(output_doc, document(), "-,0,,,1-", 0));
182   FPDF_CloseDocument(output_doc);
183 
184   UnloadPage(page);
185 }
186 
TEST_F(FPDFPPOEmbedderTest,GoodRanges)187 TEST_F(FPDFPPOEmbedderTest, GoodRanges) {
188   EXPECT_TRUE(OpenDocument("viewer_ref.pdf"));
189 
190   FPDF_PAGE page = LoadPage(0);
191   EXPECT_TRUE(page);
192 
193   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
194   EXPECT_TRUE(output_doc);
195   EXPECT_TRUE(FPDF_CopyViewerPreferences(output_doc, document()));
196   EXPECT_TRUE(FPDF_ImportPages(output_doc, document(), "1,1,1,1", 0));
197   EXPECT_EQ(4, FPDF_GetPageCount(output_doc));
198   EXPECT_TRUE(FPDF_ImportPages(output_doc, document(), "1-1", 0));
199   EXPECT_EQ(5, FPDF_GetPageCount(output_doc));
200   EXPECT_TRUE(FPDF_ImportPages(output_doc, document(), "5-5", 0));
201   EXPECT_EQ(6, FPDF_GetPageCount(output_doc));
202   EXPECT_TRUE(FPDF_ImportPages(output_doc, document(), "2-4", 0));
203   EXPECT_EQ(9, FPDF_GetPageCount(output_doc));
204   FPDF_CloseDocument(output_doc);
205 
206   UnloadPage(page);
207 }
208 
TEST_F(FPDFPPOEmbedderTest,BUG_664284)209 TEST_F(FPDFPPOEmbedderTest, BUG_664284) {
210   EXPECT_TRUE(OpenDocument("bug_664284.pdf"));
211 
212   FPDF_PAGE page = LoadPage(0);
213   ASSERT_NE(nullptr, page);
214 
215   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
216   EXPECT_TRUE(output_doc);
217   EXPECT_TRUE(FPDF_ImportPages(output_doc, document(), "1", 0));
218   FPDF_CloseDocument(output_doc);
219 
220   UnloadPage(page);
221 }
222 
223 // TODO(crbug.com/pdfium/11): Fix this test and enable.
224 #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
225 #define MAYBE_BUG_750568 DISABLED_BUG_750568
226 #else
227 #define MAYBE_BUG_750568 BUG_750568
228 #endif
TEST_F(FPDFPPOEmbedderTest,MAYBE_BUG_750568)229 TEST_F(FPDFPPOEmbedderTest, MAYBE_BUG_750568) {
230   const char* const kHashes[] = {
231       "64ad08132a1c5a166768298c8a578f57", "83b83e2f6bc80707d0a917c7634140b9",
232       "913cd3723a451e4e46fbc2c05702d1ee", "81fb7cfd4860f855eb468f73dfeb6d60"};
233 
234   ASSERT_TRUE(OpenDocument("bug_750568.pdf"));
235   ASSERT_EQ(4, FPDF_GetPageCount(document()));
236 
237   for (size_t i = 0; i < 4; ++i) {
238     FPDF_PAGE page = LoadPage(i);
239     ASSERT_TRUE(page);
240 
241     ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
242     ASSERT_EQ(200, FPDFBitmap_GetWidth(bitmap.get()));
243     ASSERT_EQ(200, FPDFBitmap_GetHeight(bitmap.get()));
244     ASSERT_EQ(800, FPDFBitmap_GetStride(bitmap.get()));
245 
246     EXPECT_EQ(kHashes[i], HashBitmap(bitmap.get()));
247     UnloadPage(page);
248   }
249 
250   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
251   ASSERT_TRUE(output_doc);
252   EXPECT_TRUE(FPDF_ImportPages(output_doc, document(), "1,2,3,4", 0));
253   ASSERT_EQ(4, FPDF_GetPageCount(output_doc));
254   for (size_t i = 0; i < 4; ++i) {
255     FPDF_PAGE page = FPDF_LoadPage(output_doc, i);
256     ASSERT_TRUE(page);
257 
258     ScopedFPDFBitmap bitmap = RenderPage(page);
259     ASSERT_EQ(200, FPDFBitmap_GetWidth(bitmap.get()));
260     ASSERT_EQ(200, FPDFBitmap_GetHeight(bitmap.get()));
261     ASSERT_EQ(800, FPDFBitmap_GetStride(bitmap.get()));
262 
263     EXPECT_EQ(kHashes[i], HashBitmap(bitmap.get()));
264     FPDF_ClosePage(page);
265   }
266   FPDF_CloseDocument(output_doc);
267 }
268 
TEST_F(FPDFPPOEmbedderTest,ImportWithZeroLengthStream)269 TEST_F(FPDFPPOEmbedderTest, ImportWithZeroLengthStream) {
270   EXPECT_TRUE(OpenDocument("zero_length_stream.pdf"));
271   FPDF_PAGE page = LoadPage(0);
272   ASSERT_TRUE(page);
273 
274   ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
275   ASSERT_EQ(200, FPDFBitmap_GetWidth(bitmap.get()));
276   ASSERT_EQ(200, FPDFBitmap_GetHeight(bitmap.get()));
277   ASSERT_EQ(800, FPDFBitmap_GetStride(bitmap.get()));
278 
279   std::string digest = HashBitmap(bitmap.get());
280   UnloadPage(page);
281 
282   FPDF_DOCUMENT new_doc = FPDF_CreateNewDocument();
283   EXPECT_TRUE(new_doc);
284   EXPECT_TRUE(FPDF_ImportPages(new_doc, document(), "1", 0));
285 
286   EXPECT_EQ(1, FPDF_GetPageCount(new_doc));
287   FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
288   ASSERT_NE(nullptr, new_page);
289   ScopedFPDFBitmap new_bitmap = RenderPage(new_page);
290   ASSERT_EQ(200, FPDFBitmap_GetWidth(new_bitmap.get()));
291   ASSERT_EQ(200, FPDFBitmap_GetHeight(new_bitmap.get()));
292   ASSERT_EQ(800, FPDFBitmap_GetStride(new_bitmap.get()));
293 
294   EXPECT_EQ(digest, HashBitmap(new_bitmap.get()));
295   FPDF_ClosePage(new_page);
296   FPDF_CloseDocument(new_doc);
297 }
298