• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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 <vector>
6 
7 #include "public/fpdf_thumbnail.h"
8 #include "public/fpdfview.h"
9 #include "testing/embedder_test.h"
10 #include "testing/utils/hash.h"
11 
12 class FPDFThumbnailEmbedderTest : public EmbedderTest {};
13 
TEST_F(FPDFThumbnailEmbedderTest,GetDecodedThumbnailDataFromPageWithFilters)14 TEST_F(FPDFThumbnailEmbedderTest, GetDecodedThumbnailDataFromPageWithFilters) {
15   ASSERT_TRUE(OpenDocument("simple_thumbnail.pdf"));
16 
17   {
18     const char kHashedDecodedData[] = "7902d0be831c9024960f4ebd5d7df1f7";
19     const unsigned long kExpectedSize = 1138u;
20 
21     FPDF_PAGE page = LoadPage(0);
22     ASSERT_TRUE(page);
23 
24     unsigned long length_bytes =
25         FPDFPage_GetDecodedThumbnailData(page, nullptr, 0);
26     ASSERT_EQ(kExpectedSize, length_bytes);
27     std::vector<uint8_t> thumb_buf(length_bytes);
28 
29     EXPECT_EQ(kExpectedSize, FPDFPage_GetDecodedThumbnailData(
30                                  page, thumb_buf.data(), length_bytes));
31     EXPECT_EQ(kHashedDecodedData,
32               GenerateMD5Base16(thumb_buf.data(), kExpectedSize));
33 
34     UnloadPage(page);
35   }
36 
37   {
38     const char kHashedDecodedData[] = "e81123a573378ba1ea80461d25cc41f6";
39     const unsigned long kExpectedSize = 1110u;
40 
41     FPDF_PAGE page = LoadPage(1);
42     ASSERT_TRUE(page);
43 
44     unsigned long length_bytes =
45         FPDFPage_GetDecodedThumbnailData(page, nullptr, 0);
46     ASSERT_EQ(kExpectedSize, length_bytes);
47     std::vector<uint8_t> thumb_buf(length_bytes);
48 
49     EXPECT_EQ(kExpectedSize, FPDFPage_GetDecodedThumbnailData(
50                                  page, thumb_buf.data(), length_bytes));
51     EXPECT_EQ(kHashedDecodedData,
52               GenerateMD5Base16(thumb_buf.data(), kExpectedSize));
53 
54     UnloadPage(page);
55   }
56 }
57 
TEST_F(FPDFThumbnailEmbedderTest,GetDecodedThumbnailDataFromPageWithNoFilters)58 TEST_F(FPDFThumbnailEmbedderTest,
59        GetDecodedThumbnailDataFromPageWithNoFilters) {
60   ASSERT_TRUE(OpenDocument("thumbnail_with_no_filters.pdf"));
61 
62   const char kHashedDecodedData[] = "b5696e586382b3373741f8a1d651cab0";
63   const unsigned long kExpectedSize = 301u;
64 
65   FPDF_PAGE page = LoadPage(0);
66   ASSERT_TRUE(page);
67 
68   unsigned long length_bytes =
69       FPDFPage_GetDecodedThumbnailData(page, nullptr, 0);
70   ASSERT_EQ(kExpectedSize, length_bytes);
71   std::vector<uint8_t> thumb_buf(length_bytes);
72 
73   EXPECT_EQ(kExpectedSize, FPDFPage_GetDecodedThumbnailData(
74                                page, thumb_buf.data(), length_bytes));
75   EXPECT_EQ(kHashedDecodedData,
76             GenerateMD5Base16(thumb_buf.data(), kExpectedSize));
77 
78   UnloadPage(page);
79 }
80 
TEST_F(FPDFThumbnailEmbedderTest,GetDecodedThumbnailDataFromPageWithNoThumbnails)81 TEST_F(FPDFThumbnailEmbedderTest,
82        GetDecodedThumbnailDataFromPageWithNoThumbnails) {
83   ASSERT_TRUE(OpenDocument("hello_world.pdf"));
84 
85   FPDF_PAGE page = LoadPage(0);
86   ASSERT_TRUE(page);
87 
88   EXPECT_EQ(0u, FPDFPage_GetDecodedThumbnailData(page, nullptr, 0));
89 
90   UnloadPage(page);
91 }
92 
TEST_F(FPDFThumbnailEmbedderTest,GetDecodedThumbnailDataFromPageNullPage)93 TEST_F(FPDFThumbnailEmbedderTest, GetDecodedThumbnailDataFromPageNullPage) {
94   EXPECT_EQ(0u, FPDFPage_GetDecodedThumbnailData(nullptr, nullptr, 0));
95 }
96 
TEST_F(FPDFThumbnailEmbedderTest,GetRawThumbnailDataFromPageWithFilters)97 TEST_F(FPDFThumbnailEmbedderTest, GetRawThumbnailDataFromPageWithFilters) {
98   ASSERT_TRUE(OpenDocument("simple_thumbnail.pdf"));
99 
100   {
101     const char kHashedRawData[] = "f6a8e8db01cccd52abb91ea433a17373";
102     const unsigned long kExpectedSize = 1851u;
103 
104     FPDF_PAGE page = LoadPage(0);
105     ASSERT_TRUE(page);
106 
107     unsigned long length_bytes = FPDFPage_GetRawThumbnailData(page, nullptr, 0);
108     ASSERT_EQ(kExpectedSize, length_bytes);
109     std::vector<uint8_t> thumb_buf(length_bytes);
110 
111     EXPECT_EQ(kExpectedSize, FPDFPage_GetRawThumbnailData(
112                                  page, thumb_buf.data(), length_bytes));
113     EXPECT_EQ(kHashedRawData,
114               GenerateMD5Base16(thumb_buf.data(), kExpectedSize));
115 
116     UnloadPage(page);
117   }
118 
119   {
120     const char kHashedRawData[] = "c7558a461d5ecfb1d4757218b473afc0";
121     const unsigned long kExpectedSize = 1792u;
122 
123     FPDF_PAGE page = LoadPage(1);
124     ASSERT_TRUE(page);
125 
126     unsigned long length_bytes = FPDFPage_GetRawThumbnailData(page, nullptr, 0);
127     ASSERT_EQ(kExpectedSize, length_bytes);
128     std::vector<uint8_t> thumb_buf(length_bytes);
129 
130     EXPECT_EQ(kExpectedSize, FPDFPage_GetRawThumbnailData(
131                                  page, thumb_buf.data(), length_bytes));
132     EXPECT_EQ(kHashedRawData,
133               GenerateMD5Base16(thumb_buf.data(), kExpectedSize));
134 
135     UnloadPage(page);
136   }
137 }
138 
TEST_F(FPDFThumbnailEmbedderTest,GetRawThumbnailDataFromPageWithNoFilters)139 TEST_F(FPDFThumbnailEmbedderTest, GetRawThumbnailDataFromPageWithNoFilters) {
140   ASSERT_TRUE(OpenDocument("thumbnail_with_no_filters.pdf"));
141 
142   const char kHashedRawData[] = "b5696e586382b3373741f8a1d651cab0";
143   const unsigned long kExpectedSize = 301u;
144 
145   FPDF_PAGE page = LoadPage(0);
146   ASSERT_TRUE(page);
147 
148   unsigned long length_bytes = FPDFPage_GetRawThumbnailData(page, nullptr, 0);
149   ASSERT_EQ(kExpectedSize, length_bytes);
150   std::vector<uint8_t> thumb_buf(length_bytes);
151 
152   EXPECT_EQ(kExpectedSize,
153             FPDFPage_GetRawThumbnailData(page, thumb_buf.data(), length_bytes));
154   EXPECT_EQ(kHashedRawData, GenerateMD5Base16(thumb_buf.data(), kExpectedSize));
155 
156   UnloadPage(page);
157 }
158 
TEST_F(FPDFThumbnailEmbedderTest,GetRawThumbnailDataFromPageWithNoThumbnails)159 TEST_F(FPDFThumbnailEmbedderTest, GetRawThumbnailDataFromPageWithNoThumbnails) {
160   ASSERT_TRUE(OpenDocument("hello_world.pdf"));
161 
162   FPDF_PAGE page = LoadPage(0);
163   ASSERT_TRUE(page);
164 
165   EXPECT_EQ(0u, FPDFPage_GetRawThumbnailData(page, nullptr, 0));
166 
167   UnloadPage(page);
168 }
169 
TEST_F(FPDFThumbnailEmbedderTest,GetRawThumbnailDataFromPageNullPage)170 TEST_F(FPDFThumbnailEmbedderTest, GetRawThumbnailDataFromPageNullPage) {
171   EXPECT_EQ(0u, FPDFPage_GetRawThumbnailData(nullptr, nullptr, 0));
172 }
173 
TEST_F(FPDFThumbnailEmbedderTest,GetThumbnailAsBitmapFromPage)174 TEST_F(FPDFThumbnailEmbedderTest, GetThumbnailAsBitmapFromPage) {
175   ASSERT_TRUE(OpenDocument("simple_thumbnail.pdf"));
176 
177   {
178     FPDF_PAGE page = LoadPage(0);
179     ASSERT_TRUE(page);
180 
181     ScopedFPDFBitmap thumb_bitmap(FPDFPage_GetThumbnailAsBitmap(page));
182 
183     EXPECT_EQ(50, FPDFBitmap_GetWidth(thumb_bitmap.get()));
184     EXPECT_EQ(50, FPDFBitmap_GetHeight(thumb_bitmap.get()));
185     EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(thumb_bitmap.get()));
186     CompareBitmap(thumb_bitmap.get(), 50, 50,
187                   "52b75451e396f55e95d1cb68e6018226");
188 
189     UnloadPage(page);
190   }
191 
192   {
193     FPDF_PAGE page = LoadPage(1);
194     ASSERT_TRUE(page);
195 
196     ScopedFPDFBitmap thumb_bitmap(FPDFPage_GetThumbnailAsBitmap(page));
197 
198     EXPECT_EQ(50, FPDFBitmap_GetWidth(thumb_bitmap.get()));
199     EXPECT_EQ(50, FPDFBitmap_GetHeight(thumb_bitmap.get()));
200     EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(thumb_bitmap.get()));
201     CompareBitmap(thumb_bitmap.get(), 50, 50,
202                   "1f448be08c6e6043ccd0bad8ecc2a351");
203 
204     UnloadPage(page);
205   }
206 }
207 
TEST_F(FPDFThumbnailEmbedderTest,GetThumbnailAsBitmapFromPageWithoutThumbnail)208 TEST_F(FPDFThumbnailEmbedderTest,
209        GetThumbnailAsBitmapFromPageWithoutThumbnail) {
210   ASSERT_TRUE(OpenDocument("hello_world.pdf"));
211 
212   FPDF_PAGE page = LoadPage(0);
213   ASSERT_TRUE(page);
214 
215   ScopedFPDFBitmap thumb_bitmap(FPDFPage_GetThumbnailAsBitmap(page));
216   ASSERT_EQ(nullptr, thumb_bitmap.get());
217 
218   UnloadPage(page);
219 }
220 
TEST_F(FPDFThumbnailEmbedderTest,GetThumbnailAsBitmapFromThumbnailWithEmptyStream)221 TEST_F(FPDFThumbnailEmbedderTest,
222        GetThumbnailAsBitmapFromThumbnailWithEmptyStream) {
223   ASSERT_TRUE(OpenDocument("thumbnail_with_empty_stream.pdf"));
224 
225   FPDF_PAGE page = LoadPage(0);
226   ASSERT_TRUE(page);
227 
228   ScopedFPDFBitmap thumb_bitmap(FPDFPage_GetThumbnailAsBitmap(page));
229   ASSERT_EQ(nullptr, thumb_bitmap.get());
230 
231   UnloadPage(page);
232 }
233 
TEST_F(FPDFThumbnailEmbedderTest,GetThumbnailAsBitmapFromThumbnailWithNoFilters)234 TEST_F(FPDFThumbnailEmbedderTest,
235        GetThumbnailAsBitmapFromThumbnailWithNoFilters) {
236   ASSERT_TRUE(OpenDocument("thumbnail_with_no_filters.pdf"));
237 
238   FPDF_PAGE page = LoadPage(0);
239   ASSERT_TRUE(page);
240 
241   ScopedFPDFBitmap thumb_bitmap(FPDFPage_GetThumbnailAsBitmap(page));
242 
243   EXPECT_EQ(10, FPDFBitmap_GetWidth(thumb_bitmap.get()));
244   EXPECT_EQ(10, FPDFBitmap_GetHeight(thumb_bitmap.get()));
245   EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(thumb_bitmap.get()));
246   CompareBitmap(thumb_bitmap.get(), 10, 10, "fe02583f9e6d094042a942ff686e9936");
247 
248   UnloadPage(page);
249 }
250 
TEST_F(FPDFThumbnailEmbedderTest,GetThumbnailDoesNotAlterPage)251 TEST_F(FPDFThumbnailEmbedderTest, GetThumbnailDoesNotAlterPage) {
252   ASSERT_TRUE(OpenDocument("simple_thumbnail.pdf"));
253 
254   const char kHashedRawData[] = "f6a8e8db01cccd52abb91ea433a17373";
255   const unsigned long kExpectedRawSize = 1851u;
256 
257   FPDF_PAGE page = LoadPage(0);
258   ASSERT_TRUE(page);
259 
260   // Get the raw data
261   unsigned long raw_size = FPDFPage_GetRawThumbnailData(page, nullptr, 0);
262   ASSERT_EQ(kExpectedRawSize, raw_size);
263   std::vector<uint8_t> raw_thumb_buf(raw_size);
264 
265   EXPECT_EQ(kExpectedRawSize,
266             FPDFPage_GetRawThumbnailData(page, raw_thumb_buf.data(), raw_size));
267   EXPECT_EQ(kHashedRawData,
268             GenerateMD5Base16(raw_thumb_buf.data(), kExpectedRawSize));
269 
270   // Get the thumbnail
271   ScopedFPDFBitmap thumb_bitmap(FPDFPage_GetThumbnailAsBitmap(page));
272 
273   EXPECT_EQ(50, FPDFBitmap_GetWidth(thumb_bitmap.get()));
274   EXPECT_EQ(50, FPDFBitmap_GetHeight(thumb_bitmap.get()));
275   EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(thumb_bitmap.get()));
276   CompareBitmap(thumb_bitmap.get(), 50, 50, "52b75451e396f55e95d1cb68e6018226");
277 
278   // Get the raw data again
279   unsigned long new_raw_size = FPDFPage_GetRawThumbnailData(page, nullptr, 0);
280   ASSERT_EQ(kExpectedRawSize, new_raw_size);
281   std::vector<uint8_t> new_raw_thumb_buf(new_raw_size);
282 
283   EXPECT_EQ(kExpectedRawSize,
284             FPDFPage_GetRawThumbnailData(page, new_raw_thumb_buf.data(),
285                                          new_raw_size));
286   EXPECT_EQ(kHashedRawData,
287             GenerateMD5Base16(new_raw_thumb_buf.data(), kExpectedRawSize));
288 
289   UnloadPage(page);
290 }
291 
TEST_F(FPDFThumbnailEmbedderTest,GetThumbnailAsBitmapFromPageNullPage)292 TEST_F(FPDFThumbnailEmbedderTest, GetThumbnailAsBitmapFromPageNullPage) {
293   EXPECT_EQ(nullptr, FPDFPage_GetThumbnailAsBitmap(nullptr));
294 }
295