OLD | NEW |
1 // Copyright 2015 PDFium Authors. All rights reserved. | 1 // Copyright 2015 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <limits> | 5 #include <limits> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "../../public/fpdfview.h" | 8 #include "../../public/fpdfview.h" |
9 #include "../../testing/embedder_test.h" | 9 #include "../../testing/embedder_test.h" |
| 10 #include "fpdfview_c_api_test.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
| 13 TEST(fpdf, CApiTest) { |
| 14 EXPECT_TRUE(CheckPDFiumCApi()); |
| 15 } |
| 16 |
12 class FPDFViewEmbeddertest : public EmbedderTest { | 17 class FPDFViewEmbeddertest : public EmbedderTest { |
13 }; | 18 }; |
14 | 19 |
15 TEST_F(FPDFViewEmbeddertest, Document) { | 20 TEST_F(FPDFViewEmbeddertest, Document) { |
16 EXPECT_TRUE(OpenDocument("testing/resources/about_blank.pdf")); | 21 EXPECT_TRUE(OpenDocument("testing/resources/about_blank.pdf")); |
17 EXPECT_EQ(1, GetPageCount()); | 22 EXPECT_EQ(1, GetPageCount()); |
18 EXPECT_EQ(0, GetFirstPageNum()); | 23 EXPECT_EQ(0, GetFirstPageNum()); |
19 | 24 |
20 int version; | 25 int version; |
21 EXPECT_TRUE(FPDF_GetFileVersion(document(), &version)); | 26 EXPECT_TRUE(FPDF_GetFileVersion(document(), &version)); |
(...skipping 21 matching lines...) Expand all Loading... |
43 } | 48 } |
44 | 49 |
45 TEST_F(FPDFViewEmbeddertest, NamedDests) { | 50 TEST_F(FPDFViewEmbeddertest, NamedDests) { |
46 EXPECT_TRUE(OpenDocument("testing/resources/named_dests.pdf")); | 51 EXPECT_TRUE(OpenDocument("testing/resources/named_dests.pdf")); |
47 long buffer_size; | 52 long buffer_size; |
48 char fixed_buffer[512]; | 53 char fixed_buffer[512]; |
49 FPDF_DEST dest; | 54 FPDF_DEST dest; |
50 | 55 |
51 // Query the size of the first item. | 56 // Query the size of the first item. |
52 buffer_size = 2000000; // Absurdly large, check not used for this case. | 57 buffer_size = 2000000; // Absurdly large, check not used for this case. |
53 dest = FPDF_GetNamedDest(document(), 0, nullptr, buffer_size); | 58 dest = FPDF_GetNamedDest(document(), 0, nullptr, &buffer_size); |
54 EXPECT_NE(nullptr, dest); | 59 EXPECT_NE(nullptr, dest); |
55 EXPECT_EQ(12u, buffer_size); | 60 EXPECT_EQ(12u, buffer_size); |
56 | 61 |
57 // Try to retrieve the first item with too small a buffer. | 62 // Try to retrieve the first item with too small a buffer. |
58 buffer_size = 10; | 63 buffer_size = 10; |
59 dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, buffer_size); | 64 dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size); |
60 EXPECT_NE(nullptr, dest); | 65 EXPECT_NE(nullptr, dest); |
61 EXPECT_EQ(-1, buffer_size); | 66 EXPECT_EQ(-1, buffer_size); |
62 | 67 |
63 // Try to retrieve the first item with correctly sized buffer. Item is | 68 // Try to retrieve the first item with correctly sized buffer. Item is |
64 // taken from Dests NameTree in named_dests.pdf. | 69 // taken from Dests NameTree in named_dests.pdf. |
65 buffer_size = 12; | 70 buffer_size = 12; |
66 dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, buffer_size); | 71 dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size); |
67 EXPECT_NE(nullptr, dest); | 72 EXPECT_NE(nullptr, dest); |
68 EXPECT_EQ(12u, buffer_size); | 73 EXPECT_EQ(12u, buffer_size); |
69 EXPECT_EQ(std::string("F\0i\0r\0s\0t\0\0\0", 12), | 74 EXPECT_EQ(std::string("F\0i\0r\0s\0t\0\0\0", 12), |
70 std::string(fixed_buffer, buffer_size)); | 75 std::string(fixed_buffer, buffer_size)); |
71 | 76 |
72 // Try to retrieve the second item with ample buffer. Item is taken | 77 // Try to retrieve the second item with ample buffer. Item is taken |
73 // from Dests NameTree but has a sub-dictionary in named_dests.pdf. | 78 // from Dests NameTree but has a sub-dictionary in named_dests.pdf. |
74 buffer_size = sizeof(fixed_buffer); | 79 buffer_size = sizeof(fixed_buffer); |
75 dest = FPDF_GetNamedDest(document(), 1, fixed_buffer, buffer_size); | 80 dest = FPDF_GetNamedDest(document(), 1, fixed_buffer, &buffer_size); |
76 EXPECT_NE(nullptr, dest); | 81 EXPECT_NE(nullptr, dest); |
77 EXPECT_EQ(10u, buffer_size); | 82 EXPECT_EQ(10u, buffer_size); |
78 EXPECT_EQ(std::string("N\0e\0x\0t\0\0\0", 10), | 83 EXPECT_EQ(std::string("N\0e\0x\0t\0\0\0", 10), |
79 std::string(fixed_buffer, buffer_size)); | 84 std::string(fixed_buffer, buffer_size)); |
80 | 85 |
81 // Try to retrieve third item with ample buffer. Item is taken | 86 // Try to retrieve third item with ample buffer. Item is taken |
82 // from Dests NameTree but has a bad sub-dictionary in named_dests.pdf. | 87 // from Dests NameTree but has a bad sub-dictionary in named_dests.pdf. |
83 // in named_dests.pdf). | 88 // in named_dests.pdf). |
84 buffer_size = sizeof(fixed_buffer); | 89 buffer_size = sizeof(fixed_buffer); |
85 dest = FPDF_GetNamedDest(document(), 2, fixed_buffer, buffer_size); | 90 dest = FPDF_GetNamedDest(document(), 2, fixed_buffer, &buffer_size); |
86 EXPECT_EQ(nullptr, dest); | 91 EXPECT_EQ(nullptr, dest); |
87 EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. | 92 EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. |
88 | 93 |
89 // Try to retrieve the forth item with ample buffer. Item is taken | 94 // Try to retrieve the forth item with ample buffer. Item is taken |
90 // from Dests NameTree but has a vale of the wrong type in named_dests.pdf. | 95 // from Dests NameTree but has a vale of the wrong type in named_dests.pdf. |
91 buffer_size = sizeof(fixed_buffer); | 96 buffer_size = sizeof(fixed_buffer); |
92 dest = FPDF_GetNamedDest(document(), 3, fixed_buffer, buffer_size); | 97 dest = FPDF_GetNamedDest(document(), 3, fixed_buffer, &buffer_size); |
93 EXPECT_EQ(nullptr, dest); | 98 EXPECT_EQ(nullptr, dest); |
94 EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. | 99 EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. |
95 | 100 |
96 // Try to retrieve fifth item with ample buffer. Item taken from the | 101 // Try to retrieve fifth item with ample buffer. Item taken from the |
97 // old-style Dests dictionary object in named_dests.pdf. | 102 // old-style Dests dictionary object in named_dests.pdf. |
98 buffer_size = sizeof(fixed_buffer); | 103 buffer_size = sizeof(fixed_buffer); |
99 dest = FPDF_GetNamedDest(document(), 4, fixed_buffer, buffer_size); | 104 dest = FPDF_GetNamedDest(document(), 4, fixed_buffer, &buffer_size); |
100 EXPECT_NE(nullptr, dest); | 105 EXPECT_NE(nullptr, dest); |
101 EXPECT_EQ(30u, buffer_size); | 106 EXPECT_EQ(30u, buffer_size); |
102 EXPECT_EQ( | 107 EXPECT_EQ( |
103 std::string("F\0i\0r\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 30), | 108 std::string("F\0i\0r\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 30), |
104 std::string(fixed_buffer, buffer_size)); | 109 std::string(fixed_buffer, buffer_size)); |
105 | 110 |
106 // Try to retrieve sixth item with ample buffer. Item istaken from the | 111 // Try to retrieve sixth item with ample buffer. Item istaken from the |
107 // old-style Dests dictionary object but has a sub-dictionary in | 112 // old-style Dests dictionary object but has a sub-dictionary in |
108 // named_dests.pdf. | 113 // named_dests.pdf. |
109 buffer_size = sizeof(fixed_buffer); | 114 buffer_size = sizeof(fixed_buffer); |
110 dest = FPDF_GetNamedDest(document(), 5, fixed_buffer, buffer_size); | 115 dest = FPDF_GetNamedDest(document(), 5, fixed_buffer, &buffer_size); |
111 EXPECT_NE(nullptr, dest); | 116 EXPECT_NE(nullptr, dest); |
112 EXPECT_EQ(28u, buffer_size); | 117 EXPECT_EQ(28u, buffer_size); |
113 EXPECT_EQ( | 118 EXPECT_EQ( |
114 std::string("L\0a\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 28), | 119 std::string("L\0a\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 28), |
115 std::string(fixed_buffer, buffer_size)); | 120 std::string(fixed_buffer, buffer_size)); |
116 | 121 |
117 // Try to retrieve non-existent item with ample buffer. | 122 // Try to retrieve non-existent item with ample buffer. |
118 buffer_size = sizeof(fixed_buffer); | 123 buffer_size = sizeof(fixed_buffer); |
119 dest = FPDF_GetNamedDest(document(), 6, fixed_buffer, buffer_size); | 124 dest = FPDF_GetNamedDest(document(), 6, fixed_buffer, &buffer_size); |
120 EXPECT_EQ(nullptr, dest); | 125 EXPECT_EQ(nullptr, dest); |
121 EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. | 126 EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. |
122 | 127 |
123 // Try to underflow/overflow the integer index. | 128 // Try to underflow/overflow the integer index. |
124 buffer_size = sizeof(fixed_buffer); | 129 buffer_size = sizeof(fixed_buffer); |
125 dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::max(), | 130 dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::max(), |
126 fixed_buffer, buffer_size); | 131 fixed_buffer, &buffer_size); |
127 EXPECT_EQ(nullptr, dest); | 132 EXPECT_EQ(nullptr, dest); |
128 EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. | 133 EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. |
129 | 134 |
130 buffer_size = sizeof(fixed_buffer); | 135 buffer_size = sizeof(fixed_buffer); |
131 dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::min(), | 136 dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::min(), |
132 fixed_buffer, buffer_size); | 137 fixed_buffer, &buffer_size); |
133 EXPECT_EQ(nullptr, dest); | 138 EXPECT_EQ(nullptr, dest); |
134 EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. | 139 EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. |
135 | 140 |
136 buffer_size = sizeof(fixed_buffer); | 141 buffer_size = sizeof(fixed_buffer); |
137 dest = FPDF_GetNamedDest(document(), -1, fixed_buffer, buffer_size); | 142 dest = FPDF_GetNamedDest(document(), -1, fixed_buffer, &buffer_size); |
138 EXPECT_EQ(nullptr, dest); | 143 EXPECT_EQ(nullptr, dest); |
139 EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. | 144 EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. |
140 } | 145 } |
141 | 146 |
142 TEST_F(FPDFViewEmbeddertest, NamedDestsByName) { | 147 TEST_F(FPDFViewEmbeddertest, NamedDestsByName) { |
143 EXPECT_TRUE(OpenDocument("testing/resources/named_dests.pdf")); | 148 EXPECT_TRUE(OpenDocument("testing/resources/named_dests.pdf")); |
144 | 149 |
145 // Null pointer returns NULL. | 150 // Null pointer returns NULL. |
146 FPDF_DEST dest = FPDF_GetNamedDestByName(document(), nullptr); | 151 FPDF_DEST dest = FPDF_GetNamedDestByName(document(), nullptr); |
147 EXPECT_EQ(nullptr, dest); | 152 EXPECT_EQ(nullptr, dest); |
148 | 153 |
149 // Empty string returns NULL. | 154 // Empty string returns NULL. |
150 dest = FPDF_GetNamedDestByName(document(), ""); | 155 dest = FPDF_GetNamedDestByName(document(), ""); |
151 EXPECT_EQ(nullptr, dest); | 156 EXPECT_EQ(nullptr, dest); |
152 | 157 |
153 // Item from Dests NameTree. | 158 // Item from Dests NameTree. |
154 dest = FPDF_GetNamedDestByName(document(), "First"); | 159 dest = FPDF_GetNamedDestByName(document(), "First"); |
155 EXPECT_NE(nullptr, dest); | 160 EXPECT_NE(nullptr, dest); |
156 | 161 |
157 long ignore_len = 0; | 162 long ignore_len = 0; |
158 FPDF_DEST dest_by_index = | 163 FPDF_DEST dest_by_index = |
159 FPDF_GetNamedDest(document(), 0, nullptr, ignore_len); | 164 FPDF_GetNamedDest(document(), 0, nullptr, &ignore_len); |
160 EXPECT_EQ(dest_by_index, dest); | 165 EXPECT_EQ(dest_by_index, dest); |
161 | 166 |
162 // Item from Dests dictionary. | 167 // Item from Dests dictionary. |
163 dest = FPDF_GetNamedDestByName(document(), "FirstAlternate"); | 168 dest = FPDF_GetNamedDestByName(document(), "FirstAlternate"); |
164 EXPECT_NE(nullptr, dest); | 169 EXPECT_NE(nullptr, dest); |
165 | 170 |
166 ignore_len = 0; | 171 ignore_len = 0; |
167 dest_by_index = FPDF_GetNamedDest(document(), 4, nullptr, ignore_len); | 172 dest_by_index = FPDF_GetNamedDest(document(), 4, nullptr, &ignore_len); |
168 EXPECT_EQ(dest_by_index, dest); | 173 EXPECT_EQ(dest_by_index, dest); |
169 | 174 |
170 // Bad value type for item from Dests NameTree array. | 175 // Bad value type for item from Dests NameTree array. |
171 dest = FPDF_GetNamedDestByName(document(), "WrongType"); | 176 dest = FPDF_GetNamedDestByName(document(), "WrongType"); |
172 EXPECT_EQ(nullptr, dest); | 177 EXPECT_EQ(nullptr, dest); |
173 | 178 |
174 // No such destination in either Dest NameTree or dictionary. | 179 // No such destination in either Dest NameTree or dictionary. |
175 dest = FPDF_GetNamedDestByName(document(), "Bogus"); | 180 dest = FPDF_GetNamedDestByName(document(), "Bogus"); |
176 EXPECT_EQ(nullptr, dest); | 181 EXPECT_EQ(nullptr, dest); |
177 } | 182 } |
178 | 183 |
179 // The following tests pass if the document opens without crashing. | 184 // The following tests pass if the document opens without crashing. |
180 TEST_F(FPDFViewEmbeddertest, Crasher_113) { | 185 TEST_F(FPDFViewEmbeddertest, Crasher_113) { |
181 EXPECT_TRUE(OpenDocument("testing/resources/bug_113.pdf")); | 186 EXPECT_TRUE(OpenDocument("testing/resources/bug_113.pdf")); |
182 } | 187 } |
183 | 188 |
184 TEST_F(FPDFViewEmbeddertest, Crasher_451830) { | 189 TEST_F(FPDFViewEmbeddertest, Crasher_451830) { |
185 EXPECT_TRUE(OpenDocument("testing/resources/bug_451830.pdf")); | 190 EXPECT_TRUE(OpenDocument("testing/resources/bug_451830.pdf")); |
186 } | 191 } |
187 | 192 |
188 TEST_F(FPDFViewEmbeddertest, Crasher_452455) { | 193 TEST_F(FPDFViewEmbeddertest, Crasher_452455) { |
189 EXPECT_TRUE(OpenDocument("testing/resources/bug_452455.pdf")); | 194 EXPECT_TRUE(OpenDocument("testing/resources/bug_452455.pdf")); |
190 FPDF_PAGE page = LoadPage(0); | 195 FPDF_PAGE page = LoadPage(0); |
191 EXPECT_NE(nullptr, page); | 196 EXPECT_NE(nullptr, page); |
192 } | 197 } |
193 | 198 |
194 TEST_F(FPDFViewEmbeddertest, Crasher3) { | 199 TEST_F(FPDFViewEmbeddertest, Crasher3) { |
195 EXPECT_TRUE(OpenDocument("testing/resources/bug_454695.pdf")); | 200 EXPECT_TRUE(OpenDocument("testing/resources/bug_454695.pdf")); |
196 } | 201 } |
OLD | NEW |