| 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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 scoped_ptr<base::Value> parsed(base::JSONReader::ReadAndReturnError( | 152 scoped_ptr<base::Value> parsed(base::JSONReader::ReadAndReturnError( |
| 152 kExampleDictPreference, | 153 kExampleDictPreference, |
| 153 base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS, | 154 base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS, |
| 154 NULL, | 155 NULL, |
| 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 |
| 162 // Wrapper of ExtensionManagement::GetInstallationMode, |id| and |
| 163 // |update_url| are used to construct an Extension for testing. |
| 161 ExtensionManagement::InstallationMode GetInstallationMode( | 164 ExtensionManagement::InstallationMode GetInstallationMode( |
| 162 const std::string& id, | 165 const std::string& id, |
| 163 const std::string& update_url) { | 166 const std::string& update_url) { |
| 164 scoped_refptr<const Extension> extension = | 167 scoped_refptr<const Extension> extension = |
| 165 CreateExtensionWithIdAndUpdateUrl(Manifest::UNPACKED, id, update_url); | 168 CreateExtension(Manifest::UNPACKED, "0.1", id, update_url); |
| 166 return extension_management_->GetInstallationMode(extension.get()); | 169 return extension_management_->GetInstallationMode(extension.get()); |
| 167 } | 170 } |
| 168 | 171 |
| 172 // Wrapper of ExtensionManagement::GetBlockedAPIPermissions, |id| and |
| 173 // |update_url| are used to construct an Extension for testing. |
| 169 APIPermissionSet GetBlockedAPIPermissions(const std::string& id, | 174 APIPermissionSet GetBlockedAPIPermissions(const std::string& id, |
| 170 const std::string& update_url) { | 175 const std::string& update_url) { |
| 171 scoped_refptr<const Extension> extension = | 176 scoped_refptr<const Extension> extension = |
| 172 CreateExtensionWithIdAndUpdateUrl(Manifest::UNPACKED, id, update_url); | 177 CreateExtension(Manifest::UNPACKED, "0.1", id, update_url); |
| 173 return extension_management_->GetBlockedAPIPermissions(extension.get()); | 178 return extension_management_->GetBlockedAPIPermissions(extension.get()); |
| 174 } | 179 } |
| 175 | 180 |
| 181 // Wrapper of ExtensionManagement::CheckMinimumVersionRequirement, |id| and |
| 182 // |version| are used to construct an Extension for testing. |
| 183 bool CheckMinimumVersionRequirement(const std::string& id, |
| 184 const std::string& version) { |
| 185 scoped_refptr<const Extension> extension = |
| 186 CreateExtension(Manifest::UNPACKED, version, id, kNonExistingUpdateUrl); |
| 187 std::string minimum_version_required; |
| 188 bool ret = extension_management_->CheckMinimumVersionRequirement( |
| 189 extension.get(), &minimum_version_required); |
| 190 EXPECT_EQ(ret, minimum_version_required.empty()); |
| 191 EXPECT_EQ(ret, extension_management_->CheckMinimumVersionRequirement( |
| 192 extension.get(), nullptr)); |
| 193 return ret; |
| 194 } |
| 195 |
| 176 protected: | 196 protected: |
| 177 scoped_ptr<TestingPrefServiceSimple> pref_service_; | 197 scoped_ptr<TestingPrefServiceSimple> pref_service_; |
| 178 scoped_ptr<ExtensionManagement> extension_management_; | 198 scoped_ptr<ExtensionManagement> extension_management_; |
| 179 | 199 |
| 180 private: | 200 private: |
| 181 // Create an extension with specified |location|, |id| and |update_url|. | 201 // Create an extension with specified |location|, |version|, |id| and |
| 182 scoped_refptr<const Extension> CreateExtensionWithIdAndUpdateUrl( | 202 // |update_url|. |
| 203 scoped_refptr<const Extension> CreateExtension( |
| 183 Manifest::Location location, | 204 Manifest::Location location, |
| 205 const std::string& version, |
| 184 const std::string& id, | 206 const std::string& id, |
| 185 const std::string& update_url) { | 207 const std::string& update_url) { |
| 186 base::DictionaryValue manifest_dict; | 208 base::DictionaryValue manifest_dict; |
| 187 manifest_dict.SetString(manifest_keys::kName, "test"); | 209 manifest_dict.SetString(manifest_keys::kName, "test"); |
| 188 manifest_dict.SetString(manifest_keys::kVersion, "0.1"); | 210 manifest_dict.SetString(manifest_keys::kVersion, version); |
| 189 manifest_dict.SetString(manifest_keys::kUpdateURL, update_url); | 211 manifest_dict.SetString(manifest_keys::kUpdateURL, update_url); |
| 190 std::string error; | 212 std::string error; |
| 191 scoped_refptr<const Extension> extension = | 213 scoped_refptr<const Extension> extension = |
| 192 Extension::Create(base::FilePath(), location, manifest_dict, | 214 Extension::Create(base::FilePath(), location, manifest_dict, |
| 193 Extension::NO_FLAGS, id, &error); | 215 Extension::NO_FLAGS, id, &error); |
| 194 CHECK(extension.get()) << error; | 216 CHECK(extension.get()) << error; |
| 195 return extension; | 217 return extension; |
| 196 } | 218 } |
| 197 }; | 219 }; |
| 198 | 220 |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 api_permission_set.insert(APIPermission::kFileSystem); | 464 api_permission_set.insert(APIPermission::kFileSystem); |
| 443 api_permission_set.insert(APIPermission::kHistory); | 465 api_permission_set.insert(APIPermission::kHistory); |
| 444 EXPECT_EQ(api_permission_set, | 466 EXPECT_EQ(api_permission_set, |
| 445 GetBlockedAPIPermissionsById(kTargetExtension3)); | 467 GetBlockedAPIPermissionsById(kTargetExtension3)); |
| 446 | 468 |
| 447 api_permission_set.clear(); | 469 api_permission_set.clear(); |
| 448 api_permission_set.insert(APIPermission::kFileSystem); | 470 api_permission_set.insert(APIPermission::kFileSystem); |
| 449 api_permission_set.insert(APIPermission::kBookmark); | 471 api_permission_set.insert(APIPermission::kBookmark); |
| 450 EXPECT_EQ(api_permission_set, | 472 EXPECT_EQ(api_permission_set, |
| 451 GetBlockedAPIPermissionsByUpdateUrl(kExampleUpdateUrl)); | 473 GetBlockedAPIPermissionsByUpdateUrl(kExampleUpdateUrl)); |
| 474 |
| 475 // Verifies minimum version settings. |
| 476 EXPECT_FALSE(CheckMinimumVersionRequirement(kTargetExtension, "1.0.99")); |
| 477 EXPECT_TRUE(CheckMinimumVersionRequirement(kTargetExtension, "1.1")); |
| 478 EXPECT_TRUE(CheckMinimumVersionRequirement(kTargetExtension, "1.1.0.1")); |
| 452 } | 479 } |
| 453 | 480 |
| 454 // Tests the handling of installation mode in case it's specified in both | 481 // Tests the handling of installation mode in case it's specified in both |
| 455 // per-extension and per-update-url settings. | 482 // per-extension and per-update-url settings. |
| 456 TEST_F(ExtensionManagementServiceTest, InstallationModeConflictHandling) { | 483 TEST_F(ExtensionManagementServiceTest, InstallationModeConflictHandling) { |
| 457 SetExampleDictPref(); | 484 SetExampleDictPref(); |
| 458 | 485 |
| 459 // Per-extension installation mode settings should always override | 486 // Per-extension installation mode settings should always override |
| 460 // per-update-url settings. | 487 // per-update-url settings. |
| 461 EXPECT_EQ(GetInstallationMode(kTargetExtension, kExampleUpdateUrl), | 488 EXPECT_EQ(GetInstallationMode(kTargetExtension, kExampleUpdateUrl), |
| (...skipping 28 matching lines...) Expand all Loading... |
| 490 EXPECT_EQ(api_permission_set, | 517 EXPECT_EQ(api_permission_set, |
| 491 GetBlockedAPIPermissions(kTargetExtension2, kExampleUpdateUrl)); | 518 GetBlockedAPIPermissions(kTargetExtension2, kExampleUpdateUrl)); |
| 492 | 519 |
| 493 api_permission_set = blocked_permissions_for_update_url; | 520 api_permission_set = blocked_permissions_for_update_url; |
| 494 api_permission_set.insert(APIPermission::kFileSystem); | 521 api_permission_set.insert(APIPermission::kFileSystem); |
| 495 api_permission_set.insert(APIPermission::kHistory); | 522 api_permission_set.insert(APIPermission::kHistory); |
| 496 EXPECT_EQ(api_permission_set, | 523 EXPECT_EQ(api_permission_set, |
| 497 GetBlockedAPIPermissions(kTargetExtension3, kExampleUpdateUrl)); | 524 GetBlockedAPIPermissions(kTargetExtension3, kExampleUpdateUrl)); |
| 498 } | 525 } |
| 499 | 526 |
| 527 // Tests the 'minimum_version_required' settings of extension management. |
| 528 TEST_F(ExtensionManagementServiceTest, kMinimumVersionRequired) { |
| 529 EXPECT_TRUE(CheckMinimumVersionRequirement(kTargetExtension, "0.0")); |
| 530 EXPECT_TRUE(CheckMinimumVersionRequirement(kTargetExtension, "3.0.0")); |
| 531 EXPECT_TRUE(CheckMinimumVersionRequirement(kTargetExtension, "9999.0")); |
| 532 |
| 533 { |
| 534 PrefUpdater pref(pref_service_.get()); |
| 535 pref.SetMinimumVersionRequired(kTargetExtension, "3.0"); |
| 536 } |
| 537 |
| 538 EXPECT_FALSE(CheckMinimumVersionRequirement(kTargetExtension, "0.0")); |
| 539 EXPECT_FALSE(CheckMinimumVersionRequirement(kTargetExtension, "2.99")); |
| 540 EXPECT_TRUE(CheckMinimumVersionRequirement(kTargetExtension, "3.0.0")); |
| 541 EXPECT_TRUE(CheckMinimumVersionRequirement(kTargetExtension, "3.0.1")); |
| 542 EXPECT_TRUE(CheckMinimumVersionRequirement(kTargetExtension, "4.0")); |
| 543 } |
| 544 |
| 500 // Tests functionality of new preference as to deprecate legacy | 545 // Tests functionality of new preference as to deprecate legacy |
| 501 // ExtensionInstallSources policy. | 546 // ExtensionInstallSources policy. |
| 502 TEST_F(ExtensionManagementServiceTest, NewInstallSources) { | 547 TEST_F(ExtensionManagementServiceTest, NewInstallSources) { |
| 503 // Set the legacy preference, and verifies that it works. | 548 // Set the legacy preference, and verifies that it works. |
| 504 base::ListValue allowed_sites_pref; | 549 base::ListValue allowed_sites_pref; |
| 505 allowed_sites_pref.AppendString("https://www.example.com/foo"); | 550 allowed_sites_pref.AppendString("https://www.example.com/foo"); |
| 506 SetPref( | 551 SetPref( |
| 507 true, pref_names::kAllowedInstallSites, allowed_sites_pref.DeepCopy()); | 552 true, pref_names::kAllowedInstallSites, allowed_sites_pref.DeepCopy()); |
| 508 EXPECT_TRUE(ReadGlobalSettings()->has_restricted_install_sources); | 553 EXPECT_TRUE(ReadGlobalSettings()->has_restricted_install_sources); |
| 509 EXPECT_TRUE(ReadGlobalSettings()->install_sources.MatchesURL( | 554 EXPECT_TRUE(ReadGlobalSettings()->install_sources.MatchesURL( |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 831 EXPECT_FALSE(error.empty()); | 876 EXPECT_FALSE(error.empty()); |
| 832 | 877 |
| 833 CreateExtension(Manifest::INTERNAL); | 878 CreateExtension(Manifest::INTERNAL); |
| 834 error.clear(); | 879 error.clear(); |
| 835 EXPECT_FALSE(MustRemainEnabled(extension_.get(), NULL)); | 880 EXPECT_FALSE(MustRemainEnabled(extension_.get(), NULL)); |
| 836 EXPECT_FALSE(MustRemainEnabled(extension_.get(), &error)); | 881 EXPECT_FALSE(MustRemainEnabled(extension_.get(), &error)); |
| 837 EXPECT_TRUE(error.empty()); | 882 EXPECT_TRUE(error.empty()); |
| 838 } | 883 } |
| 839 | 884 |
| 840 } // namespace extensions | 885 } // namespace extensions |
| OLD | NEW |