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 | |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 8 | #include "../../testing/embedder_test.h" |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame^] | 9 | #include "../../fpdfsdk/include/fpdfview.h" |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | class FPDFViewEmbeddertest : public EmbedderTest { |
| 13 | }; |
| 14 | |
| 15 | TEST_F(FPDFViewEmbeddertest, Document) { |
| 16 | EXPECT_TRUE(OpenDocument("testing/resources/about_blank.pdf")); |
| 17 | EXPECT_EQ(1, GetPageCount()); |
| 18 | EXPECT_EQ(0, GetFirstPageNum()); |
| 19 | |
| 20 | int version; |
| 21 | EXPECT_TRUE(FPDF_GetFileVersion(document(), &version)); |
| 22 | EXPECT_EQ(14, version); |
| 23 | |
| 24 | EXPECT_EQ(0xFFFFFFFF, FPDF_GetDocPermissions(document())); |
| 25 | EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document())); |
| 26 | } |
| 27 | |
| 28 | TEST_F(FPDFViewEmbeddertest, Page) { |
| 29 | EXPECT_TRUE(OpenDocument("testing/resources/about_blank.pdf")); |
| 30 | FPDF_FORMHANDLE form_handle = SetFormFillEnvironment(); |
| 31 | FPDF_PAGE page = LoadPage(0, form_handle); |
| 32 | EXPECT_NE(nullptr, page); |
| 33 | EXPECT_EQ(612.0, FPDF_GetPageWidth(page)); |
| 34 | EXPECT_EQ(792.0, FPDF_GetPageHeight(page)); |
| 35 | UnloadPage(page, form_handle); |
| 36 | EXPECT_EQ(nullptr, LoadPage(1, form_handle)); |
| 37 | ClearFormFillEnvironment(form_handle); |
| 38 | } |
| 39 | |
| 40 | TEST_F(FPDFViewEmbeddertest, ViewerRef) { |
| 41 | EXPECT_TRUE(OpenDocument("testing/resources/about_blank.pdf")); |
| 42 | EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document())); |
| 43 | EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document())); |
| 44 | EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document())); |
| 45 | } |
| 46 | |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame^] | 47 | TEST_F(FPDFViewEmbeddertest, NamedDests) { |
| 48 | EXPECT_TRUE(OpenDocument("testing/resources/named_dests.pdf")); |
| 49 | long buffer_size; |
| 50 | char fixed_buffer[512]; |
| 51 | FPDF_DEST dest; |
| 52 | |
| 53 | // Query the size of the first item. |
| 54 | buffer_size = 2000000; // Absurdly large, check not used for this case. |
| 55 | dest = FPDF_GetNamedDest(document(), 0, nullptr, buffer_size); |
| 56 | EXPECT_NE(nullptr, dest); |
| 57 | EXPECT_EQ(12u, buffer_size); |
| 58 | |
| 59 | // Try to retrieve the first item with too small a buffer. |
| 60 | buffer_size = 10; |
| 61 | dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, buffer_size); |
| 62 | EXPECT_NE(nullptr, dest); |
| 63 | EXPECT_EQ(-1, buffer_size); |
| 64 | |
| 65 | // Try to retrieve the first item with correctly sized buffer. Item is |
| 66 | // taken from Dests NameTree in named_dests.pdf. |
| 67 | buffer_size = 12; |
| 68 | dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, buffer_size); |
| 69 | EXPECT_NE(nullptr, dest); |
| 70 | EXPECT_EQ(12u, buffer_size); |
| 71 | EXPECT_EQ(std::string("F\0i\0r\0s\0t\0\0\0", 12), |
| 72 | std::string(fixed_buffer, buffer_size)); |
| 73 | |
| 74 | // Try to retrieve the second item with ample buffer. Item is taken |
| 75 | // from Dests NameTree but has a sub-dictionary in named_dests.pdf. |
| 76 | buffer_size = sizeof(fixed_buffer); |
| 77 | dest = FPDF_GetNamedDest(document(), 1, fixed_buffer, buffer_size); |
| 78 | EXPECT_NE(nullptr, dest); |
| 79 | EXPECT_EQ(10u, buffer_size); |
| 80 | EXPECT_EQ(std::string("N\0e\0x\0t\0\0\0", 10), |
| 81 | std::string(fixed_buffer, buffer_size)); |
| 82 | |
| 83 | // Try to retrieve third item with ample buffer. Item is taken |
| 84 | // from Dests NameTree but has a bad sub-dictionary in named_dests.pdf. |
| 85 | // in named_dests.pdf). |
| 86 | buffer_size = sizeof(fixed_buffer); |
| 87 | dest = FPDF_GetNamedDest(document(), 2, fixed_buffer, buffer_size); |
| 88 | EXPECT_EQ(nullptr, dest); |
| 89 | EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. |
| 90 | |
| 91 | // Try to retrieve the forth item with ample buffer. Item is taken |
| 92 | // from Dests NameTree but has a vale of the wrong type in named_dests.pdf. |
| 93 | buffer_size = sizeof(fixed_buffer); |
| 94 | dest = FPDF_GetNamedDest(document(), 3, fixed_buffer, buffer_size); |
| 95 | EXPECT_EQ(nullptr, dest); |
| 96 | EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. |
| 97 | |
| 98 | // Try to retrieve fifth item with ample buffer. Item taken from the |
| 99 | // old-style Dests dictionary object in named_dests.pdf. |
| 100 | buffer_size = sizeof(fixed_buffer); |
| 101 | dest = FPDF_GetNamedDest(document(), 4, fixed_buffer, buffer_size); |
| 102 | EXPECT_NE(nullptr, dest); |
| 103 | EXPECT_EQ(30u, buffer_size); |
| 104 | EXPECT_EQ( |
| 105 | std::string("F\0i\0r\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 30), |
| 106 | std::string(fixed_buffer, buffer_size)); |
| 107 | |
| 108 | // Try to retrieve sixth item with ample buffer. Item istaken from the |
| 109 | // old-style Dests dictionary object but has a sub-dictionary in |
| 110 | // named_dests.pdf. |
| 111 | buffer_size = sizeof(fixed_buffer); |
| 112 | dest = FPDF_GetNamedDest(document(), 5, fixed_buffer, buffer_size); |
| 113 | EXPECT_NE(nullptr, dest); |
| 114 | EXPECT_EQ(28u, buffer_size); |
| 115 | EXPECT_EQ( |
| 116 | std::string("L\0a\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 28), |
| 117 | std::string(fixed_buffer, buffer_size)); |
| 118 | |
| 119 | // Try to retrieve non-existent item with ample buffer. |
| 120 | buffer_size = sizeof(fixed_buffer); |
| 121 | dest = FPDF_GetNamedDest(document(), 6, fixed_buffer, buffer_size); |
| 122 | EXPECT_EQ(nullptr, dest); |
| 123 | EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. |
| 124 | |
| 125 | // Try to underflow/overflow the integer index. |
| 126 | buffer_size = sizeof(fixed_buffer); |
| 127 | dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::max(), |
| 128 | fixed_buffer, buffer_size); |
| 129 | EXPECT_EQ(nullptr, dest); |
| 130 | EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. |
| 131 | |
| 132 | buffer_size = sizeof(fixed_buffer); |
| 133 | dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::min(), |
| 134 | fixed_buffer, buffer_size); |
| 135 | EXPECT_EQ(nullptr, dest); |
| 136 | EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. |
| 137 | |
| 138 | buffer_size = sizeof(fixed_buffer); |
| 139 | dest = FPDF_GetNamedDest(document(), -1, fixed_buffer, buffer_size); |
| 140 | EXPECT_EQ(nullptr, dest); |
| 141 | EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. |
| 142 | } |
| 143 | |
| 144 | TEST_F(FPDFViewEmbeddertest, NamedDestsByName) { |
| 145 | EXPECT_TRUE(OpenDocument("testing/resources/named_dests.pdf")); |
| 146 | |
| 147 | // Null pointer returns NULL. |
| 148 | FPDF_DEST dest = FPDF_GetNamedDestByName(document(), nullptr); |
| 149 | EXPECT_EQ(nullptr, dest); |
| 150 | |
| 151 | // Empty string returns NULL. |
| 152 | dest = FPDF_GetNamedDestByName(document(), ""); |
| 153 | EXPECT_EQ(nullptr, dest); |
| 154 | |
| 155 | // Item from Dests NameTree. |
| 156 | dest = FPDF_GetNamedDestByName(document(), "First"); |
| 157 | EXPECT_NE(nullptr, dest); |
| 158 | |
| 159 | long ignore_len = 0; |
| 160 | FPDF_DEST dest_by_index = |
| 161 | FPDF_GetNamedDest(document(), 0, nullptr, ignore_len); |
| 162 | EXPECT_EQ(dest_by_index, dest); |
| 163 | |
| 164 | // Item from Dests dictionary. |
| 165 | dest = FPDF_GetNamedDestByName(document(), "FirstAlternate"); |
| 166 | EXPECT_NE(nullptr, dest); |
| 167 | |
| 168 | ignore_len = 0; |
| 169 | dest_by_index = FPDF_GetNamedDest(document(), 4, nullptr, ignore_len); |
| 170 | EXPECT_EQ(dest_by_index, dest); |
| 171 | |
| 172 | // Bad value type for item from Dests NameTree array. |
| 173 | dest = FPDF_GetNamedDestByName(document(), "WrongType"); |
| 174 | EXPECT_EQ(nullptr, dest); |
| 175 | |
| 176 | // No such destination in either Dest NameTree or dictionary. |
| 177 | dest = FPDF_GetNamedDestByName(document(), "Bogus"); |
| 178 | EXPECT_EQ(nullptr, dest); |
| 179 | } |