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 #include <vector> |
7 | 7 |
8 #include "base/json/json_parser.h" | 8 #include "base/json/json_parser.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/prefs/pref_registry_simple.h" | 10 #include "base/prefs/pref_registry_simple.h" |
11 #include "base/prefs/testing_pref_service.h" | 11 #include "base/prefs/testing_pref_service.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/extensions/extension_management.h" | 13 #include "chrome/browser/extensions/extension_management.h" |
14 #include "chrome/browser/extensions/extension_management_internal.h" | 14 #include "chrome/browser/extensions/extension_management_internal.h" |
15 #include "chrome/browser/extensions/extension_management_test_util.h" | 15 #include "chrome/browser/extensions/extension_management_test_util.h" |
16 #include "chrome/browser/extensions/external_policy_loader.h" | 16 #include "chrome/browser/extensions/external_policy_loader.h" |
| 17 #include "chrome/browser/extensions/standard_management_policy_provider.h" |
17 #include "extensions/browser/pref_names.h" | 18 #include "extensions/browser/pref_names.h" |
18 #include "extensions/common/manifest.h" | 19 #include "extensions/common/manifest.h" |
19 #include "extensions/common/manifest_constants.h" | 20 #include "extensions/common/manifest_constants.h" |
| 21 #include "extensions/common/permissions/api_permission.h" |
| 22 #include "extensions/common/permissions/permissions_info.h" |
20 #include "extensions/common/url_pattern.h" | 23 #include "extensions/common/url_pattern.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "url/gurl.h" | 25 #include "url/gurl.h" |
23 | 26 |
24 namespace extensions { | 27 namespace extensions { |
25 | 28 |
26 namespace { | 29 namespace { |
27 | 30 |
28 const char kTargetExtension[] = "abcdefghijklmnopabcdefghijklmnop"; | 31 const char kTargetExtension[] = "abcdefghijklmnopabcdefghijklmnop"; |
29 const char kTargetExtension2[] = "bcdefghijklmnopabcdefghijklmnopa"; | 32 const char kTargetExtension2[] = "bcdefghijklmnopabcdefghijklmnopa"; |
30 const char kTargetExtension3[] = "cdefghijklmnopabcdefghijklmnopab"; | 33 const char kTargetExtension3[] = "cdefghijklmnopabcdefghijklmnopab"; |
31 const char kTargetExtension4[] = "defghijklmnopabcdefghijklmnopabc"; | 34 const char kTargetExtension4[] = "defghijklmnopabcdefghijklmnopabc"; |
32 const char kOtherExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; | 35 const char kOtherExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
33 const char kExampleUpdateUrl[] = "http://example.com/update_url"; | 36 const char kExampleUpdateUrl[] = "http://example.com/update_url"; |
34 | 37 |
35 const char kExampleDictPreference[] = | 38 const char kExampleDictPreference[] = |
36 "{" | 39 "{" |
37 " \"abcdefghijklmnopabcdefghijklmnop\": {" // kTargetExtension | 40 " \"abcdefghijklmnopabcdefghijklmnop\": {" // kTargetExtension |
38 " \"installation_mode\": \"allowed\"," | 41 " \"installation_mode\": \"allowed\"," |
| 42 " \"blocked_permissions\": [\"fileSystem\", \"bookmarks\"]," |
39 " }," | 43 " }," |
40 " \"bcdefghijklmnopabcdefghijklmnopa\": {" // kTargetExtension2 | 44 " \"bcdefghijklmnopabcdefghijklmnopa\": {" // kTargetExtension2 |
41 " \"installation_mode\": \"force_installed\"," | 45 " \"installation_mode\": \"force_installed\"," |
42 " \"update_url\": \"http://example.com/update_url\"," | 46 " \"update_url\": \"http://example.com/update_url\"," |
| 47 " \"allowed_permissions\": [\"fileSystem\", \"bookmarks\"]," |
43 " }," | 48 " }," |
44 " \"cdefghijklmnopabcdefghijklmnopab\": {" // kTargetExtension3 | 49 " \"cdefghijklmnopabcdefghijklmnopab\": {" // kTargetExtension3 |
45 " \"installation_mode\": \"normal_installed\"," | 50 " \"installation_mode\": \"normal_installed\"," |
46 " \"update_url\": \"http://example.com/update_url\"," | 51 " \"update_url\": \"http://example.com/update_url\"," |
| 52 " \"allowed_permissions\": [\"fileSystem\", \"downloads\"]," |
| 53 " \"blocked_permissions\": [\"fileSystem\", \"history\"]," |
47 " }," | 54 " }," |
48 " \"defghijklmnopabcdefghijklmnopabc\": {" // kTargetExtension4 | 55 " \"defghijklmnopabcdefghijklmnopabc\": {" // kTargetExtension4 |
49 " \"installation_mode\": \"blocked\"," | 56 " \"installation_mode\": \"blocked\"," |
50 " }," | 57 " }," |
51 " \"*\": {" | 58 " \"*\": {" |
52 " \"installation_mode\": \"blocked\"," | 59 " \"installation_mode\": \"blocked\"," |
53 " \"install_sources\": [\"*://foo.com/*\"]," | 60 " \"install_sources\": [\"*://foo.com/*\"]," |
54 " \"allowed_types\": [\"theme\", \"user_script\"]," | 61 " \"allowed_types\": [\"theme\", \"user_script\"]," |
| 62 " \"blocked_permissions\": [\"fileSystem\", \"downloads\"]," |
55 " }," | 63 " }," |
56 "}"; | 64 "}"; |
57 | 65 |
58 } // namespace | 66 } // namespace |
59 | 67 |
60 class ExtensionManagementServiceTest : public testing::Test { | 68 class ExtensionManagementServiceTest : public testing::Test { |
61 public: | 69 public: |
62 typedef ExtensionManagementPrefUpdater<TestingPrefServiceSimple> PrefUpdater; | 70 typedef ExtensionManagementPrefUpdater<TestingPrefServiceSimple> PrefUpdater; |
63 | 71 |
64 ExtensionManagementServiceTest() {} | 72 ExtensionManagementServiceTest() {} |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 protected: | 129 protected: |
122 scoped_ptr<TestingPrefServiceSimple> pref_service_; | 130 scoped_ptr<TestingPrefServiceSimple> pref_service_; |
123 scoped_ptr<ExtensionManagement> extension_management_; | 131 scoped_ptr<ExtensionManagement> extension_management_; |
124 }; | 132 }; |
125 | 133 |
126 class ExtensionAdminPolicyTest : public ExtensionManagementServiceTest { | 134 class ExtensionAdminPolicyTest : public ExtensionManagementServiceTest { |
127 public: | 135 public: |
128 ExtensionAdminPolicyTest() {} | 136 ExtensionAdminPolicyTest() {} |
129 virtual ~ExtensionAdminPolicyTest() {} | 137 virtual ~ExtensionAdminPolicyTest() {} |
130 | 138 |
| 139 void SetUpPolicyProvider() { |
| 140 provider_.reset( |
| 141 new StandardManagementPolicyProvider(extension_management_.get())); |
| 142 } |
| 143 |
131 void CreateExtension(Manifest::Location location) { | 144 void CreateExtension(Manifest::Location location) { |
132 base::DictionaryValue values; | 145 base::DictionaryValue values; |
133 CreateExtensionFromValues(location, &values); | 146 CreateExtensionFromValues(location, &values); |
134 } | 147 } |
135 | 148 |
136 void CreateHostedApp(Manifest::Location location) { | 149 void CreateHostedApp(Manifest::Location location) { |
137 base::DictionaryValue values; | 150 base::DictionaryValue values; |
138 values.Set(extensions::manifest_keys::kWebURLs, new base::ListValue()); | 151 values.Set(extensions::manifest_keys::kWebURLs, new base::ListValue()); |
139 values.SetString(extensions::manifest_keys::kLaunchWebURL, | 152 values.SetString(extensions::manifest_keys::kLaunchWebURL, |
140 "http://www.example.com"); | 153 "http://www.example.com"); |
(...skipping 15 matching lines...) Expand all Loading... |
156 bool UserMayLoad(const base::ListValue* blacklist, | 169 bool UserMayLoad(const base::ListValue* blacklist, |
157 const base::ListValue* whitelist, | 170 const base::ListValue* whitelist, |
158 const base::DictionaryValue* forcelist, | 171 const base::DictionaryValue* forcelist, |
159 const base::ListValue* allowed_types, | 172 const base::ListValue* allowed_types, |
160 const Extension* extension, | 173 const Extension* extension, |
161 base::string16* error); | 174 base::string16* error); |
162 bool UserMayModifySettings(const Extension* extension, base::string16* error); | 175 bool UserMayModifySettings(const Extension* extension, base::string16* error); |
163 bool MustRemainEnabled(const Extension* extension, base::string16* error); | 176 bool MustRemainEnabled(const Extension* extension, base::string16* error); |
164 | 177 |
165 protected: | 178 protected: |
| 179 scoped_ptr<StandardManagementPolicyProvider> provider_; |
166 scoped_refptr<Extension> extension_; | 180 scoped_refptr<Extension> extension_; |
167 }; | 181 }; |
168 | 182 |
169 bool ExtensionAdminPolicyTest::BlacklistedByDefault( | 183 bool ExtensionAdminPolicyTest::BlacklistedByDefault( |
170 const base::ListValue* blacklist) { | 184 const base::ListValue* blacklist) { |
171 InitPrefService(); | 185 SetUpPolicyProvider(); |
172 if (blacklist) | 186 if (blacklist) |
173 SetPref(true, pref_names::kInstallDenyList, blacklist->DeepCopy()); | 187 SetPref(true, pref_names::kInstallDenyList, blacklist->DeepCopy()); |
174 return extension_management_->BlacklistedByDefault(); | 188 return extension_management_->BlacklistedByDefault(); |
175 } | 189 } |
176 | 190 |
177 bool ExtensionAdminPolicyTest::UserMayLoad( | 191 bool ExtensionAdminPolicyTest::UserMayLoad( |
178 const base::ListValue* blacklist, | 192 const base::ListValue* blacklist, |
179 const base::ListValue* whitelist, | 193 const base::ListValue* whitelist, |
180 const base::DictionaryValue* forcelist, | 194 const base::DictionaryValue* forcelist, |
181 const base::ListValue* allowed_types, | 195 const base::ListValue* allowed_types, |
182 const Extension* extension, | 196 const Extension* extension, |
183 base::string16* error) { | 197 base::string16* error) { |
184 InitPrefService(); | 198 SetUpPolicyProvider(); |
185 if (blacklist) | 199 if (blacklist) |
186 SetPref(true, pref_names::kInstallDenyList, blacklist->DeepCopy()); | 200 SetPref(true, pref_names::kInstallDenyList, blacklist->DeepCopy()); |
187 if (whitelist) | 201 if (whitelist) |
188 SetPref(true, pref_names::kInstallAllowList, whitelist->DeepCopy()); | 202 SetPref(true, pref_names::kInstallAllowList, whitelist->DeepCopy()); |
189 if (forcelist) | 203 if (forcelist) |
190 SetPref(true, pref_names::kInstallForceList, forcelist->DeepCopy()); | 204 SetPref(true, pref_names::kInstallForceList, forcelist->DeepCopy()); |
191 if (allowed_types) | 205 if (allowed_types) |
192 SetPref(true, pref_names::kAllowedTypes, allowed_types->DeepCopy()); | 206 SetPref(true, pref_names::kAllowedTypes, allowed_types->DeepCopy()); |
193 return extension_management_->GetProvider()->UserMayLoad(extension, error); | 207 return provider_->UserMayLoad(extension, error); |
194 } | 208 } |
195 | 209 |
196 bool ExtensionAdminPolicyTest::UserMayModifySettings(const Extension* extension, | 210 bool ExtensionAdminPolicyTest::UserMayModifySettings(const Extension* extension, |
197 base::string16* error) { | 211 base::string16* error) { |
198 InitPrefService(); | 212 SetUpPolicyProvider(); |
199 return extension_management_->GetProvider()->UserMayModifySettings(extension, | 213 return provider_->UserMayModifySettings(extension, error); |
200 error); | |
201 } | 214 } |
202 | 215 |
203 bool ExtensionAdminPolicyTest::MustRemainEnabled(const Extension* extension, | 216 bool ExtensionAdminPolicyTest::MustRemainEnabled(const Extension* extension, |
204 base::string16* error) { | 217 base::string16* error) { |
205 InitPrefService(); | 218 SetUpPolicyProvider(); |
206 return extension_management_->GetProvider()->MustRemainEnabled(extension, | 219 return provider_->MustRemainEnabled(extension, error); |
207 error); | |
208 } | 220 } |
209 | 221 |
210 // Verify that preference controlled by legacy ExtensionInstallSources policy is | 222 // Verify that preference controlled by legacy ExtensionInstallSources policy is |
211 // handled well. | 223 // handled well. |
212 TEST_F(ExtensionManagementServiceTest, LegacyInstallSources) { | 224 TEST_F(ExtensionManagementServiceTest, LegacyInstallSources) { |
213 base::ListValue allowed_sites_pref; | 225 base::ListValue allowed_sites_pref; |
214 allowed_sites_pref.AppendString("https://www.example.com/foo"); | 226 allowed_sites_pref.AppendString("https://www.example.com/foo"); |
215 allowed_sites_pref.AppendString("https://corp.mycompany.com/*"); | 227 allowed_sites_pref.AppendString("https://corp.mycompany.com/*"); |
216 SetPref( | 228 SetPref( |
217 true, pref_names::kAllowedInstallSites, allowed_sites_pref.DeepCopy()); | 229 true, pref_names::kAllowedInstallSites, allowed_sites_pref.DeepCopy()); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 EXPECT_TRUE(ReadGlobalSettings()->has_restricted_allowed_types); | 344 EXPECT_TRUE(ReadGlobalSettings()->has_restricted_allowed_types); |
333 const std::vector<Manifest::Type>& allowed_types = | 345 const std::vector<Manifest::Type>& allowed_types = |
334 ReadGlobalSettings()->allowed_types; | 346 ReadGlobalSettings()->allowed_types; |
335 EXPECT_EQ(allowed_types.size(), 2u); | 347 EXPECT_EQ(allowed_types.size(), 2u); |
336 EXPECT_TRUE(std::find(allowed_types.begin(), | 348 EXPECT_TRUE(std::find(allowed_types.begin(), |
337 allowed_types.end(), | 349 allowed_types.end(), |
338 Manifest::TYPE_THEME) != allowed_types.end()); | 350 Manifest::TYPE_THEME) != allowed_types.end()); |
339 EXPECT_TRUE(std::find(allowed_types.begin(), | 351 EXPECT_TRUE(std::find(allowed_types.begin(), |
340 allowed_types.end(), | 352 allowed_types.end(), |
341 Manifest::TYPE_USER_SCRIPT) != allowed_types.end()); | 353 Manifest::TYPE_USER_SCRIPT) != allowed_types.end()); |
| 354 |
| 355 // Verifies blocked permission list settings. |
| 356 APIPermissionSet api_permission_set; |
| 357 api_permission_set.clear(); |
| 358 api_permission_set.insert(APIPermission::kFileSystem); |
| 359 api_permission_set.insert(APIPermission::kDownloads); |
| 360 EXPECT_EQ(api_permission_set, |
| 361 extension_management_->GetBlockedAPIPermissions(kOtherExtension)); |
| 362 |
| 363 api_permission_set.clear(); |
| 364 api_permission_set.insert(APIPermission::kFileSystem); |
| 365 api_permission_set.insert(APIPermission::kDownloads); |
| 366 api_permission_set.insert(APIPermission::kBookmark); |
| 367 EXPECT_EQ(api_permission_set, |
| 368 extension_management_->GetBlockedAPIPermissions(kTargetExtension)); |
| 369 |
| 370 api_permission_set.clear(); |
| 371 api_permission_set.insert(APIPermission::kDownloads); |
| 372 EXPECT_EQ(api_permission_set, |
| 373 extension_management_->GetBlockedAPIPermissions(kTargetExtension2)); |
| 374 |
| 375 api_permission_set.clear(); |
| 376 api_permission_set.insert(APIPermission::kFileSystem); |
| 377 api_permission_set.insert(APIPermission::kHistory); |
| 378 EXPECT_EQ(api_permission_set, |
| 379 extension_management_->GetBlockedAPIPermissions(kTargetExtension3)); |
342 } | 380 } |
343 | 381 |
344 // Tests functionality of new preference as to deprecate legacy | 382 // Tests functionality of new preference as to deprecate legacy |
345 // ExtensionInstallSources policy. | 383 // ExtensionInstallSources policy. |
346 TEST_F(ExtensionManagementServiceTest, NewInstallSources) { | 384 TEST_F(ExtensionManagementServiceTest, NewInstallSources) { |
347 // Set the legacy preference, and verifies that it works. | 385 // Set the legacy preference, and verifies that it works. |
348 base::ListValue allowed_sites_pref; | 386 base::ListValue allowed_sites_pref; |
349 allowed_sites_pref.AppendString("https://www.example.com/foo"); | 387 allowed_sites_pref.AppendString("https://www.example.com/foo"); |
350 SetPref( | 388 SetPref( |
351 true, pref_names::kAllowedInstallSites, allowed_sites_pref.DeepCopy()); | 389 true, pref_names::kAllowedInstallSites, allowed_sites_pref.DeepCopy()); |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 EXPECT_FALSE(error.empty()); | 716 EXPECT_FALSE(error.empty()); |
679 | 717 |
680 CreateExtension(Manifest::INTERNAL); | 718 CreateExtension(Manifest::INTERNAL); |
681 error.clear(); | 719 error.clear(); |
682 EXPECT_FALSE(MustRemainEnabled(extension_.get(), NULL)); | 720 EXPECT_FALSE(MustRemainEnabled(extension_.get(), NULL)); |
683 EXPECT_FALSE(MustRemainEnabled(extension_.get(), &error)); | 721 EXPECT_FALSE(MustRemainEnabled(extension_.get(), &error)); |
684 EXPECT_TRUE(error.empty()); | 722 EXPECT_TRUE(error.empty()); |
685 } | 723 } |
686 | 724 |
687 } // namespace extensions | 725 } // namespace extensions |
OLD | NEW |