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