blob: b971e6dae1ea376bfdc51c759707dfb8f02566ce [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"
20#include "v8/include/v8.h"
21
22#ifdef _WIN32
23#define snprintf _snprintf
24#define PATH_SEPARATOR '\\'
25#else
26#define PATH_SEPARATOR '/'
27#endif
28
29namespace {
30
Tom Sepez1b1bb492015-01-22 17:36:32 -080031const char* g_exe_path_ = nullptr;
32
Tom Sepez96d13342015-01-16 14:59:26 -080033// Reads the entire contents of a file into a newly malloc'd buffer.
34static char* GetFileContents(const char* filename, size_t* retlen) {
35 FILE* file = fopen(filename, "rb");
36 if (!file) {
37 fprintf(stderr, "Failed to open: %s\n", filename);
38 return NULL;
39 }
40 (void) fseek(file, 0, SEEK_END);
41 size_t file_length = ftell(file);
42 if (!file_length) {
43 return NULL;
44 }
45 (void) fseek(file, 0, SEEK_SET);
46 char* buffer = (char*) malloc(file_length);
47 if (!buffer) {
48 return NULL;
49 }
50 size_t bytes_read = fread(buffer, 1, file_length, file);
51 (void) fclose(file);
52 if (bytes_read != file_length) {
53 fprintf(stderr, "Failed to read: %s\n", filename);
54 free(buffer);
55 return NULL;
56 }
57 *retlen = bytes_read;
58 return buffer;
59}
60
61#ifdef V8_USE_EXTERNAL_STARTUP_DATA
62// Returns the full path for an external V8 data file based on either
63// the currect exectuable path or an explicit override.
Tom Sepez1b1bb492015-01-22 17:36:32 -080064static std::string GetFullPathForSnapshotFile(const std::string& exe_path,
Tom Sepez96d13342015-01-16 14:59:26 -080065 const std::string& filename) {
66 std::string result;
Tom Sepez1b1bb492015-01-22 17:36:32 -080067 if (!exe_path.empty()) {
68 size_t last_separator = exe_path.rfind(PATH_SEPARATOR);
Tom Sepez96d13342015-01-16 14:59:26 -080069 if (last_separator != std::string::npos) {
Tom Sepez1b1bb492015-01-22 17:36:32 -080070 result = exe_path.substr(0, last_separator + 1);
Tom Sepez96d13342015-01-16 14:59:26 -080071 }
72 }
73 result += filename;
74 return result;
75}
76
77// Reads an extenal V8 data file from the |options|-indicated location,
78// returing true on success and false on error.
Tom Sepez1b1bb492015-01-22 17:36:32 -080079static bool GetExternalData(const std::string& exe_path,
80 const std::string& filename,
Tom Sepez96d13342015-01-16 14:59:26 -080081 v8::StartupData* result_data) {
Tom Sepez1b1bb492015-01-22 17:36:32 -080082 std::string full_path = GetFullPathForSnapshotFile(exe_path, filename);
Tom Sepez96d13342015-01-16 14:59:26 -080083 size_t data_length = 0;
84 char* data_buffer = GetFileContents(full_path.c_str(), &data_length);
85 if (!data_buffer) {
86 return false;
87 }
88 result_data->data = const_cast<const char*>(data_buffer);
89 result_data->raw_size = data_length;
90 return true;
91}
92#endif // V8_USE_EXTERNAL_STARTUP_DATA
93
94} // namespace
95
Tom Sepez4cb0fa72015-02-25 16:08:18 -080096class EmbedderTestDefaultDelegate : public EmbedderTest::Delegate {
97 public:
98 int Alert(FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) override {
99 printf("Form_Alert called.\n");
100 return 0;
Tom Sepez96d13342015-01-16 14:59:26 -0800101 }
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800102
103 void UnsupportedHandler(int type) {
104 std::string feature = "Unknown";
105 switch (type) {
106 case FPDF_UNSP_DOC_XFAFORM:
107 feature = "XFA";
108 break;
109 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
110 feature = "Portfolios_Packages";
111 break;
112 case FPDF_UNSP_DOC_ATTACHMENT:
113 case FPDF_UNSP_ANNOT_ATTACHMENT:
114 feature = "Attachment";
115 break;
116 case FPDF_UNSP_DOC_SECURITY:
117 feature = "Rights_Management";
118 break;
119 case FPDF_UNSP_DOC_SHAREDREVIEW:
120 feature = "Shared_Review";
121 break;
122 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
123 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
124 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
125 feature = "Shared_Form";
126 break;
127 case FPDF_UNSP_ANNOT_3DANNOT:
128 feature = "3D";
129 break;
130 case FPDF_UNSP_ANNOT_MOVIE:
131 feature = "Movie";
132 break;
133 case FPDF_UNSP_ANNOT_SOUND:
134 feature = "Sound";
135 break;
136 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
137 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
138 feature = "Screen";
139 break;
140 case FPDF_UNSP_ANNOT_SIG:
141 feature = "Digital_Signature";
142 break;
143 }
144 printf("Unsupported feature: %s.\n", feature.c_str());
145 }
146};
Tom Sepez96d13342015-01-16 14:59:26 -0800147
148class TestLoader {
149 public:
150 TestLoader(const char* pBuf, size_t len);
151
152 const char* m_pBuf;
153 size_t m_Len;
154};
155
156TestLoader::TestLoader(const char* pBuf, size_t len)
157 : m_pBuf(pBuf), m_Len(len) {
158}
159
160int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
161 unsigned long size) {
162 TestLoader* pLoader = (TestLoader*) param;
163 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
164 memcpy(pBuf, pLoader->m_pBuf + pos, size);
165 return 1;
166}
167
168bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
169 return true;
170}
171
172void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
173}
174
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800175EmbedderTest::EmbedderTest() :
176 document_(nullptr),
177 form_handle_(nullptr),
178 avail_(nullptr),
179 loader_(nullptr),
180 file_length_(0),
181 file_contents_(nullptr) {
182 memset(&hints_, 0, sizeof(hints_));
183 memset(&file_access_, 0, sizeof(file_access_));
184 memset(&file_avail_, 0, sizeof(file_avail_));
185 default_delegate_ = new EmbedderTestDefaultDelegate();
186 delegate_ = default_delegate_;
187}
188
189EmbedderTest::~EmbedderTest() {
190 delete default_delegate_;
191}
192
Tom Sepez96d13342015-01-16 14:59:26 -0800193void EmbedderTest::SetUp() {
194 v8::V8::InitializeICU();
195
196#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez1b1bb492015-01-22 17:36:32 -0800197 ASSERT_TRUE(GetExternalData(g_exe_path_, "natives_blob.bin", &natives_));
198 ASSERT_TRUE(GetExternalData(g_exe_path_, "snapshot_blob.bin", &snapshot_));
199 v8::V8::SetNativesDataBlob(&natives_);
200 v8::V8::SetSnapshotDataBlob(&snapshot_);
Tom Sepez96d13342015-01-16 14:59:26 -0800201#endif // V8_USE_EXTERNAL_STARTUP_DATA
202
203 FPDF_InitLibrary();
204
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800205 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
206 memset(info, 0, sizeof(UNSUPPORT_INFO));
207 info->version = 1;
208 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
209 FSDK_SetUnSpObjProcessHandler(info);
Tom Sepez96d13342015-01-16 14:59:26 -0800210 }
211
212void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -0800213 if (form_handle_) {
214 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
215 FPDFDOC_ExitFormFillEnvironment(form_handle_);
216 }
217 if (document_) {
218 FPDF_CloseDocument(document_);
219 }
Tom Sepez96d13342015-01-16 14:59:26 -0800220 FPDFAvail_Destroy(avail_);
221 FPDF_DestroyLibrary();
222 if (loader_) {
223 delete loader_;
224 }
225 if (file_contents_) {
226 free(file_contents_);
227 }
228}
229
230bool EmbedderTest::OpenDocument(const std::string& filename) {
231 file_contents_ = GetFileContents(filename.c_str(), &file_length_);
232 if (!file_contents_) {
233 return false;
234 }
235
236 loader_ = new TestLoader(file_contents_, file_length_);
237 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
238 file_access_.m_GetBlock = Get_Block;
239 file_access_.m_Param = loader_;
240
241 file_avail_.version = 1;
242 file_avail_.IsDataAvail = Is_Data_Avail;
243
244 hints_.version = 1;
245 hints_.AddSegment = Add_Segment;
246
247 avail_ = FPDFAvail_Create(&file_avail_, &file_access_);
248 (void) FPDFAvail_IsDocAvail(avail_, &hints_);
249
250 if (!FPDFAvail_IsLinearized(avail_)) {
251 document_ = FPDF_LoadCustomDocument(&file_access_, NULL);
252 } else {
253 document_ = FPDFAvail_GetDocument(avail_, NULL);
254 }
255 if (!document_) {
256 return false;
257 }
258 (void) FPDF_LoadXFA(document_);
259 (void) FPDF_GetDocPermissions(document_);
260 (void) FPDFAvail_IsFormAvail(avail_, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800261
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800262 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
263 memset(platform, 0, sizeof(IPDF_JSPLATFORM));
264 platform->version = 1;
265 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800266
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800267 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
268 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
269 formfillinfo->version = 1;
270 formfillinfo->m_pJsPlatform = platform;
Tom Sepez96d13342015-01-16 14:59:26 -0800271
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800272 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
Tom Sepezda8189e2015-01-30 14:41:50 -0800273 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
274 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
275
276 return true;
Tom Sepez96d13342015-01-16 14:59:26 -0800277}
278
Tom Sepezda8189e2015-01-30 14:41:50 -0800279void EmbedderTest::DoOpenActions() {
280 FORM_DoDocumentJSAction(form_handle_);
281 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800282}
283
284int EmbedderTest::GetFirstPageNum() {
285 int first_page = FPDFAvail_GetFirstPageNum(document_);
286 (void) FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
287 return first_page;
288}
289
290int EmbedderTest::GetPageCount() {
291 int page_count = FPDF_GetPageCount(document_);
292 for (int i = 0; i < page_count; ++i) {
293 (void) FPDFAvail_IsPageAvail(avail_, i, &hints_);
294 }
295 return page_count;
296}
297
Tom Sepezda8189e2015-01-30 14:41:50 -0800298FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez96d13342015-01-16 14:59:26 -0800299 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
300 if (!page) {
301 return nullptr;
302 }
Tom Sepezda8189e2015-01-30 14:41:50 -0800303 FORM_OnAfterLoadPage(page, form_handle_);
304 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
Tom Sepez96d13342015-01-16 14:59:26 -0800305 return page;
306}
307
Tom Sepezda8189e2015-01-30 14:41:50 -0800308FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Tom Sepez96d13342015-01-16 14:59:26 -0800309 int width = static_cast<int>(FPDF_GetPageWidth(page));
310 int height = static_cast<int>(FPDF_GetPageHeight(page));
311 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
312 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
313 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepezda8189e2015-01-30 14:41:50 -0800314 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepez96d13342015-01-16 14:59:26 -0800315 return bitmap;
316}
317
Tom Sepezda8189e2015-01-30 14:41:50 -0800318void EmbedderTest::UnloadPage(FPDF_PAGE page) {
319 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
320 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800321 FPDF_ClosePage(page);
322}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800323
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800324// static
325void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
326 int type) {
327 EmbedderTest* test = static_cast<EmbedderTest*>(info);
328 test->delegate_->UnsupportedHandler(type);
329}
330
331// static
332int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
333 FPDF_WIDESTRING message,
334 FPDF_WIDESTRING title,
335 int type,
336 int icon) {
337 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
338 return test->delegate_->Alert(message, title, type, icon);
339}
340
Tom Sepez1b1bb492015-01-22 17:36:32 -0800341// Can't use gtest-provided main since we need to stash the path to the
342// executable in order to find the external V8 binary data files.
343int main(int argc, char** argv) {
344 g_exe_path_ = argv[0];
345 testing::InitGoogleTest(&argc, argv);
346 return RUN_ALL_TESTS();
347}