1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/component_loader.h"
#include <stddef.h>
#include <string>
#include "ash/public/cpp/ash_pref_names.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/test_extension_service.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/testing_profile.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_set.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace extensions {
namespace {
class MockExtensionService : public TestExtensionService {
private:
bool ready_;
size_t unloaded_count_;
ExtensionRegistry* registry_;
public:
explicit MockExtensionService(Profile* profile)
: ready_(false),
unloaded_count_(0),
registry_(ExtensionRegistry::Get(profile)) {}
void AddComponentExtension(const Extension* extension) override {
EXPECT_FALSE(registry_->enabled_extensions().Contains(extension->id()));
// ExtensionService must become the owner of the extension object.
registry_->AddEnabled(extension);
}
void UnloadExtension(const std::string& extension_id,
UnloadedExtensionReason reason) override {
ASSERT_TRUE(registry_->enabled_extensions().Contains(extension_id));
// Remove the extension with the matching id.
registry_->RemoveEnabled(extension_id);
unloaded_count_++;
}
void RemoveComponentExtension(const std::string& extension_id) override {
UnloadExtension(extension_id, UnloadedExtensionReason::DISABLE);
}
bool is_ready() override { return ready_; }
void set_ready(bool ready) {
ready_ = ready;
}
size_t unloaded_count() const {
return unloaded_count_;
}
void clear_extensions() { registry_->ClearAll(); }
};
} // namespace
class ComponentLoaderTest : public testing::Test {
public:
ComponentLoaderTest()
// Note: we pass the same pref service here, to stand in for both
// user prefs and local state.
: extension_service_(&profile_),
component_loader_(&extension_service_,
&prefs_,
&local_state_,
&profile_) {
component_loader_.set_ignore_whitelist_for_testing(true);
}
void SetUp() override {
extension_path_ =
GetBasePath().AppendASCII("good")
.AppendASCII("Extensions")
.AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
.AppendASCII("1.0.0.0");
// Read in the extension manifest.
ASSERT_TRUE(base::ReadFileToString(
extension_path_.Append(kManifestFilename),
&manifest_contents_));
// Register the local state prefs.
#if defined(OS_CHROMEOS)
local_state_.registry()->RegisterBooleanPref(
ash::prefs::kAccessibilitySpokenFeedbackEnabled, false);
#endif
}
protected:
content::TestBrowserThreadBundle thread_bundle_;
TestingProfile profile_;
MockExtensionService extension_service_;
sync_preferences::TestingPrefServiceSyncable prefs_;
TestingPrefServiceSimple local_state_;
ComponentLoader component_loader_;
// The root directory of the text extension.
base::FilePath extension_path_;
// The contents of the text extension's manifest file.
std::string manifest_contents_;
base::FilePath GetBasePath() {
base::FilePath test_data_dir;
base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
return test_data_dir.AppendASCII("extensions");
}
};
TEST_F(ComponentLoaderTest, ParseManifest) {
std::unique_ptr<base::DictionaryValue> manifest;
// Test invalid JSON.
manifest = component_loader_.ParseManifest("{ 'test': 3 } invalid");
EXPECT_FALSE(manifest);
// Test manifests that are valid JSON, but don't have an object literal
// at the root. ParseManifest() should always return NULL.
manifest = component_loader_.ParseManifest(std::string());
EXPECT_FALSE(manifest);
manifest = component_loader_.ParseManifest("[{ \"foo\": 3 }]");
EXPECT_FALSE(manifest);
manifest = component_loader_.ParseManifest("\"Test\"");
EXPECT_FALSE(manifest);
manifest = component_loader_.ParseManifest("42");
EXPECT_FALSE(manifest);
manifest = component_loader_.ParseManifest("true");
EXPECT_FALSE(manifest);
manifest = component_loader_.ParseManifest("false");
EXPECT_FALSE(manifest);
manifest = component_loader_.ParseManifest("null");
EXPECT_FALSE(manifest);
// Test parsing valid JSON.
int value = 0;
manifest = component_loader_.ParseManifest(
"{ \"test\": { \"one\": 1 }, \"two\": 2 }");
ASSERT_TRUE(manifest);
EXPECT_TRUE(manifest->GetInteger("test.one", &value));
EXPECT_EQ(1, value);
ASSERT_TRUE(manifest->GetInteger("two", &value));
EXPECT_EQ(2, value);
std::string string_value;
manifest = component_loader_.ParseManifest(manifest_contents_);
ASSERT_TRUE(manifest->GetString("background.page", &string_value));
EXPECT_EQ("backgroundpage.html", string_value);
}
// Test that the extension isn't loaded if the extension service isn't ready.
TEST_F(ComponentLoaderTest, AddWhenNotReady) {
extension_service_.set_ready(false);
std::string extension_id =
component_loader_.Add(manifest_contents_, extension_path_);
EXPECT_NE("", extension_id);
ExtensionRegistry* registry = ExtensionRegistry::Get(&profile_);
EXPECT_EQ(0u, registry->enabled_extensions().size());
}
// Test that it *is* loaded when the extension service *is* ready.
TEST_F(ComponentLoaderTest, AddWhenReady) {
extension_service_.set_ready(true);
std::string extension_id =
component_loader_.Add(manifest_contents_, extension_path_);
EXPECT_NE("", extension_id);
ExtensionRegistry* registry = ExtensionRegistry::Get(&profile_);
EXPECT_EQ(1u, registry->enabled_extensions().size());
EXPECT_TRUE(registry->enabled_extensions().GetByID(extension_id));
}
TEST_F(ComponentLoaderTest, Remove) {
extension_service_.set_ready(false);
ExtensionRegistry* registry = ExtensionRegistry::Get(&profile_);
// Removing an extension that was never added should be ok.
component_loader_.Remove(extension_path_);
EXPECT_EQ(0u, registry->enabled_extensions().size());
// Try adding and removing before LoadAll() is called.
component_loader_.Add(manifest_contents_, extension_path_);
component_loader_.Remove(extension_path_);
component_loader_.LoadAll();
EXPECT_EQ(0u, registry->enabled_extensions().size());
// Load an extension, and check that it's unloaded when Remove() is called.
extension_service_.set_ready(true);
std::string extension_id =
component_loader_.Add(manifest_contents_, extension_path_);
EXPECT_EQ(1u, registry->enabled_extensions().size());
component_loader_.Remove(extension_path_);
EXPECT_EQ(0u, registry->enabled_extensions().size());
// And after calling LoadAll(), it shouldn't get loaded.
component_loader_.LoadAll();
EXPECT_EQ(0u, registry->enabled_extensions().size());
}
TEST_F(ComponentLoaderTest, LoadAll) {
extension_service_.set_ready(false);
ExtensionRegistry* registry = ExtensionRegistry::Get(&profile_);
// No extensions should be loaded if none were added.
component_loader_.LoadAll();
EXPECT_EQ(0u, registry->enabled_extensions().size());
// Use LoadAll() to load the default extensions.
component_loader_.AddDefaultComponentExtensions(false);
component_loader_.LoadAll();
unsigned int default_count = registry->enabled_extensions().size();
// Clear the list of loaded extensions, and reload with one more.
extension_service_.clear_extensions();
component_loader_.Add(manifest_contents_, extension_path_);
component_loader_.LoadAll();
EXPECT_EQ(default_count + 1, registry->enabled_extensions().size());
}
TEST_F(ComponentLoaderTest, AddOrReplace) {
EXPECT_EQ(0u, component_loader_.registered_extensions_count());
component_loader_.AddDefaultComponentExtensions(false);
size_t const default_count = component_loader_.registered_extensions_count();
base::FilePath known_extension = GetBasePath()
.AppendASCII("override_component_extension");
base::FilePath unknown_extension = extension_path_;
base::FilePath invalid_extension = GetBasePath()
.AppendASCII("this_path_does_not_exist");
// Replace a default component extension.
component_loader_.AddOrReplace(known_extension);
EXPECT_EQ(default_count, component_loader_.registered_extensions_count());
// Add a new component extension.
component_loader_.AddOrReplace(unknown_extension);
EXPECT_EQ(default_count + 1, component_loader_.registered_extensions_count());
extension_service_.set_ready(true);
component_loader_.LoadAll();
ExtensionRegistry* registry = ExtensionRegistry::Get(&profile_);
EXPECT_EQ(default_count + 1, registry->enabled_extensions().size());
EXPECT_EQ(0u, extension_service_.unloaded_count());
// replace loaded component extension.
component_loader_.AddOrReplace(known_extension);
EXPECT_EQ(default_count + 1, registry->enabled_extensions().size());
EXPECT_EQ(1u, extension_service_.unloaded_count());
// Add an invalid component extension.
std::string extension_id = component_loader_.AddOrReplace(invalid_extension);
EXPECT_TRUE(extension_id.empty());
}
} // namespace extensions
|