Chromium Code Reviews

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

Issue 1232003: Added support for pending extensions to ExtensionsService and (Closed)
Patch Set: synced to head Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
« no previous file with comments | « chrome/browser/extensions/extensions_service.cc ('k') | 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) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 "chrome/browser/extensions/extensions_service_unittest.h" 5 #include "chrome/browser/extensions/extensions_service_unittest.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/json/json_reader.h" 13 #include "base/json/json_reader.h"
14 #include "base/message_loop.h" 14 #include "base/message_loop.h"
15 #include "base/path_service.h" 15 #include "base/path_service.h"
16 #include "base/scoped_ptr.h"
16 #include "base/string16.h" 17 #include "base/string16.h"
17 #include "base/string_util.h" 18 #include "base/string_util.h"
18 #include "base/task.h" 19 #include "base/task.h"
20 #include "base/version.h"
19 #include "chrome/browser/extensions/crx_installer.h" 21 #include "chrome/browser/extensions/crx_installer.h"
20 #include "chrome/browser/extensions/extension_creator.h" 22 #include "chrome/browser/extensions/extension_creator.h"
21 #include "chrome/browser/extensions/extensions_service.h" 23 #include "chrome/browser/extensions/extensions_service.h"
22 #include "chrome/browser/extensions/external_extension_provider.h" 24 #include "chrome/browser/extensions/external_extension_provider.h"
23 #include "chrome/browser/extensions/external_pref_extension_provider.h" 25 #include "chrome/browser/extensions/external_pref_extension_provider.h"
24 #include "chrome/common/extensions/extension.h" 26 #include "chrome/common/extensions/extension.h"
25 #include "chrome/common/extensions/extension_constants.h" 27 #include "chrome/common/extensions/extension_constants.h"
26 #include "chrome/common/extensions/url_pattern.h" 28 #include "chrome/common/extensions/url_pattern.h"
27 #include "chrome/common/chrome_paths.h" 29 #include "chrome/common/chrome_paths.h"
28 #include "chrome/common/chrome_switches.h" 30 #include "chrome/common/chrome_switches.h"
(...skipping 362 matching lines...)
391 EXPECT_EQ(0u, loaded_.size()) << path.value(); 393 EXPECT_EQ(0u, loaded_.size()) << path.value();
392 EXPECT_EQ(1u, errors.size()) << path.value(); 394 EXPECT_EQ(1u, errors.size()) << path.value();
393 } 395 }
394 396
395 installed_ = NULL; 397 installed_ = NULL;
396 loaded_.clear(); 398 loaded_.clear();
397 ExtensionErrorReporter::GetInstance()->ClearErrors(); 399 ExtensionErrorReporter::GetInstance()->ClearErrors();
398 } 400 }
399 401
400 void UpdateExtension(const std::string& id, const FilePath& in_path, 402 void UpdateExtension(const std::string& id, const FilePath& in_path,
401 bool should_succeed, bool expect_report_on_failure) { 403 bool should_succeed, bool should_install,
404 bool expect_report_on_failure) {
402 ASSERT_TRUE(file_util::PathExists(in_path)); 405 ASSERT_TRUE(file_util::PathExists(in_path));
403 406
404 // We need to copy this to a temporary location because Update() will delete 407 // We need to copy this to a temporary location because Update() will delete
405 // it. 408 // it.
406 FilePath path = temp_dir_.path(); 409 FilePath path = temp_dir_.path();
407 path = path.Append(in_path.BaseName()); 410 path = path.Append(in_path.BaseName());
408 ASSERT_TRUE(file_util::CopyFile(in_path, path)); 411 ASSERT_TRUE(file_util::CopyFile(in_path, path));
409 412
410 service_->UpdateExtension(id, path, GURL()); 413 service_->UpdateExtension(id, path, GURL());
411 loop_.RunAllPending(); 414 loop_.RunAllPending();
412 std::vector<std::string> errors = GetErrors(); 415 std::vector<std::string> errors = GetErrors();
413 416
414 if (should_succeed) { 417 if (should_succeed) {
415 EXPECT_EQ(0u, errors.size()) << path.value(); 418 EXPECT_EQ(0u, errors.size()) << path.value();
416 EXPECT_EQ(1u, service_->extensions()->size()); 419 EXPECT_EQ(should_install ? 1u : 0u, service_->extensions()->size());
417 } else { 420 } else {
418 if (expect_report_on_failure) { 421 if (expect_report_on_failure) {
419 EXPECT_EQ(1u, errors.size()) << path.value(); 422 EXPECT_EQ(1u, errors.size()) << path.value();
420 } 423 }
421 } 424 }
422 425
423 // Update() should delete the temporary input file. 426 // Update() should delete the temporary input file.
424 EXPECT_FALSE(file_util::PathExists(path)); 427 EXPECT_FALSE(file_util::PathExists(path));
425 } 428 }
426 429
(...skipping 614 matching lines...)
1041 extensions_path = extensions_path.AppendASCII("extensions"); 1044 extensions_path = extensions_path.AppendASCII("extensions");
1042 1045
1043 FilePath path = extensions_path.AppendASCII("good.crx"); 1046 FilePath path = extensions_path.AppendASCII("good.crx");
1044 1047
1045 InstallExtension(path, true); 1048 InstallExtension(path, true);
1046 Extension* good = service_->extensions()->at(0); 1049 Extension* good = service_->extensions()->at(0);
1047 ASSERT_EQ("1.0.0.0", good->VersionString()); 1050 ASSERT_EQ("1.0.0.0", good->VersionString());
1048 ASSERT_EQ(good_crx, good->id()); 1051 ASSERT_EQ(good_crx, good->id());
1049 1052
1050 path = extensions_path.AppendASCII("good2.crx"); 1053 path = extensions_path.AppendASCII("good2.crx");
1051 UpdateExtension(good_crx, path, true, true); 1054 UpdateExtension(good_crx, path, true, true, true);
1052 ASSERT_EQ("1.0.0.1", loaded_[0]->version()->GetString()); 1055 ASSERT_EQ("1.0.0.1", loaded_[0]->version()->GetString());
1053 } 1056 }
1054 1057
1055 // Test updating a not-already-installed extension - this should fail 1058 // Test updating a not-already-installed extension - this should fail
1056 TEST_F(ExtensionsServiceTest, UpdateNotInstalledExtension) { 1059 TEST_F(ExtensionsServiceTest, UpdateNotInstalledExtension) {
1057 InitializeEmptyExtensionsService(); 1060 InitializeEmptyExtensionsService();
1058 FilePath extensions_path; 1061 FilePath extensions_path;
1059 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); 1062 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
1060 extensions_path = extensions_path.AppendASCII("extensions"); 1063 extensions_path = extensions_path.AppendASCII("extensions");
1061 1064
1062 FilePath path = extensions_path.AppendASCII("good.crx"); 1065 FilePath path = extensions_path.AppendASCII("good.crx");
1063 service_->UpdateExtension(good_crx, path, GURL()); 1066 UpdateExtension(good_crx, path, true, false, true);
1064 loop_.RunAllPending(); 1067 loop_.RunAllPending();
1065 1068
1066 ASSERT_EQ(0u, service_->extensions()->size()); 1069 ASSERT_EQ(0u, service_->extensions()->size());
1067 ASSERT_FALSE(installed_); 1070 ASSERT_FALSE(installed_);
1068 ASSERT_EQ(0u, loaded_.size()); 1071 ASSERT_EQ(0u, loaded_.size());
1069 } 1072 }
1070 1073
1071 // Makes sure you can't downgrade an extension via UpdateExtension 1074 // Makes sure you can't downgrade an extension via UpdateExtension
1072 TEST_F(ExtensionsServiceTest, UpdateWillNotDowngrade) { 1075 TEST_F(ExtensionsServiceTest, UpdateWillNotDowngrade) {
1073 InitializeEmptyExtensionsService(); 1076 InitializeEmptyExtensionsService();
1074 FilePath extensions_path; 1077 FilePath extensions_path;
1075 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); 1078 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
1076 extensions_path = extensions_path.AppendASCII("extensions"); 1079 extensions_path = extensions_path.AppendASCII("extensions");
1077 1080
1078 FilePath path = extensions_path.AppendASCII("good2.crx"); 1081 FilePath path = extensions_path.AppendASCII("good2.crx");
1079 1082
1080 InstallExtension(path, true); 1083 InstallExtension(path, true);
1081 Extension* good = service_->extensions()->at(0); 1084 Extension* good = service_->extensions()->at(0);
1082 ASSERT_EQ("1.0.0.1", good->VersionString()); 1085 ASSERT_EQ("1.0.0.1", good->VersionString());
1083 ASSERT_EQ(good_crx, good->id()); 1086 ASSERT_EQ(good_crx, good->id());
1084 1087
1085 // Change path from good2.crx -> good.crx 1088 // Change path from good2.crx -> good.crx
1086 path = extensions_path.AppendASCII("good.crx"); 1089 path = extensions_path.AppendASCII("good.crx");
1087 UpdateExtension(good_crx, path, false, true); 1090 UpdateExtension(good_crx, path, false, false, true);
1088 ASSERT_EQ("1.0.0.1", service_->extensions()->at(0)->VersionString()); 1091 ASSERT_EQ("1.0.0.1", service_->extensions()->at(0)->VersionString());
1089 } 1092 }
1090 1093
1091 // Make sure calling update with an identical version does nothing 1094 // Make sure calling update with an identical version does nothing
1092 TEST_F(ExtensionsServiceTest, UpdateToSameVersionIsNoop) { 1095 TEST_F(ExtensionsServiceTest, UpdateToSameVersionIsNoop) {
1093 InitializeEmptyExtensionsService(); 1096 InitializeEmptyExtensionsService();
1094 FilePath extensions_path; 1097 FilePath extensions_path;
1095 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); 1098 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
1096 extensions_path = extensions_path.AppendASCII("extensions"); 1099 extensions_path = extensions_path.AppendASCII("extensions");
1097 1100
1098 FilePath path = extensions_path.AppendASCII("good.crx"); 1101 FilePath path = extensions_path.AppendASCII("good.crx");
1099 1102
1100 InstallExtension(path, true); 1103 InstallExtension(path, true);
1101 Extension* good = service_->extensions()->at(0); 1104 Extension* good = service_->extensions()->at(0);
1102 ASSERT_EQ(good_crx, good->id()); 1105 ASSERT_EQ(good_crx, good->id());
1103 UpdateExtension(good_crx, path, false, false); 1106 UpdateExtension(good_crx, path, false, false, false);
1107 }
1108
1109 // Test adding a pending extension.
1110 TEST_F(ExtensionsServiceTest, AddPendingExtension) {
1111 InitializeEmptyExtensionsService();
1112
1113 const std::string kFakeId("fake-id");
1114 const GURL kFakeUpdateURL("http:://fake.update/url");
1115 scoped_ptr<Version> fake_version(Version::GetVersionFromString("4.3.2.1"));
1116 ASSERT_TRUE(fake_version.get());
1117 const bool kFakeIsTheme(false);
1118 const bool kFakeInstallSilently(true);
1119
1120 service_->AddPendingExtension(kFakeId, kFakeUpdateURL,
1121 *fake_version, kFakeIsTheme,
1122 kFakeInstallSilently);
1123 PendingExtensionMap::const_iterator it =
1124 service_->pending_extensions().find(kFakeId);
1125 ASSERT_TRUE(it != service_->pending_extensions().end());
1126 EXPECT_EQ(kFakeUpdateURL, it->second.update_url);
1127 EXPECT_EQ(kFakeIsTheme, it->second.is_theme);
1128 EXPECT_EQ(kFakeInstallSilently, it->second.install_silently);
1129 EXPECT_TRUE(it->second.version.Equals(*fake_version));
1130 }
1131
1132 // Test adding a pending extension for one that is already installed.
1133 TEST_F(ExtensionsServiceTest, AddPendingExtensionAlreadyInstalled) {
1134 InitializeEmptyExtensionsService();
1135
1136 FilePath extensions_path;
1137 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
1138 extensions_path = extensions_path.AppendASCII("extensions");
1139 FilePath path = extensions_path.AppendASCII("good.crx");
1140 InstallExtension(path, true);
1141 Extension* good = service_->extensions()->at(0);
1142 const bool kInstallSilently(true);
1143
1144 service_->AddPendingExtension(good->id(), good->update_url(),
1145 *good->version(), good->IsTheme(),
1146 kInstallSilently);
1147 EXPECT_FALSE(ContainsKey(service_->pending_extensions(), good->id()));
1148 }
1149
1150 namespace {
1151 const char kGoodId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
1152 const char kGoodUpdateURL[] = "http://good.update/url";
1153 const bool kGoodIsTheme = false;
1154 const bool kGoodInstallSilently = true;
1155 const char kGoodVersion[] = "1.2.3.4";
1156 } // namespace
1157
1158 // Test updating a pending extension.
1159 TEST_F(ExtensionsServiceTest, UpdatePendingExtension) {
1160 InitializeEmptyExtensionsService();
1161 scoped_ptr<Version> good_version(
1162 Version::GetVersionFromString(kGoodVersion));
1163 ASSERT_TRUE(good_version.get());
1164 service_->AddPendingExtension(kGoodId, GURL(kGoodUpdateURL),
1165 *good_version, kGoodIsTheme,
1166 kGoodInstallSilently);
1167 EXPECT_TRUE(ContainsKey(service_->pending_extensions(), kGoodId));
1168
1169 FilePath extensions_path;
1170 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
1171 extensions_path = extensions_path.AppendASCII("extensions");
1172 FilePath path = extensions_path.AppendASCII("good.crx");
1173 UpdateExtension(kGoodId, path, true, true, false);
1174
1175 EXPECT_FALSE(ContainsKey(service_->pending_extensions(), kGoodId));
1176 }
1177
1178 // TODO(akalin): Test updating a pending extension non-silently once
1179 // we can mock out ExtensionInstallUI and inject our version into
1180 // UpdateExtension().
1181
1182 // Test updating a pending extension with wrong is_theme.
1183 TEST_F(ExtensionsServiceTest, UpdatePendingExtensionWrongIsTheme) {
1184 InitializeEmptyExtensionsService();
1185 scoped_ptr<Version> good_version(
1186 Version::GetVersionFromString(kGoodVersion));
1187 ASSERT_TRUE(good_version.get());
1188 // Add pending extension with a flipped is_theme.
1189 service_->AddPendingExtension(kGoodId, GURL(kGoodUpdateURL),
1190 *good_version, !kGoodIsTheme,
1191 kGoodInstallSilently);
1192 EXPECT_TRUE(ContainsKey(service_->pending_extensions(), kGoodId));
1193
1194 FilePath extensions_path;
1195 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
1196 extensions_path = extensions_path.AppendASCII("extensions");
1197 FilePath path = extensions_path.AppendASCII("good.crx");
1198 UpdateExtension(kGoodId, path, true, false, false);
1199
1200 // TODO(akalin): Figure out how to check that the extensions
1201 // directory is cleaned up properly in OnExtensionInstalled().
1202
1203 EXPECT_TRUE(ContainsKey(service_->pending_extensions(), kGoodId));
1204 }
1205
1206 // Test updating a pending extension for one that is not pending.
1207 TEST_F(ExtensionsServiceTest, UpdatePendingExtensionNotPending) {
1208 InitializeEmptyExtensionsService();
1209
1210 FilePath extensions_path;
1211 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
1212 extensions_path = extensions_path.AppendASCII("extensions");
1213 FilePath path = extensions_path.AppendASCII("good.crx");
1214 UpdateExtension(kGoodId, path, true, false, false);
1215
1216 EXPECT_FALSE(ContainsKey(service_->pending_extensions(), kGoodId));
1217 }
1218
1219 // Test updating a pending extension for one that is already
1220 // installed.
1221 TEST_F(ExtensionsServiceTest, UpdatePendingExtensionAlreadyInstalled) {
1222 InitializeEmptyExtensionsService();
1223
1224 FilePath extensions_path;
1225 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
1226 extensions_path = extensions_path.AppendASCII("extensions");
1227 FilePath path = extensions_path.AppendASCII("good.crx");
1228 InstallExtension(path, true);
1229 ASSERT_EQ(1u, service_->extensions()->size());
1230 Extension* good = service_->extensions()->at(0);
1231
1232 // Use AddPendingExtensionInternal() as AddPendingExtension() would
1233 // balk.
1234 service_->AddPendingExtensionInternal(
1235 good->id(), good->update_url(), *good->version(),
1236 good->IsTheme(), kGoodInstallSilently);
1237
1238 UpdateExtension(good->id(), path, true, true, false);
1239
1240 EXPECT_TRUE(ContainsKey(service_->pending_extensions(), kGoodId));
1104 } 1241 }
1105 1242
1106 // Test pref settings for blacklist and unblacklist extensions. 1243 // Test pref settings for blacklist and unblacklist extensions.
1107 TEST_F(ExtensionsServiceTest, SetUnsetBlacklistInPrefs) { 1244 TEST_F(ExtensionsServiceTest, SetUnsetBlacklistInPrefs) {
1108 InitializeEmptyExtensionsService(); 1245 InitializeEmptyExtensionsService();
1109 std::vector<std::string> blacklist; 1246 std::vector<std::string> blacklist;
1110 blacklist.push_back(good0); 1247 blacklist.push_back(good0);
1111 blacklist.push_back("invalid_id"); // an invalid id 1248 blacklist.push_back("invalid_id"); // an invalid id
1112 blacklist.push_back(good1); 1249 blacklist.push_back(good1);
1113 service_->UpdateExtensionBlacklist(blacklist); 1250 service_->UpdateExtensionBlacklist(blacklist);
(...skipping 23 matching lines...)
1137 InitializeEmptyExtensionsService(); 1274 InitializeEmptyExtensionsService();
1138 FilePath extensions_path; 1275 FilePath extensions_path;
1139 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); 1276 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
1140 extensions_path = extensions_path.AppendASCII("extensions"); 1277 extensions_path = extensions_path.AppendASCII("extensions");
1141 1278
1142 FilePath path = extensions_path.AppendASCII("good.crx"); 1279 FilePath path = extensions_path.AppendASCII("good.crx");
1143 1280
1144 InstallExtension(path, true); 1281 InstallExtension(path, true);
1145 Extension* good = service_->extensions()->at(0); 1282 Extension* good = service_->extensions()->at(0);
1146 EXPECT_EQ(good_crx, good->id()); 1283 EXPECT_EQ(good_crx, good->id());
1147 UpdateExtension(good_crx, path, false, false); 1284 UpdateExtension(good_crx, path, false, false, false);
1148 1285
1149 std::vector<std::string> blacklist; 1286 std::vector<std::string> blacklist;
1150 blacklist.push_back(good_crx); 1287 blacklist.push_back(good_crx);
1151 service_->UpdateExtensionBlacklist(blacklist); 1288 service_->UpdateExtensionBlacklist(blacklist);
1152 // Make sure pref is updated 1289 // Make sure pref is updated
1153 loop_.RunAllPending(); 1290 loop_.RunAllPending();
1154 1291
1155 // Now, the good_crx is blacklisted. 1292 // Now, the good_crx is blacklisted.
1156 ValidateBooleanPref(good_crx, L"blacklist", true); 1293 ValidateBooleanPref(good_crx, L"blacklist", true);
1157 EXPECT_EQ(0u, service_->extensions()->size()); 1294 EXPECT_EQ(0u, service_->extensions()->size());
(...skipping 629 matching lines...)
1787 // Component extensions shouldn't get recourded in the prefs. 1924 // Component extensions shouldn't get recourded in the prefs.
1788 ValidatePrefKeyCount(0); 1925 ValidatePrefKeyCount(0);
1789 1926
1790 // Reload all extensions, and make sure it comes back. 1927 // Reload all extensions, and make sure it comes back.
1791 std::string extension_id = service_->extensions()->at(0)->id(); 1928 std::string extension_id = service_->extensions()->at(0)->id();
1792 loaded_.clear(); 1929 loaded_.clear();
1793 service_->ReloadExtensions(); 1930 service_->ReloadExtensions();
1794 ASSERT_EQ(1u, service_->extensions()->size()); 1931 ASSERT_EQ(1u, service_->extensions()->size());
1795 EXPECT_EQ(extension_id, service_->extensions()->at(0)->id()); 1932 EXPECT_EQ(extension_id, service_->extensions()->at(0)->id());
1796 } 1933 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extensions_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine