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

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

Issue 6965018: Install CRX updates one at a time. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 6 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <map> 5 #include <map>
6 6
7 #include "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
10 #include "base/stl_util-inl.h" 11 #include "base/stl_util-inl.h"
11 #include "base/string_number_conversions.h" 12 #include "base/string_number_conversions.h"
12 #include "base/string_split.h" 13 #include "base/string_split.h"
13 #include "base/string_util.h" 14 #include "base/string_util.h"
14 #include "base/stringprintf.h" 15 #include "base/stringprintf.h"
15 #include "base/threading/thread.h" 16 #include "base/threading/thread.h"
16 #include "base/version.h" 17 #include "base/version.h"
18 #include "chrome/browser/extensions/crx_installer.h"
17 #include "chrome/browser/extensions/extension_error_reporter.h" 19 #include "chrome/browser/extensions/extension_error_reporter.h"
18 #include "chrome/browser/extensions/extension_sync_data.h" 20 #include "chrome/browser/extensions/extension_sync_data.h"
19 #include "chrome/browser/extensions/extension_updater.h" 21 #include "chrome/browser/extensions/extension_updater.h"
20 #include "chrome/browser/extensions/test_extension_prefs.h" 22 #include "chrome/browser/extensions/test_extension_prefs.h"
21 #include "chrome/browser/extensions/test_extension_service.h" 23 #include "chrome/browser/extensions/test_extension_service.h"
22 #include "chrome/browser/prefs/pref_service.h" 24 #include "chrome/browser/prefs/pref_service.h"
23 #include "chrome/common/extensions/extension.h" 25 #include "chrome/common/extensions/extension.h"
24 #include "chrome/common/extensions/extension_constants.h" 26 #include "chrome/common/extensions/extension_constants.h"
25 #include "chrome/common/net/test_url_fetcher_factory.h" 27 #include "chrome/common/net/test_url_fetcher_factory.h"
26 #include "chrome/common/pref_names.h" 28 #include "chrome/common/pref_names.h"
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 void set_extensions(ExtensionList extensions) { 171 void set_extensions(ExtensionList extensions) {
170 extensions_ = extensions; 172 extensions_ = extensions;
171 } 173 }
172 174
173 private: 175 private:
174 ExtensionList extensions_; 176 ExtensionList extensions_;
175 }; 177 };
176 178
177 class ServiceForDownloadTests : public MockService { 179 class ServiceForDownloadTests : public MockService {
178 public: 180 public:
179 virtual void UpdateExtension(const std::string& id, 181 ServiceForDownloadTests()
180 const FilePath& extension_path, 182 : MockService() {
181 const GURL& download_url) { 183 }
184
185 // Add a fake crx installer to be returned by a call to UpdateExtension()
186 // with a specific ID. Caller keeps ownership of |crx_installer|.
187 void AddFakeCrxInstaller(std::string& id,
188 CrxInstaller* crx_installer) {
189 fake_crx_installers_[id] = crx_installer;
190 }
191
192 bool UpdateExtension(
193 const std::string& id,
194 const FilePath& extension_path,
195 const GURL& download_url,
196 Source<CrxInstaller>* out_install_notification_source) OVERRIDE {
182 extension_id_ = id; 197 extension_id_ = id;
183 install_path_ = extension_path; 198 install_path_ = extension_path;
184 download_url_ = download_url; 199 download_url_ = download_url;
200
201 if (ContainsKey(fake_crx_installers_, id)) {
202 *out_install_notification_source =
203 Source<CrxInstaller>(fake_crx_installers_[id]);
204 return true;
205 }
206
207 return false;
185 } 208 }
186 209
187 virtual PendingExtensionManager* pending_extension_manager() OVERRIDE { 210 virtual PendingExtensionManager* pending_extension_manager() OVERRIDE {
188 return &pending_extension_manager_; 211 return &pending_extension_manager_;
189 } 212 }
190 213
191 virtual const Extension* GetExtensionById( 214 virtual const Extension* GetExtensionById(
192 const std::string& id, bool) const OVERRIDE { 215 const std::string& id, bool) const OVERRIDE {
193 last_inquired_extension_id_ = id; 216 last_inquired_extension_id_ = id;
194 return NULL; 217 return NULL;
195 } 218 }
196 219
197 const std::string& extension_id() const { return extension_id_; } 220 const std::string& extension_id() const { return extension_id_; }
198 const FilePath& install_path() const { return install_path_; } 221 const FilePath& install_path() const { return install_path_; }
199 const GURL& download_url() const { return download_url_; } 222 const GURL& download_url() const { return download_url_; }
200 const std::string& last_inquired_extension_id() const { 223 const std::string& last_inquired_extension_id() const {
201 return last_inquired_extension_id_; 224 return last_inquired_extension_id_;
202 } 225 }
203 226
204 private: 227 private:
228 // Hold the set of ids that UpdateExtension() should fake success on.
229 // UpdateExtension(id, ...) will return true iff fake_crx_installers_
230 // contains key |id|. |out_install_notification_source| will be set
231 // to Source<CrxInstaller(fake_crx_installers_[i]).
232 std::map<std::string, CrxInstaller*> fake_crx_installers_;
233
205 std::string extension_id_; 234 std::string extension_id_;
206 FilePath install_path_; 235 FilePath install_path_;
207 GURL download_url_; 236 GURL download_url_;
208 237
209 // The last extension ID that GetExtensionById was called with. 238 // The last extension ID that GetExtensionById was called with.
210 // Mutable because the method that sets it (GetExtensionById) is const 239 // Mutable because the method that sets it (GetExtensionById) is const
211 // in the actual extension service, but must record the last extension 240 // in the actual extension service, but must record the last extension
212 // ID in this test class. 241 // ID in this test class.
213 mutable std::string last_inquired_extension_id_; 242 mutable std::string last_inquired_extension_id_;
214 }; 243 };
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 BrowserThread ui_thread(BrowserThread::UI, &ui_loop); 575 BrowserThread ui_thread(BrowserThread::UI, &ui_loop);
547 BrowserThread file_thread(BrowserThread::FILE); 576 BrowserThread file_thread(BrowserThread::FILE);
548 file_thread.Start(); 577 file_thread.Start();
549 BrowserThread io_thread(BrowserThread::IO); 578 BrowserThread io_thread(BrowserThread::IO);
550 io_thread.Start(); 579 io_thread.Start();
551 580
552 TestURLFetcherFactory factory; 581 TestURLFetcherFactory factory;
553 TestURLFetcher* fetcher = NULL; 582 TestURLFetcher* fetcher = NULL;
554 URLFetcher::set_factory(&factory); 583 URLFetcher::set_factory(&factory);
555 scoped_ptr<ServiceForDownloadTests> service(new ServiceForDownloadTests); 584 scoped_ptr<ServiceForDownloadTests> service(new ServiceForDownloadTests);
556 ExtensionUpdater updater(service.get(), service->extension_prefs(), 585 ExtensionUpdater updater(service.get(),
586 service->extension_prefs(),
557 service->pref_service(), 587 service->pref_service(),
558 service->profile(), 588 service->profile(),
559 kUpdateFrequencySecs); 589 kUpdateFrequencySecs);
560 updater.Start(); 590 updater.Start();
561 591
562 GURL url1("http://localhost/manifest1"); 592 GURL url1("http://localhost/manifest1");
563 GURL url2("http://localhost/manifest2"); 593 GURL url2("http://localhost/manifest2");
564 594
565 // Request 2 update checks - the first should begin immediately and the 595 // Request 2 update checks - the first should begin immediately and the
566 // second one should be queued up. 596 // second one should be queued up.
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
732 // The updater should have called extension service to process the 762 // The updater should have called extension service to process the
733 // blacklist. 763 // blacklist.
734 EXPECT_TRUE(service.processed_blacklist()); 764 EXPECT_TRUE(service.processed_blacklist());
735 765
736 EXPECT_EQ(version, service.pref_service()-> 766 EXPECT_EQ(version, service.pref_service()->
737 GetString(prefs::kExtensionBlacklistUpdateVersion)); 767 GetString(prefs::kExtensionBlacklistUpdateVersion));
738 768
739 URLFetcher::set_factory(NULL); 769 URLFetcher::set_factory(NULL);
740 } 770 }
741 771
742 static void TestMultipleExtensionDownloading() { 772 // Two extensions are updated. If |updates_start_running| is true, the
773 // mock extensions service has UpdateExtension(...) return true, and
774 // the test is responsible for creating fake CrxInstallers. Otherwise,
775 // UpdateExtension() returns false, signaling install failures.
776 static void TestMultipleExtensionDownloading(bool updates_start_running) {
743 MessageLoopForUI message_loop; 777 MessageLoopForUI message_loop;
744 BrowserThread ui_thread(BrowserThread::UI, &message_loop); 778 BrowserThread ui_thread(BrowserThread::UI, &message_loop);
745 BrowserThread file_thread(BrowserThread::FILE, &message_loop); 779 BrowserThread file_thread(BrowserThread::FILE, &message_loop);
746 BrowserThread io_thread(BrowserThread::IO); 780 BrowserThread io_thread(BrowserThread::IO);
747 io_thread.Start(); 781 io_thread.Start();
748 782
749 TestURLFetcherFactory factory; 783 TestURLFetcherFactory factory;
750 TestURLFetcher* fetcher = NULL; 784 TestURLFetcher* fetcher = NULL;
751 URLFetcher::set_factory(&factory); 785 URLFetcher::set_factory(&factory);
752 ServiceForDownloadTests service; 786 ServiceForDownloadTests service;
753 ExtensionUpdater updater( 787 ExtensionUpdater updater(
754 &service, service.extension_prefs(), service.pref_service(), 788 &service, service.extension_prefs(), service.pref_service(),
755 service.profile(), kUpdateFrequencySecs); 789 service.profile(), kUpdateFrequencySecs);
756 updater.Start(); 790 updater.Start();
757 791
792 EXPECT_FALSE(updater.crx_install_is_running_);
793
758 GURL url1("http://localhost/extension1.crx"); 794 GURL url1("http://localhost/extension1.crx");
759 GURL url2("http://localhost/extension2.crx"); 795 GURL url2("http://localhost/extension2.crx");
760 796
761 std::string id1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 797 std::string id1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
762 std::string id2 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; 798 std::string id2 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
763 799
764 std::string hash1 = ""; 800 std::string hash1 = "";
765 std::string hash2 = ""; 801 std::string hash2 = "";
766 802
767 std::string version1 = "0.1"; 803 std::string version1 = "0.1";
768 std::string version2 = "0.1"; 804 std::string version2 = "0.1";
769 // Start two fetches 805 // Start two fetches
770 updater.FetchUpdatedExtension(id1, url1, hash1, version1); 806 updater.FetchUpdatedExtension(id1, url1, hash1, version1);
771 updater.FetchUpdatedExtension(id2, url2, hash2, version2); 807 updater.FetchUpdatedExtension(id2, url2, hash2, version2);
772 808
773 // Make the first fetch complete. 809 // Make the first fetch complete.
774 FilePath extension_file_path(FILE_PATH_LITERAL("/whatever")); 810 FilePath extension_file_path(FILE_PATH_LITERAL("/whatever"));
775 811
776 fetcher = factory.GetFetcherByID(ExtensionUpdater::kExtensionFetcherId); 812 fetcher = factory.GetFetcherByID(ExtensionUpdater::kExtensionFetcherId);
777 EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL); 813 EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
778 EXPECT_TRUE(fetcher->load_flags() == expected_load_flags); 814 EXPECT_TRUE(fetcher->load_flags() == expected_load_flags);
779 815
816 const base::WeakPtr<ExtensionService> kNullExtensionsService;
817 scoped_refptr<CrxInstaller> fake_crx1(
818 new CrxInstaller(kNullExtensionsService, NULL));
819 scoped_refptr<CrxInstaller> fake_crx2(
820 new CrxInstaller(kNullExtensionsService, NULL));
821
822 if (updates_start_running) {
823 // Add fake CrxInstaller to be returned by service.UpdateExtension().
824 service.AddFakeCrxInstaller(id1, fake_crx1.get());
825 service.AddFakeCrxInstaller(id2, fake_crx2.get());
826 } else {
827 // If we don't add fake CRX installers, the mock service fakes a failure
828 // starting the install.
829 }
830
780 fetcher->set_url(url1); 831 fetcher->set_url(url1);
781 fetcher->set_status(net::URLRequestStatus()); 832 fetcher->set_status(net::URLRequestStatus());
782 fetcher->set_response_code(200); 833 fetcher->set_response_code(200);
783 fetcher->SetResponseFilePath(extension_file_path); 834 fetcher->SetResponseFilePath(extension_file_path);
784 fetcher->delegate()->OnURLFetchComplete(fetcher); 835 fetcher->delegate()->OnURLFetchComplete(fetcher);
785 836
786 message_loop.RunAllPending(); 837 message_loop.RunAllPending();
787 838
788 // Expect that the service was asked to do an install with the right data. 839 // Expect that the service was asked to do an install with the right data.
789 FilePath tmpfile_path = service.install_path(); 840 FilePath tmpfile_path = service.install_path();
790 EXPECT_FALSE(tmpfile_path.empty()); 841 EXPECT_FALSE(tmpfile_path.empty());
791 EXPECT_EQ(id1, service.extension_id()); 842 EXPECT_EQ(id1, service.extension_id());
792 EXPECT_EQ(url1, service.download_url()); 843 EXPECT_EQ(url1, service.download_url());
793 message_loop.RunAllPending(); 844 message_loop.RunAllPending();
794 845
795 // Make sure the second fetch finished and asked the service to do an 846 // Make sure the second fetch finished and asked the service to do an
796 // update. 847 // update.
797 FilePath extension_file_path2(FILE_PATH_LITERAL("/whatever2")); 848 FilePath extension_file_path2(FILE_PATH_LITERAL("/whatever2"));
798 fetcher = factory.GetFetcherByID(ExtensionUpdater::kExtensionFetcherId); 849 fetcher = factory.GetFetcherByID(ExtensionUpdater::kExtensionFetcherId);
799 EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL); 850 EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
800 EXPECT_TRUE(fetcher->load_flags() == expected_load_flags); 851 EXPECT_TRUE(fetcher->load_flags() == expected_load_flags);
801 852
802 fetcher->set_url(url2); 853 fetcher->set_url(url2);
803 fetcher->set_status(net::URLRequestStatus()); 854 fetcher->set_status(net::URLRequestStatus());
804 fetcher->set_response_code(200); 855 fetcher->set_response_code(200);
805 fetcher->SetResponseFilePath(extension_file_path2); 856 fetcher->SetResponseFilePath(extension_file_path2);
806 fetcher->delegate()->OnURLFetchComplete(fetcher); 857 fetcher->delegate()->OnURLFetchComplete(fetcher);
807 message_loop.RunAllPending(); 858 message_loop.RunAllPending();
859
860 if (updates_start_running) {
861 EXPECT_TRUE(updater.crx_install_is_running_);
862
863 // The second install should not have run, because the first has not
864 // sent a notification that it finished.
865 EXPECT_EQ(id1, service.extension_id());
866 EXPECT_EQ(url1, service.download_url());
867
868 // Fake install notice. This should start the second installation,
869 // which will be checked below.
870 fake_crx1->NotifyCrxInstallComplete();
871
872 EXPECT_TRUE(updater.crx_install_is_running_);
873 }
874
808 EXPECT_EQ(id2, service.extension_id()); 875 EXPECT_EQ(id2, service.extension_id());
809 EXPECT_EQ(url2, service.download_url()); 876 EXPECT_EQ(url2, service.download_url());
810 EXPECT_FALSE(service.install_path().empty()); 877 EXPECT_FALSE(service.install_path().empty());
811 878
812 // Make sure the correct crx contents were passed for the update call. 879 // Make sure the correct crx contents were passed for the update call.
813 EXPECT_EQ(extension_file_path2, service.install_path()); 880 EXPECT_EQ(extension_file_path2, service.install_path());
881
882 if (updates_start_running) {
883 EXPECT_TRUE(updater.crx_install_is_running_);
884 fake_crx2->NotifyCrxInstallComplete();
885 }
886 EXPECT_FALSE(updater.crx_install_is_running_);
814 } 887 }
815 888
816 // Test requests to both a Google server and a non-google server. This allows 889 // Test requests to both a Google server and a non-google server. This allows
817 // us to test various combinations of installed (ie roll call) and active 890 // us to test various combinations of installed (ie roll call) and active
818 // (ie app launch) ping scenarios. The invariant is that each type of ping 891 // (ie app launch) ping scenarios. The invariant is that each type of ping
819 // value should be present at most once per day, and can be calculated based 892 // value should be present at most once per day, and can be calculated based
820 // on the delta between now and the last ping time (or in the case of active 893 // on the delta between now and the last ping time (or in the case of active
821 // pings, that delta plus whether the app has been active). 894 // pings, that delta plus whether the app has been active).
822 static void TestGalleryRequests(int rollcall_ping_days, 895 static void TestGalleryRequests(int rollcall_ping_days,
823 int active_ping_days, 896 int active_ping_days,
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
1011 1084
1012 TEST(ExtensionUpdaterTest, TestSingleExtensionDownloadingPending) { 1085 TEST(ExtensionUpdaterTest, TestSingleExtensionDownloadingPending) {
1013 ExtensionUpdaterTest::TestSingleExtensionDownloading(true); 1086 ExtensionUpdaterTest::TestSingleExtensionDownloading(true);
1014 } 1087 }
1015 1088
1016 // This test is disabled on Mac, see http://crbug.com/26035. 1089 // This test is disabled on Mac, see http://crbug.com/26035.
1017 TEST(ExtensionUpdaterTest, TestBlacklistDownloading) { 1090 TEST(ExtensionUpdaterTest, TestBlacklistDownloading) {
1018 ExtensionUpdaterTest::TestBlacklistDownloading(); 1091 ExtensionUpdaterTest::TestBlacklistDownloading();
1019 } 1092 }
1020 1093
1021 TEST(ExtensionUpdaterTest, TestMultipleExtensionDownloading) { 1094 TEST(ExtensionUpdaterTest, TestMultipleExtensionDownloadingUpdatesFail) {
1022 ExtensionUpdaterTest::TestMultipleExtensionDownloading(); 1095 ExtensionUpdaterTest::TestMultipleExtensionDownloading(false);
1096 }
1097 TEST(ExtensionUpdaterTest, TestMultipleExtensionDownloadingUpdatesSucceed) {
1098 ExtensionUpdaterTest::TestMultipleExtensionDownloading(true);
1023 } 1099 }
1024 1100
1025 TEST(ExtensionUpdaterTest, TestGalleryRequests) { 1101 TEST(ExtensionUpdaterTest, TestGalleryRequests) {
1026 // We want to test a variety of combinations of expected ping conditions for 1102 // We want to test a variety of combinations of expected ping conditions for
1027 // rollcall and active pings. 1103 // rollcall and active pings.
1028 int ping_cases[] = { ManifestFetchData::kNeverPinged, 0, 1, 5 }; 1104 int ping_cases[] = { ManifestFetchData::kNeverPinged, 0, 1, 5 };
1029 1105
1030 for (size_t i = 0; i < arraysize(ping_cases); i++) { 1106 for (size_t i = 0; i < arraysize(ping_cases); i++) {
1031 for (size_t j = 0; j < arraysize(ping_cases); j++) { 1107 for (size_t j = 0; j < arraysize(ping_cases); j++) {
1032 for (size_t k = 0; k < 2; k++) { 1108 for (size_t k = 0; k < 2; k++) {
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
1154 1230
1155 // TODO(asargent) - (http://crbug.com/12780) add tests for: 1231 // TODO(asargent) - (http://crbug.com/12780) add tests for:
1156 // -prodversionmin (shouldn't update if browser version too old) 1232 // -prodversionmin (shouldn't update if browser version too old)
1157 // -manifests & updates arriving out of order / interleaved 1233 // -manifests & updates arriving out of order / interleaved
1158 // -malformed update url (empty, file://, has query, has a # fragment, etc.) 1234 // -malformed update url (empty, file://, has query, has a # fragment, etc.)
1159 // -An extension gets uninstalled while updates are in progress (so it doesn't 1235 // -An extension gets uninstalled while updates are in progress (so it doesn't
1160 // "come back from the dead") 1236 // "come back from the dead")
1161 // -An extension gets manually updated to v3 while we're downloading v2 (ie 1237 // -An extension gets manually updated to v3 while we're downloading v2 (ie
1162 // you don't get downgraded accidentally) 1238 // you don't get downgraded accidentally)
1163 // -An update manifest mentions multiple updates 1239 // -An update manifest mentions multiple updates
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698