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

Side by Side Diff: chrome/browser/extensions/extension_management_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698