blob: 8576104c2e8d073b2cbe23ea8e895e91793883d5 [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
Dan Sinclairf766ad22016-03-14 13:51:24 -04008#include "fpdfsdk/fpdfview_c_api_test.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -08009#include "public/fpdfview.h"
Wei Li091f7a02015-11-09 12:09:55 -080010#include "testing/embedder_test.h"
Tom Sepez96d13342015-01-16 14:59:26 -080011#include "testing/gtest/include/gtest/gtest.h"
Dan Sinclaircb377be2017-05-11 16:05:32 -040012#include "testing/utils/path_service.h"
Tom Sepez96d13342015-01-16 14:59:26 -080013
Tom Sepezcf22eb82015-05-12 17:28:08 -070014TEST(fpdf, CApiTest) {
15 EXPECT_TRUE(CheckPDFiumCApi());
16}
17
Nico Weber9d8ec5a2015-08-04 13:00:21 -070018class FPDFViewEmbeddertest : public EmbedderTest {};
Tom Sepez96d13342015-01-16 14:59:26 -080019
20TEST_F(FPDFViewEmbeddertest, Document) {
Wei Li091f7a02015-11-09 12:09:55 -080021 EXPECT_TRUE(OpenDocument("about_blank.pdf"));
Tom Sepez96d13342015-01-16 14:59:26 -080022 EXPECT_EQ(1, GetPageCount());
23 EXPECT_EQ(0, GetFirstPageNum());
24
25 int version;
26 EXPECT_TRUE(FPDF_GetFileVersion(document(), &version));
27 EXPECT_EQ(14, version);
28
29 EXPECT_EQ(0xFFFFFFFF, FPDF_GetDocPermissions(document()));
30 EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document()));
31}
32
Jane Liu8a1081f2017-08-22 16:28:05 -040033TEST_F(FPDFViewEmbeddertest, LoadNonexistentDocument) {
34 FPDF_DOCUMENT doc = FPDF_LoadDocument("nonexistent_document.pdf", "");
35 ASSERT_FALSE(doc);
36 EXPECT_EQ(static_cast<int>(FPDF_GetLastError()), FPDF_ERR_FILE);
37}
38
thestigb8db5112016-04-06 12:12:52 -070039// See bug 465.
40TEST_F(FPDFViewEmbeddertest, EmptyDocument) {
41 EXPECT_TRUE(CreateEmptyDocument());
42
43 {
44 int version = 42;
45 EXPECT_FALSE(FPDF_GetFileVersion(document(), &version));
46 EXPECT_EQ(0, version);
47 }
48
49 {
50#ifndef PDF_ENABLE_XFA
51 const unsigned long kExpected = 0;
52#else
53 const unsigned long kExpected = static_cast<uint32_t>(-1);
54#endif
55 EXPECT_EQ(kExpected, FPDF_GetDocPermissions(document()));
56 }
57
58 EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document()));
59
60 EXPECT_EQ(0, FPDF_GetPageCount(document()));
61
62 EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
63 EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document()));
64 EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
65
thestig04bebfe2016-11-04 16:07:25 -070066 char buf[100];
67 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
68 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
69
weili3cc01f22016-05-16 13:53:42 -070070 EXPECT_EQ(0u, FPDF_CountNamedDests(document()));
thestigb8db5112016-04-06 12:12:52 -070071}
72
Lei Zhangabe82802017-08-09 13:33:31 -070073TEST_F(FPDFViewEmbeddertest, LinearizedDocument) {
74 EXPECT_TRUE(OpenDocument("feature_linearized_loading.pdf", nullptr, true));
75 int version;
76 EXPECT_TRUE(FPDF_GetFileVersion(document(), &version));
77 EXPECT_EQ(16, version);
78}
79
Tom Sepez96d13342015-01-16 14:59:26 -080080TEST_F(FPDFViewEmbeddertest, Page) {
Wei Li091f7a02015-11-09 12:09:55 -080081 EXPECT_TRUE(OpenDocument("about_blank.pdf"));
Tom Sepezda8189e2015-01-30 14:41:50 -080082 FPDF_PAGE page = LoadPage(0);
Tom Sepez96d13342015-01-16 14:59:26 -080083 EXPECT_NE(nullptr, page);
84 EXPECT_EQ(612.0, FPDF_GetPageWidth(page));
85 EXPECT_EQ(792.0, FPDF_GetPageHeight(page));
Tom Sepezda8189e2015-01-30 14:41:50 -080086 UnloadPage(page);
87 EXPECT_EQ(nullptr, LoadPage(1));
Tom Sepez96d13342015-01-16 14:59:26 -080088}
89
thestig04bebfe2016-11-04 16:07:25 -070090TEST_F(FPDFViewEmbeddertest, ViewerRefDummy) {
Wei Li091f7a02015-11-09 12:09:55 -080091 EXPECT_TRUE(OpenDocument("about_blank.pdf"));
Tom Sepez96d13342015-01-16 14:59:26 -080092 EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
93 EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document()));
94 EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
thestig04bebfe2016-11-04 16:07:25 -070095
96 char buf[100];
97 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
98 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
99}
100
101TEST_F(FPDFViewEmbeddertest, ViewerRef) {
102 EXPECT_TRUE(OpenDocument("viewer_ref.pdf"));
103 EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
104 EXPECT_EQ(5, FPDF_VIEWERREF_GetNumCopies(document()));
105 EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
106
107 // Test some corner cases.
108 char buf[100];
109 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "", buf, sizeof(buf)));
110 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
111 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
112
113 // Make sure |buf| does not get written into when it appears to be too small.
Dan Sinclair85c8e7f2016-11-21 13:50:32 -0500114 // NOLINTNEXTLINE(runtime/printf)
thestig04bebfe2016-11-04 16:07:25 -0700115 strcpy(buf, "ABCD");
116 EXPECT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, 1));
117 EXPECT_STREQ("ABCD", buf);
118
119 // Note "Foo" is a different key from "foo".
120 EXPECT_EQ(4U,
121 FPDF_VIEWERREF_GetName(document(), "Foo", nullptr, sizeof(buf)));
122 ASSERT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, sizeof(buf)));
123 EXPECT_STREQ("foo", buf);
124
125 // Try to retrieve a boolean and an integer.
126 EXPECT_EQ(
127 0U, FPDF_VIEWERREF_GetName(document(), "HideToolbar", buf, sizeof(buf)));
128 EXPECT_EQ(0U,
129 FPDF_VIEWERREF_GetName(document(), "NumCopies", buf, sizeof(buf)));
130
131 // Try more valid cases.
132 ASSERT_EQ(4U,
133 FPDF_VIEWERREF_GetName(document(), "Direction", buf, sizeof(buf)));
134 EXPECT_STREQ("R2L", buf);
135 ASSERT_EQ(8U,
136 FPDF_VIEWERREF_GetName(document(), "ViewArea", buf, sizeof(buf)));
137 EXPECT_STREQ("CropBox", buf);
Tom Sepez96d13342015-01-16 14:59:26 -0800138}
139
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800140TEST_F(FPDFViewEmbeddertest, NamedDests) {
Wei Li091f7a02015-11-09 12:09:55 -0800141 EXPECT_TRUE(OpenDocument("named_dests.pdf"));
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800142 long buffer_size;
143 char fixed_buffer[512];
144 FPDF_DEST dest;
145
146 // Query the size of the first item.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 buffer_size = 2000000; // Absurdly large, check not used for this case.
Tom Sepezcf22eb82015-05-12 17:28:08 -0700148 dest = FPDF_GetNamedDest(document(), 0, nullptr, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800149 EXPECT_NE(nullptr, dest);
weili3cc01f22016-05-16 13:53:42 -0700150 EXPECT_EQ(12, buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800151
152 // Try to retrieve the first item with too small a buffer.
Oliver Chang35e68a52015-12-09 12:44:33 -0800153 buffer_size = 10;
Tom Sepezcf22eb82015-05-12 17:28:08 -0700154 dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800155 EXPECT_NE(nullptr, dest);
156 EXPECT_EQ(-1, buffer_size);
157
158 // Try to retrieve the first item with correctly sized buffer. Item is
159 // taken from Dests NameTree in named_dests.pdf.
160 buffer_size = 12;
Tom Sepezcf22eb82015-05-12 17:28:08 -0700161 dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800162 EXPECT_NE(nullptr, dest);
weili3cc01f22016-05-16 13:53:42 -0700163 EXPECT_EQ(12, buffer_size);
Oliver Chang35e68a52015-12-09 12:44:33 -0800164 EXPECT_EQ(std::string("F\0i\0r\0s\0t\0\0\0", 12),
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800165 std::string(fixed_buffer, buffer_size));
166
167 // Try to retrieve the second item with ample buffer. Item is taken
168 // from Dests NameTree but has a sub-dictionary in named_dests.pdf.
169 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700170 dest = FPDF_GetNamedDest(document(), 1, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800171 EXPECT_NE(nullptr, dest);
weili3cc01f22016-05-16 13:53:42 -0700172 EXPECT_EQ(10, buffer_size);
Oliver Chang35e68a52015-12-09 12:44:33 -0800173 EXPECT_EQ(std::string("N\0e\0x\0t\0\0\0", 10),
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800174 std::string(fixed_buffer, buffer_size));
175
176 // Try to retrieve third item with ample buffer. Item is taken
177 // from Dests NameTree but has a bad sub-dictionary in named_dests.pdf.
178 // in named_dests.pdf).
179 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700180 dest = FPDF_GetNamedDest(document(), 2, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800181 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700182 EXPECT_EQ(sizeof(fixed_buffer),
183 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800184
185 // Try to retrieve the forth item with ample buffer. Item is taken
186 // from Dests NameTree but has a vale of the wrong type in named_dests.pdf.
187 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700188 dest = FPDF_GetNamedDest(document(), 3, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800189 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700190 EXPECT_EQ(sizeof(fixed_buffer),
191 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800192
193 // Try to retrieve fifth item with ample buffer. Item taken from the
194 // old-style Dests dictionary object in named_dests.pdf.
195 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700196 dest = FPDF_GetNamedDest(document(), 4, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800197 EXPECT_NE(nullptr, dest);
weili3cc01f22016-05-16 13:53:42 -0700198 EXPECT_EQ(30, buffer_size);
Oliver Chang35e68a52015-12-09 12:44:33 -0800199 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 -0700200 std::string(fixed_buffer, buffer_size));
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800201
202 // Try to retrieve sixth item with ample buffer. Item istaken from the
203 // old-style Dests dictionary object but has a sub-dictionary in
204 // named_dests.pdf.
205 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700206 dest = FPDF_GetNamedDest(document(), 5, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800207 EXPECT_NE(nullptr, dest);
weili3cc01f22016-05-16 13:53:42 -0700208 EXPECT_EQ(28, buffer_size);
Oliver Chang35e68a52015-12-09 12:44:33 -0800209 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 -0700210 std::string(fixed_buffer, buffer_size));
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800211
212 // Try to retrieve non-existent item with ample buffer.
213 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700214 dest = FPDF_GetNamedDest(document(), 6, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800215 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700216 EXPECT_EQ(sizeof(fixed_buffer),
217 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800218
219 // Try to underflow/overflow the integer index.
220 buffer_size = sizeof(fixed_buffer);
221 dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::max(),
Tom Sepezcf22eb82015-05-12 17:28:08 -0700222 fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800223 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700224 EXPECT_EQ(sizeof(fixed_buffer),
225 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800226
227 buffer_size = sizeof(fixed_buffer);
228 dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::min(),
Tom Sepezcf22eb82015-05-12 17:28:08 -0700229 fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800230 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700231 EXPECT_EQ(sizeof(fixed_buffer),
232 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800233
234 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700235 dest = FPDF_GetNamedDest(document(), -1, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800236 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700237 EXPECT_EQ(sizeof(fixed_buffer),
238 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800239}
240
241TEST_F(FPDFViewEmbeddertest, NamedDestsByName) {
Wei Li091f7a02015-11-09 12:09:55 -0800242 EXPECT_TRUE(OpenDocument("named_dests.pdf"));
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800243
thestig1cd352e2016-06-07 17:53:06 -0700244 // Null pointer returns nullptr.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800245 FPDF_DEST dest = FPDF_GetNamedDestByName(document(), nullptr);
246 EXPECT_EQ(nullptr, dest);
247
thestig1cd352e2016-06-07 17:53:06 -0700248 // Empty string returns nullptr.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800249 dest = FPDF_GetNamedDestByName(document(), "");
250 EXPECT_EQ(nullptr, dest);
251
252 // Item from Dests NameTree.
253 dest = FPDF_GetNamedDestByName(document(), "First");
254 EXPECT_NE(nullptr, dest);
255
256 long ignore_len = 0;
257 FPDF_DEST dest_by_index =
Tom Sepezcf22eb82015-05-12 17:28:08 -0700258 FPDF_GetNamedDest(document(), 0, nullptr, &ignore_len);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800259 EXPECT_EQ(dest_by_index, dest);
260
261 // Item from Dests dictionary.
262 dest = FPDF_GetNamedDestByName(document(), "FirstAlternate");
263 EXPECT_NE(nullptr, dest);
264
265 ignore_len = 0;
Tom Sepezcf22eb82015-05-12 17:28:08 -0700266 dest_by_index = FPDF_GetNamedDest(document(), 4, nullptr, &ignore_len);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800267 EXPECT_EQ(dest_by_index, dest);
268
269 // Bad value type for item from Dests NameTree array.
270 dest = FPDF_GetNamedDestByName(document(), "WrongType");
271 EXPECT_EQ(nullptr, dest);
272
273 // No such destination in either Dest NameTree or dictionary.
274 dest = FPDF_GetNamedDestByName(document(), "Bogus");
275 EXPECT_EQ(nullptr, dest);
276}
Tom Sepeze80685c2015-01-26 16:59:09 -0800277
278// The following tests pass if the document opens without crashing.
Tom Sepez4dcf74d2015-02-03 16:24:43 -0800279TEST_F(FPDFViewEmbeddertest, Crasher_113) {
Wei Li091f7a02015-11-09 12:09:55 -0800280 EXPECT_TRUE(OpenDocument("bug_113.pdf"));
Tom Sepez4dcf74d2015-02-03 16:24:43 -0800281}
282
283TEST_F(FPDFViewEmbeddertest, Crasher_451830) {
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400284 // Document is damaged and can't be opened.
Wei Li091f7a02015-11-09 12:09:55 -0800285 EXPECT_FALSE(OpenDocument("bug_451830.pdf"));
Tom Sepeze80685c2015-01-26 16:59:09 -0800286}
Tom Sepez3795a742015-02-02 11:22:42 -0800287
Tom Sepez4dcf74d2015-02-03 16:24:43 -0800288TEST_F(FPDFViewEmbeddertest, Crasher_452455) {
Wei Li091f7a02015-11-09 12:09:55 -0800289 EXPECT_TRUE(OpenDocument("bug_452455.pdf"));
Tom Sepez3795a742015-02-02 11:22:42 -0800290 FPDF_PAGE page = LoadPage(0);
291 EXPECT_NE(nullptr, page);
Lei Zhangd27acae2015-05-15 15:36:02 -0700292 UnloadPage(page);
Tom Sepez3795a742015-02-02 11:22:42 -0800293}
Tom Sepez93daa3c2015-02-05 10:51:54 -0800294
295TEST_F(FPDFViewEmbeddertest, Crasher_454695) {
Lei Zhangab5537d2016-01-06 14:58:14 -0800296 // Document is damaged and can't be opened.
Wei Li091f7a02015-11-09 12:09:55 -0800297 EXPECT_FALSE(OpenDocument("bug_454695.pdf"));
Tom Sepez93daa3c2015-02-05 10:51:54 -0800298}
Lei Zhang5fae9722015-12-09 23:56:51 -0800299
Lei Zhangab5537d2016-01-06 14:58:14 -0800300TEST_F(FPDFViewEmbeddertest, Crasher_572871) {
301 EXPECT_TRUE(OpenDocument("bug_572871.pdf"));
302}
303
Wei Li07fb4cf2016-01-14 15:15:14 -0800304// It tests that document can still be loaded even the trailer has no 'Size'
305// field if other information is right.
306TEST_F(FPDFViewEmbeddertest, Failed_213) {
307 EXPECT_TRUE(OpenDocument("bug_213.pdf"));
308}
309
Lei Zhang5fae9722015-12-09 23:56:51 -0800310// The following tests pass if the document opens without infinite looping.
311TEST_F(FPDFViewEmbeddertest, Hang_298) {
312 EXPECT_FALSE(OpenDocument("bug_298.pdf"));
313}
Wei Lic009e8e2016-01-05 12:51:32 -0800314
315// Test if the document opens without infinite looping.
316// Previously this test will hang in a loop inside LoadAllCrossRefV4. After
317// the fix, LoadAllCrossRefV4 will return false after detecting a cross
318// reference loop. Cross references will be rebuilt successfully.
319TEST_F(FPDFViewEmbeddertest, CrossRefV4Loop) {
320 EXPECT_TRUE(OpenDocument("bug_xrefv4_loop.pdf"));
Lei Zhang671f0d42017-08-31 11:00:54 -0700321
322 // Make sure calling FPDFAvail_IsDocAvail() on this file does not infinite
323 // loop either. See bug 875.
324 int ret = PDF_DATA_NOTAVAIL;
325 while (ret == PDF_DATA_NOTAVAIL)
326 ret = FPDFAvail_IsDocAvail(avail_, &hints_);
327 EXPECT_EQ(PDF_DATA_AVAIL, ret);
Wei Lic009e8e2016-01-05 12:51:32 -0800328}
Wei Li8e3f8932016-01-08 14:36:59 -0800329
330// The test should pass when circular references to ParseIndirectObject will not
331// cause infinite loop.
332TEST_F(FPDFViewEmbeddertest, Hang_343) {
333 EXPECT_FALSE(OpenDocument("bug_343.pdf"));
Wei Lif2b300a2016-01-11 14:16:10 -0800334}
335
336// The test should pass when the absence of 'Contents' field in a signature
337// dictionary will not cause an infinite loop in CPDF_SyntaxParser::GetObject().
338TEST_F(FPDFViewEmbeddertest, Hang_344) {
339 EXPECT_FALSE(OpenDocument("bug_344.pdf"));
Wei Li215816b2016-01-14 12:29:02 -0800340}
341
Wei Li6bc997a2016-01-19 12:35:03 -0800342// The test should pass when there is no infinite recursion in
343// CPDF_SyntaxParser::GetString().
344TEST_F(FPDFViewEmbeddertest, Hang_355) {
345 EXPECT_FALSE(OpenDocument("bug_355.pdf"));
346}
Wei Li215816b2016-01-14 12:29:02 -0800347// The test should pass even when the file has circular references to pages.
348TEST_F(FPDFViewEmbeddertest, Hang_360) {
349 EXPECT_FALSE(OpenDocument("bug_360.pdf"));
Wei Li07fb4cf2016-01-14 15:15:14 -0800350}
thestiga78ba602016-11-23 15:25:48 -0800351
352TEST_F(FPDFViewEmbeddertest, FPDF_RenderPageBitmapWithMatrix) {
Nicolas Pena60bde102017-07-26 13:50:12 -0400353 const char kOriginalMD5[] = "210157942bcce97b057a1107a1fd62f8";
354 const char kTopLeftQuarterMD5[] = "c54d58dda13e3cd04eb63e1d0db0feda";
355 const char kTrimmedMD5[] = "88225d7951a21d0eef191cfed06c36ce";
356 const char kRotatedMD5[] = "7d38bc58aa50ad271bc432e77256d3de";
thestiga78ba602016-11-23 15:25:48 -0800357
Nicolas Pena60bde102017-07-26 13:50:12 -0400358 EXPECT_TRUE(OpenDocument("rectangles.pdf"));
thestiga78ba602016-11-23 15:25:48 -0800359 FPDF_PAGE page = LoadPage(0);
360 EXPECT_NE(nullptr, page);
361 const int width = static_cast<int>(FPDF_GetPageWidth(page));
362 const int height = static_cast<int>(FPDF_GetPageHeight(page));
Nicolas Pena60bde102017-07-26 13:50:12 -0400363 EXPECT_EQ(200, width);
364 EXPECT_EQ(200, height);
thestiga78ba602016-11-23 15:25:48 -0800365
366 FPDF_BITMAP bitmap = RenderPage(page);
Nicolas Pena60bde102017-07-26 13:50:12 -0400367 CompareBitmap(bitmap, width, height, kOriginalMD5);
thestiga78ba602016-11-23 15:25:48 -0800368 FPDFBitmap_Destroy(bitmap);
369
370 // Try rendering with an identity matrix. The output should be the same as
371 // the RenderPage() output.
372 FS_MATRIX matrix;
373 matrix.a = 1;
374 matrix.b = 0;
375 matrix.c = 0;
376 matrix.d = 1;
377 matrix.e = 0;
378 matrix.f = 0;
379
380 FS_RECTF rect;
381 rect.left = 0;
382 rect.top = 0;
383 rect.right = width;
384 rect.bottom = height;
385
386 bitmap = FPDFBitmap_Create(width, height, 0);
387 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
388 FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0);
Nicolas Pena60bde102017-07-26 13:50:12 -0400389 CompareBitmap(bitmap, width, height, kOriginalMD5);
thestiga78ba602016-11-23 15:25:48 -0800390 FPDFBitmap_Destroy(bitmap);
391
Nicolas Pena60bde102017-07-26 13:50:12 -0400392 // Now render again with the image scaled smaller.
thestiga78ba602016-11-23 15:25:48 -0800393 matrix.a = 0.5;
394 matrix.d = 0.5;
395
396 bitmap = FPDFBitmap_Create(width, height, 0);
397 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
398 FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0);
Nicolas Pena60bde102017-07-26 13:50:12 -0400399 CompareBitmap(bitmap, width, height, kTopLeftQuarterMD5);
400 FPDFBitmap_Destroy(bitmap);
401
402 // Now render again with the image scaled larger horizontally (will be
403 // trimmed).
404 matrix.a = 2;
405 matrix.d = 1;
406 bitmap = FPDFBitmap_Create(width, height, 0);
407 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
408 FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0);
409 CompareBitmap(bitmap, width, height, kTrimmedMD5);
410 FPDFBitmap_Destroy(bitmap);
411
412 // Now try a 90 degree rotation
413 matrix.a = 0;
414 matrix.b = 1;
415 matrix.c = -1;
416 matrix.d = 0;
417 matrix.e = width;
418 bitmap = FPDFBitmap_Create(width, height, 0);
419 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
420 FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0);
421 CompareBitmap(bitmap, width, height, kRotatedMD5);
thestiga78ba602016-11-23 15:25:48 -0800422 FPDFBitmap_Destroy(bitmap);
423
424 UnloadPage(page);
425}
Dan Sinclaircb377be2017-05-11 16:05:32 -0400426
427class UnSupRecordDelegate : public EmbedderTest::Delegate {
428 public:
429 UnSupRecordDelegate() : type_(-1) {}
430 ~UnSupRecordDelegate() override {}
431
432 void UnsupportedHandler(int type) override { type_ = type; }
433
434 int type_;
435};
436
437TEST_F(FPDFViewEmbeddertest, UnSupportedOperations_NotFound) {
438 UnSupRecordDelegate delegate;
439 SetDelegate(&delegate);
440 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
441 EXPECT_EQ(delegate.type_, -1);
442 SetDelegate(nullptr);
443}
444
445TEST_F(FPDFViewEmbeddertest, UnSupportedOperations_LoadCustomDocument) {
446 UnSupRecordDelegate delegate;
447 SetDelegate(&delegate);
448 ASSERT_TRUE(OpenDocument("unsupported_feature.pdf"));
449 EXPECT_EQ(FPDF_UNSP_DOC_PORTABLECOLLECTION, delegate.type_);
450 SetDelegate(nullptr);
451}
452
453TEST_F(FPDFViewEmbeddertest, UnSupportedOperations_LoadDocument) {
454 std::string file_path;
455 ASSERT_TRUE(
456 PathService::GetTestFilePath("unsupported_feature.pdf", &file_path));
457
458 UnSupRecordDelegate delegate;
459 SetDelegate(&delegate);
460 FPDF_DOCUMENT doc = FPDF_LoadDocument(file_path.c_str(), "");
461 EXPECT_TRUE(doc != nullptr);
462 EXPECT_EQ(FPDF_UNSP_DOC_PORTABLECOLLECTION, delegate.type_);
463 FPDF_CloseDocument(doc);
464 SetDelegate(nullptr);
465}