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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
// 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 "base/command_line.h"
#include "base/json/json_file_value_serializer.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/extensions/permissions_updater.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/ui/webui/extensions/extension_settings_handler.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/testing_profile.h"
#include "components/crx_file/id_util.h"
#include "content/public/test/test_browser_thread.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/management_policy.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/feature_switch.h"
#include "extensions/common/value_builder.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#endif
namespace extensions {
namespace {
const char kAllHostsPermission[] = "*://*/*";
}
class ExtensionUITest : public testing::Test {
public:
ExtensionUITest()
: ui_thread_(content::BrowserThread::UI, &message_loop_),
file_thread_(content::BrowserThread::FILE, &message_loop_) {}
protected:
void SetUp() override {
// Create an ExtensionService and ManagementPolicy to inject into the
// ExtensionSettingsHandler.
profile_.reset(new TestingProfile());
TestExtensionSystem* system =
static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile_.get()));
extension_service_ = system->CreateExtensionService(
base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
management_policy_ = system->management_policy();
handler_.reset(new ExtensionSettingsHandler(extension_service_,
management_policy_));
}
void TearDown() override {
handler_.reset();
profile_.reset();
// Execute any pending deletion tasks.
message_loop_.RunUntilIdle();
}
static base::DictionaryValue* DeserializeJSONTestData(
const base::FilePath& path,
std::string *error) {
base::Value* value;
JSONFileValueSerializer serializer(path);
value = serializer.Deserialize(NULL, error);
return static_cast<base::DictionaryValue*>(value);
}
const scoped_refptr<const Extension> CreateExtension(
const std::string& name,
ListBuilder& permissions) {
const std::string kId = crx_file::id_util::GenerateId(name);
scoped_refptr<const Extension> extension =
ExtensionBuilder().SetManifest(
DictionaryBuilder()
.Set("name", name)
.Set("description", "an extension")
.Set("manifest_version", 2)
.Set("version", "1.0.0")
.Set("permissions", permissions))
.SetLocation(Manifest::INTERNAL)
.SetID(kId)
.Build();
ExtensionRegistry::Get(profile())->AddEnabled(extension);
PermissionsUpdater(profile()).InitializePermissions(extension.get());
return extension;
}
base::DictionaryValue* CreateExtensionDetailViewFromPath(
const base::FilePath& extension_path,
const std::vector<ExtensionPage>& pages,
Manifest::Location location) {
std::string error;
base::FilePath manifest_path = extension_path.Append(kManifestFilename);
scoped_ptr<base::DictionaryValue> extension_data(DeserializeJSONTestData(
manifest_path, &error));
EXPECT_EQ("", error);
scoped_refptr<Extension> extension(Extension::Create(
extension_path, location, *extension_data, Extension::REQUIRE_KEY,
&error));
EXPECT_TRUE(extension.get());
EXPECT_EQ("", error);
return handler_->CreateExtensionDetailValue(extension.get(), pages, NULL);
}
void CompareExpectedAndActualOutput(
const base::FilePath& extension_path,
const std::vector<ExtensionPage>& pages,
const base::FilePath& expected_output_path) {
std::string error;
scoped_ptr<base::DictionaryValue> expected_output_data(
DeserializeJSONTestData(expected_output_path, &error));
EXPECT_EQ("", error);
// Produce test output.
scoped_ptr<base::DictionaryValue> actual_output_data(
CreateExtensionDetailViewFromPath(
extension_path, pages, Manifest::INVALID_LOCATION));
// Compare the outputs.
// Ignore unknown fields in the actual output data.
std::string paths_details = " - expected (" +
expected_output_path.MaybeAsASCII() + ") vs. actual (" +
extension_path.MaybeAsASCII() + ")";
for (base::DictionaryValue::Iterator field(*expected_output_data);
!field.IsAtEnd(); field.Advance()) {
const base::Value* expected_value = &field.value();
base::Value* actual_value = NULL;
EXPECT_TRUE(actual_output_data->Get(field.key(), &actual_value)) <<
field.key() + " is missing" + paths_details;
EXPECT_TRUE(expected_value->Equals(actual_value)) << field.key() +
paths_details;
}
}
Profile* profile() { return profile_.get(); }
ExtensionSettingsHandler* handler() { return handler_.get(); }
base::MessageLoop message_loop_;
content::TestBrowserThread ui_thread_;
content::TestBrowserThread file_thread_;
scoped_ptr<TestingProfile> profile_;
ExtensionService* extension_service_;
ManagementPolicy* management_policy_;
scoped_ptr<ExtensionSettingsHandler> handler_;
#if defined OS_CHROMEOS
chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
chromeos::ScopedTestCrosSettings test_cros_settings_;
chromeos::ScopedTestUserManager test_user_manager_;
#endif
};
TEST_F(ExtensionUITest, GenerateExtensionsJSONData) {
base::FilePath data_test_dir_path, extension_path, expected_output_path;
EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_test_dir_path));
// Test Extension1
extension_path = data_test_dir_path.AppendASCII("extensions")
.AppendASCII("good")
.AppendASCII("Extensions")
.AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
.AppendASCII("1.0.0.0");
std::vector<ExtensionPage> pages;
pages.push_back(ExtensionPage(
GURL("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/bar.html"),
42, 88, false, false));
pages.push_back(ExtensionPage(
GURL("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/dog.html"),
0, 0, false, false));
expected_output_path = data_test_dir_path.AppendASCII("extensions")
.AppendASCII("ui")
.AppendASCII("create_extension_detail_value_expected_output")
.AppendASCII("good-extension1.json");
CompareExpectedAndActualOutput(extension_path, pages, expected_output_path);
#if !defined(OS_CHROMEOS)
// Test Extension2
extension_path = data_test_dir_path.AppendASCII("extensions")
.AppendASCII("good")
.AppendASCII("Extensions")
.AppendASCII("hpiknbiabeeppbpihjehijgoemciehgk")
.AppendASCII("2");
expected_output_path = data_test_dir_path.AppendASCII("extensions")
.AppendASCII("ui")
.AppendASCII("create_extension_detail_value_expected_output")
.AppendASCII("good-extension2.json");
// It's OK to have duplicate URLs, so long as the IDs are different.
pages[1].url = pages[0].url;
CompareExpectedAndActualOutput(extension_path, pages, expected_output_path);
#endif
// Test Extension3
extension_path = data_test_dir_path.AppendASCII("extensions")
.AppendASCII("good")
.AppendASCII("Extensions")
.AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa")
.AppendASCII("1.0");
expected_output_path = data_test_dir_path.AppendASCII("extensions")
.AppendASCII("ui")
.AppendASCII("create_extension_detail_value_expected_output")
.AppendASCII("good-extension3.json");
pages.clear();
CompareExpectedAndActualOutput(extension_path, pages, expected_output_path);
}
// Test that using Manifest::UNPACKED for the extension location triggers the
// correct values in the details, including location, order, and allow_reload.
TEST_F(ExtensionUITest, LocationLoadPropagation) {
base::FilePath data_test_dir_path, extension_path;
EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_test_dir_path));
extension_path = data_test_dir_path.AppendASCII("extensions")
.AppendASCII("good")
.AppendASCII("Extensions")
.AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
.AppendASCII("1.0.0.0");
std::vector<ExtensionPage> pages;
scoped_ptr<base::DictionaryValue> extension_details(
CreateExtensionDetailViewFromPath(
extension_path, pages, Manifest::UNPACKED));
bool ui_allow_reload = false;
bool ui_is_unpacked = false;
base::FilePath::StringType ui_path;
EXPECT_TRUE(extension_details->GetBoolean("allow_reload", &ui_allow_reload));
EXPECT_TRUE(extension_details->GetBoolean("isUnpacked", &ui_is_unpacked));
EXPECT_TRUE(extension_details->GetString("path", &ui_path));
EXPECT_EQ(true, ui_allow_reload);
EXPECT_EQ(true, ui_is_unpacked);
EXPECT_EQ(extension_path, base::FilePath(ui_path));
}
// Test that using Manifest::EXTERNAL_PREF for the extension location triggers
// the correct values in the details, including location, order, and
// allow_reload. Contrast to Manifest::UNPACKED, which has somewhat different
// values.
TEST_F(ExtensionUITest, LocationExternalPrefPropagation) {
base::FilePath data_test_dir_path, extension_path;
EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_test_dir_path));
extension_path = data_test_dir_path.AppendASCII("extensions")
.AppendASCII("good")
.AppendASCII("Extensions")
.AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
.AppendASCII("1.0.0.0");
std::vector<ExtensionPage> pages;
scoped_ptr<base::DictionaryValue> extension_details(
CreateExtensionDetailViewFromPath(
extension_path, pages, Manifest::EXTERNAL_PREF));
bool ui_allow_reload = true;
bool ui_is_unpacked = true;
base::FilePath::StringType ui_path;
EXPECT_TRUE(extension_details->GetBoolean("allow_reload", &ui_allow_reload));
EXPECT_TRUE(extension_details->GetBoolean("isUnpacked", &ui_is_unpacked));
EXPECT_FALSE(extension_details->GetString("path", &ui_path));
EXPECT_FALSE(ui_allow_reload);
EXPECT_FALSE(ui_is_unpacked);
}
// Test that the extension path is correctly propagated into the extension
// details.
TEST_F(ExtensionUITest, PathPropagation) {
base::FilePath data_test_dir_path, extension_path;
EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_test_dir_path));
extension_path = data_test_dir_path.AppendASCII("extensions")
.AppendASCII("good")
.AppendASCII("Extensions")
.AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
.AppendASCII("1.0.0.0");
std::vector<ExtensionPage> pages;
scoped_ptr<base::DictionaryValue> extension_details(
CreateExtensionDetailViewFromPath(
extension_path, pages, Manifest::UNPACKED));
base::FilePath::StringType ui_path;
EXPECT_TRUE(extension_details->GetString("path", &ui_path));
EXPECT_EQ(extension_path, base::FilePath(ui_path));
}
// Test that the all_urls checkbox only shows up for extensions that want all
// urls, and only when the switch is on.
TEST_F(ExtensionUITest, ExtensionUIAllUrlsCheckbox) {
// Start with the switch enabled.
scoped_ptr<FeatureSwitch::ScopedOverride> enable_scripts_switch(
new FeatureSwitch::ScopedOverride(
FeatureSwitch::scripts_require_action(), true));
// Two extensions - one with all urls, one without.
scoped_refptr<const Extension> all_urls_extension = CreateExtension(
"all_urls", ListBuilder().Append(kAllHostsPermission).Pass());
scoped_refptr<const Extension> no_urls_extension =
CreateExtension("no urls", ListBuilder().Pass());
scoped_ptr<base::DictionaryValue> value(handler()->CreateExtensionDetailValue(
all_urls_extension.get(), std::vector<ExtensionPage>(), NULL));
bool result = false;
const std::string kWantsAllUrls = "wantsAllUrls";
const std::string kAllowAllUrls = "allowAllUrls";
// The extension should want all urls, but not currently have it.
EXPECT_TRUE(value->GetBoolean(kWantsAllUrls, &result));
EXPECT_TRUE(result);
EXPECT_TRUE(value->GetBoolean(kAllowAllUrls, &result));
EXPECT_FALSE(result);
// Give the extension all urls.
util::SetAllowedScriptingOnAllUrls(
all_urls_extension->id(), profile(), true);
// Now the extension should both want and have all urls.
value.reset(handler()->CreateExtensionDetailValue(
all_urls_extension.get(), std::vector<ExtensionPage>(), NULL));
EXPECT_TRUE(value->GetBoolean(kWantsAllUrls, &result));
EXPECT_TRUE(result);
EXPECT_TRUE(value->GetBoolean(kAllowAllUrls, &result));
EXPECT_TRUE(result);
// The other extension should neither want nor have all urls.
value.reset(handler()->CreateExtensionDetailValue(
no_urls_extension.get(), std::vector<ExtensionPage>(), NULL));
EXPECT_TRUE(value->GetBoolean(kWantsAllUrls, &result));
EXPECT_FALSE(result);
EXPECT_TRUE(value->GetBoolean(kAllowAllUrls, &result));
EXPECT_FALSE(result);
// Turn off the switch and load another extension (so permissions are
// re-initialized).
enable_scripts_switch.reset();
// Even though the extension has the all urls preference, the checkbox
// shouldn't show up with the switch off.
value.reset(handler()->CreateExtensionDetailValue(
all_urls_extension.get(), std::vector<ExtensionPage>(), NULL));
EXPECT_TRUE(value->GetBoolean(kWantsAllUrls, &result));
EXPECT_FALSE(result);
EXPECT_TRUE(value->GetBoolean(kAllowAllUrls, &result));
EXPECT_TRUE(result);
// Load another extension with all urls (so permissions get re-init'd).
all_urls_extension = CreateExtension(
"all_urls_II", ListBuilder().Append(kAllHostsPermission).Pass());
// Even though the extension has all_urls permission, the checkbox shouldn't
// show up without the switch.
value.reset(handler()->CreateExtensionDetailValue(
all_urls_extension.get(), std::vector<ExtensionPage>(), NULL));
EXPECT_TRUE(value->GetBoolean(kWantsAllUrls, &result));
EXPECT_FALSE(result);
EXPECT_TRUE(value->GetBoolean(kAllowAllUrls, &result));
EXPECT_FALSE(result);
}
} // namespace extensions
|