| 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 "public/fpdf_text.h" | 5 #include "public/fpdf_text.h" |
| 6 #include "public/fpdfview.h" | 6 #include "public/fpdfview.h" |
| 7 #include "testing/embedder_test.h" | 7 #include "testing/embedder_test.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); | 35 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); |
| 36 EXPECT_NE(nullptr, textpage); | 36 EXPECT_NE(nullptr, textpage); |
| 37 | 37 |
| 38 static const char expected[] = "Hello, world!\r\nGoodbye, world!"; | 38 static const char expected[] = "Hello, world!\r\nGoodbye, world!"; |
| 39 unsigned short fixed_buffer[128]; | 39 unsigned short fixed_buffer[128]; |
| 40 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 40 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
| 41 | 41 |
| 42 // Check includes the terminating NUL that is provided. | 42 // Check includes the terminating NUL that is provided. |
| 43 int num_chars = FPDFText_GetText(textpage, 0, 128, fixed_buffer); | 43 int num_chars = FPDFText_GetText(textpage, 0, 128, fixed_buffer); |
| 44 ASSERT_GE(num_chars, 0); | 44 ASSERT_GE(num_chars, 0); |
| 45 EXPECT_EQ(sizeof(expected), static_cast<size_t>(num_chars)); | 45 EXPECT_EQ(sizeof(expected) - 1, static_cast<size_t>(num_chars)); |
| 46 EXPECT_TRUE(check_unsigned_shorts(expected, fixed_buffer, sizeof(expected))); | 46 EXPECT_TRUE( |
| 47 check_unsigned_shorts(expected, fixed_buffer, sizeof(expected) - 1)); |
| 47 | 48 |
| 48 // Count does not include the terminating NUL in the string literal. | 49 // Count does not include the terminating NUL in the string literal. |
| 49 EXPECT_EQ(sizeof(expected) - 1, FPDFText_CountChars(textpage)); | 50 EXPECT_EQ(sizeof(expected) - 1, FPDFText_CountChars(textpage)); |
| 50 for (size_t i = 0; i < sizeof(expected) - 1; ++i) { | 51 for (size_t i = 0; i < sizeof(expected) - 1; ++i) { |
| 51 EXPECT_EQ(static_cast<unsigned int>(expected[i]), | 52 EXPECT_EQ(static_cast<unsigned int>(expected[i]), |
| 52 FPDFText_GetUnicode(textpage, i)) | 53 FPDFText_GetUnicode(textpage, i)) |
| 53 << " at " << i; | 54 << " at " << i; |
| 54 } | 55 } |
| 55 | 56 |
| 56 EXPECT_EQ(12.0, FPDFText_GetFontSize(textpage, 0)); | 57 EXPECT_EQ(12.0, FPDFText_GetFontSize(textpage, 0)); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 1)); | 119 EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 1)); |
| 119 EXPECT_EQ(0xbdbd, fixed_buffer[1]); | 120 EXPECT_EQ(0xbdbd, fixed_buffer[1]); |
| 120 | 121 |
| 121 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 122 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
| 122 EXPECT_EQ(9, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0, | 123 EXPECT_EQ(9, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0, |
| 123 fixed_buffer, 9)); | 124 fixed_buffer, 9)); |
| 124 EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9)); | 125 EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9)); |
| 125 EXPECT_EQ(0xbdbd, fixed_buffer[9]); | 126 EXPECT_EQ(0xbdbd, fixed_buffer[9]); |
| 126 | 127 |
| 127 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 128 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
| 128 EXPECT_EQ(10, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0, | 129 EXPECT_EQ(9, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0, |
| 129 fixed_buffer, 128)); | 130 fixed_buffer, 128)); |
| 130 EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9)); | 131 EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9)); |
| 131 EXPECT_EQ(0u, fixed_buffer[9]); | 132 EXPECT_EQ(0xbdbd, fixed_buffer[9]); |
| 132 EXPECT_EQ(0xbdbd, fixed_buffer[10]); | |
| 133 | 133 |
| 134 FPDFText_ClosePage(textpage); | 134 FPDFText_ClosePage(textpage); |
| 135 UnloadPage(page); | 135 UnloadPage(page); |
| 136 } | 136 } |
| 137 | 137 |
| 138 TEST_F(FPDFTextEmbeddertest, TextSearch) { | 138 TEST_F(FPDFTextEmbeddertest, TextSearch) { |
| 139 EXPECT_TRUE(OpenDocument("hello_world.pdf")); | 139 EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 140 FPDF_PAGE page = LoadPage(0); | 140 FPDF_PAGE page = LoadPage(0); |
| 141 EXPECT_NE(nullptr, page); | 141 EXPECT_NE(nullptr, page); |
| 142 | 142 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 | 262 |
| 263 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); | 263 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); |
| 264 EXPECT_NE(nullptr, textpage); | 264 EXPECT_NE(nullptr, textpage); |
| 265 | 265 |
| 266 FPDF_PAGELINK pagelink = FPDFLink_LoadWebLinks(textpage); | 266 FPDF_PAGELINK pagelink = FPDFLink_LoadWebLinks(textpage); |
| 267 EXPECT_NE(nullptr, pagelink); | 267 EXPECT_NE(nullptr, pagelink); |
| 268 | 268 |
| 269 // Page contains two HTTP-style URLs. | 269 // Page contains two HTTP-style URLs. |
| 270 EXPECT_EQ(2, FPDFLink_CountWebLinks(pagelink)); | 270 EXPECT_EQ(2, FPDFLink_CountWebLinks(pagelink)); |
| 271 | 271 |
| 272 // Only a terminating NUL required for bogus links. | 272 EXPECT_EQ(0, FPDFLink_GetURL(pagelink, 2, nullptr, 0)); |
| 273 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 2, nullptr, 0)); | 273 EXPECT_EQ(0, FPDFLink_GetURL(pagelink, 1400, nullptr, 0)); |
| 274 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 1400, nullptr, 0)); | 274 EXPECT_EQ(0, FPDFLink_GetURL(pagelink, -1, nullptr, 0)); |
| 275 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, -1, nullptr, 0)); | |
| 276 | 275 |
| 277 // Query the number of characters required for each link (incl NUL). | 276 // Query the number of characters required for each link (incl NUL). |
| 278 EXPECT_EQ(25, FPDFLink_GetURL(pagelink, 0, nullptr, 0)); | 277 EXPECT_EQ(24, FPDFLink_GetURL(pagelink, 0, nullptr, 0)); |
| 279 EXPECT_EQ(26, FPDFLink_GetURL(pagelink, 1, nullptr, 0)); | 278 EXPECT_EQ(25, FPDFLink_GetURL(pagelink, 1, nullptr, 0)); |
| 280 | 279 |
| 281 static const char expected_url[] = "http://example.com?q=foo"; | 280 static const char expected_url[] = "http://example.com?q=foo"; |
| 282 unsigned short fixed_buffer[128]; | 281 unsigned short fixed_buffer[128]; |
| 283 | 282 |
| 284 // Retrieve a link with too small a buffer. Buffer will not be | 283 // Retrieve a link with too small a buffer. Buffer will not be |
| 285 // NUL-terminated, but must not be modified past indicated length, | 284 // NUL-terminated, but must not be modified past indicated length, |
| 286 // so pre-fill with a pattern to check write bounds. | 285 // so pre-fill with a pattern to check write bounds. |
| 287 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 286 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
| 288 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 0, fixed_buffer, 1)); | 287 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 0, fixed_buffer, 1)); |
| 289 EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, 1)); | 288 EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, 1)); |
| 290 EXPECT_EQ(0xbdbd, fixed_buffer[1]); | 289 EXPECT_EQ(0xbdbd, fixed_buffer[1]); |
| 291 | 290 |
| 292 // Check buffer that doesn't have space for a terminating NUL. | 291 // Check buffer that doesn't have space for a terminating NUL. |
| 293 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 292 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
| 294 EXPECT_EQ( | 293 EXPECT_EQ( |
| 295 sizeof(expected_url) - 1, | 294 sizeof(expected_url) - 1, |
| 296 FPDFLink_GetURL(pagelink, 0, fixed_buffer, sizeof(expected_url) - 1)); | 295 FPDFLink_GetURL(pagelink, 0, fixed_buffer, sizeof(expected_url) - 1)); |
| 297 EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, | 296 EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, |
| 298 sizeof(expected_url) - 1)); | 297 sizeof(expected_url) - 1)); |
| 299 EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url) - 1]); | 298 EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url) - 1]); |
| 300 | 299 |
| 301 // Retreive link with exactly-sized buffer. | 300 // Retreive link with exactly-sized buffer. |
| 302 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 301 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
| 303 EXPECT_EQ(sizeof(expected_url), | 302 EXPECT_EQ(sizeof(expected_url) - 1, |
| 304 FPDFLink_GetURL(pagelink, 0, fixed_buffer, sizeof(expected_url))); | 303 FPDFLink_GetURL(pagelink, 0, fixed_buffer, sizeof(expected_url))); |
| 305 EXPECT_TRUE( | 304 EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, |
| 306 check_unsigned_shorts(expected_url, fixed_buffer, sizeof(expected_url))); | 305 sizeof(expected_url) - 1)); |
| 307 EXPECT_EQ(0u, fixed_buffer[sizeof(expected_url) - 1]); | 306 EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url) - 1]); |
| 308 EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url)]); | |
| 309 | 307 |
| 310 // Retreive link with ample-sized-buffer. | 308 // Retreive link with ample-sized-buffer. |
| 311 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 309 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
| 312 EXPECT_EQ(sizeof(expected_url), | 310 EXPECT_EQ(sizeof(expected_url) - 1, |
| 313 FPDFLink_GetURL(pagelink, 0, fixed_buffer, 128)); | 311 FPDFLink_GetURL(pagelink, 0, fixed_buffer, 128)); |
| 314 EXPECT_TRUE( | 312 EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, |
| 315 check_unsigned_shorts(expected_url, fixed_buffer, sizeof(expected_url))); | 313 sizeof(expected_url) - 1)); |
| 316 EXPECT_EQ(0u, fixed_buffer[sizeof(expected_url) - 1]); | 314 EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url) - 1]); |
| 317 EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url)]); | |
| 318 | 315 |
| 319 // Each link rendered in a single rect in this test page. | 316 // Each link rendered in a single rect in this test page. |
| 320 EXPECT_EQ(1, FPDFLink_CountRects(pagelink, 0)); | 317 EXPECT_EQ(1, FPDFLink_CountRects(pagelink, 0)); |
| 321 EXPECT_EQ(1, FPDFLink_CountRects(pagelink, 1)); | 318 EXPECT_EQ(1, FPDFLink_CountRects(pagelink, 1)); |
| 322 | 319 |
| 323 // Each link rendered in a single rect in this test page. | 320 // Each link rendered in a single rect in this test page. |
| 324 EXPECT_EQ(0, FPDFLink_CountRects(pagelink, -1)); | 321 EXPECT_EQ(0, FPDFLink_CountRects(pagelink, -1)); |
| 325 EXPECT_EQ(0, FPDFLink_CountRects(pagelink, 2)); | 322 EXPECT_EQ(0, FPDFLink_CountRects(pagelink, 2)); |
| 326 EXPECT_EQ(0, FPDFLink_CountRects(pagelink, 10000)); | 323 EXPECT_EQ(0, FPDFLink_CountRects(pagelink, 10000)); |
| 327 | 324 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 355 FPDFLink_GetRect(pagelink, -1, 0, &left, &top, &right, &bottom); | 352 FPDFLink_GetRect(pagelink, -1, 0, &left, &top, &right, &bottom); |
| 356 EXPECT_EQ(-2.0, left); | 353 EXPECT_EQ(-2.0, left); |
| 357 EXPECT_EQ(-2.0, right); | 354 EXPECT_EQ(-2.0, right); |
| 358 EXPECT_EQ(-2.0, bottom); | 355 EXPECT_EQ(-2.0, bottom); |
| 359 EXPECT_EQ(-2.0, top); | 356 EXPECT_EQ(-2.0, top); |
| 360 | 357 |
| 361 FPDFLink_CloseWebLinks(pagelink); | 358 FPDFLink_CloseWebLinks(pagelink); |
| 362 FPDFText_ClosePage(textpage); | 359 FPDFText_ClosePage(textpage); |
| 363 UnloadPage(page); | 360 UnloadPage(page); |
| 364 } | 361 } |
| OLD | NEW |