1bee9932bab44713b74b422b4b759b49f5a222977Bo Liu// Copyright 2014 The Chromium Authors. All rights reserved.
2bee9932bab44713b74b422b4b759b49f5a222977Bo Liu// Use of this source code is governed by a BSD-style license that can be
3bee9932bab44713b74b422b4b759b49f5a222977Bo Liu// found in the LICENSE file.
4bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
5bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "chrome/browser/safe_browsing/last_download_finder.h"
6bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
7bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include <string>
8bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include <vector>
9bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
10bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "base/bind.h"
11bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "base/callback.h"
12bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "base/file_util.h"
13bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "base/run_loop.h"
14bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "base/strings/string_number_conversions.h"
15bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "base/strings/utf_string_conversions.h"
16bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "chrome/browser/history/chrome_history_client.h"
17bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "chrome/browser/history/chrome_history_client_factory.h"
18bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "chrome/browser/history/download_row.h"
19bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "chrome/browser/history/history_service.h"
20bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "chrome/browser/history/history_service_factory.h"
21bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "chrome/browser/history/web_history_service_factory.h"
22bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "chrome/browser/prefs/browser_prefs.h"
23bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "chrome/browser/profiles/profile_manager.h"
24bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "chrome/common/chrome_constants.h"
25bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "chrome/common/pref_names.h"
26bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "chrome/common/safe_browsing/csd.pb.h"
27bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "chrome/test/base/testing_browser_process.h"
28bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "chrome/test/base/testing_pref_service_syncable.h"
29bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "chrome/test/base/testing_profile.h"
30bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "chrome/test/base/testing_profile_manager.h"
31bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "content/public/test/test_browser_thread_bundle.h"
32bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "content/public/test/test_utils.h"
33bee9932bab44713b74b422b4b759b49f5a222977Bo Liu#include "testing/gtest/include/gtest/gtest.h"
34bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
35bee9932bab44713b74b422b4b759b49f5a222977Bo Liunamespace {
36bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
37bee9932bab44713b74b422b4b759b49f5a222977Bo Liu// A BrowserContextKeyedServiceFactory::TestingFactoryFunction that creates a
38bee9932bab44713b74b422b4b759b49f5a222977Bo Liu// HistoryService for a TestingProfile.
39bee9932bab44713b74b422b4b759b49f5a222977Bo LiuKeyedService* BuildHistoryService(content::BrowserContext* context) {
40bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  TestingProfile* profile = static_cast<TestingProfile*>(context);
41bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
42bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // Delete the file before creating the service.
43bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  base::FilePath history_path(
44bee9932bab44713b74b422b4b759b49f5a222977Bo Liu      profile->GetPath().Append(chrome::kHistoryFilename));
45bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  if (!base::DeleteFile(history_path, false) ||
46bee9932bab44713b74b422b4b759b49f5a222977Bo Liu      base::PathExists(history_path)) {
47bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    ADD_FAILURE() << "failed to delete history db file "
48bee9932bab44713b74b422b4b759b49f5a222977Bo Liu                  << history_path.value();
49bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    return NULL;
50bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  }
51bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
52bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  HistoryService* history_service = new HistoryService(
53bee9932bab44713b74b422b4b759b49f5a222977Bo Liu      ChromeHistoryClientFactory::GetForProfile(profile), profile);
54bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  if (history_service->Init(profile->GetPath()))
55bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    return history_service;
56bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
57bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  ADD_FAILURE() << "failed to initialize history service";
58bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  delete history_service;
59bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  return NULL;
60bee9932bab44713b74b422b4b759b49f5a222977Bo Liu}
61bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
62bee9932bab44713b74b422b4b759b49f5a222977Bo Liu}  // namespace
63bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
64bee9932bab44713b74b422b4b759b49f5a222977Bo Liuclass LastDownloadFinderTest : public testing::Test {
65bee9932bab44713b74b422b4b759b49f5a222977Bo Liu public:
66bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  void NeverCalled(scoped_ptr<
67bee9932bab44713b74b422b4b759b49f5a222977Bo Liu      safe_browsing::ClientIncidentReport_DownloadDetails> download) {
68bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    FAIL();
69bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  }
70bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
71bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // Creates a new profile that participates in safe browsing and adds a
72bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // download to its history.
73bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  void CreateProfileWithDownload() {
74bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    TestingProfile* profile = CreateProfile(SAFE_BROWSING_OPT_IN);
75bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    HistoryService* history_service =
76bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS);
77bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    history_service->CreateDownload(
78bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        CreateTestDownloadRow(),
79bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        base::Bind(&LastDownloadFinderTest::OnDownloadCreated,
80bee9932bab44713b74b422b4b759b49f5a222977Bo Liu                   base::Unretained(this)));
81bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  }
82bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
83bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // safe_browsing::LastDownloadFinder::LastDownloadCallback implementation that
84bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // passes the found download to |result| and then runs a closure.
85bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  void OnLastDownload(
86bee9932bab44713b74b422b4b759b49f5a222977Bo Liu      scoped_ptr<safe_browsing::ClientIncidentReport_DownloadDetails>* result,
87bee9932bab44713b74b422b4b759b49f5a222977Bo Liu      const base::Closure& quit_closure,
88bee9932bab44713b74b422b4b759b49f5a222977Bo Liu      scoped_ptr<safe_browsing::ClientIncidentReport_DownloadDetails>
89bee9932bab44713b74b422b4b759b49f5a222977Bo Liu          download) {
90bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    *result = download.Pass();
91bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    quit_closure.Run();
92bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  }
93bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
94bee9932bab44713b74b422b4b759b49f5a222977Bo Liu protected:
95bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // A type for specifying whether or not a profile created by CreateProfile
96bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // participates in safe browsing.
97bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  enum SafeBrowsingDisposition {
98bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    SAFE_BROWSING_OPT_OUT,
99bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    SAFE_BROWSING_OPT_IN,
100bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  };
101bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
102bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  LastDownloadFinderTest() : profile_number_() {}
103bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
104bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  virtual void SetUp() OVERRIDE {
105bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    testing::Test::SetUp();
106bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    profile_manager_.reset(
107bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
108bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    ASSERT_TRUE(profile_manager_->SetUp());
109bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  }
110bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
111bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  virtual void TearDown() OVERRIDE {
112bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    // Shut down the history service on all profiles.
113bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    std::vector<Profile*> profiles(
114bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        profile_manager_->profile_manager()->GetLoadedProfiles());
115bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    for (size_t i = 0; i < profiles.size(); ++i) {
116bee9932bab44713b74b422b4b759b49f5a222977Bo Liu      profiles[0]->AsTestingProfile()->DestroyHistoryService();
117bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    }
118bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    profile_manager_.reset();
119bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    TestingBrowserProcess::DeleteInstance();
120bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    testing::Test::TearDown();
121bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  }
122bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
123bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  TestingProfile* CreateProfile(SafeBrowsingDisposition safe_browsing_opt_in) {
124bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    std::string profile_name("profile");
125bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    profile_name.append(base::IntToString(++profile_number_));
126bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
127bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    // Set up keyed service factories.
128bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    TestingProfile::TestingFactories factories;
129bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    // Build up a custom history service.
130bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    factories.push_back(std::make_pair(HistoryServiceFactory::GetInstance(),
131bee9932bab44713b74b422b4b759b49f5a222977Bo Liu                                       &BuildHistoryService));
132bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    // Suppress WebHistoryService since it makes network requests.
133bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    factories.push_back(std::make_pair(
134bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        WebHistoryServiceFactory::GetInstance(),
135bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        static_cast<BrowserContextKeyedServiceFactory::TestingFactoryFunction>(
136bee9932bab44713b74b422b4b759b49f5a222977Bo Liu            NULL)));
137bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
138bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    // Create prefs for the profile with safe browsing enabled or not.
139bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    scoped_ptr<TestingPrefServiceSyncable> prefs(
140bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        new TestingPrefServiceSyncable);
141bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    chrome::RegisterUserProfilePrefs(prefs->registry());
142bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    prefs->SetBoolean(prefs::kSafeBrowsingEnabled,
143bee9932bab44713b74b422b4b759b49f5a222977Bo Liu                      safe_browsing_opt_in == SAFE_BROWSING_OPT_IN);
144bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
145bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    TestingProfile* profile = profile_manager_->CreateTestingProfile(
146bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        profile_name,
147bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        prefs.PassAs<PrefServiceSyncable>(),
148bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        base::UTF8ToUTF16(profile_name),  // user_name
149bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        0,                                // avatar_id
150bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        std::string(),                    // supervised_user_id
151bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        factories);
152bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
153bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    return profile;
154bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  }
155bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
156bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  void AddDownload(Profile* profile, const history::DownloadRow& download) {
157bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    base::RunLoop run_loop;
158bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
159bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    HistoryService* history_service =
160bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS);
161bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    history_service->CreateDownload(
162bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        download,
163bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        base::Bind(&LastDownloadFinderTest::ContinueOnDownloadCreated,
164bee9932bab44713b74b422b4b759b49f5a222977Bo Liu                   base::Unretained(this),
165bee9932bab44713b74b422b4b759b49f5a222977Bo Liu                   run_loop.QuitClosure()));
166bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    run_loop.Run();
167bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  }
168bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
169bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // Wait for the history backend thread to process any outstanding tasks.
170bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // This is needed because HistoryService::QueryDownloads uses PostTaskAndReply
171bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // to do work on the backend thread and then invoke the caller's callback on
172bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // the originating thread. The PostTaskAndReplyRelay holds a reference to the
173bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // backend until its RunReplyAndSelfDestruct is called on the originating
174bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // thread. This reference MUST be released (on the originating thread,
175bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // remember) _before_ calling DestroyHistoryService in TearDown(). See the
176bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // giant comment in HistoryService::Cleanup explaining where the backend's
177bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // dtor must be run.
178bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  void FlushHistoryBackend(Profile* profile) {
179bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    base::RunLoop run_loop;
180bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS)
181bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        ->FlushForTest(run_loop.QuitClosure());
182bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    run_loop.Run();
183bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    // Then make sure anything bounced back to the main thread has been handled.
184bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    base::RunLoop().RunUntilIdle();
185bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  }
186bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
187bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // Runs the last download finder on all loaded profiles, returning the found
188bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // download or an empty pointer if none was found.
189bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  scoped_ptr<safe_browsing::ClientIncidentReport_DownloadDetails>
190bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  RunLastDownloadFinder() {
191bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    base::RunLoop run_loop;
192bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
193bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    scoped_ptr<safe_browsing::ClientIncidentReport_DownloadDetails>
194bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        last_download;
195bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
196bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    scoped_ptr<safe_browsing::LastDownloadFinder> finder(
197bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        safe_browsing::LastDownloadFinder::Create(
198bee9932bab44713b74b422b4b759b49f5a222977Bo Liu            base::Bind(&LastDownloadFinderTest::OnLastDownload,
199bee9932bab44713b74b422b4b759b49f5a222977Bo Liu                       base::Unretained(this),
200bee9932bab44713b74b422b4b759b49f5a222977Bo Liu                       &last_download,
201bee9932bab44713b74b422b4b759b49f5a222977Bo Liu                       run_loop.QuitClosure())));
202bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
203bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    if (finder)
204bee9932bab44713b74b422b4b759b49f5a222977Bo Liu      run_loop.Run();
205bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
206bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    return last_download.Pass();
207bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  }
208bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
209bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  history::DownloadRow CreateTestDownloadRow() {
210bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    base::Time now(base::Time::Now());
211bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    return history::DownloadRow(
212bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        base::FilePath(FILE_PATH_LITERAL("spam.exe")),
213bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        base::FilePath(FILE_PATH_LITERAL("spam.exe")),
214bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        std::vector<GURL>(1, GURL("http://www.google.com")),  // url_chain
215bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        GURL(),                                               // referrer
216bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        "application/octet-stream",                           // mime_type
217bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        "application/octet-stream",                   // original_mime_type
218bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        now - base::TimeDelta::FromMinutes(10),       // start
219bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        now - base::TimeDelta::FromMinutes(9),        // end
220bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        std::string(),                                // etag
221bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        std::string(),                                // last_modified
222bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        47LL,                                         // received
223bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        47LL,                                         // total
224bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        content::DownloadItem::COMPLETE,              // download_state
225bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,  // danger_type
226bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        content::DOWNLOAD_INTERRUPT_REASON_NONE,      // interrupt_reason,
227bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        1,                                            // id
228bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        false,                                        // download_opened
229bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        std::string(),                                // ext_id
230bee9932bab44713b74b422b4b759b49f5a222977Bo Liu        std::string());                               // ext_name
231bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  }
232bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
233bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  void ExpectNoDownloadFound(scoped_ptr<
234bee9932bab44713b74b422b4b759b49f5a222977Bo Liu      safe_browsing::ClientIncidentReport_DownloadDetails> download) {
235bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    EXPECT_FALSE(download);
236bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  }
237bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
238bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  void ExpectFoundTestDownload(scoped_ptr<
239bee9932bab44713b74b422b4b759b49f5a222977Bo Liu      safe_browsing::ClientIncidentReport_DownloadDetails> download) {
240bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    ASSERT_TRUE(download);
241bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  }
242bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
243bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  content::TestBrowserThreadBundle browser_thread_bundle_;
244bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  scoped_ptr<TestingProfileManager> profile_manager_;
245bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
246bee9932bab44713b74b422b4b759b49f5a222977Bo Liu private:
247bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // A HistoryService::DownloadCreateCallback that asserts that the download was
248bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // created and runs |closure|.
249bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  void ContinueOnDownloadCreated(const base::Closure& closure, bool created) {
250bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    ASSERT_TRUE(created);
251bee9932bab44713b74b422b4b759b49f5a222977Bo Liu    closure.Run();
252bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  }
253bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
254bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // A HistoryService::DownloadCreateCallback that asserts that the download was
255bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // created.
256bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  void OnDownloadCreated(bool created) { ASSERT_TRUE(created); }
257bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
258bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  int profile_number_;
259bee9932bab44713b74b422b4b759b49f5a222977Bo Liu};
260bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
261bee9932bab44713b74b422b4b759b49f5a222977Bo Liu// Tests that nothing happens if there are no profiles at all.
262bee9932bab44713b74b422b4b759b49f5a222977Bo LiuTEST_F(LastDownloadFinderTest, NoProfiles) {
263bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  ExpectNoDownloadFound(RunLastDownloadFinder());
264bee9932bab44713b74b422b4b759b49f5a222977Bo Liu}
265bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
266bee9932bab44713b74b422b4b759b49f5a222977Bo Liu// Tests that nothing happens other than the callback being invoked if there are
267bee9932bab44713b74b422b4b759b49f5a222977Bo Liu// no profiles participating in safe browsing.
268bee9932bab44713b74b422b4b759b49f5a222977Bo LiuTEST_F(LastDownloadFinderTest, NoParticipatingProfiles) {
269bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // Create a profile with a history service that is opted-out
270bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  TestingProfile* profile = CreateProfile(SAFE_BROWSING_OPT_OUT);
271bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
272bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // Add a download.
273bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  AddDownload(profile, CreateTestDownloadRow());
274bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
275bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  ExpectNoDownloadFound(RunLastDownloadFinder());
276bee9932bab44713b74b422b4b759b49f5a222977Bo Liu}
277bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
278bee9932bab44713b74b422b4b759b49f5a222977Bo Liu// Tests that a download is found from a single profile.
279bee9932bab44713b74b422b4b759b49f5a222977Bo LiuTEST_F(LastDownloadFinderTest, SimpleEndToEnd) {
280bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // Create a profile with a history service that is opted-in.
281bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  TestingProfile* profile = CreateProfile(SAFE_BROWSING_OPT_IN);
282bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
283bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // Add a download.
284bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  AddDownload(profile, CreateTestDownloadRow());
285bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
286bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  ExpectFoundTestDownload(RunLastDownloadFinder());
287bee9932bab44713b74b422b4b759b49f5a222977Bo Liu}
288bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
289bee9932bab44713b74b422b4b759b49f5a222977Bo Liu// Tests that there is no crash if the finder is deleted before results arrive.
290bee9932bab44713b74b422b4b759b49f5a222977Bo LiuTEST_F(LastDownloadFinderTest, DeleteBeforeResults) {
291bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // Create a profile with a history service that is opted-in.
292bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  TestingProfile* profile = CreateProfile(SAFE_BROWSING_OPT_IN);
293bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
294bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // Add a download.
295bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  AddDownload(profile, CreateTestDownloadRow());
296bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
297bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // Start a finder and kill it before the search completes.
298bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  safe_browsing::LastDownloadFinder::Create(
299bee9932bab44713b74b422b4b759b49f5a222977Bo Liu      base::Bind(&LastDownloadFinderTest::NeverCalled, base::Unretained(this)))
300bee9932bab44713b74b422b4b759b49f5a222977Bo Liu      .reset();
301bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
302bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // Flush tasks on the history backend thread.
303bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  FlushHistoryBackend(profile);
304bee9932bab44713b74b422b4b759b49f5a222977Bo Liu}
305bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
306bee9932bab44713b74b422b4b759b49f5a222977Bo Liu// Tests that a download in profile added after the search is begun is found.
307bee9932bab44713b74b422b4b759b49f5a222977Bo LiuTEST_F(LastDownloadFinderTest, AddProfileAfterStarting) {
308bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // Create a profile with a history service that is opted-in.
309bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  CreateProfile(SAFE_BROWSING_OPT_IN);
310bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
311bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  scoped_ptr<safe_browsing::ClientIncidentReport_DownloadDetails> last_download;
312bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  base::RunLoop run_loop;
313bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
314bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // Post a task that will create a second profile once the main loop is run.
315bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  base::MessageLoop::current()->PostTask(
316bee9932bab44713b74b422b4b759b49f5a222977Bo Liu      FROM_HERE,
317bee9932bab44713b74b422b4b759b49f5a222977Bo Liu      base::Bind(&LastDownloadFinderTest::CreateProfileWithDownload,
318bee9932bab44713b74b422b4b759b49f5a222977Bo Liu                 base::Unretained(this)));
319bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
320bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  // Create a finder that we expect will find a download in the second profile.
321bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  scoped_ptr<safe_browsing::LastDownloadFinder> finder(
322bee9932bab44713b74b422b4b759b49f5a222977Bo Liu      safe_browsing::LastDownloadFinder::Create(
323bee9932bab44713b74b422b4b759b49f5a222977Bo Liu          base::Bind(&LastDownloadFinderTest::OnLastDownload,
324bee9932bab44713b74b422b4b759b49f5a222977Bo Liu                     base::Unretained(this),
325bee9932bab44713b74b422b4b759b49f5a222977Bo Liu                     &last_download,
326bee9932bab44713b74b422b4b759b49f5a222977Bo Liu                     run_loop.QuitClosure())));
327bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
328bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  run_loop.Run();
329bee9932bab44713b74b422b4b759b49f5a222977Bo Liu
330bee9932bab44713b74b422b4b759b49f5a222977Bo Liu  ExpectFoundTestDownload(last_download.Pass());
331bee9932bab44713b74b422b4b759b49f5a222977Bo Liu}
332