blob: 20f1cadf09588055def3ff46465aa29899615cd5 [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
Chris Palmer2b797292017-04-06 14:45:39 -070012#include "core/fxcrt/fx_memory.h"
Wei Li091f7a02015-11-09 12:09:55 -080013#include "testing/utils/path_service.h"
Tom Sepezd831dc72015-10-19 16:04:22 -070014
Lei Zhang8241df72015-11-06 14:38:48 -080015#ifdef PDF_ENABLE_V8
16#include "v8/include/libplatform/libplatform.h"
17#endif
18
Tom Sepezd831dc72015-10-19 16:04:22 -070019namespace {
20
21#ifdef PDF_ENABLE_V8
22#ifdef V8_USE_EXTERNAL_STARTUP_DATA
23// Returns the full path for an external V8 data file based on either
24// the currect exectuable path or an explicit override.
25std::string GetFullPathForSnapshotFile(const std::string& exe_path,
26 const std::string& bin_dir,
27 const std::string& filename) {
28 std::string result;
29 if (!bin_dir.empty()) {
30 result = bin_dir;
31 if (*bin_dir.rbegin() != PATH_SEPARATOR) {
32 result += PATH_SEPARATOR;
33 }
34 } else if (!exe_path.empty()) {
35 size_t last_separator = exe_path.rfind(PATH_SEPARATOR);
36 if (last_separator != std::string::npos) {
37 result = exe_path.substr(0, last_separator + 1);
38 }
39 }
40 result += filename;
41 return result;
42}
43
44bool GetExternalData(const std::string& exe_path,
45 const std::string& bin_dir,
46 const std::string& filename,
47 v8::StartupData* result_data) {
48 std::string full_path =
49 GetFullPathForSnapshotFile(exe_path, bin_dir, filename);
50 size_t data_length = 0;
Lei Zhangbcc03a42016-01-07 00:48:00 -080051 std::unique_ptr<char, pdfium::FreeDeleter> data_buffer =
52 GetFileContents(full_path.c_str(), &data_length);
53 if (!data_buffer)
Tom Sepezd831dc72015-10-19 16:04:22 -070054 return false;
Lei Zhangbcc03a42016-01-07 00:48:00 -080055
56 result_data->data = data_buffer.release();
Tom Sepezd831dc72015-10-19 16:04:22 -070057 result_data->raw_size = data_length;
58 return true;
59}
60#endif // V8_USE_EXTERNAL_STARTUP_DATA
61
jochen9e077d22016-06-09 02:51:13 -070062void InitializeV8Common(const char* exe_path, v8::Platform** platform) {
63 v8::V8::InitializeICUDefaultLocation(exe_path);
Tom Sepezd831dc72015-10-19 16:04:22 -070064
65 *platform = v8::platform::CreateDefaultPlatform();
66 v8::V8::InitializePlatform(*platform);
Tom Sepezd831dc72015-10-19 16:04:22 -070067
68 // By enabling predictable mode, V8 won't post any background tasks.
tsepez98822432017-01-11 05:53:10 -080069 // By enabling GC, it makes it easier to chase use-after-free.
70 const char v8_flags[] = "--predictable --expose-gc";
71 v8::V8::SetFlagsFromString(v8_flags, static_cast<int>(strlen(v8_flags)));
72 v8::V8::Initialize();
Tom Sepezd831dc72015-10-19 16:04:22 -070073}
74#endif // PDF_ENABLE_V8
75
76} // namespace
77
Tom Sepez0aa35312016-01-06 10:16:32 -080078std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
79 size_t* retlen) {
Tom Sepezd831dc72015-10-19 16:04:22 -070080 FILE* file = fopen(filename, "rb");
81 if (!file) {
82 fprintf(stderr, "Failed to open: %s\n", filename);
83 return nullptr;
84 }
85 (void)fseek(file, 0, SEEK_END);
86 size_t file_length = ftell(file);
87 if (!file_length) {
88 return nullptr;
89 }
90 (void)fseek(file, 0, SEEK_SET);
Tom Sepez0aa35312016-01-06 10:16:32 -080091 std::unique_ptr<char, pdfium::FreeDeleter> buffer(
92 static_cast<char*>(malloc(file_length)));
Tom Sepezd831dc72015-10-19 16:04:22 -070093 if (!buffer) {
94 return nullptr;
95 }
Tom Sepez0aa35312016-01-06 10:16:32 -080096 size_t bytes_read = fread(buffer.get(), 1, file_length, file);
Tom Sepezd831dc72015-10-19 16:04:22 -070097 (void)fclose(file);
98 if (bytes_read != file_length) {
99 fprintf(stderr, "Failed to read: %s\n", filename);
Tom Sepezd831dc72015-10-19 16:04:22 -0700100 return nullptr;
101 }
102 *retlen = bytes_read;
103 return buffer;
104}
105
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800106std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) {
Lei Zhang79e893a2015-11-04 16:02:47 -0800107 if (!wstr)
108 return nullptr;
109
110 size_t characters = 0;
111 while (wstr[characters])
112 ++characters;
113
114 std::wstring platform_string(characters, L'\0');
115 for (size_t i = 0; i < characters + 1; ++i) {
116 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]);
117 platform_string[i] = ptr[0] + 256 * ptr[1];
118 }
119 return platform_string;
120}
121
tsepezf09bdfa2016-04-18 16:08:26 -0700122std::vector<std::string> StringSplit(const std::string& str, char delimiter) {
123 std::vector<std::string> result;
124 size_t pos = 0;
125 while (1) {
126 size_t found = str.find(delimiter, pos);
127 if (found == std::string::npos)
128 break;
129
130 result.push_back(str.substr(pos, found - pos));
131 pos = found + 1;
132 }
133 result.push_back(str.substr(pos));
134 return result;
135}
136
Tom Sepez0aa35312016-01-06 10:16:32 -0800137std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
138 const std::wstring& wstr) {
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800139 size_t length = sizeof(uint16_t) * (wstr.length() + 1);
Tom Sepez0aa35312016-01-06 10:16:32 -0800140 std::unique_ptr<unsigned short, pdfium::FreeDeleter> result(
141 static_cast<unsigned short*>(malloc(length)));
142 char* ptr = reinterpret_cast<char*>(result.get());
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800143 size_t i = 0;
144 for (wchar_t w : wstr) {
145 ptr[i++] = w & 0xff;
146 ptr[i++] = (w >> 8) & 0xff;
147 }
148 ptr[i++] = 0;
149 ptr[i] = 0;
Tom Sepez0aa35312016-01-06 10:16:32 -0800150 return result;
Tom Sepez8ab45ea2016-01-05 10:17:30 -0800151}
152
Tom Sepezd831dc72015-10-19 16:04:22 -0700153#ifdef PDF_ENABLE_V8
154#ifdef V8_USE_EXTERNAL_STARTUP_DATA
155bool InitializeV8ForPDFium(const std::string& exe_path,
156 const std::string& bin_dir,
157 v8::StartupData* natives_blob,
158 v8::StartupData* snapshot_blob,
159 v8::Platform** platform) {
jochen9e077d22016-06-09 02:51:13 -0700160 InitializeV8Common(exe_path.c_str(), platform);
thestigc08cd7a2016-06-27 09:47:59 -0700161 if (natives_blob && snapshot_blob) {
162 if (!GetExternalData(exe_path, bin_dir, "natives_blob.bin", natives_blob))
163 return false;
164 if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob))
165 return false;
166 v8::V8::SetNativesDataBlob(natives_blob);
167 v8::V8::SetSnapshotDataBlob(snapshot_blob);
168 }
Tom Sepezd831dc72015-10-19 16:04:22 -0700169 return true;
170}
171#else // V8_USE_EXTERNAL_STARTUP_DATA
jochen9e077d22016-06-09 02:51:13 -0700172bool InitializeV8ForPDFium(const std::string& exe_path,
173 v8::Platform** platform) {
174 InitializeV8Common(exe_path.c_str(), platform);
Tom Sepezd831dc72015-10-19 16:04:22 -0700175 return true;
176}
177#endif // V8_USE_EXTERNAL_STARTUP_DATA
178#endif // PDF_ENABLE_V8
179
180TestLoader::TestLoader(const char* pBuf, size_t len)
181 : m_pBuf(pBuf), m_Len(len) {
182}
183
184// static
185int TestLoader::GetBlock(void* param,
186 unsigned long pos,
187 unsigned char* pBuf,
188 unsigned long size) {
189 TestLoader* pLoader = static_cast<TestLoader*>(param);
190 if (pos + size < pos || pos + size > pLoader->m_Len)
191 return 0;
192
193 memcpy(pBuf, pLoader->m_pBuf + pos, size);
194 return 1;
195}
Tom Sepez0aec19b2016-01-07 12:22:44 -0800196
197TestSaver::TestSaver() {
198 FPDF_FILEWRITE::version = 1;
199 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
200}
201
202void TestSaver::ClearString() {
203 m_String.clear();
204}
205
206// static
207int TestSaver::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
208 const void* data,
209 unsigned long size) {
210 TestSaver* pThis = static_cast<TestSaver*>(pFileWrite);
211 pThis->m_String.append(static_cast<const char*>(data), size);
212 return 1;
213}
Chris Palmere4b035b2017-03-26 15:48:34 -0700214
215namespace pdfium {
216
217void FPDF_Test::SetUp() {
Chris Palmer2b797292017-04-06 14:45:39 -0700218 FXMEM_InitalizePartitionAlloc();
Chris Palmere4b035b2017-03-26 15:48:34 -0700219}
220
221void FPDF_Test::TearDown() {
Chris Palmere4b035b2017-03-26 15:48:34 -0700222}
223
224} // namespace pdfium