blob: a530b3ea4c401724389943c4256979f2afc8a050 [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
jochen9e077d22016-06-09 02:51:13 -070061void InitializeV8Common(const char* exe_path, v8::Platform** platform) {
62 v8::V8::InitializeICUDefaultLocation(exe_path);
Tom Sepezd831dc72015-10-19 16:04:22 -070063
64 *platform = v8::platform::CreateDefaultPlatform();
65 v8::V8::InitializePlatform(*platform);
Tom Sepezd831dc72015-10-19 16:04:22 -070066
67 // By enabling predictable mode, V8 won't post any background tasks.
tsepez98822432017-01-11 05:53:10 -080068 // By enabling GC, it makes it easier to chase use-after-free.
69 const char v8_flags[] = "--predictable --expose-gc";
70 v8::V8::SetFlagsFromString(v8_flags, static_cast<int>(strlen(v8_flags)));
71 v8::V8::Initialize();
Tom Sepezd831dc72015-10-19 16:04:22 -070072}
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
tsepezf09bdfa2016-04-18 16:08:26 -0700121std::vector<std::string> StringSplit(const std::string& str, char delimiter) {
122 std::vector<std::string> result;
123 size_t pos = 0;
124 while (1) {
125 size_t found = str.find(delimiter, pos);
126 if (found == std::string::npos)
127 break;
128
129 result.push_back(str.substr(pos, found - pos));
130 pos = found + 1;
131 }
132 result.push_back(str.substr(pos));
133 return result;
134}
135
Tom Sepez0aa35312016-01-06 10:16:32 -0800136std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
137 const std::wstring& wstr) {
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800138 size_t length = sizeof(uint16_t) * (wstr.length() + 1);
Tom Sepez0aa35312016-01-06 10:16:32 -0800139 std::unique_ptr<unsigned short, pdfium::FreeDeleter> result(
140 static_cast<unsigned short*>(malloc(length)));
141 char* ptr = reinterpret_cast<char*>(result.get());
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800142 size_t i = 0;
143 for (wchar_t w : wstr) {
144 ptr[i++] = w & 0xff;
145 ptr[i++] = (w >> 8) & 0xff;
146 }
147 ptr[i++] = 0;
148 ptr[i] = 0;
Tom Sepez0aa35312016-01-06 10:16:32 -0800149 return result;
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800150}
151
Tom Sepezd831dc72015-10-19 16:04:22 -0700152#ifdef PDF_ENABLE_V8
153#ifdef V8_USE_EXTERNAL_STARTUP_DATA
154bool InitializeV8ForPDFium(const std::string& exe_path,
155 const std::string& bin_dir,
156 v8::StartupData* natives_blob,
157 v8::StartupData* snapshot_blob,
158 v8::Platform** platform) {
jochen9e077d22016-06-09 02:51:13 -0700159 InitializeV8Common(exe_path.c_str(), platform);
thestigc08cd7a2016-06-27 09:47:59 -0700160 if (natives_blob && snapshot_blob) {
161 if (!GetExternalData(exe_path, bin_dir, "natives_blob.bin", natives_blob))
162 return false;
163 if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob))
164 return false;
165 v8::V8::SetNativesDataBlob(natives_blob);
166 v8::V8::SetSnapshotDataBlob(snapshot_blob);
167 }
Tom Sepezd831dc72015-10-19 16:04:22 -0700168 return true;
169}
170#else // V8_USE_EXTERNAL_STARTUP_DATA
jochen9e077d22016-06-09 02:51:13 -0700171bool InitializeV8ForPDFium(const std::string& exe_path,
172 v8::Platform** platform) {
173 InitializeV8Common(exe_path.c_str(), platform);
Tom Sepezd831dc72015-10-19 16:04:22 -0700174 return true;
175}
176#endif // V8_USE_EXTERNAL_STARTUP_DATA
177#endif // PDF_ENABLE_V8
178
179TestLoader::TestLoader(const char* pBuf, size_t len)
180 : m_pBuf(pBuf), m_Len(len) {
181}
182
183// static
184int TestLoader::GetBlock(void* param,
185 unsigned long pos,
186 unsigned char* pBuf,
187 unsigned long size) {
188 TestLoader* pLoader = static_cast<TestLoader*>(param);
189 if (pos + size < pos || pos + size > pLoader->m_Len)
190 return 0;
191
192 memcpy(pBuf, pLoader->m_pBuf + pos, size);
193 return 1;
194}
Tom Sepez0aec19b2016-01-07 12:22:44 -0800195
196TestSaver::TestSaver() {
197 FPDF_FILEWRITE::version = 1;
198 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
199}
200
201void TestSaver::ClearString() {
202 m_String.clear();
203}
204
205// static
206int TestSaver::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
207 const void* data,
208 unsigned long size) {
209 TestSaver* pThis = static_cast<TestSaver*>(pFileWrite);
210 pThis->m_String.append(static_cast<const char*>(data), size);
211 return 1;
212}