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" |
| 12 | |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 13 | TEST(fpdf, CApiTest) { |
| 14 | EXPECT_TRUE(CheckPDFiumCApi()); |
| 15 | } |
| 16 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 17 | class FPDFViewEmbeddertest : public EmbedderTest {}; |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 18 | |
| 19 | TEST_F(FPDFViewEmbeddertest, Document) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 20 | EXPECT_TRUE(OpenDocument("about_blank.pdf")); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 21 | EXPECT_EQ(1, GetPageCount()); |
| 22 | EXPECT_EQ(0, GetFirstPageNum()); |
| 23 | |
| 24 | int version; |
| 25 | EXPECT_TRUE(FPDF_GetFileVersion(document(), &version)); |
| 26 | EXPECT_EQ(14, version); |
| 27 | |
| 28 | EXPECT_EQ(0xFFFFFFFF, FPDF_GetDocPermissions(document())); |
| 29 | EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document())); |
| 30 | } |
| 31 | |
thestig | b8db511 | 2016-04-06 12:12:52 -0700 | [diff] [blame] | 32 | // See bug 465. |
| 33 | TEST_F(FPDFViewEmbeddertest, EmptyDocument) { |
| 34 | EXPECT_TRUE(CreateEmptyDocument()); |
| 35 | |
| 36 | { |
| 37 | int version = 42; |
| 38 | EXPECT_FALSE(FPDF_GetFileVersion(document(), &version)); |
| 39 | EXPECT_EQ(0, version); |
| 40 | } |
| 41 | |
| 42 | { |
| 43 | #ifndef PDF_ENABLE_XFA |
| 44 | const unsigned long kExpected = 0; |
| 45 | #else |
| 46 | const unsigned long kExpected = static_cast<uint32_t>(-1); |
| 47 | #endif |
| 48 | EXPECT_EQ(kExpected, FPDF_GetDocPermissions(document())); |
| 49 | } |
| 50 | |
| 51 | EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document())); |
| 52 | |
| 53 | EXPECT_EQ(0, FPDF_GetPageCount(document())); |
| 54 | |
| 55 | EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document())); |
| 56 | EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document())); |
| 57 | EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document())); |
| 58 | |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 59 | char buf[100]; |
| 60 | EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0)); |
| 61 | EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf))); |
| 62 | |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 63 | EXPECT_EQ(0u, FPDF_CountNamedDests(document())); |
thestig | b8db511 | 2016-04-06 12:12:52 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 66 | TEST_F(FPDFViewEmbeddertest, Page) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 67 | EXPECT_TRUE(OpenDocument("about_blank.pdf")); |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 68 | FPDF_PAGE page = LoadPage(0); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 69 | EXPECT_NE(nullptr, page); |
| 70 | EXPECT_EQ(612.0, FPDF_GetPageWidth(page)); |
| 71 | EXPECT_EQ(792.0, FPDF_GetPageHeight(page)); |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 72 | UnloadPage(page); |
| 73 | EXPECT_EQ(nullptr, LoadPage(1)); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 74 | } |
| 75 | |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 76 | TEST_F(FPDFViewEmbeddertest, ViewerRefDummy) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 77 | EXPECT_TRUE(OpenDocument("about_blank.pdf")); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 78 | EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document())); |
| 79 | EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document())); |
| 80 | EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document())); |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 81 | |
| 82 | char buf[100]; |
| 83 | EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0)); |
| 84 | EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf))); |
| 85 | } |
| 86 | |
| 87 | TEST_F(FPDFViewEmbeddertest, ViewerRef) { |
| 88 | EXPECT_TRUE(OpenDocument("viewer_ref.pdf")); |
| 89 | EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document())); |
| 90 | EXPECT_EQ(5, FPDF_VIEWERREF_GetNumCopies(document())); |
| 91 | EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document())); |
| 92 | |
| 93 | // Test some corner cases. |
| 94 | char buf[100]; |
| 95 | EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "", buf, sizeof(buf))); |
| 96 | EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0)); |
| 97 | EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf))); |
| 98 | |
| 99 | // 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] | 100 | // NOLINTNEXTLINE(runtime/printf) |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 101 | strcpy(buf, "ABCD"); |
| 102 | EXPECT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, 1)); |
| 103 | EXPECT_STREQ("ABCD", buf); |
| 104 | |
| 105 | // Note "Foo" is a different key from "foo". |
| 106 | EXPECT_EQ(4U, |
| 107 | FPDF_VIEWERREF_GetName(document(), "Foo", nullptr, sizeof(buf))); |
| 108 | ASSERT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, sizeof(buf))); |
| 109 | EXPECT_STREQ("foo", buf); |
| 110 | |
| 111 | // Try to retrieve a boolean and an integer. |
| 112 | EXPECT_EQ( |
| 113 | 0U, FPDF_VIEWERREF_GetName(document(), "HideToolbar", buf, sizeof(buf))); |
| 114 | EXPECT_EQ(0U, |
| 115 | FPDF_VIEWERREF_GetName(document(), "NumCopies", buf, sizeof(buf))); |
| 116 | |
| 117 | // Try more valid cases. |
| 118 | ASSERT_EQ(4U, |
| 119 | FPDF_VIEWERREF_GetName(document(), "Direction", buf, sizeof(buf))); |
| 120 | EXPECT_STREQ("R2L", buf); |
| 121 | ASSERT_EQ(8U, |
| 122 | FPDF_VIEWERREF_GetName(document(), "ViewArea", buf, sizeof(buf))); |
| 123 | EXPECT_STREQ("CropBox", buf); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 126 | TEST_F(FPDFViewEmbeddertest, NamedDests) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 127 | EXPECT_TRUE(OpenDocument("named_dests.pdf")); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 128 | long buffer_size; |
| 129 | char fixed_buffer[512]; |
| 130 | FPDF_DEST dest; |
| 131 | |
| 132 | // Query the size of the first item. |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 133 | buffer_size = 2000000; // Absurdly large, check not used for this case. |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 134 | dest = FPDF_GetNamedDest(document(), 0, nullptr, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 135 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 136 | EXPECT_EQ(12, buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 137 | |
| 138 | // Try to retrieve the first item with too small a buffer. |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 139 | buffer_size = 10; |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 140 | dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 141 | EXPECT_NE(nullptr, dest); |
| 142 | EXPECT_EQ(-1, buffer_size); |
| 143 | |
| 144 | // Try to retrieve the first item with correctly sized buffer. Item is |
| 145 | // taken from Dests NameTree in named_dests.pdf. |
| 146 | buffer_size = 12; |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 147 | dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 148 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 149 | EXPECT_EQ(12, buffer_size); |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 150 | 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] | 151 | std::string(fixed_buffer, buffer_size)); |
| 152 | |
| 153 | // Try to retrieve the second item with ample buffer. Item is taken |
| 154 | // from Dests NameTree but has a sub-dictionary in named_dests.pdf. |
| 155 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 156 | dest = FPDF_GetNamedDest(document(), 1, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 157 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 158 | EXPECT_EQ(10, buffer_size); |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 159 | EXPECT_EQ(std::string("N\0e\0x\0t\0\0\0", 10), |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 160 | std::string(fixed_buffer, buffer_size)); |
| 161 | |
| 162 | // Try to retrieve third item with ample buffer. Item is taken |
| 163 | // from Dests NameTree but has a bad sub-dictionary in named_dests.pdf. |
| 164 | // in named_dests.pdf). |
| 165 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 166 | dest = FPDF_GetNamedDest(document(), 2, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 167 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 168 | EXPECT_EQ(sizeof(fixed_buffer), |
| 169 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 170 | |
| 171 | // Try to retrieve the forth item with ample buffer. Item is taken |
| 172 | // from Dests NameTree but has a vale of the wrong type in named_dests.pdf. |
| 173 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 174 | dest = FPDF_GetNamedDest(document(), 3, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 175 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 176 | EXPECT_EQ(sizeof(fixed_buffer), |
| 177 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 178 | |
| 179 | // Try to retrieve fifth item with ample buffer. Item taken from the |
| 180 | // old-style Dests dictionary object in named_dests.pdf. |
| 181 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 182 | dest = FPDF_GetNamedDest(document(), 4, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 183 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 184 | EXPECT_EQ(30, buffer_size); |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 185 | 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] | 186 | std::string(fixed_buffer, buffer_size)); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 187 | |
| 188 | // Try to retrieve sixth item with ample buffer. Item istaken from the |
| 189 | // old-style Dests dictionary object but has a sub-dictionary in |
| 190 | // named_dests.pdf. |
| 191 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 192 | dest = FPDF_GetNamedDest(document(), 5, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 193 | EXPECT_NE(nullptr, dest); |
weili | 3cc01f2 | 2016-05-16 13:53:42 -0700 | [diff] [blame] | 194 | EXPECT_EQ(28, buffer_size); |
Oliver Chang | 35e68a5 | 2015-12-09 12:44:33 -0800 | [diff] [blame] | 195 | 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] | 196 | std::string(fixed_buffer, buffer_size)); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 197 | |
| 198 | // Try to retrieve non-existent item with ample buffer. |
| 199 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 200 | dest = FPDF_GetNamedDest(document(), 6, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 201 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 202 | EXPECT_EQ(sizeof(fixed_buffer), |
| 203 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 204 | |
| 205 | // Try to underflow/overflow the integer index. |
| 206 | buffer_size = sizeof(fixed_buffer); |
| 207 | dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::max(), |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 208 | fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 209 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 210 | EXPECT_EQ(sizeof(fixed_buffer), |
| 211 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 212 | |
| 213 | buffer_size = sizeof(fixed_buffer); |
| 214 | dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::min(), |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 215 | 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 | buffer_size = sizeof(fixed_buffer); |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 221 | dest = FPDF_GetNamedDest(document(), -1, fixed_buffer, &buffer_size); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 222 | EXPECT_EQ(nullptr, dest); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 223 | EXPECT_EQ(sizeof(fixed_buffer), |
| 224 | static_cast<size_t>(buffer_size)); // unmodified. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | TEST_F(FPDFViewEmbeddertest, NamedDestsByName) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 228 | EXPECT_TRUE(OpenDocument("named_dests.pdf")); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 229 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 230 | // Null pointer returns nullptr. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 231 | FPDF_DEST dest = FPDF_GetNamedDestByName(document(), nullptr); |
| 232 | EXPECT_EQ(nullptr, dest); |
| 233 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 234 | // Empty string returns nullptr. |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 235 | dest = FPDF_GetNamedDestByName(document(), ""); |
| 236 | EXPECT_EQ(nullptr, dest); |
| 237 | |
| 238 | // Item from Dests NameTree. |
| 239 | dest = FPDF_GetNamedDestByName(document(), "First"); |
| 240 | EXPECT_NE(nullptr, dest); |
| 241 | |
| 242 | long ignore_len = 0; |
| 243 | FPDF_DEST dest_by_index = |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 244 | FPDF_GetNamedDest(document(), 0, nullptr, &ignore_len); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 245 | EXPECT_EQ(dest_by_index, dest); |
| 246 | |
| 247 | // Item from Dests dictionary. |
| 248 | dest = FPDF_GetNamedDestByName(document(), "FirstAlternate"); |
| 249 | EXPECT_NE(nullptr, dest); |
| 250 | |
| 251 | ignore_len = 0; |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 252 | dest_by_index = FPDF_GetNamedDest(document(), 4, nullptr, &ignore_len); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 253 | EXPECT_EQ(dest_by_index, dest); |
| 254 | |
| 255 | // Bad value type for item from Dests NameTree array. |
| 256 | dest = FPDF_GetNamedDestByName(document(), "WrongType"); |
| 257 | EXPECT_EQ(nullptr, dest); |
| 258 | |
| 259 | // No such destination in either Dest NameTree or dictionary. |
| 260 | dest = FPDF_GetNamedDestByName(document(), "Bogus"); |
| 261 | EXPECT_EQ(nullptr, dest); |
| 262 | } |
Tom Sepez | e80685c | 2015-01-26 16:59:09 -0800 | [diff] [blame] | 263 | |
| 264 | // The following tests pass if the document opens without crashing. |
Tom Sepez | 4dcf74d | 2015-02-03 16:24:43 -0800 | [diff] [blame] | 265 | TEST_F(FPDFViewEmbeddertest, Crasher_113) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 266 | EXPECT_TRUE(OpenDocument("bug_113.pdf")); |
Tom Sepez | 4dcf74d | 2015-02-03 16:24:43 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | TEST_F(FPDFViewEmbeddertest, Crasher_451830) { |
Dan Sinclair | 6be2aab | 2015-10-28 13:58:49 -0400 | [diff] [blame] | 270 | // Document is damaged and can't be opened. |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 271 | EXPECT_FALSE(OpenDocument("bug_451830.pdf")); |
Tom Sepez | e80685c | 2015-01-26 16:59:09 -0800 | [diff] [blame] | 272 | } |
Tom Sepez | 3795a74 | 2015-02-02 11:22:42 -0800 | [diff] [blame] | 273 | |
Tom Sepez | 4dcf74d | 2015-02-03 16:24:43 -0800 | [diff] [blame] | 274 | TEST_F(FPDFViewEmbeddertest, Crasher_452455) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 275 | EXPECT_TRUE(OpenDocument("bug_452455.pdf")); |
Tom Sepez | 3795a74 | 2015-02-02 11:22:42 -0800 | [diff] [blame] | 276 | FPDF_PAGE page = LoadPage(0); |
| 277 | EXPECT_NE(nullptr, page); |
Lei Zhang | d27acae | 2015-05-15 15:36:02 -0700 | [diff] [blame] | 278 | UnloadPage(page); |
Tom Sepez | 3795a74 | 2015-02-02 11:22:42 -0800 | [diff] [blame] | 279 | } |
Tom Sepez | 93daa3c | 2015-02-05 10:51:54 -0800 | [diff] [blame] | 280 | |
| 281 | TEST_F(FPDFViewEmbeddertest, Crasher_454695) { |
Lei Zhang | ab5537d | 2016-01-06 14:58:14 -0800 | [diff] [blame] | 282 | // Document is damaged and can't be opened. |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 283 | EXPECT_FALSE(OpenDocument("bug_454695.pdf")); |
Tom Sepez | 93daa3c | 2015-02-05 10:51:54 -0800 | [diff] [blame] | 284 | } |
Lei Zhang | 5fae972 | 2015-12-09 23:56:51 -0800 | [diff] [blame] | 285 | |
Lei Zhang | ab5537d | 2016-01-06 14:58:14 -0800 | [diff] [blame] | 286 | TEST_F(FPDFViewEmbeddertest, Crasher_572871) { |
| 287 | EXPECT_TRUE(OpenDocument("bug_572871.pdf")); |
| 288 | } |
| 289 | |
Wei Li | 07fb4cf | 2016-01-14 15:15:14 -0800 | [diff] [blame] | 290 | // It tests that document can still be loaded even the trailer has no 'Size' |
| 291 | // field if other information is right. |
| 292 | TEST_F(FPDFViewEmbeddertest, Failed_213) { |
| 293 | EXPECT_TRUE(OpenDocument("bug_213.pdf")); |
| 294 | } |
| 295 | |
Lei Zhang | 5fae972 | 2015-12-09 23:56:51 -0800 | [diff] [blame] | 296 | // The following tests pass if the document opens without infinite looping. |
| 297 | TEST_F(FPDFViewEmbeddertest, Hang_298) { |
| 298 | EXPECT_FALSE(OpenDocument("bug_298.pdf")); |
| 299 | } |
Wei Li | c009e8e | 2016-01-05 12:51:32 -0800 | [diff] [blame] | 300 | |
| 301 | // Test if the document opens without infinite looping. |
| 302 | // Previously this test will hang in a loop inside LoadAllCrossRefV4. After |
| 303 | // the fix, LoadAllCrossRefV4 will return false after detecting a cross |
| 304 | // reference loop. Cross references will be rebuilt successfully. |
| 305 | TEST_F(FPDFViewEmbeddertest, CrossRefV4Loop) { |
| 306 | EXPECT_TRUE(OpenDocument("bug_xrefv4_loop.pdf")); |
| 307 | } |
Wei Li | 8e3f893 | 2016-01-08 14:36:59 -0800 | [diff] [blame] | 308 | |
| 309 | // The test should pass when circular references to ParseIndirectObject will not |
| 310 | // cause infinite loop. |
| 311 | TEST_F(FPDFViewEmbeddertest, Hang_343) { |
| 312 | EXPECT_FALSE(OpenDocument("bug_343.pdf")); |
Wei Li | f2b300a | 2016-01-11 14:16:10 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | // The test should pass when the absence of 'Contents' field in a signature |
| 316 | // dictionary will not cause an infinite loop in CPDF_SyntaxParser::GetObject(). |
| 317 | TEST_F(FPDFViewEmbeddertest, Hang_344) { |
| 318 | EXPECT_FALSE(OpenDocument("bug_344.pdf")); |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Wei Li | 6bc997a | 2016-01-19 12:35:03 -0800 | [diff] [blame] | 321 | // The test should pass when there is no infinite recursion in |
| 322 | // CPDF_SyntaxParser::GetString(). |
| 323 | TEST_F(FPDFViewEmbeddertest, Hang_355) { |
| 324 | EXPECT_FALSE(OpenDocument("bug_355.pdf")); |
| 325 | } |
Wei Li | 215816b | 2016-01-14 12:29:02 -0800 | [diff] [blame] | 326 | // The test should pass even when the file has circular references to pages. |
| 327 | TEST_F(FPDFViewEmbeddertest, Hang_360) { |
| 328 | EXPECT_FALSE(OpenDocument("bug_360.pdf")); |
Wei Li | 07fb4cf | 2016-01-14 15:15:14 -0800 | [diff] [blame] | 329 | } |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame^] | 330 | |
| 331 | TEST_F(FPDFViewEmbeddertest, FPDF_RenderPageBitmapWithMatrix) { |
| 332 | const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615"; |
| 333 | const char kTopLeftQuarterBlackMd5sum[] = "24e4d1ec06fa0258af758cfc8b2ad50a"; |
| 334 | |
| 335 | EXPECT_TRUE(OpenDocument("black.pdf")); |
| 336 | FPDF_PAGE page = LoadPage(0); |
| 337 | EXPECT_NE(nullptr, page); |
| 338 | const int width = static_cast<int>(FPDF_GetPageWidth(page)); |
| 339 | const int height = static_cast<int>(FPDF_GetPageHeight(page)); |
| 340 | EXPECT_EQ(612, width); |
| 341 | EXPECT_EQ(792, height); |
| 342 | |
| 343 | FPDF_BITMAP bitmap = RenderPage(page); |
| 344 | CompareBitmap(bitmap, width, height, kAllBlackMd5sum); |
| 345 | FPDFBitmap_Destroy(bitmap); |
| 346 | |
| 347 | // Try rendering with an identity matrix. The output should be the same as |
| 348 | // the RenderPage() output. |
| 349 | FS_MATRIX matrix; |
| 350 | matrix.a = 1; |
| 351 | matrix.b = 0; |
| 352 | matrix.c = 0; |
| 353 | matrix.d = 1; |
| 354 | matrix.e = 0; |
| 355 | matrix.f = 0; |
| 356 | |
| 357 | FS_RECTF rect; |
| 358 | rect.left = 0; |
| 359 | rect.top = 0; |
| 360 | rect.right = width; |
| 361 | rect.bottom = height; |
| 362 | |
| 363 | bitmap = FPDFBitmap_Create(width, height, 0); |
| 364 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 365 | FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0); |
| 366 | CompareBitmap(bitmap, width, height, kAllBlackMd5sum); |
| 367 | FPDFBitmap_Destroy(bitmap); |
| 368 | |
| 369 | // Now render again with the image scaled. |
| 370 | matrix.a = 0.5; |
| 371 | matrix.d = 0.5; |
| 372 | |
| 373 | bitmap = FPDFBitmap_Create(width, height, 0); |
| 374 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 375 | FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0); |
| 376 | CompareBitmap(bitmap, width, height, kTopLeftQuarterBlackMd5sum); |
| 377 | FPDFBitmap_Destroy(bitmap); |
| 378 | |
| 379 | UnloadPage(page); |
| 380 | } |