OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
| 6 #include <vector> |
6 | 7 |
| 8 #include "base/json/json_parser.h" |
7 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
8 #include "base/prefs/pref_registry_simple.h" | 10 #include "base/prefs/pref_registry_simple.h" |
9 #include "base/prefs/testing_pref_service.h" | 11 #include "base/prefs/testing_pref_service.h" |
| 12 #include "base/strings/stringprintf.h" |
10 #include "base/values.h" | 13 #include "base/values.h" |
11 #include "chrome/browser/extensions/extension_management.h" | 14 #include "chrome/browser/extensions/extension_management.h" |
| 15 #include "chrome/browser/extensions/extension_management_test_util.h" |
12 #include "chrome/browser/extensions/external_policy_loader.h" | 16 #include "chrome/browser/extensions/external_policy_loader.h" |
13 #include "extensions/browser/pref_names.h" | 17 #include "extensions/browser/pref_names.h" |
| 18 #include "extensions/common/manifest.h" |
14 #include "extensions/common/manifest_constants.h" | 19 #include "extensions/common/manifest_constants.h" |
15 #include "extensions/common/url_pattern.h" | 20 #include "extensions/common/url_pattern.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "url/gurl.h" |
17 | 23 |
18 namespace extensions { | 24 namespace extensions { |
19 | 25 |
20 namespace { | 26 namespace { |
| 27 |
21 const char kTargetExtension[] = "abcdefghijklmnopabcdefghijklmnop"; | 28 const char kTargetExtension[] = "abcdefghijklmnopabcdefghijklmnop"; |
| 29 const char kTargetExtension2[] = "bcdefghijklmnopabcdefghijklmnopa"; |
| 30 const char kTargetExtension3[] = "cdefghijklmnopabcdefghijklmnopab"; |
22 const char kOtherExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; | 31 const char kOtherExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
23 const char kExampleUpdateUrl[] = "http://example.com/update_url"; | 32 const char kExampleUpdateUrl[] = "http://example.com/update_url"; |
| 33 |
| 34 const char kExampleDictPreference[] = |
| 35 "{" |
| 36 " \"%s\": {" // kTargetExtension |
| 37 " \"installation_mode\": \"allowed\"," |
| 38 " }," |
| 39 " \"%s\": {" // kTargetExtension2 |
| 40 " \"installation_mode\": \"force_installed\"," |
| 41 " \"update_url\": \"http://example.com/update_url\"," |
| 42 " }," |
| 43 " \"%s\": {" // kTargetExtension3 |
| 44 " \"installation_mode\": \"normal_installed\"," |
| 45 " \"update_url\": \"http://example.com/update_url\"," |
| 46 " }," |
| 47 " \"*\": {" |
| 48 " \"installation_mode\": \"blocked\"," |
| 49 " \"install_sources\": [\"*://foo.com/*\"]," |
| 50 " \"allowed_types\": [\"theme\", \"user_script\"]," |
| 51 " }," |
| 52 "}"; |
| 53 |
24 } // namespace | 54 } // namespace |
25 | 55 |
26 class ExtensionManagementTest : public testing::Test { | 56 class ExtensionManagementServiceTest : public testing::Test { |
27 public: | 57 public: |
28 ExtensionManagementTest() {} | 58 typedef ExtensionManagementPrefUpdater<TestingPrefServiceSimple> PrefUpdater; |
29 virtual ~ExtensionManagementTest() {} | 59 |
| 60 ExtensionManagementServiceTest() {} |
| 61 virtual ~ExtensionManagementServiceTest() {} |
30 | 62 |
31 // testing::Test: | 63 // testing::Test: |
32 virtual void SetUp() OVERRIDE { | 64 virtual void SetUp() OVERRIDE { |
33 InitPrefService(); | 65 InitPrefService(); |
34 } | 66 } |
35 | 67 |
36 void InitPrefService() { | 68 void InitPrefService() { |
37 extension_management_.reset(); | 69 extension_management_.reset(); |
38 pref_service_.reset(new TestingPrefServiceSimple()); | 70 pref_service_.reset(new TestingPrefServiceSimple()); |
39 pref_service_->registry()->RegisterListPref( | 71 pref_service_->registry()->RegisterListPref( |
40 pref_names::kAllowedInstallSites); | 72 pref_names::kAllowedInstallSites); |
41 pref_service_->registry()->RegisterListPref(pref_names::kAllowedTypes); | 73 pref_service_->registry()->RegisterListPref(pref_names::kAllowedTypes); |
42 pref_service_->registry()->RegisterListPref(pref_names::kInstallDenyList); | 74 pref_service_->registry()->RegisterListPref(pref_names::kInstallDenyList); |
43 pref_service_->registry()->RegisterListPref(pref_names::kInstallAllowList); | 75 pref_service_->registry()->RegisterListPref(pref_names::kInstallAllowList); |
44 pref_service_->registry()->RegisterDictionaryPref( | 76 pref_service_->registry()->RegisterDictionaryPref( |
45 pref_names::kInstallForceList); | 77 pref_names::kInstallForceList); |
| 78 pref_service_->registry()->RegisterDictionaryPref( |
| 79 pref_names::kExtensionManagement); |
46 extension_management_.reset(new ExtensionManagement(pref_service_.get())); | 80 extension_management_.reset(new ExtensionManagement(pref_service_.get())); |
47 } | 81 } |
48 | 82 |
49 void SetPref(bool managed, const char* path, base::Value* value) { | 83 void SetPref(bool managed, const char* path, base::Value* value) { |
50 if (managed) | 84 if (managed) |
51 pref_service_->SetManagedPref(path, value); | 85 pref_service_->SetManagedPref(path, value); |
52 else | 86 else |
53 pref_service_->SetUserPref(path, value); | 87 pref_service_->SetUserPref(path, value); |
54 } | 88 } |
55 | 89 |
56 void RemovePref(bool managed, const char* path) { | 90 void RemovePref(bool managed, const char* path) { |
57 if (managed) | 91 if (managed) |
58 pref_service_->RemoveManagedPref(path); | 92 pref_service_->RemoveManagedPref(path); |
59 else | 93 else |
60 pref_service_->RemoveUserPref(path); | 94 pref_service_->RemoveUserPref(path); |
61 } | 95 } |
62 | 96 |
| 97 void SetExampleDictPref() { |
| 98 std::string pref_json_string = base::StringPrintf(kExampleDictPreference, |
| 99 kTargetExtension, |
| 100 kTargetExtension2, |
| 101 kTargetExtension3); |
| 102 std::string error_msg; |
| 103 scoped_ptr<base::Value> parsed(base::JSONReader::ReadAndReturnError( |
| 104 pref_json_string, |
| 105 base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS, |
| 106 NULL, |
| 107 &error_msg)); |
| 108 ASSERT_TRUE(parsed && parsed->IsType(base::Value::TYPE_DICTIONARY)) |
| 109 << error_msg; |
| 110 SetPref(true, pref_names::kExtensionManagement, parsed->DeepCopy()); |
| 111 } |
| 112 |
63 protected: | 113 protected: |
64 scoped_ptr<TestingPrefServiceSimple> pref_service_; | 114 scoped_ptr<TestingPrefServiceSimple> pref_service_; |
65 scoped_ptr<ExtensionManagement> extension_management_; | 115 scoped_ptr<ExtensionManagement> extension_management_; |
66 }; | 116 }; |
67 | 117 |
68 class ExtensionAdminPolicyTest : public ExtensionManagementTest { | 118 class ExtensionAdminPolicyTest : public ExtensionManagementServiceTest { |
69 public: | 119 public: |
70 ExtensionAdminPolicyTest() {} | 120 ExtensionAdminPolicyTest() {} |
71 virtual ~ExtensionAdminPolicyTest() {} | 121 virtual ~ExtensionAdminPolicyTest() {} |
72 | 122 |
73 void CreateExtension(Manifest::Location location) { | 123 void CreateExtension(Manifest::Location location) { |
74 base::DictionaryValue values; | 124 base::DictionaryValue values; |
75 CreateExtensionFromValues(location, &values); | 125 CreateExtensionFromValues(location, &values); |
76 } | 126 } |
77 | 127 |
78 void CreateHostedApp(Manifest::Location location) { | 128 void CreateHostedApp(Manifest::Location location) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 | 194 |
145 bool ExtensionAdminPolicyTest::MustRemainEnabled(const Extension* extension, | 195 bool ExtensionAdminPolicyTest::MustRemainEnabled(const Extension* extension, |
146 base::string16* error) { | 196 base::string16* error) { |
147 InitPrefService(); | 197 InitPrefService(); |
148 return extension_management_->GetProvider()->MustRemainEnabled(extension, | 198 return extension_management_->GetProvider()->MustRemainEnabled(extension, |
149 error); | 199 error); |
150 } | 200 } |
151 | 201 |
152 // Verify that preference controlled by legacy ExtensionInstallSources policy is | 202 // Verify that preference controlled by legacy ExtensionInstallSources policy is |
153 // handled well. | 203 // handled well. |
154 TEST_F(ExtensionManagementTest, LegacyInstallSources) { | 204 TEST_F(ExtensionManagementServiceTest, LegacyInstallSources) { |
155 base::ListValue allowed_sites_pref; | 205 base::ListValue allowed_sites_pref; |
156 allowed_sites_pref.AppendString("https://www.example.com/foo"); | 206 allowed_sites_pref.AppendString("https://www.example.com/foo"); |
157 allowed_sites_pref.AppendString("https://corp.mycompany.com/*"); | 207 allowed_sites_pref.AppendString("https://corp.mycompany.com/*"); |
158 SetPref( | 208 SetPref( |
159 true, pref_names::kAllowedInstallSites, allowed_sites_pref.DeepCopy()); | 209 true, pref_names::kAllowedInstallSites, allowed_sites_pref.DeepCopy()); |
160 const URLPatternSet& allowed_sites = | 210 const URLPatternSet& allowed_sites = |
161 extension_management_->ReadGlobalSettings().install_sources; | 211 extension_management_->ReadGlobalSettings().install_sources; |
162 ASSERT_TRUE(extension_management_->ReadGlobalSettings() | 212 ASSERT_TRUE(extension_management_->ReadGlobalSettings() |
163 .has_restricted_install_sources); | 213 .has_restricted_install_sources); |
164 EXPECT_FALSE(allowed_sites.is_empty()); | 214 EXPECT_FALSE(allowed_sites.is_empty()); |
165 EXPECT_TRUE(allowed_sites.MatchesURL(GURL("https://www.example.com/foo"))); | 215 EXPECT_TRUE(allowed_sites.MatchesURL(GURL("https://www.example.com/foo"))); |
166 EXPECT_FALSE(allowed_sites.MatchesURL(GURL("https://www.example.com/bar"))); | 216 EXPECT_FALSE(allowed_sites.MatchesURL(GURL("https://www.example.com/bar"))); |
167 EXPECT_TRUE( | 217 EXPECT_TRUE( |
168 allowed_sites.MatchesURL(GURL("https://corp.mycompany.com/entry"))); | 218 allowed_sites.MatchesURL(GURL("https://corp.mycompany.com/entry"))); |
169 EXPECT_FALSE( | 219 EXPECT_FALSE( |
170 allowed_sites.MatchesURL(GURL("https://www.mycompany.com/entry"))); | 220 allowed_sites.MatchesURL(GURL("https://www.mycompany.com/entry"))); |
171 } | 221 } |
172 | 222 |
173 // Verify that preference controlled by legacy ExtensionAllowedTypes policy is | 223 // Verify that preference controlled by legacy ExtensionAllowedTypes policy is |
174 // handled well. | 224 // handled well. |
175 TEST_F(ExtensionManagementTest, LegacyAllowedTypes) { | 225 TEST_F(ExtensionManagementServiceTest, LegacyAllowedTypes) { |
176 base::ListValue allowed_types_pref; | 226 base::ListValue allowed_types_pref; |
177 allowed_types_pref.AppendInteger(Manifest::TYPE_THEME); | 227 allowed_types_pref.AppendInteger(Manifest::TYPE_THEME); |
178 allowed_types_pref.AppendInteger(Manifest::TYPE_USER_SCRIPT); | 228 allowed_types_pref.AppendInteger(Manifest::TYPE_USER_SCRIPT); |
179 | 229 |
180 SetPref(true, pref_names::kAllowedTypes, allowed_types_pref.DeepCopy()); | 230 SetPref(true, pref_names::kAllowedTypes, allowed_types_pref.DeepCopy()); |
181 const std::vector<Manifest::Type>& allowed_types = | 231 const std::vector<Manifest::Type>& allowed_types = |
182 extension_management_->ReadGlobalSettings().allowed_types; | 232 extension_management_->ReadGlobalSettings().allowed_types; |
183 ASSERT_TRUE( | 233 ASSERT_TRUE( |
184 extension_management_->ReadGlobalSettings().has_restricted_allowed_types); | 234 extension_management_->ReadGlobalSettings().has_restricted_allowed_types); |
185 EXPECT_TRUE(allowed_types.size() == 2); | 235 EXPECT_TRUE(allowed_types.size() == 2); |
186 EXPECT_FALSE(std::find(allowed_types.begin(), | 236 EXPECT_FALSE(std::find(allowed_types.begin(), |
187 allowed_types.end(), | 237 allowed_types.end(), |
188 Manifest::TYPE_EXTENSION) != allowed_types.end()); | 238 Manifest::TYPE_EXTENSION) != allowed_types.end()); |
189 EXPECT_TRUE(std::find(allowed_types.begin(), | 239 EXPECT_TRUE(std::find(allowed_types.begin(), |
190 allowed_types.end(), | 240 allowed_types.end(), |
191 Manifest::TYPE_THEME) != allowed_types.end()); | 241 Manifest::TYPE_THEME) != allowed_types.end()); |
192 EXPECT_TRUE(std::find(allowed_types.begin(), | 242 EXPECT_TRUE(std::find(allowed_types.begin(), |
193 allowed_types.end(), | 243 allowed_types.end(), |
194 Manifest::TYPE_USER_SCRIPT) != allowed_types.end()); | 244 Manifest::TYPE_USER_SCRIPT) != allowed_types.end()); |
195 } | 245 } |
196 | 246 |
197 // Verify that preference controlled by legacy ExtensionInstallBlacklist policy | 247 // Verify that preference controlled by legacy ExtensionInstallBlacklist policy |
198 // is handled well. | 248 // is handled well. |
199 TEST_F(ExtensionManagementTest, LegacyInstallBlacklist) { | 249 TEST_F(ExtensionManagementServiceTest, LegacyInstallBlacklist) { |
200 base::ListValue denied_list_pref; | 250 base::ListValue denied_list_pref; |
201 denied_list_pref.AppendString(kTargetExtension); | 251 denied_list_pref.AppendString(kTargetExtension); |
202 | 252 |
203 SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy()); | 253 SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy()); |
204 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode, | 254 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode, |
205 ExtensionManagement::INSTALLATION_BLOCKED); | 255 ExtensionManagement::INSTALLATION_BLOCKED); |
206 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode, | 256 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode, |
207 ExtensionManagement::INSTALLATION_ALLOWED); | 257 ExtensionManagement::INSTALLATION_ALLOWED); |
208 } | 258 } |
209 | 259 |
210 // Verify that preference controlled by legacy ExtensionInstallWhitelist policy | 260 // Verify that preference controlled by legacy ExtensionInstallWhitelist policy |
211 // is handled well. | 261 // is handled well. |
212 TEST_F(ExtensionManagementTest, LegacyInstallWhitelist) { | 262 TEST_F(ExtensionManagementServiceTest, LegacyInstallWhitelist) { |
213 base::ListValue denied_list_pref; | 263 base::ListValue denied_list_pref; |
214 denied_list_pref.AppendString("*"); | 264 denied_list_pref.AppendString("*"); |
215 base::ListValue allowed_list_pref; | 265 base::ListValue allowed_list_pref; |
216 allowed_list_pref.AppendString(kTargetExtension); | 266 allowed_list_pref.AppendString(kTargetExtension); |
217 | 267 |
218 SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy()); | 268 SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy()); |
219 SetPref(true, pref_names::kInstallAllowList, allowed_list_pref.DeepCopy()); | 269 SetPref(true, pref_names::kInstallAllowList, allowed_list_pref.DeepCopy()); |
220 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode, | 270 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode, |
221 ExtensionManagement::INSTALLATION_ALLOWED); | 271 ExtensionManagement::INSTALLATION_ALLOWED); |
222 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode, | 272 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode, |
223 ExtensionManagement::INSTALLATION_BLOCKED); | 273 ExtensionManagement::INSTALLATION_BLOCKED); |
224 | 274 |
225 // Verify that install whitelist preference set by user is ignored. | 275 // Verify that install whitelist preference set by user is ignored. |
226 RemovePref(true, pref_names::kInstallAllowList); | 276 RemovePref(true, pref_names::kInstallAllowList); |
227 SetPref(false, pref_names::kInstallAllowList, allowed_list_pref.DeepCopy()); | 277 SetPref(false, pref_names::kInstallAllowList, allowed_list_pref.DeepCopy()); |
228 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode, | 278 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode, |
229 ExtensionManagement::INSTALLATION_BLOCKED); | 279 ExtensionManagement::INSTALLATION_BLOCKED); |
230 } | 280 } |
231 | 281 |
232 // Verify that preference controlled by legacy ExtensionInstallForcelist policy | 282 // Verify that preference controlled by legacy ExtensionInstallForcelist policy |
233 // is handled well. | 283 // is handled well. |
234 TEST_F(ExtensionManagementTest, LegacyInstallForcelist) { | 284 TEST_F(ExtensionManagementServiceTest, LegacyInstallForcelist) { |
235 base::DictionaryValue forced_list_pref; | 285 base::DictionaryValue forced_list_pref; |
236 ExternalPolicyLoader::AddExtension( | 286 ExternalPolicyLoader::AddExtension( |
237 &forced_list_pref, kTargetExtension, kExampleUpdateUrl); | 287 &forced_list_pref, kTargetExtension, kExampleUpdateUrl); |
238 | 288 |
239 SetPref(true, pref_names::kInstallForceList, forced_list_pref.DeepCopy()); | 289 SetPref(true, pref_names::kInstallForceList, forced_list_pref.DeepCopy()); |
240 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode, | 290 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode, |
241 ExtensionManagement::INSTALLATION_FORCED); | 291 ExtensionManagement::INSTALLATION_FORCED); |
242 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).update_url, | 292 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).update_url, |
243 kExampleUpdateUrl); | 293 kExampleUpdateUrl); |
244 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode, | 294 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode, |
245 ExtensionManagement::INSTALLATION_ALLOWED); | 295 ExtensionManagement::INSTALLATION_ALLOWED); |
246 | 296 |
247 // Verify that install forcelist preference set by user is ignored. | 297 // Verify that install forcelist preference set by user is ignored. |
248 RemovePref(true, pref_names::kInstallForceList); | 298 RemovePref(true, pref_names::kInstallForceList); |
249 SetPref(false, pref_names::kInstallForceList, forced_list_pref.DeepCopy()); | 299 SetPref(false, pref_names::kInstallForceList, forced_list_pref.DeepCopy()); |
250 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode, | 300 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode, |
251 ExtensionManagement::INSTALLATION_ALLOWED); | 301 ExtensionManagement::INSTALLATION_ALLOWED); |
252 } | 302 } |
253 | 303 |
| 304 // Tests parsing of new dictionary preference. |
| 305 TEST_F(ExtensionManagementServiceTest, PreferenceParsing) { |
| 306 SetExampleDictPref(); |
| 307 |
| 308 // Verifies the installation mode settings. |
| 309 EXPECT_TRUE(extension_management_->BlacklistedByDefault()); |
| 310 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode, |
| 311 ExtensionManagement::INSTALLATION_ALLOWED); |
| 312 EXPECT_EQ( |
| 313 extension_management_->ReadById(kTargetExtension2).installation_mode, |
| 314 ExtensionManagement::INSTALLATION_FORCED); |
| 315 EXPECT_EQ(extension_management_->ReadById(kTargetExtension2).update_url, |
| 316 kExampleUpdateUrl); |
| 317 EXPECT_EQ( |
| 318 extension_management_->ReadById(kTargetExtension3).installation_mode, |
| 319 ExtensionManagement::INSTALLATION_RECOMMENDED); |
| 320 EXPECT_EQ(extension_management_->ReadById(kTargetExtension3).update_url, |
| 321 kExampleUpdateUrl); |
| 322 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode, |
| 323 ExtensionManagement::INSTALLATION_BLOCKED); |
| 324 |
| 325 // Verifies global settings. |
| 326 EXPECT_TRUE(extension_management_->ReadGlobalSettings() |
| 327 .has_restricted_install_sources); |
| 328 const URLPatternSet& allowed_sites = |
| 329 extension_management_->ReadGlobalSettings().install_sources; |
| 330 EXPECT_EQ(allowed_sites.size(), 1u); |
| 331 EXPECT_TRUE(allowed_sites.MatchesURL(GURL("http://foo.com/entry"))); |
| 332 EXPECT_FALSE(allowed_sites.MatchesURL(GURL("http://bar.com/entry"))); |
| 333 |
| 334 EXPECT_TRUE( |
| 335 extension_management_->ReadGlobalSettings().has_restricted_allowed_types); |
| 336 const std::vector<Manifest::Type>& allowed_types = |
| 337 extension_management_->ReadGlobalSettings().allowed_types; |
| 338 EXPECT_EQ(allowed_types.size(), 2u); |
| 339 EXPECT_TRUE(std::find(allowed_types.begin(), |
| 340 allowed_types.end(), |
| 341 Manifest::TYPE_THEME) != allowed_types.end()); |
| 342 EXPECT_TRUE(std::find(allowed_types.begin(), |
| 343 allowed_types.end(), |
| 344 Manifest::TYPE_USER_SCRIPT) != allowed_types.end()); |
| 345 } |
| 346 |
| 347 // Tests functionality of new preference as to deprecate legacy |
| 348 // ExtensionInstallSources policy. |
| 349 TEST_F(ExtensionManagementServiceTest, NewInstallSources) { |
| 350 // Set the legacy preference, and verifies that it works. |
| 351 base::ListValue allowed_sites_pref; |
| 352 allowed_sites_pref.AppendString("https://www.example.com/foo"); |
| 353 SetPref( |
| 354 true, pref_names::kAllowedInstallSites, allowed_sites_pref.DeepCopy()); |
| 355 EXPECT_TRUE(extension_management_->ReadGlobalSettings() |
| 356 .has_restricted_install_sources); |
| 357 EXPECT_TRUE( |
| 358 extension_management_->ReadGlobalSettings() |
| 359 .install_sources.MatchesURL(GURL("https://www.example.com/foo"))); |
| 360 |
| 361 // Set the new dictionary preference. |
| 362 { |
| 363 PrefUpdater updater(pref_service_.get()); |
| 364 updater.ClearInstallSources(); |
| 365 } |
| 366 // Verifies that the new one overrides the legacy ones. |
| 367 EXPECT_TRUE(extension_management_->ReadGlobalSettings() |
| 368 .has_restricted_install_sources); |
| 369 EXPECT_FALSE( |
| 370 extension_management_->ReadGlobalSettings() |
| 371 .install_sources.MatchesURL(GURL("https://www.example.com/foo"))); |
| 372 |
| 373 // update the new dictionary preference. |
| 374 { |
| 375 PrefUpdater updater(pref_service_.get()); |
| 376 updater.AddInstallSource("https://corp.mycompany.com/*"); |
| 377 } |
| 378 EXPECT_TRUE(extension_management_->ReadGlobalSettings() |
| 379 .has_restricted_install_sources); |
| 380 EXPECT_TRUE(extension_management_->ReadGlobalSettings() |
| 381 .install_sources.MatchesURL( |
| 382 GURL("https://corp.mycompany.com/entry"))); |
| 383 } |
| 384 |
| 385 // Tests functionality of new preference as to deprecate legacy |
| 386 // ExtensionAllowedTypes policy. |
| 387 TEST_F(ExtensionManagementServiceTest, NewAllowedTypes) { |
| 388 // Set the legacy preference, and verifies that it works. |
| 389 base::ListValue allowed_types_pref; |
| 390 allowed_types_pref.AppendInteger(Manifest::TYPE_USER_SCRIPT); |
| 391 SetPref(true, pref_names::kAllowedTypes, allowed_types_pref.DeepCopy()); |
| 392 EXPECT_TRUE( |
| 393 extension_management_->ReadGlobalSettings().has_restricted_allowed_types); |
| 394 EXPECT_EQ(extension_management_->ReadGlobalSettings().allowed_types.size(), |
| 395 1u); |
| 396 EXPECT_EQ(extension_management_->ReadGlobalSettings().allowed_types[0], |
| 397 Manifest::TYPE_USER_SCRIPT); |
| 398 |
| 399 // Set the new dictionary preference. |
| 400 { |
| 401 PrefUpdater updater(pref_service_.get()); |
| 402 updater.ClearAllowedTypes(); |
| 403 } |
| 404 // Verifies that the new one overrides the legacy ones. |
| 405 EXPECT_TRUE( |
| 406 extension_management_->ReadGlobalSettings().has_restricted_allowed_types); |
| 407 EXPECT_EQ(extension_management_->ReadGlobalSettings().allowed_types.size(), |
| 408 0u); |
| 409 |
| 410 // update the new dictionary preference. |
| 411 { |
| 412 PrefUpdater updater(pref_service_.get()); |
| 413 updater.AddAllowedType("theme"); |
| 414 } |
| 415 EXPECT_TRUE( |
| 416 extension_management_->ReadGlobalSettings().has_restricted_allowed_types); |
| 417 EXPECT_EQ(extension_management_->ReadGlobalSettings().allowed_types.size(), |
| 418 1u); |
| 419 EXPECT_EQ(extension_management_->ReadGlobalSettings().allowed_types[0], |
| 420 Manifest::TYPE_THEME); |
| 421 } |
| 422 |
| 423 // Tests functionality of new preference as to deprecate legacy |
| 424 // ExtensionInstallBlacklist policy. |
| 425 TEST_F(ExtensionManagementServiceTest, NewInstallBlacklist) { |
| 426 // Set the new dictionary preference. |
| 427 { |
| 428 PrefUpdater updater(pref_service_.get()); |
| 429 updater.SetBlacklistedByDefault(false); // Allowed by default. |
| 430 updater.SetIndividualExtensionInstallationAllowed(kTargetExtension, false); |
| 431 } |
| 432 EXPECT_FALSE(extension_management_->BlacklistedByDefault()); |
| 433 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode, |
| 434 ExtensionManagement::INSTALLATION_BLOCKED); |
| 435 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode, |
| 436 ExtensionManagement::INSTALLATION_ALLOWED); |
| 437 |
| 438 // Set legacy preference. |
| 439 base::ListValue denied_list_pref; |
| 440 denied_list_pref.AppendString("*"); |
| 441 denied_list_pref.AppendString(kTargetExtension2); |
| 442 SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy()); |
| 443 |
| 444 base::ListValue allowed_list_pref; |
| 445 allowed_list_pref.AppendString(kTargetExtension); |
| 446 SetPref(true, pref_names::kInstallAllowList, allowed_list_pref.DeepCopy()); |
| 447 |
| 448 // Verifies that the new one have higher priority over the legacy ones. |
| 449 EXPECT_FALSE(extension_management_->BlacklistedByDefault()); |
| 450 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode, |
| 451 ExtensionManagement::INSTALLATION_BLOCKED); |
| 452 EXPECT_EQ( |
| 453 extension_management_->ReadById(kTargetExtension2).installation_mode, |
| 454 ExtensionManagement::INSTALLATION_BLOCKED); |
| 455 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode, |
| 456 ExtensionManagement::INSTALLATION_ALLOWED); |
| 457 } |
| 458 |
| 459 // Tests functionality of new preference as to deprecate legacy |
| 460 // ExtensionInstallWhitelist policy. |
| 461 TEST_F(ExtensionManagementServiceTest, NewInstallWhitelist) { |
| 462 // Set the new dictionary preference. |
| 463 { |
| 464 PrefUpdater updater(pref_service_.get()); |
| 465 updater.SetBlacklistedByDefault(true); // Disallowed by default. |
| 466 updater.SetIndividualExtensionInstallationAllowed(kTargetExtension, true); |
| 467 } |
| 468 EXPECT_TRUE(extension_management_->BlacklistedByDefault()); |
| 469 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode, |
| 470 ExtensionManagement::INSTALLATION_ALLOWED); |
| 471 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode, |
| 472 ExtensionManagement::INSTALLATION_BLOCKED); |
| 473 |
| 474 // Set legacy preference. |
| 475 base::ListValue denied_list_pref; |
| 476 denied_list_pref.AppendString(kTargetExtension); |
| 477 SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy()); |
| 478 |
| 479 base::ListValue allowed_list_pref; |
| 480 allowed_list_pref.AppendString(kTargetExtension2); |
| 481 SetPref(true, pref_names::kInstallAllowList, allowed_list_pref.DeepCopy()); |
| 482 |
| 483 // Verifies that the new one have higher priority over the legacy ones. |
| 484 EXPECT_TRUE(extension_management_->BlacklistedByDefault()); |
| 485 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode, |
| 486 ExtensionManagement::INSTALLATION_ALLOWED); |
| 487 EXPECT_EQ( |
| 488 extension_management_->ReadById(kTargetExtension2).installation_mode, |
| 489 ExtensionManagement::INSTALLATION_ALLOWED); |
| 490 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode, |
| 491 ExtensionManagement::INSTALLATION_BLOCKED); |
| 492 } |
| 493 |
| 494 // Tests functionality of new preference as to deprecate legacy |
| 495 // ExtensionInstallForcelist policy. |
| 496 TEST_F(ExtensionManagementServiceTest, NewInstallForcelist) { |
| 497 // Set some legacy preferences, to verify that the new one overrides the |
| 498 // legacy ones. |
| 499 base::ListValue denied_list_pref; |
| 500 denied_list_pref.AppendString(kTargetExtension); |
| 501 SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy()); |
| 502 |
| 503 // Set the new dictionary preference. |
| 504 { |
| 505 PrefUpdater updater(pref_service_.get()); |
| 506 updater.SetIndividualExtensionAutoInstalled( |
| 507 kTargetExtension, kExampleUpdateUrl, true); |
| 508 } |
| 509 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode, |
| 510 ExtensionManagement::INSTALLATION_FORCED); |
| 511 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).update_url, |
| 512 kExampleUpdateUrl); |
| 513 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode, |
| 514 ExtensionManagement::INSTALLATION_ALLOWED); |
| 515 } |
| 516 |
254 // Tests the flag value indicating that extensions are blacklisted by default. | 517 // Tests the flag value indicating that extensions are blacklisted by default. |
255 TEST_F(ExtensionAdminPolicyTest, BlacklistedByDefault) { | 518 TEST_F(ExtensionAdminPolicyTest, BlacklistedByDefault) { |
256 EXPECT_FALSE(BlacklistedByDefault(NULL)); | 519 EXPECT_FALSE(BlacklistedByDefault(NULL)); |
257 | 520 |
258 base::ListValue blacklist; | 521 base::ListValue blacklist; |
259 blacklist.Append(new base::StringValue(kOtherExtension)); | 522 blacklist.Append(new base::StringValue(kOtherExtension)); |
260 EXPECT_FALSE(BlacklistedByDefault(&blacklist)); | 523 EXPECT_FALSE(BlacklistedByDefault(&blacklist)); |
261 blacklist.Append(new base::StringValue("*")); | 524 blacklist.Append(new base::StringValue("*")); |
262 EXPECT_TRUE(BlacklistedByDefault(&blacklist)); | 525 EXPECT_TRUE(BlacklistedByDefault(&blacklist)); |
263 | 526 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 EXPECT_FALSE(error.empty()); | 656 EXPECT_FALSE(error.empty()); |
394 | 657 |
395 CreateExtension(Manifest::INTERNAL); | 658 CreateExtension(Manifest::INTERNAL); |
396 error.clear(); | 659 error.clear(); |
397 EXPECT_FALSE(MustRemainEnabled(extension_.get(), NULL)); | 660 EXPECT_FALSE(MustRemainEnabled(extension_.get(), NULL)); |
398 EXPECT_FALSE(MustRemainEnabled(extension_.get(), &error)); | 661 EXPECT_FALSE(MustRemainEnabled(extension_.get(), &error)); |
399 EXPECT_TRUE(error.empty()); | 662 EXPECT_TRUE(error.empty()); |
400 } | 663 } |
401 | 664 |
402 } // namespace extensions | 665 } // namespace extensions |
OLD | NEW |