blob: bebe307cf3431b499d04655b9200ae5305c6af9c [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
17#include "../fpdfsdk/include/fpdf_ext.h"
18#include "../fpdfsdk/include/fpdftext.h"
19#include "../fpdfsdk/include/fpdfview.h"
20#include "../core/include/fxcrt/fx_system.h"
21#include "v8/include/v8.h"
22
23#ifdef _WIN32
24#define snprintf _snprintf
25#define PATH_SEPARATOR '\\'
26#else
27#define PATH_SEPARATOR '/'
28#endif
29
30namespace {
31
Tom Sepez1b1bb492015-01-22 17:36:32 -080032const char* g_exe_path_ = nullptr;
33
Tom Sepez96d13342015-01-16 14:59:26 -080034// Reads the entire contents of a file into a newly malloc'd buffer.
35static char* GetFileContents(const char* filename, size_t* retlen) {
36 FILE* file = fopen(filename, "rb");
37 if (!file) {
38 fprintf(stderr, "Failed to open: %s\n", filename);
39 return NULL;
40 }
41 (void) fseek(file, 0, SEEK_END);
42 size_t file_length = ftell(file);
43 if (!file_length) {
44 return NULL;
45 }
46 (void) fseek(file, 0, SEEK_SET);
47 char* buffer = (char*) malloc(file_length);
48 if (!buffer) {
49 return NULL;
50 }
51 size_t bytes_read = fread(buffer, 1, file_length, file);
52 (void) fclose(file);
53 if (bytes_read != file_length) {
54 fprintf(stderr, "Failed to read: %s\n", filename);
55 free(buffer);
56 return NULL;
57 }
58 *retlen = bytes_read;
59 return buffer;
60}
61
62#ifdef V8_USE_EXTERNAL_STARTUP_DATA
63// Returns the full path for an external V8 data file based on either
64// the currect exectuable path or an explicit override.
Tom Sepez1b1bb492015-01-22 17:36:32 -080065static std::string GetFullPathForSnapshotFile(const std::string& exe_path,
Tom Sepez96d13342015-01-16 14:59:26 -080066 const std::string& filename) {
67 std::string result;
Tom Sepez1b1bb492015-01-22 17:36:32 -080068 if (!exe_path.empty()) {
69 size_t last_separator = exe_path.rfind(PATH_SEPARATOR);
Tom Sepez96d13342015-01-16 14:59:26 -080070 if (last_separator != std::string::npos) {
Tom Sepez1b1bb492015-01-22 17:36:32 -080071 result = exe_path.substr(0, last_separator + 1);
Tom Sepez96d13342015-01-16 14:59:26 -080072 }
73 }
74 result += filename;
75 return result;
76}
77
78// Reads an extenal V8 data file from the |options|-indicated location,
79// returing true on success and false on error.
Tom Sepez1b1bb492015-01-22 17:36:32 -080080static bool GetExternalData(const std::string& exe_path,
81 const std::string& filename,
Tom Sepez96d13342015-01-16 14:59:26 -080082 v8::StartupData* result_data) {
Tom Sepez1b1bb492015-01-22 17:36:32 -080083 std::string full_path = GetFullPathForSnapshotFile(exe_path, filename);
Tom Sepez96d13342015-01-16 14:59:26 -080084 size_t data_length = 0;
85 char* data_buffer = GetFileContents(full_path.c_str(), &data_length);
86 if (!data_buffer) {
87 return false;
88 }
89 result_data->data = const_cast<const char*>(data_buffer);
90 result_data->raw_size = data_length;
91 return true;
92}
93#endif // V8_USE_EXTERNAL_STARTUP_DATA
94
95} // namespace
96
97int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) {
98 printf("Form_Alert called.\n");
99 return 0;
100}
101
102void Unsupported_Handler(UNSUPPORT_INFO*, int type) {
103 std::string feature = "Unknown";
104 switch (type) {
105 case FPDF_UNSP_DOC_XFAFORM:
106 feature = "XFA";
107 break;
108 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
109 feature = "Portfolios_Packages";
110 break;
111 case FPDF_UNSP_DOC_ATTACHMENT:
112 case FPDF_UNSP_ANNOT_ATTACHMENT:
113 feature = "Attachment";
114 break;
115 case FPDF_UNSP_DOC_SECURITY:
116 feature = "Rights_Management";
117 break;
118 case FPDF_UNSP_DOC_SHAREDREVIEW:
119 feature = "Shared_Review";
120 break;
121 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
122 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
123 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
124 feature = "Shared_Form";
125 break;
126 case FPDF_UNSP_ANNOT_3DANNOT:
127 feature = "3D";
128 break;
129 case FPDF_UNSP_ANNOT_MOVIE:
130 feature = "Movie";
131 break;
132 case FPDF_UNSP_ANNOT_SOUND:
133 feature = "Sound";
134 break;
135 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
136 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
137 feature = "Screen";
138 break;
139 case FPDF_UNSP_ANNOT_SIG:
140 feature = "Digital_Signature";
141 break;
142 }
143 printf("Unsupported feature: %s.\n", feature.c_str());
144}
145
146class TestLoader {
147 public:
148 TestLoader(const char* pBuf, size_t len);
149
150 const char* m_pBuf;
151 size_t m_Len;
152};
153
154TestLoader::TestLoader(const char* pBuf, size_t len)
155 : m_pBuf(pBuf), m_Len(len) {
156}
157
158int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
159 unsigned long size) {
160 TestLoader* pLoader = (TestLoader*) param;
161 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
162 memcpy(pBuf, pLoader->m_pBuf + pos, size);
163 return 1;
164}
165
166bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
167 return true;
168}
169
170void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
171}
172
173void EmbedderTest::SetUp() {
174 v8::V8::InitializeICU();
175
176#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez1b1bb492015-01-22 17:36:32 -0800177 ASSERT_TRUE(GetExternalData(g_exe_path_, "natives_blob.bin", &natives_));
178 ASSERT_TRUE(GetExternalData(g_exe_path_, "snapshot_blob.bin", &snapshot_));
179 v8::V8::SetNativesDataBlob(&natives_);
180 v8::V8::SetSnapshotDataBlob(&snapshot_);
Tom Sepez96d13342015-01-16 14:59:26 -0800181#endif // V8_USE_EXTERNAL_STARTUP_DATA
182
183 FPDF_InitLibrary();
184
185 UNSUPPORT_INFO unsuppored_info;
186 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
187 unsuppored_info.version = 1;
188 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler;
189 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
190 }
191
192void EmbedderTest::TearDown() {
193 FPDF_CloseDocument(document_);
194 FPDFAvail_Destroy(avail_);
195 FPDF_DestroyLibrary();
196 if (loader_) {
197 delete loader_;
198 }
199 if (file_contents_) {
200 free(file_contents_);
201 }
202}
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_);
222 (void) FPDFAvail_IsDocAvail(avail_, &hints_);
223
224 if (!FPDFAvail_IsLinearized(avail_)) {
225 document_ = FPDF_LoadCustomDocument(&file_access_, NULL);
226 } else {
227 document_ = FPDFAvail_GetDocument(avail_, NULL);
228 }
229 if (!document_) {
230 return false;
231 }
232 (void) FPDF_LoadXFA(document_);
233 (void) FPDF_GetDocPermissions(document_);
234 (void) FPDFAvail_IsFormAvail(avail_, &hints_);
235 return true;
236}
237
238FPDF_FORMHANDLE EmbedderTest::SetFormFillEnvironment() {
239 IPDF_JSPLATFORM platform_callbacks;
240 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
241 platform_callbacks.version = 1;
242 platform_callbacks.app_alert = Form_Alert;
243
244 FPDF_FORMFILLINFO form_callbacks;
245 memset(&form_callbacks, '\0', sizeof(form_callbacks));
246 form_callbacks.version = 1;
247 form_callbacks.m_pJsPlatform = &platform_callbacks;
248
249 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(document_,
250 &form_callbacks);
251 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
252 FPDF_SetFormFieldHighlightAlpha(form, 100);
253 return form;
254}
255
256void EmbedderTest::ClearFormFillEnvironment(FPDF_FORMHANDLE form) {
257 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
258 FPDFDOC_ExitFormFillEnvironment(form);
259}
260
261void EmbedderTest::DoOpenActions(FPDF_FORMHANDLE form) {
262 FORM_DoDocumentJSAction(form);
263 FORM_DoDocumentOpenAction(form);
264}
265
266int EmbedderTest::GetFirstPageNum() {
267 int first_page = FPDFAvail_GetFirstPageNum(document_);
268 (void) FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
269 return first_page;
270}
271
272int EmbedderTest::GetPageCount() {
273 int page_count = FPDF_GetPageCount(document_);
274 for (int i = 0; i < page_count; ++i) {
275 (void) FPDFAvail_IsPageAvail(avail_, i, &hints_);
276 }
277 return page_count;
278}
279
280FPDF_PAGE EmbedderTest::LoadPage(int page_number,
281 FPDF_FORMHANDLE form) {
282 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
283 if (!page) {
284 return nullptr;
285 }
286 FORM_OnAfterLoadPage(page, form);
287 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
288 return page;
289}
290
291FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page,
292 FPDF_FORMHANDLE form) {
293 int width = static_cast<int>(FPDF_GetPageWidth(page));
294 int height = static_cast<int>(FPDF_GetPageHeight(page));
295 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
296 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
297 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
298 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
299 return bitmap;
300}
301
302void EmbedderTest::UnloadPage(FPDF_PAGE page, FPDF_FORMHANDLE form) {
303 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
304 FORM_OnBeforeClosePage(page, form);
305 FPDF_ClosePage(page);
306}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800307
308// Can't use gtest-provided main since we need to stash the path to the
309// executable in order to find the external V8 binary data files.
310int main(int argc, char** argv) {
311 g_exe_path_ = argv[0];
312 testing::InitGoogleTest(&argc, argv);
313 return RUN_ALL_TESTS();
314}