blob: 1ccd32f084f10a9e45a3040dc07cf12adeee5d95 [file] [log] [blame]
Tom Sepezd831dc72015-10-19 16:04:22 -07001// Copyright 2015 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Wei Li091f7a02015-11-09 12:09:55 -08005#include "testing/test_support.h"
Tom Sepezd831dc72015-10-19 16:04:22 -07006
7#include <stdio.h>
8#include <string.h>
9
Dan Sinclair3ebd1212016-03-09 09:59:23 -050010#include <string>
11
Wei Li091f7a02015-11-09 12:09:55 -080012#include "testing/utils/path_service.h"
Tom Sepezd831dc72015-10-19 16:04:22 -070013
Lei Zhang8241df72015-11-06 14:38:48 -080014#ifdef PDF_ENABLE_V8
15#include "v8/include/libplatform/libplatform.h"
16#endif
17
Tom Sepezd831dc72015-10-19 16:04:22 -070018namespace {
19
20#ifdef PDF_ENABLE_V8
21#ifdef V8_USE_EXTERNAL_STARTUP_DATA
22// Returns the full path for an external V8 data file based on either
23// the currect exectuable path or an explicit override.
24std::string GetFullPathForSnapshotFile(const std::string& exe_path,
25 const std::string& bin_dir,
26 const std::string& filename) {
27 std::string result;
28 if (!bin_dir.empty()) {
29 result = bin_dir;
30 if (*bin_dir.rbegin() != PATH_SEPARATOR) {
31 result += PATH_SEPARATOR;
32 }
33 } else if (!exe_path.empty()) {
34 size_t last_separator = exe_path.rfind(PATH_SEPARATOR);
35 if (last_separator != std::string::npos) {
36 result = exe_path.substr(0, last_separator + 1);
37 }
38 }
39 result += filename;
40 return result;
41}
42
43bool GetExternalData(const std::string& exe_path,
44 const std::string& bin_dir,
45 const std::string& filename,
46 v8::StartupData* result_data) {
47 std::string full_path =
48 GetFullPathForSnapshotFile(exe_path, bin_dir, filename);
49 size_t data_length = 0;
Lei Zhangbcc03a42016-01-07 00:48:00 -080050 std::unique_ptr<char, pdfium::FreeDeleter> data_buffer =
51 GetFileContents(full_path.c_str(), &data_length);
52 if (!data_buffer)
Tom Sepezd831dc72015-10-19 16:04:22 -070053 return false;
Lei Zhangbcc03a42016-01-07 00:48:00 -080054
55 result_data->data = data_buffer.release();
Tom Sepezd831dc72015-10-19 16:04:22 -070056 result_data->raw_size = data_length;
57 return true;
58}
59#endif // V8_USE_EXTERNAL_STARTUP_DATA
60
61void InitializeV8Common(v8::Platform** platform) {
62 v8::V8::InitializeICU();
63
64 *platform = v8::platform::CreateDefaultPlatform();
65 v8::V8::InitializePlatform(*platform);
66 v8::V8::Initialize();
67
68 // By enabling predictable mode, V8 won't post any background tasks.
69 const char predictable_flag[] = "--predictable";
70 v8::V8::SetFlagsFromString(predictable_flag,
71 static_cast<int>(strlen(predictable_flag)));
72}
73#endif // PDF_ENABLE_V8
74
75} // namespace
76
Tom Sepez0aa35312016-01-06 10:16:32 -080077std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
78 size_t* retlen) {
Tom Sepezd831dc72015-10-19 16:04:22 -070079 FILE* file = fopen(filename, "rb");
80 if (!file) {
81 fprintf(stderr, "Failed to open: %s\n", filename);
82 return nullptr;
83 }
84 (void)fseek(file, 0, SEEK_END);
85 size_t file_length = ftell(file);
86 if (!file_length) {
87 return nullptr;
88 }
89 (void)fseek(file, 0, SEEK_SET);
Tom Sepez0aa35312016-01-06 10:16:32 -080090 std::unique_ptr<char, pdfium::FreeDeleter> buffer(
91 static_cast<char*>(malloc(file_length)));
Tom Sepezd831dc72015-10-19 16:04:22 -070092 if (!buffer) {
93 return nullptr;
94 }
Tom Sepez0aa35312016-01-06 10:16:32 -080095 size_t bytes_read = fread(buffer.get(), 1, file_length, file);
Tom Sepezd831dc72015-10-19 16:04:22 -070096 (void)fclose(file);
97 if (bytes_read != file_length) {
98 fprintf(stderr, "Failed to read: %s\n", filename);
Tom Sepezd831dc72015-10-19 16:04:22 -070099 return nullptr;
100 }
101 *retlen = bytes_read;
102 return buffer;
103}
104
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800105std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) {
Lei Zhang79e893a2015-11-04 16:02:47 -0800106 if (!wstr)
107 return nullptr;
108
109 size_t characters = 0;
110 while (wstr[characters])
111 ++characters;
112
113 std::wstring platform_string(characters, L'\0');
114 for (size_t i = 0; i < characters + 1; ++i) {
115 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]);
116 platform_string[i] = ptr[0] + 256 * ptr[1];
117 }
118 return platform_string;
119}
120
Tom Sepez0aa35312016-01-06 10:16:32 -0800121std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
122 const std::wstring& wstr) {
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800123 size_t length = sizeof(uint16_t) * (wstr.length() + 1);
Tom Sepez0aa35312016-01-06 10:16:32 -0800124 std::unique_ptr<unsigned short, pdfium::FreeDeleter> result(
125 static_cast<unsigned short*>(malloc(length)));
126 char* ptr = reinterpret_cast<char*>(result.get());
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800127 size_t i = 0;
128 for (wchar_t w : wstr) {
129 ptr[i++] = w & 0xff;
130 ptr[i++] = (w >> 8) & 0xff;
131 }
132 ptr[i++] = 0;
133 ptr[i] = 0;
Tom Sepez0aa35312016-01-06 10:16:32 -0800134 return result;
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800135}
136
Tom Sepezd831dc72015-10-19 16:04:22 -0700137#ifdef PDF_ENABLE_V8
138#ifdef V8_USE_EXTERNAL_STARTUP_DATA
139bool InitializeV8ForPDFium(const std::string& exe_path,
140 const std::string& bin_dir,
141 v8::StartupData* natives_blob,
142 v8::StartupData* snapshot_blob,
143 v8::Platform** platform) {
144 InitializeV8Common(platform);
145 if (!GetExternalData(exe_path, bin_dir, "natives_blob.bin", natives_blob))
146 return false;
147 if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob))
148 return false;
149 v8::V8::SetNativesDataBlob(natives_blob);
150 v8::V8::SetSnapshotDataBlob(snapshot_blob);
151 return true;
152}
153#else // V8_USE_EXTERNAL_STARTUP_DATA
154bool InitializeV8ForPDFium(v8::Platform** platform) {
155 InitializeV8Common(platform);
156 return true;
157}
158#endif // V8_USE_EXTERNAL_STARTUP_DATA
159#endif // PDF_ENABLE_V8
160
161TestLoader::TestLoader(const char* pBuf, size_t len)
162 : m_pBuf(pBuf), m_Len(len) {
163}
164
165// static
166int TestLoader::GetBlock(void* param,
167 unsigned long pos,
168 unsigned char* pBuf,
169 unsigned long size) {
170 TestLoader* pLoader = static_cast<TestLoader*>(param);
171 if (pos + size < pos || pos + size > pLoader->m_Len)
172 return 0;
173
174 memcpy(pBuf, pLoader->m_pBuf + pos, size);
175 return 1;
176}
Tom Sepez0aec19b2016-01-07 12:22:44 -0800177
178TestSaver::TestSaver() {
179 FPDF_FILEWRITE::version = 1;
180 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
181}
182
183void TestSaver::ClearString() {
184 m_String.clear();
185}
186
187// static
188int TestSaver::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
189 const void* data,
190 unsigned long size) {
191 TestSaver* pThis = static_cast<TestSaver*>(pFileWrite);
192 pThis->m_String.append(static_cast<const char*>(data), size);
193 return 1;
194}