blob: c9997324db09498ffcf47a8df257396c4067c5eb [file] [log] [blame]
Lei Zhang1ac47eb2015-12-21 11:04:44 -08001// Copyright 2015 PDFium Authors. All rights reserved.
Tom Sepez96d13342015-01-16 14:59:26 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Wei Li091f7a02015-11-09 12:09:55 -08005#include "testing/embedder_test.h"
Tom Sepez96d13342015-01-16 14:59:26 -08006
7#include <limits.h>
Tom Sepez96d13342015-01-16 14:59:26 -08008
9#include <list>
Lei Zhang9f72c452018-02-08 21:49:54 +000010#include <map>
Lei Zhanga98e3662018-02-07 20:28:35 +000011#include <memory>
Tom Sepez96d13342015-01-16 14:59:26 -080012#include <string>
13#include <utility>
Lei Zhangd69e0652019-04-13 00:39:35 +000014#include <vector>
Tom Sepez96d13342015-01-16 14:59:26 -080015
Lei Zhangd145e4b2018-10-12 18:54:31 +000016#include "core/fdrm/fx_crypt.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000017#include "public/cpp/fpdf_scopers.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080018#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080019#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080020#include "public/fpdf_text.h"
21#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080022#include "testing/gmock/include/gmock/gmock.h"
Lei Zhangd50bdff2019-02-05 19:42:33 +000023#include "testing/test_loader.h"
Henrique Nakashimaf956bad2018-08-16 16:41:42 +000024#include "testing/utils/bitmap_saver.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000025#include "testing/utils/file_util.h"
Lei Zhang4c64e962019-02-05 19:24:12 +000026#include "testing/utils/hash.h"
Wei Li091f7a02015-11-09 12:09:55 -080027#include "testing/utils/path_service.h"
Lei Zhang75c81712018-02-08 17:22:39 +000028#include "third_party/base/logging.h"
Artem Strygin0e60b9e2017-09-28 18:46:03 +030029#include "third_party/base/ptr_util.h"
Lei Zhang75c81712018-02-08 17:22:39 +000030#include "third_party/base/stl_util.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070031
32#ifdef PDF_ENABLE_V8
Lei Zhang8241df72015-11-06 14:38:48 -080033#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050034#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070035#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080036
Tom Sepez96d13342015-01-16 14:59:26 -080037namespace {
thestigc08cd7a2016-06-27 09:47:59 -070038
Jane Liu28fb7ba2017-08-02 21:45:57 -040039int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
40 const int format = FPDFBitmap_GetFormat(bitmap);
41 switch (format) {
42 case FPDFBitmap_Gray:
43 return 1;
44 case FPDFBitmap_BGR:
45 return 3;
46 case FPDFBitmap_BGRx:
47 case FPDFBitmap_BGRA:
48 return 4;
49 default:
50 ASSERT(false);
51 return 0;
52 }
53}
54
Lei Zhang614f1a02019-05-30 00:03:53 +000055#if defined(OS_WIN)
56int CALLBACK GetRecordProc(HDC hdc,
57 HANDLETABLE* handle_table,
58 const ENHMETARECORD* record,
59 int objects_count,
60 LPARAM param) {
61 auto& records = *reinterpret_cast<std::vector<const ENHMETARECORD*>*>(param);
62 records.push_back(record);
63 return 1;
64}
65#endif // defined(OS_WIN)
66
thestigbcd3e532016-11-21 13:37:28 -080067} // namespace
68
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069EmbedderTest::EmbedderTest()
Lei Zhang0729be22018-02-05 21:13:51 +000070 : default_delegate_(pdfium::MakeUnique<EmbedderTest::Delegate>()),
71 delegate_(default_delegate_.get()) {
Nicolas Pena3ff54002017-07-05 11:55:35 -040072 FPDF_FILEWRITE::version = 1;
73 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -080074}
Tom Sepez96d13342015-01-16 14:59:26 -080075
Dan Sinclair5553d8b2018-01-03 09:44:28 -050076EmbedderTest::~EmbedderTest() {}
Tom Sepezf288bb12015-11-20 12:12:46 -080077
78void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -070079 FPDF_LIBRARY_CONFIG config;
80 config.version = 2;
81 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -070082 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -070083 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -070084 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -080085
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
87 memset(info, 0, sizeof(UNSUPPORT_INFO));
88 info->version = 1;
89 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
90 FSDK_SetUnSpObjProcessHandler(info);
Henrique Nakashima9fa50362017-11-10 22:40:44 +000091
Lei Zhang0729be22018-02-05 21:13:51 +000092 saved_document_ = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093}
Tom Sepez96d13342015-01-16 14:59:26 -080094
95void EmbedderTest::TearDown() {
Lei Zhang75c81712018-02-08 17:22:39 +000096 // Use an EXPECT_EQ() here and continue to let TearDown() finish as cleanly as
97 // possible. This can fail when an ASSERT test fails in a test case.
98 EXPECT_EQ(0U, page_map_.size());
Lei Zhang9f72c452018-02-08 21:49:54 +000099 EXPECT_EQ(0U, saved_page_map_.size());
Lei Zhang75c81712018-02-08 17:22:39 +0000100
Tom Sepezda8189e2015-01-30 14:41:50 -0800101 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700102 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepez38f725f2019-12-04 00:26:34 +0000103 CloseDocument();
Tom Sepezda8189e2015-01-30 14:41:50 -0800104 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800105
Tom Sepez96d13342015-01-16 14:59:26 -0800106 FPDFAvail_Destroy(avail_);
107 FPDF_DestroyLibrary();
Lei Zhangb3be4a12019-02-05 22:11:07 +0000108 loader_.reset();
Tom Sepez96d13342015-01-16 14:59:26 -0800109}
110
Lei Zhang378ec542018-10-18 19:15:47 +0000111#ifdef PDF_ENABLE_V8
112void EmbedderTest::SetExternalIsolate(void* isolate) {
113 external_isolate_ = static_cast<v8::Isolate*>(isolate);
114}
115#endif // PDF_ENABLE_V8
116
Tom Sepezd483eb42016-01-06 10:03:59 -0800117bool EmbedderTest::CreateEmptyDocument() {
118 document_ = FPDF_CreateNewDocument();
119 if (!document_)
120 return false;
121
Tom Sepez0784c732018-04-23 18:02:57 +0000122 form_handle_ =
123 SetupFormFillEnvironment(document_, JavaScriptOption::kEnableJavaScript);
Tom Sepezd483eb42016-01-06 10:03:59 -0800124 return true;
125}
126
Lei Zhang208eecf2017-12-20 19:40:50 +0000127bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000128 return OpenDocumentWithOptions(filename, nullptr,
129 LinearizeOption::kDefaultLinearize,
130 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000131}
132
133bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000134 return OpenDocumentWithOptions(filename, nullptr,
135 LinearizeOption::kMustLinearize,
136 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000137}
138
139bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
140 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000141 return OpenDocumentWithOptions(filename, password,
142 LinearizeOption::kDefaultLinearize,
143 JavaScriptOption::kEnableJavaScript);
144}
145
146bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
147 return OpenDocumentWithOptions(filename, nullptr,
148 LinearizeOption::kDefaultLinearize,
149 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000150}
151
152bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
153 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000154 LinearizeOption linearize_option,
155 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800156 std::string file_path;
157 if (!PathService::GetTestFilePath(filename, &file_path))
158 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000159
Wei Li091f7a02015-11-09 12:09:55 -0800160 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400161 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800162 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800163
thestig29ce9232016-06-22 07:03:23 -0700164 EXPECT_TRUE(!loader_);
Lei Zhangb3be4a12019-02-05 22:11:07 +0000165 loader_ = pdfium::MakeUnique<TestLoader>(
166 pdfium::make_span(file_contents_.get(), file_length_));
Lei Zhang0729be22018-02-05 21:13:51 +0000167
168 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800169 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700170 file_access_.m_GetBlock = TestLoader::GetBlock;
Lei Zhangb3be4a12019-02-05 22:11:07 +0000171 file_access_.m_Param = loader_.get();
Lei Zhang0729be22018-02-05 21:13:51 +0000172
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300173 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000174 return OpenDocumentHelper(password, linearize_option, javascript_option,
175 fake_file_access_.get(), &document_, &avail_,
176 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400177}
Tom Sepez96d13342015-01-16 14:59:26 -0800178
Nicolas Pena56fc9722017-07-13 16:31:34 -0400179bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000180 LinearizeOption linearize_option,
181 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300182 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400183 FPDF_DOCUMENT* document,
184 FPDF_AVAIL* avail,
185 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300186 network_simulator->AddSegment(0, 1024);
187 network_simulator->SetRequestedDataAvailable();
188 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
189 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400190 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300191 int32_t nRet = PDF_DATA_NOTAVAIL;
192 while (nRet == PDF_DATA_NOTAVAIL) {
193 network_simulator->SetRequestedDataAvailable();
194 nRet =
195 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
196 }
197 if (nRet == PDF_DATA_ERROR)
198 return false;
199
Nicolas Pena56fc9722017-07-13 16:31:34 -0400200 *document = FPDFAvail_GetDocument(*avail, password);
201 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800202 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400203
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300204 nRet = PDF_DATA_NOTAVAIL;
205 while (nRet == PDF_DATA_NOTAVAIL) {
206 network_simulator->SetRequestedDataAvailable();
207 nRet =
208 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
209 }
210 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800211 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400212
213 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800214 for (int i = 0; i < page_count; ++i) {
215 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300216 while (nRet == PDF_DATA_NOTAVAIL) {
217 network_simulator->SetRequestedDataAvailable();
218 nRet = FPDFAvail_IsPageAvail(*avail, i,
219 network_simulator->GetDownloadHints());
220 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400221 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800222 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800223 }
224 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000225 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800226 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300227 network_simulator->SetWholeFileAvailable();
228 *document =
229 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400230 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800231 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800232 }
Tom Sepez0784c732018-04-23 18:02:57 +0000233 *form_handle = SetupFormFillEnvironment(*document, javascript_option);
234
Ryan Harrison854d71c2017-10-18 12:28:14 -0400235 int doc_type = FPDF_GetFormType(*document);
236 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
237 FPDF_LoadXFA(*document);
Tom Sepez0784c732018-04-23 18:02:57 +0000238
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000239 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800240 return true;
241}
Tom Sepez96d13342015-01-16 14:59:26 -0800242
Tom Sepez38f725f2019-12-04 00:26:34 +0000243void EmbedderTest::CloseDocument() {
244 FPDFDOC_ExitFormFillEnvironment(form_handle_);
245 form_handle_ = nullptr;
246
247 FPDF_CloseDocument(document_);
248 document_ = nullptr;
249}
250
Tom Sepez0784c732018-04-23 18:02:57 +0000251FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
252 FPDF_DOCUMENT doc,
253 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800254 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400255 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200256 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800257 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400258 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800259
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800260 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
261 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800262#ifdef PDF_ENABLE_XFA
263 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800264#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800265 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800266#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700267 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
268 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700269 formfillinfo->FFI_GetPage = GetPageTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000270 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
271
Tom Sepez0784c732018-04-23 18:02:57 +0000272 if (javascript_option == JavaScriptOption::kEnableJavaScript)
273 formfillinfo->m_pJsPlatform = platform;
274
Nicolas Pena3ff54002017-07-05 11:55:35 -0400275 FPDF_FORMHANDLE form_handle =
276 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000277 FPDF_SetFormFieldHighlightColor(form_handle, FPDF_FORMFIELD_UNKNOWN,
278 0xFFE4DD);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400279 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
280 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800281}
282
Tom Sepezda8189e2015-01-30 14:41:50 -0800283void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400284 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800285 FORM_DoDocumentJSAction(form_handle_);
286 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800287}
288
289int EmbedderTest::GetFirstPageNum() {
290 int first_page = FPDFAvail_GetFirstPageNum(document_);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000291 (void)FPDFAvail_IsPageAvail(avail_, first_page,
292 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800293 return first_page;
294}
295
296int EmbedderTest::GetPageCount() {
297 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400298 for (int i = 0; i < page_count; ++i)
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000299 (void)FPDFAvail_IsPageAvail(avail_, i,
300 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800301 return page_count;
302}
303
Tom Sepezda8189e2015-01-30 14:41:50 -0800304FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez507d0192018-11-07 16:37:51 +0000305 return LoadPageCommon(page_number, true);
306}
307
308FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
309 return LoadPageCommon(page_number, false);
310}
311
312FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400313 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000314 ASSERT(page_number >= 0);
315 ASSERT(!pdfium::ContainsKey(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700316
Tom Sepez96d13342015-01-16 14:59:26 -0800317 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400318 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800319 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400320
Tom Sepez507d0192018-11-07 16:37:51 +0000321 if (do_events) {
322 FORM_OnAfterLoadPage(page, form_handle_);
323 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
324 }
weili0dadcc62016-08-23 21:10:57 -0700325 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700326 return page;
327}
328
Tom Sepezda8189e2015-01-30 14:41:50 -0800329void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Tom Sepez507d0192018-11-07 16:37:51 +0000330 UnloadPageCommon(page, true);
331}
Lei Zhang75c81712018-02-08 17:22:39 +0000332
Tom Sepez507d0192018-11-07 16:37:51 +0000333void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
334 UnloadPageCommon(page, false);
335}
336
337void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
338 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000339 int page_number = GetPageNumberForLoadedPage(page);
340 if (page_number < 0) {
341 NOTREACHED();
342 return;
343 }
Tom Sepez507d0192018-11-07 16:37:51 +0000344 if (do_events) {
345 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
346 FORM_OnBeforeClosePage(page, form_handle_);
347 }
Tom Sepez96d13342015-01-16 14:59:26 -0800348 FPDF_ClosePage(page);
Lei Zhang75c81712018-02-08 17:22:39 +0000349 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800350}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800351
Tom Sepeze08d2b12018-04-25 18:49:32 +0000352ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000353 return RenderLoadedPageWithFlags(page, 0);
354}
355
Tom Sepeze08d2b12018-04-25 18:49:32 +0000356ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
357 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000358 if (GetPageNumberForLoadedPage(page) < 0) {
359 NOTREACHED();
360 return nullptr;
361 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000362 return RenderPageWithFlags(page, form_handle_, flags);
363}
364
Tom Sepeze08d2b12018-04-25 18:49:32 +0000365ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000366 return RenderSavedPageWithFlags(page, 0);
367}
368
Tom Sepeze08d2b12018-04-25 18:49:32 +0000369ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
370 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000371 if (GetPageNumberForSavedPage(page) < 0) {
372 NOTREACHED();
373 return nullptr;
374 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000375 return RenderPageWithFlags(page, saved_form_handle_, flags);
376}
377
378// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000379ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
380 FPDF_FORMHANDLE handle,
381 int flags) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000382 int width = static_cast<int>(FPDF_GetPageWidth(page));
383 int height = static_cast<int>(FPDF_GetPageHeight(page));
384 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000385 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000386 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
387 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
388 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
389 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
390 return bitmap;
391}
392
Lei Zhang30ff2532019-01-31 21:37:55 +0000393// static
394ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) {
395 return RenderPageWithFlags(page, nullptr, 0);
396}
397
Lei Zhangd69e0652019-04-13 00:39:35 +0000398#if defined(OS_WIN)
399// static
400std::vector<uint8_t> EmbedderTest::RenderPageWithFlagsToEmf(FPDF_PAGE page,
401 int flags) {
402 HDC dc = CreateEnhMetaFileA(nullptr, nullptr, nullptr, nullptr);
403
404 int width = static_cast<int>(FPDF_GetPageWidth(page));
405 int height = static_cast<int>(FPDF_GetPageHeight(page));
406 HRGN rgn = CreateRectRgn(0, 0, width, height);
407 SelectClipRgn(dc, rgn);
408 DeleteObject(rgn);
409
410 SelectObject(dc, GetStockObject(NULL_PEN));
411 SelectObject(dc, GetStockObject(WHITE_BRUSH));
412 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
413 Rectangle(dc, 0, 0, width + 1, height + 1);
414
415 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, flags);
416
417 HENHMETAFILE emf = CloseEnhMetaFile(dc);
418 size_t size_in_bytes = GetEnhMetaFileBits(emf, 0, nullptr);
419 std::vector<uint8_t> buffer(size_in_bytes);
420 GetEnhMetaFileBits(emf, size_in_bytes, buffer.data());
421 DeleteEnhMetaFile(emf);
422 return buffer;
423}
Lei Zhang614f1a02019-05-30 00:03:53 +0000424
425// static
426std::string EmbedderTest::GetPostScriptFromEmf(
Tom Sepez87b9e3a2019-11-20 23:45:43 +0000427 pdfium::span<const uint8_t> emf_data) {
Lei Zhang614f1a02019-05-30 00:03:53 +0000428 // This comes from Emf::InitFromData() in Chromium.
429 HENHMETAFILE emf = SetEnhMetaFileBits(emf_data.size(), emf_data.data());
430 if (!emf)
431 return std::string();
432
433 // This comes from Emf::Enumerator::Enumerator() in Chromium.
434 std::vector<const ENHMETARECORD*> records;
435 if (!EnumEnhMetaFile(nullptr, emf, &GetRecordProc, &records, nullptr)) {
436 DeleteEnhMetaFile(emf);
437 return std::string();
438 }
439
440 // This comes from PostScriptMetaFile::SafePlayback() in Chromium.
441 std::string ps_data;
442 for (const auto* record : records) {
443 if (record->iType != EMR_GDICOMMENT)
444 continue;
445
446 // PostScript data is encapsulated inside EMF comment records.
447 // The first two bytes of the comment indicate the string length. The rest
448 // is the actual string data.
449 const auto* comment = reinterpret_cast<const EMRGDICOMMENT*>(record);
450 const char* data = reinterpret_cast<const char*>(comment->Data);
451 uint16_t size = *reinterpret_cast<const uint16_t*>(data);
452 data += 2;
453 ps_data.append(data, size);
454 }
455 DeleteEnhMetaFile(emf);
456 return ps_data;
457}
Lei Zhangd69e0652019-04-13 00:39:35 +0000458#endif // defined(OS_WIN)
459
Lei Zhang0b494052019-01-31 21:41:15 +0000460FPDF_DOCUMENT EmbedderTest::OpenSavedDocument() {
461 return OpenSavedDocumentWithPassword(nullptr);
462}
463
464FPDF_DOCUMENT EmbedderTest::OpenSavedDocumentWithPassword(
465 const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300466 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000467 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300468 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000469 // Copy data to prevent clearing it before saved document close.
470 saved_document_file_data_ = data_string_;
471 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400472
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300473 saved_fake_file_access_ =
474 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
475
Tom Sepez0784c732018-04-23 18:02:57 +0000476 EXPECT_TRUE(OpenDocumentHelper(
477 password, LinearizeOption::kDefaultLinearize,
478 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
479 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000480 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400481}
482
483void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000484 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400485
Lei Zhang0729be22018-02-05 21:13:51 +0000486 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
487 FPDF_CloseDocument(saved_document_);
488 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400489
Lei Zhang0729be22018-02-05 21:13:51 +0000490 saved_form_handle_ = nullptr;
491 saved_document_ = nullptr;
492 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400493}
494
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000495FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000496 ASSERT(saved_form_handle_);
497 ASSERT(page_number >= 0);
498 ASSERT(!pdfium::ContainsKey(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400499
Lei Zhang0729be22018-02-05 21:13:51 +0000500 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000501 if (!page)
502 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400503
Lei Zhang9f72c452018-02-08 21:49:54 +0000504 FORM_OnAfterLoadPage(page, saved_form_handle_);
505 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
506 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000507 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400508}
509
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000510void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000511 ASSERT(saved_form_handle_);
512
513 int page_number = GetPageNumberForSavedPage(page);
514 if (page_number < 0) {
515 NOTREACHED();
516 return;
517 }
518
519 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
520 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000521 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000522
523 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400524}
525
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000526void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
527 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400528 int height,
529 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000530 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000531 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400532
Tom Sepeze08d2b12018-04-25 18:49:32 +0000533 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000534 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400535}
536
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400537void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Lei Zhang0b494052019-01-31 21:41:15 +0000538 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000539 FPDF_PAGE page = LoadSavedPage(0);
540 VerifySavedRendering(page, width, height, md5);
541 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400542 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400543}
544
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300545void EmbedderTest::SetWholeFileAvailable() {
546 ASSERT(fake_file_access_);
547 fake_file_access_->SetWholeFileAvailable();
548}
549
weili0dadcc62016-08-23 21:10:57 -0700550FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700551 FPDF_DOCUMENT document,
552 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700553 EmbedderTest* test = static_cast<EmbedderTest*>(info);
554 auto it = test->page_map_.find(page_index);
555 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700556}
557
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800558// static
559void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
560 int type) {
561 EmbedderTest* test = static_cast<EmbedderTest*>(info);
562 test->delegate_->UnsupportedHandler(type);
563}
564
565// static
566int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
567 FPDF_WIDESTRING message,
568 FPDF_WIDESTRING title,
569 int type,
570 int icon) {
571 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
572 return test->delegate_->Alert(message, title, type, icon);
573}
574
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700575// static
576int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700577 int msecs,
578 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700579 EmbedderTest* test = static_cast<EmbedderTest*>(info);
580 return test->delegate_->SetTimer(msecs, fn);
581}
582
583// static
584void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
585 EmbedderTest* test = static_cast<EmbedderTest*>(info);
586 return test->delegate_->KillTimer(id);
587}
588
Tom Sepez396e8722015-09-09 10:16:08 -0700589// static
590FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
591 FPDF_DOCUMENT document,
592 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700593 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
594 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700595}
596
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000597// static
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000598void EmbedderTest::DoURIActionTrampoline(FPDF_FORMFILLINFO* info,
599 FPDF_BYTESTRING uri) {
600 EmbedderTest* test = static_cast<EmbedderTest*>(info);
601 return test->delegate_->DoURIAction(uri);
602}
603
604// static
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000605std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400606 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000607 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
608 FPDFBitmap_GetWidth(bitmap) *
609 GetBitmapBytesPerPixel(bitmap) *
610 FPDFBitmap_GetHeight(bitmap),
611 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400612 return CryptToBase16(digest);
613}
614
Henrique Nakashimadb269572018-01-16 19:02:15 +0000615#ifndef NDEBUG
616// static
617void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
618 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000619 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000620}
621#endif
622
thestigbcd3e532016-11-21 13:37:28 -0800623// static
624void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
625 int expected_width,
626 int expected_height,
627 const char* expected_md5sum) {
628 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
629 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400630
631 // The expected stride is calculated using the same formula as in
632 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
633 const int expected_stride =
634 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800635 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
636
637 if (!expected_md5sum)
638 return;
639
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000640 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800641}
642
Nicolas Pena3ff54002017-07-05 11:55:35 -0400643// static
644int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
645 const void* data,
646 unsigned long size) {
647 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000648
Lei Zhang0729be22018-02-05 21:13:51 +0000649 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000650
651 if (pThis->filestream_.is_open())
652 pThis->filestream_.write(static_cast<const char*>(data), size);
653
Nicolas Pena3ff54002017-07-05 11:55:35 -0400654 return 1;
655}
656
657// static
658int EmbedderTest::GetBlockFromString(void* param,
659 unsigned long pos,
660 unsigned char* buf,
661 unsigned long size) {
662 std::string* new_file = static_cast<std::string*>(param);
Lei Zhangcef91f12019-01-08 21:32:51 +0000663 if (!new_file || pos + size < pos) {
664 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400665 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000666 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400667
Lei Zhangcef91f12019-01-08 21:32:51 +0000668 if (pos + size > new_file->size()) {
669 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400670 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000671 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400672
673 memcpy(buf, new_file->data() + pos, size);
674 return 1;
675}
Lei Zhang75c81712018-02-08 17:22:39 +0000676
Lei Zhang9f72c452018-02-08 21:49:54 +0000677// static
678int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
679 FPDF_PAGE page) {
680 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000681 if (it.second == page) {
682 int page_number = it.first;
683 ASSERT(page_number >= 0);
684 return page_number;
685 }
686 }
687 return -1;
688}
Lei Zhang9f72c452018-02-08 21:49:54 +0000689
690int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
691 return GetPageNumberForPage(page_map_, page);
692}
693
694int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
695 return GetPageNumberForPage(saved_page_map_, page);
696}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000697
698void EmbedderTest::OpenPDFFileForWrite(const char* filename) {
699 filestream_.open(filename, std::ios_base::binary);
700}
701
702void EmbedderTest::ClosePDFFileForWrite() {
703 filestream_.close();
704}