blob: 1003cb51870fb31a8a4bb5e95e11b5ee829f7d0c [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 Sepez96d13342015-01-16 14:59:26 -080017#include "../core/include/fxcrt/fx_system.h"
Tom Sepez1ed8a212015-05-11 15:25:39 -070018#include "../public/fpdf_text.h"
19#include "../public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080020#include "testing/gmock/include/gmock/gmock.h"
Tom Sepez2785fb42015-03-03 09:49:29 -080021#include "v8/include/libplatform/libplatform.h"
Tom Sepez6efc0ad2015-06-02 17:11:18 -070022#include "v8/include/v8.h"
Tom Sepez96d13342015-01-16 14:59:26 -080023
24#ifdef _WIN32
25#define snprintf _snprintf
26#define PATH_SEPARATOR '\\'
27#else
28#define PATH_SEPARATOR '/'
29#endif
30
31namespace {
32
Tom Sepez1b1bb492015-01-22 17:36:32 -080033const char* g_exe_path_ = nullptr;
34
Tom Sepez96d13342015-01-16 14:59:26 -080035// Reads the entire contents of a file into a newly malloc'd buffer.
36static char* GetFileContents(const char* filename, size_t* retlen) {
37 FILE* file = fopen(filename, "rb");
38 if (!file) {
39 fprintf(stderr, "Failed to open: %s\n", filename);
Lei Zhangd27acae2015-05-15 15:36:02 -070040 return nullptr;
Tom Sepez96d13342015-01-16 14:59:26 -080041 }
42 (void) fseek(file, 0, SEEK_END);
43 size_t file_length = ftell(file);
44 if (!file_length) {
Lei Zhangd27acae2015-05-15 15:36:02 -070045 return nullptr;
Tom Sepez96d13342015-01-16 14:59:26 -080046 }
47 (void) fseek(file, 0, SEEK_SET);
48 char* buffer = (char*) malloc(file_length);
49 if (!buffer) {
Lei Zhangd27acae2015-05-15 15:36:02 -070050 return nullptr;
Tom Sepez96d13342015-01-16 14:59:26 -080051 }
52 size_t bytes_read = fread(buffer, 1, file_length, file);
53 (void) fclose(file);
54 if (bytes_read != file_length) {
55 fprintf(stderr, "Failed to read: %s\n", filename);
56 free(buffer);
Lei Zhangd27acae2015-05-15 15:36:02 -070057 return nullptr;
Tom Sepez96d13342015-01-16 14:59:26 -080058 }
59 *retlen = bytes_read;
60 return buffer;
61}
62
63#ifdef V8_USE_EXTERNAL_STARTUP_DATA
64// Returns the full path for an external V8 data file based on either
65// the currect exectuable path or an explicit override.
Tom Sepez1b1bb492015-01-22 17:36:32 -080066static std::string GetFullPathForSnapshotFile(const std::string& exe_path,
Tom Sepez96d13342015-01-16 14:59:26 -080067 const std::string& filename) {
68 std::string result;
Tom Sepez1b1bb492015-01-22 17:36:32 -080069 if (!exe_path.empty()) {
70 size_t last_separator = exe_path.rfind(PATH_SEPARATOR);
Tom Sepez96d13342015-01-16 14:59:26 -080071 if (last_separator != std::string::npos) {
Tom Sepez1b1bb492015-01-22 17:36:32 -080072 result = exe_path.substr(0, last_separator + 1);
Tom Sepez96d13342015-01-16 14:59:26 -080073 }
74 }
75 result += filename;
76 return result;
77}
78
79// Reads an extenal V8 data file from the |options|-indicated location,
80// returing true on success and false on error.
Tom Sepez1b1bb492015-01-22 17:36:32 -080081static bool GetExternalData(const std::string& exe_path,
82 const std::string& filename,
Tom Sepez96d13342015-01-16 14:59:26 -080083 v8::StartupData* result_data) {
Tom Sepez1b1bb492015-01-22 17:36:32 -080084 std::string full_path = GetFullPathForSnapshotFile(exe_path, filename);
Tom Sepez96d13342015-01-16 14:59:26 -080085 size_t data_length = 0;
86 char* data_buffer = GetFileContents(full_path.c_str(), &data_length);
87 if (!data_buffer) {
88 return false;
89 }
90 result_data->data = const_cast<const char*>(data_buffer);
91 result_data->raw_size = data_length;
92 return true;
93}
94#endif // V8_USE_EXTERNAL_STARTUP_DATA
95
96} // namespace
97
Tom Sepez96d13342015-01-16 14:59:26 -080098class TestLoader {
99 public:
100 TestLoader(const char* pBuf, size_t len);
101
102 const char* m_pBuf;
103 size_t m_Len;
104};
105
106TestLoader::TestLoader(const char* pBuf, size_t len)
107 : m_pBuf(pBuf), m_Len(len) {
108}
109
110int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
111 unsigned long size) {
112 TestLoader* pLoader = (TestLoader*) param;
113 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
114 memcpy(pBuf, pLoader->m_pBuf + pos, size);
115 return 1;
116}
117
Tom Sepezcf22eb82015-05-12 17:28:08 -0700118FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
Tom Sepez96d13342015-01-16 14:59:26 -0800119 return true;
120}
121
122void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
123}
124
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800125EmbedderTest::EmbedderTest() :
126 document_(nullptr),
127 form_handle_(nullptr),
128 avail_(nullptr),
129 loader_(nullptr),
130 file_length_(0),
131 file_contents_(nullptr) {
132 memset(&hints_, 0, sizeof(hints_));
133 memset(&file_access_, 0, sizeof(file_access_));
134 memset(&file_avail_, 0, sizeof(file_avail_));
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700135 default_delegate_ = new EmbedderTest::Delegate();
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800136 delegate_ = default_delegate_;
137}
138
139EmbedderTest::~EmbedderTest() {
140 delete default_delegate_;
141}
142
Tom Sepez96d13342015-01-16 14:59:26 -0800143void EmbedderTest::SetUp() {
144 v8::V8::InitializeICU();
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700145
146 platform_ = v8::platform::CreateDefaultPlatform();
147 v8::V8::InitializePlatform(platform_);
148 v8::V8::Initialize();
149
150 // By enabling predictable mode, V8 won't post any background tasks.
151 const char predictable_flag[] = "--predictable";
152 v8::V8::SetFlagsFromString(predictable_flag,
153 static_cast<int>(strlen(predictable_flag)));
Tom Sepez96d13342015-01-16 14:59:26 -0800154
155#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez1b1bb492015-01-22 17:36:32 -0800156 ASSERT_TRUE(GetExternalData(g_exe_path_, "natives_blob.bin", &natives_));
157 ASSERT_TRUE(GetExternalData(g_exe_path_, "snapshot_blob.bin", &snapshot_));
158 v8::V8::SetNativesDataBlob(&natives_);
159 v8::V8::SetSnapshotDataBlob(&snapshot_);
Tom Sepez96d13342015-01-16 14:59:26 -0800160#endif // V8_USE_EXTERNAL_STARTUP_DATA
161
162 FPDF_InitLibrary();
163
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800164 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
165 memset(info, 0, sizeof(UNSUPPORT_INFO));
166 info->version = 1;
167 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
168 FSDK_SetUnSpObjProcessHandler(info);
Tom Sepez96d13342015-01-16 14:59:26 -0800169 }
170
171void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -0800172 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700173 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepezda8189e2015-01-30 14:41:50 -0800174 FPDF_CloseDocument(document_);
Lei Zhangd27acae2015-05-15 15:36:02 -0700175 FPDFDOC_ExitFormFillEnvironment(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800176 }
Tom Sepez96d13342015-01-16 14:59:26 -0800177 FPDFAvail_Destroy(avail_);
178 FPDF_DestroyLibrary();
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700179 v8::V8::ShutdownPlatform();
180 delete platform_;
Lei Zhangd27acae2015-05-15 15:36:02 -0700181 delete loader_;
182 free(file_contents_);
Tom Sepez96d13342015-01-16 14:59:26 -0800183}
184
185bool EmbedderTest::OpenDocument(const std::string& filename) {
186 file_contents_ = GetFileContents(filename.c_str(), &file_length_);
187 if (!file_contents_) {
188 return false;
189 }
190
191 loader_ = new TestLoader(file_contents_, file_length_);
192 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
193 file_access_.m_GetBlock = Get_Block;
194 file_access_.m_Param = loader_;
195
196 file_avail_.version = 1;
197 file_avail_.IsDataAvail = Is_Data_Avail;
198
199 hints_.version = 1;
200 hints_.AddSegment = Add_Segment;
201
202 avail_ = FPDFAvail_Create(&file_avail_, &file_access_);
203 (void) FPDFAvail_IsDocAvail(avail_, &hints_);
204
205 if (!FPDFAvail_IsLinearized(avail_)) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700206 document_ = FPDF_LoadCustomDocument(&file_access_, nullptr);
Tom Sepez96d13342015-01-16 14:59:26 -0800207 } else {
Lei Zhangd27acae2015-05-15 15:36:02 -0700208 document_ = FPDFAvail_GetDocument(avail_, nullptr);
Tom Sepez96d13342015-01-16 14:59:26 -0800209 }
210 if (!document_) {
211 return false;
212 }
JUN FANG827a1722015-03-05 13:39:21 -0800213 int docType = DOCTYPE_PDF;
Tom Sepezcf22eb82015-05-12 17:28:08 -0700214 if (FPDF_HasXFAField(document_, &docType))
JUN FANG827a1722015-03-05 13:39:21 -0800215 {
216 if (docType != DOCTYPE_PDF)
217 (void) FPDF_LoadXFA(document_);
218 }
Tom Sepez96d13342015-01-16 14:59:26 -0800219 (void) FPDF_GetDocPermissions(document_);
220 (void) FPDFAvail_IsFormAvail(avail_, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800221
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800222 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
223 memset(platform, 0, sizeof(IPDF_JSPLATFORM));
224 platform->version = 1;
225 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800226
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800227 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
228 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
229 formfillinfo->version = 1;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700230 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
231 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800232 formfillinfo->m_pJsPlatform = platform;
Tom Sepez96d13342015-01-16 14:59:26 -0800233
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800234 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
Tom Sepezda8189e2015-01-30 14:41:50 -0800235 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
236 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
237
238 return true;
Tom Sepez96d13342015-01-16 14:59:26 -0800239}
240
Tom Sepezda8189e2015-01-30 14:41:50 -0800241void EmbedderTest::DoOpenActions() {
242 FORM_DoDocumentJSAction(form_handle_);
243 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800244}
245
246int EmbedderTest::GetFirstPageNum() {
247 int first_page = FPDFAvail_GetFirstPageNum(document_);
248 (void) FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
249 return first_page;
250}
251
252int EmbedderTest::GetPageCount() {
253 int page_count = FPDF_GetPageCount(document_);
254 for (int i = 0; i < page_count; ++i) {
255 (void) FPDFAvail_IsPageAvail(avail_, i, &hints_);
256 }
257 return page_count;
258}
259
Tom Sepezda8189e2015-01-30 14:41:50 -0800260FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez96d13342015-01-16 14:59:26 -0800261 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
262 if (!page) {
263 return nullptr;
264 }
Tom Sepezda8189e2015-01-30 14:41:50 -0800265 FORM_OnAfterLoadPage(page, form_handle_);
266 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
Tom Sepez96d13342015-01-16 14:59:26 -0800267 return page;
268}
269
Tom Sepezda8189e2015-01-30 14:41:50 -0800270FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Tom Sepez96d13342015-01-16 14:59:26 -0800271 int width = static_cast<int>(FPDF_GetPageWidth(page));
272 int height = static_cast<int>(FPDF_GetPageHeight(page));
273 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
274 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
275 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepezda8189e2015-01-30 14:41:50 -0800276 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepez96d13342015-01-16 14:59:26 -0800277 return bitmap;
278}
279
Tom Sepezda8189e2015-01-30 14:41:50 -0800280void EmbedderTest::UnloadPage(FPDF_PAGE page) {
281 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
282 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800283 FPDF_ClosePage(page);
284}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800285
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800286// static
287void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
288 int type) {
289 EmbedderTest* test = static_cast<EmbedderTest*>(info);
290 test->delegate_->UnsupportedHandler(type);
291}
292
293// static
294int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
295 FPDF_WIDESTRING message,
296 FPDF_WIDESTRING title,
297 int type,
298 int icon) {
299 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
300 return test->delegate_->Alert(message, title, type, icon);
301}
302
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700303// static
304int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
305 int msecs, TimerCallback fn) {
306 EmbedderTest* test = static_cast<EmbedderTest*>(info);
307 return test->delegate_->SetTimer(msecs, fn);
308}
309
310// static
311void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
312 EmbedderTest* test = static_cast<EmbedderTest*>(info);
313 return test->delegate_->KillTimer(id);
314}
315
Tom Sepez1b1bb492015-01-22 17:36:32 -0800316// Can't use gtest-provided main since we need to stash the path to the
317// executable in order to find the external V8 binary data files.
318int main(int argc, char** argv) {
319 g_exe_path_ = argv[0];
320 testing::InitGoogleTest(&argc, argv);
Tom Sepeza310e002015-02-27 13:03:07 -0800321 testing::InitGoogleMock(&argc, argv);
Tom Sepez1b1bb492015-01-22 17:36:32 -0800322 return RUN_ALL_TESTS();
323}