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 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 8 | #include "fpdfsdk/fpdfview_c_api_test.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 9 | #include "public/fpdfview.h" |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 10 | #include "testing/embedder_test.h" |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
Dan Sinclair | cb377be | 2017-05-11 16:05:32 -0400 | [diff] [blame] | 12 | #include "testing/utils/path_service.h" |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 13 | |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 14 | TEST(fpdf, CApiTest) { |
| 15 | EXPECT_TRUE(CheckPDFiumCApi()); |
| 16 | } |
| 17 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 18 | class FPDFViewEmbeddertest : public EmbedderTest {}; |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 19 | |
| 20 | TEST_F(FPDFViewEmbeddertest, Document) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 21 | EXPECT_TRUE(OpenDocument("about_blank.pdf")); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 22 | EXPECT_EQ(1, GetPageCount()); |
| 23 | EXPECT_EQ(0, GetFirstPageNum()); |
| 24 | |
| 25 | int version; |
| 26 | EXPECT_TRUE(FPDF_GetFileVersion(document(), &version)); |
| 27 | EXPECT_EQ(14, version); |
| 28 | |
| 29 | EXPECT_EQ(0xFFFFFFFF, FPDF_GetDocPermissions(document())); |
| 30 | EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document())); |
| 31 | } |
| 32 | |
Jane Liu | 8a1081f | 2017-08-22 16:28:05 -0400 | [diff] [blame] | 33 | TEST_F(FPDFViewEmbeddertest, LoadNonexistentDocument) { |
| 34 | FPDF_DOCUMENT doc = FPDF_LoadDocument("nonexistent_document.pdf", ""); |
| 35 | ASSERT_FALSE(doc); |
| 36 | EXPECT_EQ(static_cast<int>(FPDF_GetLastError()), FPDF_ERR_FILE); |
| 37 | } |
| 38 | |
thestig | b8db511 | 2016-04-06 12:12:52 -0700 | [diff] [blame] | 39 | // See bug 465. |
| 40 | TEST_F(FPDFViewEmbeddertest, EmptyDocument) { |
| 41 | EXPECT_TRUE(CreateEmptyDocument()); |
| 42 | |
| 43 | { |
| 44 | int version = 42; |
| 45 | EXPECT_FALSE(FPDF_GetFileVersion(document(), &version)); |
| 46 | EXPECT_EQ(0, version); |
| 47 | } |
| 48 | |
| 49 | { |
| 50 | #ifndef PDF_ENABLE_XFA |
| 51 | const unsigned long kExpected = 0; |
| 52 | #else |
| 53 | const unsigned long kExpected = static_cast<uint32_t>(-1); |
| 54 | #endif |
| 55 | EXPECT_EQ(kExpected, FPDF_GetDocPermissions(document())); |
| 56 | } |
| 57 | |
| 58 | EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document())); |
| 59 | |
| 60 | EXPECT_EQ(0, FPDF_GetPageCount(document())); |
| 61 | |
| 62 | EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document())); |
| 63 | EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document())); |
| 64 | EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document())); |
| 65 | |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 66 | char buf[100]; |
| 67 | EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0)); |
| 68 | EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf))); |
| 69 | |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 70 | EXPECT_EQ(0u, FPDF_CountNamedDests(document())); |
thestig | b8db511 | 2016-04-06 12:12:52 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Lei Zhang | abe8280 | 2017-08-09 13:33:31 -0700 | [diff] [blame] | 73 | TEST_F(FPDFViewEmbeddertest, LinearizedDocument) { |
| 74 | EXPECT_TRUE(OpenDocument("feature_linearized_loading.pdf", nullptr, true)); |
| 75 | int version; |
| 76 | EXPECT_TRUE(FPDF_GetFileVersion(document(), &version)); |
| 77 | EXPECT_EQ(16, version); |
| 78 | } |
| 79 | |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 80 | TEST_F(FPDFViewEmbeddertest, Page) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 81 | EXPECT_TRUE(OpenDocument("about_blank.pdf")); |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 82 | FPDF_PAGE page = LoadPage(0); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 83 | EXPECT_NE(nullptr, page); |
| 84 | EXPECT_EQ(612.0, FPDF_GetPageWidth(page)); |
| 85 | EXPECT_EQ(792.0, FPDF_GetPageHeight(page)); |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 86 | UnloadPage(page); |
| 87 | EXPECT_EQ(nullptr, LoadPage(1)); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 88 | } |
| 89 | |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 90 | TEST_F(FPDFViewEmbeddertest, ViewerRefDummy) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 91 | EXPECT_TRUE(OpenDocument("about_blank.pdf")); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 92 | EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document())); |
| 93 | EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document())); |
| 94 | EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document())); |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 95 | |
| 96 | char buf[100]; |
| 97 | EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0)); |
| 98 | EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf))); |
| 99 | } |
| 100 | |
| 101 | TEST_F(FPDFViewEmbeddertest, ViewerRef) { |
| 102 | EXPECT_TRUE(OpenDocument("viewer_ref.pdf")); |
| 103 | EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document())); |
| 104 | EXPECT_EQ(5, FPDF_VIEWERREF_GetNumCopies(document())); |
| 105 | EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document())); |
| 106 | |
| 107 | // Test some corner cases. |
| 108 | char buf[100]; |
| 109 | EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "", buf, sizeof(buf))); |
| 110 | EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0)); |
| 111 | EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf))); |
| 112 | |
| 113 | // Make sure |buf| does not get written into when it appears to be too small. |
Dan Sinclair | 85c8e7f | 2016-11-21 13:50:32 -0500 | [diff] [blame] | 114 | // NOLINTNEXTLINE(runtime/printf) |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 115 | strcpy(buf, "ABCD"); |
| 116 | EXPECT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, 1)); |
| 117 | EXPECT_STREQ("ABCD", buf); |
| 118 | |
| 119 | // Note "Foo" is a different key from "foo". |
| 120 | EXPECT_EQ(4U, |
| 121 | FPDF_VIEWERREF_GetName(document(), "Foo", nullptr, sizeof(buf))); |
| 122 | ASSERT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, sizeof(buf))); |
| 123 | EXPECT_STREQ("foo", buf); |
| 124 | |
| 125 | // Try to retrieve a boolean and an integer. |
| 126 | EXPECT_EQ( |
| 127 | 0U, FPDF_VIEWERREF_GetName(document(), "HideToolbar", buf, sizeof(buf))); |
| 128 | EXPECT_EQ(0U, |
| 129 | FPDF_VIEWERREF_GetName(document(), "NumCopies", buf, sizeof(buf))); |
| 130 | |
| 131 | // Try more valid cases. |
| 132 | ASSERT_EQ(4U, |
| 133 | FPDF_VIEWERREF_GetName(document(), "Direction", buf, sizeof(buf))); |
| 134 | EXPECT_STREQ("R2L", buf); |
| 135 | ASSERT_EQ(8U, |
| 136 | FPDF_VIEWERREF_GetName(document(), "ViewArea", buf, sizeof(buf))); |
| 137 | EXPECT_STREQ("CropBox", buf); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 140 | TEST_F(FPDFViewEmbeddertest, NamedDests) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 141 | EXPECT_TRUE(OpenDocument("named_dests.pdf")); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 142 | long buffer_size; |
| 143 | char fixed_buffer[512]; |
| 144 | FPDF_DEST dest; |
| 145 | |
| 146 | // Query the size of the first item. |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 147 | buffer_size = 2000000; // Absurdly large, check not used for this case. |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 148 | dest = FPDF_GetNamedDest(document(), 0, nullptr, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 149 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 150 | EXPECT_EQ(12, buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 151 | |
| 152 | // Try to retrieve the first item with too small a buffer. |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 153 | buffer_size = 10; |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 154 | dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 155 | EXPECT_NE(nullptr, dest); |
| 156 | EXPECT_EQ(-1, buffer_size); |
| 157 | |
| 158 | // Try to retrieve the first item with correctly sized buffer. Item is |
| 159 | // taken from Dests NameTree in named_dests.pdf. |
| 160 | buffer_size = 12; |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 161 | dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 162 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 163 | EXPECT_EQ(12, buffer_size); |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 164 | 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] | 165 | std::string(fixed_buffer, buffer_size)); |
| 166 | |
| 167 | // Try to retrieve the second item with ample buffer. Item is taken |
| 168 | // from Dests NameTree but has a sub-dictionary in named_dests.pdf. |
| 169 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 170 | dest = FPDF_GetNamedDest(document(), 1, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 171 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 172 | EXPECT_EQ(10, buffer_size); |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 173 | EXPECT_EQ(std::string("N\0e\0x\0t\0\0\0", 10), |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 174 | std::string(fixed_buffer, buffer_size)); |
| 175 | |
| 176 | // Try to retrieve third item with ample buffer. Item is taken |
| 177 | // from Dests NameTree but has a bad sub-dictionary in named_dests.pdf. |
| 178 | // in named_dests.pdf). |
| 179 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 180 | dest = FPDF_GetNamedDest(document(), 2, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 181 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 182 | EXPECT_EQ(sizeof(fixed_buffer), |
| 183 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 184 | |
| 185 | // Try to retrieve the forth item with ample buffer. Item is taken |
| 186 | // from Dests NameTree but has a vale of the wrong type in named_dests.pdf. |
| 187 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 188 | dest = FPDF_GetNamedDest(document(), 3, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 189 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 190 | EXPECT_EQ(sizeof(fixed_buffer), |
| 191 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 192 | |
| 193 | // Try to retrieve fifth item with ample buffer. Item taken from the |
| 194 | // old-style Dests dictionary object in named_dests.pdf. |
| 195 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 196 | dest = FPDF_GetNamedDest(document(), 4, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 197 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 198 | EXPECT_EQ(30, buffer_size); |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 199 | 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] | 200 | std::string(fixed_buffer, buffer_size)); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 201 | |
| 202 | // Try to retrieve sixth item with ample buffer. Item istaken from the |
| 203 | // old-style Dests dictionary object but has a sub-dictionary in |
| 204 | // named_dests.pdf. |
| 205 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 206 | dest = FPDF_GetNamedDest(document(), 5, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 207 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 208 | EXPECT_EQ(28, buffer_size); |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 209 | 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] | 210 | std::string(fixed_buffer, buffer_size)); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 211 | |
| 212 | // Try to retrieve non-existent item with ample buffer. |
| 213 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 214 | dest = FPDF_GetNamedDest(document(), 6, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 215 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 216 | EXPECT_EQ(sizeof(fixed_buffer), |
| 217 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 218 | |
| 219 | // Try to underflow/overflow the integer index. |
| 220 | buffer_size = sizeof(fixed_buffer); |
| 221 | dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::max(), |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 222 | fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 223 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 224 | EXPECT_EQ(sizeof(fixed_buffer), |
| 225 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 226 | |
| 227 | buffer_size = sizeof(fixed_buffer); |
| 228 | dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::min(), |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 229 | fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 230 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 231 | EXPECT_EQ(sizeof(fixed_buffer), |
| 232 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 233 | |
| 234 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 235 | dest = FPDF_GetNamedDest(document(), -1, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 236 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 237 | EXPECT_EQ(sizeof(fixed_buffer), |
| 238 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | TEST_F(FPDFViewEmbeddertest, NamedDestsByName) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 242 | EXPECT_TRUE(OpenDocument("named_dests.pdf")); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 243 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 244 | // Null pointer returns nullptr. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 245 | FPDF_DEST dest = FPDF_GetNamedDestByName(document(), nullptr); |
| 246 | EXPECT_EQ(nullptr, dest); |
| 247 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 248 | // Empty string returns nullptr. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 249 | dest = FPDF_GetNamedDestByName(document(), ""); |
| 250 | EXPECT_EQ(nullptr, dest); |
| 251 | |
| 252 | // Item from Dests NameTree. |
| 253 | dest = FPDF_GetNamedDestByName(document(), "First"); |
| 254 | EXPECT_NE(nullptr, dest); |
| 255 | |
| 256 | long ignore_len = 0; |
| 257 | FPDF_DEST dest_by_index = |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 258 | FPDF_GetNamedDest(document(), 0, nullptr, &ignore_len); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 259 | EXPECT_EQ(dest_by_index, dest); |
| 260 | |
| 261 | // Item from Dests dictionary. |
| 262 | dest = FPDF_GetNamedDestByName(document(), "FirstAlternate"); |
| 263 | EXPECT_NE(nullptr, dest); |
| 264 | |
| 265 | ignore_len = 0; |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 266 | dest_by_index = FPDF_GetNamedDest(document(), 4, nullptr, &ignore_len); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 267 | EXPECT_EQ(dest_by_index, dest); |
| 268 | |
| 269 | // Bad value type for item from Dests NameTree array. |
| 270 | dest = FPDF_GetNamedDestByName(document(), "WrongType"); |
| 271 | EXPECT_EQ(nullptr, dest); |
| 272 | |
| 273 | // No such destination in either Dest NameTree or dictionary. |
| 274 | dest = FPDF_GetNamedDestByName(document(), "Bogus"); |
| 275 | EXPECT_EQ(nullptr, dest); |
| 276 | } |
Tom Sepez | e80685c | 2015-01-26 16:59:09 -0800 | [diff] [blame] | 277 | |
| 278 | // The following tests pass if the document opens without crashing. |
Tom Sepez | 4dcf74d | 2015-02-03 16:24:43 -0800 | [diff] [blame] | 279 | TEST_F(FPDFViewEmbeddertest, Crasher_113) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 280 | EXPECT_TRUE(OpenDocument("bug_113.pdf")); |
Tom Sepez | 4dcf74d | 2015-02-03 16:24:43 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | TEST_F(FPDFViewEmbeddertest, Crasher_451830) { |
Dan Sinclair | 6be2aab | 2015-10-28 13:58:49 -0400 | [diff] [blame] | 284 | // Document is damaged and can't be opened. |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 285 | EXPECT_FALSE(OpenDocument("bug_451830.pdf")); |
Tom Sepez | e80685c | 2015-01-26 16:59:09 -0800 | [diff] [blame] | 286 | } |
Tom Sepez | 3795a74 | 2015-02-02 11:22:42 -0800 | [diff] [blame] | 287 | |
Tom Sepez | 4dcf74d | 2015-02-03 16:24:43 -0800 | [diff] [blame] | 288 | TEST_F(FPDFViewEmbeddertest, Crasher_452455) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 289 | EXPECT_TRUE(OpenDocument("bug_452455.pdf")); |
Tom Sepez | 3795a74 | 2015-02-02 11:22:42 -0800 | [diff] [blame] | 290 | FPDF_PAGE page = LoadPage(0); |
| 291 | EXPECT_NE(nullptr, page); |
Lei Zhang | d27acae | 2015-05-15 15:36:02 -0700 | [diff] [blame] | 292 | UnloadPage(page); |
Tom Sepez | 3795a74 | 2015-02-02 11:22:42 -0800 | [diff] [blame] | 293 | } |
Tom Sepez | 93daa3c | 2015-02-05 10:51:54 -0800 | [diff] [blame] | 294 | |
| 295 | TEST_F(FPDFViewEmbeddertest, Crasher_454695) { |
Lei Zhang | ab5537d | 2016-01-06 14:58:14 -0800 | [diff] [blame] | 296 | // Document is damaged and can't be opened. |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 297 | EXPECT_FALSE(OpenDocument("bug_454695.pdf")); |
Tom Sepez | 93daa3c | 2015-02-05 10:51:54 -0800 | [diff] [blame] | 298 | } |
Lei Zhang | 5fae972 | 2015-12-09 23:56:51 -0800 | [diff] [blame] | 299 | |
Lei Zhang | ab5537d | 2016-01-06 14:58:14 -0800 | [diff] [blame] | 300 | TEST_F(FPDFViewEmbeddertest, Crasher_572871) { |
| 301 | EXPECT_TRUE(OpenDocument("bug_572871.pdf")); |
| 302 | } |
| 303 | |
Wei Li | 07fb4cf | 2016-01-14 15:15:14 -0800 | [diff] [blame] | 304 | // It tests that document can still be loaded even the trailer has no 'Size' |
| 305 | // field if other information is right. |
| 306 | TEST_F(FPDFViewEmbeddertest, Failed_213) { |
| 307 | EXPECT_TRUE(OpenDocument("bug_213.pdf")); |
| 308 | } |
| 309 | |
Lei Zhang | 5fae972 | 2015-12-09 23:56:51 -0800 | [diff] [blame] | 310 | // The following tests pass if the document opens without infinite looping. |
| 311 | TEST_F(FPDFViewEmbeddertest, Hang_298) { |
| 312 | EXPECT_FALSE(OpenDocument("bug_298.pdf")); |
| 313 | } |
Wei Li | c009e8e | 2016-01-05 12:51:32 -0800 | [diff] [blame] | 314 | |
| 315 | // Test if the document opens without infinite looping. |
| 316 | // Previously this test will hang in a loop inside LoadAllCrossRefV4. After |
| 317 | // the fix, LoadAllCrossRefV4 will return false after detecting a cross |
| 318 | // reference loop. Cross references will be rebuilt successfully. |
| 319 | TEST_F(FPDFViewEmbeddertest, CrossRefV4Loop) { |
| 320 | EXPECT_TRUE(OpenDocument("bug_xrefv4_loop.pdf")); |
Lei Zhang | 671f0d4 | 2017-08-31 11:00:54 -0700 | [diff] [blame] | 321 | |
| 322 | // Make sure calling FPDFAvail_IsDocAvail() on this file does not infinite |
| 323 | // loop either. See bug 875. |
| 324 | int ret = PDF_DATA_NOTAVAIL; |
| 325 | while (ret == PDF_DATA_NOTAVAIL) |
| 326 | ret = FPDFAvail_IsDocAvail(avail_, &hints_); |
| 327 | EXPECT_EQ(PDF_DATA_AVAIL, ret); |
Wei Li | c009e8e | 2016-01-05 12:51:32 -0800 | [diff] [blame] | 328 | } |
Wei Li | 8e3f893 | 2016-01-08 14:36:59 -0800 | [diff] [blame] | 329 | |
| 330 | // The test should pass when circular references to ParseIndirectObject will not |
| 331 | // cause infinite loop. |
| 332 | TEST_F(FPDFViewEmbeddertest, Hang_343) { |
| 333 | EXPECT_FALSE(OpenDocument("bug_343.pdf")); |
Wei Li | f2b300a | 2016-01-11 14:16:10 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | // The test should pass when the absence of 'Contents' field in a signature |
| 337 | // dictionary will not cause an infinite loop in CPDF_SyntaxParser::GetObject(). |
| 338 | TEST_F(FPDFViewEmbeddertest, Hang_344) { |
| 339 | EXPECT_FALSE(OpenDocument("bug_344.pdf")); |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Wei Li | 6bc997a | 2016-01-19 12:35:03 -0800 | [diff] [blame] | 342 | // The test should pass when there is no infinite recursion in |
| 343 | // CPDF_SyntaxParser::GetString(). |
| 344 | TEST_F(FPDFViewEmbeddertest, Hang_355) { |
| 345 | EXPECT_FALSE(OpenDocument("bug_355.pdf")); |
| 346 | } |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 347 | // The test should pass even when the file has circular references to pages. |
| 348 | TEST_F(FPDFViewEmbeddertest, Hang_360) { |
| 349 | EXPECT_FALSE(OpenDocument("bug_360.pdf")); |
Wei Li | 07fb4cf | 2016-01-14 15:15:14 -0800 | [diff] [blame] | 350 | } |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 351 | |
| 352 | TEST_F(FPDFViewEmbeddertest, FPDF_RenderPageBitmapWithMatrix) { |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 353 | const char kOriginalMD5[] = "210157942bcce97b057a1107a1fd62f8"; |
| 354 | const char kTopLeftQuarterMD5[] = "c54d58dda13e3cd04eb63e1d0db0feda"; |
| 355 | const char kTrimmedMD5[] = "88225d7951a21d0eef191cfed06c36ce"; |
| 356 | const char kRotatedMD5[] = "7d38bc58aa50ad271bc432e77256d3de"; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 357 | |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 358 | EXPECT_TRUE(OpenDocument("rectangles.pdf")); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 359 | FPDF_PAGE page = LoadPage(0); |
| 360 | EXPECT_NE(nullptr, page); |
| 361 | const int width = static_cast<int>(FPDF_GetPageWidth(page)); |
| 362 | const int height = static_cast<int>(FPDF_GetPageHeight(page)); |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 363 | EXPECT_EQ(200, width); |
| 364 | EXPECT_EQ(200, height); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 365 | |
| 366 | FPDF_BITMAP bitmap = RenderPage(page); |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 367 | CompareBitmap(bitmap, width, height, kOriginalMD5); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 368 | FPDFBitmap_Destroy(bitmap); |
| 369 | |
| 370 | // Try rendering with an identity matrix. The output should be the same as |
| 371 | // the RenderPage() output. |
| 372 | FS_MATRIX matrix; |
| 373 | matrix.a = 1; |
| 374 | matrix.b = 0; |
| 375 | matrix.c = 0; |
| 376 | matrix.d = 1; |
| 377 | matrix.e = 0; |
| 378 | matrix.f = 0; |
| 379 | |
| 380 | FS_RECTF rect; |
| 381 | rect.left = 0; |
| 382 | rect.top = 0; |
| 383 | rect.right = width; |
| 384 | rect.bottom = height; |
| 385 | |
| 386 | bitmap = FPDFBitmap_Create(width, height, 0); |
| 387 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 388 | FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0); |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 389 | CompareBitmap(bitmap, width, height, kOriginalMD5); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 390 | FPDFBitmap_Destroy(bitmap); |
| 391 | |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 392 | // Now render again with the image scaled smaller. |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 393 | matrix.a = 0.5; |
| 394 | matrix.d = 0.5; |
| 395 | |
| 396 | bitmap = FPDFBitmap_Create(width, height, 0); |
| 397 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 398 | FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0); |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 399 | CompareBitmap(bitmap, width, height, kTopLeftQuarterMD5); |
| 400 | FPDFBitmap_Destroy(bitmap); |
| 401 | |
| 402 | // Now render again with the image scaled larger horizontally (will be |
| 403 | // trimmed). |
| 404 | matrix.a = 2; |
| 405 | matrix.d = 1; |
| 406 | bitmap = FPDFBitmap_Create(width, height, 0); |
| 407 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 408 | FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0); |
| 409 | CompareBitmap(bitmap, width, height, kTrimmedMD5); |
| 410 | FPDFBitmap_Destroy(bitmap); |
| 411 | |
| 412 | // Now try a 90 degree rotation |
| 413 | matrix.a = 0; |
| 414 | matrix.b = 1; |
| 415 | matrix.c = -1; |
| 416 | matrix.d = 0; |
| 417 | matrix.e = width; |
| 418 | bitmap = FPDFBitmap_Create(width, height, 0); |
| 419 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 420 | FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0); |
| 421 | CompareBitmap(bitmap, width, height, kRotatedMD5); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 422 | FPDFBitmap_Destroy(bitmap); |
| 423 | |
| 424 | UnloadPage(page); |
| 425 | } |
Dan Sinclair | cb377be | 2017-05-11 16:05:32 -0400 | [diff] [blame] | 426 | |
| 427 | class UnSupRecordDelegate : public EmbedderTest::Delegate { |
| 428 | public: |
| 429 | UnSupRecordDelegate() : type_(-1) {} |
| 430 | ~UnSupRecordDelegate() override {} |
| 431 | |
| 432 | void UnsupportedHandler(int type) override { type_ = type; } |
| 433 | |
| 434 | int type_; |
| 435 | }; |
| 436 | |
| 437 | TEST_F(FPDFViewEmbeddertest, UnSupportedOperations_NotFound) { |
| 438 | UnSupRecordDelegate delegate; |
| 439 | SetDelegate(&delegate); |
| 440 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| 441 | EXPECT_EQ(delegate.type_, -1); |
| 442 | SetDelegate(nullptr); |
| 443 | } |
| 444 | |
| 445 | TEST_F(FPDFViewEmbeddertest, UnSupportedOperations_LoadCustomDocument) { |
| 446 | UnSupRecordDelegate delegate; |
| 447 | SetDelegate(&delegate); |
| 448 | ASSERT_TRUE(OpenDocument("unsupported_feature.pdf")); |
| 449 | EXPECT_EQ(FPDF_UNSP_DOC_PORTABLECOLLECTION, delegate.type_); |
| 450 | SetDelegate(nullptr); |
| 451 | } |
| 452 | |
| 453 | TEST_F(FPDFViewEmbeddertest, UnSupportedOperations_LoadDocument) { |
| 454 | std::string file_path; |
| 455 | ASSERT_TRUE( |
| 456 | PathService::GetTestFilePath("unsupported_feature.pdf", &file_path)); |
| 457 | |
| 458 | UnSupRecordDelegate delegate; |
| 459 | SetDelegate(&delegate); |
| 460 | FPDF_DOCUMENT doc = FPDF_LoadDocument(file_path.c_str(), ""); |
| 461 | EXPECT_TRUE(doc != nullptr); |
| 462 | EXPECT_EQ(FPDF_UNSP_DOC_PORTABLECOLLECTION, delegate.type_); |
| 463 | FPDF_CloseDocument(doc); |
| 464 | SetDelegate(nullptr); |
| 465 | } |