blob: 699fc8a71b699c7479990058d7d3f351584e55bd [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
Artem Strygin0e60b9e2017-09-28 18:46:03 +030015namespace {
16
17class MockDownloadHints : public FX_DOWNLOADHINTS {
18 public:
19 static void SAddSegment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
20 }
21
22 MockDownloadHints() {
23 FX_DOWNLOADHINTS::version = 1;
24 FX_DOWNLOADHINTS::AddSegment = SAddSegment;
25 }
26
27 ~MockDownloadHints() {}
28};
29
30} // namespace
31
Tom Sepezcf22eb82015-05-12 17:28:08 -070032TEST(fpdf, CApiTest) {
33 EXPECT_TRUE(CheckPDFiumCApi());
34}
35
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036class FPDFViewEmbeddertest : public EmbedderTest {};
Tom Sepez96d13342015-01-16 14:59:26 -080037
38TEST_F(FPDFViewEmbeddertest, Document) {
Wei Li091f7a02015-11-09 12:09:55 -080039 EXPECT_TRUE(OpenDocument("about_blank.pdf"));
Tom Sepez96d13342015-01-16 14:59:26 -080040 EXPECT_EQ(1, GetPageCount());
41 EXPECT_EQ(0, GetFirstPageNum());
42
43 int version;
44 EXPECT_TRUE(FPDF_GetFileVersion(document(), &version));
45 EXPECT_EQ(14, version);
46
47 EXPECT_EQ(0xFFFFFFFF, FPDF_GetDocPermissions(document()));
48 EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document()));
49}
50
Jane Liu8a1081f2017-08-22 16:28:05 -040051TEST_F(FPDFViewEmbeddertest, LoadNonexistentDocument) {
52 FPDF_DOCUMENT doc = FPDF_LoadDocument("nonexistent_document.pdf", "");
53 ASSERT_FALSE(doc);
54 EXPECT_EQ(static_cast<int>(FPDF_GetLastError()), FPDF_ERR_FILE);
55}
56
thestigb8db5112016-04-06 12:12:52 -070057// See bug 465.
58TEST_F(FPDFViewEmbeddertest, EmptyDocument) {
59 EXPECT_TRUE(CreateEmptyDocument());
60
61 {
62 int version = 42;
63 EXPECT_FALSE(FPDF_GetFileVersion(document(), &version));
64 EXPECT_EQ(0, version);
65 }
66
67 {
68#ifndef PDF_ENABLE_XFA
69 const unsigned long kExpected = 0;
70#else
71 const unsigned long kExpected = static_cast<uint32_t>(-1);
72#endif
73 EXPECT_EQ(kExpected, FPDF_GetDocPermissions(document()));
74 }
75
76 EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document()));
77
78 EXPECT_EQ(0, FPDF_GetPageCount(document()));
79
80 EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
81 EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document()));
82 EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
83
thestig04bebfe2016-11-04 16:07:25 -070084 char buf[100];
85 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
86 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
87
weili3cc01f22016-05-16 13:53:42 -070088 EXPECT_EQ(0u, FPDF_CountNamedDests(document()));
thestigb8db5112016-04-06 12:12:52 -070089}
90
Lei Zhangabe82802017-08-09 13:33:31 -070091TEST_F(FPDFViewEmbeddertest, LinearizedDocument) {
92 EXPECT_TRUE(OpenDocument("feature_linearized_loading.pdf", nullptr, true));
93 int version;
94 EXPECT_TRUE(FPDF_GetFileVersion(document(), &version));
95 EXPECT_EQ(16, version);
96}
97
Tom Sepez96d13342015-01-16 14:59:26 -080098TEST_F(FPDFViewEmbeddertest, Page) {
Wei Li091f7a02015-11-09 12:09:55 -080099 EXPECT_TRUE(OpenDocument("about_blank.pdf"));
Tom Sepezda8189e2015-01-30 14:41:50 -0800100 FPDF_PAGE page = LoadPage(0);
Tom Sepez96d13342015-01-16 14:59:26 -0800101 EXPECT_NE(nullptr, page);
102 EXPECT_EQ(612.0, FPDF_GetPageWidth(page));
103 EXPECT_EQ(792.0, FPDF_GetPageHeight(page));
Tom Sepezda8189e2015-01-30 14:41:50 -0800104 UnloadPage(page);
105 EXPECT_EQ(nullptr, LoadPage(1));
Tom Sepez96d13342015-01-16 14:59:26 -0800106}
107
thestig04bebfe2016-11-04 16:07:25 -0700108TEST_F(FPDFViewEmbeddertest, ViewerRefDummy) {
Wei Li091f7a02015-11-09 12:09:55 -0800109 EXPECT_TRUE(OpenDocument("about_blank.pdf"));
Tom Sepez96d13342015-01-16 14:59:26 -0800110 EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
111 EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document()));
112 EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
thestig04bebfe2016-11-04 16:07:25 -0700113
114 char buf[100];
115 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
116 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
117}
118
119TEST_F(FPDFViewEmbeddertest, ViewerRef) {
120 EXPECT_TRUE(OpenDocument("viewer_ref.pdf"));
121 EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
122 EXPECT_EQ(5, FPDF_VIEWERREF_GetNumCopies(document()));
123 EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
124
125 // Test some corner cases.
126 char buf[100];
127 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "", buf, sizeof(buf)));
128 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
129 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
130
131 // Make sure |buf| does not get written into when it appears to be too small.
Dan Sinclair85c8e7f2016-11-21 13:50:32 -0500132 // NOLINTNEXTLINE(runtime/printf)
thestig04bebfe2016-11-04 16:07:25 -0700133 strcpy(buf, "ABCD");
134 EXPECT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, 1));
135 EXPECT_STREQ("ABCD", buf);
136
137 // Note "Foo" is a different key from "foo".
138 EXPECT_EQ(4U,
139 FPDF_VIEWERREF_GetName(document(), "Foo", nullptr, sizeof(buf)));
140 ASSERT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, sizeof(buf)));
141 EXPECT_STREQ("foo", buf);
142
143 // Try to retrieve a boolean and an integer.
144 EXPECT_EQ(
145 0U, FPDF_VIEWERREF_GetName(document(), "HideToolbar", buf, sizeof(buf)));
146 EXPECT_EQ(0U,
147 FPDF_VIEWERREF_GetName(document(), "NumCopies", buf, sizeof(buf)));
148
149 // Try more valid cases.
150 ASSERT_EQ(4U,
151 FPDF_VIEWERREF_GetName(document(), "Direction", buf, sizeof(buf)));
152 EXPECT_STREQ("R2L", buf);
153 ASSERT_EQ(8U,
154 FPDF_VIEWERREF_GetName(document(), "ViewArea", buf, sizeof(buf)));
155 EXPECT_STREQ("CropBox", buf);
Tom Sepez96d13342015-01-16 14:59:26 -0800156}
157
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800158TEST_F(FPDFViewEmbeddertest, NamedDests) {
Wei Li091f7a02015-11-09 12:09:55 -0800159 EXPECT_TRUE(OpenDocument("named_dests.pdf"));
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800160 long buffer_size;
161 char fixed_buffer[512];
162 FPDF_DEST dest;
163
164 // Query the size of the first item.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 buffer_size = 2000000; // Absurdly large, check not used for this case.
Tom Sepezcf22eb82015-05-12 17:28:08 -0700166 dest = FPDF_GetNamedDest(document(), 0, nullptr, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800167 EXPECT_NE(nullptr, dest);
weili3cc01f22016-05-16 13:53:42 -0700168 EXPECT_EQ(12, buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800169
170 // Try to retrieve the first item with too small a buffer.
Oliver Chang35e68a52015-12-09 12:44:33 -0800171 buffer_size = 10;
Tom Sepezcf22eb82015-05-12 17:28:08 -0700172 dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800173 EXPECT_NE(nullptr, dest);
174 EXPECT_EQ(-1, buffer_size);
175
176 // Try to retrieve the first item with correctly sized buffer. Item is
177 // taken from Dests NameTree in named_dests.pdf.
178 buffer_size = 12;
Tom Sepezcf22eb82015-05-12 17:28:08 -0700179 dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800180 EXPECT_NE(nullptr, dest);
weili3cc01f22016-05-16 13:53:42 -0700181 EXPECT_EQ(12, buffer_size);
Oliver Chang35e68a52015-12-09 12:44:33 -0800182 EXPECT_EQ(std::string("F\0i\0r\0s\0t\0\0\0", 12),
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800183 std::string(fixed_buffer, buffer_size));
184
185 // Try to retrieve the second item with ample buffer. Item is taken
186 // from Dests NameTree but has a sub-dictionary in named_dests.pdf.
187 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700188 dest = FPDF_GetNamedDest(document(), 1, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800189 EXPECT_NE(nullptr, dest);
weili3cc01f22016-05-16 13:53:42 -0700190 EXPECT_EQ(10, buffer_size);
Oliver Chang35e68a52015-12-09 12:44:33 -0800191 EXPECT_EQ(std::string("N\0e\0x\0t\0\0\0", 10),
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800192 std::string(fixed_buffer, buffer_size));
193
194 // Try to retrieve third item with ample buffer. Item is taken
195 // from Dests NameTree but has a bad sub-dictionary in named_dests.pdf.
196 // in named_dests.pdf).
197 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700198 dest = FPDF_GetNamedDest(document(), 2, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800199 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700200 EXPECT_EQ(sizeof(fixed_buffer),
201 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800202
203 // Try to retrieve the forth item with ample buffer. Item is taken
204 // from Dests NameTree but has a vale of the wrong type in named_dests.pdf.
205 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700206 dest = FPDF_GetNamedDest(document(), 3, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800207 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700208 EXPECT_EQ(sizeof(fixed_buffer),
209 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800210
211 // Try to retrieve fifth item with ample buffer. Item taken from the
212 // old-style Dests dictionary object in named_dests.pdf.
213 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700214 dest = FPDF_GetNamedDest(document(), 4, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800215 EXPECT_NE(nullptr, dest);
weili3cc01f22016-05-16 13:53:42 -0700216 EXPECT_EQ(30, buffer_size);
Oliver Chang35e68a52015-12-09 12:44:33 -0800217 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 -0700218 std::string(fixed_buffer, buffer_size));
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800219
220 // Try to retrieve sixth item with ample buffer. Item istaken from the
221 // old-style Dests dictionary object but has a sub-dictionary in
222 // named_dests.pdf.
223 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700224 dest = FPDF_GetNamedDest(document(), 5, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800225 EXPECT_NE(nullptr, dest);
weili3cc01f22016-05-16 13:53:42 -0700226 EXPECT_EQ(28, buffer_size);
Oliver Chang35e68a52015-12-09 12:44:33 -0800227 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 -0700228 std::string(fixed_buffer, buffer_size));
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800229
230 // Try to retrieve non-existent item with ample buffer.
231 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700232 dest = FPDF_GetNamedDest(document(), 6, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800233 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700234 EXPECT_EQ(sizeof(fixed_buffer),
235 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800236
237 // Try to underflow/overflow the integer index.
238 buffer_size = sizeof(fixed_buffer);
239 dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::max(),
Tom Sepezcf22eb82015-05-12 17:28:08 -0700240 fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800241 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700242 EXPECT_EQ(sizeof(fixed_buffer),
243 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800244
245 buffer_size = sizeof(fixed_buffer);
246 dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::min(),
Tom Sepezcf22eb82015-05-12 17:28:08 -0700247 fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800248 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700249 EXPECT_EQ(sizeof(fixed_buffer),
250 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800251
252 buffer_size = sizeof(fixed_buffer);
Tom Sepezcf22eb82015-05-12 17:28:08 -0700253 dest = FPDF_GetNamedDest(document(), -1, fixed_buffer, &buffer_size);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800254 EXPECT_EQ(nullptr, dest);
Wei Li05d53f02016-03-29 16:42:53 -0700255 EXPECT_EQ(sizeof(fixed_buffer),
256 static_cast<size_t>(buffer_size)); // unmodified.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800257}
258
259TEST_F(FPDFViewEmbeddertest, NamedDestsByName) {
Wei Li091f7a02015-11-09 12:09:55 -0800260 EXPECT_TRUE(OpenDocument("named_dests.pdf"));
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800261
thestig1cd352e2016-06-07 17:53:06 -0700262 // Null pointer returns nullptr.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800263 FPDF_DEST dest = FPDF_GetNamedDestByName(document(), nullptr);
264 EXPECT_EQ(nullptr, dest);
265
thestig1cd352e2016-06-07 17:53:06 -0700266 // Empty string returns nullptr.
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800267 dest = FPDF_GetNamedDestByName(document(), "");
268 EXPECT_EQ(nullptr, dest);
269
270 // Item from Dests NameTree.
271 dest = FPDF_GetNamedDestByName(document(), "First");
272 EXPECT_NE(nullptr, dest);
273
274 long ignore_len = 0;
275 FPDF_DEST dest_by_index =
Tom Sepezcf22eb82015-05-12 17:28:08 -0700276 FPDF_GetNamedDest(document(), 0, nullptr, &ignore_len);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800277 EXPECT_EQ(dest_by_index, dest);
278
279 // Item from Dests dictionary.
280 dest = FPDF_GetNamedDestByName(document(), "FirstAlternate");
281 EXPECT_NE(nullptr, dest);
282
283 ignore_len = 0;
Tom Sepezcf22eb82015-05-12 17:28:08 -0700284 dest_by_index = FPDF_GetNamedDest(document(), 4, nullptr, &ignore_len);
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800285 EXPECT_EQ(dest_by_index, dest);
286
287 // Bad value type for item from Dests NameTree array.
288 dest = FPDF_GetNamedDestByName(document(), "WrongType");
289 EXPECT_EQ(nullptr, dest);
290
291 // No such destination in either Dest NameTree or dictionary.
292 dest = FPDF_GetNamedDestByName(document(), "Bogus");
293 EXPECT_EQ(nullptr, dest);
294}
Tom Sepeze80685c2015-01-26 16:59:09 -0800295
296// The following tests pass if the document opens without crashing.
Tom Sepez4dcf74d2015-02-03 16:24:43 -0800297TEST_F(FPDFViewEmbeddertest, Crasher_113) {
Wei Li091f7a02015-11-09 12:09:55 -0800298 EXPECT_TRUE(OpenDocument("bug_113.pdf"));
Tom Sepez4dcf74d2015-02-03 16:24:43 -0800299}
300
301TEST_F(FPDFViewEmbeddertest, Crasher_451830) {
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400302 // Document is damaged and can't be opened.
Wei Li091f7a02015-11-09 12:09:55 -0800303 EXPECT_FALSE(OpenDocument("bug_451830.pdf"));
Tom Sepeze80685c2015-01-26 16:59:09 -0800304}
Tom Sepez3795a742015-02-02 11:22:42 -0800305
Tom Sepez4dcf74d2015-02-03 16:24:43 -0800306TEST_F(FPDFViewEmbeddertest, Crasher_452455) {
Wei Li091f7a02015-11-09 12:09:55 -0800307 EXPECT_TRUE(OpenDocument("bug_452455.pdf"));
Tom Sepez3795a742015-02-02 11:22:42 -0800308 FPDF_PAGE page = LoadPage(0);
309 EXPECT_NE(nullptr, page);
Lei Zhangd27acae2015-05-15 15:36:02 -0700310 UnloadPage(page);
Tom Sepez3795a742015-02-02 11:22:42 -0800311}
Tom Sepez93daa3c2015-02-05 10:51:54 -0800312
313TEST_F(FPDFViewEmbeddertest, Crasher_454695) {
Lei Zhangab5537d2016-01-06 14:58:14 -0800314 // Document is damaged and can't be opened.
Wei Li091f7a02015-11-09 12:09:55 -0800315 EXPECT_FALSE(OpenDocument("bug_454695.pdf"));
Tom Sepez93daa3c2015-02-05 10:51:54 -0800316}
Lei Zhang5fae9722015-12-09 23:56:51 -0800317
Lei Zhangab5537d2016-01-06 14:58:14 -0800318TEST_F(FPDFViewEmbeddertest, Crasher_572871) {
319 EXPECT_TRUE(OpenDocument("bug_572871.pdf"));
320}
321
Wei Li07fb4cf2016-01-14 15:15:14 -0800322// It tests that document can still be loaded even the trailer has no 'Size'
323// field if other information is right.
324TEST_F(FPDFViewEmbeddertest, Failed_213) {
325 EXPECT_TRUE(OpenDocument("bug_213.pdf"));
326}
327
Lei Zhang5fae9722015-12-09 23:56:51 -0800328// The following tests pass if the document opens without infinite looping.
329TEST_F(FPDFViewEmbeddertest, Hang_298) {
330 EXPECT_FALSE(OpenDocument("bug_298.pdf"));
331}
Wei Lic009e8e2016-01-05 12:51:32 -0800332
333// Test if the document opens without infinite looping.
334// Previously this test will hang in a loop inside LoadAllCrossRefV4. After
335// the fix, LoadAllCrossRefV4 will return false after detecting a cross
336// reference loop. Cross references will be rebuilt successfully.
337TEST_F(FPDFViewEmbeddertest, CrossRefV4Loop) {
338 EXPECT_TRUE(OpenDocument("bug_xrefv4_loop.pdf"));
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300339 MockDownloadHints hints;
Lei Zhang671f0d42017-08-31 11:00:54 -0700340
341 // Make sure calling FPDFAvail_IsDocAvail() on this file does not infinite
342 // loop either. See bug 875.
343 int ret = PDF_DATA_NOTAVAIL;
344 while (ret == PDF_DATA_NOTAVAIL)
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300345 ret = FPDFAvail_IsDocAvail(avail_, &hints);
Lei Zhang671f0d42017-08-31 11:00:54 -0700346 EXPECT_EQ(PDF_DATA_AVAIL, ret);
Wei Lic009e8e2016-01-05 12:51:32 -0800347}
Wei Li8e3f8932016-01-08 14:36:59 -0800348
349// The test should pass when circular references to ParseIndirectObject will not
350// cause infinite loop.
351TEST_F(FPDFViewEmbeddertest, Hang_343) {
352 EXPECT_FALSE(OpenDocument("bug_343.pdf"));
Wei Lif2b300a2016-01-11 14:16:10 -0800353}
354
355// The test should pass when the absence of 'Contents' field in a signature
356// dictionary will not cause an infinite loop in CPDF_SyntaxParser::GetObject().
357TEST_F(FPDFViewEmbeddertest, Hang_344) {
358 EXPECT_FALSE(OpenDocument("bug_344.pdf"));
Wei Li215816b2016-01-14 12:29:02 -0800359}
360
Wei Li6bc997a2016-01-19 12:35:03 -0800361// The test should pass when there is no infinite recursion in
362// CPDF_SyntaxParser::GetString().
363TEST_F(FPDFViewEmbeddertest, Hang_355) {
364 EXPECT_FALSE(OpenDocument("bug_355.pdf"));
365}
Wei Li215816b2016-01-14 12:29:02 -0800366// The test should pass even when the file has circular references to pages.
367TEST_F(FPDFViewEmbeddertest, Hang_360) {
368 EXPECT_FALSE(OpenDocument("bug_360.pdf"));
Wei Li07fb4cf2016-01-14 15:15:14 -0800369}
thestiga78ba602016-11-23 15:25:48 -0800370
371TEST_F(FPDFViewEmbeddertest, FPDF_RenderPageBitmapWithMatrix) {
Nicolas Pena24b07332017-09-13 18:02:11 -0400372 const char* const kRotatedMD5[4] = {
373 "0a90de37f52127619c3dfb642b5fa2fe", "d599429574ff0dcad3bc898ea8b874ca",
374 "0113386bb0bd45125bacc6dee78bfe78", "051fcfa4c1f9de28765705633a8ef3a9"};
375 const char kTopLeftQuarterMD5[] = "4982be08db3f6d2e6409186ebbced9eb";
376 const char kHoriStretchedMD5[] = "004bf38f3c5c76a644e6fca204747f21";
377 const char kRotateandStretchMD5[] = "0ea95cacc716d003cf063a2c5ed6c8d7";
thestiga78ba602016-11-23 15:25:48 -0800378
Nicolas Pena60bde102017-07-26 13:50:12 -0400379 EXPECT_TRUE(OpenDocument("rectangles.pdf"));
thestiga78ba602016-11-23 15:25:48 -0800380 FPDF_PAGE page = LoadPage(0);
381 EXPECT_NE(nullptr, page);
Nicolas Pena24b07332017-09-13 18:02:11 -0400382 const int initial_width = static_cast<int>(FPDF_GetPageWidth(page));
383 const int initial_height = static_cast<int>(FPDF_GetPageHeight(page));
384 EXPECT_EQ(200, initial_width);
385 EXPECT_EQ(300, initial_height);
thestiga78ba602016-11-23 15:25:48 -0800386
387 FPDF_BITMAP bitmap = RenderPage(page);
Nicolas Pena24b07332017-09-13 18:02:11 -0400388 CompareBitmap(bitmap, initial_width, initial_height, kRotatedMD5[0]);
thestiga78ba602016-11-23 15:25:48 -0800389 FPDFBitmap_Destroy(bitmap);
390
Nicolas Pena24b07332017-09-13 18:02:11 -0400391 int width;
392 int height;
thestiga78ba602016-11-23 15:25:48 -0800393 FS_RECTF rect;
394 rect.left = 0;
395 rect.top = 0;
Nicolas Pena24b07332017-09-13 18:02:11 -0400396 FS_MATRIX matrix;
thestiga78ba602016-11-23 15:25:48 -0800397
Nicolas Pena24b07332017-09-13 18:02:11 -0400398 // Try the easy rotations: 0, 90, 180, 270 clockwise. The output should be the
399 // same as FPDF_RenderPageBitmap with the appropriate rotation flag. Per PDF
400 // spec section 4.2.2, a t degree rotation is represented by [cos(t) sin(t)
401 // -sin(t) cos(t) 0 0] (matrix goes on the right in the multiplication).
402 rect.right = initial_width;
403 rect.bottom = initial_height;
404 CFX_Matrix rot_matrices[4] = {
405 CFX_Matrix(1, 0, 0, 1, 0, 0), CFX_Matrix(0, -1, 1, 0, 0, 0),
406 CFX_Matrix(-1, 0, 0, -1, 0, 0), CFX_Matrix(0, 1, -1, 0, 0, 0)};
407 for (int rot = 0; rot < 4; ++rot) {
408 matrix.a = rot_matrices[rot].a;
409 matrix.b = rot_matrices[rot].b;
410 matrix.c = rot_matrices[rot].c;
411 matrix.d = rot_matrices[rot].d;
412 matrix.e = rot_matrices[rot].e;
413 matrix.f = rot_matrices[rot].f;
414 if (rot % 2 == 0) {
415 width = initial_width;
416 height = initial_height;
417 } else {
418 width = initial_height;
419 height = initial_width;
420 }
421 rect.right = width;
422 rect.bottom = height;
423
424 bitmap = FPDFBitmap_Create(width, height, 0);
425 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
426 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, rot, 0);
427 CompareBitmap(bitmap, width, height, kRotatedMD5[rot]);
428 FPDFBitmap_Destroy(bitmap);
429
430 bitmap = FPDFBitmap_Create(width, height, 0);
431 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
432 FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0);
433 CompareBitmap(bitmap, width, height, kRotatedMD5[rot]);
434 FPDFBitmap_Destroy(bitmap);
435 }
436 // TODO(npm): what to do with transformations that do not align the page with
437 // the axis, like a 45 degree rotation (currently, part of the page gets cut
438 // out). pdfium:849
thestiga78ba602016-11-23 15:25:48 -0800439
Nicolas Pena60bde102017-07-26 13:50:12 -0400440 // Now render again with the image scaled smaller.
Nicolas Pena24b07332017-09-13 18:02:11 -0400441 width = initial_width / 2;
442 height = initial_height / 2;
thestiga78ba602016-11-23 15:25:48 -0800443 matrix.a = 0.5;
Nicolas Pena24b07332017-09-13 18:02:11 -0400444 matrix.b = 0;
445 matrix.c = 0;
thestiga78ba602016-11-23 15:25:48 -0800446 matrix.d = 0.5;
447
Nicolas Pena24b07332017-09-13 18:02:11 -0400448 rect.right = width;
449 rect.bottom = height;
450
thestiga78ba602016-11-23 15:25:48 -0800451 bitmap = FPDFBitmap_Create(width, height, 0);
452 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
453 FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0);
Nicolas Pena60bde102017-07-26 13:50:12 -0400454 CompareBitmap(bitmap, width, height, kTopLeftQuarterMD5);
455 FPDFBitmap_Destroy(bitmap);
456
Nicolas Pena24b07332017-09-13 18:02:11 -0400457 // Now render again with the image scaled larger horizontally.
458 width = initial_width * 2;
459 height = initial_height;
Nicolas Pena60bde102017-07-26 13:50:12 -0400460 matrix.a = 2;
461 matrix.d = 1;
Nicolas Pena24b07332017-09-13 18:02:11 -0400462 rect.right = width;
463 rect.bottom = height;
Nicolas Pena60bde102017-07-26 13:50:12 -0400464 bitmap = FPDFBitmap_Create(width, height, 0);
465 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
466 FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0);
Nicolas Pena24b07332017-09-13 18:02:11 -0400467 CompareBitmap(bitmap, width, height, kHoriStretchedMD5);
Nicolas Pena60bde102017-07-26 13:50:12 -0400468 FPDFBitmap_Destroy(bitmap);
469
Nicolas Pena24b07332017-09-13 18:02:11 -0400470 // Test a rotation followed by a stretch.
471 width = initial_height * 2;
472 height = initial_width;
Nicolas Pena60bde102017-07-26 13:50:12 -0400473 matrix.a = 0;
Nicolas Pena24b07332017-09-13 18:02:11 -0400474 matrix.b = -1;
475 matrix.c = 2;
Nicolas Pena60bde102017-07-26 13:50:12 -0400476 matrix.d = 0;
Nicolas Pena24b07332017-09-13 18:02:11 -0400477 matrix.e = 0;
478 matrix.f = 0;
479 rect.right = width;
480 rect.bottom = height;
Nicolas Pena60bde102017-07-26 13:50:12 -0400481 bitmap = FPDFBitmap_Create(width, height, 0);
482 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
483 FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0);
Nicolas Pena24b07332017-09-13 18:02:11 -0400484 CompareBitmap(bitmap, width, height, kRotateandStretchMD5);
thestiga78ba602016-11-23 15:25:48 -0800485 FPDFBitmap_Destroy(bitmap);
486
487 UnloadPage(page);
488}
Dan Sinclaircb377be2017-05-11 16:05:32 -0400489
490class UnSupRecordDelegate : public EmbedderTest::Delegate {
491 public:
492 UnSupRecordDelegate() : type_(-1) {}
493 ~UnSupRecordDelegate() override {}
494
495 void UnsupportedHandler(int type) override { type_ = type; }
496
497 int type_;
498};
499
500TEST_F(FPDFViewEmbeddertest, UnSupportedOperations_NotFound) {
501 UnSupRecordDelegate delegate;
502 SetDelegate(&delegate);
503 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
504 EXPECT_EQ(delegate.type_, -1);
505 SetDelegate(nullptr);
506}
507
508TEST_F(FPDFViewEmbeddertest, UnSupportedOperations_LoadCustomDocument) {
509 UnSupRecordDelegate delegate;
510 SetDelegate(&delegate);
511 ASSERT_TRUE(OpenDocument("unsupported_feature.pdf"));
512 EXPECT_EQ(FPDF_UNSP_DOC_PORTABLECOLLECTION, delegate.type_);
513 SetDelegate(nullptr);
514}
515
516TEST_F(FPDFViewEmbeddertest, UnSupportedOperations_LoadDocument) {
517 std::string file_path;
518 ASSERT_TRUE(
519 PathService::GetTestFilePath("unsupported_feature.pdf", &file_path));
520
521 UnSupRecordDelegate delegate;
522 SetDelegate(&delegate);
523 FPDF_DOCUMENT doc = FPDF_LoadDocument(file_path.c_str(), "");
524 EXPECT_TRUE(doc != nullptr);
525 EXPECT_EQ(FPDF_UNSP_DOC_PORTABLECOLLECTION, delegate.type_);
526 FPDF_CloseDocument(doc);
527 SetDelegate(nullptr);
528}