blob: bc4c027c6641cae5a52fb8a6efab54e8a3998bb6 [file] [log] [blame]
Lei Zhang1ac47eb2015-12-21 11:04:44 -08001// Copyright 2015 PDFium Authors. All rights reserved.
Tom Sepez96d13342015-01-16 14:59:26 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Wei Li091f7a02015-11-09 12:09:55 -08005#include "testing/embedder_test.h"
Tom Sepez96d13342015-01-16 14:59:26 -08006
7#include <limits.h>
Tom Sepez96d13342015-01-16 14:59:26 -08008
9#include <list>
10#include <string>
11#include <utility>
12#include <vector>
13
thestigbcd3e532016-11-21 13:37:28 -080014#include "core/fdrm/crypto/fx_crypt.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080015#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080016#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080017#include "public/fpdf_text.h"
18#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080019#include "testing/gmock/include/gmock/gmock.h"
Wei Li091f7a02015-11-09 12:09:55 -080020#include "testing/test_support.h"
21#include "testing/utils/path_service.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070022
23#ifdef PDF_ENABLE_V8
Lei Zhang8241df72015-11-06 14:38:48 -080024#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050025#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070026#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080027
Tom Sepez96d13342015-01-16 14:59:26 -080028namespace {
thestigc08cd7a2016-06-27 09:47:59 -070029
30const char* g_exe_path = nullptr;
31
32#ifdef PDF_ENABLE_V8
33#ifdef V8_USE_EXTERNAL_STARTUP_DATA
34v8::StartupData* g_v8_natives = nullptr;
35v8::StartupData* g_v8_snapshot = nullptr;
36#endif // V8_USE_EXTERNAL_STARTUP_DATA
37#endif // PDF_ENABLE_V8
38
Tom Sepezcf22eb82015-05-12 17:28:08 -070039FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
Tom Sepez96d13342015-01-16 14:59:26 -080040 return true;
41}
42
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {}
Tom Sepez96d13342015-01-16 14:59:26 -080044
thestigbcd3e532016-11-21 13:37:28 -080045std::string CRYPT_ToBase16(const uint8_t* digest) {
46 static char const zEncode[] = "0123456789abcdef";
47 std::string ret;
48 ret.resize(32);
49 for (int i = 0, j = 0; i < 16; i++, j += 2) {
50 uint8_t a = digest[i];
51 ret[j] = zEncode[(a >> 4) & 0xf];
52 ret[j + 1] = zEncode[a & 0xf];
53 }
54 return ret;
55}
56
57} // namespace
58
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059EmbedderTest::EmbedderTest()
Tom Sepeza72e8e22015-10-07 10:17:53 -070060 : default_delegate_(new EmbedderTest::Delegate()),
61 document_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080062 form_handle_(nullptr),
63 avail_(nullptr),
Tom Sepeza72e8e22015-10-07 10:17:53 -070064 external_isolate_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080065 loader_(nullptr),
66 file_length_(0),
67 file_contents_(nullptr) {
68 memset(&hints_, 0, sizeof(hints_));
69 memset(&file_access_, 0, sizeof(file_access_));
70 memset(&file_avail_, 0, sizeof(file_avail_));
Tom Sepeza72e8e22015-10-07 10:17:53 -070071 delegate_ = default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -080072
Tom Sepez452b4f32015-10-13 09:27:27 -070073#ifdef PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080074#ifdef V8_USE_EXTERNAL_STARTUP_DATA
thestigc08cd7a2016-06-27 09:47:59 -070075 if (g_v8_natives && g_v8_snapshot) {
76 InitializeV8ForPDFium(g_exe_path, std::string(), nullptr, nullptr,
77 &platform_);
78 } else {
79 g_v8_natives = new v8::StartupData;
80 g_v8_snapshot = new v8::StartupData;
81 InitializeV8ForPDFium(g_exe_path, std::string(), g_v8_natives,
82 g_v8_snapshot, &platform_);
83 }
Tom Sepezd831dc72015-10-19 16:04:22 -070084#else
thestigc08cd7a2016-06-27 09:47:59 -070085 InitializeV8ForPDFium(g_exe_path, &platform_);
Tom Sepez96d13342015-01-16 14:59:26 -080086#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -070087#endif // FPDF_ENABLE_V8
Tom Sepezf288bb12015-11-20 12:12:46 -080088}
Tom Sepez96d13342015-01-16 14:59:26 -080089
jochen38a1f0a2016-05-31 12:07:40 -070090EmbedderTest::~EmbedderTest() {
91#ifdef PDF_ENABLE_V8
92 v8::V8::ShutdownPlatform();
93 delete platform_;
94#endif // PDF_ENABLE_V8
95}
Tom Sepezf288bb12015-11-20 12:12:46 -080096
97void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -070098 FPDF_LIBRARY_CONFIG config;
99 config.version = 2;
100 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700101 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -0700102 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700103 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -0800104
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
106 memset(info, 0, sizeof(UNSUPPORT_INFO));
107 info->version = 1;
108 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
109 FSDK_SetUnSpObjProcessHandler(info);
110}
Tom Sepez96d13342015-01-16 14:59:26 -0800111
112void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -0800113 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700114 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepezc46d0002015-11-30 15:46:36 -0800115 FPDFDOC_ExitFormFillEnvironment(form_handle_);
116 FPDF_CloseDocument(document_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800117 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800118
Tom Sepez96d13342015-01-16 14:59:26 -0800119 FPDFAvail_Destroy(avail_);
120 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -0700121
Lei Zhangd27acae2015-05-15 15:36:02 -0700122 delete loader_;
Tom Sepez96d13342015-01-16 14:59:26 -0800123}
124
Tom Sepezd483eb42016-01-06 10:03:59 -0800125bool EmbedderTest::CreateEmptyDocument() {
126 document_ = FPDF_CreateNewDocument();
127 if (!document_)
128 return false;
129
130 SetupFormFillEnvironment();
131 return true;
132}
133
Jun Fangdf7f3662015-11-10 18:29:18 +0800134bool EmbedderTest::OpenDocument(const std::string& filename,
thestig27ddf162016-05-23 15:06:59 -0700135 const char* password,
Jun Fangdf7f3662015-11-10 18:29:18 +0800136 bool must_linearize) {
Wei Li091f7a02015-11-09 12:09:55 -0800137 std::string file_path;
138 if (!PathService::GetTestFilePath(filename, &file_path))
139 return false;
140 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400141 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800142 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800143
thestig29ce9232016-06-22 07:03:23 -0700144 EXPECT_TRUE(!loader_);
Tom Sepez0aa35312016-01-06 10:16:32 -0800145 loader_ = new TestLoader(file_contents_.get(), file_length_);
Tom Sepez96d13342015-01-16 14:59:26 -0800146 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700147 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800148 file_access_.m_Param = loader_;
149
150 file_avail_.version = 1;
151 file_avail_.IsDataAvail = Is_Data_Avail;
152
153 hints_.version = 1;
154 hints_.AddSegment = Add_Segment;
155
156 avail_ = FPDFAvail_Create(&file_avail_, &file_access_);
Tom Sepez96d13342015-01-16 14:59:26 -0800157
Jun Fangdf7f3662015-11-10 18:29:18 +0800158 if (FPDFAvail_IsLinearized(avail_) == PDF_LINEARIZED) {
thestig27ddf162016-05-23 15:06:59 -0700159 document_ = FPDFAvail_GetDocument(avail_, password);
Jun Fangdf7f3662015-11-10 18:29:18 +0800160 if (!document_) {
161 return false;
162 }
163 int32_t nRet = PDF_DATA_NOTAVAIL;
164 while (nRet == PDF_DATA_NOTAVAIL) {
165 nRet = FPDFAvail_IsDocAvail(avail_, &hints_);
166 }
167 if (nRet == PDF_DATA_ERROR) {
168 return false;
169 }
170 nRet = FPDFAvail_IsFormAvail(avail_, &hints_);
171 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
172 return false;
173 }
174 int page_count = FPDF_GetPageCount(document_);
175 for (int i = 0; i < page_count; ++i) {
176 nRet = PDF_DATA_NOTAVAIL;
177 while (nRet == PDF_DATA_NOTAVAIL) {
178 nRet = FPDFAvail_IsPageAvail(avail_, i, &hints_);
179 }
180 if (nRet == PDF_DATA_ERROR) {
181 return false;
182 }
183 }
184 } else {
185 if (must_linearize) {
186 return false;
187 }
188 document_ = FPDF_LoadCustomDocument(&file_access_, nullptr);
189 if (!document_) {
190 return false;
191 }
192 }
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400193
Tom Sepezc46d0002015-11-30 15:46:36 -0800194#ifdef PDF_ENABLE_XFA
JUN FANG827a1722015-03-05 13:39:21 -0800195 int docType = DOCTYPE_PDF;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 if (FPDF_HasXFAField(document_, &docType)) {
JUN FANG827a1722015-03-05 13:39:21 -0800197 if (docType != DOCTYPE_PDF)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 (void)FPDF_LoadXFA(document_);
JUN FANG827a1722015-03-05 13:39:21 -0800199 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800200#endif // PDF_ENABLE_XFA
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400201
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 (void)FPDF_GetDocPermissions(document_);
Tom Sepezd483eb42016-01-06 10:03:59 -0800203 SetupFormFillEnvironment();
204 return true;
205}
Tom Sepez96d13342015-01-16 14:59:26 -0800206
Tom Sepezd483eb42016-01-06 10:03:59 -0800207void EmbedderTest::SetupFormFillEnvironment() {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800208 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
209 memset(platform, 0, sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200210 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800211 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800212
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800213 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
214 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800215#ifdef PDF_ENABLE_XFA
216 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800217#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800218 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800219#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700220 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
221 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700222 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800223 formfillinfo->m_pJsPlatform = platform;
Tom Sepez96d13342015-01-16 14:59:26 -0800224
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800225 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
Tom Sepezda8189e2015-01-30 14:41:50 -0800226 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
227 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
Tom Sepez96d13342015-01-16 14:59:26 -0800228}
229
Tom Sepezda8189e2015-01-30 14:41:50 -0800230void EmbedderTest::DoOpenActions() {
231 FORM_DoDocumentJSAction(form_handle_);
232 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800233}
234
235int EmbedderTest::GetFirstPageNum() {
236 int first_page = FPDFAvail_GetFirstPageNum(document_);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 (void)FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800238 return first_page;
239}
240
241int EmbedderTest::GetPageCount() {
242 int page_count = FPDF_GetPageCount(document_);
243 for (int i = 0; i < page_count; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 (void)FPDFAvail_IsPageAvail(avail_, i, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800245 }
246 return page_count;
247}
248
Tom Sepezda8189e2015-01-30 14:41:50 -0800249FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
weili0dadcc62016-08-23 21:10:57 -0700250 // First check whether it is loaded already.
251 auto it = page_map_.find(page_number);
252 if (it != page_map_.end())
253 return it->second;
254
Tom Sepez96d13342015-01-16 14:59:26 -0800255 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
256 if (!page) {
257 return nullptr;
258 }
Tom Sepezda8189e2015-01-30 14:41:50 -0800259 FORM_OnAfterLoadPage(page, form_handle_);
260 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
weili0dadcc62016-08-23 21:10:57 -0700261 // Cache the page.
262 page_map_[page_number] = page;
dsinclaircb92dc72016-09-07 09:02:48 -0700263 page_reverse_map_[page] = page_number;
Tom Sepez396e8722015-09-09 10:16:08 -0700264 return page;
265}
266
Tom Sepezda8189e2015-01-30 14:41:50 -0800267FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Tom Sepez96d13342015-01-16 14:59:26 -0800268 int width = static_cast<int>(FPDF_GetPageWidth(page));
269 int height = static_cast<int>(FPDF_GetPageHeight(page));
Lei Zhang453d96b2015-12-31 13:13:10 -0800270 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
271 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
272 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
273 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
Tom Sepez96d13342015-01-16 14:59:26 -0800274 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepezda8189e2015-01-30 14:41:50 -0800275 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepez96d13342015-01-16 14:59:26 -0800276 return bitmap;
277}
278
Tom Sepezda8189e2015-01-30 14:41:50 -0800279void EmbedderTest::UnloadPage(FPDF_PAGE page) {
280 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
281 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800282 FPDF_ClosePage(page);
dsinclaircb92dc72016-09-07 09:02:48 -0700283
284 auto it = page_reverse_map_.find(page);
285 if (it == page_reverse_map_.end())
286 return;
287
288 page_map_.erase(it->second);
289 page_reverse_map_.erase(it);
Tom Sepez96d13342015-01-16 14:59:26 -0800290}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800291
weili0dadcc62016-08-23 21:10:57 -0700292FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700293 FPDF_DOCUMENT document,
294 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700295 EmbedderTest* test = static_cast<EmbedderTest*>(info);
296 auto it = test->page_map_.find(page_index);
297 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700298}
299
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800300// static
301void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
302 int type) {
303 EmbedderTest* test = static_cast<EmbedderTest*>(info);
304 test->delegate_->UnsupportedHandler(type);
305}
306
307// static
308int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
309 FPDF_WIDESTRING message,
310 FPDF_WIDESTRING title,
311 int type,
312 int icon) {
313 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
314 return test->delegate_->Alert(message, title, type, icon);
315}
316
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700317// static
318int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 int msecs,
320 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700321 EmbedderTest* test = static_cast<EmbedderTest*>(info);
322 return test->delegate_->SetTimer(msecs, fn);
323}
324
325// static
326void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
327 EmbedderTest* test = static_cast<EmbedderTest*>(info);
328 return test->delegate_->KillTimer(id);
329}
330
Tom Sepez396e8722015-09-09 10:16:08 -0700331// static
332FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
333 FPDF_DOCUMENT document,
334 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700335 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
336 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700337}
338
thestigbcd3e532016-11-21 13:37:28 -0800339// static
340void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
341 int expected_width,
342 int expected_height,
343 const char* expected_md5sum) {
344 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
345 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
346 const int expected_stride = expected_width * 4;
347 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
348
349 if (!expected_md5sum)
350 return;
351
352 uint8_t digest[16];
353 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
354 expected_stride * expected_height, digest);
355 EXPECT_EQ(expected_md5sum, CRYPT_ToBase16(digest));
356}
357
Tom Sepez1b1bb492015-01-22 17:36:32 -0800358// Can't use gtest-provided main since we need to stash the path to the
359// executable in order to find the external V8 binary data files.
360int main(int argc, char** argv) {
thestigc08cd7a2016-06-27 09:47:59 -0700361 g_exe_path = argv[0];
Tom Sepez1b1bb492015-01-22 17:36:32 -0800362 testing::InitGoogleTest(&argc, argv);
Tom Sepeza310e002015-02-27 13:03:07 -0800363 testing::InitGoogleMock(&argc, argv);
thestigc08cd7a2016-06-27 09:47:59 -0700364 int ret_val = RUN_ALL_TESTS();
365
366#ifdef PDF_ENABLE_V8
367#ifdef V8_USE_EXTERNAL_STARTUP_DATA
368 if (g_v8_natives)
369 free(const_cast<char*>(g_v8_natives->data));
370 if (g_v8_snapshot)
371 free(const_cast<char*>(g_v8_snapshot->data));
372#endif // V8_USE_EXTERNAL_STARTUP_DATA
373#endif // PDF_ENABLE_V8
374
375 return ret_val;
Tom Sepez1b1bb492015-01-22 17:36:32 -0800376}