blob: cf0bdf892c5dc01613b8ecbb500fbe316dc77657 [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 "../fpdfsdk/include/fpdftext.h"
18#include "../fpdfsdk/include/fpdfview.h"
19#include "../core/include/fxcrt/fx_system.h"
Tom Sepeza310e002015-02-27 13:03:07 -080020#include "testing/gmock/include/gmock/gmock.h"
Tom Sepez96d13342015-01-16 14:59:26 -080021#include "v8/include/v8.h"
Tom Sepez2785fb42015-03-03 09:49:29 -080022#include "v8/include/libplatform/libplatform.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);
40 return NULL;
41 }
42 (void) fseek(file, 0, SEEK_END);
43 size_t file_length = ftell(file);
44 if (!file_length) {
45 return NULL;
46 }
47 (void) fseek(file, 0, SEEK_SET);
48 char* buffer = (char*) malloc(file_length);
49 if (!buffer) {
50 return NULL;
51 }
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);
57 return NULL;
58 }
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 Sepez4cb0fa72015-02-25 16:08:18 -080098class EmbedderTestDefaultDelegate : public EmbedderTest::Delegate {
99 public:
100 int Alert(FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) override {
101 printf("Form_Alert called.\n");
102 return 0;
Tom Sepez96d13342015-01-16 14:59:26 -0800103 }
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800104
105 void UnsupportedHandler(int type) {
106 std::string feature = "Unknown";
107 switch (type) {
108 case FPDF_UNSP_DOC_XFAFORM:
109 feature = "XFA";
110 break;
111 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
112 feature = "Portfolios_Packages";
113 break;
114 case FPDF_UNSP_DOC_ATTACHMENT:
115 case FPDF_UNSP_ANNOT_ATTACHMENT:
116 feature = "Attachment";
117 break;
118 case FPDF_UNSP_DOC_SECURITY:
119 feature = "Rights_Management";
120 break;
121 case FPDF_UNSP_DOC_SHAREDREVIEW:
122 feature = "Shared_Review";
123 break;
124 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
125 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
126 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
127 feature = "Shared_Form";
128 break;
129 case FPDF_UNSP_ANNOT_3DANNOT:
130 feature = "3D";
131 break;
132 case FPDF_UNSP_ANNOT_MOVIE:
133 feature = "Movie";
134 break;
135 case FPDF_UNSP_ANNOT_SOUND:
136 feature = "Sound";
137 break;
138 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
139 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
140 feature = "Screen";
141 break;
142 case FPDF_UNSP_ANNOT_SIG:
143 feature = "Digital_Signature";
144 break;
145 }
146 printf("Unsupported feature: %s.\n", feature.c_str());
147 }
148};
Tom Sepez96d13342015-01-16 14:59:26 -0800149
150class TestLoader {
151 public:
152 TestLoader(const char* pBuf, size_t len);
153
154 const char* m_pBuf;
155 size_t m_Len;
156};
157
158TestLoader::TestLoader(const char* pBuf, size_t len)
159 : m_pBuf(pBuf), m_Len(len) {
160}
161
162int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
163 unsigned long size) {
164 TestLoader* pLoader = (TestLoader*) param;
165 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
166 memcpy(pBuf, pLoader->m_pBuf + pos, size);
167 return 1;
168}
169
170bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
171 return true;
172}
173
174void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
175}
176
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800177EmbedderTest::EmbedderTest() :
178 document_(nullptr),
179 form_handle_(nullptr),
180 avail_(nullptr),
181 loader_(nullptr),
182 file_length_(0),
183 file_contents_(nullptr) {
184 memset(&hints_, 0, sizeof(hints_));
185 memset(&file_access_, 0, sizeof(file_access_));
186 memset(&file_avail_, 0, sizeof(file_avail_));
187 default_delegate_ = new EmbedderTestDefaultDelegate();
188 delegate_ = default_delegate_;
189}
190
191EmbedderTest::~EmbedderTest() {
192 delete default_delegate_;
193}
194
Tom Sepez96d13342015-01-16 14:59:26 -0800195void EmbedderTest::SetUp() {
196 v8::V8::InitializeICU();
Tom Sepez2785fb42015-03-03 09:49:29 -0800197 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
198 v8::V8::InitializePlatform(platform);
199 v8::V8::Initialize();
Tom Sepez96d13342015-01-16 14:59:26 -0800200
201#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez1b1bb492015-01-22 17:36:32 -0800202 ASSERT_TRUE(GetExternalData(g_exe_path_, "natives_blob.bin", &natives_));
203 ASSERT_TRUE(GetExternalData(g_exe_path_, "snapshot_blob.bin", &snapshot_));
204 v8::V8::SetNativesDataBlob(&natives_);
205 v8::V8::SetSnapshotDataBlob(&snapshot_);
Tom Sepez96d13342015-01-16 14:59:26 -0800206#endif // V8_USE_EXTERNAL_STARTUP_DATA
207
208 FPDF_InitLibrary();
209
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800210 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
211 memset(info, 0, sizeof(UNSUPPORT_INFO));
212 info->version = 1;
213 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
214 FSDK_SetUnSpObjProcessHandler(info);
Tom Sepez96d13342015-01-16 14:59:26 -0800215 }
216
217void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -0800218 if (form_handle_) {
219 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
220 FPDFDOC_ExitFormFillEnvironment(form_handle_);
221 }
222 if (document_) {
223 FPDF_CloseDocument(document_);
224 }
Tom Sepez96d13342015-01-16 14:59:26 -0800225 FPDFAvail_Destroy(avail_);
226 FPDF_DestroyLibrary();
227 if (loader_) {
228 delete loader_;
229 }
230 if (file_contents_) {
231 free(file_contents_);
232 }
Tom Sepez2785fb42015-03-03 09:49:29 -0800233 v8::V8::ShutdownPlatform();
Tom Sepez96d13342015-01-16 14:59:26 -0800234}
235
236bool EmbedderTest::OpenDocument(const std::string& filename) {
237 file_contents_ = GetFileContents(filename.c_str(), &file_length_);
238 if (!file_contents_) {
239 return false;
240 }
241
242 loader_ = new TestLoader(file_contents_, file_length_);
243 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
244 file_access_.m_GetBlock = Get_Block;
245 file_access_.m_Param = loader_;
246
247 file_avail_.version = 1;
248 file_avail_.IsDataAvail = Is_Data_Avail;
249
250 hints_.version = 1;
251 hints_.AddSegment = Add_Segment;
252
253 avail_ = FPDFAvail_Create(&file_avail_, &file_access_);
254 (void) FPDFAvail_IsDocAvail(avail_, &hints_);
255
256 if (!FPDFAvail_IsLinearized(avail_)) {
257 document_ = FPDF_LoadCustomDocument(&file_access_, NULL);
258 } else {
259 document_ = FPDFAvail_GetDocument(avail_, NULL);
260 }
261 if (!document_) {
262 return false;
263 }
264 (void) FPDF_LoadXFA(document_);
265 (void) FPDF_GetDocPermissions(document_);
266 (void) FPDFAvail_IsFormAvail(avail_, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800267
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800268 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
269 memset(platform, 0, sizeof(IPDF_JSPLATFORM));
270 platform->version = 1;
271 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800272
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800273 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
274 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
275 formfillinfo->version = 1;
276 formfillinfo->m_pJsPlatform = platform;
Tom Sepez96d13342015-01-16 14:59:26 -0800277
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800278 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
Tom Sepezda8189e2015-01-30 14:41:50 -0800279 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
280 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
281
282 return true;
Tom Sepez96d13342015-01-16 14:59:26 -0800283}
284
Tom Sepezda8189e2015-01-30 14:41:50 -0800285void EmbedderTest::DoOpenActions() {
286 FORM_DoDocumentJSAction(form_handle_);
287 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800288}
289
290int EmbedderTest::GetFirstPageNum() {
291 int first_page = FPDFAvail_GetFirstPageNum(document_);
292 (void) FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
293 return first_page;
294}
295
296int EmbedderTest::GetPageCount() {
297 int page_count = FPDF_GetPageCount(document_);
298 for (int i = 0; i < page_count; ++i) {
299 (void) FPDFAvail_IsPageAvail(avail_, i, &hints_);
300 }
301 return page_count;
302}
303
Tom Sepezda8189e2015-01-30 14:41:50 -0800304FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez96d13342015-01-16 14:59:26 -0800305 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
306 if (!page) {
307 return nullptr;
308 }
Tom Sepezda8189e2015-01-30 14:41:50 -0800309 FORM_OnAfterLoadPage(page, form_handle_);
310 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
Tom Sepez96d13342015-01-16 14:59:26 -0800311 return page;
312}
313
Tom Sepezda8189e2015-01-30 14:41:50 -0800314FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Tom Sepez96d13342015-01-16 14:59:26 -0800315 int width = static_cast<int>(FPDF_GetPageWidth(page));
316 int height = static_cast<int>(FPDF_GetPageHeight(page));
317 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
318 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
319 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepezda8189e2015-01-30 14:41:50 -0800320 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepez96d13342015-01-16 14:59:26 -0800321 return bitmap;
322}
323
Tom Sepezda8189e2015-01-30 14:41:50 -0800324void EmbedderTest::UnloadPage(FPDF_PAGE page) {
325 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
326 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800327 FPDF_ClosePage(page);
328}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800329
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 Sepez1b1bb492015-01-22 17:36:32 -0800347// Can't use gtest-provided main since we need to stash the path to the
348// executable in order to find the external V8 binary data files.
349int main(int argc, char** argv) {
350 g_exe_path_ = argv[0];
351 testing::InitGoogleTest(&argc, argv);
Tom Sepeza310e002015-02-27 13:03:07 -0800352 testing::InitGoogleMock(&argc, argv);
Tom Sepez1b1bb492015-01-22 17:36:32 -0800353 return RUN_ALL_TESTS();
354}