Home | History | Annotate | Download | only in updater
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <list>
      6 #include <map>
      7 #include <set>
      8 #include <vector>
      9 
     10 #include "base/bind.h"
     11 #include "base/bind_helpers.h"
     12 #include "base/command_line.h"
     13 #include "base/compiler_specific.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/memory/weak_ptr.h"
     16 #include "base/message_loop/message_loop.h"
     17 #include "base/run_loop.h"
     18 #include "base/sequenced_task_runner.h"
     19 #include "base/stl_util.h"
     20 #include "base/strings/string_number_conversions.h"
     21 #include "base/strings/string_split.h"
     22 #include "base/strings/string_util.h"
     23 #include "base/strings/stringprintf.h"
     24 #include "base/threading/thread.h"
     25 #include "base/version.h"
     26 #include "chrome/browser/chrome_notification_types.h"
     27 #include "chrome/browser/extensions/crx_installer.h"
     28 #include "chrome/browser/extensions/extension_error_reporter.h"
     29 #include "chrome/browser/extensions/extension_sync_data.h"
     30 #include "chrome/browser/extensions/test_extension_prefs.h"
     31 #include "chrome/browser/extensions/test_extension_service.h"
     32 #include "chrome/browser/extensions/test_extension_system.h"
     33 #include "chrome/browser/extensions/updater/extension_downloader.h"
     34 #include "chrome/browser/extensions/updater/extension_downloader_delegate.h"
     35 #include "chrome/browser/extensions/updater/extension_updater.h"
     36 #include "chrome/browser/extensions/updater/manifest_fetch_data.h"
     37 #include "chrome/browser/extensions/updater/request_queue_impl.h"
     38 #include "chrome/browser/google/google_brand.h"
     39 #include "chrome/browser/omaha_query_params/omaha_query_params.h"
     40 #include "chrome/browser/prefs/pref_service_syncable.h"
     41 #include "chrome/common/pref_names.h"
     42 #include "chrome/test/base/testing_profile.h"
     43 #include "content/public/browser/notification_details.h"
     44 #include "content/public/browser/notification_observer.h"
     45 #include "content/public/browser/notification_registrar.h"
     46 #include "content/public/browser/notification_service.h"
     47 #include "content/public/browser/notification_source.h"
     48 #include "content/public/test/test_browser_thread_bundle.h"
     49 #include "content/public/test/test_utils.h"
     50 #include "extensions/browser/extension_prefs.h"
     51 #include "extensions/browser/extension_registry.h"
     52 #include "extensions/browser/extension_system.h"
     53 #include "extensions/common/extension.h"
     54 #include "extensions/common/id_util.h"
     55 #include "extensions/common/manifest_constants.h"
     56 #include "libxml/globals.h"
     57 #include "net/base/backoff_entry.h"
     58 #include "net/base/escape.h"
     59 #include "net/base/load_flags.h"
     60 #include "net/url_request/test_url_fetcher_factory.h"
     61 #include "net/url_request/url_request_status.h"
     62 #include "testing/gmock/include/gmock/gmock.h"
     63 #include "testing/gtest/include/gtest/gtest.h"
     64 #include "url/third_party/mozilla/url_parse.h"
     65 
     66 #if defined(OS_CHROMEOS)
     67 #include "chrome/browser/chromeos/login/users/user_manager.h"
     68 #include "chrome/browser/chromeos/settings/cros_settings.h"
     69 #include "chrome/browser/chromeos/settings/device_settings_service.h"
     70 #endif
     71 
     72 using base::Time;
     73 using base::TimeDelta;
     74 using content::BrowserThread;
     75 using testing::DoAll;
     76 using testing::Invoke;
     77 using testing::InvokeWithoutArgs;
     78 using testing::Mock;
     79 using testing::Return;
     80 using testing::SetArgPointee;
     81 using testing::_;
     82 
     83 namespace extensions {
     84 
     85 typedef ExtensionDownloaderDelegate::Error Error;
     86 typedef ExtensionDownloaderDelegate::PingResult PingResult;
     87 
     88 namespace {
     89 
     90 const net::BackoffEntry::Policy kNoBackoffPolicy = {
     91   // Number of initial errors (in sequence) to ignore before applying
     92   // exponential back-off rules.
     93   1000,
     94 
     95   // Initial delay for exponential back-off in ms.
     96   0,
     97 
     98   // Factor by which the waiting time will be multiplied.
     99   0,
    100 
    101   // Fuzzing percentage. ex: 10% will spread requests randomly
    102   // between 90%-100% of the calculated time.
    103   0,
    104 
    105   // Maximum amount of time we are willing to delay our request in ms.
    106   0,
    107 
    108   // Time to keep an entry from being discarded even when it
    109   // has no significant state, -1 to never discard.
    110   -1,
    111 
    112   // Don't use initial delay unless the last request was an error.
    113   false,
    114 };
    115 
    116 const char kEmptyUpdateUrlData[] = "";
    117 
    118 const char kAuthUserQueryKey[] = "authuser";
    119 
    120 int kExpectedLoadFlags =
    121     net::LOAD_DO_NOT_SEND_COOKIES |
    122     net::LOAD_DO_NOT_SAVE_COOKIES |
    123     net::LOAD_DISABLE_CACHE;
    124 
    125 int kExpectedLoadFlagsForProtectedDownload = net::LOAD_DISABLE_CACHE;
    126 
    127 const ManifestFetchData::PingData kNeverPingedData(
    128     ManifestFetchData::kNeverPinged, ManifestFetchData::kNeverPinged, true);
    129 
    130 class MockExtensionDownloaderDelegate : public ExtensionDownloaderDelegate {
    131  public:
    132   MOCK_METHOD4(OnExtensionDownloadFailed, void(const std::string&,
    133                                                Error,
    134                                                const PingResult&,
    135                                                const std::set<int>&));
    136   MOCK_METHOD7(OnExtensionDownloadFinished, void(const std::string&,
    137                                                  const base::FilePath&,
    138                                                  bool,
    139                                                  const GURL&,
    140                                                  const std::string&,
    141                                                  const PingResult&,
    142                                                  const std::set<int>&));
    143   MOCK_METHOD2(GetPingDataForExtension,
    144                bool(const std::string&, ManifestFetchData::PingData*));
    145   MOCK_METHOD1(GetUpdateUrlData, std::string(const std::string&));
    146   MOCK_METHOD1(IsExtensionPending, bool(const std::string&));
    147   MOCK_METHOD2(GetExtensionExistingVersion,
    148                bool(const std::string&, std::string*));
    149 
    150   void Wait() {
    151     scoped_refptr<content::MessageLoopRunner> runner =
    152         new content::MessageLoopRunner;
    153     quit_closure_ = runner->QuitClosure();
    154     runner->Run();
    155     quit_closure_.Reset();
    156   }
    157 
    158   void Quit() {
    159     quit_closure_.Run();
    160   }
    161 
    162   void DelegateTo(ExtensionDownloaderDelegate* delegate) {
    163     ON_CALL(*this, OnExtensionDownloadFailed(_, _, _, _))
    164         .WillByDefault(Invoke(delegate,
    165             &ExtensionDownloaderDelegate::OnExtensionDownloadFailed));
    166     ON_CALL(*this, OnExtensionDownloadFinished(_, _, _, _, _, _, _))
    167         .WillByDefault(Invoke(delegate,
    168             &ExtensionDownloaderDelegate::OnExtensionDownloadFinished));
    169     ON_CALL(*this, GetPingDataForExtension(_, _))
    170         .WillByDefault(Invoke(delegate,
    171             &ExtensionDownloaderDelegate::GetPingDataForExtension));
    172     ON_CALL(*this, GetUpdateUrlData(_))
    173         .WillByDefault(Invoke(delegate,
    174             &ExtensionDownloaderDelegate::GetUpdateUrlData));
    175     ON_CALL(*this, IsExtensionPending(_))
    176         .WillByDefault(Invoke(delegate,
    177             &ExtensionDownloaderDelegate::IsExtensionPending));
    178     ON_CALL(*this, GetExtensionExistingVersion(_, _))
    179         .WillByDefault(Invoke(delegate,
    180             &ExtensionDownloaderDelegate::GetExtensionExistingVersion));
    181   }
    182 
    183  private:
    184   base::Closure quit_closure_;
    185 };
    186 
    187 const int kNotificationsObserved[] = {
    188   chrome::NOTIFICATION_EXTENSION_UPDATING_STARTED,
    189   chrome::NOTIFICATION_EXTENSION_UPDATE_FOUND,
    190 };
    191 
    192 // A class that observes the notifications sent by the ExtensionUpdater and
    193 // the ExtensionDownloader.
    194 class NotificationsObserver : public content::NotificationObserver {
    195  public:
    196   NotificationsObserver() {
    197     for (size_t i = 0; i < arraysize(kNotificationsObserved); ++i) {
    198       count_[i] = 0;
    199       registrar_.Add(this,
    200                      kNotificationsObserved[i],
    201                      content::NotificationService::AllSources());
    202     }
    203   }
    204 
    205   virtual ~NotificationsObserver() {
    206     for (size_t i = 0; i < arraysize(kNotificationsObserved); ++i) {
    207       registrar_.Remove(this,
    208                         kNotificationsObserved[i],
    209                         content::NotificationService::AllSources());
    210     }
    211   }
    212 
    213   size_t StartedCount() { return count_[0]; }
    214   size_t UpdatedCount() { return count_[1]; }
    215 
    216   bool Updated(const std::string& id) {
    217     return updated_.find(id) != updated_.end();
    218   }
    219 
    220   void Wait() {
    221     scoped_refptr<content::MessageLoopRunner> runner =
    222         new content::MessageLoopRunner;
    223     quit_closure_ = runner->QuitClosure();
    224     runner->Run();
    225     quit_closure_.Reset();
    226   }
    227 
    228  private:
    229   virtual void Observe(int type,
    230                        const content::NotificationSource& source,
    231                        const content::NotificationDetails& details) OVERRIDE {
    232     if (!quit_closure_.is_null())
    233       quit_closure_.Run();
    234     for (size_t i = 0; i < arraysize(kNotificationsObserved); ++i) {
    235       if (kNotificationsObserved[i] == type) {
    236         count_[i]++;
    237         if (type == chrome::NOTIFICATION_EXTENSION_UPDATE_FOUND) {
    238           updated_.insert(
    239               content::Details<UpdateDetails>(details)->id);
    240         }
    241         return;
    242       }
    243     }
    244     NOTREACHED();
    245   }
    246 
    247   content::NotificationRegistrar registrar_;
    248   size_t count_[arraysize(kNotificationsObserved)];
    249   std::set<std::string> updated_;
    250   base::Closure quit_closure_;
    251 
    252   DISALLOW_COPY_AND_ASSIGN(NotificationsObserver);
    253 };
    254 
    255 // Extracts the integer value of the |authuser| query parameter. Returns 0 if
    256 // the parameter is not set.
    257 int GetAuthUserQueryValue(const GURL& url) {
    258   std::string query_string = url.query();
    259   url::Component query(0, query_string.length());
    260   url::Component key, value;
    261   while (
    262       url::ExtractQueryKeyValue(query_string.c_str(), &query, &key, &value)) {
    263     std::string key_string = query_string.substr(key.begin, key.len);
    264     if (key_string == kAuthUserQueryKey) {
    265       int user_index = 0;
    266       base::StringToInt(query_string.substr(value.begin, value.len),
    267                         &user_index);
    268       return user_index;
    269     }
    270   }
    271   return 0;
    272 }
    273 
    274 }  // namespace
    275 
    276 // Base class for further specialized test classes.
    277 class MockService : public TestExtensionService {
    278  public:
    279   explicit MockService(TestExtensionPrefs* prefs)
    280       : prefs_(prefs), pending_extension_manager_(&profile_) {}
    281 
    282   virtual ~MockService() {}
    283 
    284   virtual PendingExtensionManager* pending_extension_manager() OVERRIDE {
    285     ADD_FAILURE() << "Subclass should override this if it will "
    286                   << "be accessed by a test.";
    287     return &pending_extension_manager_;
    288   }
    289 
    290   Profile* profile() { return &profile_; }
    291 
    292   net::URLRequestContextGetter* request_context() {
    293     return profile_.GetRequestContext();
    294   }
    295 
    296   ExtensionPrefs* extension_prefs() { return prefs_->prefs(); }
    297 
    298   PrefService* pref_service() { return prefs_->pref_service(); }
    299 
    300   // Creates test extensions and inserts them into list. The name and
    301   // version are all based on their index. If |update_url| is non-null, it
    302   // will be used as the update_url for each extension.
    303   // The |id| is used to distinguish extension names and make sure that
    304   // no two extensions share the same name.
    305   void CreateTestExtensions(int id, int count, ExtensionList *list,
    306                             const std::string* update_url,
    307                             Manifest::Location location) {
    308     for (int i = 1; i <= count; i++) {
    309       base::DictionaryValue manifest;
    310       manifest.SetString(manifest_keys::kVersion,
    311                          base::StringPrintf("%d.0.0.0", i));
    312       manifest.SetString(manifest_keys::kName,
    313                          base::StringPrintf("Extension %d.%d", id, i));
    314       if (update_url)
    315         manifest.SetString(manifest_keys::kUpdateURL, *update_url);
    316       scoped_refptr<Extension> e =
    317           prefs_->AddExtensionWithManifest(manifest, location);
    318       ASSERT_TRUE(e.get() != NULL);
    319       list->push_back(e);
    320     }
    321   }
    322 
    323  protected:
    324   TestExtensionPrefs* const prefs_;
    325   TestingProfile profile_;
    326   PendingExtensionManager pending_extension_manager_;
    327 
    328  private:
    329   DISALLOW_COPY_AND_ASSIGN(MockService);
    330 };
    331 
    332 
    333 bool ShouldInstallExtensionsOnly(const Extension* extension) {
    334   return extension->GetType() == Manifest::TYPE_EXTENSION;
    335 }
    336 
    337 bool ShouldInstallThemesOnly(const Extension* extension) {
    338   return extension->is_theme();
    339 }
    340 
    341 bool ShouldAlwaysInstall(const Extension* extension) {
    342   return true;
    343 }
    344 
    345 // Loads some pending extension records into a pending extension manager.
    346 void SetupPendingExtensionManagerForTest(
    347     int count,
    348     const GURL& update_url,
    349     PendingExtensionManager* pending_extension_manager) {
    350   for (int i = 1; i <= count; ++i) {
    351     PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install =
    352         (i % 2 == 0) ? &ShouldInstallThemesOnly : &ShouldInstallExtensionsOnly;
    353     const bool kIsFromSync = true;
    354     const bool kInstallSilently = true;
    355     const bool kMarkAcknowledged = false;
    356     const bool kRemoteInstall = false;
    357     std::string id = id_util::GenerateId(base::StringPrintf("extension%i", i));
    358 
    359     pending_extension_manager->AddForTesting(
    360         PendingExtensionInfo(id,
    361                              std::string(),
    362                              update_url,
    363                              Version(),
    364                              should_allow_install,
    365                              kIsFromSync,
    366                              kInstallSilently,
    367                              Manifest::INTERNAL,
    368                              Extension::NO_FLAGS,
    369                              kMarkAcknowledged,
    370                              kRemoteInstall));
    371   }
    372 }
    373 
    374 class ServiceForManifestTests : public MockService {
    375  public:
    376   explicit ServiceForManifestTests(TestExtensionPrefs* prefs)
    377       : MockService(prefs), registry_(ExtensionRegistry::Get(profile())) {}
    378 
    379   virtual ~ServiceForManifestTests() {}
    380 
    381   virtual const Extension* GetExtensionById(
    382       const std::string& id, bool include_disabled) const OVERRIDE {
    383     const Extension* result = registry_->enabled_extensions().GetByID(id);
    384     if (result || !include_disabled)
    385       return result;
    386     return registry_->disabled_extensions().GetByID(id);
    387   }
    388 
    389   virtual const ExtensionSet* extensions() const OVERRIDE {
    390     return &registry_->enabled_extensions();
    391   }
    392 
    393   virtual PendingExtensionManager* pending_extension_manager() OVERRIDE {
    394     return &pending_extension_manager_;
    395   }
    396 
    397   virtual const Extension* GetPendingExtensionUpdate(
    398       const std::string& id) const OVERRIDE {
    399     return NULL;
    400   }
    401 
    402   virtual bool IsExtensionEnabled(const std::string& id) const OVERRIDE {
    403     return !registry_->disabled_extensions().Contains(id);
    404   }
    405 
    406   void set_extensions(ExtensionList extensions,
    407                       ExtensionList disabled_extensions) {
    408     registry_->ClearAll();
    409     for (ExtensionList::const_iterator it = extensions.begin();
    410          it != extensions.end(); ++it) {
    411       registry_->AddEnabled(*it);
    412     }
    413     for (ExtensionList::const_iterator it = disabled_extensions.begin();
    414          it != disabled_extensions.end(); ++it) {
    415       registry_->AddDisabled(*it);
    416     }
    417   }
    418 
    419  private:
    420   ExtensionRegistry* registry_;
    421 };
    422 
    423 class ServiceForDownloadTests : public MockService {
    424  public:
    425   explicit ServiceForDownloadTests(TestExtensionPrefs* prefs)
    426       : MockService(prefs) {
    427   }
    428 
    429   // Add a fake crx installer to be returned by a call to UpdateExtension()
    430   // with a specific ID.  Caller keeps ownership of |crx_installer|.
    431   void AddFakeCrxInstaller(const std::string& id, CrxInstaller* crx_installer) {
    432     fake_crx_installers_[id] = crx_installer;
    433   }
    434 
    435   virtual bool UpdateExtension(
    436       const std::string& id,
    437       const base::FilePath& extension_path,
    438       bool file_ownership_passed,
    439       CrxInstaller** out_crx_installer) OVERRIDE {
    440     extension_id_ = id;
    441     install_path_ = extension_path;
    442 
    443     if (ContainsKey(fake_crx_installers_, id)) {
    444       *out_crx_installer = fake_crx_installers_[id];
    445       return true;
    446     }
    447 
    448     return false;
    449   }
    450 
    451   virtual PendingExtensionManager* pending_extension_manager() OVERRIDE {
    452     return &pending_extension_manager_;
    453   }
    454 
    455   virtual const Extension* GetExtensionById(
    456       const std::string& id, bool) const OVERRIDE {
    457     last_inquired_extension_id_ = id;
    458     return NULL;
    459   }
    460 
    461   const std::string& extension_id() const { return extension_id_; }
    462   const base::FilePath& install_path() const { return install_path_; }
    463 
    464  private:
    465   // Hold the set of ids that UpdateExtension() should fake success on.
    466   // UpdateExtension(id, ...) will return true iff fake_crx_installers_
    467   // contains key |id|.  |out_install_notification_source| will be set
    468   // to Source<CrxInstaller(fake_crx_installers_[i]).
    469   std::map<std::string, CrxInstaller*> fake_crx_installers_;
    470 
    471   std::string extension_id_;
    472   base::FilePath install_path_;
    473   GURL download_url_;
    474 
    475   // The last extension ID that GetExtensionById was called with.
    476   // Mutable because the method that sets it (GetExtensionById) is const
    477   // in the actual extension service, but must record the last extension
    478   // ID in this test class.
    479   mutable std::string last_inquired_extension_id_;
    480 };
    481 
    482 static const int kUpdateFrequencySecs = 15;
    483 
    484 // Takes a string with KEY=VALUE parameters separated by '&' in |params| and
    485 // puts the key/value pairs into |result|. For keys with no value, the empty
    486 // string is used. So for "a=1&b=foo&c", result would map "a" to "1", "b" to
    487 // "foo", and "c" to "".
    488 static void ExtractParameters(const std::string& params,
    489                               std::map<std::string, std::string>* result) {
    490   std::vector<std::string> pairs;
    491   base::SplitString(params, '&', &pairs);
    492   for (size_t i = 0; i < pairs.size(); i++) {
    493     std::vector<std::string> key_val;
    494     base::SplitString(pairs[i], '=', &key_val);
    495     if (!key_val.empty()) {
    496       std::string key = key_val[0];
    497       EXPECT_TRUE(result->find(key) == result->end());
    498       (*result)[key] = (key_val.size() == 2) ? key_val[1] : std::string();
    499     } else {
    500       NOTREACHED();
    501     }
    502   }
    503 }
    504 
    505 static void VerifyQueryAndExtractParameters(
    506     const std::string& query,
    507     std::map<std::string, std::string>* result) {
    508   std::map<std::string, std::string> params;
    509   ExtractParameters(query, &params);
    510 
    511   std::string omaha_params =
    512       chrome::OmahaQueryParams::Get(chrome::OmahaQueryParams::CRX);
    513   std::map<std::string, std::string> expected;
    514   ExtractParameters(omaha_params, &expected);
    515 
    516   for (std::map<std::string, std::string>::iterator it = expected.begin();
    517        it != expected.end(); ++it) {
    518     EXPECT_EQ(it->second, params[it->first]);
    519   }
    520 
    521   EXPECT_EQ(1U, params.count("x"));
    522   std::string decoded = net::UnescapeURLComponent(
    523       params["x"], net::UnescapeRule::URL_SPECIAL_CHARS);
    524   ExtractParameters(decoded, result);
    525 }
    526 
    527 // All of our tests that need to use private APIs of ExtensionUpdater live
    528 // inside this class (which is a friend to ExtensionUpdater).
    529 class ExtensionUpdaterTest : public testing::Test {
    530  public:
    531   ExtensionUpdaterTest()
    532       : thread_bundle_(
    533             content::TestBrowserThreadBundle::IO_MAINLOOP) {
    534   }
    535 
    536   virtual void SetUp() OVERRIDE {
    537     prefs_.reset(new TestExtensionPrefs(base::MessageLoopProxy::current()));
    538   }
    539 
    540   virtual void TearDown() OVERRIDE {
    541     // Some tests create URLRequestContextGetters, whose destruction must run
    542     // on the IO thread. Make sure the IO loop spins before shutdown so that
    543     // those objects are released.
    544     RunUntilIdle();
    545     prefs_.reset();
    546   }
    547 
    548   void RunUntilIdle() {
    549     prefs_->pref_service()->CommitPendingWrite();
    550     base::RunLoop().RunUntilIdle();
    551   }
    552 
    553   void SimulateTimerFired(ExtensionUpdater* updater) {
    554     EXPECT_TRUE(updater->timer_.IsRunning());
    555     updater->timer_.Stop();
    556     updater->TimerFired();
    557   }
    558 
    559   // Adds a Result with the given data to results.
    560   void AddParseResult(const std::string& id,
    561                       const std::string& version,
    562                       const std::string& url,
    563                       UpdateManifest::Results* results) {
    564     UpdateManifest::Result result;
    565     result.extension_id = id;
    566     result.version = version;
    567     result.crx_url = GURL(url);
    568     results->list.push_back(result);
    569   }
    570 
    571   void ResetDownloader(ExtensionUpdater* updater,
    572                        ExtensionDownloader* downloader) {
    573     EXPECT_FALSE(updater->downloader_.get());
    574     updater->downloader_.reset(downloader);
    575   }
    576 
    577   void StartUpdateCheck(ExtensionDownloader* downloader,
    578                         ManifestFetchData* fetch_data) {
    579     downloader->StartUpdateCheck(scoped_ptr<ManifestFetchData>(fetch_data));
    580   }
    581 
    582   size_t ManifestFetchersCount(ExtensionDownloader* downloader) {
    583     return downloader->manifests_queue_.size() +
    584            (downloader->manifest_fetcher_.get() ? 1 : 0);
    585   }
    586 
    587   void TestExtensionUpdateCheckRequests(bool pending) {
    588     // Create an extension with an update_url.
    589     ServiceForManifestTests service(prefs_.get());
    590     std::string update_url("http://foo.com/bar");
    591     ExtensionList extensions;
    592     NotificationsObserver observer;
    593     PendingExtensionManager* pending_extension_manager =
    594         service.pending_extension_manager();
    595     if (pending) {
    596       SetupPendingExtensionManagerForTest(1, GURL(update_url),
    597                                           pending_extension_manager);
    598     } else {
    599       service.CreateTestExtensions(1, 1, &extensions, &update_url,
    600                                    Manifest::INTERNAL);
    601       service.set_extensions(extensions, ExtensionList());
    602     }
    603 
    604     // Set up and start the updater.
    605     net::TestURLFetcherFactory factory;
    606     ExtensionUpdater updater(
    607         &service, service.extension_prefs(), service.pref_service(),
    608         service.profile(), 60*60*24, NULL);
    609     updater.Start();
    610 
    611     // Tell the update that it's time to do update checks.
    612     EXPECT_EQ(0u, observer.StartedCount());
    613     SimulateTimerFired(&updater);
    614     EXPECT_EQ(1u, observer.StartedCount());
    615 
    616     // Get the url our mock fetcher was asked to fetch.
    617     net::TestURLFetcher* fetcher =
    618         factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
    619     const GURL& url = fetcher->GetOriginalURL();
    620     EXPECT_FALSE(url.is_empty());
    621     EXPECT_TRUE(url.is_valid());
    622     EXPECT_TRUE(url.SchemeIs("http"));
    623     EXPECT_EQ("foo.com", url.host());
    624     EXPECT_EQ("/bar", url.path());
    625 
    626     // Validate the extension request parameters in the query. It should
    627     // look something like "x=id%3D<id>%26v%3D<version>%26uc".
    628     EXPECT_TRUE(url.has_query());
    629     std::map<std::string, std::string> params;
    630     VerifyQueryAndExtractParameters(url.query(), &params);
    631     if (pending) {
    632       EXPECT_TRUE(pending_extension_manager->IsIdPending(params["id"]));
    633       EXPECT_EQ("0.0.0.0", params["v"]);
    634     } else {
    635       EXPECT_EQ(extensions[0]->id(), params["id"]);
    636       EXPECT_EQ(extensions[0]->VersionString(), params["v"]);
    637     }
    638     EXPECT_EQ("", params["uc"]);
    639   }
    640 
    641   void TestUpdateUrlDataEmpty() {
    642     const std::string id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    643     const std::string version = "1.0";
    644 
    645     // Make sure that an empty update URL data string does not cause a ap=
    646     // option to appear in the x= parameter.
    647     ManifestFetchData fetch_data(GURL("http://localhost/foo"), 0);
    648     fetch_data.AddExtension(
    649         id, version, &kNeverPingedData, std::string(), std::string());
    650 
    651     std::map<std::string, std::string> params;
    652     VerifyQueryAndExtractParameters(fetch_data.full_url().query(), &params);
    653     EXPECT_EQ(id, params["id"]);
    654     EXPECT_EQ(version, params["v"]);
    655     EXPECT_EQ(0U, params.count("ap"));
    656   }
    657 
    658   void TestUpdateUrlDataSimple() {
    659     const std::string id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    660     const std::string version = "1.0";
    661 
    662     // Make sure that an update URL data string causes an appropriate ap=
    663     // option to appear in the x= parameter.
    664     ManifestFetchData fetch_data(GURL("http://localhost/foo"), 0);
    665     fetch_data.AddExtension(
    666         id, version, &kNeverPingedData, "bar", std::string());
    667     std::map<std::string, std::string> params;
    668     VerifyQueryAndExtractParameters(fetch_data.full_url().query(), &params);
    669     EXPECT_EQ(id, params["id"]);
    670     EXPECT_EQ(version, params["v"]);
    671     EXPECT_EQ("bar", params["ap"]);
    672   }
    673 
    674   void TestUpdateUrlDataCompound() {
    675     const std::string id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    676     const std::string version = "1.0";
    677 
    678     // Make sure that an update URL data string causes an appropriate ap=
    679     // option to appear in the x= parameter.
    680     ManifestFetchData fetch_data(GURL("http://localhost/foo"), 0);
    681     fetch_data.AddExtension(
    682         id, version, &kNeverPingedData, "a=1&b=2&c", std::string());
    683     std::map<std::string, std::string> params;
    684     VerifyQueryAndExtractParameters(fetch_data.full_url().query(), &params);
    685     EXPECT_EQ(id, params["id"]);
    686     EXPECT_EQ(version, params["v"]);
    687     EXPECT_EQ("a%3D1%26b%3D2%26c", params["ap"]);
    688   }
    689 
    690   void TestUpdateUrlDataFromGallery(const std::string& gallery_url) {
    691     net::TestURLFetcherFactory factory;
    692 
    693     MockService service(prefs_.get());
    694     MockExtensionDownloaderDelegate delegate;
    695     ExtensionDownloader downloader(&delegate, service.request_context());
    696     ExtensionList extensions;
    697     std::string url(gallery_url);
    698 
    699     service.CreateTestExtensions(1, 1, &extensions, &url, Manifest::INTERNAL);
    700 
    701     const std::string& id = extensions[0]->id();
    702     EXPECT_CALL(delegate, GetPingDataForExtension(id, _));
    703 
    704     downloader.AddExtension(*extensions[0].get(), 0);
    705     downloader.StartAllPending(NULL);
    706     net::TestURLFetcher* fetcher =
    707         factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
    708     ASSERT_TRUE(fetcher);
    709     // Make sure that extensions that update from the gallery ignore any
    710     // update URL data.
    711     const std::string& update_url = fetcher->GetOriginalURL().spec();
    712     std::string::size_type x = update_url.find("x=");
    713     EXPECT_NE(std::string::npos, x);
    714     std::string::size_type ap = update_url.find("ap%3D", x);
    715     EXPECT_EQ(std::string::npos, ap);
    716   }
    717 
    718   void TestInstallSource() {
    719     const std::string id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    720     const std::string version = "1.0";
    721     const std::string install_source = "instally";
    722 
    723     // Make sure that an installsource= appears in the x= parameter.
    724     ManifestFetchData fetch_data(GURL("http://localhost/foo"), 0);
    725     fetch_data.AddExtension(id, version, &kNeverPingedData,
    726                             kEmptyUpdateUrlData, install_source);
    727     std::map<std::string, std::string> params;
    728     VerifyQueryAndExtractParameters(fetch_data.full_url().query(), &params);
    729     EXPECT_EQ(id, params["id"]);
    730     EXPECT_EQ(version, params["v"]);
    731     EXPECT_EQ(install_source, params["installsource"]);
    732   }
    733 
    734   void TestDetermineUpdates() {
    735     TestingProfile profile;
    736     MockExtensionDownloaderDelegate delegate;
    737     ExtensionDownloader downloader(&delegate, profile.GetRequestContext());
    738 
    739     // Check passing an empty list of parse results to DetermineUpdates
    740     ManifestFetchData fetch_data(GURL("http://localhost/foo"), 0);
    741     UpdateManifest::Results updates;
    742     std::vector<int> updateable;
    743     downloader.DetermineUpdates(fetch_data, updates, &updateable);
    744     EXPECT_TRUE(updateable.empty());
    745 
    746     // Create two updates - expect that DetermineUpdates will return the first
    747     // one (v1.0 installed, v1.1 available) but not the second one (both
    748     // installed and available at v2.0).
    749     const std::string id1 = id_util::GenerateId("1");
    750     const std::string id2 = id_util::GenerateId("2");
    751     fetch_data.AddExtension(
    752         id1, "1.0.0.0", &kNeverPingedData, kEmptyUpdateUrlData, std::string());
    753     AddParseResult(id1, "1.1", "http://localhost/e1_1.1.crx", &updates);
    754     fetch_data.AddExtension(
    755         id2, "2.0.0.0", &kNeverPingedData, kEmptyUpdateUrlData, std::string());
    756     AddParseResult(id2, "2.0.0.0", "http://localhost/e2_2.0.crx", &updates);
    757 
    758     EXPECT_CALL(delegate, IsExtensionPending(_)).WillRepeatedly(Return(false));
    759     EXPECT_CALL(delegate, GetExtensionExistingVersion(id1, _))
    760         .WillOnce(DoAll(SetArgPointee<1>("1.0.0.0"),
    761                         Return(true)));
    762     EXPECT_CALL(delegate, GetExtensionExistingVersion(id2, _))
    763         .WillOnce(DoAll(SetArgPointee<1>("2.0.0.0"),
    764                         Return(true)));
    765 
    766     downloader.DetermineUpdates(fetch_data, updates, &updateable);
    767     EXPECT_EQ(1u, updateable.size());
    768     EXPECT_EQ(0, updateable[0]);
    769   }
    770 
    771   void TestDetermineUpdatesPending() {
    772     // Create a set of test extensions
    773     ServiceForManifestTests service(prefs_.get());
    774     PendingExtensionManager* pending_extension_manager =
    775         service.pending_extension_manager();
    776     SetupPendingExtensionManagerForTest(3, GURL(), pending_extension_manager);
    777 
    778     TestingProfile profile;
    779     MockExtensionDownloaderDelegate delegate;
    780     ExtensionDownloader downloader(&delegate, profile.GetRequestContext());
    781 
    782     ManifestFetchData fetch_data(GURL("http://localhost/foo"), 0);
    783     UpdateManifest::Results updates;
    784 
    785     std::list<std::string> ids_for_update_check;
    786     pending_extension_manager->GetPendingIdsForUpdateCheck(
    787         &ids_for_update_check);
    788 
    789     std::list<std::string>::const_iterator it;
    790     for (it = ids_for_update_check.begin();
    791          it != ids_for_update_check.end(); ++it) {
    792       fetch_data.AddExtension(*it,
    793                               "1.0.0.0",
    794                               &kNeverPingedData,
    795                               kEmptyUpdateUrlData,
    796                               std::string());
    797       AddParseResult(*it, "1.1", "http://localhost/e1_1.1.crx", &updates);
    798     }
    799 
    800     // The delegate will tell the downloader that all the extensions are
    801     // pending.
    802     EXPECT_CALL(delegate, IsExtensionPending(_)).WillRepeatedly(Return(true));
    803 
    804     std::vector<int> updateable;
    805     downloader.DetermineUpdates(fetch_data, updates, &updateable);
    806     // All the apps should be updateable.
    807     EXPECT_EQ(3u, updateable.size());
    808     for (std::vector<int>::size_type i = 0; i < updateable.size(); ++i) {
    809       EXPECT_EQ(static_cast<int>(i), updateable[i]);
    810     }
    811   }
    812 
    813   void TestMultipleManifestDownloading() {
    814     net::TestURLFetcherFactory factory;
    815     factory.set_remove_fetcher_on_delete(true);
    816     net::TestURLFetcher* fetcher = NULL;
    817     MockService service(prefs_.get());
    818     MockExtensionDownloaderDelegate delegate;
    819     ExtensionDownloader downloader(&delegate, service.request_context());
    820     downloader.manifests_queue_.set_backoff_policy(&kNoBackoffPolicy);
    821 
    822     GURL kUpdateUrl("http://localhost/manifest1");
    823 
    824     scoped_ptr<ManifestFetchData> fetch1(new ManifestFetchData(kUpdateUrl, 0));
    825     scoped_ptr<ManifestFetchData> fetch2(new ManifestFetchData(kUpdateUrl, 0));
    826     scoped_ptr<ManifestFetchData> fetch3(new ManifestFetchData(kUpdateUrl, 0));
    827     scoped_ptr<ManifestFetchData> fetch4(new ManifestFetchData(kUpdateUrl, 0));
    828     ManifestFetchData::PingData zeroDays(0, 0, true);
    829     fetch1->AddExtension(
    830         "1111", "1.0", &zeroDays, kEmptyUpdateUrlData, std::string());
    831     fetch2->AddExtension(
    832         "2222", "2.0", &zeroDays, kEmptyUpdateUrlData, std::string());
    833     fetch3->AddExtension(
    834         "3333", "3.0", &zeroDays, kEmptyUpdateUrlData, std::string());
    835     fetch4->AddExtension(
    836         "4444", "4.0", &zeroDays, kEmptyUpdateUrlData, std::string());
    837 
    838     // This will start the first fetcher and queue the others. The next in queue
    839     // is started as each fetcher receives its response. Note that the fetchers
    840     // don't necessarily run in the order that they are started from here.
    841     GURL fetch1_url = fetch1->full_url();
    842     GURL fetch2_url = fetch2->full_url();
    843     GURL fetch3_url = fetch3->full_url();
    844     GURL fetch4_url = fetch4->full_url();
    845     downloader.StartUpdateCheck(fetch1.Pass());
    846     downloader.StartUpdateCheck(fetch2.Pass());
    847     downloader.StartUpdateCheck(fetch3.Pass());
    848     downloader.StartUpdateCheck(fetch4.Pass());
    849     RunUntilIdle();
    850 
    851     for (int i = 0; i < 4; ++i) {
    852       fetcher = factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
    853       ASSERT_TRUE(fetcher);
    854       ASSERT_TRUE(fetcher->delegate());
    855       EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
    856       EXPECT_FALSE(fetcher->GetOriginalURL().is_empty());
    857 
    858       if (fetcher->GetOriginalURL() == fetch1_url) {
    859         // The first fetch will fail.
    860         EXPECT_CALL(delegate, OnExtensionDownloadFailed(
    861             "1111", ExtensionDownloaderDelegate::MANIFEST_FETCH_FAILED, _, _));
    862         fetcher->set_url(kUpdateUrl);
    863         fetcher->set_status(net::URLRequestStatus());
    864         fetcher->set_response_code(400);
    865         fetcher->delegate()->OnURLFetchComplete(fetcher);
    866         RunUntilIdle();
    867         Mock::VerifyAndClearExpectations(&delegate);
    868         fetch1_url = GURL();
    869       } else if (fetcher->GetOriginalURL() == fetch2_url) {
    870         // The second fetch gets invalid data.
    871         const std::string kInvalidXml = "invalid xml";
    872         EXPECT_CALL(delegate, OnExtensionDownloadFailed(
    873             "2222", ExtensionDownloaderDelegate::MANIFEST_INVALID, _, _))
    874             .WillOnce(InvokeWithoutArgs(
    875                 &delegate,
    876                 &MockExtensionDownloaderDelegate::Quit));
    877         fetcher->set_url(kUpdateUrl);
    878         fetcher->set_status(net::URLRequestStatus());
    879         fetcher->set_response_code(200);
    880         fetcher->SetResponseString(kInvalidXml);
    881         fetcher->delegate()->OnURLFetchComplete(fetcher);
    882         delegate.Wait();
    883         Mock::VerifyAndClearExpectations(&delegate);
    884         fetch2_url = GURL();
    885       } else if (fetcher->GetOriginalURL() == fetch3_url) {
    886         // The third fetcher doesn't have an update available.
    887         const std::string kNoUpdate =
    888             "<?xml version='1.0' encoding='UTF-8'?>"
    889             "<gupdate xmlns='http://www.google.com/update2/response'"
    890             "                protocol='2.0'>"
    891             " <app appid='3333'>"
    892             "  <updatecheck codebase='http://example.com/extension_3.0.0.0.crx'"
    893             "               version='3.0.0.0' prodversionmin='3.0.0.0' />"
    894             " </app>"
    895             "</gupdate>";
    896         EXPECT_CALL(delegate, IsExtensionPending("3333"))
    897             .WillOnce(Return(false));
    898         EXPECT_CALL(delegate, GetExtensionExistingVersion("3333", _))
    899             .WillOnce(DoAll(SetArgPointee<1>("3.0.0.0"),
    900                             Return(true)));
    901         EXPECT_CALL(delegate, OnExtensionDownloadFailed(
    902             "3333", ExtensionDownloaderDelegate::NO_UPDATE_AVAILABLE, _, _))
    903             .WillOnce(InvokeWithoutArgs(
    904                 &delegate,
    905                 &MockExtensionDownloaderDelegate::Quit));
    906         fetcher->set_url(kUpdateUrl);
    907         fetcher->set_status(net::URLRequestStatus());
    908         fetcher->set_response_code(200);
    909         fetcher->SetResponseString(kNoUpdate);
    910         fetcher->delegate()->OnURLFetchComplete(fetcher);
    911         delegate.Wait();
    912         Mock::VerifyAndClearExpectations(&delegate);
    913         fetch3_url = GURL();
    914       } else if (fetcher->GetOriginalURL() == fetch4_url) {
    915         // The last fetcher has an update.
    916         NotificationsObserver observer;
    917         const std::string kUpdateAvailable =
    918             "<?xml version='1.0' encoding='UTF-8'?>"
    919             "<gupdate xmlns='http://www.google.com/update2/response'"
    920             "                protocol='2.0'>"
    921             " <app appid='4444'>"
    922             "  <updatecheck codebase='http://example.com/extension_1.2.3.4.crx'"
    923             "               version='4.0.42.0' prodversionmin='4.0.42.0' />"
    924             " </app>"
    925             "</gupdate>";
    926         EXPECT_CALL(delegate, IsExtensionPending("4444"))
    927             .WillOnce(Return(false));
    928         EXPECT_CALL(delegate, GetExtensionExistingVersion("4444", _))
    929             .WillOnce(DoAll(SetArgPointee<1>("4.0.0.0"),
    930                             Return(true)));
    931         fetcher->set_url(kUpdateUrl);
    932         fetcher->set_status(net::URLRequestStatus());
    933         fetcher->set_response_code(200);
    934         fetcher->SetResponseString(kUpdateAvailable);
    935         fetcher->delegate()->OnURLFetchComplete(fetcher);
    936         observer.Wait();
    937         Mock::VerifyAndClearExpectations(&delegate);
    938 
    939         // Verify that the downloader decided to update this extension.
    940         EXPECT_EQ(1u, observer.UpdatedCount());
    941         EXPECT_TRUE(observer.Updated("4444"));
    942         fetch4_url = GURL();
    943       } else {
    944         ADD_FAILURE() << "Unexpected fetch: " << fetcher->GetOriginalURL();
    945       }
    946     }
    947 
    948     fetcher = factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
    949     if (fetcher)
    950       ADD_FAILURE() << "Unexpected fetch: " << fetcher->GetOriginalURL();
    951   }
    952 
    953   void TestManifestRetryDownloading() {
    954     net::TestURLFetcherFactory factory;
    955     net::TestURLFetcher* fetcher = NULL;
    956     NotificationsObserver observer;
    957     MockService service(prefs_.get());
    958     MockExtensionDownloaderDelegate delegate;
    959     ExtensionDownloader downloader(&delegate, service.request_context());
    960     downloader.manifests_queue_.set_backoff_policy(&kNoBackoffPolicy);
    961 
    962     GURL kUpdateUrl("http://localhost/manifest1");
    963 
    964     scoped_ptr<ManifestFetchData> fetch(new ManifestFetchData(kUpdateUrl, 0));
    965     ManifestFetchData::PingData zeroDays(0, 0, true);
    966     fetch->AddExtension(
    967         "1111", "1.0", &zeroDays, kEmptyUpdateUrlData, std::string());
    968 
    969     // This will start the first fetcher.
    970     downloader.StartUpdateCheck(fetch.Pass());
    971     RunUntilIdle();
    972 
    973     // ExtensionDownloader should retry kMaxRetries times and then fail.
    974     EXPECT_CALL(delegate, OnExtensionDownloadFailed(
    975         "1111", ExtensionDownloaderDelegate::MANIFEST_FETCH_FAILED, _, _));
    976     for (int i = 0; i <= ExtensionDownloader::kMaxRetries; ++i) {
    977       // All fetches will fail.
    978       fetcher = factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
    979       EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
    980       EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
    981       fetcher->set_url(kUpdateUrl);
    982       fetcher->set_status(net::URLRequestStatus());
    983       // Code 5xx causes ExtensionDownloader to retry.
    984       fetcher->set_response_code(500);
    985       fetcher->delegate()->OnURLFetchComplete(fetcher);
    986       RunUntilIdle();
    987     }
    988     Mock::VerifyAndClearExpectations(&delegate);
    989 
    990 
    991     // For response codes that are not in the 5xx range ExtensionDownloader
    992     // should not retry.
    993     fetch.reset(new ManifestFetchData(kUpdateUrl, 0));
    994     fetch->AddExtension(
    995         "1111", "1.0", &zeroDays, kEmptyUpdateUrlData, std::string());
    996 
    997     // This will start the first fetcher.
    998     downloader.StartUpdateCheck(fetch.Pass());
    999     RunUntilIdle();
   1000 
   1001     EXPECT_CALL(delegate, OnExtensionDownloadFailed(
   1002         "1111", ExtensionDownloaderDelegate::MANIFEST_FETCH_FAILED, _, _));
   1003     // The first fetch will fail, and require retrying.
   1004     fetcher = factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
   1005     EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
   1006     EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
   1007     fetcher->set_url(kUpdateUrl);
   1008     fetcher->set_status(net::URLRequestStatus());
   1009     fetcher->set_response_code(500);
   1010     fetcher->delegate()->OnURLFetchComplete(fetcher);
   1011     RunUntilIdle();
   1012 
   1013     // The second fetch will fail with response 400 and should not cause
   1014     // ExtensionDownloader to retry.
   1015     fetcher = factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
   1016     EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
   1017     EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
   1018     fetcher->set_url(kUpdateUrl);
   1019     fetcher->set_status(net::URLRequestStatus());
   1020     fetcher->set_response_code(400);
   1021     fetcher->delegate()->OnURLFetchComplete(fetcher);
   1022     RunUntilIdle();
   1023 
   1024     Mock::VerifyAndClearExpectations(&delegate);
   1025   }
   1026 
   1027   void TestSingleExtensionDownloading(bool pending, bool retry, bool fail) {
   1028     net::TestURLFetcherFactory factory;
   1029     net::TestURLFetcher* fetcher = NULL;
   1030     scoped_ptr<ServiceForDownloadTests> service(
   1031         new ServiceForDownloadTests(prefs_.get()));
   1032     ExtensionUpdater updater(service.get(), service->extension_prefs(),
   1033                              service->pref_service(),
   1034                              service->profile(),
   1035                              kUpdateFrequencySecs,
   1036                              NULL);
   1037     updater.Start();
   1038     MockExtensionDownloaderDelegate delegate;
   1039     delegate.DelegateTo(&updater);
   1040     ResetDownloader(
   1041         &updater,
   1042         new ExtensionDownloader(&delegate, service->request_context()));
   1043     updater.downloader_->extensions_queue_.set_backoff_policy(
   1044         &kNoBackoffPolicy);
   1045 
   1046     GURL test_url("http://localhost/extension.crx");
   1047 
   1048     std::string id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
   1049     std::string hash;
   1050     Version version("0.0.1");
   1051     std::set<int> requests;
   1052     requests.insert(0);
   1053     scoped_ptr<ExtensionDownloader::ExtensionFetch> fetch(
   1054         new ExtensionDownloader::ExtensionFetch(
   1055             id, test_url, hash, version.GetString(), requests));
   1056     updater.downloader_->FetchUpdatedExtension(fetch.Pass());
   1057 
   1058     if (pending) {
   1059       const bool kIsFromSync = true;
   1060       const bool kInstallSilently = true;
   1061       const bool kMarkAcknowledged = false;
   1062       const bool kRemoteInstall = false;
   1063       PendingExtensionManager* pending_extension_manager =
   1064           service->pending_extension_manager();
   1065       pending_extension_manager->AddForTesting(
   1066           PendingExtensionInfo(id,
   1067                                std::string(),
   1068                                test_url,
   1069                                version,
   1070                                &ShouldAlwaysInstall,
   1071                                kIsFromSync,
   1072                                kInstallSilently,
   1073                                Manifest::INTERNAL,
   1074                                Extension::NO_FLAGS,
   1075                                kMarkAcknowledged,
   1076                                kRemoteInstall));
   1077     }
   1078 
   1079     // Call back the ExtensionUpdater with a 200 response and some test data
   1080     base::FilePath extension_file_path(FILE_PATH_LITERAL("/whatever"));
   1081     fetcher = factory.GetFetcherByID(ExtensionDownloader::kExtensionFetcherId);
   1082     EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
   1083     EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
   1084 
   1085     if (retry) {
   1086       // Reply with response code 500 to cause ExtensionDownloader to retry
   1087       fetcher->set_url(test_url);
   1088       fetcher->set_status(net::URLRequestStatus());
   1089       fetcher->set_response_code(500);
   1090       fetcher->delegate()->OnURLFetchComplete(fetcher);
   1091 
   1092       RunUntilIdle();
   1093       fetcher = factory.GetFetcherByID(
   1094           ExtensionDownloader::kExtensionFetcherId);
   1095       EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
   1096       EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
   1097     }
   1098 
   1099     fetcher->set_url(test_url);
   1100     fetcher->set_status(net::URLRequestStatus());
   1101     if (fail) {
   1102       fetcher->set_response_code(404);
   1103       EXPECT_CALL(delegate, OnExtensionDownloadFailed(id, _, _, requests));
   1104     } else {
   1105       fetcher->set_response_code(200);
   1106       fetcher->SetResponseFilePath(extension_file_path);
   1107       EXPECT_CALL(delegate, OnExtensionDownloadFinished(
   1108           id, _, _, _, version.GetString(), _, requests));
   1109     }
   1110     fetcher->delegate()->OnURLFetchComplete(fetcher);
   1111 
   1112     RunUntilIdle();
   1113 
   1114     if (fail) {
   1115       // Don't expect any extension to have been installed.
   1116       EXPECT_TRUE(service->extension_id().empty());
   1117     } else {
   1118       // Expect that ExtensionUpdater asked the mock extensions service to
   1119       // install a file with the test data for the right id.
   1120       EXPECT_EQ(id, service->extension_id());
   1121       base::FilePath tmpfile_path = service->install_path();
   1122       EXPECT_FALSE(tmpfile_path.empty());
   1123       EXPECT_EQ(extension_file_path, tmpfile_path);
   1124     }
   1125   }
   1126 
   1127   // Update a single extension in an environment where the download request
   1128   // initially responds with a 403 status. Expect the fetcher to automatically
   1129   // retry with cookies enabled.
   1130   void TestSingleProtectedExtensionDownloading(bool use_https,
   1131                                                bool fail,
   1132                                                int max_authuser,
   1133                                                int valid_authuser) {
   1134     net::TestURLFetcherFactory factory;
   1135     net::TestURLFetcher* fetcher = NULL;
   1136     scoped_ptr<ServiceForDownloadTests> service(
   1137         new ServiceForDownloadTests(prefs_.get()));
   1138     ExtensionUpdater updater(service.get(), service->extension_prefs(),
   1139                              service->pref_service(),
   1140                              service->profile(),
   1141                              kUpdateFrequencySecs,
   1142                              NULL);
   1143     updater.Start();
   1144     ResetDownloader(
   1145         &updater,
   1146         new ExtensionDownloader(&updater, service->request_context()));
   1147     updater.downloader_->extensions_queue_.set_backoff_policy(
   1148         &kNoBackoffPolicy);
   1149 
   1150     GURL test_url(use_https ? "https://localhost/extension.crx" :
   1151                               "http://localhost/extension.crx");
   1152 
   1153     std::string id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
   1154     std::string hash;
   1155     Version version("0.0.1");
   1156     std::set<int> requests;
   1157     requests.insert(0);
   1158     scoped_ptr<ExtensionDownloader::ExtensionFetch> fetch(
   1159         new ExtensionDownloader::ExtensionFetch(
   1160             id, test_url, hash, version.GetString(), requests));
   1161     updater.downloader_->FetchUpdatedExtension(fetch.Pass());
   1162 
   1163     fetcher = factory.GetFetcherByID(ExtensionDownloader::kExtensionFetcherId);
   1164     EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
   1165     EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
   1166 
   1167     // Fake a 403 response.
   1168     fetcher->set_url(test_url);
   1169     fetcher->set_status(net::URLRequestStatus());
   1170     fetcher->set_response_code(403);
   1171     fetcher->delegate()->OnURLFetchComplete(fetcher);
   1172     RunUntilIdle();
   1173 
   1174     // Verify that the fetcher has been switched to protected download mode
   1175     // so that cookies would be sent with the next request (https only).
   1176     fetcher = factory.GetFetcherByID(ExtensionDownloader::kExtensionFetcherId);
   1177     EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
   1178     if (use_https) {
   1179       EXPECT_TRUE(
   1180           fetcher->GetLoadFlags() == kExpectedLoadFlagsForProtectedDownload);
   1181     } else {
   1182       EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
   1183     }
   1184 
   1185     // Attempt to fetch again after the auth failure.
   1186     bool succeed = !fail;
   1187     if (fail) {
   1188       // Do not simulate incremental authuser retries.
   1189       if (max_authuser == 0) {
   1190         // Fail and verify that the fetch queue is cleared.
   1191         fetcher->set_url(test_url);
   1192         fetcher->set_status(net::URLRequestStatus());
   1193         fetcher->set_response_code(401);
   1194         fetcher->delegate()->OnURLFetchComplete(fetcher);
   1195         RunUntilIdle();
   1196 
   1197         EXPECT_EQ(0U, updater.downloader_->extensions_queue_.active_request());
   1198         return;
   1199       }
   1200 
   1201       // Simulate incremental authuser retries.
   1202       for (int user_index = 0; user_index <= max_authuser; ++user_index) {
   1203         const ExtensionDownloader::ExtensionFetch& fetch =
   1204             *updater.downloader_->extensions_queue_.active_request();
   1205         EXPECT_EQ(user_index, GetAuthUserQueryValue(fetch.url));
   1206         if (user_index == valid_authuser) {
   1207           succeed = true;
   1208           break;
   1209         }
   1210         fetcher =
   1211             factory.GetFetcherByID(ExtensionDownloader::kExtensionFetcherId);
   1212         EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
   1213         fetcher->set_url(fetch.url);
   1214         fetcher->set_status(net::URLRequestStatus());
   1215         fetcher->set_response_code(403);
   1216         fetcher->delegate()->OnURLFetchComplete(fetcher);
   1217         RunUntilIdle();
   1218       }
   1219     }
   1220 
   1221     // Succeed
   1222     if (succeed) {
   1223       fetcher =
   1224           factory.GetFetcherByID(ExtensionDownloader::kExtensionFetcherId);
   1225       EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
   1226       base::FilePath extension_file_path(FILE_PATH_LITERAL("/whatever"));
   1227       fetcher->set_url(test_url);
   1228       fetcher->set_status(net::URLRequestStatus());
   1229       fetcher->set_response_code(200);
   1230       fetcher->SetResponseFilePath(extension_file_path);
   1231       fetcher->delegate()->OnURLFetchComplete(fetcher);
   1232       RunUntilIdle();
   1233 
   1234       // Verify installation would proceed as normal.
   1235       EXPECT_EQ(id, service->extension_id());
   1236       base::FilePath tmpfile_path = service->install_path();
   1237       EXPECT_FALSE(tmpfile_path.empty());
   1238       EXPECT_EQ(extension_file_path, tmpfile_path);
   1239     }
   1240   }
   1241 
   1242   // Two extensions are updated.  If |updates_start_running| is true, the
   1243   // mock extensions service has UpdateExtension(...) return true, and
   1244   // the test is responsible for creating fake CrxInstallers.  Otherwise,
   1245   // UpdateExtension() returns false, signaling install failures.
   1246   void TestMultipleExtensionDownloading(bool updates_start_running) {
   1247     net::TestURLFetcherFactory factory;
   1248     net::TestURLFetcher* fetcher = NULL;
   1249     ServiceForDownloadTests service(prefs_.get());
   1250     ExtensionUpdater updater(
   1251         &service, service.extension_prefs(), service.pref_service(),
   1252         service.profile(), kUpdateFrequencySecs, NULL);
   1253     updater.Start();
   1254     ResetDownloader(
   1255         &updater,
   1256         new ExtensionDownloader(&updater, service.request_context()));
   1257     updater.downloader_->extensions_queue_.set_backoff_policy(
   1258         &kNoBackoffPolicy);
   1259 
   1260     EXPECT_FALSE(updater.crx_install_is_running_);
   1261 
   1262     GURL url1("http://localhost/extension1.crx");
   1263     GURL url2("http://localhost/extension2.crx");
   1264 
   1265     std::string id1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
   1266     std::string id2 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
   1267 
   1268     std::string hash1;
   1269     std::string hash2;
   1270 
   1271     std::string version1 = "0.1";
   1272     std::string version2 = "0.1";
   1273     std::set<int> requests;
   1274     requests.insert(0);
   1275     // Start two fetches
   1276     scoped_ptr<ExtensionDownloader::ExtensionFetch> fetch1(
   1277         new ExtensionDownloader::ExtensionFetch(
   1278             id1, url1, hash1, version1, requests));
   1279     scoped_ptr<ExtensionDownloader::ExtensionFetch> fetch2(
   1280         new ExtensionDownloader::ExtensionFetch(
   1281             id2, url2, hash2, version2, requests));
   1282     updater.downloader_->FetchUpdatedExtension(fetch1.Pass());
   1283     updater.downloader_->FetchUpdatedExtension(fetch2.Pass());
   1284 
   1285     // Make the first fetch complete.
   1286     base::FilePath extension_file_path(FILE_PATH_LITERAL("/whatever"));
   1287 
   1288     fetcher = factory.GetFetcherByID(ExtensionDownloader::kExtensionFetcherId);
   1289     EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
   1290     EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
   1291 
   1292     // We need some CrxInstallers, and CrxInstallers require a real
   1293     // ExtensionService.  Create one on the testing profile.  Any action
   1294     // the CrxInstallers take is on the testing profile's extension
   1295     // service, not on our mock |service|.  This allows us to fake
   1296     // the CrxInstaller actions we want.
   1297     TestingProfile profile;
   1298     static_cast<TestExtensionSystem*>(
   1299         ExtensionSystem::Get(&profile))->
   1300         CreateExtensionService(
   1301             CommandLine::ForCurrentProcess(),
   1302             base::FilePath(),
   1303             false);
   1304     ExtensionService* extension_service =
   1305         ExtensionSystem::Get(&profile)->extension_service();
   1306     extension_service->set_extensions_enabled(true);
   1307     extension_service->set_show_extensions_prompts(false);
   1308 
   1309     scoped_refptr<CrxInstaller> fake_crx1(
   1310         CrxInstaller::CreateSilent(extension_service));
   1311     scoped_refptr<CrxInstaller> fake_crx2(
   1312         CrxInstaller::CreateSilent(extension_service));
   1313 
   1314     if (updates_start_running) {
   1315       // Add fake CrxInstaller to be returned by service.UpdateExtension().
   1316       service.AddFakeCrxInstaller(id1, fake_crx1.get());
   1317       service.AddFakeCrxInstaller(id2, fake_crx2.get());
   1318     } else {
   1319       // If we don't add fake CRX installers, the mock service fakes a failure
   1320       // starting the install.
   1321     }
   1322 
   1323     fetcher->set_url(url1);
   1324     fetcher->set_status(net::URLRequestStatus());
   1325     fetcher->set_response_code(200);
   1326     fetcher->SetResponseFilePath(extension_file_path);
   1327     fetcher->delegate()->OnURLFetchComplete(fetcher);
   1328 
   1329     RunUntilIdle();
   1330 
   1331     // Expect that the service was asked to do an install with the right data.
   1332     base::FilePath tmpfile_path = service.install_path();
   1333     EXPECT_FALSE(tmpfile_path.empty());
   1334     EXPECT_EQ(id1, service.extension_id());
   1335     RunUntilIdle();
   1336 
   1337     // Make sure the second fetch finished and asked the service to do an
   1338     // update.
   1339     base::FilePath extension_file_path2(FILE_PATH_LITERAL("/whatever2"));
   1340     fetcher = factory.GetFetcherByID(ExtensionDownloader::kExtensionFetcherId);
   1341     EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
   1342     EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
   1343 
   1344     fetcher->set_url(url2);
   1345     fetcher->set_status(net::URLRequestStatus());
   1346     fetcher->set_response_code(200);
   1347     fetcher->SetResponseFilePath(extension_file_path2);
   1348     fetcher->delegate()->OnURLFetchComplete(fetcher);
   1349     RunUntilIdle();
   1350 
   1351     if (updates_start_running) {
   1352       EXPECT_TRUE(updater.crx_install_is_running_);
   1353 
   1354       // The second install should not have run, because the first has not
   1355       // sent a notification that it finished.
   1356       EXPECT_EQ(id1, service.extension_id());
   1357 
   1358       // Fake install notice.  This should start the second installation,
   1359       // which will be checked below.
   1360       fake_crx1->NotifyCrxInstallComplete(false);
   1361 
   1362       EXPECT_TRUE(updater.crx_install_is_running_);
   1363     }
   1364 
   1365     EXPECT_EQ(id2, service.extension_id());
   1366     EXPECT_FALSE(service.install_path().empty());
   1367 
   1368     // Make sure the correct crx contents were passed for the update call.
   1369     EXPECT_EQ(extension_file_path2, service.install_path());
   1370 
   1371     if (updates_start_running) {
   1372       EXPECT_TRUE(updater.crx_install_is_running_);
   1373       fake_crx2->NotifyCrxInstallComplete(false);
   1374     }
   1375     EXPECT_FALSE(updater.crx_install_is_running_);
   1376   }
   1377 
   1378   void TestGalleryRequestsWithBrand(bool use_organic_brand_code) {
   1379     google_brand::BrandForTesting brand_for_testing(
   1380         use_organic_brand_code ? "GGLS" : "TEST");
   1381 
   1382     // We want to test a variety of combinations of expected ping conditions for
   1383     // rollcall and active pings.
   1384     int ping_cases[] = { ManifestFetchData::kNeverPinged, 0, 1, 5 };
   1385 
   1386     for (size_t i = 0; i < arraysize(ping_cases); i++) {
   1387       for (size_t j = 0; j < arraysize(ping_cases); j++) {
   1388         for (size_t k = 0; k < 2; k++) {
   1389           int rollcall_ping_days = ping_cases[i];
   1390           int active_ping_days = ping_cases[j];
   1391           // Skip cases where rollcall_ping_days == -1, but
   1392           // active_ping_days > 0, because rollcall_ping_days == -1 means the
   1393           // app was just installed and this is the first update check after
   1394           // installation.
   1395           if (rollcall_ping_days == ManifestFetchData::kNeverPinged &&
   1396               active_ping_days > 0)
   1397             continue;
   1398 
   1399           bool active_bit = k > 0;
   1400           TestGalleryRequests(rollcall_ping_days, active_ping_days, active_bit,
   1401                               !use_organic_brand_code);
   1402           ASSERT_FALSE(HasFailure()) <<
   1403             " rollcall_ping_days=" << ping_cases[i] <<
   1404             " active_ping_days=" << ping_cases[j] <<
   1405             " active_bit=" << active_bit;
   1406         }
   1407       }
   1408     }
   1409   }
   1410 
   1411   // Test requests to both a Google server and a non-google server. This allows
   1412   // us to test various combinations of installed (ie roll call) and active
   1413   // (ie app launch) ping scenarios. The invariant is that each type of ping
   1414   // value should be present at most once per day, and can be calculated based
   1415   // on the delta between now and the last ping time (or in the case of active
   1416   // pings, that delta plus whether the app has been active).
   1417   void TestGalleryRequests(int rollcall_ping_days,
   1418                            int active_ping_days,
   1419                            bool active_bit,
   1420                            bool expect_brand_code) {
   1421     net::TestURLFetcherFactory factory;
   1422 
   1423     // Set up 2 mock extensions, one with a google.com update url and one
   1424     // without.
   1425     prefs_.reset(new TestExtensionPrefs(base::MessageLoopProxy::current()));
   1426     ServiceForManifestTests service(prefs_.get());
   1427     ExtensionList tmp;
   1428     GURL url1("http://clients2.google.com/service/update2/crx");
   1429     GURL url2("http://www.somewebsite.com");
   1430     service.CreateTestExtensions(1, 1, &tmp, &url1.possibly_invalid_spec(),
   1431                                  Manifest::INTERNAL);
   1432     service.CreateTestExtensions(2, 1, &tmp, &url2.possibly_invalid_spec(),
   1433                                  Manifest::INTERNAL);
   1434     EXPECT_EQ(2u, tmp.size());
   1435     service.set_extensions(tmp, ExtensionList());
   1436 
   1437     ExtensionPrefs* prefs = service.extension_prefs();
   1438     const std::string& id = tmp[0]->id();
   1439     Time now = Time::Now();
   1440     if (rollcall_ping_days == 0) {
   1441       prefs->SetLastPingDay(id, now - TimeDelta::FromSeconds(15));
   1442     } else if (rollcall_ping_days > 0) {
   1443       Time last_ping_day = now -
   1444                            TimeDelta::FromDays(rollcall_ping_days) -
   1445                            TimeDelta::FromSeconds(15);
   1446       prefs->SetLastPingDay(id, last_ping_day);
   1447     }
   1448 
   1449     // Store a value for the last day we sent an active ping.
   1450     if (active_ping_days == 0) {
   1451       prefs->SetLastActivePingDay(id, now - TimeDelta::FromSeconds(15));
   1452     } else if (active_ping_days > 0) {
   1453       Time last_active_ping_day = now -
   1454                                   TimeDelta::FromDays(active_ping_days) -
   1455                                   TimeDelta::FromSeconds(15);
   1456       prefs->SetLastActivePingDay(id, last_active_ping_day);
   1457     }
   1458     if (active_bit)
   1459       prefs->SetActiveBit(id, true);
   1460 
   1461     ExtensionUpdater updater(
   1462         &service, service.extension_prefs(), service.pref_service(),
   1463         service.profile(), kUpdateFrequencySecs, NULL);
   1464     ExtensionUpdater::CheckParams params;
   1465     updater.Start();
   1466     updater.CheckNow(params);
   1467 
   1468     // Make the updater do manifest fetching, and note the urls it tries to
   1469     // fetch.
   1470     std::vector<GURL> fetched_urls;
   1471     net::TestURLFetcher* fetcher =
   1472       factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
   1473     EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
   1474     fetched_urls.push_back(fetcher->GetOriginalURL());
   1475 
   1476     fetcher->set_url(fetched_urls[0]);
   1477     fetcher->set_status(net::URLRequestStatus());
   1478     fetcher->set_response_code(500);
   1479     fetcher->SetResponseString(std::string());
   1480     fetcher->delegate()->OnURLFetchComplete(fetcher);
   1481 
   1482     fetcher = factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
   1483     fetched_urls.push_back(fetcher->GetOriginalURL());
   1484 
   1485     // The urls could have been fetched in either order, so use the host to
   1486     // tell them apart and note the query each used.
   1487     std::string url1_query;
   1488     std::string url2_query;
   1489     if (fetched_urls[0].host() == url1.host()) {
   1490       url1_query = fetched_urls[0].query();
   1491       url2_query = fetched_urls[1].query();
   1492     } else if (fetched_urls[0].host() == url2.host()) {
   1493       url1_query = fetched_urls[1].query();
   1494       url2_query = fetched_urls[0].query();
   1495     } else {
   1496       NOTREACHED();
   1497     }
   1498 
   1499     // First make sure the non-google query had no ping parameter.
   1500     std::string search_string = "ping%3D";
   1501     EXPECT_TRUE(url2_query.find(search_string) == std::string::npos);
   1502 
   1503     // Now make sure the google query had the correct ping parameter.
   1504     bool ping_expected = false;
   1505     bool did_rollcall = false;
   1506     if (rollcall_ping_days != 0) {
   1507       search_string += "r%253D" + base::IntToString(rollcall_ping_days);
   1508       did_rollcall = true;
   1509       ping_expected = true;
   1510     }
   1511     if (active_bit && active_ping_days != 0) {
   1512       if (did_rollcall)
   1513         search_string += "%2526";
   1514       search_string += "a%253D" + base::IntToString(active_ping_days);
   1515       ping_expected = true;
   1516     }
   1517     bool ping_found = url1_query.find(search_string) != std::string::npos;
   1518     EXPECT_EQ(ping_expected, ping_found) << "query was: " << url1_query
   1519         << " was looking for " << search_string;
   1520 
   1521     // Make sure the non-google query has no brand parameter.
   1522     const std::string brand_string = "brand%3D";
   1523     EXPECT_TRUE(url2_query.find(brand_string) == std::string::npos);
   1524 
   1525 #if defined(GOOGLE_CHROME_BUILD)
   1526     // Make sure the google query has a brand parameter, but only if the
   1527     // brand is non-organic.
   1528     if (expect_brand_code) {
   1529       EXPECT_TRUE(url1_query.find(brand_string) != std::string::npos);
   1530     } else {
   1531       EXPECT_TRUE(url1_query.find(brand_string) == std::string::npos);
   1532     }
   1533 #else
   1534     // Chromium builds never add the brand to the parameter, even for google
   1535     // queries.
   1536     EXPECT_TRUE(url1_query.find(brand_string) == std::string::npos);
   1537 #endif
   1538 
   1539     RunUntilIdle();
   1540   }
   1541 
   1542   // This makes sure that the extension updater properly stores the results
   1543   // of a <daystart> tag from a manifest fetch in one of two cases: 1) This is
   1544   // the first time we fetched the extension, or 2) We sent a ping value of
   1545   // >= 1 day for the extension.
   1546   void TestHandleManifestResults() {
   1547     ServiceForManifestTests service(prefs_.get());
   1548     GURL update_url("http://www.google.com/manifest");
   1549     ExtensionList tmp;
   1550     service.CreateTestExtensions(1, 1, &tmp, &update_url.spec(),
   1551                                  Manifest::INTERNAL);
   1552     service.set_extensions(tmp, ExtensionList());
   1553 
   1554     ExtensionUpdater updater(
   1555         &service, service.extension_prefs(), service.pref_service(),
   1556         service.profile(), kUpdateFrequencySecs, NULL);
   1557     updater.Start();
   1558     ResetDownloader(
   1559         &updater,
   1560         new ExtensionDownloader(&updater, service.request_context()));
   1561 
   1562     ManifestFetchData fetch_data(update_url, 0);
   1563     const Extension* extension = tmp[0].get();
   1564     fetch_data.AddExtension(extension->id(),
   1565                             extension->VersionString(),
   1566                             &kNeverPingedData,
   1567                             kEmptyUpdateUrlData,
   1568                             std::string());
   1569     UpdateManifest::Results results;
   1570     results.daystart_elapsed_seconds = 750;
   1571 
   1572     updater.downloader_->HandleManifestResults(fetch_data, &results);
   1573     Time last_ping_day =
   1574         service.extension_prefs()->LastPingDay(extension->id());
   1575     EXPECT_FALSE(last_ping_day.is_null());
   1576     int64 seconds_diff = (Time::Now() - last_ping_day).InSeconds();
   1577     EXPECT_LT(seconds_diff - results.daystart_elapsed_seconds, 5);
   1578   }
   1579 
   1580  protected:
   1581   scoped_ptr<TestExtensionPrefs> prefs_;
   1582 
   1583  private:
   1584   content::TestBrowserThreadBundle thread_bundle_;
   1585   content::InProcessUtilityThreadHelper in_process_utility_thread_helper_;
   1586 
   1587 #if defined OS_CHROMEOS
   1588   chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
   1589   chromeos::ScopedTestCrosSettings test_cros_settings_;
   1590   chromeos::ScopedTestUserManager test_user_manager_;
   1591 #endif
   1592 };
   1593 
   1594 // Because we test some private methods of ExtensionUpdater, it's easier for the
   1595 // actual test code to live in ExtenionUpdaterTest methods instead of TEST_F
   1596 // subclasses where friendship with ExtenionUpdater is not inherited.
   1597 
   1598 TEST_F(ExtensionUpdaterTest, TestExtensionUpdateCheckRequests) {
   1599   TestExtensionUpdateCheckRequests(false);
   1600 }
   1601 
   1602 TEST_F(ExtensionUpdaterTest, TestExtensionUpdateCheckRequestsPending) {
   1603   TestExtensionUpdateCheckRequests(true);
   1604 }
   1605 
   1606 TEST_F(ExtensionUpdaterTest, TestUpdateUrlData) {
   1607   TestUpdateUrlDataEmpty();
   1608   TestUpdateUrlDataSimple();
   1609   TestUpdateUrlDataCompound();
   1610   TestUpdateUrlDataFromGallery(
   1611       extension_urls::GetWebstoreUpdateUrl().spec());
   1612 }
   1613 
   1614 TEST_F(ExtensionUpdaterTest, TestInstallSource) {
   1615   TestInstallSource();
   1616 }
   1617 
   1618 TEST_F(ExtensionUpdaterTest, TestDetermineUpdates) {
   1619   TestDetermineUpdates();
   1620 }
   1621 
   1622 TEST_F(ExtensionUpdaterTest, TestDetermineUpdatesPending) {
   1623   TestDetermineUpdatesPending();
   1624 }
   1625 
   1626 TEST_F(ExtensionUpdaterTest, TestMultipleManifestDownloading) {
   1627   TestMultipleManifestDownloading();
   1628 }
   1629 
   1630 TEST_F(ExtensionUpdaterTest, TestSingleExtensionDownloading) {
   1631   TestSingleExtensionDownloading(false, false, false);
   1632 }
   1633 
   1634 TEST_F(ExtensionUpdaterTest, TestSingleExtensionDownloadingPending) {
   1635   TestSingleExtensionDownloading(true, false, false);
   1636 }
   1637 
   1638 TEST_F(ExtensionUpdaterTest, TestSingleExtensionDownloadingWithRetry) {
   1639   TestSingleExtensionDownloading(false, true, false);
   1640 }
   1641 
   1642 TEST_F(ExtensionUpdaterTest, TestSingleExtensionDownloadingPendingWithRetry) {
   1643   TestSingleExtensionDownloading(true, true, false);
   1644 }
   1645 
   1646 TEST_F(ExtensionUpdaterTest, TestSingleExtensionDownloadingFailure) {
   1647   TestSingleExtensionDownloading(false, false, true);
   1648 }
   1649 
   1650 TEST_F(ExtensionUpdaterTest, TestSingleExtensionDownloadingFailureWithRetry) {
   1651   TestSingleExtensionDownloading(false, true, true);
   1652 }
   1653 
   1654 TEST_F(ExtensionUpdaterTest, TestSingleExtensionDownloadingFailurePending) {
   1655   TestSingleExtensionDownloading(true, false, true);
   1656 }
   1657 
   1658 TEST_F(ExtensionUpdaterTest, SingleProtectedExtensionDownloading) {
   1659   TestSingleProtectedExtensionDownloading(true, false, 0, 0);
   1660 }
   1661 
   1662 TEST_F(ExtensionUpdaterTest, SingleProtectedExtensionDownloadingFailure) {
   1663   TestSingleProtectedExtensionDownloading(true, true, 0, 0);
   1664 }
   1665 
   1666 TEST_F(ExtensionUpdaterTest, SingleProtectedExtensionDownloadingNoHTTPS) {
   1667   TestSingleProtectedExtensionDownloading(false, false, 0, 0);
   1668 }
   1669 
   1670 TEST_F(ExtensionUpdaterTest,
   1671        SingleProtectedExtensionDownloadingWithNonDefaultAuthUser1) {
   1672   TestSingleProtectedExtensionDownloading(true, true, 2, 1);
   1673 }
   1674 
   1675 TEST_F(ExtensionUpdaterTest,
   1676        SingleProtectedExtensionDownloadingWithNonDefaultAuthUser2) {
   1677   TestSingleProtectedExtensionDownloading(true, true, 2, 2);
   1678 }
   1679 
   1680 TEST_F(ExtensionUpdaterTest,
   1681        SingleProtectedExtensionDownloadingAuthUserExhaustionFailure) {
   1682   TestSingleProtectedExtensionDownloading(true, true, 2, 5);
   1683 }
   1684 
   1685 TEST_F(ExtensionUpdaterTest, TestMultipleExtensionDownloadingUpdatesFail) {
   1686   TestMultipleExtensionDownloading(false);
   1687 }
   1688 TEST_F(ExtensionUpdaterTest, TestMultipleExtensionDownloadingUpdatesSucceed) {
   1689   TestMultipleExtensionDownloading(true);
   1690 }
   1691 
   1692 TEST_F(ExtensionUpdaterTest, TestManifestRetryDownloading) {
   1693   TestManifestRetryDownloading();
   1694 }
   1695 
   1696 TEST_F(ExtensionUpdaterTest, TestGalleryRequestsWithOrganicBrand) {
   1697   TestGalleryRequestsWithBrand(true);
   1698 }
   1699 
   1700 TEST_F(ExtensionUpdaterTest, TestGalleryRequestsWithNonOrganicBrand) {
   1701   TestGalleryRequestsWithBrand(false);
   1702 }
   1703 
   1704 TEST_F(ExtensionUpdaterTest, TestHandleManifestResults) {
   1705   TestHandleManifestResults();
   1706 }
   1707 
   1708 TEST_F(ExtensionUpdaterTest, TestNonAutoUpdateableLocations) {
   1709   net::TestURLFetcherFactory factory;
   1710   ServiceForManifestTests service(prefs_.get());
   1711   ExtensionUpdater updater(&service, service.extension_prefs(),
   1712                            service.pref_service(), service.profile(),
   1713                            kUpdateFrequencySecs, NULL);
   1714   MockExtensionDownloaderDelegate delegate;
   1715   // Set the downloader directly, so that all its events end up in the mock
   1716   // |delegate|.
   1717   ExtensionDownloader* downloader =
   1718       new ExtensionDownloader(&delegate, service.request_context());
   1719   ResetDownloader(&updater, downloader);
   1720 
   1721   // Non-internal non-external extensions should be rejected.
   1722   ExtensionList extensions;
   1723   service.CreateTestExtensions(1, 1, &extensions, NULL,
   1724                                Manifest::INVALID_LOCATION);
   1725   service.CreateTestExtensions(2, 1, &extensions, NULL, Manifest::INTERNAL);
   1726   ASSERT_EQ(2u, extensions.size());
   1727   const std::string& updateable_id = extensions[1]->id();
   1728 
   1729   // These expectations fail if the delegate's methods are invoked for the
   1730   // first extension, which has a non-matching id.
   1731   EXPECT_CALL(delegate, GetUpdateUrlData(updateable_id)).WillOnce(Return(""));
   1732   EXPECT_CALL(delegate, GetPingDataForExtension(updateable_id, _));
   1733 
   1734   service.set_extensions(extensions, ExtensionList());
   1735   ExtensionUpdater::CheckParams params;
   1736   updater.Start();
   1737   updater.CheckNow(params);
   1738 }
   1739 
   1740 TEST_F(ExtensionUpdaterTest, TestUpdatingDisabledExtensions) {
   1741   net::TestURLFetcherFactory factory;
   1742   ServiceForManifestTests service(prefs_.get());
   1743   ExtensionUpdater updater(&service, service.extension_prefs(),
   1744                            service.pref_service(), service.profile(),
   1745                            kUpdateFrequencySecs, NULL);
   1746   MockExtensionDownloaderDelegate delegate;
   1747   // Set the downloader directly, so that all its events end up in the mock
   1748   // |delegate|.
   1749   ExtensionDownloader* downloader =
   1750       new ExtensionDownloader(&delegate, service.request_context());
   1751   ResetDownloader(&updater, downloader);
   1752 
   1753   // Non-internal non-external extensions should be rejected.
   1754   ExtensionList enabled_extensions;
   1755   ExtensionList disabled_extensions;
   1756   service.CreateTestExtensions(1, 1, &enabled_extensions, NULL,
   1757       Manifest::INTERNAL);
   1758   service.CreateTestExtensions(2, 1, &disabled_extensions, NULL,
   1759       Manifest::INTERNAL);
   1760   ASSERT_EQ(1u, enabled_extensions.size());
   1761   ASSERT_EQ(1u, disabled_extensions.size());
   1762   const std::string& enabled_id = enabled_extensions[0]->id();
   1763   const std::string& disabled_id = disabled_extensions[0]->id();
   1764 
   1765   // We expect that both enabled and disabled extensions are auto-updated.
   1766   EXPECT_CALL(delegate, GetUpdateUrlData(enabled_id)).WillOnce(Return(""));
   1767   EXPECT_CALL(delegate, GetPingDataForExtension(enabled_id, _));
   1768   EXPECT_CALL(delegate, GetUpdateUrlData(disabled_id)).WillOnce(Return(""));
   1769   EXPECT_CALL(delegate, GetPingDataForExtension(disabled_id, _));
   1770 
   1771   service.set_extensions(enabled_extensions, disabled_extensions);
   1772   ExtensionUpdater::CheckParams params;
   1773   updater.Start();
   1774   updater.CheckNow(params);
   1775 }
   1776 
   1777 TEST_F(ExtensionUpdaterTest, TestManifestFetchesBuilderAddExtension) {
   1778   net::TestURLFetcherFactory factory;
   1779   MockService service(prefs_.get());
   1780   MockExtensionDownloaderDelegate delegate;
   1781   scoped_ptr<ExtensionDownloader> downloader(
   1782       new ExtensionDownloader(&delegate, service.request_context()));
   1783   EXPECT_EQ(0u, ManifestFetchersCount(downloader.get()));
   1784 
   1785   // First, verify that adding valid extensions does invoke the callbacks on
   1786   // the delegate.
   1787   std::string id = id_util::GenerateId("foo");
   1788   EXPECT_CALL(delegate, GetPingDataForExtension(id, _)).WillOnce(Return(false));
   1789   EXPECT_TRUE(
   1790       downloader->AddPendingExtension(id, GURL("http://example.com/update"),
   1791                                       0));
   1792   downloader->StartAllPending(NULL);
   1793   Mock::VerifyAndClearExpectations(&delegate);
   1794   EXPECT_EQ(1u, ManifestFetchersCount(downloader.get()));
   1795 
   1796   // Extensions with invalid update URLs should be rejected.
   1797   id = id_util::GenerateId("foo2");
   1798   EXPECT_FALSE(
   1799       downloader->AddPendingExtension(id, GURL("http:google.com:foo"), 0));
   1800   downloader->StartAllPending(NULL);
   1801   EXPECT_EQ(1u, ManifestFetchersCount(downloader.get()));
   1802 
   1803   // Extensions with empty IDs should be rejected.
   1804   EXPECT_FALSE(downloader->AddPendingExtension(std::string(), GURL(), 0));
   1805   downloader->StartAllPending(NULL);
   1806   EXPECT_EQ(1u, ManifestFetchersCount(downloader.get()));
   1807 
   1808   // TODO(akalin): Test that extensions with empty update URLs
   1809   // converted from user scripts are rejected.
   1810 
   1811   // Reset the ExtensionDownloader so that it drops the current fetcher.
   1812   downloader.reset(
   1813       new ExtensionDownloader(&delegate, service.request_context()));
   1814   EXPECT_EQ(0u, ManifestFetchersCount(downloader.get()));
   1815 
   1816   // Extensions with empty update URLs should have a default one
   1817   // filled in.
   1818   id = id_util::GenerateId("foo3");
   1819   EXPECT_CALL(delegate, GetPingDataForExtension(id, _)).WillOnce(Return(false));
   1820   EXPECT_TRUE(downloader->AddPendingExtension(id, GURL(), 0));
   1821   downloader->StartAllPending(NULL);
   1822   EXPECT_EQ(1u, ManifestFetchersCount(downloader.get()));
   1823 
   1824   net::TestURLFetcher* fetcher =
   1825       factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
   1826   ASSERT_TRUE(fetcher);
   1827   EXPECT_FALSE(fetcher->GetOriginalURL().is_empty());
   1828 }
   1829 
   1830 TEST_F(ExtensionUpdaterTest, TestStartUpdateCheckMemory) {
   1831   net::TestURLFetcherFactory factory;
   1832   MockService service(prefs_.get());
   1833   MockExtensionDownloaderDelegate delegate;
   1834   ExtensionDownloader downloader(&delegate, service.request_context());
   1835 
   1836   StartUpdateCheck(&downloader, new ManifestFetchData(GURL(), 0));
   1837   // This should delete the newly-created ManifestFetchData.
   1838   StartUpdateCheck(&downloader, new ManifestFetchData(GURL(), 0));
   1839   // This should add into |manifests_pending_|.
   1840   StartUpdateCheck(&downloader, new ManifestFetchData(GURL(
   1841       GURL("http://www.google.com")), 0));
   1842   // The dtor of |downloader| should delete the pending fetchers.
   1843 }
   1844 
   1845 TEST_F(ExtensionUpdaterTest, TestCheckSoon) {
   1846   ServiceForManifestTests service(prefs_.get());
   1847   net::TestURLFetcherFactory factory;
   1848   ExtensionUpdater updater(
   1849       &service, service.extension_prefs(), service.pref_service(),
   1850       service.profile(), kUpdateFrequencySecs, NULL);
   1851   EXPECT_FALSE(updater.WillCheckSoon());
   1852   updater.Start();
   1853   EXPECT_FALSE(updater.WillCheckSoon());
   1854   updater.CheckSoon();
   1855   EXPECT_TRUE(updater.WillCheckSoon());
   1856   updater.CheckSoon();
   1857   EXPECT_TRUE(updater.WillCheckSoon());
   1858   RunUntilIdle();
   1859   EXPECT_FALSE(updater.WillCheckSoon());
   1860   updater.CheckSoon();
   1861   EXPECT_TRUE(updater.WillCheckSoon());
   1862   updater.Stop();
   1863   EXPECT_FALSE(updater.WillCheckSoon());
   1864 }
   1865 
   1866 // TODO(asargent) - (http://crbug.com/12780) add tests for:
   1867 // -prodversionmin (shouldn't update if browser version too old)
   1868 // -manifests & updates arriving out of order / interleaved
   1869 // -malformed update url (empty, file://, has query, has a # fragment, etc.)
   1870 // -An extension gets uninstalled while updates are in progress (so it doesn't
   1871 //  "come back from the dead")
   1872 // -An extension gets manually updated to v3 while we're downloading v2 (ie
   1873 //  you don't get downgraded accidentally)
   1874 // -An update manifest mentions multiple updates
   1875 
   1876 }  // namespace extensions
   1877