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