blob: a85f5c679c3d865a14ede50e1709d93f41a60553 [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) {
Tom Sepez6dc12402020-01-07 21:02:17 +000040 return EmbedderTest::BytesPerPixelForFormat(FPDFBitmap_GetFormat(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -040041}
42
Lei Zhang614f1a02019-05-30 00:03:53 +000043#if defined(OS_WIN)
44int CALLBACK GetRecordProc(HDC hdc,
45 HANDLETABLE* handle_table,
46 const ENHMETARECORD* record,
47 int objects_count,
48 LPARAM param) {
49 auto& records = *reinterpret_cast<std::vector<const ENHMETARECORD*>*>(param);
50 records.push_back(record);
51 return 1;
52}
53#endif // defined(OS_WIN)
54
thestigbcd3e532016-11-21 13:37:28 -080055} // namespace
56
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057EmbedderTest::EmbedderTest()
Lei Zhang0729be22018-02-05 21:13:51 +000058 : default_delegate_(pdfium::MakeUnique<EmbedderTest::Delegate>()),
59 delegate_(default_delegate_.get()) {
Nicolas Pena3ff54002017-07-05 11:55:35 -040060 FPDF_FILEWRITE::version = 1;
61 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -080062}
Tom Sepez96d13342015-01-16 14:59:26 -080063
Lei Zhangea208512019-12-18 00:42:11 +000064EmbedderTest::~EmbedderTest() = default;
Tom Sepezf288bb12015-11-20 12:12:46 -080065
66void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -070067 FPDF_LIBRARY_CONFIG config;
68 config.version = 2;
69 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -070070 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -070071 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -070072 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -080073
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
75 memset(info, 0, sizeof(UNSUPPORT_INFO));
76 info->version = 1;
77 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
78 FSDK_SetUnSpObjProcessHandler(info);
Henrique Nakashima9fa50362017-11-10 22:40:44 +000079
Lei Zhang0729be22018-02-05 21:13:51 +000080 saved_document_ = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081}
Tom Sepez96d13342015-01-16 14:59:26 -080082
83void EmbedderTest::TearDown() {
Lei Zhang75c81712018-02-08 17:22:39 +000084 // Use an EXPECT_EQ() here and continue to let TearDown() finish as cleanly as
85 // possible. This can fail when an ASSERT test fails in a test case.
86 EXPECT_EQ(0U, page_map_.size());
Lei Zhang9f72c452018-02-08 21:49:54 +000087 EXPECT_EQ(0U, saved_page_map_.size());
Lei Zhang75c81712018-02-08 17:22:39 +000088
Lei Zhang44005192020-04-17 01:35:43 +000089 if (document_)
Tom Sepez38f725f2019-12-04 00:26:34 +000090 CloseDocument();
Tom Sepezc46d0002015-11-30 15:46:36 -080091
Tom Sepez96d13342015-01-16 14:59:26 -080092 FPDFAvail_Destroy(avail_);
93 FPDF_DestroyLibrary();
Lei Zhangb3be4a12019-02-05 22:11:07 +000094 loader_.reset();
Tom Sepez96d13342015-01-16 14:59:26 -080095}
96
Lei Zhang378ec542018-10-18 19:15:47 +000097#ifdef PDF_ENABLE_V8
98void EmbedderTest::SetExternalIsolate(void* isolate) {
99 external_isolate_ = static_cast<v8::Isolate*>(isolate);
100}
101#endif // PDF_ENABLE_V8
102
Tom Sepezd483eb42016-01-06 10:03:59 -0800103bool EmbedderTest::CreateEmptyDocument() {
104 document_ = FPDF_CreateNewDocument();
105 if (!document_)
106 return false;
107
Tom Sepez0784c732018-04-23 18:02:57 +0000108 form_handle_ =
109 SetupFormFillEnvironment(document_, JavaScriptOption::kEnableJavaScript);
Tom Sepezd483eb42016-01-06 10:03:59 -0800110 return true;
111}
112
Lei Zhang208eecf2017-12-20 19:40:50 +0000113bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000114 return OpenDocumentWithOptions(filename, nullptr,
115 LinearizeOption::kDefaultLinearize,
116 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000117}
118
119bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000120 return OpenDocumentWithOptions(filename, nullptr,
121 LinearizeOption::kMustLinearize,
122 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000123}
124
125bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
126 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000127 return OpenDocumentWithOptions(filename, password,
128 LinearizeOption::kDefaultLinearize,
129 JavaScriptOption::kEnableJavaScript);
130}
131
132bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
133 return OpenDocumentWithOptions(filename, nullptr,
134 LinearizeOption::kDefaultLinearize,
135 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000136}
137
138bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
139 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000140 LinearizeOption linearize_option,
141 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800142 std::string file_path;
143 if (!PathService::GetTestFilePath(filename, &file_path))
144 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000145
Wei Li091f7a02015-11-09 12:09:55 -0800146 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400147 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800148 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800149
thestig29ce9232016-06-22 07:03:23 -0700150 EXPECT_TRUE(!loader_);
Lei Zhangb3be4a12019-02-05 22:11:07 +0000151 loader_ = pdfium::MakeUnique<TestLoader>(
152 pdfium::make_span(file_contents_.get(), file_length_));
Lei Zhang0729be22018-02-05 21:13:51 +0000153
154 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800155 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700156 file_access_.m_GetBlock = TestLoader::GetBlock;
Lei Zhangb3be4a12019-02-05 22:11:07 +0000157 file_access_.m_Param = loader_.get();
Lei Zhang0729be22018-02-05 21:13:51 +0000158
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300159 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000160 return OpenDocumentHelper(password, linearize_option, javascript_option,
161 fake_file_access_.get(), &document_, &avail_,
162 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400163}
Tom Sepez96d13342015-01-16 14:59:26 -0800164
Nicolas Pena56fc9722017-07-13 16:31:34 -0400165bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000166 LinearizeOption linearize_option,
167 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300168 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400169 FPDF_DOCUMENT* document,
170 FPDF_AVAIL* avail,
171 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300172 network_simulator->AddSegment(0, 1024);
173 network_simulator->SetRequestedDataAvailable();
174 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
175 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400176 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300177 int32_t nRet = PDF_DATA_NOTAVAIL;
178 while (nRet == PDF_DATA_NOTAVAIL) {
179 network_simulator->SetRequestedDataAvailable();
180 nRet =
181 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
182 }
183 if (nRet == PDF_DATA_ERROR)
184 return false;
185
Nicolas Pena56fc9722017-07-13 16:31:34 -0400186 *document = FPDFAvail_GetDocument(*avail, password);
187 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800188 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400189
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300190 nRet = PDF_DATA_NOTAVAIL;
191 while (nRet == PDF_DATA_NOTAVAIL) {
192 network_simulator->SetRequestedDataAvailable();
193 nRet =
194 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
195 }
196 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800197 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400198
199 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800200 for (int i = 0; i < page_count; ++i) {
201 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300202 while (nRet == PDF_DATA_NOTAVAIL) {
203 network_simulator->SetRequestedDataAvailable();
204 nRet = FPDFAvail_IsPageAvail(*avail, i,
205 network_simulator->GetDownloadHints());
206 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400207 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800208 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800209 }
210 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000211 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800212 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300213 network_simulator->SetWholeFileAvailable();
214 *document =
215 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400216 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800217 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800218 }
Tom Sepez0784c732018-04-23 18:02:57 +0000219 *form_handle = SetupFormFillEnvironment(*document, javascript_option);
220
Ryan Harrison854d71c2017-10-18 12:28:14 -0400221 int doc_type = FPDF_GetFormType(*document);
222 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
223 FPDF_LoadXFA(*document);
Tom Sepez0784c732018-04-23 18:02:57 +0000224
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000225 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800226 return true;
227}
Tom Sepez96d13342015-01-16 14:59:26 -0800228
Tom Sepez38f725f2019-12-04 00:26:34 +0000229void EmbedderTest::CloseDocument() {
Lei Zhang44005192020-04-17 01:35:43 +0000230 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepez38f725f2019-12-04 00:26:34 +0000231 FPDFDOC_ExitFormFillEnvironment(form_handle_);
232 form_handle_ = nullptr;
233
234 FPDF_CloseDocument(document_);
235 document_ = nullptr;
236}
237
Tom Sepez0784c732018-04-23 18:02:57 +0000238FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
239 FPDF_DOCUMENT doc,
240 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800241 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400242 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200243 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800244 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400245 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800246
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800247 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
248 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Aayush Dhirc63913e2020-02-12 09:45:34 +0000249 formfillinfo->version = form_fill_info_version_;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700250 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
251 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700252 formfillinfo->FFI_GetPage = GetPageTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000253 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000254 formfillinfo->FFI_DoGoToAction = DoGoToActionTrampoline;
Aayush Dhirc63913e2020-02-12 09:45:34 +0000255 formfillinfo->FFI_OnFocusChange = OnFocusChangeTrampoline;
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000256 formfillinfo->FFI_DoURIActionWithKeyboardModifier =
257 DoURIActionWithKeyboardModifierTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000258
Tom Sepez0784c732018-04-23 18:02:57 +0000259 if (javascript_option == JavaScriptOption::kEnableJavaScript)
260 formfillinfo->m_pJsPlatform = platform;
261
Nicolas Pena3ff54002017-07-05 11:55:35 -0400262 FPDF_FORMHANDLE form_handle =
263 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Tom Sepez92f92222020-01-07 22:16:07 +0000264 SetInitialFormFieldHighlight(form_handle);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400265 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800266}
267
Tom Sepezda8189e2015-01-30 14:41:50 -0800268void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400269 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800270 FORM_DoDocumentJSAction(form_handle_);
271 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800272}
273
274int EmbedderTest::GetFirstPageNum() {
275 int first_page = FPDFAvail_GetFirstPageNum(document_);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000276 (void)FPDFAvail_IsPageAvail(avail_, first_page,
277 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800278 return first_page;
279}
280
281int EmbedderTest::GetPageCount() {
282 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400283 for (int i = 0; i < page_count; ++i)
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000284 (void)FPDFAvail_IsPageAvail(avail_, i,
285 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800286 return page_count;
287}
288
Tom Sepezda8189e2015-01-30 14:41:50 -0800289FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez507d0192018-11-07 16:37:51 +0000290 return LoadPageCommon(page_number, true);
291}
292
293FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
294 return LoadPageCommon(page_number, false);
295}
296
297FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400298 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000299 ASSERT(page_number >= 0);
300 ASSERT(!pdfium::ContainsKey(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700301
Tom Sepez96d13342015-01-16 14:59:26 -0800302 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400303 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800304 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400305
Tom Sepez507d0192018-11-07 16:37:51 +0000306 if (do_events) {
307 FORM_OnAfterLoadPage(page, form_handle_);
308 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
309 }
weili0dadcc62016-08-23 21:10:57 -0700310 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700311 return page;
312}
313
Tom Sepezda8189e2015-01-30 14:41:50 -0800314void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Tom Sepez507d0192018-11-07 16:37:51 +0000315 UnloadPageCommon(page, true);
316}
Lei Zhang75c81712018-02-08 17:22:39 +0000317
Tom Sepez507d0192018-11-07 16:37:51 +0000318void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
319 UnloadPageCommon(page, false);
320}
321
322void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
323 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000324 int page_number = GetPageNumberForLoadedPage(page);
325 if (page_number < 0) {
326 NOTREACHED();
327 return;
328 }
Tom Sepez507d0192018-11-07 16:37:51 +0000329 if (do_events) {
330 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
331 FORM_OnBeforeClosePage(page, form_handle_);
332 }
Tom Sepez96d13342015-01-16 14:59:26 -0800333 FPDF_ClosePage(page);
Lei Zhang75c81712018-02-08 17:22:39 +0000334 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800335}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800336
Tom Sepez92f92222020-01-07 22:16:07 +0000337void EmbedderTest::SetInitialFormFieldHighlight(FPDF_FORMHANDLE form) {
338 FPDF_SetFormFieldHighlightColor(form, FPDF_FORMFIELD_UNKNOWN, 0xFFE4DD);
339 FPDF_SetFormFieldHighlightAlpha(form, 100);
340}
341
Tom Sepeze08d2b12018-04-25 18:49:32 +0000342ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000343 return RenderLoadedPageWithFlags(page, 0);
344}
345
Tom Sepeze08d2b12018-04-25 18:49:32 +0000346ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
347 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000348 if (GetPageNumberForLoadedPage(page) < 0) {
349 NOTREACHED();
350 return nullptr;
351 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000352 return RenderPageWithFlags(page, form_handle_, flags);
353}
354
Tom Sepeze08d2b12018-04-25 18:49:32 +0000355ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000356 return RenderSavedPageWithFlags(page, 0);
357}
358
Tom Sepeze08d2b12018-04-25 18:49:32 +0000359ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
360 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000361 if (GetPageNumberForSavedPage(page) < 0) {
362 NOTREACHED();
363 return nullptr;
364 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000365 return RenderPageWithFlags(page, saved_form_handle_, flags);
366}
367
368// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000369ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
370 FPDF_FORMHANDLE handle,
371 int flags) {
Lei Zhangdfa075b2019-12-05 21:52:43 +0000372 int width = static_cast<int>(FPDF_GetPageWidthF(page));
373 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhanga98e3662018-02-07 20:28:35 +0000374 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000375 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000376 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
377 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
378 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
379 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
380 return bitmap;
381}
382
Lei Zhang30ff2532019-01-31 21:37:55 +0000383// static
384ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) {
385 return RenderPageWithFlags(page, nullptr, 0);
386}
387
Lei Zhangd69e0652019-04-13 00:39:35 +0000388#if defined(OS_WIN)
389// static
390std::vector<uint8_t> EmbedderTest::RenderPageWithFlagsToEmf(FPDF_PAGE page,
391 int flags) {
392 HDC dc = CreateEnhMetaFileA(nullptr, nullptr, nullptr, nullptr);
393
Lei Zhangdfa075b2019-12-05 21:52:43 +0000394 int width = static_cast<int>(FPDF_GetPageWidthF(page));
395 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhangd69e0652019-04-13 00:39:35 +0000396 HRGN rgn = CreateRectRgn(0, 0, width, height);
397 SelectClipRgn(dc, rgn);
398 DeleteObject(rgn);
399
400 SelectObject(dc, GetStockObject(NULL_PEN));
401 SelectObject(dc, GetStockObject(WHITE_BRUSH));
402 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
403 Rectangle(dc, 0, 0, width + 1, height + 1);
404
405 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, flags);
406
407 HENHMETAFILE emf = CloseEnhMetaFile(dc);
408 size_t size_in_bytes = GetEnhMetaFileBits(emf, 0, nullptr);
409 std::vector<uint8_t> buffer(size_in_bytes);
410 GetEnhMetaFileBits(emf, size_in_bytes, buffer.data());
411 DeleteEnhMetaFile(emf);
412 return buffer;
413}
Lei Zhang614f1a02019-05-30 00:03:53 +0000414
415// static
416std::string EmbedderTest::GetPostScriptFromEmf(
Tom Sepez87b9e3a2019-11-20 23:45:43 +0000417 pdfium::span<const uint8_t> emf_data) {
Lei Zhang614f1a02019-05-30 00:03:53 +0000418 // This comes from Emf::InitFromData() in Chromium.
419 HENHMETAFILE emf = SetEnhMetaFileBits(emf_data.size(), emf_data.data());
420 if (!emf)
421 return std::string();
422
423 // This comes from Emf::Enumerator::Enumerator() in Chromium.
424 std::vector<const ENHMETARECORD*> records;
425 if (!EnumEnhMetaFile(nullptr, emf, &GetRecordProc, &records, nullptr)) {
426 DeleteEnhMetaFile(emf);
427 return std::string();
428 }
429
430 // This comes from PostScriptMetaFile::SafePlayback() in Chromium.
431 std::string ps_data;
432 for (const auto* record : records) {
433 if (record->iType != EMR_GDICOMMENT)
434 continue;
435
436 // PostScript data is encapsulated inside EMF comment records.
437 // The first two bytes of the comment indicate the string length. The rest
438 // is the actual string data.
439 const auto* comment = reinterpret_cast<const EMRGDICOMMENT*>(record);
440 const char* data = reinterpret_cast<const char*>(comment->Data);
441 uint16_t size = *reinterpret_cast<const uint16_t*>(data);
442 data += 2;
443 ps_data.append(data, size);
444 }
445 DeleteEnhMetaFile(emf);
446 return ps_data;
447}
Lei Zhangd69e0652019-04-13 00:39:35 +0000448#endif // defined(OS_WIN)
449
Lei Zhang0b494052019-01-31 21:41:15 +0000450FPDF_DOCUMENT EmbedderTest::OpenSavedDocument() {
451 return OpenSavedDocumentWithPassword(nullptr);
452}
453
Tom Sepez6dc12402020-01-07 21:02:17 +0000454// static
455int EmbedderTest::BytesPerPixelForFormat(int format) {
456 switch (format) {
457 case FPDFBitmap_Gray:
458 return 1;
459 case FPDFBitmap_BGR:
460 return 3;
461 case FPDFBitmap_BGRx:
462 case FPDFBitmap_BGRA:
463 return 4;
464 default:
465 NOTREACHED();
466 return 0;
467 }
468}
469
Lei Zhang0b494052019-01-31 21:41:15 +0000470FPDF_DOCUMENT EmbedderTest::OpenSavedDocumentWithPassword(
471 const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300472 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000473 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300474 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000475 // Copy data to prevent clearing it before saved document close.
476 saved_document_file_data_ = data_string_;
477 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400478
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300479 saved_fake_file_access_ =
480 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
481
Tom Sepez0784c732018-04-23 18:02:57 +0000482 EXPECT_TRUE(OpenDocumentHelper(
483 password, LinearizeOption::kDefaultLinearize,
484 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
485 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000486 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400487}
488
489void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000490 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400491
Lei Zhang0729be22018-02-05 21:13:51 +0000492 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
493 FPDF_CloseDocument(saved_document_);
494 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400495
Lei Zhang0729be22018-02-05 21:13:51 +0000496 saved_form_handle_ = nullptr;
497 saved_document_ = nullptr;
498 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400499}
500
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000501FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000502 ASSERT(saved_form_handle_);
503 ASSERT(page_number >= 0);
504 ASSERT(!pdfium::ContainsKey(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400505
Lei Zhang0729be22018-02-05 21:13:51 +0000506 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000507 if (!page)
508 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400509
Lei Zhang9f72c452018-02-08 21:49:54 +0000510 FORM_OnAfterLoadPage(page, saved_form_handle_);
511 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
512 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000513 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400514}
515
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000516void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000517 ASSERT(saved_form_handle_);
518
519 int page_number = GetPageNumberForSavedPage(page);
520 if (page_number < 0) {
521 NOTREACHED();
522 return;
523 }
524
525 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
526 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000527 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000528
529 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400530}
531
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000532void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
533 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400534 int height,
535 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000536 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000537 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400538
Tom Sepeze08d2b12018-04-25 18:49:32 +0000539 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000540 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400541}
542
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400543void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Lei Zhang0b494052019-01-31 21:41:15 +0000544 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000545 FPDF_PAGE page = LoadSavedPage(0);
546 VerifySavedRendering(page, width, height, md5);
547 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400548 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400549}
550
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300551void EmbedderTest::SetWholeFileAvailable() {
552 ASSERT(fake_file_access_);
553 fake_file_access_->SetWholeFileAvailable();
554}
555
weili0dadcc62016-08-23 21:10:57 -0700556FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700557 FPDF_DOCUMENT document,
558 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700559 EmbedderTest* test = static_cast<EmbedderTest*>(info);
560 auto it = test->page_map_.find(page_index);
561 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700562}
563
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800564// static
565void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
566 int type) {
567 EmbedderTest* test = static_cast<EmbedderTest*>(info);
568 test->delegate_->UnsupportedHandler(type);
569}
570
571// static
572int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
573 FPDF_WIDESTRING message,
574 FPDF_WIDESTRING title,
575 int type,
576 int icon) {
577 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
578 return test->delegate_->Alert(message, title, type, icon);
579}
580
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700581// static
582int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700583 int msecs,
584 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700585 EmbedderTest* test = static_cast<EmbedderTest*>(info);
586 return test->delegate_->SetTimer(msecs, fn);
587}
588
589// static
590void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
591 EmbedderTest* test = static_cast<EmbedderTest*>(info);
592 return test->delegate_->KillTimer(id);
593}
594
Tom Sepez396e8722015-09-09 10:16:08 -0700595// static
596FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
597 FPDF_DOCUMENT document,
598 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700599 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
600 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700601}
602
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000603// static
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000604void EmbedderTest::DoURIActionTrampoline(FPDF_FORMFILLINFO* info,
605 FPDF_BYTESTRING uri) {
606 EmbedderTest* test = static_cast<EmbedderTest*>(info);
607 return test->delegate_->DoURIAction(uri);
608}
609
610// static
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000611void EmbedderTest::DoGoToActionTrampoline(FPDF_FORMFILLINFO* info,
612 int page_index,
613 int zoom_mode,
614 float* pos_array,
615 int array_size) {
616 EmbedderTest* test = static_cast<EmbedderTest*>(info);
617 return test->delegate_->DoGoToAction(info, page_index, zoom_mode, pos_array,
618 array_size);
619}
620
621// static
Aayush Dhirc63913e2020-02-12 09:45:34 +0000622void EmbedderTest::OnFocusChangeTrampoline(FPDF_FORMFILLINFO* info,
623 FPDF_ANNOTATION annot,
624 int page_index) {
625 EmbedderTest* test = static_cast<EmbedderTest*>(info);
626 return test->delegate_->OnFocusChange(info, annot, page_index);
627}
628
629// static
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000630void EmbedderTest::DoURIActionWithKeyboardModifierTrampoline(
631 FPDF_FORMFILLINFO* info,
632 FPDF_BYTESTRING uri,
633 int modifiers) {
634 EmbedderTest* test = static_cast<EmbedderTest*>(info);
635 return test->delegate_->DoURIActionWithKeyboardModifier(info, uri, modifiers);
636}
637
638// static
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000639std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Lei Zhangbf980142019-12-20 01:05:42 +0000640 int stride = FPDFBitmap_GetStride(bitmap);
641 int usable_bytes_per_row =
642 GetBitmapBytesPerPixel(bitmap) * FPDFBitmap_GetWidth(bitmap);
643 int height = FPDFBitmap_GetHeight(bitmap);
644 auto span = pdfium::make_span(
645 static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)), stride * height);
646
647 CRYPT_md5_context context = CRYPT_MD5Start();
648 for (int i = 0; i < height; ++i)
649 CRYPT_MD5Update(&context, span.subspan(i * stride, usable_bytes_per_row));
Dan Sinclair957480c2017-06-13 15:21:14 -0400650 uint8_t digest[16];
Lei Zhangbf980142019-12-20 01:05:42 +0000651 CRYPT_MD5Finish(&context, digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400652 return CryptToBase16(digest);
653}
654
Henrique Nakashimadb269572018-01-16 19:02:15 +0000655#ifndef NDEBUG
656// static
657void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
658 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000659 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000660}
661#endif
662
thestigbcd3e532016-11-21 13:37:28 -0800663// static
664void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
665 int expected_width,
666 int expected_height,
667 const char* expected_md5sum) {
668 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
669 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400670
671 // The expected stride is calculated using the same formula as in
672 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
673 const int expected_stride =
674 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800675 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
676
677 if (!expected_md5sum)
678 return;
679
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000680 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800681}
682
Nicolas Pena3ff54002017-07-05 11:55:35 -0400683// static
684int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
685 const void* data,
686 unsigned long size) {
687 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000688
Lei Zhang0729be22018-02-05 21:13:51 +0000689 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000690
691 if (pThis->filestream_.is_open())
692 pThis->filestream_.write(static_cast<const char*>(data), size);
693
Nicolas Pena3ff54002017-07-05 11:55:35 -0400694 return 1;
695}
696
697// static
698int EmbedderTest::GetBlockFromString(void* param,
699 unsigned long pos,
700 unsigned char* buf,
701 unsigned long size) {
702 std::string* new_file = static_cast<std::string*>(param);
Lei Zhangcef91f12019-01-08 21:32:51 +0000703 if (!new_file || pos + size < pos) {
704 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400705 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000706 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400707
Lei Zhangcef91f12019-01-08 21:32:51 +0000708 if (pos + size > new_file->size()) {
709 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400710 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000711 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400712
713 memcpy(buf, new_file->data() + pos, size);
714 return 1;
715}
Lei Zhang75c81712018-02-08 17:22:39 +0000716
Lei Zhang9f72c452018-02-08 21:49:54 +0000717// static
718int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
719 FPDF_PAGE page) {
720 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000721 if (it.second == page) {
722 int page_number = it.first;
723 ASSERT(page_number >= 0);
724 return page_number;
725 }
726 }
727 return -1;
728}
Lei Zhang9f72c452018-02-08 21:49:54 +0000729
730int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
731 return GetPageNumberForPage(page_map_, page);
732}
733
734int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
735 return GetPageNumberForPage(saved_page_map_, page);
736}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000737
Lei Zhang81a799e2019-12-16 17:37:13 +0000738#ifndef NDEBUG
739void EmbedderTest::OpenPDFFileForWrite(const std::string& filename) {
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000740 filestream_.open(filename, std::ios_base::binary);
741}
742
743void EmbedderTest::ClosePDFFileForWrite() {
744 filestream_.close();
745}
Lei Zhang81a799e2019-12-16 17:37:13 +0000746#endif