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

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

Issue 436903002: Add tests for Extension installation/update for supervised users. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review comments Created 6 years, 4 months 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 <set> 6 #include <set>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/at_exit.h" 9 #include "base/at_exit.h"
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 #include "chrome/browser/extensions/fake_safe_browsing_database_manager.h" 52 #include "chrome/browser/extensions/fake_safe_browsing_database_manager.h"
53 #include "chrome/browser/extensions/installed_loader.h" 53 #include "chrome/browser/extensions/installed_loader.h"
54 #include "chrome/browser/extensions/pack_extension_job.h" 54 #include "chrome/browser/extensions/pack_extension_job.h"
55 #include "chrome/browser/extensions/pending_extension_info.h" 55 #include "chrome/browser/extensions/pending_extension_info.h"
56 #include "chrome/browser/extensions/pending_extension_manager.h" 56 #include "chrome/browser/extensions/pending_extension_manager.h"
57 #include "chrome/browser/extensions/test_blacklist.h" 57 #include "chrome/browser/extensions/test_blacklist.h"
58 #include "chrome/browser/extensions/test_extension_system.h" 58 #include "chrome/browser/extensions/test_extension_system.h"
59 #include "chrome/browser/extensions/unpacked_installer.h" 59 #include "chrome/browser/extensions/unpacked_installer.h"
60 #include "chrome/browser/extensions/updater/extension_updater.h" 60 #include "chrome/browser/extensions/updater/extension_updater.h"
61 #include "chrome/browser/prefs/pref_service_syncable.h" 61 #include "chrome/browser/prefs/pref_service_syncable.h"
62 #include "chrome/browser/supervised_user/supervised_user_service.h"
63 #include "chrome/browser/supervised_user/supervised_user_service_factory.h"
62 #include "chrome/browser/sync/profile_sync_service.h" 64 #include "chrome/browser/sync/profile_sync_service.h"
63 #include "chrome/browser/sync/profile_sync_service_factory.h" 65 #include "chrome/browser/sync/profile_sync_service_factory.h"
64 #include "chrome/common/chrome_constants.h" 66 #include "chrome/common/chrome_constants.h"
65 #include "chrome/common/chrome_switches.h" 67 #include "chrome/common/chrome_switches.h"
66 #include "chrome/common/extensions/api/plugins/plugins_handler.h" 68 #include "chrome/common/extensions/api/plugins/plugins_handler.h"
67 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" 69 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
68 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h" 70 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h"
69 #include "chrome/common/extensions/manifest_url_handler.h" 71 #include "chrome/common/extensions/manifest_url_handler.h"
70 #include "chrome/common/pref_names.h" 72 #include "chrome/common/pref_names.h"
71 #include "chrome/common/url_constants.h" 73 #include "chrome/common/url_constants.h"
(...skipping 665 matching lines...) Expand 10 before | Expand all | Expand 10 after
737 loaded_.clear(); 739 loaded_.clear();
738 ExtensionErrorReporter::GetInstance()->ClearErrors(); 740 ExtensionErrorReporter::GetInstance()->ClearErrors();
739 return extension; 741 return extension;
740 } 742 }
741 743
742 enum UpdateState { 744 enum UpdateState {
743 FAILED_SILENTLY, 745 FAILED_SILENTLY,
744 FAILED, 746 FAILED,
745 UPDATED, 747 UPDATED,
746 INSTALLED, 748 INSTALLED,
749 DISABLED,
747 ENABLED 750 ENABLED
748 }; 751 };
749 752
750 void BlackListWebGL() { 753 void BlackListWebGL() {
751 static const std::string json_blacklist = 754 static const std::string json_blacklist =
752 "{\n" 755 "{\n"
753 " \"name\": \"gpu blacklist\",\n" 756 " \"name\": \"gpu blacklist\",\n"
754 " \"version\": \"1.0\",\n" 757 " \"version\": \"1.0\",\n"
755 " \"entries\": [\n" 758 " \"entries\": [\n"
756 " {\n" 759 " {\n"
(...skipping 10 matching lines...) Expand all
767 // Helper method to set up a WindowedNotificationObserver to wait for a 770 // Helper method to set up a WindowedNotificationObserver to wait for a
768 // specific CrxInstaller to finish if we don't know the value of the 771 // specific CrxInstaller to finish if we don't know the value of the
769 // |installer| yet. 772 // |installer| yet.
770 static bool IsCrxInstallerDone(extensions::CrxInstaller** installer, 773 static bool IsCrxInstallerDone(extensions::CrxInstaller** installer,
771 const content::NotificationSource& source, 774 const content::NotificationSource& source,
772 const content::NotificationDetails& details) { 775 const content::NotificationDetails& details) {
773 return content::Source<extensions::CrxInstaller>(source).ptr() == 776 return content::Source<extensions::CrxInstaller>(source).ptr() ==
774 *installer; 777 *installer;
775 } 778 }
776 779
780 void PackCRXAndUpdateExtension(const std::string& id,
781 const base::FilePath& dir_path,
782 const base::FilePath& pem_path,
783 UpdateState expected_state) {
784 base::ScopedTempDir temp_dir;
785 EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
786 base::FilePath crx_path = temp_dir.path().AppendASCII("temp.crx");
787
788 PackCRX(dir_path, pem_path, crx_path);
789 UpdateExtension(id, crx_path, expected_state);
790 }
791
777 void UpdateExtension(const std::string& id, 792 void UpdateExtension(const std::string& id,
778 const base::FilePath& in_path, 793 const base::FilePath& in_path,
779 UpdateState expected_state) { 794 UpdateState expected_state) {
780 ASSERT_TRUE(base::PathExists(in_path)); 795 ASSERT_TRUE(base::PathExists(in_path));
781 796
782 // We need to copy this to a temporary location because Update() will delete 797 // We need to copy this to a temporary location because Update() will delete
783 // it. 798 // it.
784 base::FilePath path = temp_dir().path(); 799 base::FilePath path = temp_dir().path();
785 path = path.Append(in_path.BaseName()); 800 path = path.Append(in_path.BaseName());
786 ASSERT_TRUE(base::CopyFile(in_path, path)); 801 ASSERT_TRUE(base::CopyFile(in_path, path));
(...skipping 5310 matching lines...) Expand 10 before | Expand all | Expand 10 after
6097 const extensions::PendingExtensionInfo* info; 6112 const extensions::PendingExtensionInfo* info;
6098 EXPECT_TRUE( 6113 EXPECT_TRUE(
6099 (info = service()->pending_extension_manager()->GetById(good_crx))); 6114 (info = service()->pending_extension_manager()->GetById(good_crx)));
6100 EXPECT_EQ(ext_specifics->update_url(), info->update_url().spec()); 6115 EXPECT_EQ(ext_specifics->update_url(), info->update_url().spec());
6101 EXPECT_TRUE(info->is_from_sync()); 6116 EXPECT_TRUE(info->is_from_sync());
6102 EXPECT_TRUE(info->install_silently()); 6117 EXPECT_TRUE(info->install_silently());
6103 EXPECT_EQ(Manifest::INTERNAL, info->install_source()); 6118 EXPECT_EQ(Manifest::INTERNAL, info->install_source());
6104 // TODO(akalin): Figure out a way to test |info.ShouldAllowInstall()|. 6119 // TODO(akalin): Figure out a way to test |info.ShouldAllowInstall()|.
6105 } 6120 }
6106 6121
6107 TEST_F(ExtensionServiceTest, SyncUninstallForSupervisedUser) { 6122 TEST_F(ExtensionServiceTest, SupervisedUser_InstallOnlyAllowedByCustodian) {
6123 ExtensionServiceInitParams params = CreateDefaultInitParams();
6124 params.profile_is_supervised = true;
6125 InitializeExtensionService(params);
6126
6127 SupervisedUserService* supervised_user_service =
6128 SupervisedUserServiceFactory::GetForProfile(profile());
6129 GetManagementPolicy()->RegisterProvider(supervised_user_service);
6130
6131 base::FilePath path1 = data_dir().AppendASCII("good.crx");
6132 base::FilePath path2 = data_dir().AppendASCII("good2048.crx");
6133 const Extension* extensions[] = {
6134 InstallCRX(path1, INSTALL_FAILED),
6135 InstallCRX(path2, INSTALL_NEW, Extension::WAS_INSTALLED_BY_CUSTODIAN)
6136 };
6137
6138 // Only the extension with the "installed by custodian" flag should have been
6139 // installed and enabled.
6140 EXPECT_FALSE(extensions[0]);
6141 ASSERT_TRUE(extensions[1]);
6142 EXPECT_TRUE(registry()->enabled_extensions().Contains(extensions[1]->id()));
6143 }
6144
6145 TEST_F(ExtensionServiceTest, SupervisedUser_UpdateWithoutPermissionIncrease) {
6146 ExtensionServiceInitParams params = CreateDefaultInitParams();
6147 params.profile_is_supervised = true;
6148 InitializeExtensionService(params);
6149
6150 SupervisedUserService* supervised_user_service =
6151 SupervisedUserServiceFactory::GetForProfile(profile());
6152 GetManagementPolicy()->RegisterProvider(supervised_user_service);
6153
6154 base::FilePath base_path = data_dir().AppendASCII("autoupdate");
6155 base::FilePath pem_path = base_path.AppendASCII("key.pem");
6156
6157 base::FilePath path = base_path.AppendASCII("v1");
6158 const Extension* extension =
6159 PackAndInstallCRX(path, pem_path, INSTALL_NEW,
6160 Extension::WAS_INSTALLED_BY_CUSTODIAN);
6161 // The extension must now be installed and enabled.
6162 ASSERT_TRUE(extension);
6163 ASSERT_TRUE(registry()->enabled_extensions().Contains(extension->id()));
6164
6165 // Save the id, as the extension object will be destroyed during updating.
6166 std::string id = extension->id();
6167
6168 std::string old_version = extension->VersionString();
6169
6170 // Update to a new version.
6171 path = base_path.AppendASCII("v2");
6172 PackCRXAndUpdateExtension(id, path, pem_path, ENABLED);
6173
6174 // The extension should still be there and enabled.
6175 extension = registry()->enabled_extensions().GetByID(id);
6176 ASSERT_TRUE(extension);
6177 // The version should have changed.
6178 EXPECT_NE(extension->VersionString(), old_version);
6179 }
6180
6181 TEST_F(ExtensionServiceTest, SupervisedUser_UpdateWithPermissionIncrease) {
6182 ExtensionServiceInitParams params = CreateDefaultInitParams();
6183 params.profile_is_supervised = true;
6184 InitializeExtensionService(params);
6185
6186 SupervisedUserService* supervised_user_service =
6187 SupervisedUserServiceFactory::GetForProfile(profile());
6188 GetManagementPolicy()->RegisterProvider(supervised_user_service);
6189
6190 base::FilePath base_path = data_dir().AppendASCII("permissions_increase");
6191 base::FilePath pem_path = base_path.AppendASCII("permissions.pem");
6192
6193 base::FilePath path = base_path.AppendASCII("v1");
6194 const Extension* extension =
6195 PackAndInstallCRX(path, pem_path, INSTALL_NEW,
6196 Extension::WAS_INSTALLED_BY_CUSTODIAN);
6197 // The extension must now be installed and enabled.
6198 ASSERT_TRUE(extension);
6199 ASSERT_TRUE(registry()->enabled_extensions().Contains(extension->id()));
6200
6201 // Save the id, as the extension object will be destroyed during updating.
6202 std::string id = extension->id();
6203
6204 std::string old_version = extension->VersionString();
6205
6206 // Update to a new version with increased permissions.
6207 path = base_path.AppendASCII("v2");
6208 PackCRXAndUpdateExtension(id, path, pem_path, DISABLED);
6209
6210 // The extension should still be there, but disabled.
6211 EXPECT_FALSE(registry()->enabled_extensions().Contains(id));
6212 extension = registry()->disabled_extensions().GetByID(id);
6213 ASSERT_TRUE(extension);
6214 // The version should have changed.
6215 EXPECT_NE(extension->VersionString(), old_version);
6216 }
6217
6218 TEST_F(ExtensionServiceTest,
6219 SupervisedUser_SyncUninstallByCustodianSkipsPolicy) {
6108 InitializeEmptyExtensionService(); 6220 InitializeEmptyExtensionService();
6109 InitializeExtensionSyncService(); 6221 InitializeExtensionSyncService();
6110 extension_sync_service()->MergeDataAndStartSyncing( 6222 extension_sync_service()->MergeDataAndStartSyncing(
6111 syncer::EXTENSIONS, 6223 syncer::EXTENSIONS,
6112 syncer::SyncDataList(), 6224 syncer::SyncDataList(),
6113 scoped_ptr<syncer::SyncChangeProcessor>( 6225 scoped_ptr<syncer::SyncChangeProcessor>(
6114 new syncer::FakeSyncChangeProcessor), 6226 new syncer::FakeSyncChangeProcessor),
6115 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); 6227 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
6116 6228
6117 // Install two extensions. 6229 // Install two extensions.
6118 base::FilePath path1 = data_dir().AppendASCII("good.crx"); 6230 base::FilePath path1 = data_dir().AppendASCII("good.crx");
6119 base::FilePath path2 = data_dir().AppendASCII("good2048.crx"); 6231 base::FilePath path2 = data_dir().AppendASCII("good2048.crx");
6120 const Extension* extensions[2] = { 6232 const Extension* extensions[] = {
6121 InstallCRX(path1, INSTALL_NEW), 6233 InstallCRX(path1, INSTALL_NEW),
6122 InstallCRX(path2, INSTALL_NEW, Extension::WAS_INSTALLED_BY_CUSTODIAN) 6234 InstallCRX(path2, INSTALL_NEW, Extension::WAS_INSTALLED_BY_CUSTODIAN)
6123 }; 6235 };
6124 6236
6125 // Add a policy provider that will disallow any changes. 6237 // Add a policy provider that will disallow any changes.
6126 extensions::TestManagementPolicyProvider provider( 6238 extensions::TestManagementPolicyProvider provider(
6127 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS); 6239 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS);
6128 GetManagementPolicy()->RegisterProvider(&provider); 6240 GetManagementPolicy()->RegisterProvider(&provider);
6129 6241
6130 // Create a sync deletion for each extension. 6242 // Create a sync deletion for each extension.
6131 syncer::SyncChangeList change_list; 6243 syncer::SyncChangeList change_list;
6132 for (int i = 0; i < 2; i++) { 6244 for (size_t i = 0; i < arraysize(extensions); i++) {
6133 const std::string& id = extensions[i]->id(); 6245 const std::string& id = extensions[i]->id();
6134 sync_pb::EntitySpecifics specifics; 6246 sync_pb::EntitySpecifics specifics;
6135 sync_pb::ExtensionSpecifics* ext_specifics = specifics.mutable_extension(); 6247 sync_pb::ExtensionSpecifics* ext_specifics = specifics.mutable_extension();
6136 ext_specifics->set_id(id); 6248 ext_specifics->set_id(id);
6137 ext_specifics->set_version("1.0"); 6249 ext_specifics->set_version("1.0");
6138 ext_specifics->set_installed_by_custodian( 6250 ext_specifics->set_installed_by_custodian(
6139 extensions[i]->was_installed_by_custodian()); 6251 extensions[i]->was_installed_by_custodian());
6140 syncer::SyncData sync_data = 6252 syncer::SyncData sync_data =
6141 syncer::SyncData::CreateLocalData(id, "Name", specifics); 6253 syncer::SyncData::CreateLocalData(id, "Name", specifics);
6142 change_list.push_back(syncer::SyncChange(FROM_HERE, 6254 change_list.push_back(syncer::SyncChange(FROM_HERE,
6143 syncer::SyncChange::ACTION_DELETE, 6255 syncer::SyncChange::ACTION_DELETE,
6144 sync_data)); 6256 sync_data));
6145 } 6257 }
6146 6258
6147 // Save the extension ids, as uninstalling destroys the Extension instance. 6259 // Save the extension ids, as uninstalling destroys the Extension instance.
6148 std::string extension_ids[2] = { 6260 std::string extension_ids[] = {
6149 extensions[0]->id(), 6261 extensions[0]->id(),
6150 extensions[1]->id() 6262 extensions[1]->id()
6151 }; 6263 };
6152 6264
6153 // Now apply the uninstallations. 6265 // Now apply the uninstallations.
6154 extension_sync_service()->ProcessSyncChanges(FROM_HERE, change_list); 6266 extension_sync_service()->ProcessSyncChanges(FROM_HERE, change_list);
6155 6267
6156 // Uninstalling the extension without installed_by_custodian should have been 6268 // Uninstalling the extension without installed_by_custodian should have been
6157 // blocked by policy. 6269 // blocked by policy, so it should still be there.
6158 EXPECT_TRUE(service()->GetExtensionById(extension_ids[0], true)); 6270 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_ids[0]));
6159 6271
6160 // But installed_by_custodian should result in bypassing the policy check. 6272 // But installed_by_custodian should result in bypassing the policy check.
6161 EXPECT_FALSE(service()->GetExtensionById(extension_ids[1], true)); 6273 EXPECT_FALSE(
6274 registry()->GenerateInstalledExtensionsSet()->Contains(extension_ids[1]));
6162 } 6275 }
6163 6276
6164 TEST_F(ExtensionServiceTest, InstallPriorityExternalUpdateUrl) { 6277 TEST_F(ExtensionServiceTest, InstallPriorityExternalUpdateUrl) {
6165 InitializeEmptyExtensionService(); 6278 InitializeEmptyExtensionService();
6166 6279
6167 base::FilePath path = data_dir().AppendASCII("good.crx"); 6280 base::FilePath path = data_dir().AppendASCII("good.crx");
6168 InstallCRX(path, INSTALL_NEW); 6281 InstallCRX(path, INSTALL_NEW);
6169 ValidatePrefKeyCount(1u); 6282 ValidatePrefKeyCount(1u);
6170 ValidateIntegerPref(good_crx, "state", Extension::ENABLED); 6283 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
6171 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL); 6284 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
(...skipping 867 matching lines...) Expand 10 before | Expand all | Expand 10 after
7039 7152
7040 service()->Observe(chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED, 7153 service()->Observe(chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED,
7041 content::Source<Profile>(profile()), 7154 content::Source<Profile>(profile()),
7042 content::NotificationService::NoDetails()); 7155 content::NotificationService::NoDetails());
7043 EXPECT_EQ(UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN, unloaded_reason_); 7156 EXPECT_EQ(UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN, unloaded_reason_);
7044 EXPECT_EQ(0u, registry()->enabled_extensions().size()); 7157 EXPECT_EQ(0u, registry()->enabled_extensions().size());
7045 EXPECT_EQ(0u, registry()->disabled_extensions().size()); 7158 EXPECT_EQ(0u, registry()->disabled_extensions().size());
7046 EXPECT_EQ(0u, registry()->terminated_extensions().size()); 7159 EXPECT_EQ(0u, registry()->terminated_extensions().size());
7047 EXPECT_EQ(0u, registry()->blacklisted_extensions().size()); 7160 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
7048 } 7161 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698