blob: b092bc6aeb77b46f58fd5011d8bd6f1363bad9c9 [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 Sepezc46d0002015-11-30 15:46:36 -0800103 FPDFDOC_ExitFormFillEnvironment(form_handle_);
104 FPDF_CloseDocument(document_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800105 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800106
Tom Sepez96d13342015-01-16 14:59:26 -0800107 FPDFAvail_Destroy(avail_);
108 FPDF_DestroyLibrary();
Lei Zhangb3be4a12019-02-05 22:11:07 +0000109 loader_.reset();
Tom Sepez96d13342015-01-16 14:59:26 -0800110}
111
Lei Zhang378ec542018-10-18 19:15:47 +0000112#ifdef PDF_ENABLE_V8
113void EmbedderTest::SetExternalIsolate(void* isolate) {
114 external_isolate_ = static_cast<v8::Isolate*>(isolate);
115}
116#endif // PDF_ENABLE_V8
117
Tom Sepezd483eb42016-01-06 10:03:59 -0800118bool EmbedderTest::CreateEmptyDocument() {
119 document_ = FPDF_CreateNewDocument();
120 if (!document_)
121 return false;
122
Tom Sepez0784c732018-04-23 18:02:57 +0000123 form_handle_ =
124 SetupFormFillEnvironment(document_, JavaScriptOption::kEnableJavaScript);
Tom Sepezd483eb42016-01-06 10:03:59 -0800125 return true;
126}
127
Lei Zhang208eecf2017-12-20 19:40:50 +0000128bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000129 return OpenDocumentWithOptions(filename, nullptr,
130 LinearizeOption::kDefaultLinearize,
131 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000132}
133
134bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000135 return OpenDocumentWithOptions(filename, nullptr,
136 LinearizeOption::kMustLinearize,
137 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000138}
139
140bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
141 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000142 return OpenDocumentWithOptions(filename, password,
143 LinearizeOption::kDefaultLinearize,
144 JavaScriptOption::kEnableJavaScript);
145}
146
147bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
148 return OpenDocumentWithOptions(filename, nullptr,
149 LinearizeOption::kDefaultLinearize,
150 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000151}
152
153bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
154 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000155 LinearizeOption linearize_option,
156 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800157 std::string file_path;
158 if (!PathService::GetTestFilePath(filename, &file_path))
159 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000160
Wei Li091f7a02015-11-09 12:09:55 -0800161 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400162 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800163 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800164
thestig29ce9232016-06-22 07:03:23 -0700165 EXPECT_TRUE(!loader_);
Lei Zhangb3be4a12019-02-05 22:11:07 +0000166 loader_ = pdfium::MakeUnique<TestLoader>(
167 pdfium::make_span(file_contents_.get(), file_length_));
Lei Zhang0729be22018-02-05 21:13:51 +0000168
169 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800170 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700171 file_access_.m_GetBlock = TestLoader::GetBlock;
Lei Zhangb3be4a12019-02-05 22:11:07 +0000172 file_access_.m_Param = loader_.get();
Lei Zhang0729be22018-02-05 21:13:51 +0000173
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300174 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000175 return OpenDocumentHelper(password, linearize_option, javascript_option,
176 fake_file_access_.get(), &document_, &avail_,
177 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400178}
Tom Sepez96d13342015-01-16 14:59:26 -0800179
Nicolas Pena56fc9722017-07-13 16:31:34 -0400180bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000181 LinearizeOption linearize_option,
182 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300183 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400184 FPDF_DOCUMENT* document,
185 FPDF_AVAIL* avail,
186 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300187 network_simulator->AddSegment(0, 1024);
188 network_simulator->SetRequestedDataAvailable();
189 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
190 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400191 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300192 int32_t nRet = PDF_DATA_NOTAVAIL;
193 while (nRet == PDF_DATA_NOTAVAIL) {
194 network_simulator->SetRequestedDataAvailable();
195 nRet =
196 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
197 }
198 if (nRet == PDF_DATA_ERROR)
199 return false;
200
Nicolas Pena56fc9722017-07-13 16:31:34 -0400201 *document = FPDFAvail_GetDocument(*avail, password);
202 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800203 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400204
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300205 nRet = PDF_DATA_NOTAVAIL;
206 while (nRet == PDF_DATA_NOTAVAIL) {
207 network_simulator->SetRequestedDataAvailable();
208 nRet =
209 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
210 }
211 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800212 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400213
214 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800215 for (int i = 0; i < page_count; ++i) {
216 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300217 while (nRet == PDF_DATA_NOTAVAIL) {
218 network_simulator->SetRequestedDataAvailable();
219 nRet = FPDFAvail_IsPageAvail(*avail, i,
220 network_simulator->GetDownloadHints());
221 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400222
223 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800224 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800225 }
226 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000227 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800228 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300229 network_simulator->SetWholeFileAvailable();
230 *document =
231 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400232 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800233 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800234 }
Tom Sepez0784c732018-04-23 18:02:57 +0000235 *form_handle = SetupFormFillEnvironment(*document, javascript_option);
236
Tom Sepezc46d0002015-11-30 15:46:36 -0800237#ifdef PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400238 int doc_type = FPDF_GetFormType(*document);
239 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
240 FPDF_LoadXFA(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800241#endif // PDF_ENABLE_XFA
Tom Sepez0784c732018-04-23 18:02:57 +0000242
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000243 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800244 return true;
245}
Tom Sepez96d13342015-01-16 14:59:26 -0800246
Tom Sepez0784c732018-04-23 18:02:57 +0000247FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
248 FPDF_DOCUMENT doc,
249 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800250 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400251 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200252 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800253 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400254 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800255
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800256 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
257 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800258#ifdef PDF_ENABLE_XFA
259 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800260#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800261 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800262#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700263 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
264 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700265 formfillinfo->FFI_GetPage = GetPageTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000266 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
267
Tom Sepez0784c732018-04-23 18:02:57 +0000268 if (javascript_option == JavaScriptOption::kEnableJavaScript)
269 formfillinfo->m_pJsPlatform = platform;
270
Nicolas Pena3ff54002017-07-05 11:55:35 -0400271 FPDF_FORMHANDLE form_handle =
272 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000273 FPDF_SetFormFieldHighlightColor(form_handle, FPDF_FORMFIELD_UNKNOWN,
274 0xFFE4DD);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400275 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
276 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800277}
278
Tom Sepezda8189e2015-01-30 14:41:50 -0800279void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400280 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800281 FORM_DoDocumentJSAction(form_handle_);
282 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800283}
284
285int EmbedderTest::GetFirstPageNum() {
286 int first_page = FPDFAvail_GetFirstPageNum(document_);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000287 (void)FPDFAvail_IsPageAvail(avail_, first_page,
288 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800289 return first_page;
290}
291
292int EmbedderTest::GetPageCount() {
293 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400294 for (int i = 0; i < page_count; ++i)
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000295 (void)FPDFAvail_IsPageAvail(avail_, i,
296 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800297 return page_count;
298}
299
Tom Sepezda8189e2015-01-30 14:41:50 -0800300FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez507d0192018-11-07 16:37:51 +0000301 return LoadPageCommon(page_number, true);
302}
303
304FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
305 return LoadPageCommon(page_number, false);
306}
307
308FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400309 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000310 ASSERT(page_number >= 0);
311 ASSERT(!pdfium::ContainsKey(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700312
Tom Sepez96d13342015-01-16 14:59:26 -0800313 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400314 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800315 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400316
Tom Sepez507d0192018-11-07 16:37:51 +0000317 if (do_events) {
318 FORM_OnAfterLoadPage(page, form_handle_);
319 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
320 }
weili0dadcc62016-08-23 21:10:57 -0700321 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700322 return page;
323}
324
Tom Sepezda8189e2015-01-30 14:41:50 -0800325void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Tom Sepez507d0192018-11-07 16:37:51 +0000326 UnloadPageCommon(page, true);
327}
Lei Zhang75c81712018-02-08 17:22:39 +0000328
Tom Sepez507d0192018-11-07 16:37:51 +0000329void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
330 UnloadPageCommon(page, false);
331}
332
333void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
334 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000335 int page_number = GetPageNumberForLoadedPage(page);
336 if (page_number < 0) {
337 NOTREACHED();
338 return;
339 }
Tom Sepez507d0192018-11-07 16:37:51 +0000340 if (do_events) {
341 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
342 FORM_OnBeforeClosePage(page, form_handle_);
343 }
Tom Sepez96d13342015-01-16 14:59:26 -0800344 FPDF_ClosePage(page);
Lei Zhang75c81712018-02-08 17:22:39 +0000345 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800346}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800347
Tom Sepeze08d2b12018-04-25 18:49:32 +0000348ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000349 return RenderLoadedPageWithFlags(page, 0);
350}
351
Tom Sepeze08d2b12018-04-25 18:49:32 +0000352ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
353 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000354 if (GetPageNumberForLoadedPage(page) < 0) {
355 NOTREACHED();
356 return nullptr;
357 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000358 return RenderPageWithFlags(page, form_handle_, flags);
359}
360
Tom Sepeze08d2b12018-04-25 18:49:32 +0000361ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000362 return RenderSavedPageWithFlags(page, 0);
363}
364
Tom Sepeze08d2b12018-04-25 18:49:32 +0000365ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
366 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000367 if (GetPageNumberForSavedPage(page) < 0) {
368 NOTREACHED();
369 return nullptr;
370 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000371 return RenderPageWithFlags(page, saved_form_handle_, flags);
372}
373
374// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000375ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
376 FPDF_FORMHANDLE handle,
377 int flags) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000378 int width = static_cast<int>(FPDF_GetPageWidth(page));
379 int height = static_cast<int>(FPDF_GetPageHeight(page));
380 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000381 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000382 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
383 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
384 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
385 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
386 return bitmap;
387}
388
Lei Zhang30ff2532019-01-31 21:37:55 +0000389// static
390ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) {
391 return RenderPageWithFlags(page, nullptr, 0);
392}
393
Lei Zhangd69e0652019-04-13 00:39:35 +0000394#if defined(OS_WIN)
395// static
396std::vector<uint8_t> EmbedderTest::RenderPageWithFlagsToEmf(FPDF_PAGE page,
397 int flags) {
398 HDC dc = CreateEnhMetaFileA(nullptr, nullptr, nullptr, nullptr);
399
400 int width = static_cast<int>(FPDF_GetPageWidth(page));
401 int height = static_cast<int>(FPDF_GetPageHeight(page));
402 HRGN rgn = CreateRectRgn(0, 0, width, height);
403 SelectClipRgn(dc, rgn);
404 DeleteObject(rgn);
405
406 SelectObject(dc, GetStockObject(NULL_PEN));
407 SelectObject(dc, GetStockObject(WHITE_BRUSH));
408 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
409 Rectangle(dc, 0, 0, width + 1, height + 1);
410
411 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, flags);
412
413 HENHMETAFILE emf = CloseEnhMetaFile(dc);
414 size_t size_in_bytes = GetEnhMetaFileBits(emf, 0, nullptr);
415 std::vector<uint8_t> buffer(size_in_bytes);
416 GetEnhMetaFileBits(emf, size_in_bytes, buffer.data());
417 DeleteEnhMetaFile(emf);
418 return buffer;
419}
Lei Zhang614f1a02019-05-30 00:03:53 +0000420
421// static
422std::string EmbedderTest::GetPostScriptFromEmf(
423 const std::vector<uint8_t>& emf_data) {
424 // This comes from Emf::InitFromData() in Chromium.
425 HENHMETAFILE emf = SetEnhMetaFileBits(emf_data.size(), emf_data.data());
426 if (!emf)
427 return std::string();
428
429 // This comes from Emf::Enumerator::Enumerator() in Chromium.
430 std::vector<const ENHMETARECORD*> records;
431 if (!EnumEnhMetaFile(nullptr, emf, &GetRecordProc, &records, nullptr)) {
432 DeleteEnhMetaFile(emf);
433 return std::string();
434 }
435
436 // This comes from PostScriptMetaFile::SafePlayback() in Chromium.
437 std::string ps_data;
438 for (const auto* record : records) {
439 if (record->iType != EMR_GDICOMMENT)
440 continue;
441
442 // PostScript data is encapsulated inside EMF comment records.
443 // The first two bytes of the comment indicate the string length. The rest
444 // is the actual string data.
445 const auto* comment = reinterpret_cast<const EMRGDICOMMENT*>(record);
446 const char* data = reinterpret_cast<const char*>(comment->Data);
447 uint16_t size = *reinterpret_cast<const uint16_t*>(data);
448 data += 2;
449 ps_data.append(data, size);
450 }
451 DeleteEnhMetaFile(emf);
452 return ps_data;
453}
Lei Zhangd69e0652019-04-13 00:39:35 +0000454#endif // defined(OS_WIN)
455
Lei Zhang0b494052019-01-31 21:41:15 +0000456FPDF_DOCUMENT EmbedderTest::OpenSavedDocument() {
457 return OpenSavedDocumentWithPassword(nullptr);
458}
459
460FPDF_DOCUMENT EmbedderTest::OpenSavedDocumentWithPassword(
461 const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300462 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000463 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300464 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000465 // Copy data to prevent clearing it before saved document close.
466 saved_document_file_data_ = data_string_;
467 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400468
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300469 saved_fake_file_access_ =
470 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
471
Tom Sepez0784c732018-04-23 18:02:57 +0000472 EXPECT_TRUE(OpenDocumentHelper(
473 password, LinearizeOption::kDefaultLinearize,
474 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
475 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000476 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400477}
478
479void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000480 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400481
Lei Zhang0729be22018-02-05 21:13:51 +0000482 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
483 FPDF_CloseDocument(saved_document_);
484 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400485
Lei Zhang0729be22018-02-05 21:13:51 +0000486 saved_form_handle_ = nullptr;
487 saved_document_ = nullptr;
488 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400489}
490
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000491FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000492 ASSERT(saved_form_handle_);
493 ASSERT(page_number >= 0);
494 ASSERT(!pdfium::ContainsKey(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400495
Lei Zhang0729be22018-02-05 21:13:51 +0000496 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000497 if (!page)
498 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400499
Lei Zhang9f72c452018-02-08 21:49:54 +0000500 FORM_OnAfterLoadPage(page, saved_form_handle_);
501 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
502 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000503 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400504}
505
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000506void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000507 ASSERT(saved_form_handle_);
508
509 int page_number = GetPageNumberForSavedPage(page);
510 if (page_number < 0) {
511 NOTREACHED();
512 return;
513 }
514
515 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
516 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000517 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000518
519 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400520}
521
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000522void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
523 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400524 int height,
525 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000526 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000527 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400528
Tom Sepeze08d2b12018-04-25 18:49:32 +0000529 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000530 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400531}
532
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400533void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Lei Zhang0b494052019-01-31 21:41:15 +0000534 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000535 FPDF_PAGE page = LoadSavedPage(0);
536 VerifySavedRendering(page, width, height, md5);
537 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400538 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400539}
540
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300541void EmbedderTest::SetWholeFileAvailable() {
542 ASSERT(fake_file_access_);
543 fake_file_access_->SetWholeFileAvailable();
544}
545
weili0dadcc62016-08-23 21:10:57 -0700546FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700547 FPDF_DOCUMENT document,
548 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700549 EmbedderTest* test = static_cast<EmbedderTest*>(info);
550 auto it = test->page_map_.find(page_index);
551 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700552}
553
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800554// static
555void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
556 int type) {
557 EmbedderTest* test = static_cast<EmbedderTest*>(info);
558 test->delegate_->UnsupportedHandler(type);
559}
560
561// static
562int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
563 FPDF_WIDESTRING message,
564 FPDF_WIDESTRING title,
565 int type,
566 int icon) {
567 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
568 return test->delegate_->Alert(message, title, type, icon);
569}
570
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700571// static
572int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700573 int msecs,
574 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700575 EmbedderTest* test = static_cast<EmbedderTest*>(info);
576 return test->delegate_->SetTimer(msecs, fn);
577}
578
579// static
580void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
581 EmbedderTest* test = static_cast<EmbedderTest*>(info);
582 return test->delegate_->KillTimer(id);
583}
584
Tom Sepez396e8722015-09-09 10:16:08 -0700585// static
586FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
587 FPDF_DOCUMENT document,
588 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700589 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
590 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700591}
592
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000593// static
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000594void EmbedderTest::DoURIActionTrampoline(FPDF_FORMFILLINFO* info,
595 FPDF_BYTESTRING uri) {
596 EmbedderTest* test = static_cast<EmbedderTest*>(info);
597 return test->delegate_->DoURIAction(uri);
598}
599
600// static
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000601std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400602 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000603 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
604 FPDFBitmap_GetWidth(bitmap) *
605 GetBitmapBytesPerPixel(bitmap) *
606 FPDFBitmap_GetHeight(bitmap),
607 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400608 return CryptToBase16(digest);
609}
610
Henrique Nakashimadb269572018-01-16 19:02:15 +0000611#ifndef NDEBUG
612// static
613void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
614 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000615 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000616}
617#endif
618
thestigbcd3e532016-11-21 13:37:28 -0800619// static
620void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
621 int expected_width,
622 int expected_height,
623 const char* expected_md5sum) {
624 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
625 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400626
627 // The expected stride is calculated using the same formula as in
628 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
629 const int expected_stride =
630 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800631 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
632
633 if (!expected_md5sum)
634 return;
635
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000636 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800637}
638
Nicolas Pena3ff54002017-07-05 11:55:35 -0400639// static
640int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
641 const void* data,
642 unsigned long size) {
643 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000644
Lei Zhang0729be22018-02-05 21:13:51 +0000645 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000646
647 if (pThis->filestream_.is_open())
648 pThis->filestream_.write(static_cast<const char*>(data), size);
649
Nicolas Pena3ff54002017-07-05 11:55:35 -0400650 return 1;
651}
652
653// static
654int EmbedderTest::GetBlockFromString(void* param,
655 unsigned long pos,
656 unsigned char* buf,
657 unsigned long size) {
658 std::string* new_file = static_cast<std::string*>(param);
Lei Zhangcef91f12019-01-08 21:32:51 +0000659 if (!new_file || pos + size < pos) {
660 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400661 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000662 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400663
Lei Zhangcef91f12019-01-08 21:32:51 +0000664 if (pos + size > new_file->size()) {
665 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400666 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000667 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400668
669 memcpy(buf, new_file->data() + pos, size);
670 return 1;
671}
Lei Zhang75c81712018-02-08 17:22:39 +0000672
Lei Zhang9f72c452018-02-08 21:49:54 +0000673// static
674int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
675 FPDF_PAGE page) {
676 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000677 if (it.second == page) {
678 int page_number = it.first;
679 ASSERT(page_number >= 0);
680 return page_number;
681 }
682 }
683 return -1;
684}
Lei Zhang9f72c452018-02-08 21:49:54 +0000685
686int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
687 return GetPageNumberForPage(page_map_, page);
688}
689
690int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
691 return GetPageNumberForPage(saved_page_map_, page);
692}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000693
694void EmbedderTest::OpenPDFFileForWrite(const char* filename) {
695 filestream_.open(filename, std::ios_base::binary);
696}
697
698void EmbedderTest::ClosePDFFileForWrite() {
699 filestream_.close();
700}