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 | |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 15 | TEST(fpdf, CApiTest) { |
| 16 | EXPECT_TRUE(CheckPDFiumCApi()); |
| 17 | } |
| 18 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 19 | class FPDFViewEmbeddertest : public EmbedderTest {}; |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 20 | |
| 21 | TEST_F(FPDFViewEmbeddertest, Document) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 22 | EXPECT_TRUE(OpenDocument("about_blank.pdf")); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 23 | 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 Liu | 8a1081f | 2017-08-22 16:28:05 -0400 | [diff] [blame] | 34 | TEST_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 | |
thestig | b8db511 | 2016-04-06 12:12:52 -0700 | [diff] [blame] | 40 | // See bug 465. |
| 41 | TEST_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 | |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 67 | 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 | |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 71 | EXPECT_EQ(0u, FPDF_CountNamedDests(document())); |
thestig | b8db511 | 2016-04-06 12:12:52 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Lei Zhang | abe8280 | 2017-08-09 13:33:31 -0700 | [diff] [blame] | 74 | TEST_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 Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 81 | TEST_F(FPDFViewEmbeddertest, Page) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 82 | EXPECT_TRUE(OpenDocument("about_blank.pdf")); |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 83 | FPDF_PAGE page = LoadPage(0); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 84 | EXPECT_NE(nullptr, page); |
| 85 | EXPECT_EQ(612.0, FPDF_GetPageWidth(page)); |
| 86 | EXPECT_EQ(792.0, FPDF_GetPageHeight(page)); |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 87 | UnloadPage(page); |
| 88 | EXPECT_EQ(nullptr, LoadPage(1)); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 89 | } |
| 90 | |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 91 | TEST_F(FPDFViewEmbeddertest, ViewerRefDummy) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 92 | EXPECT_TRUE(OpenDocument("about_blank.pdf")); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 93 | EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document())); |
| 94 | EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document())); |
| 95 | EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document())); |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 96 | |
| 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 | |
| 102 | TEST_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 Sinclair | 85c8e7f | 2016-11-21 13:50:32 -0500 | [diff] [blame] | 115 | // NOLINTNEXTLINE(runtime/printf) |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 116 | 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 Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 141 | TEST_F(FPDFViewEmbeddertest, NamedDests) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 142 | EXPECT_TRUE(OpenDocument("named_dests.pdf")); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 143 | long buffer_size; |
| 144 | char fixed_buffer[512]; |
| 145 | FPDF_DEST dest; |
| 146 | |
| 147 | // Query the size of the first item. |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 148 | buffer_size = 2000000; // Absurdly large, check not used for this case. |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 149 | dest = FPDF_GetNamedDest(document(), 0, nullptr, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 150 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 151 | EXPECT_EQ(12, buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 152 | |
| 153 | // Try to retrieve the first item with too small a buffer. |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 154 | buffer_size = 10; |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 155 | dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 156 | 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 162 | dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 163 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 164 | EXPECT_EQ(12, buffer_size); |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 165 | 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] | 166 | 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 171 | dest = FPDF_GetNamedDest(document(), 1, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 172 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 173 | EXPECT_EQ(10, buffer_size); |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 174 | EXPECT_EQ(std::string("N\0e\0x\0t\0\0\0", 10), |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 175 | 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 181 | dest = FPDF_GetNamedDest(document(), 2, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 182 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 183 | EXPECT_EQ(sizeof(fixed_buffer), |
| 184 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 185 | |
| 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 189 | dest = FPDF_GetNamedDest(document(), 3, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 190 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 191 | EXPECT_EQ(sizeof(fixed_buffer), |
| 192 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 193 | |
| 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 197 | dest = FPDF_GetNamedDest(document(), 4, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 198 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 199 | EXPECT_EQ(30, buffer_size); |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 200 | 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] | 201 | std::string(fixed_buffer, buffer_size)); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 202 | |
| 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 207 | dest = FPDF_GetNamedDest(document(), 5, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 208 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 209 | EXPECT_EQ(28, buffer_size); |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 210 | 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] | 211 | std::string(fixed_buffer, buffer_size)); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 212 | |
| 213 | // Try to retrieve non-existent item with ample buffer. |
| 214 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 215 | dest = FPDF_GetNamedDest(document(), 6, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 216 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 217 | EXPECT_EQ(sizeof(fixed_buffer), |
| 218 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 219 | |
| 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 223 | fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 224 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 225 | EXPECT_EQ(sizeof(fixed_buffer), |
| 226 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 227 | |
| 228 | buffer_size = sizeof(fixed_buffer); |
| 229 | dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::min(), |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 230 | fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 231 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 232 | EXPECT_EQ(sizeof(fixed_buffer), |
| 233 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 234 | |
| 235 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 236 | dest = FPDF_GetNamedDest(document(), -1, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 237 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 238 | EXPECT_EQ(sizeof(fixed_buffer), |
| 239 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | TEST_F(FPDFViewEmbeddertest, NamedDestsByName) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 243 | EXPECT_TRUE(OpenDocument("named_dests.pdf")); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 244 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 245 | // Null pointer returns nullptr. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 246 | FPDF_DEST dest = FPDF_GetNamedDestByName(document(), nullptr); |
| 247 | EXPECT_EQ(nullptr, dest); |
| 248 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 249 | // Empty string returns nullptr. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 250 | 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 259 | FPDF_GetNamedDest(document(), 0, nullptr, &ignore_len); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 260 | 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 Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 267 | dest_by_index = FPDF_GetNamedDest(document(), 4, nullptr, &ignore_len); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 268 | 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 Sepez | e80685c | 2015-01-26 16:59:09 -0800 | [diff] [blame] | 278 | |
| 279 | // The following tests pass if the document opens without crashing. |
Tom Sepez | 4dcf74d | 2015-02-03 16:24:43 -0800 | [diff] [blame] | 280 | TEST_F(FPDFViewEmbeddertest, Crasher_113) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 281 | EXPECT_TRUE(OpenDocument("bug_113.pdf")); |
Tom Sepez | 4dcf74d | 2015-02-03 16:24:43 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | TEST_F(FPDFViewEmbeddertest, Crasher_451830) { |
Dan Sinclair | 6be2aab | 2015-10-28 13:58:49 -0400 | [diff] [blame] | 285 | // Document is damaged and can't be opened. |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 286 | EXPECT_FALSE(OpenDocument("bug_451830.pdf")); |
Tom Sepez | e80685c | 2015-01-26 16:59:09 -0800 | [diff] [blame] | 287 | } |
Tom Sepez | 3795a74 | 2015-02-02 11:22:42 -0800 | [diff] [blame] | 288 | |
Tom Sepez | 4dcf74d | 2015-02-03 16:24:43 -0800 | [diff] [blame] | 289 | TEST_F(FPDFViewEmbeddertest, Crasher_452455) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 290 | EXPECT_TRUE(OpenDocument("bug_452455.pdf")); |
Tom Sepez | 3795a74 | 2015-02-02 11:22:42 -0800 | [diff] [blame] | 291 | FPDF_PAGE page = LoadPage(0); |
| 292 | EXPECT_NE(nullptr, page); |
Lei Zhang | d27acae | 2015-05-15 15:36:02 -0700 | [diff] [blame] | 293 | UnloadPage(page); |
Tom Sepez | 3795a74 | 2015-02-02 11:22:42 -0800 | [diff] [blame] | 294 | } |
Tom Sepez | 93daa3c | 2015-02-05 10:51:54 -0800 | [diff] [blame] | 295 | |
| 296 | TEST_F(FPDFViewEmbeddertest, Crasher_454695) { |
Lei Zhang | ab5537d | 2016-01-06 14:58:14 -0800 | [diff] [blame] | 297 | // Document is damaged and can't be opened. |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 298 | EXPECT_FALSE(OpenDocument("bug_454695.pdf")); |
Tom Sepez | 93daa3c | 2015-02-05 10:51:54 -0800 | [diff] [blame] | 299 | } |
Lei Zhang | 5fae972 | 2015-12-09 23:56:51 -0800 | [diff] [blame] | 300 | |
Lei Zhang | ab5537d | 2016-01-06 14:58:14 -0800 | [diff] [blame] | 301 | TEST_F(FPDFViewEmbeddertest, Crasher_572871) { |
| 302 | EXPECT_TRUE(OpenDocument("bug_572871.pdf")); |
| 303 | } |
| 304 | |
Wei Li | 07fb4cf | 2016-01-14 15:15:14 -0800 | [diff] [blame] | 305 | // It tests that document can still be loaded even the trailer has no 'Size' |
| 306 | // field if other information is right. |
| 307 | TEST_F(FPDFViewEmbeddertest, Failed_213) { |
| 308 | EXPECT_TRUE(OpenDocument("bug_213.pdf")); |
| 309 | } |
| 310 | |
Lei Zhang | 5fae972 | 2015-12-09 23:56:51 -0800 | [diff] [blame] | 311 | // The following tests pass if the document opens without infinite looping. |
| 312 | TEST_F(FPDFViewEmbeddertest, Hang_298) { |
| 313 | EXPECT_FALSE(OpenDocument("bug_298.pdf")); |
| 314 | } |
Wei Li | c009e8e | 2016-01-05 12:51:32 -0800 | [diff] [blame] | 315 | |
| 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. |
| 320 | TEST_F(FPDFViewEmbeddertest, CrossRefV4Loop) { |
| 321 | EXPECT_TRUE(OpenDocument("bug_xrefv4_loop.pdf")); |
Lei Zhang | 671f0d4 | 2017-08-31 11:00:54 -0700 | [diff] [blame] | 322 | |
| 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 Li | c009e8e | 2016-01-05 12:51:32 -0800 | [diff] [blame] | 329 | } |
Wei Li | 8e3f893 | 2016-01-08 14:36:59 -0800 | [diff] [blame] | 330 | |
| 331 | // The test should pass when circular references to ParseIndirectObject will not |
| 332 | // cause infinite loop. |
| 333 | TEST_F(FPDFViewEmbeddertest, Hang_343) { |
| 334 | EXPECT_FALSE(OpenDocument("bug_343.pdf")); |
Wei Li | f2b300a | 2016-01-11 14:16:10 -0800 | [diff] [blame] | 335 | } |
| 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(). |
| 339 | TEST_F(FPDFViewEmbeddertest, Hang_344) { |
| 340 | EXPECT_FALSE(OpenDocument("bug_344.pdf")); |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Wei Li | 6bc997a | 2016-01-19 12:35:03 -0800 | [diff] [blame] | 343 | // The test should pass when there is no infinite recursion in |
| 344 | // CPDF_SyntaxParser::GetString(). |
| 345 | TEST_F(FPDFViewEmbeddertest, Hang_355) { |
| 346 | EXPECT_FALSE(OpenDocument("bug_355.pdf")); |
| 347 | } |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 348 | // The test should pass even when the file has circular references to pages. |
| 349 | TEST_F(FPDFViewEmbeddertest, Hang_360) { |
| 350 | EXPECT_FALSE(OpenDocument("bug_360.pdf")); |
Wei Li | 07fb4cf | 2016-01-14 15:15:14 -0800 | [diff] [blame] | 351 | } |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 352 | |
| 353 | TEST_F(FPDFViewEmbeddertest, FPDF_RenderPageBitmapWithMatrix) { |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame^] | 354 | 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"; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 360 | |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 361 | EXPECT_TRUE(OpenDocument("rectangles.pdf")); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 362 | FPDF_PAGE page = LoadPage(0); |
| 363 | EXPECT_NE(nullptr, page); |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame^] | 364 | 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); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 368 | |
| 369 | FPDF_BITMAP bitmap = RenderPage(page); |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame^] | 370 | CompareBitmap(bitmap, initial_width, initial_height, kRotatedMD5[0]); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 371 | FPDFBitmap_Destroy(bitmap); |
| 372 | |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame^] | 373 | int width; |
| 374 | int height; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 375 | FS_RECTF rect; |
| 376 | rect.left = 0; |
| 377 | rect.top = 0; |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame^] | 378 | FS_MATRIX matrix; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 379 | |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame^] | 380 | // 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 |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 421 | |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 422 | // Now render again with the image scaled smaller. |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame^] | 423 | width = initial_width / 2; |
| 424 | height = initial_height / 2; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 425 | matrix.a = 0.5; |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame^] | 426 | matrix.b = 0; |
| 427 | matrix.c = 0; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 428 | matrix.d = 0.5; |
| 429 | |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame^] | 430 | rect.right = width; |
| 431 | rect.bottom = height; |
| 432 | |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 433 | bitmap = FPDFBitmap_Create(width, height, 0); |
| 434 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 435 | FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0); |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 436 | CompareBitmap(bitmap, width, height, kTopLeftQuarterMD5); |
| 437 | FPDFBitmap_Destroy(bitmap); |
| 438 | |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame^] | 439 | // Now render again with the image scaled larger horizontally. |
| 440 | width = initial_width * 2; |
| 441 | height = initial_height; |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 442 | matrix.a = 2; |
| 443 | matrix.d = 1; |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame^] | 444 | rect.right = width; |
| 445 | rect.bottom = height; |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 446 | bitmap = FPDFBitmap_Create(width, height, 0); |
| 447 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 448 | FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0); |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame^] | 449 | CompareBitmap(bitmap, width, height, kHoriStretchedMD5); |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 450 | FPDFBitmap_Destroy(bitmap); |
| 451 | |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame^] | 452 | // Test a rotation followed by a stretch. |
| 453 | width = initial_height * 2; |
| 454 | height = initial_width; |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 455 | matrix.a = 0; |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame^] | 456 | matrix.b = -1; |
| 457 | matrix.c = 2; |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 458 | matrix.d = 0; |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame^] | 459 | matrix.e = 0; |
| 460 | matrix.f = 0; |
| 461 | rect.right = width; |
| 462 | rect.bottom = height; |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 463 | bitmap = FPDFBitmap_Create(width, height, 0); |
| 464 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 465 | FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0); |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame^] | 466 | CompareBitmap(bitmap, width, height, kRotateandStretchMD5); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 467 | FPDFBitmap_Destroy(bitmap); |
| 468 | |
| 469 | UnloadPage(page); |
| 470 | } |
Dan Sinclair | cb377be | 2017-05-11 16:05:32 -0400 | [diff] [blame] | 471 | |
| 472 | class 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 | |
| 482 | TEST_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 | |
| 490 | TEST_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 | |
| 498 | TEST_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 | } |