Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6510)

Unified Diff: chrome/browser/extensions/extension_management_unittest.cc

Issue 499313002: Add ExtensionManagement class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: rebase minor robustness improvement Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/extension_management_unittest.cc
diff --git a/chrome/browser/extensions/extension_management_unittest.cc b/chrome/browser/extensions/extension_management_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c90dab70a1118f29ce62f98647060650cc8b7191
--- /dev/null
+++ b/chrome/browser/extensions/extension_management_unittest.cc
@@ -0,0 +1,178 @@
+// Copyright 2014 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 <algorithm>
+
+#include "base/memory/scoped_ptr.h"
+#include "base/prefs/pref_registry_simple.h"
+#include "base/prefs/testing_pref_service.h"
+#include "base/values.h"
+#include "chrome/browser/extensions/extension_management.h"
+#include "chrome/browser/extensions/external_policy_loader.h"
+#include "extensions/browser/pref_names.h"
+#include "extensions/common/url_pattern.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace extensions {
+
+namespace {
+const std::string kTargetExtension = "abcdefghijklmnopabcdefghijklmnop";
Joao da Silva 2014/09/02 19:34:02 Make this const char kTargetExtension[] = "..."; (
binjin 2014/09/02 20:02:56 Done.
+const std::string kOtherExtension = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
+const std::string kExampleUpdateUrl = "http://example.com/update_url";
+};
Joao da Silva 2014/09/02 19:34:02 nit: no ";" when closing a namespace. Add a comme
binjin 2014/09/02 20:02:56 Done.
+
+class ExtensionManagementTest : public testing::Test {
+ public:
+ ExtensionManagementTest() {}
+ virtual ~ExtensionManagementTest() {}
+
+ // testing::Test:
+ virtual void SetUp() OVERRIDE {
+ pref_service_.reset(new TestingPrefServiceSimple());
Joao da Silva 2014/09/02 19:34:02 Why is this in a scoped_ptr and not just directly
binjin 2014/09/02 20:02:56 See below.
+ pref_service_->registry()->RegisterListPref(
+ pref_names::kAllowedInstallSites);
+ pref_service_->registry()->RegisterListPref(pref_names::kAllowedTypes);
+ pref_service_->registry()->RegisterListPref(pref_names::kInstallDenyList);
+ pref_service_->registry()->RegisterListPref(pref_names::kInstallAllowList);
+ pref_service_->registry()->RegisterDictionaryPref(
+ pref_names::kInstallForceList);
+ extension_management_.reset(new ExtensionManagement(pref_service_.get()));
Joao da Silva 2014/09/02 19:34:02 Same for this one, can't it be directly used as a
binjin 2014/09/02 20:02:56 See below.
+ }
+
+ virtual void TearDown() OVERRIDE {
+ extension_management_.reset();
+ pref_service_.reset();
Joao da Silva 2014/09/02 19:34:02 Is this TearDown needed?
binjin 2014/09/02 20:02:56 Because extension_management_ doesn't own pref_ser
Joao da Silva 2014/09/03 09:20:40 They will be destroyed in the reverse order of dec
binjin 2014/09/03 11:28:21 I see. But in the following CL I need to reset bot
+ }
+
+ void SetPref(bool managed, const char* path, base::Value* value) {
+ if (managed)
+ pref_service_->SetManagedPref(path, value);
+ else
+ pref_service_->SetUserPref(path, value);
+ }
+
+ void RemovePref(bool managed, const char* path) {
+ if (managed)
+ pref_service_->RemoveManagedPref(path);
+ else
+ pref_service_->RemoveUserPref(path);
+ }
+
+ void Refresh() {
+ extension_management_->Refresh();
+ }
+
+ protected:
+ scoped_ptr<TestingPrefServiceSimple> pref_service_;
+ scoped_ptr<ExtensionManagement> extension_management_;
+};
+
+// Verify that preference controlled by legacy ExtensionInstallSources policy is
+// handled well.
+TEST_F(ExtensionManagementTest, LegacyInstallSources) {
+ base::ListValue allowed_sites_pref;
+ allowed_sites_pref.AppendString("https://www.example.com/foo");
+ allowed_sites_pref.AppendString("https://corp.mycompany.com/*");
+ SetPref(
+ true, pref_names::kAllowedInstallSites, allowed_sites_pref.DeepCopy());
+ Refresh();
+ const URLPatternSet* allowed_sites =
+ extension_management_->ReadGlobalSettings().install_sources.get();
+ ASSERT_TRUE(allowed_sites);
+ EXPECT_FALSE(allowed_sites->is_empty());
+ EXPECT_TRUE(allowed_sites->MatchesURL(GURL("https://www.example.com/foo")));
+ EXPECT_FALSE(allowed_sites->MatchesURL(GURL("https://www.example.com/bar")));
+ EXPECT_TRUE(
+ allowed_sites->MatchesURL(GURL("https://corp.mycompany.com/entry")));
+ EXPECT_FALSE(
+ allowed_sites->MatchesURL(GURL("https://www.mycompany.com/entry")));
+}
+
+// Verify that preference controlled by legacy ExtensionAllowedTypes policy is
+// handled well.
+TEST_F(ExtensionManagementTest, LegacyAllowedTypes) {
+ base::ListValue allowed_types_pref;
+ allowed_types_pref.AppendInteger(Manifest::TYPE_THEME);
+ allowed_types_pref.AppendInteger(Manifest::TYPE_USER_SCRIPT);
+
+ SetPref(true, pref_names::kAllowedTypes, allowed_types_pref.DeepCopy());
+ Refresh();
+ const std::vector<Manifest::Type>* allowed_types =
+ extension_management_->ReadGlobalSettings().allowed_types.get();
+ ASSERT_TRUE(allowed_types);
+ EXPECT_TRUE(allowed_types->size() == 2);
+ EXPECT_FALSE(find(allowed_types->begin(),
Joao da Silva 2014/09/02 19:34:02 Use Contains from base/stl_util.h
binjin 2014/09/02 20:02:56 stl_util.h only provide ContainsKey for containers
+ allowed_types->end(),
+ Manifest::TYPE_EXTENSION) != allowed_types->end());
+ EXPECT_TRUE(find(allowed_types->begin(),
+ allowed_types->end(),
+ Manifest::TYPE_THEME) != allowed_types->end());
+ EXPECT_TRUE(find(allowed_types->begin(),
+ allowed_types->end(),
+ Manifest::TYPE_USER_SCRIPT) != allowed_types->end());
+}
+
+// Verify that preference controlled by legacy ExtensionInstallBlacklist policy
+// is handled well.
+TEST_F(ExtensionManagementTest, LegacyInstallBlacklist) {
+ base::ListValue denied_list_pref;
+ denied_list_pref.AppendString(kTargetExtension);
+
+ SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy());
+ Refresh();
+ EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
+ ExtensionManagement::INSTALLATION_BLOCKED);
+ EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode,
+ ExtensionManagement::INSTALLATION_ALLOWED);
+}
+
+// Verify that preference controlled by legacy ExtensionInstallWhitelist policy
+// is handled well.
+TEST_F(ExtensionManagementTest, LegacyInstallWhitelist) {
+ base::ListValue denied_list_pref;
+ denied_list_pref.AppendString("*");
+ base::ListValue allowed_list_pref;
+ allowed_list_pref.AppendString(kTargetExtension);
+
+ SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy());
+ SetPref(true, pref_names::kInstallAllowList, allowed_list_pref.DeepCopy());
+ Refresh();
+ EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
+ ExtensionManagement::INSTALLATION_ALLOWED);
+ EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode,
+ ExtensionManagement::INSTALLATION_BLOCKED);
+
+ // Verify that install whitelist preference set by user is ignored.
+ RemovePref(true, pref_names::kInstallAllowList);
+ SetPref(false, pref_names::kInstallAllowList, allowed_list_pref.DeepCopy());
+ Refresh();
+ EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
+ ExtensionManagement::INSTALLATION_BLOCKED);
+}
+
+// Verify that preference controlled by legacy ExtensionInstallForcelist policy
+// is handled well.
+TEST_F(ExtensionManagementTest, LegacyInstallForcelist) {
+ base::DictionaryValue forced_list_pref;
+ ExternalPolicyLoader::AddExtension(
+ &forced_list_pref, kTargetExtension, kExampleUpdateUrl);
+
+ SetPref(true, pref_names::kInstallForceList, forced_list_pref.DeepCopy());
+ Refresh();
+ EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
+ ExtensionManagement::INSTALLATION_FORCED);
+ EXPECT_EQ(extension_management_->ReadById(kTargetExtension).update_url,
+ kExampleUpdateUrl);
+ EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode,
+ ExtensionManagement::INSTALLATION_ALLOWED);
+
+ // Verify that install forcelist preference set by user is ignored.
+ RemovePref(true, pref_names::kInstallForceList);
+ SetPref(false, pref_names::kInstallForceList, forced_list_pref.DeepCopy());
+ Refresh();
+ EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
+ ExtensionManagement::INSTALLATION_ALLOWED);
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698