| OLD | NEW |
| 1 // Copyright (c) 2015 PDFium Authors. All rights reserved. | 1 // Copyright (c) 2015 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "embedder_test.h" | 5 #include "embedder_test.h" |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 #include <stdio.h> | |
| 9 #include <stdlib.h> | |
| 10 #include <string.h> | |
| 11 | 8 |
| 12 #include <list> | 9 #include <list> |
| 13 #include <string> | 10 #include <string> |
| 14 #include <utility> | 11 #include <utility> |
| 15 #include <vector> | 12 #include <vector> |
| 16 | 13 |
| 17 #include "../public/fpdf_text.h" | 14 #include "../public/fpdf_text.h" |
| 18 #include "../public/fpdfview.h" | 15 #include "../public/fpdfview.h" |
| 16 #include "test_support.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 20 | 18 |
| 21 #ifdef PDF_ENABLE_V8 | 19 #ifdef PDF_ENABLE_V8 |
| 22 #include "v8/include/libplatform/libplatform.h" | 20 #include "v8/include/libplatform/libplatform.h" |
| 23 #include "v8/include/v8.h" | 21 #include "v8/include/v8.h" |
| 24 #endif // PDF_ENABLE_V8 | 22 #endif // PDF_ENABLE_V8 |
| 25 | 23 |
| 26 #ifdef _WIN32 | |
| 27 #define snprintf _snprintf | |
| 28 #define PATH_SEPARATOR '\\' | |
| 29 #else | |
| 30 #define PATH_SEPARATOR '/' | |
| 31 #endif | |
| 32 | |
| 33 namespace { | 24 namespace { |
| 34 | |
| 35 const char* g_exe_path_ = nullptr; | 25 const char* g_exe_path_ = nullptr; |
| 36 | |
| 37 // Reads the entire contents of a file into a newly malloc'd buffer. | |
| 38 static char* GetFileContents(const char* filename, size_t* retlen) { | |
| 39 FILE* file = fopen(filename, "rb"); | |
| 40 if (!file) { | |
| 41 fprintf(stderr, "Failed to open: %s\n", filename); | |
| 42 return nullptr; | |
| 43 } | |
| 44 (void)fseek(file, 0, SEEK_END); | |
| 45 size_t file_length = ftell(file); | |
| 46 if (!file_length) { | |
| 47 return nullptr; | |
| 48 } | |
| 49 (void)fseek(file, 0, SEEK_SET); | |
| 50 char* buffer = (char*)malloc(file_length); | |
| 51 if (!buffer) { | |
| 52 return nullptr; | |
| 53 } | |
| 54 size_t bytes_read = fread(buffer, 1, file_length, file); | |
| 55 (void)fclose(file); | |
| 56 if (bytes_read != file_length) { | |
| 57 fprintf(stderr, "Failed to read: %s\n", filename); | |
| 58 free(buffer); | |
| 59 return nullptr; | |
| 60 } | |
| 61 *retlen = bytes_read; | |
| 62 return buffer; | |
| 63 } | |
| 64 | |
| 65 #ifdef PDF_ENABLE_V8 | |
| 66 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | |
| 67 // Returns the full path for an external V8 data file based on either | |
| 68 // the currect exectuable path or an explicit override. | |
| 69 static std::string GetFullPathForSnapshotFile(const std::string& exe_path, | |
| 70 const std::string& filename) { | |
| 71 std::string result; | |
| 72 if (!exe_path.empty()) { | |
| 73 size_t last_separator = exe_path.rfind(PATH_SEPARATOR); | |
| 74 if (last_separator != std::string::npos) { | |
| 75 result = exe_path.substr(0, last_separator + 1); | |
| 76 } | |
| 77 } | |
| 78 result += filename; | |
| 79 return result; | |
| 80 } | |
| 81 | |
| 82 // Reads an extenal V8 data file from the |options|-indicated location, | |
| 83 // returing true on success and false on error. | |
| 84 static bool GetExternalData(const std::string& exe_path, | |
| 85 const std::string& filename, | |
| 86 v8::StartupData* result_data) { | |
| 87 std::string full_path = GetFullPathForSnapshotFile(exe_path, filename); | |
| 88 size_t data_length = 0; | |
| 89 char* data_buffer = GetFileContents(full_path.c_str(), &data_length); | |
| 90 if (!data_buffer) { | |
| 91 return false; | |
| 92 } | |
| 93 result_data->data = const_cast<const char*>(data_buffer); | |
| 94 result_data->raw_size = data_length; | |
| 95 return true; | |
| 96 } | |
| 97 #endif // V8_USE_EXTERNAL_STARTUP_DATA | |
| 98 #endif // PDF_ENABLE_V8 | |
| 99 } // namespace | 26 } // namespace |
| 100 | 27 |
| 101 class TestLoader { | |
| 102 public: | |
| 103 TestLoader(const char* pBuf, size_t len); | |
| 104 | |
| 105 const char* m_pBuf; | |
| 106 size_t m_Len; | |
| 107 }; | |
| 108 | |
| 109 TestLoader::TestLoader(const char* pBuf, size_t len) | |
| 110 : m_pBuf(pBuf), m_Len(len) {} | |
| 111 | |
| 112 int Get_Block(void* param, | |
| 113 unsigned long pos, | |
| 114 unsigned char* pBuf, | |
| 115 unsigned long size) { | |
| 116 TestLoader* pLoader = (TestLoader*)param; | |
| 117 if (pos + size < pos || pos + size > pLoader->m_Len) | |
| 118 return 0; | |
| 119 memcpy(pBuf, pLoader->m_pBuf + pos, size); | |
| 120 return 1; | |
| 121 } | |
| 122 | |
| 123 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { | 28 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { |
| 124 return true; | 29 return true; |
| 125 } | 30 } |
| 126 | 31 |
| 127 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {} | 32 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {} |
| 128 | 33 |
| 129 EmbedderTest::EmbedderTest() | 34 EmbedderTest::EmbedderTest() |
| 130 : default_delegate_(new EmbedderTest::Delegate()), | 35 : default_delegate_(new EmbedderTest::Delegate()), |
| 131 document_(nullptr), | 36 document_(nullptr), |
| 132 form_handle_(nullptr), | 37 form_handle_(nullptr), |
| 133 avail_(nullptr), | 38 avail_(nullptr), |
| 134 external_isolate_(nullptr), | 39 external_isolate_(nullptr), |
| 135 loader_(nullptr), | 40 loader_(nullptr), |
| 136 file_length_(0), | 41 file_length_(0), |
| 137 file_contents_(nullptr) { | 42 file_contents_(nullptr) { |
| 138 memset(&hints_, 0, sizeof(hints_)); | 43 memset(&hints_, 0, sizeof(hints_)); |
| 139 memset(&file_access_, 0, sizeof(file_access_)); | 44 memset(&file_access_, 0, sizeof(file_access_)); |
| 140 memset(&file_avail_, 0, sizeof(file_avail_)); | 45 memset(&file_avail_, 0, sizeof(file_avail_)); |
| 141 delegate_ = default_delegate_.get(); | 46 delegate_ = default_delegate_.get(); |
| 142 } | 47 } |
| 143 | 48 |
| 144 EmbedderTest::~EmbedderTest() { | 49 EmbedderTest::~EmbedderTest() { |
| 145 } | 50 } |
| 146 | 51 |
| 147 void EmbedderTest::SetUp() { | 52 void EmbedderTest::SetUp() { |
| 148 #ifdef PDF_ENABLE_V8 | 53 #ifdef PDF_ENABLE_V8 |
| 149 v8::V8::InitializeICU(); | |
| 150 | |
| 151 platform_ = v8::platform::CreateDefaultPlatform(); | |
| 152 v8::V8::InitializePlatform(platform_); | |
| 153 v8::V8::Initialize(); | |
| 154 | |
| 155 // By enabling predictable mode, V8 won't post any background tasks. | |
| 156 const char predictable_flag[] = "--predictable"; | |
| 157 v8::V8::SetFlagsFromString(predictable_flag, | |
| 158 static_cast<int>(strlen(predictable_flag))); | |
| 159 | |
| 160 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 54 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 161 ASSERT_TRUE(GetExternalData(g_exe_path_, "natives_blob.bin", &natives_)); | 55 InitializeV8ForPDFium(g_exe_path_, std::string(), &natives_, &snapshot_, |
| 162 ASSERT_TRUE(GetExternalData(g_exe_path_, "snapshot_blob.bin", &snapshot_)); | 56 &platform_); |
| 163 v8::V8::SetNativesDataBlob(&natives_); | 57 #else |
| 164 v8::V8::SetSnapshotDataBlob(&snapshot_); | 58 InitializeV8ForPDFium(&platform_); |
| 165 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 59 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 166 #endif // FPDF_ENABLE_V8 | 60 #endif // FPDF_ENABLE_V8 |
| 167 | 61 |
| 168 FPDF_LIBRARY_CONFIG config; | 62 FPDF_LIBRARY_CONFIG config; |
| 169 config.version = 2; | 63 config.version = 2; |
| 170 config.m_pUserFontPaths = nullptr; | 64 config.m_pUserFontPaths = nullptr; |
| 171 config.m_v8EmbedderSlot = 0; | 65 config.m_v8EmbedderSlot = 0; |
| 172 config.m_pIsolate = external_isolate_; | 66 config.m_pIsolate = external_isolate_; |
| 173 FPDF_InitLibraryWithConfig(&config); | 67 FPDF_InitLibraryWithConfig(&config); |
| 174 | 68 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 202 } | 96 } |
| 203 | 97 |
| 204 bool EmbedderTest::OpenDocument(const std::string& filename) { | 98 bool EmbedderTest::OpenDocument(const std::string& filename) { |
| 205 file_contents_ = GetFileContents(filename.c_str(), &file_length_); | 99 file_contents_ = GetFileContents(filename.c_str(), &file_length_); |
| 206 if (!file_contents_) { | 100 if (!file_contents_) { |
| 207 return false; | 101 return false; |
| 208 } | 102 } |
| 209 | 103 |
| 210 loader_ = new TestLoader(file_contents_, file_length_); | 104 loader_ = new TestLoader(file_contents_, file_length_); |
| 211 file_access_.m_FileLen = static_cast<unsigned long>(file_length_); | 105 file_access_.m_FileLen = static_cast<unsigned long>(file_length_); |
| 212 file_access_.m_GetBlock = Get_Block; | 106 file_access_.m_GetBlock = TestLoader::GetBlock; |
| 213 file_access_.m_Param = loader_; | 107 file_access_.m_Param = loader_; |
| 214 | 108 |
| 215 file_avail_.version = 1; | 109 file_avail_.version = 1; |
| 216 file_avail_.IsDataAvail = Is_Data_Avail; | 110 file_avail_.IsDataAvail = Is_Data_Avail; |
| 217 | 111 |
| 218 hints_.version = 1; | 112 hints_.version = 1; |
| 219 hints_.AddSegment = Add_Segment; | 113 hints_.AddSegment = Add_Segment; |
| 220 | 114 |
| 221 avail_ = FPDFAvail_Create(&file_avail_, &file_access_); | 115 avail_ = FPDFAvail_Create(&file_avail_, &file_access_); |
| 222 (void)FPDFAvail_IsDocAvail(avail_, &hints_); | 116 (void)FPDFAvail_IsDocAvail(avail_, &hints_); |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 } | 261 } |
| 368 | 262 |
| 369 // Can't use gtest-provided main since we need to stash the path to the | 263 // Can't use gtest-provided main since we need to stash the path to the |
| 370 // executable in order to find the external V8 binary data files. | 264 // executable in order to find the external V8 binary data files. |
| 371 int main(int argc, char** argv) { | 265 int main(int argc, char** argv) { |
| 372 g_exe_path_ = argv[0]; | 266 g_exe_path_ = argv[0]; |
| 373 testing::InitGoogleTest(&argc, argv); | 267 testing::InitGoogleTest(&argc, argv); |
| 374 testing::InitGoogleMock(&argc, argv); | 268 testing::InitGoogleMock(&argc, argv); |
| 375 return RUN_ALL_TESTS(); | 269 return RUN_ALL_TESTS(); |
| 376 } | 270 } |
| OLD | NEW |