blob: 0aacd48df9acad173c24b7a1777bff0b5c7b98ef [file] [log] [blame]
Tom Sepez96d13342015-01-16 14:59:26 -08001// Copyright (c) 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 "embedder_test.h"
6
7#include <limits.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include <list>
13#include <string>
14#include <utility>
15#include <vector>
16
Tom Sepez1ed8a212015-05-11 15:25:39 -070017#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"
Tom Sepez452b4f32015-10-13 09:27:27 -070020
21#ifdef PDF_ENABLE_V8
Tom Sepez2785fb42015-03-03 09:49:29 -080022#include "v8/include/libplatform/libplatform.h"
Tom Sepez6efc0ad2015-06-02 17:11:18 -070023#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070024#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080025
26#ifdef _WIN32
27#define snprintf _snprintf
28#define PATH_SEPARATOR '\\'
29#else
30#define PATH_SEPARATOR '/'
31#endif
32
33namespace {
34
Tom Sepez1b1bb492015-01-22 17:36:32 -080035const char* g_exe_path_ = nullptr;
36
Tom Sepez96d13342015-01-16 14:59:26 -080037// Reads the entire contents of a file into a newly malloc'd buffer.
38static char* GetFileContents(const char* filename, size_t* retlen) {
39 FILE* file = fopen(filename, "rb");
40 if (!file) {
41 fprintf(stderr, "Failed to open: %s\n", filename);
Lei Zhangd27acae2015-05-15 15:36:02 -070042 return nullptr;
Tom Sepez96d13342015-01-16 14:59:26 -080043 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 (void)fseek(file, 0, SEEK_END);
Tom Sepez96d13342015-01-16 14:59:26 -080045 size_t file_length = ftell(file);
46 if (!file_length) {
Lei Zhangd27acae2015-05-15 15:36:02 -070047 return nullptr;
Tom Sepez96d13342015-01-16 14:59:26 -080048 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 (void)fseek(file, 0, SEEK_SET);
50 char* buffer = (char*)malloc(file_length);
Tom Sepez96d13342015-01-16 14:59:26 -080051 if (!buffer) {
Lei Zhangd27acae2015-05-15 15:36:02 -070052 return nullptr;
Tom Sepez96d13342015-01-16 14:59:26 -080053 }
54 size_t bytes_read = fread(buffer, 1, file_length, file);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 (void)fclose(file);
Tom Sepez96d13342015-01-16 14:59:26 -080056 if (bytes_read != file_length) {
57 fprintf(stderr, "Failed to read: %s\n", filename);
58 free(buffer);
Lei Zhangd27acae2015-05-15 15:36:02 -070059 return nullptr;
Tom Sepez96d13342015-01-16 14:59:26 -080060 }
61 *retlen = bytes_read;
62 return buffer;
63}
64
Tom Sepez452b4f32015-10-13 09:27:27 -070065#ifdef PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080066#ifdef V8_USE_EXTERNAL_STARTUP_DATA
67// Returns the full path for an external V8 data file based on either
68// the currect exectuable path or an explicit override.
Tom Sepez1b1bb492015-01-22 17:36:32 -080069static std::string GetFullPathForSnapshotFile(const std::string& exe_path,
Tom Sepez96d13342015-01-16 14:59:26 -080070 const std::string& filename) {
71 std::string result;
Tom Sepez1b1bb492015-01-22 17:36:32 -080072 if (!exe_path.empty()) {
73 size_t last_separator = exe_path.rfind(PATH_SEPARATOR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 if (last_separator != std::string::npos) {
Tom Sepez1b1bb492015-01-22 17:36:32 -080075 result = exe_path.substr(0, last_separator + 1);
Tom Sepez96d13342015-01-16 14:59:26 -080076 }
77 }
78 result += filename;
79 return result;
80}
81
82// Reads an extenal V8 data file from the |options|-indicated location,
83// returing true on success and false on error.
Tom Sepez1b1bb492015-01-22 17:36:32 -080084static bool GetExternalData(const std::string& exe_path,
85 const std::string& filename,
Tom Sepez96d13342015-01-16 14:59:26 -080086 v8::StartupData* result_data) {
Tom Sepez1b1bb492015-01-22 17:36:32 -080087 std::string full_path = GetFullPathForSnapshotFile(exe_path, filename);
Tom Sepez96d13342015-01-16 14:59:26 -080088 size_t data_length = 0;
89 char* data_buffer = GetFileContents(full_path.c_str(), &data_length);
90 if (!data_buffer) {
91 return false;
92 }
93 result_data->data = const_cast<const char*>(data_buffer);
94 result_data->raw_size = data_length;
95 return true;
96}
97#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -070098#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080099} // namespace
100
Tom Sepez96d13342015-01-16 14:59:26 -0800101class TestLoader {
102 public:
103 TestLoader(const char* pBuf, size_t len);
104
105 const char* m_pBuf;
106 size_t m_Len;
107};
108
109TestLoader::TestLoader(const char* pBuf, size_t len)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 : m_pBuf(pBuf), m_Len(len) {}
Tom Sepez96d13342015-01-16 14:59:26 -0800111
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112int Get_Block(void* param,
113 unsigned long pos,
114 unsigned char* pBuf,
Tom Sepez96d13342015-01-16 14:59:26 -0800115 unsigned long size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 TestLoader* pLoader = (TestLoader*)param;
117 if (pos + size < pos || pos + size > pLoader->m_Len)
118 return 0;
Tom Sepez96d13342015-01-16 14:59:26 -0800119 memcpy(pBuf, pLoader->m_pBuf + pos, size);
120 return 1;
121}
122
Tom Sepezcf22eb82015-05-12 17:28:08 -0700123FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
Tom Sepez96d13342015-01-16 14:59:26 -0800124 return true;
125}
126
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {}
Tom Sepez96d13342015-01-16 14:59:26 -0800128
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129EmbedderTest::EmbedderTest()
Tom Sepeza72e8e22015-10-07 10:17:53 -0700130 : default_delegate_(new EmbedderTest::Delegate()),
131 document_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800132 form_handle_(nullptr),
133 avail_(nullptr),
Tom Sepeza72e8e22015-10-07 10:17:53 -0700134 external_isolate_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800135 loader_(nullptr),
136 file_length_(0),
137 file_contents_(nullptr) {
138 memset(&hints_, 0, sizeof(hints_));
139 memset(&file_access_, 0, sizeof(file_access_));
140 memset(&file_avail_, 0, sizeof(file_avail_));
Tom Sepeza72e8e22015-10-07 10:17:53 -0700141 delegate_ = default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800142}
143
144EmbedderTest::~EmbedderTest() {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800145}
146
Tom Sepez96d13342015-01-16 14:59:26 -0800147void EmbedderTest::SetUp() {
Tom Sepez452b4f32015-10-13 09:27:27 -0700148#ifdef PDF_ENABLE_V8
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149 v8::V8::InitializeICU();
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700150
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 platform_ = v8::platform::CreateDefaultPlatform();
152 v8::V8::InitializePlatform(platform_);
153 v8::V8::Initialize();
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700154
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155 // By enabling predictable mode, V8 won't post any background tasks.
156 const char predictable_flag[] = "--predictable";
157 v8::V8::SetFlagsFromString(predictable_flag,
158 static_cast<int>(strlen(predictable_flag)));
Tom Sepez96d13342015-01-16 14:59:26 -0800159
160#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 ASSERT_TRUE(GetExternalData(g_exe_path_, "natives_blob.bin", &natives_));
162 ASSERT_TRUE(GetExternalData(g_exe_path_, "snapshot_blob.bin", &snapshot_));
163 v8::V8::SetNativesDataBlob(&natives_);
164 v8::V8::SetSnapshotDataBlob(&snapshot_);
Tom Sepez96d13342015-01-16 14:59:26 -0800165#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700166#endif // FPDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -0800167
Tom Sepeza72e8e22015-10-07 10:17:53 -0700168 FPDF_LIBRARY_CONFIG config;
169 config.version = 2;
170 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700171 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -0700172 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700173 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -0800174
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
176 memset(info, 0, sizeof(UNSUPPORT_INFO));
177 info->version = 1;
178 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
179 FSDK_SetUnSpObjProcessHandler(info);
180}
Tom Sepez96d13342015-01-16 14:59:26 -0800181
182void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -0800183 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700184 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -0700185
186 // Note: The shut down order here is the reverse of the non-XFA branch
187 // order. Need to work out if this is required, and if it is, the lifetimes
188 // of objects owned by |doc| that |form| reference.
Tom Sepezda8189e2015-01-30 14:41:50 -0800189 FPDF_CloseDocument(document_);
Lei Zhangd27acae2015-05-15 15:36:02 -0700190 FPDFDOC_ExitFormFillEnvironment(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800191 }
Tom Sepez96d13342015-01-16 14:59:26 -0800192 FPDFAvail_Destroy(avail_);
193 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -0700194
195#ifdef PDF_ENABLE_V8
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700196 v8::V8::ShutdownPlatform();
197 delete platform_;
Tom Sepez452b4f32015-10-13 09:27:27 -0700198#endif // PDF_ENABLE_V8
199
Lei Zhangd27acae2015-05-15 15:36:02 -0700200 delete loader_;
201 free(file_contents_);
Tom Sepez96d13342015-01-16 14:59:26 -0800202}
203
204bool EmbedderTest::OpenDocument(const std::string& filename) {
205 file_contents_ = GetFileContents(filename.c_str(), &file_length_);
206 if (!file_contents_) {
207 return false;
208 }
209
210 loader_ = new TestLoader(file_contents_, file_length_);
211 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
212 file_access_.m_GetBlock = Get_Block;
213 file_access_.m_Param = loader_;
214
215 file_avail_.version = 1;
216 file_avail_.IsDataAvail = Is_Data_Avail;
217
218 hints_.version = 1;
219 hints_.AddSegment = Add_Segment;
220
221 avail_ = FPDFAvail_Create(&file_avail_, &file_access_);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 (void)FPDFAvail_IsDocAvail(avail_, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800223
224 if (!FPDFAvail_IsLinearized(avail_)) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700225 document_ = FPDF_LoadCustomDocument(&file_access_, nullptr);
Tom Sepez96d13342015-01-16 14:59:26 -0800226 } else {
Lei Zhangd27acae2015-05-15 15:36:02 -0700227 document_ = FPDFAvail_GetDocument(avail_, nullptr);
Tom Sepez96d13342015-01-16 14:59:26 -0800228 }
229 if (!document_) {
230 return false;
231 }
JUN FANG827a1722015-03-05 13:39:21 -0800232 int docType = DOCTYPE_PDF;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 if (FPDF_HasXFAField(document_, &docType)) {
JUN FANG827a1722015-03-05 13:39:21 -0800234 if (docType != DOCTYPE_PDF)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 (void)FPDF_LoadXFA(document_);
JUN FANG827a1722015-03-05 13:39:21 -0800236 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 (void)FPDF_GetDocPermissions(document_);
238 (void)FPDFAvail_IsFormAvail(avail_, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800239
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800240 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
241 memset(platform, 0, sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200242 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800243 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800244
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800245 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
246 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
247 formfillinfo->version = 1;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700248 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
249 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700250 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800251 formfillinfo->m_pJsPlatform = platform;
Tom Sepez96d13342015-01-16 14:59:26 -0800252
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800253 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
Tom Sepezda8189e2015-01-30 14:41:50 -0800254 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
255 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
256
257 return true;
Tom Sepez96d13342015-01-16 14:59:26 -0800258}
259
Tom Sepezda8189e2015-01-30 14:41:50 -0800260void EmbedderTest::DoOpenActions() {
261 FORM_DoDocumentJSAction(form_handle_);
262 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800263}
264
265int EmbedderTest::GetFirstPageNum() {
266 int first_page = FPDFAvail_GetFirstPageNum(document_);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 (void)FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800268 return first_page;
269}
270
271int EmbedderTest::GetPageCount() {
272 int page_count = FPDF_GetPageCount(document_);
273 for (int i = 0; i < page_count; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 (void)FPDFAvail_IsPageAvail(avail_, i, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800275 }
276 return page_count;
277}
278
Tom Sepezda8189e2015-01-30 14:41:50 -0800279FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez96d13342015-01-16 14:59:26 -0800280 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
281 if (!page) {
282 return nullptr;
283 }
Tom Sepezda8189e2015-01-30 14:41:50 -0800284 FORM_OnAfterLoadPage(page, form_handle_);
285 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
Tom Sepez96d13342015-01-16 14:59:26 -0800286 return page;
287}
288
Tom Sepez396e8722015-09-09 10:16:08 -0700289FPDF_PAGE EmbedderTest::LoadAndCachePage(int page_number) {
290 FPDF_PAGE page = delegate_->GetPage(form_handle_, document_, page_number);
291 if (!page) {
292 return nullptr;
293 }
294 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
295 return page;
296}
297
Tom Sepezda8189e2015-01-30 14:41:50 -0800298FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Tom Sepez96d13342015-01-16 14:59:26 -0800299 int width = static_cast<int>(FPDF_GetPageWidth(page));
300 int height = static_cast<int>(FPDF_GetPageHeight(page));
301 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
302 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
303 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepezda8189e2015-01-30 14:41:50 -0800304 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepez96d13342015-01-16 14:59:26 -0800305 return bitmap;
306}
307
Tom Sepezda8189e2015-01-30 14:41:50 -0800308void EmbedderTest::UnloadPage(FPDF_PAGE page) {
309 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
310 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800311 FPDF_ClosePage(page);
312}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800313
Tom Sepez396e8722015-09-09 10:16:08 -0700314FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMHANDLE form_handle,
315 FPDF_DOCUMENT document,
316 int page_index) {
317 auto it = m_pageMap.find(page_index);
318 if (it != m_pageMap.end()) {
319 return it->second;
320 }
321 FPDF_PAGE page = FPDF_LoadPage(document, page_index);
322 if (!page) {
323 return nullptr;
324 }
325 m_pageMap[page_index] = page;
326 FORM_OnAfterLoadPage(page, form_handle);
327 return page;
328}
329
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800330// static
331void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
332 int type) {
333 EmbedderTest* test = static_cast<EmbedderTest*>(info);
334 test->delegate_->UnsupportedHandler(type);
335}
336
337// static
338int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
339 FPDF_WIDESTRING message,
340 FPDF_WIDESTRING title,
341 int type,
342 int icon) {
343 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
344 return test->delegate_->Alert(message, title, type, icon);
345}
346
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700347// static
348int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 int msecs,
350 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700351 EmbedderTest* test = static_cast<EmbedderTest*>(info);
352 return test->delegate_->SetTimer(msecs, fn);
353}
354
355// static
356void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
357 EmbedderTest* test = static_cast<EmbedderTest*>(info);
358 return test->delegate_->KillTimer(id);
359}
360
Tom Sepez396e8722015-09-09 10:16:08 -0700361// static
362FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
363 FPDF_DOCUMENT document,
364 int page_index) {
365 EmbedderTest* test = static_cast<EmbedderTest*>(info);
Tom Sepez436977e2015-10-02 09:16:40 -0700366 return test->delegate_->GetPage(test->form_handle(), document, page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700367}
368
Tom Sepez1b1bb492015-01-22 17:36:32 -0800369// Can't use gtest-provided main since we need to stash the path to the
370// executable in order to find the external V8 binary data files.
371int main(int argc, char** argv) {
372 g_exe_path_ = argv[0];
373 testing::InitGoogleTest(&argc, argv);
Tom Sepeza310e002015-02-27 13:03:07 -0800374 testing::InitGoogleMock(&argc, argv);
Tom Sepez1b1bb492015-01-22 17:36:32 -0800375 return RUN_ALL_TESTS();
376}