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

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

Powered by Google App Engine
This is Rietveld 408576698