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

Side by Side 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: use std::find() 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <algorithm>
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_registry_simple.h"
9 #include "base/prefs/testing_pref_service.h"
10 #include "base/values.h"
11 #include "chrome/browser/extensions/extension_management.h"
12 #include "chrome/browser/extensions/external_policy_loader.h"
13 #include "extensions/browser/pref_names.h"
14 #include "extensions/common/url_pattern.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace extensions {
18
19 namespace {
20 const char kTargetExtension[] = "abcdefghijklmnopabcdefghijklmnop";
21 const char kOtherExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
22 const char kExampleUpdateUrl[] = "http://example.com/update_url";
23 } // namespace
24
25 class ExtensionManagementTest : public testing::Test {
26 public:
27 ExtensionManagementTest() {}
28 virtual ~ExtensionManagementTest() {}
29
30 // testing::Test:
31 virtual void SetUp() OVERRIDE {
32 pref_service_.reset(new TestingPrefServiceSimple());
33 pref_service_->registry()->RegisterListPref(
34 pref_names::kAllowedInstallSites);
35 pref_service_->registry()->RegisterListPref(pref_names::kAllowedTypes);
36 pref_service_->registry()->RegisterListPref(pref_names::kInstallDenyList);
37 pref_service_->registry()->RegisterListPref(pref_names::kInstallAllowList);
38 pref_service_->registry()->RegisterDictionaryPref(
39 pref_names::kInstallForceList);
40 extension_management_.reset(new ExtensionManagement(pref_service_.get()));
41 }
42
43 virtual void TearDown() OVERRIDE {
44 extension_management_.reset();
45 pref_service_.reset();
46 }
47
48 void SetPref(bool managed, const char* path, base::Value* value) {
49 if (managed)
50 pref_service_->SetManagedPref(path, value);
51 else
52 pref_service_->SetUserPref(path, value);
53 }
54
55 void RemovePref(bool managed, const char* path) {
56 if (managed)
57 pref_service_->RemoveManagedPref(path);
58 else
59 pref_service_->RemoveUserPref(path);
60 }
61
62 void Refresh() {
63 extension_management_->Refresh();
64 }
65
66 protected:
67 scoped_ptr<TestingPrefServiceSimple> pref_service_;
68 scoped_ptr<ExtensionManagement> extension_management_;
69 };
70
71 // Verify that preference controlled by legacy ExtensionInstallSources policy is
72 // handled well.
73 TEST_F(ExtensionManagementTest, LegacyInstallSources) {
74 base::ListValue allowed_sites_pref;
75 allowed_sites_pref.AppendString("https://www.example.com/foo");
76 allowed_sites_pref.AppendString("https://corp.mycompany.com/*");
77 SetPref(
78 true, pref_names::kAllowedInstallSites, allowed_sites_pref.DeepCopy());
79 Refresh();
80 const URLPatternSet& allowed_sites =
81 extension_management_->ReadGlobalSettings().install_sources;
82 ASSERT_TRUE(extension_management_->ReadGlobalSettings()
83 .has_restricted_install_sources);
84 EXPECT_FALSE(allowed_sites.is_empty());
85 EXPECT_TRUE(allowed_sites.MatchesURL(GURL("https://www.example.com/foo")));
86 EXPECT_FALSE(allowed_sites.MatchesURL(GURL("https://www.example.com/bar")));
87 EXPECT_TRUE(
88 allowed_sites.MatchesURL(GURL("https://corp.mycompany.com/entry")));
89 EXPECT_FALSE(
90 allowed_sites.MatchesURL(GURL("https://www.mycompany.com/entry")));
91 }
92
93 // Verify that preference controlled by legacy ExtensionAllowedTypes policy is
94 // handled well.
95 TEST_F(ExtensionManagementTest, LegacyAllowedTypes) {
96 base::ListValue allowed_types_pref;
97 allowed_types_pref.AppendInteger(Manifest::TYPE_THEME);
98 allowed_types_pref.AppendInteger(Manifest::TYPE_USER_SCRIPT);
99
100 SetPref(true, pref_names::kAllowedTypes, allowed_types_pref.DeepCopy());
101 Refresh();
102 const std::vector<Manifest::Type>& allowed_types =
103 extension_management_->ReadGlobalSettings().allowed_types;
104 ASSERT_TRUE(
105 extension_management_->ReadGlobalSettings().has_restricted_allowed_types);
106 EXPECT_TRUE(allowed_types.size() == 2);
107 EXPECT_FALSE(std::find(allowed_types.begin(),
108 allowed_types.end(),
109 Manifest::TYPE_EXTENSION) != allowed_types.end());
110 EXPECT_TRUE(std::find(allowed_types.begin(),
111 allowed_types.end(),
112 Manifest::TYPE_THEME) != allowed_types.end());
113 EXPECT_TRUE(std::find(allowed_types.begin(),
114 allowed_types.end(),
115 Manifest::TYPE_USER_SCRIPT) != allowed_types.end());
116 }
117
118 // Verify that preference controlled by legacy ExtensionInstallBlacklist policy
119 // is handled well.
120 TEST_F(ExtensionManagementTest, LegacyInstallBlacklist) {
121 base::ListValue denied_list_pref;
122 denied_list_pref.AppendString(kTargetExtension);
123
124 SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy());
125 Refresh();
126 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
127 ExtensionManagement::INSTALLATION_BLOCKED);
128 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode,
129 ExtensionManagement::INSTALLATION_ALLOWED);
130 }
131
132 // Verify that preference controlled by legacy ExtensionInstallWhitelist policy
133 // is handled well.
134 TEST_F(ExtensionManagementTest, LegacyInstallWhitelist) {
135 base::ListValue denied_list_pref;
136 denied_list_pref.AppendString("*");
137 base::ListValue allowed_list_pref;
138 allowed_list_pref.AppendString(kTargetExtension);
139
140 SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy());
141 SetPref(true, pref_names::kInstallAllowList, allowed_list_pref.DeepCopy());
142 Refresh();
143 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
144 ExtensionManagement::INSTALLATION_ALLOWED);
145 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode,
146 ExtensionManagement::INSTALLATION_BLOCKED);
147
148 // Verify that install whitelist preference set by user is ignored.
149 RemovePref(true, pref_names::kInstallAllowList);
150 SetPref(false, pref_names::kInstallAllowList, allowed_list_pref.DeepCopy());
151 Refresh();
152 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
153 ExtensionManagement::INSTALLATION_BLOCKED);
154 }
155
156 // Verify that preference controlled by legacy ExtensionInstallForcelist policy
157 // is handled well.
158 TEST_F(ExtensionManagementTest, LegacyInstallForcelist) {
159 base::DictionaryValue forced_list_pref;
160 ExternalPolicyLoader::AddExtension(
161 &forced_list_pref, kTargetExtension, kExampleUpdateUrl);
162
163 SetPref(true, pref_names::kInstallForceList, forced_list_pref.DeepCopy());
164 Refresh();
165 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
166 ExtensionManagement::INSTALLATION_FORCED);
167 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).update_url,
168 kExampleUpdateUrl);
169 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode,
170 ExtensionManagement::INSTALLATION_ALLOWED);
171
172 // Verify that install forcelist preference set by user is ignored.
173 RemovePref(true, pref_names::kInstallForceList);
174 SetPref(false, pref_names::kInstallForceList, forced_list_pref.DeepCopy());
175 Refresh();
176 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
177 ExtensionManagement::INSTALLATION_ALLOWED);
178 }
179
180 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698