blob: 97ba9d7c4ced6385bfc4b585b1436637661d5244 [file] [log] [blame]
Tom Sepez96d13342015-01-16 14:59:26 -08001// 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
Tom Sepezc8f6ab62015-01-22 11:20:06 -08005#include <limits>
6#include <string>
7
Nicolas Pena24b07332017-09-13 18:02:11 -04008#include "core/fxcrt/fx_coordinates.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -04009#include "fpdfsdk/fpdfview_c_api_test.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080010#include "public/fpdfview.h"
Wei Li091f7a02015-11-09 12:09:55 -080011#include "testing/embedder_test.h"
Tom Sepez96d13342015-01-16 14:59:26 -080012#include "testing/gtest/include/gtest/gtest.h"
Dan Sinclaircb377be2017-05-11 16:05:32 -040013#include "testing/utils/path_service.h"
Tom Sepez96d13342015-01-16 14:59:26 -080014
Tom Sepezcf22eb82015-05-12 17:28:08 -070015TEST(fpdf, CApiTest) {
16 EXPECT_TRUE(CheckPDFiumCApi());
17}
18
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019class FPDFViewEmbeddertest : public EmbedderTest {};
Tom Sepez96d13342015-01-16 14:59:26 -080020
21TEST_F(FPDFViewEmbeddertest, Document) {
Wei Li091f7a02015-11-09 12:09:55 -080022 EXPECT_TRUE(OpenDocument("about_blank.pdf"));
Tom Sepez96d13342015-01-16 14:59:26 -080023 EXPECT_EQ(1, GetPageCount());
24 EXPECT_EQ(0, GetFirstPageNum());
25
26 int version;
27 EXPECT_TRUE(FPDF_GetFileVersion(document(), &version));
28 EXPECT_EQ(14, version);
29
30 EXPECT_EQ(0xFFFFFFFF, FPDF_GetDocPermissions(document()));
31 EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document()));
32}
33
Jane Liu8a1081f2017-08-22 16:28:05 -040034TEST_F(FPDFViewEmbeddertest, LoadNonexistentDocument) {
35 FPDF_DOCUMENT doc = FPDF_LoadDocument("nonexistent_document.pdf", "");
36 ASSERT_FALSE(doc);
37 EXPECT_EQ(static_cast<int>(FPDF_GetLastError()), FPDF_ERR_FILE);
38}
39
thestigb8db5112016-04-06 12:12:52 -070040// See bug 465.
41TEST_F(FPDFViewEmbeddertest, EmptyDocument) {
42 EXPECT_TRUE(CreateEmptyDocument());
43
44 {
45 int version = 42;
46 EXPECT_FALSE(FPDF_GetFileVersion(document(), &version));
47 EXPECT_EQ(0, version);
48 }
49
50 {
51#ifndef PDF_ENABLE_XFA
52 const unsigned long kExpected = 0;
53#else
54 const unsigned long kExpected = static_cast<uint32_t>(-1);
55#endif
56 EXPECT_EQ(kExpected, FPDF_GetDocPermissions(document()));
57 }
58
59 EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document()));
60
61 EXPECT_EQ(0, FPDF_GetPageCount(document()));
62
63 EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
64 EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document()));
65 EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
66
thestig04bebfe2016-11-04 16:07:25 -070067 char buf[100];
68 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
69 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
70
weili3cc01f22016-05-16 13:53:42 -070071 EXPECT_EQ(0u, FPDF_CountNamedDests(document()));
thestigb8db5112016-04-06 12:12:52 -070072}
73
Lei Zhangabe82802017-08-09 13:33:31 -070074TEST_F(FPDFViewEmbeddertest, LinearizedDocument) {
75 EXPECT_TRUE(OpenDocument("feature_linearized_loading.pdf", nullptr, true));
76 int version;
77 EXPECT_TRUE(FPDF_GetFileVersion(document(), &version));
78 EXPECT_EQ(16, version);
79}
80
Tom Sepez96d13342015-01-16 14:59:26 -080081TEST_F(FPDFViewEmbeddertest, Page) {
Wei Li091f7a02015-11-09 12:09:55 -080082 EXPECT_TRUE(OpenDocument("about_blank.pdf"));
Tom Sepezda8189e2015-01-30 14:41:50 -080083 FPDF_PAGE page = LoadPage(0);
Tom Sepez96d13342015-01-16 14:59:26 -080084 EXPECT_NE(nullptr, page);
85 EXPECT_EQ(612.0, FPDF_GetPageWidth(page));
86 EXPECT_EQ(792.0, FPDF_GetPageHeight(page));
Tom Sepezda8189e2015-01-30 14:41:50 -080087 UnloadPage(page);
88 EXPECT_EQ(nullptr, LoadPage(1));
Tom Sepez96d13342015-01-16 14:59:26 -080089}
90
thestig04bebfe2016-11-04 16:07:25 -070091TEST_F(FPDFViewEmbeddertest, ViewerRefDummy) {
Wei Li091f7a02015-11-09 12:09:55 -080092 EXPECT_TRUE(OpenDocument("about_blank.pdf"));
Tom Sepez96d13342015-01-16 14:59:26 -080093 EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
94 EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document()));
95 EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
thestig04bebfe2016-11-04 16:07:25 -070096
97 char buf[100];
98 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
99 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
100}
101
102TEST_F(FPDFViewEmbeddertest, ViewerRef) {
103 EXPECT_TRUE(OpenDocument("viewer_ref.pdf"));
104 EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
105 EXPECT_EQ(5, FPDF_VIEWERREF_GetNumCopies(document()));
106 EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
107
108 // Test some corner cases.
109 char buf[100];
110 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "", buf, sizeof(buf)));
111 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
112 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
113
114 // Make sure |buf| does not get written into when it appears to be too small.
Dan Sinclair85c8e7f2016-11-21 13:50:32 -0500115 // NOLINTNEXTLINE(runtime/printf)
thestig04bebfe2016-11-04 16:07:25 -0700116 strcpy(buf, "ABCD");
117 EXPECT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, 1));
118 EXPECT_STREQ("ABCD", buf);
119
120 // Note "Foo" is a different key from "foo".
121 EXPECT_EQ(4U,
122 FPDF_VIEWERREF_GetName(document(), "Foo", nullptr, sizeof(buf)));
123 ASSERT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, sizeof(buf)));
124 EXPECT_STREQ("foo", buf);
125
126 // Try to retrieve a boolean and an integer.
127 EXPECT_EQ(
128 0U, FPDF_VIEWERREF_GetName(document(), "HideToolbar", buf, sizeof(buf)));
129 EXPECT_EQ(0U,
130 FPDF_VIEWERREF_GetName(document(), "NumCopies", buf, sizeof(buf)));
131
132 // Try more valid cases.
133 ASSERT_EQ(4U,
134 FPDF_VIEWERREF_GetName(document(), "Direction", buf, sizeof(buf)));
135 EXPECT_STREQ("R2L", buf);
136 ASSERT_EQ(8U,
137 FPDF_VIEWERREF_GetName(document(), "ViewArea", buf, sizeof(buf)));
138 EXPECT_STREQ("CropBox", buf);
Tom Sepez96d13342015-01-16 14:59:26 -0800139}
140
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800141TEST_F(FPDFViewEmbeddertest, NamedDests) {
Wei Li091f7a02015-11-09 12:09:55 -0800142 EXPECT_TRUE(OpenDocument("named_dests.pdf"));
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800143 long buffer_size;
144 char fixed_buffer[512];
145 FPDF_DEST dest;
146
147 // Query the size of the first item.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148 buffer_size = 2000000; // Absurdly large, check not used for this case.
Tom Sepezcf22eb82015-05-12 17:28:08 -0700149 dest = FPDF_GetNamedDest(document(), 0, nullptr, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800150 EXPECT_NE(nullptr, dest);
weili3cc01f22016-05-16 13:53:42 -0700151 EXPECT_EQ(12, buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800152
153 // Try to retrieve the first item with too small a buffer.
Oliver Chang35e68a52015-12-09 12:44:33 -0800154 buffer_size = 10;
Tom Sepezcf22eb82015-05-12 17:28:08 -0700155 dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800156 EXPECT_NE(nullptr, dest);
157 EXPECT_EQ(-1, buffer_size);
158
159 // Try to retrieve the first item with correctly sized buffer. Item is
160 // taken from Dests NameTree in named_dests.pdf.
161 buffer_size = 12;
Tom Sepezcf22eb82015-05-12 17:28:08 -0700162 dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800163 EXPECT_NE(nullptr, dest);
weili3cc01f22016-05-16 13:53:42 -0700164 EXPECT_EQ(12, buffer_size);
Oliver Chang35e68a52015-12-09 12:44:33 -0800165 EXPECT_EQ(std::string("F\0i\0r\0s\0t\0\0\0", 12),
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800166 std::string(fixed_buffer, buffer_size));
167
168 // Try to retrieve the second item with ample buffer. Item is taken
169 // from Dests NameTree but has a sub-dictionary in named_dests.pdf.
170 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700171 dest = FPDF_GetNamedDest(document(), 1, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800172 EXPECT_NE(nullptr, dest);
weili3cc01f22016-05-16 13:53:42 -0700173 EXPECT_EQ(10, buffer_size);
Oliver Chang35e68a52015-12-09 12:44:33 -0800174 EXPECT_EQ(std::string("N\0e\0x\0t\0\0\0", 10),
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800175 std::string(fixed_buffer, buffer_size));
176
177 // Try to retrieve third item with ample buffer. Item is taken
178 // from Dests NameTree but has a bad sub-dictionary in named_dests.pdf.
179 // in named_dests.pdf).
180 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700181 dest = FPDF_GetNamedDest(document(), 2, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800182 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700183 EXPECT_EQ(sizeof(fixed_buffer),
184 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800185
186 // Try to retrieve the forth item with ample buffer. Item is taken
187 // from Dests NameTree but has a vale of the wrong type in named_dests.pdf.
188 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700189 dest = FPDF_GetNamedDest(document(), 3, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800190 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700191 EXPECT_EQ(sizeof(fixed_buffer),
192 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800193
194 // Try to retrieve fifth item with ample buffer. Item taken from the
195 // old-style Dests dictionary object in named_dests.pdf.
196 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700197 dest = FPDF_GetNamedDest(document(), 4, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800198 EXPECT_NE(nullptr, dest);
weili3cc01f22016-05-16 13:53:42 -0700199 EXPECT_EQ(30, buffer_size);
Oliver Chang35e68a52015-12-09 12:44:33 -0800200 EXPECT_EQ(std::string("F\0i\0r\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 30),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 std::string(fixed_buffer, buffer_size));
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800202
203 // Try to retrieve sixth item with ample buffer. Item istaken from the
204 // old-style Dests dictionary object but has a sub-dictionary in
205 // named_dests.pdf.
206 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700207 dest = FPDF_GetNamedDest(document(), 5, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800208 EXPECT_NE(nullptr, dest);
weili3cc01f22016-05-16 13:53:42 -0700209 EXPECT_EQ(28, buffer_size);
Oliver Chang35e68a52015-12-09 12:44:33 -0800210 EXPECT_EQ(std::string("L\0a\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 28),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 std::string(fixed_buffer, buffer_size));
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800212
213 // Try to retrieve non-existent item with ample buffer.
214 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700215 dest = FPDF_GetNamedDest(document(), 6, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800216 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700217 EXPECT_EQ(sizeof(fixed_buffer),
218 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800219
220 // Try to underflow/overflow the integer index.
221 buffer_size = sizeof(fixed_buffer);
222 dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::max(),
Tom Sepezcf22eb82015-05-12 17:28:08 -0700223 fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800224 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700225 EXPECT_EQ(sizeof(fixed_buffer),
226 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800227
228 buffer_size = sizeof(fixed_buffer);
229 dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::min(),
Tom Sepezcf22eb82015-05-12 17:28:08 -0700230 fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800231 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700232 EXPECT_EQ(sizeof(fixed_buffer),
233 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800234
235 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700236 dest = FPDF_GetNamedDest(document(), -1, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800237 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700238 EXPECT_EQ(sizeof(fixed_buffer),
239 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800240}
241
242TEST_F(FPDFViewEmbeddertest, NamedDestsByName) {
Wei Li091f7a02015-11-09 12:09:55 -0800243 EXPECT_TRUE(OpenDocument("named_dests.pdf"));
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800244
thestig1cd352e2016-06-07 17:53:06 -0700245 // Null pointer returns nullptr.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800246 FPDF_DEST dest = FPDF_GetNamedDestByName(document(), nullptr);
247 EXPECT_EQ(nullptr, dest);
248
thestig1cd352e2016-06-07 17:53:06 -0700249 // Empty string returns nullptr.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800250 dest = FPDF_GetNamedDestByName(document(), "");
251 EXPECT_EQ(nullptr, dest);
252
253 // Item from Dests NameTree.
254 dest = FPDF_GetNamedDestByName(document(), "First");
255 EXPECT_NE(nullptr, dest);
256
257 long ignore_len = 0;
258 FPDF_DEST dest_by_index =
Tom Sepezcf22eb82015-05-12 17:28:08 -0700259 FPDF_GetNamedDest(document(), 0, nullptr, &ignore_len);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800260 EXPECT_EQ(dest_by_index, dest);
261
262 // Item from Dests dictionary.
263 dest = FPDF_GetNamedDestByName(document(), "FirstAlternate");
264 EXPECT_NE(nullptr, dest);
265
266 ignore_len = 0;
Tom Sepezcf22eb82015-05-12 17:28:08 -0700267 dest_by_index = FPDF_GetNamedDest(document(), 4, nullptr, &ignore_len);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800268 EXPECT_EQ(dest_by_index, dest);
269
270 // Bad value type for item from Dests NameTree array.
271 dest = FPDF_GetNamedDestByName(document(), "WrongType");
272 EXPECT_EQ(nullptr, dest);
273
274 // No such destination in either Dest NameTree or dictionary.
275 dest = FPDF_GetNamedDestByName(document(), "Bogus");
276 EXPECT_EQ(nullptr, dest);
277}
Tom Sepeze80685c2015-01-26 16:59:09 -0800278
279// The following tests pass if the document opens without crashing.
Tom Sepez4dcf74d2015-02-03 16:24:43 -0800280TEST_F(FPDFViewEmbeddertest, Crasher_113) {
Wei Li091f7a02015-11-09 12:09:55 -0800281 EXPECT_TRUE(OpenDocument("bug_113.pdf"));
Tom Sepez4dcf74d2015-02-03 16:24:43 -0800282}
283
284TEST_F(FPDFViewEmbeddertest, Crasher_451830) {
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400285 // Document is damaged and can't be opened.
Wei Li091f7a02015-11-09 12:09:55 -0800286 EXPECT_FALSE(OpenDocument("bug_451830.pdf"));
Tom Sepeze80685c2015-01-26 16:59:09 -0800287}
Tom Sepez3795a742015-02-02 11:22:42 -0800288
Tom Sepez4dcf74d2015-02-03 16:24:43 -0800289TEST_F(FPDFViewEmbeddertest, Crasher_452455) {
Wei Li091f7a02015-11-09 12:09:55 -0800290 EXPECT_TRUE(OpenDocument("bug_452455.pdf"));
Tom Sepez3795a742015-02-02 11:22:42 -0800291 FPDF_PAGE page = LoadPage(0);
292 EXPECT_NE(nullptr, page);
Lei Zhangd27acae2015-05-15 15:36:02 -0700293 UnloadPage(page);
Tom Sepez3795a742015-02-02 11:22:42 -0800294}
Tom Sepez93daa3c2015-02-05 10:51:54 -0800295
296TEST_F(FPDFViewEmbeddertest, Crasher_454695) {
Lei Zhangab5537d2016-01-06 14:58:14 -0800297 // Document is damaged and can't be opened.
Wei Li091f7a02015-11-09 12:09:55 -0800298 EXPECT_FALSE(OpenDocument("bug_454695.pdf"));
Tom Sepez93daa3c2015-02-05 10:51:54 -0800299}
Lei Zhang5fae9722015-12-09 23:56:51 -0800300
Lei Zhangab5537d2016-01-06 14:58:14 -0800301TEST_F(FPDFViewEmbeddertest, Crasher_572871) {
302 EXPECT_TRUE(OpenDocument("bug_572871.pdf"));
303}
304
Wei Li07fb4cf2016-01-14 15:15:14 -0800305// It tests that document can still be loaded even the trailer has no 'Size'
306// field if other information is right.
307TEST_F(FPDFViewEmbeddertest, Failed_213) {
308 EXPECT_TRUE(OpenDocument("bug_213.pdf"));
309}
310
Lei Zhang5fae9722015-12-09 23:56:51 -0800311// The following tests pass if the document opens without infinite looping.
312TEST_F(FPDFViewEmbeddertest, Hang_298) {
313 EXPECT_FALSE(OpenDocument("bug_298.pdf"));
314}
Wei Lic009e8e2016-01-05 12:51:32 -0800315
316// Test if the document opens without infinite looping.
317// Previously this test will hang in a loop inside LoadAllCrossRefV4. After
318// the fix, LoadAllCrossRefV4 will return false after detecting a cross
319// reference loop. Cross references will be rebuilt successfully.
320TEST_F(FPDFViewEmbeddertest, CrossRefV4Loop) {
321 EXPECT_TRUE(OpenDocument("bug_xrefv4_loop.pdf"));
Lei Zhang671f0d42017-08-31 11:00:54 -0700322
323 // Make sure calling FPDFAvail_IsDocAvail() on this file does not infinite
324 // loop either. See bug 875.
325 int ret = PDF_DATA_NOTAVAIL;
326 while (ret == PDF_DATA_NOTAVAIL)
327 ret = FPDFAvail_IsDocAvail(avail_, &hints_);
328 EXPECT_EQ(PDF_DATA_AVAIL, ret);
Wei Lic009e8e2016-01-05 12:51:32 -0800329}
Wei Li8e3f8932016-01-08 14:36:59 -0800330
331// The test should pass when circular references to ParseIndirectObject will not
332// cause infinite loop.
333TEST_F(FPDFViewEmbeddertest, Hang_343) {
334 EXPECT_FALSE(OpenDocument("bug_343.pdf"));
Wei Lif2b300a2016-01-11 14:16:10 -0800335}
336
337// The test should pass when the absence of 'Contents' field in a signature
338// dictionary will not cause an infinite loop in CPDF_SyntaxParser::GetObject().
339TEST_F(FPDFViewEmbeddertest, Hang_344) {
340 EXPECT_FALSE(OpenDocument("bug_344.pdf"));
Wei Li215816b2016-01-14 12:29:02 -0800341}
342
Wei Li6bc997a2016-01-19 12:35:03 -0800343// The test should pass when there is no infinite recursion in
344// CPDF_SyntaxParser::GetString().
345TEST_F(FPDFViewEmbeddertest, Hang_355) {
346 EXPECT_FALSE(OpenDocument("bug_355.pdf"));
347}
Wei Li215816b2016-01-14 12:29:02 -0800348// The test should pass even when the file has circular references to pages.
349TEST_F(FPDFViewEmbeddertest, Hang_360) {
350 EXPECT_FALSE(OpenDocument("bug_360.pdf"));
Wei Li07fb4cf2016-01-14 15:15:14 -0800351}
thestiga78ba602016-11-23 15:25:48 -0800352
353TEST_F(FPDFViewEmbeddertest, FPDF_RenderPageBitmapWithMatrix) {
Nicolas Pena24b07332017-09-13 18:02:11 -0400354 const char* const kRotatedMD5[4] = {
355 "0a90de37f52127619c3dfb642b5fa2fe", "d599429574ff0dcad3bc898ea8b874ca",
356 "0113386bb0bd45125bacc6dee78bfe78", "051fcfa4c1f9de28765705633a8ef3a9"};
357 const char kTopLeftQuarterMD5[] = "4982be08db3f6d2e6409186ebbced9eb";
358 const char kHoriStretchedMD5[] = "004bf38f3c5c76a644e6fca204747f21";
359 const char kRotateandStretchMD5[] = "0ea95cacc716d003cf063a2c5ed6c8d7";
thestiga78ba602016-11-23 15:25:48 -0800360
Nicolas Pena60bde102017-07-26 13:50:12 -0400361 EXPECT_TRUE(OpenDocument("rectangles.pdf"));
thestiga78ba602016-11-23 15:25:48 -0800362 FPDF_PAGE page = LoadPage(0);
363 EXPECT_NE(nullptr, page);
Nicolas Pena24b07332017-09-13 18:02:11 -0400364 const int initial_width = static_cast<int>(FPDF_GetPageWidth(page));
365 const int initial_height = static_cast<int>(FPDF_GetPageHeight(page));
366 EXPECT_EQ(200, initial_width);
367 EXPECT_EQ(300, initial_height);
thestiga78ba602016-11-23 15:25:48 -0800368
369 FPDF_BITMAP bitmap = RenderPage(page);
Nicolas Pena24b07332017-09-13 18:02:11 -0400370 CompareBitmap(bitmap, initial_width, initial_height, kRotatedMD5[0]);
thestiga78ba602016-11-23 15:25:48 -0800371 FPDFBitmap_Destroy(bitmap);
372
Nicolas Pena24b07332017-09-13 18:02:11 -0400373 int width;
374 int height;
thestiga78ba602016-11-23 15:25:48 -0800375 FS_RECTF rect;
376 rect.left = 0;
377 rect.top = 0;
Nicolas Pena24b07332017-09-13 18:02:11 -0400378 FS_MATRIX matrix;
thestiga78ba602016-11-23 15:25:48 -0800379
Nicolas Pena24b07332017-09-13 18:02:11 -0400380 // Try the easy rotations: 0, 90, 180, 270 clockwise. The output should be the
381 // same as FPDF_RenderPageBitmap with the appropriate rotation flag. Per PDF
382 // spec section 4.2.2, a t degree rotation is represented by [cos(t) sin(t)
383 // -sin(t) cos(t) 0 0] (matrix goes on the right in the multiplication).
384 rect.right = initial_width;
385 rect.bottom = initial_height;
386 CFX_Matrix rot_matrices[4] = {
387 CFX_Matrix(1, 0, 0, 1, 0, 0), CFX_Matrix(0, -1, 1, 0, 0, 0),
388 CFX_Matrix(-1, 0, 0, -1, 0, 0), CFX_Matrix(0, 1, -1, 0, 0, 0)};
389 for (int rot = 0; rot < 4; ++rot) {
390 matrix.a = rot_matrices[rot].a;
391 matrix.b = rot_matrices[rot].b;
392 matrix.c = rot_matrices[rot].c;
393 matrix.d = rot_matrices[rot].d;
394 matrix.e = rot_matrices[rot].e;
395 matrix.f = rot_matrices[rot].f;
396 if (rot % 2 == 0) {
397 width = initial_width;
398 height = initial_height;
399 } else {
400 width = initial_height;
401 height = initial_width;
402 }
403 rect.right = width;
404 rect.bottom = height;
405
406 bitmap = FPDFBitmap_Create(width, height, 0);
407 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
408 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, rot, 0);
409 CompareBitmap(bitmap, width, height, kRotatedMD5[rot]);
410 FPDFBitmap_Destroy(bitmap);
411
412 bitmap = FPDFBitmap_Create(width, height, 0);
413 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
414 FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0);
415 CompareBitmap(bitmap, width, height, kRotatedMD5[rot]);
416 FPDFBitmap_Destroy(bitmap);
417 }
418 // TODO(npm): what to do with transformations that do not align the page with
419 // the axis, like a 45 degree rotation (currently, part of the page gets cut
420 // out). pdfium:849
thestiga78ba602016-11-23 15:25:48 -0800421
Nicolas Pena60bde102017-07-26 13:50:12 -0400422 // Now render again with the image scaled smaller.
Nicolas Pena24b07332017-09-13 18:02:11 -0400423 width = initial_width / 2;
424 height = initial_height / 2;
thestiga78ba602016-11-23 15:25:48 -0800425 matrix.a = 0.5;
Nicolas Pena24b07332017-09-13 18:02:11 -0400426 matrix.b = 0;
427 matrix.c = 0;
thestiga78ba602016-11-23 15:25:48 -0800428 matrix.d = 0.5;
429
Nicolas Pena24b07332017-09-13 18:02:11 -0400430 rect.right = width;
431 rect.bottom = height;
432
thestiga78ba602016-11-23 15:25:48 -0800433 bitmap = FPDFBitmap_Create(width, height, 0);
434 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
435 FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0);
Nicolas Pena60bde102017-07-26 13:50:12 -0400436 CompareBitmap(bitmap, width, height, kTopLeftQuarterMD5);
437 FPDFBitmap_Destroy(bitmap);
438
Nicolas Pena24b07332017-09-13 18:02:11 -0400439 // Now render again with the image scaled larger horizontally.
440 width = initial_width * 2;
441 height = initial_height;
Nicolas Pena60bde102017-07-26 13:50:12 -0400442 matrix.a = 2;
443 matrix.d = 1;
Nicolas Pena24b07332017-09-13 18:02:11 -0400444 rect.right = width;
445 rect.bottom = height;
Nicolas Pena60bde102017-07-26 13:50:12 -0400446 bitmap = FPDFBitmap_Create(width, height, 0);
447 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
448 FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0);
Nicolas Pena24b07332017-09-13 18:02:11 -0400449 CompareBitmap(bitmap, width, height, kHoriStretchedMD5);
Nicolas Pena60bde102017-07-26 13:50:12 -0400450 FPDFBitmap_Destroy(bitmap);
451
Nicolas Pena24b07332017-09-13 18:02:11 -0400452 // Test a rotation followed by a stretch.
453 width = initial_height * 2;
454 height = initial_width;
Nicolas Pena60bde102017-07-26 13:50:12 -0400455 matrix.a = 0;
Nicolas Pena24b07332017-09-13 18:02:11 -0400456 matrix.b = -1;
457 matrix.c = 2;
Nicolas Pena60bde102017-07-26 13:50:12 -0400458 matrix.d = 0;
Nicolas Pena24b07332017-09-13 18:02:11 -0400459 matrix.e = 0;
460 matrix.f = 0;
461 rect.right = width;
462 rect.bottom = height;
Nicolas Pena60bde102017-07-26 13:50:12 -0400463 bitmap = FPDFBitmap_Create(width, height, 0);
464 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
465 FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0);
Nicolas Pena24b07332017-09-13 18:02:11 -0400466 CompareBitmap(bitmap, width, height, kRotateandStretchMD5);
thestiga78ba602016-11-23 15:25:48 -0800467 FPDFBitmap_Destroy(bitmap);
468
469 UnloadPage(page);
470}
Dan Sinclaircb377be2017-05-11 16:05:32 -0400471
472class UnSupRecordDelegate : public EmbedderTest::Delegate {
473 public:
474 UnSupRecordDelegate() : type_(-1) {}
475 ~UnSupRecordDelegate() override {}
476
477 void UnsupportedHandler(int type) override { type_ = type; }
478
479 int type_;
480};
481
482TEST_F(FPDFViewEmbeddertest, UnSupportedOperations_NotFound) {
483 UnSupRecordDelegate delegate;
484 SetDelegate(&delegate);
485 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
486 EXPECT_EQ(delegate.type_, -1);
487 SetDelegate(nullptr);
488}
489
490TEST_F(FPDFViewEmbeddertest, UnSupportedOperations_LoadCustomDocument) {
491 UnSupRecordDelegate delegate;
492 SetDelegate(&delegate);
493 ASSERT_TRUE(OpenDocument("unsupported_feature.pdf"));
494 EXPECT_EQ(FPDF_UNSP_DOC_PORTABLECOLLECTION, delegate.type_);
495 SetDelegate(nullptr);
496}
497
498TEST_F(FPDFViewEmbeddertest, UnSupportedOperations_LoadDocument) {
499 std::string file_path;
500 ASSERT_TRUE(
501 PathService::GetTestFilePath("unsupported_feature.pdf", &file_path));
502
503 UnSupRecordDelegate delegate;
504 SetDelegate(&delegate);
505 FPDF_DOCUMENT doc = FPDF_LoadDocument(file_path.c_str(), "");
506 EXPECT_TRUE(doc != nullptr);
507 EXPECT_EQ(FPDF_UNSP_DOC_PORTABLECOLLECTION, delegate.type_);
508 FPDF_CloseDocument(doc);
509 SetDelegate(nullptr);
510}