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