Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 1 | // 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 Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 5 | #include "testing/test_support.h" |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 10 | #include <string> |
| 11 | |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 12 | #include "testing/utils/path_service.h" |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 13 | |
Lei Zhang | 8241df7 | 2015-11-06 14:38:48 -0800 | [diff] [blame] | 14 | #ifdef PDF_ENABLE_V8 |
| 15 | #include "v8/include/libplatform/libplatform.h" |
| 16 | #endif |
| 17 | |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 18 | namespace { |
| 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. |
| 24 | std::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 | |
| 43 | bool 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 Zhang | bcc03a4 | 2016-01-07 00:48:00 -0800 | [diff] [blame] | 50 | std::unique_ptr<char, pdfium::FreeDeleter> data_buffer = |
| 51 | GetFileContents(full_path.c_str(), &data_length); |
| 52 | if (!data_buffer) |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 53 | return false; |
Lei Zhang | bcc03a4 | 2016-01-07 00:48:00 -0800 | [diff] [blame] | 54 | |
| 55 | result_data->data = data_buffer.release(); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 56 | result_data->raw_size = data_length; |
| 57 | return true; |
| 58 | } |
| 59 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 60 | |
jochen | 9e077d2 | 2016-06-09 02:51:13 -0700 | [diff] [blame] | 61 | void InitializeV8Common(const char* exe_path, v8::Platform** platform) { |
| 62 | v8::V8::InitializeICUDefaultLocation(exe_path); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 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 Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 77 | std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename, |
| 78 | size_t* retlen) { |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 79 | 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 Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 90 | std::unique_ptr<char, pdfium::FreeDeleter> buffer( |
| 91 | static_cast<char*>(malloc(file_length))); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 92 | if (!buffer) { |
| 93 | return nullptr; |
| 94 | } |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 95 | size_t bytes_read = fread(buffer.get(), 1, file_length, file); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 96 | (void)fclose(file); |
| 97 | if (bytes_read != file_length) { |
| 98 | fprintf(stderr, "Failed to read: %s\n", filename); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 99 | return nullptr; |
| 100 | } |
| 101 | *retlen = bytes_read; |
| 102 | return buffer; |
| 103 | } |
| 104 | |
Tom Sepez | 8ab45ea | 2016-01-05 10:17:30 -0800 | [diff] [blame] | 105 | std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) { |
Lei Zhang | 79e893a | 2015-11-04 16:02:47 -0800 | [diff] [blame] | 106 | 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 | |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 121 | std::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 Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 136 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString( |
| 137 | const std::wstring& wstr) { |
Tom Sepez | 8ab45ea | 2016-01-05 10:17:30 -0800 | [diff] [blame] | 138 | size_t length = sizeof(uint16_t) * (wstr.length() + 1); |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 139 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> result( |
| 140 | static_cast<unsigned short*>(malloc(length))); |
| 141 | char* ptr = reinterpret_cast<char*>(result.get()); |
Tom Sepez | 8ab45ea | 2016-01-05 10:17:30 -0800 | [diff] [blame] | 142 | 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 Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 149 | return result; |
Tom Sepez | 8ab45ea | 2016-01-05 10:17:30 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 152 | #ifdef PDF_ENABLE_V8 |
| 153 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 154 | bool 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) { |
jochen | 9e077d2 | 2016-06-09 02:51:13 -0700 | [diff] [blame] | 159 | InitializeV8Common(exe_path.c_str(), platform); |
thestig | c08cd7a | 2016-06-27 09:47:59 -0700 | [diff] [blame^] | 160 | 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 Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 168 | return true; |
| 169 | } |
| 170 | #else // V8_USE_EXTERNAL_STARTUP_DATA |
jochen | 9e077d2 | 2016-06-09 02:51:13 -0700 | [diff] [blame] | 171 | bool InitializeV8ForPDFium(const std::string& exe_path, |
| 172 | v8::Platform** platform) { |
| 173 | InitializeV8Common(exe_path.c_str(), platform); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 177 | #endif // PDF_ENABLE_V8 |
| 178 | |
| 179 | TestLoader::TestLoader(const char* pBuf, size_t len) |
| 180 | : m_pBuf(pBuf), m_Len(len) { |
| 181 | } |
| 182 | |
| 183 | // static |
| 184 | int 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 Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 195 | |
| 196 | TestSaver::TestSaver() { |
| 197 | FPDF_FILEWRITE::version = 1; |
| 198 | FPDF_FILEWRITE::WriteBlock = WriteBlockCallback; |
| 199 | } |
| 200 | |
| 201 | void TestSaver::ClearString() { |
| 202 | m_String.clear(); |
| 203 | } |
| 204 | |
| 205 | // static |
| 206 | int 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 | } |