Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 1 | // 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 Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 5 | #include <limits> |
| 6 | #include <string> |
| 7 | |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 8 | #include "core/fxcrt/fx_coordinates.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 9 | #include "fpdfsdk/fpdfview_c_api_test.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 10 | #include "public/fpdfview.h" |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 11 | #include "testing/embedder_test.h" |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
Dan Sinclair | cb377be | 2017-05-11 16:05:32 -0400 | [diff] [blame] | 13 | #include "testing/utils/path_service.h" |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 14 | |
Artem Strygin | 0e60b9e | 2017-09-28 18:46:03 +0300 | [diff] [blame] | 15 | namespace { |
| 16 | |
| 17 | class 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 32 | TEST(fpdf, CApiTest) { |
| 33 | EXPECT_TRUE(CheckPDFiumCApi()); |
| 34 | } |
| 35 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 36 | class FPDFViewEmbeddertest : public EmbedderTest {}; |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 37 | |
| 38 | TEST_F(FPDFViewEmbeddertest, Document) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 39 | EXPECT_TRUE(OpenDocument("about_blank.pdf")); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 40 | 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 Liu | 8a1081f | 2017-08-22 16:28:05 -0400 | [diff] [blame] | 51 | TEST_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 | |
thestig | b8db511 | 2016-04-06 12:12:52 -0700 | [diff] [blame] | 57 | // See bug 465. |
| 58 | TEST_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 | |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 84 | 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 | |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 88 | EXPECT_EQ(0u, FPDF_CountNamedDests(document())); |
thestig | b8db511 | 2016-04-06 12:12:52 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Lei Zhang | abe8280 | 2017-08-09 13:33:31 -0700 | [diff] [blame] | 91 | TEST_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 Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 98 | TEST_F(FPDFViewEmbeddertest, Page) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 99 | EXPECT_TRUE(OpenDocument("about_blank.pdf")); |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 100 | FPDF_PAGE page = LoadPage(0); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 101 | EXPECT_NE(nullptr, page); |
| 102 | EXPECT_EQ(612.0, FPDF_GetPageWidth(page)); |
| 103 | EXPECT_EQ(792.0, FPDF_GetPageHeight(page)); |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 104 | UnloadPage(page); |
| 105 | EXPECT_EQ(nullptr, LoadPage(1)); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 106 | } |
| 107 | |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 108 | TEST_F(FPDFViewEmbeddertest, ViewerRefDummy) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 109 | EXPECT_TRUE(OpenDocument("about_blank.pdf")); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 110 | EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document())); |
| 111 | EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document())); |
| 112 | EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document())); |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 113 | |
| 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 | |
| 119 | TEST_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 Sinclair | 85c8e7f | 2016-11-21 13:50:32 -0500 | [diff] [blame] | 132 | // NOLINTNEXTLINE(runtime/printf) |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 133 | 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 Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 158 | TEST_F(FPDFViewEmbeddertest, NamedDests) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 159 | EXPECT_TRUE(OpenDocument("named_dests.pdf")); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 160 | long buffer_size; |
| 161 | char fixed_buffer[512]; |
| 162 | FPDF_DEST dest; |
| 163 | |
| 164 | // Query the size of the first item. |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 165 | buffer_size = 2000000; // Absurdly large, check not used for this case. |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 166 | dest = FPDF_GetNamedDest(document(), 0, nullptr, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 167 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 168 | EXPECT_EQ(12, buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 169 | |
| 170 | // Try to retrieve the first item with too small a buffer. |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 171 | buffer_size = 10; |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 172 | dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 173 | 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 179 | dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 180 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 181 | EXPECT_EQ(12, buffer_size); |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 182 | EXPECT_EQ(std::string("F\0i\0r\0s\0t\0\0\0", 12), |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 183 | 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 188 | dest = FPDF_GetNamedDest(document(), 1, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 189 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 190 | EXPECT_EQ(10, buffer_size); |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 191 | EXPECT_EQ(std::string("N\0e\0x\0t\0\0\0", 10), |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 192 | 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 198 | dest = FPDF_GetNamedDest(document(), 2, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 199 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 200 | EXPECT_EQ(sizeof(fixed_buffer), |
| 201 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 202 | |
| 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 206 | dest = FPDF_GetNamedDest(document(), 3, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 207 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 208 | EXPECT_EQ(sizeof(fixed_buffer), |
| 209 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 210 | |
| 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 214 | dest = FPDF_GetNamedDest(document(), 4, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 215 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 216 | EXPECT_EQ(30, buffer_size); |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 217 | EXPECT_EQ(std::string("F\0i\0r\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 30), |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 218 | std::string(fixed_buffer, buffer_size)); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 219 | |
| 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 224 | dest = FPDF_GetNamedDest(document(), 5, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 225 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 226 | EXPECT_EQ(28, buffer_size); |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 227 | EXPECT_EQ(std::string("L\0a\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 28), |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 228 | std::string(fixed_buffer, buffer_size)); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 229 | |
| 230 | // Try to retrieve non-existent item with ample buffer. |
| 231 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 232 | dest = FPDF_GetNamedDest(document(), 6, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 233 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 234 | EXPECT_EQ(sizeof(fixed_buffer), |
| 235 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 236 | |
| 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 240 | fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 241 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 242 | EXPECT_EQ(sizeof(fixed_buffer), |
| 243 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 244 | |
| 245 | buffer_size = sizeof(fixed_buffer); |
| 246 | dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::min(), |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 247 | fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 248 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 249 | EXPECT_EQ(sizeof(fixed_buffer), |
| 250 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 251 | |
| 252 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 253 | dest = FPDF_GetNamedDest(document(), -1, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 254 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 255 | EXPECT_EQ(sizeof(fixed_buffer), |
| 256 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | TEST_F(FPDFViewEmbeddertest, NamedDestsByName) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 260 | EXPECT_TRUE(OpenDocument("named_dests.pdf")); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 261 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 262 | // Null pointer returns nullptr. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 263 | FPDF_DEST dest = FPDF_GetNamedDestByName(document(), nullptr); |
| 264 | EXPECT_EQ(nullptr, dest); |
| 265 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 266 | // Empty string returns nullptr. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 267 | 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 276 | FPDF_GetNamedDest(document(), 0, nullptr, &ignore_len); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 277 | 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 284 | dest_by_index = FPDF_GetNamedDest(document(), 4, nullptr, &ignore_len); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 285 | 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 Sepez | e80685c | 2015-01-26 16:59:09 -0800 | [diff] [blame] | 295 | |
| 296 | // The following tests pass if the document opens without crashing. |
Tom Sepez | 4dcf74d | 2015-02-03 16:24:43 -0800 | [diff] [blame] | 297 | TEST_F(FPDFViewEmbeddertest, Crasher_113) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 298 | EXPECT_TRUE(OpenDocument("bug_113.pdf")); |
Tom Sepez | 4dcf74d | 2015-02-03 16:24:43 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | TEST_F(FPDFViewEmbeddertest, Crasher_451830) { |
Dan Sinclair | 6be2aab | 2015-10-28 13:58:49 -0400 | [diff] [blame] | 302 | // Document is damaged and can't be opened. |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 303 | EXPECT_FALSE(OpenDocument("bug_451830.pdf")); |
Tom Sepez | e80685c | 2015-01-26 16:59:09 -0800 | [diff] [blame] | 304 | } |
Tom Sepez | 3795a74 | 2015-02-02 11:22:42 -0800 | [diff] [blame] | 305 | |
Tom Sepez | 4dcf74d | 2015-02-03 16:24:43 -0800 | [diff] [blame] | 306 | TEST_F(FPDFViewEmbeddertest, Crasher_452455) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 307 | EXPECT_TRUE(OpenDocument("bug_452455.pdf")); |
Tom Sepez | 3795a74 | 2015-02-02 11:22:42 -0800 | [diff] [blame] | 308 | FPDF_PAGE page = LoadPage(0); |
| 309 | EXPECT_NE(nullptr, page); |
Lei Zhang | d27acae | 2015-05-15 15:36:02 -0700 | [diff] [blame] | 310 | UnloadPage(page); |
Tom Sepez | 3795a74 | 2015-02-02 11:22:42 -0800 | [diff] [blame] | 311 | } |
Tom Sepez | 93daa3c | 2015-02-05 10:51:54 -0800 | [diff] [blame] | 312 | |
| 313 | TEST_F(FPDFViewEmbeddertest, Crasher_454695) { |
Lei Zhang | ab5537d | 2016-01-06 14:58:14 -0800 | [diff] [blame] | 314 | // Document is damaged and can't be opened. |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 315 | EXPECT_FALSE(OpenDocument("bug_454695.pdf")); |
Tom Sepez | 93daa3c | 2015-02-05 10:51:54 -0800 | [diff] [blame] | 316 | } |
Lei Zhang | 5fae972 | 2015-12-09 23:56:51 -0800 | [diff] [blame] | 317 | |
Lei Zhang | ab5537d | 2016-01-06 14:58:14 -0800 | [diff] [blame] | 318 | TEST_F(FPDFViewEmbeddertest, Crasher_572871) { |
| 319 | EXPECT_TRUE(OpenDocument("bug_572871.pdf")); |
| 320 | } |
| 321 | |
Wei Li | 07fb4cf | 2016-01-14 15:15:14 -0800 | [diff] [blame] | 322 | // It tests that document can still be loaded even the trailer has no 'Size' |
| 323 | // field if other information is right. |
| 324 | TEST_F(FPDFViewEmbeddertest, Failed_213) { |
| 325 | EXPECT_TRUE(OpenDocument("bug_213.pdf")); |
| 326 | } |
| 327 | |
Lei Zhang | 5fae972 | 2015-12-09 23:56:51 -0800 | [diff] [blame] | 328 | // The following tests pass if the document opens without infinite looping. |
| 329 | TEST_F(FPDFViewEmbeddertest, Hang_298) { |
| 330 | EXPECT_FALSE(OpenDocument("bug_298.pdf")); |
| 331 | } |
Wei Li | c009e8e | 2016-01-05 12:51:32 -0800 | [diff] [blame] | 332 | |
| 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. |
| 337 | TEST_F(FPDFViewEmbeddertest, CrossRefV4Loop) { |
| 338 | EXPECT_TRUE(OpenDocument("bug_xrefv4_loop.pdf")); |
Artem Strygin | 0e60b9e | 2017-09-28 18:46:03 +0300 | [diff] [blame] | 339 | MockDownloadHints hints; |
Lei Zhang | 671f0d4 | 2017-08-31 11:00:54 -0700 | [diff] [blame] | 340 | |
| 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 Strygin | 0e60b9e | 2017-09-28 18:46:03 +0300 | [diff] [blame] | 345 | ret = FPDFAvail_IsDocAvail(avail_, &hints); |
Lei Zhang | 671f0d4 | 2017-08-31 11:00:54 -0700 | [diff] [blame] | 346 | EXPECT_EQ(PDF_DATA_AVAIL, ret); |
Wei Li | c009e8e | 2016-01-05 12:51:32 -0800 | [diff] [blame] | 347 | } |
Wei Li | 8e3f893 | 2016-01-08 14:36:59 -0800 | [diff] [blame] | 348 | |
| 349 | // The test should pass when circular references to ParseIndirectObject will not |
| 350 | // cause infinite loop. |
| 351 | TEST_F(FPDFViewEmbeddertest, Hang_343) { |
| 352 | EXPECT_FALSE(OpenDocument("bug_343.pdf")); |
Wei Li | f2b300a | 2016-01-11 14:16:10 -0800 | [diff] [blame] | 353 | } |
| 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(). |
| 357 | TEST_F(FPDFViewEmbeddertest, Hang_344) { |
| 358 | EXPECT_FALSE(OpenDocument("bug_344.pdf")); |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Wei Li | 6bc997a | 2016-01-19 12:35:03 -0800 | [diff] [blame] | 361 | // The test should pass when there is no infinite recursion in |
| 362 | // CPDF_SyntaxParser::GetString(). |
| 363 | TEST_F(FPDFViewEmbeddertest, Hang_355) { |
| 364 | EXPECT_FALSE(OpenDocument("bug_355.pdf")); |
| 365 | } |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 366 | // The test should pass even when the file has circular references to pages. |
| 367 | TEST_F(FPDFViewEmbeddertest, Hang_360) { |
| 368 | EXPECT_FALSE(OpenDocument("bug_360.pdf")); |
Wei Li | 07fb4cf | 2016-01-14 15:15:14 -0800 | [diff] [blame] | 369 | } |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 370 | |
| 371 | TEST_F(FPDFViewEmbeddertest, FPDF_RenderPageBitmapWithMatrix) { |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 372 | 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"; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 378 | |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 379 | EXPECT_TRUE(OpenDocument("rectangles.pdf")); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 380 | FPDF_PAGE page = LoadPage(0); |
| 381 | EXPECT_NE(nullptr, page); |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 382 | 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); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 386 | |
| 387 | FPDF_BITMAP bitmap = RenderPage(page); |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 388 | CompareBitmap(bitmap, initial_width, initial_height, kRotatedMD5[0]); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 389 | FPDFBitmap_Destroy(bitmap); |
| 390 | |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 391 | int width; |
| 392 | int height; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 393 | FS_RECTF rect; |
| 394 | rect.left = 0; |
| 395 | rect.top = 0; |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 396 | FS_MATRIX matrix; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 397 | |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 398 | // 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 |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 439 | |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 440 | // Now render again with the image scaled smaller. |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 441 | width = initial_width / 2; |
| 442 | height = initial_height / 2; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 443 | matrix.a = 0.5; |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 444 | matrix.b = 0; |
| 445 | matrix.c = 0; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 446 | matrix.d = 0.5; |
| 447 | |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 448 | rect.right = width; |
| 449 | rect.bottom = height; |
| 450 | |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 451 | bitmap = FPDFBitmap_Create(width, height, 0); |
| 452 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 453 | FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0); |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 454 | CompareBitmap(bitmap, width, height, kTopLeftQuarterMD5); |
| 455 | FPDFBitmap_Destroy(bitmap); |
| 456 | |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 457 | // Now render again with the image scaled larger horizontally. |
| 458 | width = initial_width * 2; |
| 459 | height = initial_height; |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 460 | matrix.a = 2; |
| 461 | matrix.d = 1; |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 462 | rect.right = width; |
| 463 | rect.bottom = height; |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 464 | bitmap = FPDFBitmap_Create(width, height, 0); |
| 465 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 466 | FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0); |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 467 | CompareBitmap(bitmap, width, height, kHoriStretchedMD5); |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 468 | FPDFBitmap_Destroy(bitmap); |
| 469 | |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 470 | // Test a rotation followed by a stretch. |
| 471 | width = initial_height * 2; |
| 472 | height = initial_width; |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 473 | matrix.a = 0; |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 474 | matrix.b = -1; |
| 475 | matrix.c = 2; |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 476 | matrix.d = 0; |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 477 | matrix.e = 0; |
| 478 | matrix.f = 0; |
| 479 | rect.right = width; |
| 480 | rect.bottom = height; |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 481 | bitmap = FPDFBitmap_Create(width, height, 0); |
| 482 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 483 | FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0); |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 484 | CompareBitmap(bitmap, width, height, kRotateandStretchMD5); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 485 | FPDFBitmap_Destroy(bitmap); |
| 486 | |
| 487 | UnloadPage(page); |
| 488 | } |
Dan Sinclair | cb377be | 2017-05-11 16:05:32 -0400 | [diff] [blame] | 489 | |
| 490 | class 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 | |
| 500 | TEST_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 | |
| 508 | TEST_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 | |
| 516 | TEST_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 | } |