[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 5 | #include "extensions/renderer/module_system_test.h" |
| 6 | |
| 7 | #include <map> |
| 8 | #include <string> |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame^] | 9 | #include <utility> |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 10 | |
| 11 | #include "base/callback.h" |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
thestig | 9471270 | 2014-09-10 07:46:59 | [diff] [blame] | 13 | #include "base/files/file_util.h" |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 14 | #include "base/lazy_instance.h" |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 15 | #include "base/memory/scoped_ptr.h" |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 16 | #include "base/path_service.h" |
[email protected] | 4570a25 | 2013-03-31 00:35:43 | [diff] [blame] | 17 | #include "base/strings/string_piece.h" |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 18 | #include "extensions/common/extension_paths.h" |
[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 19 | #include "extensions/renderer/logging_native_handler.h" |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 20 | #include "extensions/renderer/object_backed_native_handler.h" |
kalman | 33076cb | 2015-08-11 19:12:07 | [diff] [blame] | 21 | #include "extensions/renderer/safe_builtins.h" |
[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 22 | #include "extensions/renderer/utils_native_handler.h" |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 23 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 24 | |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 25 | namespace extensions { |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | class FailsOnException : public ModuleSystem::ExceptionHandler { |
| 29 | public: |
bashi | 6a4854f | 2015-06-19 00:51:51 | [diff] [blame] | 30 | FailsOnException() : ModuleSystem::ExceptionHandler(nullptr) {} |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 31 | void HandleUncaughtException(const v8::TryCatch& try_catch) override { |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 32 | FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch); |
| 33 | } |
| 34 | }; |
| 35 | |
kalman | 33076cb | 2015-08-11 19:12:07 | [diff] [blame] | 36 | class V8ExtensionConfigurator { |
| 37 | public: |
| 38 | V8ExtensionConfigurator() |
| 39 | : safe_builtins_(SafeBuiltins::CreateV8Extension()), |
| 40 | names_(1, safe_builtins_->name()), |
| 41 | configuration_( |
| 42 | new v8::ExtensionConfiguration(static_cast<int>(names_.size()), |
davidben | b50f00c | 2015-12-01 00:01:50 | [diff] [blame] | 43 | names_.data())) { |
kalman | 33076cb | 2015-08-11 19:12:07 | [diff] [blame] | 44 | v8::RegisterExtension(safe_builtins_.get()); |
| 45 | } |
| 46 | |
| 47 | v8::ExtensionConfiguration* GetConfiguration() { |
| 48 | return configuration_.get(); |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | scoped_ptr<v8::Extension> safe_builtins_; |
| 53 | std::vector<const char*> names_; |
| 54 | scoped_ptr<v8::ExtensionConfiguration> configuration_; |
| 55 | }; |
| 56 | |
| 57 | base::LazyInstance<V8ExtensionConfigurator>::Leaky g_v8_extension_configurator = |
| 58 | LAZY_INSTANCE_INITIALIZER; |
| 59 | |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 60 | } // namespace |
| 61 | |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 62 | // Native JS functions for doing asserts. |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 63 | class ModuleSystemTestEnvironment::AssertNatives |
| 64 | : public ObjectBackedNativeHandler { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 65 | public: |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 66 | explicit AssertNatives(ScriptContext* context) |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 67 | : ObjectBackedNativeHandler(context), |
[email protected] | 2e0e0bc | 2013-02-04 10:30:34 | [diff] [blame] | 68 | assertion_made_(false), |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 69 | failed_(false) { |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 70 | RouteFunction( |
| 71 | "AssertTrue", |
| 72 | base::Bind(&AssertNatives::AssertTrue, base::Unretained(this))); |
| 73 | RouteFunction( |
| 74 | "AssertFalse", |
| 75 | base::Bind(&AssertNatives::AssertFalse, base::Unretained(this))); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | bool assertion_made() { return assertion_made_; } |
| 79 | bool failed() { return failed_; } |
| 80 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 81 | void AssertTrue(const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 82 | CHECK_EQ(1, args.Length()); |
| 83 | assertion_made_ = true; |
dcarney | 04cd964 | 2014-11-21 13:58:11 | [diff] [blame] | 84 | failed_ = failed_ || !args[0]->ToBoolean(args.GetIsolate())->Value(); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 87 | void AssertFalse(const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 88 | CHECK_EQ(1, args.Length()); |
| 89 | assertion_made_ = true; |
dcarney | 04cd964 | 2014-11-21 13:58:11 | [diff] [blame] | 90 | failed_ = failed_ || args[0]->ToBoolean(args.GetIsolate())->Value(); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | private: |
| 94 | bool assertion_made_; |
| 95 | bool failed_; |
| 96 | }; |
| 97 | |
| 98 | // Source map that operates on std::strings. |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 99 | class ModuleSystemTestEnvironment::StringSourceMap |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 100 | : public ModuleSystem::SourceMap { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 101 | public: |
| 102 | StringSourceMap() {} |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 103 | ~StringSourceMap() override {} |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 104 | |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 105 | v8::Local<v8::Value> GetSource(v8::Isolate* isolate, |
| 106 | const std::string& name) override { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 107 | if (source_map_.count(name) == 0) |
[email protected] | 6f59d3b | 2013-12-02 12:50:50 | [diff] [blame] | 108 | return v8::Undefined(isolate); |
| 109 | return v8::String::NewFromUtf8(isolate, source_map_[name].c_str()); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 110 | } |
| 111 | |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 112 | bool Contains(const std::string& name) override { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 113 | return source_map_.count(name); |
| 114 | } |
| 115 | |
| 116 | void RegisterModule(const std::string& name, const std::string& source) { |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 117 | CHECK_EQ(0u, source_map_.count(name)) << "Module " << name << " not found"; |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 118 | source_map_[name] = source; |
| 119 | } |
| 120 | |
| 121 | private: |
| 122 | std::map<std::string, std::string> source_map_; |
| 123 | }; |
| 124 | |
[email protected] | 99ea16b | 2014-07-17 22:15:56 | [diff] [blame] | 125 | ModuleSystemTestEnvironment::ModuleSystemTestEnvironment(v8::Isolate* isolate) |
| 126 | : isolate_(isolate), |
| 127 | context_holder_(new gin::ContextHolder(isolate_)), |
| 128 | handle_scope_(isolate_), |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 129 | source_map_(new StringSourceMap()) { |
kalman | 33076cb | 2015-08-11 19:12:07 | [diff] [blame] | 130 | context_holder_->SetContext(v8::Context::New( |
| 131 | isolate, g_v8_extension_configurator.Get().GetConfiguration())); |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 132 | context_.reset(new ScriptContext(context_holder_->context(), |
bashi | 6a4854f | 2015-06-19 00:51:51 | [diff] [blame] | 133 | nullptr, // WebFrame |
| 134 | nullptr, // Extension |
mek | 7e1d745 | 2014-09-08 23:55:57 | [diff] [blame] | 135 | Feature::BLESSED_EXTENSION_CONTEXT, |
bashi | 6a4854f | 2015-06-19 00:51:51 | [diff] [blame] | 136 | nullptr, // Effective Extension |
[email protected] | f80685c3 | 2014-07-26 19:48:04 | [diff] [blame] | 137 | Feature::BLESSED_EXTENSION_CONTEXT)); |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 138 | context_->v8_context()->Enter(); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 139 | assert_natives_ = new AssertNatives(context_.get()); |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 140 | |
| 141 | { |
| 142 | scoped_ptr<ModuleSystem> module_system( |
| 143 | new ModuleSystem(context_.get(), source_map_.get())); |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame^] | 144 | context_->set_module_system(std::move(module_system)); |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 145 | } |
| 146 | ModuleSystem* module_system = context_->module_system(); |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 147 | module_system->RegisterNativeHandler( |
| 148 | "assert", scoped_ptr<NativeHandler>(assert_natives_)); |
| 149 | module_system->RegisterNativeHandler( |
| 150 | "logging", |
| 151 | scoped_ptr<NativeHandler>(new LoggingNativeHandler(context_.get()))); |
| 152 | module_system->RegisterNativeHandler( |
| 153 | "utils", |
| 154 | scoped_ptr<NativeHandler>(new UtilsNativeHandler(context_.get()))); |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 155 | module_system->SetExceptionHandlerForTest( |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 156 | scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException)); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 157 | } |
| 158 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 159 | ModuleSystemTestEnvironment::~ModuleSystemTestEnvironment() { |
sammc | de54a47e | 2015-01-13 23:16:34 | [diff] [blame] | 160 | if (context_->is_valid()) |
kalman | b0c1c50 | 2015-04-15 00:25:06 | [diff] [blame] | 161 | ShutdownModuleSystem(); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 164 | void ModuleSystemTestEnvironment::RegisterModule(const std::string& name, |
| 165 | const std::string& code) { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 166 | source_map_->RegisterModule(name, code); |
| 167 | } |
| 168 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 169 | void ModuleSystemTestEnvironment::RegisterModule(const std::string& name, |
| 170 | int resource_id) { |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 171 | const std::string& code = ResourceBundle::GetSharedInstance() |
| 172 | .GetRawDataResource(resource_id) |
| 173 | .as_string(); |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 174 | source_map_->RegisterModule(name, code); |
| 175 | } |
| 176 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 177 | void ModuleSystemTestEnvironment::OverrideNativeHandler( |
| 178 | const std::string& name, |
| 179 | const std::string& code) { |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 180 | RegisterModule(name, code); |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 181 | context_->module_system()->OverrideNativeHandlerForTest(name); |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 182 | } |
| 183 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 184 | void ModuleSystemTestEnvironment::RegisterTestFile( |
| 185 | const std::string& module_name, |
| 186 | const std::string& file_name) { |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 187 | base::FilePath test_js_file_path; |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 188 | ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &test_js_file_path)); |
| 189 | test_js_file_path = test_js_file_path.AppendASCII(file_name); |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 190 | std::string test_js; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 191 | ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js)); |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 192 | source_map_->RegisterModule(module_name, test_js); |
| 193 | } |
| 194 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 195 | void ModuleSystemTestEnvironment::ShutdownGin() { |
| 196 | context_holder_.reset(); |
| 197 | } |
| 198 | |
| 199 | void ModuleSystemTestEnvironment::ShutdownModuleSystem() { |
kalman | b0c1c50 | 2015-04-15 00:25:06 | [diff] [blame] | 200 | CHECK(context_->is_valid()); |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 201 | context_->v8_context()->Exit(); |
sammc | de54a47e | 2015-01-13 23:16:34 | [diff] [blame] | 202 | context_->Invalidate(); |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 203 | } |
| 204 | |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 205 | v8::Local<v8::Object> ModuleSystemTestEnvironment::CreateGlobal( |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 206 | const std::string& name) { |
[email protected] | 99ea16b | 2014-07-17 22:15:56 | [diff] [blame] | 207 | v8::EscapableHandleScope handle_scope(isolate_); |
| 208 | v8::Local<v8::Object> object = v8::Object::New(isolate_); |
| 209 | isolate_->GetCurrentContext()->Global()->Set( |
| 210 | v8::String::NewFromUtf8(isolate_, name.c_str()), object); |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 211 | return handle_scope.Escape(object); |
| 212 | } |
| 213 | |
| 214 | ModuleSystemTest::ModuleSystemTest() |
[email protected] | 99ea16b | 2014-07-17 22:15:56 | [diff] [blame] | 215 | : isolate_(v8::Isolate::GetCurrent()), |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 216 | should_assertions_be_made_(true) { |
| 217 | } |
| 218 | |
| 219 | ModuleSystemTest::~ModuleSystemTest() { |
| 220 | } |
| 221 | |
sammc | 32a6fc7d | 2014-09-15 02:47:31 | [diff] [blame] | 222 | void ModuleSystemTest::SetUp() { |
| 223 | env_ = CreateEnvironment(); |
| 224 | } |
| 225 | |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 226 | void ModuleSystemTest::TearDown() { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 227 | // All tests must assert at least once unless otherwise specified. |
| 228 | EXPECT_EQ(should_assertions_be_made_, |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 229 | env_->assert_natives()->assertion_made()); |
| 230 | EXPECT_FALSE(env_->assert_natives()->failed()); |
sammc | 32a6fc7d | 2014-09-15 02:47:31 | [diff] [blame] | 231 | env_.reset(); |
| 232 | v8::HeapStatistics stats; |
| 233 | isolate_->GetHeapStatistics(&stats); |
| 234 | size_t old_heap_size = 0; |
| 235 | // Run the GC until the heap size reaches a steady state to ensure that |
| 236 | // all the garbage is collected. |
| 237 | while (stats.used_heap_size() != old_heap_size) { |
| 238 | old_heap_size = stats.used_heap_size(); |
| 239 | isolate_->RequestGarbageCollectionForTesting( |
| 240 | v8::Isolate::kFullGarbageCollection); |
| 241 | isolate_->GetHeapStatistics(&stats); |
| 242 | } |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | scoped_ptr<ModuleSystemTestEnvironment> ModuleSystemTest::CreateEnvironment() { |
[email protected] | 99ea16b | 2014-07-17 22:15:56 | [diff] [blame] | 246 | return make_scoped_ptr(new ModuleSystemTestEnvironment(isolate_)); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void ModuleSystemTest::ExpectNoAssertionsMade() { |
| 250 | should_assertions_be_made_ = false; |
| 251 | } |
| 252 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 253 | void ModuleSystemTest::RunResolvedPromises() { |
[email protected] | 99ea16b | 2014-07-17 22:15:56 | [diff] [blame] | 254 | isolate_->RunMicrotasks(); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 255 | } |
[email protected] | 582f6e9 | 2014-07-16 23:39:15 | [diff] [blame] | 256 | |
| 257 | } // namespace extensions |