88cce00f5a69f42dba054d79611e624f49727e90
[pdfium.git] / testing / embedder_test.cpp
1 // 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
30 namespace {
31
32 const char* g_exe_path_ = nullptr;
33
34 // Reads the entire contents of a file into a newly malloc'd buffer.
35 static 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.
65 static std::string GetFullPathForSnapshotFile(const std::string& exe_path,
66                                               const std::string& filename) {
67   std::string result;
68   if (!exe_path.empty()) {
69     size_t last_separator = exe_path.rfind(PATH_SEPARATOR);
70     if (last_separator != std::string::npos)  {
71       result = exe_path.substr(0, last_separator + 1);
72     }
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.
80 static bool GetExternalData(const std::string& exe_path,
81                             const std::string& filename,
82                             v8::StartupData* result_data) {
83   std::string full_path = GetFullPathForSnapshotFile(exe_path, filename);
84   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
97 int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) {
98   printf("Form_Alert called.\n");
99   return 0;
100 }
101
102 void 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
146 class TestLoader {
147  public:
148   TestLoader(const char* pBuf, size_t len);
149
150   const char* m_pBuf;
151   size_t m_Len;
152 };
153
154 TestLoader::TestLoader(const char* pBuf, size_t len)
155     : m_pBuf(pBuf), m_Len(len) {
156 }
157
158 int 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
166 bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
167   return true;
168 }
169
170 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
171 }
172
173 void EmbedderTest::SetUp() {
174     v8::V8::InitializeICU();
175
176 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
177     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_);
181 #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
192 void 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
204 bool 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
230   (void) FPDF_GetDocPermissions(document_);
231   (void) FPDFAvail_IsFormAvail(avail_, &hints_);
232   return true;
233 }
234
235 FPDF_FORMHANDLE EmbedderTest::SetFormFillEnvironment() {
236   IPDF_JSPLATFORM platform_callbacks;
237   memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
238   platform_callbacks.version = 1;
239   platform_callbacks.app_alert = Form_Alert;
240
241   FPDF_FORMFILLINFO form_callbacks;
242   memset(&form_callbacks, '\0', sizeof(form_callbacks));
243   form_callbacks.version = 1;
244   form_callbacks.m_pJsPlatform = &platform_callbacks;
245
246   FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(document_,
247                                                          &form_callbacks);
248   FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
249   FPDF_SetFormFieldHighlightAlpha(form, 100);
250   return form;
251 }
252
253 void EmbedderTest::ClearFormFillEnvironment(FPDF_FORMHANDLE form) {
254   FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
255   FPDFDOC_ExitFormFillEnvironment(form);
256 }
257
258 void EmbedderTest::DoOpenActions(FPDF_FORMHANDLE form) {
259   FORM_DoDocumentJSAction(form);
260   FORM_DoDocumentOpenAction(form);
261 }
262
263 int EmbedderTest::GetFirstPageNum() {
264   int first_page = FPDFAvail_GetFirstPageNum(document_);
265   (void) FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
266   return first_page;
267 }
268
269 int EmbedderTest::GetPageCount() {
270   int page_count = FPDF_GetPageCount(document_);
271   for (int i = 0; i < page_count; ++i) {
272     (void) FPDFAvail_IsPageAvail(avail_, i, &hints_);
273   }
274   return page_count;
275 }
276
277 FPDF_PAGE EmbedderTest::LoadPage(int page_number,
278                                  FPDF_FORMHANDLE form) {
279   FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
280   if (!page) {
281     return nullptr;
282   }
283   FORM_OnAfterLoadPage(page, form);
284   FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
285   return page;
286 }
287
288 FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page,
289                                      FPDF_FORMHANDLE form) {
290   int width = static_cast<int>(FPDF_GetPageWidth(page));
291   int height = static_cast<int>(FPDF_GetPageHeight(page));
292   FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
293   FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
294   FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
295   FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
296   return bitmap;
297 }
298
299 void EmbedderTest::UnloadPage(FPDF_PAGE page, FPDF_FORMHANDLE form) {
300   FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
301   FORM_OnBeforeClosePage(page, form);
302   FPDF_ClosePage(page);
303 }
304
305 // Can't use gtest-provided main since we need to stash the path to the
306 // executable in order to find the external V8 binary data files.
307 int main(int argc, char** argv) {
308   g_exe_path_ = argv[0];
309   testing::InitGoogleTest(&argc, argv);
310   return RUN_ALL_TESTS();
311 }