Kill overloaded cast operators in CJS_Value.
[pdfium.git] / fpdfsdk / src / fpdftext_embeddertest.cpp
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
5 #include "../../testing/embedder_test.h"
6 #include "../../fpdfsdk/include/fpdfview.h"
7 #include "../../fpdfsdk/include/fpdftext.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace {
11
12 static bool check_unsigned_shorts(const char* expected,
13                                   const unsigned short* actual,
14                                   size_t length) {
15   if (length > strlen(expected) + 1) {
16     return false;
17   }
18   for (size_t i = 0; i < length; ++i) {
19     if (actual[i] != static_cast<unsigned short>(expected[i])) {
20       return false;
21     }
22   }
23   return true;
24 }
25
26 }  // namespace
27
28 class FPDFTextEmbeddertest : public EmbedderTest {
29 };
30
31 TEST_F(FPDFTextEmbeddertest, Text) {
32   EXPECT_TRUE(OpenDocument("testing/resources/hello_world.pdf"));
33   FPDF_PAGE page = LoadPage(0);
34   EXPECT_NE(nullptr, page);
35
36   FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page);
37   EXPECT_NE(nullptr, textpage);
38
39   const char expected[] = "Hello, world!\r\nGoodbye, world!";
40   unsigned short fixed_buffer[128];
41   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
42
43   // Check includes the terminating NUL that is provided.
44   EXPECT_EQ(sizeof(expected), FPDFText_GetText(textpage, 0, 128, fixed_buffer));
45   EXPECT_TRUE(check_unsigned_shorts(expected, fixed_buffer, sizeof(expected)));
46
47   // Count does not include the terminating NUL in the string literal.
48   EXPECT_EQ(sizeof(expected) - 1, FPDFText_CountChars(textpage));
49   for (size_t i = 0; i < sizeof(expected) - 1; ++i) {
50     EXPECT_EQ(expected[i], FPDFText_GetUnicode(textpage, i)) << " at " << i;
51   }
52
53   EXPECT_EQ(12.0, FPDFText_GetFontSize(textpage, 0));
54   EXPECT_EQ(16.0, FPDFText_GetFontSize(textpage, 15));
55
56   double left = 0.0;
57   double right = 0.0;
58   double bottom = 0.0;
59   double top = 0.0;
60   FPDFText_GetCharBox(textpage, 4, &left, &right, &bottom, &top);
61   EXPECT_NEAR(41.071, left, 0.001);
62   EXPECT_NEAR(46.243, right, 0.001);
63   EXPECT_NEAR(49.844, bottom, 0.001);
64   EXPECT_NEAR(55.520, top, 0.001);
65
66   EXPECT_EQ(4, FPDFText_GetCharIndexAtPos(
67       textpage, 42.0, 50.0, 1.0, 1.0));
68   EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(
69       textpage, 0.0, 0.0, 1.0, 1.0));
70   EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(
71       textpage, 199.0, 199.0, 1.0, 1.0));
72
73   // Test out of range indicies.
74   EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(
75       textpage, 42.0, 10000000.0, 1.0, 1.0));
76   EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(
77       textpage, -1.0, 50.0, 1.0, 1.0));
78
79   // Count does not include the terminating NUL in the string literal.
80   EXPECT_EQ(2, FPDFText_CountRects(textpage, 0, sizeof(expected) - 1));
81
82   left = 0.0;
83   right = 0.0;
84   bottom = 0.0;
85   top = 0.0;
86   FPDFText_GetRect(textpage, 1, &left, &top, &right, &bottom);
87   EXPECT_NEAR(20.847, left, 0.001);
88   EXPECT_NEAR(135.167, right, 0.001);
89   EXPECT_NEAR(96.655, bottom, 0.001);
90   EXPECT_NEAR(116.000, top, 0.001);
91
92   // Test out of range indicies set outputs to (0.0, 0.0, 0.0, 0.0).
93   left = -1.0;
94   right = -1.0;
95   bottom = -1.0;
96   top = -1.0;
97   FPDFText_GetRect(textpage, -1, &left, &top, &right, &bottom);
98   EXPECT_EQ(0.0, left);
99   EXPECT_EQ(0.0, right);
100   EXPECT_EQ(0.0, bottom);
101   EXPECT_EQ(0.0, top);
102
103   left = -2.0;
104   right = -2.0;
105   bottom = -2.0;
106   top = -2.0;
107   FPDFText_GetRect(textpage, 2, &left, &top, &right, &bottom);
108   EXPECT_EQ(0.0, left);
109   EXPECT_EQ(0.0, right);
110   EXPECT_EQ(0.0, bottom);
111   EXPECT_EQ(0.0, top);
112
113   EXPECT_EQ(9, FPDFText_GetBoundedText(
114       textpage, 41.0, 56.0, 82.0, 48.0, 0, 0));
115
116   // Extract starting at character 4 as above.
117   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
118   EXPECT_EQ(1, FPDFText_GetBoundedText(
119       textpage, 41.0, 56.0, 82.0, 48.0, fixed_buffer, 1));
120   EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 1));
121   EXPECT_EQ(0xbdbd, fixed_buffer[1]);
122
123   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
124   EXPECT_EQ(9, FPDFText_GetBoundedText(
125       textpage, 41.0, 56.0, 82.0, 48.0, fixed_buffer, 9));
126   EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9));
127   EXPECT_EQ(0xbdbd, fixed_buffer[9]);
128
129   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
130   EXPECT_EQ(10, FPDFText_GetBoundedText(
131       textpage, 41.0, 56.0, 82.0, 48.0, fixed_buffer, 128));
132   EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9));
133   EXPECT_EQ(0u, fixed_buffer[9]);
134   EXPECT_EQ(0xbdbd, fixed_buffer[10]);
135
136   FPDFText_ClosePage(textpage);
137 }
138
139 TEST_F(FPDFTextEmbeddertest, TextSearch) {
140   EXPECT_TRUE(OpenDocument("testing/resources/hello_world.pdf"));
141   FPDF_PAGE page = LoadPage(0);
142   EXPECT_NE(nullptr, page);
143
144   FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page);
145   EXPECT_NE(nullptr, textpage);
146
147   // Avoid issues with system wchar_t width vs. FPDF_WideString.
148   const unsigned short nope[] = { 'n', 'o', 'p', 'e', '\0' };
149   const unsigned short world[] = { 'w', 'o', 'r', 'l', 'd', '\0' };
150   const unsigned short world_caps[] = { 'W', 'O', 'R', 'L', 'D', '\0' };
151   const unsigned short world_substr[] = { 'o', 'r', 'l', 'd', '\0' };
152
153   // No occurences of "nope" in test page.
154   FPDF_SCHHANDLE search = FPDFText_FindStart(textpage, nope, 0, 0);
155   EXPECT_NE(nullptr, search);
156   EXPECT_EQ(0, FPDFText_GetSchResultIndex(search));
157   EXPECT_EQ(0, FPDFText_GetSchCount(search));
158
159   // Advancing finds nothing.
160   EXPECT_FALSE(FPDFText_FindNext(search));
161   EXPECT_EQ(0, FPDFText_GetSchResultIndex(search));
162   EXPECT_EQ(0, FPDFText_GetSchCount(search));
163
164   // Retreating finds nothing.
165   EXPECT_FALSE(FPDFText_FindPrev(search));
166   EXPECT_EQ(0, FPDFText_GetSchResultIndex(search));
167   EXPECT_EQ(0, FPDFText_GetSchCount(search));
168   FPDFText_FindClose(search);
169
170   // Two occurences of "world" in test page.
171   search = FPDFText_FindStart(textpage, world, 0, 2);
172   EXPECT_NE(nullptr, search);
173
174   // Remains not found until advanced.
175   EXPECT_EQ(0, FPDFText_GetSchResultIndex(search));
176   EXPECT_EQ(0, FPDFText_GetSchCount(search));
177
178   // First occurence of "world" in this test page.
179   EXPECT_TRUE(FPDFText_FindNext(search));
180   EXPECT_EQ(7, FPDFText_GetSchResultIndex(search));
181   EXPECT_EQ(5, FPDFText_GetSchCount(search));
182
183   // Last occurence of "world" in this test page.
184   EXPECT_TRUE(FPDFText_FindNext(search));
185   EXPECT_EQ(24, FPDFText_GetSchResultIndex(search));
186   EXPECT_EQ(5, FPDFText_GetSchCount(search));
187
188   // Found position unchanged when fails to advance.
189   EXPECT_FALSE(FPDFText_FindNext(search));
190   EXPECT_EQ(24, FPDFText_GetSchResultIndex(search));
191   EXPECT_EQ(5, FPDFText_GetSchCount(search));
192
193   // Back to first occurence.
194   EXPECT_TRUE(FPDFText_FindPrev(search));
195   EXPECT_EQ(7, FPDFText_GetSchResultIndex(search));
196   EXPECT_EQ(5, FPDFText_GetSchCount(search));
197
198   // Found position unchanged when fails to retreat.
199   EXPECT_FALSE(FPDFText_FindPrev(search));
200   EXPECT_EQ(7, FPDFText_GetSchResultIndex(search));
201   EXPECT_EQ(5, FPDFText_GetSchCount(search));
202   FPDFText_FindClose(search);
203
204   // Exact search unaffected by case sensitiity and whole word flags.
205   search = FPDFText_FindStart(
206       textpage, world, FPDF_MATCHCASE | FPDF_MATCHWHOLEWORD, 0);
207   EXPECT_NE(nullptr, search);
208   EXPECT_TRUE(FPDFText_FindNext(search));
209   EXPECT_EQ(7, FPDFText_GetSchResultIndex(search));
210   EXPECT_EQ(5, FPDFText_GetSchCount(search));
211   FPDFText_FindClose(search);
212
213   // Default is case-insensitive, so matching agaist caps works.
214   search = FPDFText_FindStart(textpage, world_caps, 0, 0);
215   EXPECT_NE(nullptr, search);
216   EXPECT_TRUE(FPDFText_FindNext(search));
217   EXPECT_EQ(7, FPDFText_GetSchResultIndex(search));
218   EXPECT_EQ(5, FPDFText_GetSchCount(search));
219   FPDFText_FindClose(search);
220
221   // But can be made case sensitive, in which case this fails.
222   search = FPDFText_FindStart(textpage, world_caps, FPDF_MATCHCASE, 0);
223   EXPECT_FALSE(FPDFText_FindNext(search));
224   EXPECT_EQ(0, FPDFText_GetSchResultIndex(search));
225   EXPECT_EQ(0, FPDFText_GetSchCount(search));
226   FPDFText_FindClose(search);
227
228   // Default is match anywhere within word, so matching substirng works.
229   search = FPDFText_FindStart(textpage, world_substr, 0, 0);
230   EXPECT_TRUE(FPDFText_FindNext(search));
231   EXPECT_EQ(8, FPDFText_GetSchResultIndex(search));
232   EXPECT_EQ(4, FPDFText_GetSchCount(search));
233   FPDFText_FindClose(search);
234
235   // But can be made to mach word boundaries, in which case this fails.
236   search = FPDFText_FindStart(textpage, world_substr, FPDF_MATCHWHOLEWORD, 0);
237   EXPECT_FALSE(FPDFText_FindNext(search));
238   // TODO(tsepez): investigate strange index/count values in this state.
239   FPDFText_FindClose(search);
240
241   FPDFText_ClosePage(textpage);
242 }
243
244 // Test that the page has characters despite a bad stream length.
245 TEST_F(FPDFTextEmbeddertest, StreamLengthPastEndOfFile) {
246   EXPECT_TRUE(OpenDocument("testing/resources/bug_57.pdf"));
247   FPDF_PAGE page = LoadPage(0);
248   EXPECT_NE(nullptr, page);
249
250   FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page);
251   EXPECT_NE(nullptr, textpage);
252   EXPECT_EQ(13, FPDFText_CountChars(textpage));
253
254   FPDFText_ClosePage(textpage);
255 }
256
257 TEST_F(FPDFTextEmbeddertest, WebLinks) {
258   EXPECT_TRUE(OpenDocument("testing/resources/weblinks.pdf"));
259   FPDF_PAGE page = LoadPage(0);
260   EXPECT_NE(nullptr, page);
261
262   FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page);
263   EXPECT_NE(nullptr, textpage);
264
265   FPDF_PAGELINK pagelink = FPDFLink_LoadWebLinks(textpage);
266   EXPECT_NE(nullptr, pagelink);
267
268   // Page contains two HTTP-style URLs.
269   EXPECT_EQ(2, FPDFLink_CountWebLinks(pagelink));
270
271   // Only a terminating NUL required for bogus links.
272   EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 2, nullptr, 0));
273   EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 1400, nullptr, 0));
274   EXPECT_EQ(1, FPDFLink_GetURL(pagelink, -1, nullptr, 0));
275
276   // Query the number of characters required for each link (incl NUL).
277   EXPECT_EQ(25, FPDFLink_GetURL(pagelink, 0, nullptr, 0));
278   EXPECT_EQ(26, FPDFLink_GetURL(pagelink, 1, nullptr, 0));
279
280   const char expected_url[] = "http://example.com?q=foo";
281   unsigned short fixed_buffer[128];
282
283   // Retrieve a link with too small a buffer.  Buffer will not be
284   // NUL-terminated, but must not be modified past indicated length,
285   // so pre-fill with a pattern to check write bounds.
286   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
287   EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 0, fixed_buffer, 1));
288   EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, 1));
289   EXPECT_EQ(0xbdbd, fixed_buffer[1]);
290
291   // Check buffer that doesn't have space for a terminating NUL.
292   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
293   EXPECT_EQ(sizeof(expected_url) - 1, FPDFLink_GetURL(
294       pagelink, 0, fixed_buffer, sizeof(expected_url) - 1));
295   EXPECT_TRUE(check_unsigned_shorts(
296       expected_url, fixed_buffer, sizeof(expected_url) - 1));
297   EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url) - 1]);
298
299   // Retreive link with exactly-sized buffer.
300   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
301   EXPECT_EQ(sizeof(expected_url), FPDFLink_GetURL(
302       pagelink, 0, fixed_buffer, sizeof(expected_url)));
303   EXPECT_TRUE(check_unsigned_shorts(
304       expected_url, fixed_buffer, sizeof(expected_url)));
305   EXPECT_EQ(0u, fixed_buffer[sizeof(expected_url) - 1]);
306   EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url)]);
307
308   // Retreive link with ample-sized-buffer.
309   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
310   EXPECT_EQ(sizeof(expected_url), FPDFLink_GetURL(
311       pagelink, 0, fixed_buffer, 128));
312   EXPECT_TRUE(check_unsigned_shorts(
313       expected_url, fixed_buffer, sizeof(expected_url)));
314   EXPECT_EQ(0u, fixed_buffer[sizeof(expected_url) - 1]);
315   EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url)]);
316
317   // Each link rendered in a single rect in this test page.
318   EXPECT_EQ(1, FPDFLink_CountRects(pagelink, 0));
319   EXPECT_EQ(1, FPDFLink_CountRects(pagelink, 1));
320
321   // Each link rendered in a single rect in this test page.
322   EXPECT_EQ(0, FPDFLink_CountRects(pagelink, -1));
323   EXPECT_EQ(0, FPDFLink_CountRects(pagelink, 2));
324   EXPECT_EQ(0, FPDFLink_CountRects(pagelink, 10000));
325
326   // Check boundary of valid link index with valid rect index.
327   double left = 0.0;
328   double right = 0.0;
329   double top = 0.0;
330   double bottom = 0.0;
331   FPDFLink_GetRect(pagelink, 0, 0, &left, &top, &right, &bottom);
332   EXPECT_NEAR(50.791, left, 0.001);
333   EXPECT_NEAR(187.963, right, 0.001);
334   EXPECT_NEAR(97.624, bottom, 0.001);
335   EXPECT_NEAR(108.736, top, 0.001);
336
337   // Check that valid link with invalid rect index leaves parameters unchanged.
338   left = -1.0;
339   right = -1.0;
340   top = -1.0;
341   bottom = -1.0;
342   FPDFLink_GetRect(pagelink, 0, 1, &left, &top, &right, &bottom);
343   EXPECT_EQ(-1.0, left);
344   EXPECT_EQ(-1.0, right);
345   EXPECT_EQ(-1.0, bottom);
346   EXPECT_EQ(-1.0, top);
347
348   // Check that invalid link index leaves parameters unchanged.
349   left = -2.0;
350   right = -2.0;
351   top = -2.0;
352   bottom = -2.0;
353   FPDFLink_GetRect(pagelink, -1, 0, &left, &top, &right, &bottom);
354   EXPECT_EQ(-2.0, left);
355   EXPECT_EQ(-2.0, right);
356   EXPECT_EQ(-2.0, bottom);
357   EXPECT_EQ(-2.0, top);
358
359   FPDFLink_CloseWebLinks(pagelink);
360   FPDFText_ClosePage(textpage);
361 }