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 <string> | 6 #include <string> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/json/json_parser.h" | 9 #include "base/json/json_parser.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 const char kExampleUpdateUrl[] = "http://example.com/update_url"; | 36 const char kExampleUpdateUrl[] = "http://example.com/update_url"; |
37 | 37 |
38 const char kNonExistingExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; | 38 const char kNonExistingExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
39 const char kNonExistingUpdateUrl[] = "http://example.net/update.xml"; | 39 const char kNonExistingUpdateUrl[] = "http://example.net/update.xml"; |
40 | 40 |
41 const char kExampleDictPreference[] = | 41 const char kExampleDictPreference[] = |
42 "{" | 42 "{" |
43 " \"abcdefghijklmnopabcdefghijklmnop\": {" // kTargetExtension | 43 " \"abcdefghijklmnopabcdefghijklmnop\": {" // kTargetExtension |
44 " \"installation_mode\": \"allowed\"," | 44 " \"installation_mode\": \"allowed\"," |
45 " \"blocked_permissions\": [\"fileSystem\", \"bookmarks\"]," | 45 " \"blocked_permissions\": [\"fileSystem\", \"bookmarks\"]," |
| 46 " \"minimum_version_required\": \"1.1.0\"," |
46 " }," | 47 " }," |
47 " \"bcdefghijklmnopabcdefghijklmnopa\": {" // kTargetExtension2 | 48 " \"bcdefghijklmnopabcdefghijklmnopa\": {" // kTargetExtension2 |
48 " \"installation_mode\": \"force_installed\"," | 49 " \"installation_mode\": \"force_installed\"," |
49 " \"update_url\": \"http://example.com/update_url\"," | 50 " \"update_url\": \"http://example.com/update_url\"," |
50 " \"allowed_permissions\": [\"fileSystem\", \"bookmarks\"]," | 51 " \"allowed_permissions\": [\"fileSystem\", \"bookmarks\"]," |
51 " }," | 52 " }," |
52 " \"cdefghijklmnopabcdefghijklmnopab\": {" // kTargetExtension3 | 53 " \"cdefghijklmnopabcdefghijklmnopab\": {" // kTargetExtension3 |
53 " \"installation_mode\": \"normal_installed\"," | 54 " \"installation_mode\": \"normal_installed\"," |
54 " \"update_url\": \"http://example.com/update_url\"," | 55 " \"update_url\": \"http://example.com/update_url\"," |
55 " \"allowed_permissions\": [\"fileSystem\", \"downloads\"]," | 56 " \"allowed_permissions\": [\"fileSystem\", \"downloads\"]," |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 &error_msg)); | 156 &error_msg)); |
156 ASSERT_TRUE(parsed && parsed->IsType(base::Value::TYPE_DICTIONARY)) | 157 ASSERT_TRUE(parsed && parsed->IsType(base::Value::TYPE_DICTIONARY)) |
157 << error_msg; | 158 << error_msg; |
158 SetPref(true, pref_names::kExtensionManagement, parsed.release()); | 159 SetPref(true, pref_names::kExtensionManagement, parsed.release()); |
159 } | 160 } |
160 | 161 |
161 ExtensionManagement::InstallationMode GetInstallationMode( | 162 ExtensionManagement::InstallationMode GetInstallationMode( |
162 const std::string& id, | 163 const std::string& id, |
163 const std::string& update_url) { | 164 const std::string& update_url) { |
164 scoped_refptr<const Extension> extension = | 165 scoped_refptr<const Extension> extension = |
165 CreateExtensionWithIdAndUpdateUrl(Manifest::UNPACKED, id, update_url); | 166 CreateExtension(Manifest::UNPACKED, "0.1", id, update_url); |
166 return extension_management_->GetInstallationMode(extension.get()); | 167 return extension_management_->GetInstallationMode(extension.get()); |
167 } | 168 } |
168 | 169 |
169 APIPermissionSet GetBlockedAPIPermissions(const std::string& id, | 170 APIPermissionSet GetBlockedAPIPermissions(const std::string& id, |
170 const std::string& update_url) { | 171 const std::string& update_url) { |
171 scoped_refptr<const Extension> extension = | 172 scoped_refptr<const Extension> extension = |
172 CreateExtensionWithIdAndUpdateUrl(Manifest::UNPACKED, id, update_url); | 173 CreateExtension(Manifest::UNPACKED, "0.1", id, update_url); |
173 return extension_management_->GetBlockedAPIPermissions(extension.get()); | 174 return extension_management_->GetBlockedAPIPermissions(extension.get()); |
174 } | 175 } |
175 | 176 |
| 177 bool CheckMinimumVersion(const std::string& id, const std::string& version) { |
| 178 scoped_refptr<const Extension> extension = |
| 179 CreateExtension(Manifest::UNPACKED, version, id, kNonExistingUpdateUrl); |
| 180 std::string minimum_version_required; |
| 181 bool ret = extension_management_->CheckMinimumVersion( |
| 182 extension.get(), &minimum_version_required); |
| 183 EXPECT_EQ(ret, minimum_version_required.empty()); |
| 184 EXPECT_EQ(ret, extension_management_->CheckMinimumVersion(extension.get(), |
| 185 nullptr)); |
| 186 return ret; |
| 187 } |
| 188 |
176 protected: | 189 protected: |
177 scoped_ptr<TestingPrefServiceSimple> pref_service_; | 190 scoped_ptr<TestingPrefServiceSimple> pref_service_; |
178 scoped_ptr<ExtensionManagement> extension_management_; | 191 scoped_ptr<ExtensionManagement> extension_management_; |
179 | 192 |
180 private: | 193 private: |
181 // Create an extension with specified |location|, |id| and |update_url|. | 194 // Create an extension with specified |location|, |version|, |id| and |
182 scoped_refptr<const Extension> CreateExtensionWithIdAndUpdateUrl( | 195 // |update_url|. |
| 196 scoped_refptr<const Extension> CreateExtension( |
183 Manifest::Location location, | 197 Manifest::Location location, |
| 198 const std::string& version, |
184 const std::string& id, | 199 const std::string& id, |
185 const std::string& update_url) { | 200 const std::string& update_url) { |
186 base::DictionaryValue manifest_dict; | 201 base::DictionaryValue manifest_dict; |
187 manifest_dict.SetString(manifest_keys::kName, "test"); | 202 manifest_dict.SetString(manifest_keys::kName, "test"); |
188 manifest_dict.SetString(manifest_keys::kVersion, "0.1"); | 203 manifest_dict.SetString(manifest_keys::kVersion, version); |
189 manifest_dict.SetString(manifest_keys::kUpdateURL, update_url); | 204 manifest_dict.SetString(manifest_keys::kUpdateURL, update_url); |
190 std::string error; | 205 std::string error; |
191 scoped_refptr<const Extension> extension = | 206 scoped_refptr<const Extension> extension = |
192 Extension::Create(base::FilePath(), location, manifest_dict, | 207 Extension::Create(base::FilePath(), location, manifest_dict, |
193 Extension::NO_FLAGS, id, &error); | 208 Extension::NO_FLAGS, id, &error); |
194 CHECK(extension.get()) << error; | 209 CHECK(extension.get()) << error; |
195 return extension; | 210 return extension; |
196 } | 211 } |
197 }; | 212 }; |
198 | 213 |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 api_permission_set.insert(APIPermission::kFileSystem); | 457 api_permission_set.insert(APIPermission::kFileSystem); |
443 api_permission_set.insert(APIPermission::kHistory); | 458 api_permission_set.insert(APIPermission::kHistory); |
444 EXPECT_EQ(api_permission_set, | 459 EXPECT_EQ(api_permission_set, |
445 GetBlockedAPIPermissionsById(kTargetExtension3)); | 460 GetBlockedAPIPermissionsById(kTargetExtension3)); |
446 | 461 |
447 api_permission_set.clear(); | 462 api_permission_set.clear(); |
448 api_permission_set.insert(APIPermission::kFileSystem); | 463 api_permission_set.insert(APIPermission::kFileSystem); |
449 api_permission_set.insert(APIPermission::kBookmark); | 464 api_permission_set.insert(APIPermission::kBookmark); |
450 EXPECT_EQ(api_permission_set, | 465 EXPECT_EQ(api_permission_set, |
451 GetBlockedAPIPermissionsByUpdateUrl(kExampleUpdateUrl)); | 466 GetBlockedAPIPermissionsByUpdateUrl(kExampleUpdateUrl)); |
| 467 |
| 468 // Verifies minimum version settings. |
| 469 EXPECT_FALSE(CheckMinimumVersion(kTargetExtension, "1.0.99")); |
| 470 EXPECT_TRUE(CheckMinimumVersion(kTargetExtension, "1.1")); |
| 471 EXPECT_TRUE(CheckMinimumVersion(kTargetExtension, "1.1.0.1")); |
452 } | 472 } |
453 | 473 |
454 // Tests the handling of installation mode in case it's specified in both | 474 // Tests the handling of installation mode in case it's specified in both |
455 // per-extension and per-update-url settings. | 475 // per-extension and per-update-url settings. |
456 TEST_F(ExtensionManagementServiceTest, InstallationModeConflictHandling) { | 476 TEST_F(ExtensionManagementServiceTest, InstallationModeConflictHandling) { |
457 SetExampleDictPref(); | 477 SetExampleDictPref(); |
458 | 478 |
459 // Per-extension installation mode settings should always override | 479 // Per-extension installation mode settings should always override |
460 // per-update-url settings. | 480 // per-update-url settings. |
461 EXPECT_EQ(GetInstallationMode(kTargetExtension, kExampleUpdateUrl), | 481 EXPECT_EQ(GetInstallationMode(kTargetExtension, kExampleUpdateUrl), |
(...skipping 28 matching lines...) Expand all Loading... |
490 EXPECT_EQ(api_permission_set, | 510 EXPECT_EQ(api_permission_set, |
491 GetBlockedAPIPermissions(kTargetExtension2, kExampleUpdateUrl)); | 511 GetBlockedAPIPermissions(kTargetExtension2, kExampleUpdateUrl)); |
492 | 512 |
493 api_permission_set = blocked_permissions_for_update_url; | 513 api_permission_set = blocked_permissions_for_update_url; |
494 api_permission_set.insert(APIPermission::kFileSystem); | 514 api_permission_set.insert(APIPermission::kFileSystem); |
495 api_permission_set.insert(APIPermission::kHistory); | 515 api_permission_set.insert(APIPermission::kHistory); |
496 EXPECT_EQ(api_permission_set, | 516 EXPECT_EQ(api_permission_set, |
497 GetBlockedAPIPermissions(kTargetExtension3, kExampleUpdateUrl)); | 517 GetBlockedAPIPermissions(kTargetExtension3, kExampleUpdateUrl)); |
498 } | 518 } |
499 | 519 |
| 520 // Tests the 'minimum_version_required' settings of extension management. |
| 521 TEST_F(ExtensionManagementServiceTest, kMinimumVersionRequired) { |
| 522 EXPECT_TRUE(CheckMinimumVersion(kTargetExtension, "0.0")); |
| 523 EXPECT_TRUE(CheckMinimumVersion(kTargetExtension, "3.0.0")); |
| 524 EXPECT_TRUE(CheckMinimumVersion(kTargetExtension, "9999.0")); |
| 525 |
| 526 { |
| 527 PrefUpdater pref(pref_service_.get()); |
| 528 pref.SetMinimumVersionRequired(kTargetExtension, "3.0"); |
| 529 } |
| 530 |
| 531 EXPECT_FALSE(CheckMinimumVersion(kTargetExtension, "0.0")); |
| 532 EXPECT_FALSE(CheckMinimumVersion(kTargetExtension, "2.99")); |
| 533 EXPECT_TRUE(CheckMinimumVersion(kTargetExtension, "3.0.0")); |
| 534 EXPECT_TRUE(CheckMinimumVersion(kTargetExtension, "3.0.1")); |
| 535 EXPECT_TRUE(CheckMinimumVersion(kTargetExtension, "4.0")); |
| 536 } |
| 537 |
500 // Tests functionality of new preference as to deprecate legacy | 538 // Tests functionality of new preference as to deprecate legacy |
501 // ExtensionInstallSources policy. | 539 // ExtensionInstallSources policy. |
502 TEST_F(ExtensionManagementServiceTest, NewInstallSources) { | 540 TEST_F(ExtensionManagementServiceTest, NewInstallSources) { |
503 // Set the legacy preference, and verifies that it works. | 541 // Set the legacy preference, and verifies that it works. |
504 base::ListValue allowed_sites_pref; | 542 base::ListValue allowed_sites_pref; |
505 allowed_sites_pref.AppendString("https://www.example.com/foo"); | 543 allowed_sites_pref.AppendString("https://www.example.com/foo"); |
506 SetPref( | 544 SetPref( |
507 true, pref_names::kAllowedInstallSites, allowed_sites_pref.DeepCopy()); | 545 true, pref_names::kAllowedInstallSites, allowed_sites_pref.DeepCopy()); |
508 EXPECT_TRUE(ReadGlobalSettings()->has_restricted_install_sources); | 546 EXPECT_TRUE(ReadGlobalSettings()->has_restricted_install_sources); |
509 EXPECT_TRUE(ReadGlobalSettings()->install_sources.MatchesURL( | 547 EXPECT_TRUE(ReadGlobalSettings()->install_sources.MatchesURL( |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
831 EXPECT_FALSE(error.empty()); | 869 EXPECT_FALSE(error.empty()); |
832 | 870 |
833 CreateExtension(Manifest::INTERNAL); | 871 CreateExtension(Manifest::INTERNAL); |
834 error.clear(); | 872 error.clear(); |
835 EXPECT_FALSE(MustRemainEnabled(extension_.get(), NULL)); | 873 EXPECT_FALSE(MustRemainEnabled(extension_.get(), NULL)); |
836 EXPECT_FALSE(MustRemainEnabled(extension_.get(), &error)); | 874 EXPECT_FALSE(MustRemainEnabled(extension_.get(), &error)); |
837 EXPECT_TRUE(error.empty()); | 875 EXPECT_TRUE(error.empty()); |
838 } | 876 } |
839 | 877 |
840 } // namespace extensions | 878 } // namespace extensions |
OLD | NEW |