1 // Copyright 2014 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.
5 #include "chrome/browser/safe_browsing/incident_reporting/last_download_finder.h"
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/files/file_util.h"
13 #include "base/location.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/run_loop.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/thread_task_runner_handle.h"
20 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
21 #include "chrome/browser/history/chrome_history_client.h"
22 #include "chrome/browser/history/history_service_factory.h"
23 #include "chrome/browser/history/web_history_service_factory.h"
24 #include "chrome/browser/prefs/browser_prefs.h"
25 #include "chrome/browser/profiles/profile_manager.h"
26 #include "chrome/common/chrome_constants.h"
27 #include "chrome/common/pref_names.h"
28 #include "chrome/common/safe_browsing/csd.pb.h"
29 #include "chrome/test/base/testing_browser_process.h"
30 #include "chrome/test/base/testing_pref_service_syncable.h"
31 #include "chrome/test/base/testing_profile.h"
32 #include "chrome/test/base/testing_profile_manager.h"
33 #include "components/history/content/browser/content_visit_delegate.h"
34 #include "components/history/content/browser/download_constants_utils.h"
35 #include "components/history/content/browser/history_database_helper.h"
36 #include "components/history/core/browser/download_constants.h"
37 #include "components/history/core/browser/download_row.h"
38 #include "components/history/core/browser/history_constants.h"
39 #include "components/history/core/browser/history_database_params.h"
40 #include "components/history/core/browser/history_service.h"
41 #include "content/public/test/test_browser_thread_bundle.h"
42 #include "content/public/test/test_utils.h"
43 #include "testing/gtest/include/gtest/gtest.h"
47 // A BrowserContextKeyedServiceFactory::TestingFactoryFunction that creates a
48 // HistoryService for a TestingProfile.
49 scoped_ptr
<KeyedService
> BuildHistoryService(content::BrowserContext
* context
) {
50 TestingProfile
* profile
= static_cast<TestingProfile
*>(context
);
52 // Delete the file before creating the service.
53 base::FilePath
history_path(
54 profile
->GetPath().Append(history::kHistoryFilename
));
55 if (!base::DeleteFile(history_path
, false) ||
56 base::PathExists(history_path
)) {
57 ADD_FAILURE() << "failed to delete history db file "
58 << history_path
.value();
62 scoped_ptr
<history::HistoryService
> history_service(
63 new history::HistoryService(
64 make_scoped_ptr(new ChromeHistoryClient(
65 BookmarkModelFactory::GetForProfile(profile
))),
66 scoped_ptr
<history::VisitDelegate
>()));
67 if (history_service
->Init(
68 profile
->GetPrefs()->GetString(prefs::kAcceptLanguages
),
69 history::HistoryDatabaseParamsForPath(profile
->GetPath()))) {
70 return history_service
.Pass();
73 ADD_FAILURE() << "failed to initialize history service";
79 namespace safe_browsing
{
81 class LastDownloadFinderTest
: public testing::Test
{
83 void NeverCalled(scoped_ptr
<ClientIncidentReport_DownloadDetails
> download
) {
87 // Creates a new profile that participates in safe browsing and adds a
88 // download to its history.
89 void CreateProfileWithDownload() {
90 TestingProfile
* profile
= CreateProfile(SAFE_BROWSING_OPT_IN
);
91 history::HistoryService
* history_service
=
92 HistoryServiceFactory::GetForProfile(
93 profile
, ServiceAccessType::EXPLICIT_ACCESS
);
94 history_service
->CreateDownload(
95 CreateTestDownloadRow(),
96 base::Bind(&LastDownloadFinderTest::OnDownloadCreated
,
97 base::Unretained(this)));
100 // LastDownloadFinder::LastDownloadCallback implementation that
101 // passes the found download to |result| and then runs a closure.
103 scoped_ptr
<ClientIncidentReport_DownloadDetails
>* result
,
104 const base::Closure
& quit_closure
,
105 scoped_ptr
<ClientIncidentReport_DownloadDetails
> download
) {
106 *result
= download
.Pass();
111 // A type for specifying whether or not a profile created by CreateProfile
112 // participates in safe browsing.
113 enum SafeBrowsingDisposition
{
114 SAFE_BROWSING_OPT_OUT
,
115 SAFE_BROWSING_OPT_IN
,
118 LastDownloadFinderTest() : profile_number_() {}
120 void SetUp() override
{
121 testing::Test::SetUp();
122 profile_manager_
.reset(
123 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
124 ASSERT_TRUE(profile_manager_
->SetUp());
127 void TearDown() override
{
128 // Shut down the history service on all profiles.
129 std::vector
<Profile
*> profiles(
130 profile_manager_
->profile_manager()->GetLoadedProfiles());
131 for (size_t i
= 0; i
< profiles
.size(); ++i
) {
132 profiles
[0]->AsTestingProfile()->DestroyHistoryService();
134 profile_manager_
.reset();
135 TestingBrowserProcess::DeleteInstance();
136 testing::Test::TearDown();
139 TestingProfile
* CreateProfile(SafeBrowsingDisposition safe_browsing_opt_in
) {
140 std::string
profile_name("profile");
141 profile_name
.append(base::IntToString(++profile_number_
));
143 // Set up keyed service factories.
144 TestingProfile::TestingFactories factories
;
145 // Build up a custom history service.
146 factories
.push_back(std::make_pair(HistoryServiceFactory::GetInstance(),
147 &BuildHistoryService
));
148 // Suppress WebHistoryService since it makes network requests.
149 factories
.push_back(std::make_pair(
150 WebHistoryServiceFactory::GetInstance(),
151 static_cast<BrowserContextKeyedServiceFactory::TestingFactoryFunction
>(
154 // Create prefs for the profile with safe browsing enabled or not.
155 scoped_ptr
<TestingPrefServiceSyncable
> prefs(
156 new TestingPrefServiceSyncable
);
157 chrome::RegisterUserProfilePrefs(prefs
->registry());
158 prefs
->SetBoolean(prefs::kSafeBrowsingEnabled
,
159 safe_browsing_opt_in
== SAFE_BROWSING_OPT_IN
);
161 TestingProfile
* profile
= profile_manager_
->CreateTestingProfile(
164 base::UTF8ToUTF16(profile_name
), // user_name
166 std::string(), // supervised_user_id
172 LastDownloadFinder::DownloadDetailsGetter
GetDownloadDetailsGetter() {
173 return base::Bind(&LastDownloadFinderTest::GetDownloadDetails
,
174 base::Unretained(this));
177 void AddDownload(Profile
* profile
, const history::DownloadRow
& download
) {
178 base::RunLoop run_loop
;
180 history::HistoryService
* history_service
=
181 HistoryServiceFactory::GetForProfile(
182 profile
, ServiceAccessType::EXPLICIT_ACCESS
);
183 history_service
->CreateDownload(
185 base::Bind(&LastDownloadFinderTest::ContinueOnDownloadCreated
,
186 base::Unretained(this),
187 run_loop
.QuitClosure()));
191 // Wait for the history backend thread to process any outstanding tasks.
192 // This is needed because HistoryService::QueryDownloads uses PostTaskAndReply
193 // to do work on the backend thread and then invoke the caller's callback on
194 // the originating thread. The PostTaskAndReplyRelay holds a reference to the
195 // backend until its RunReplyAndSelfDestruct is called on the originating
196 // thread. This reference MUST be released (on the originating thread,
197 // remember) _before_ calling DestroyHistoryService in TearDown(). See the
198 // giant comment in HistoryService::Cleanup explaining where the backend's
200 void FlushHistoryBackend(Profile
* profile
) {
201 base::RunLoop run_loop
;
202 HistoryServiceFactory::GetForProfile(profile
,
203 ServiceAccessType::EXPLICIT_ACCESS
)
204 ->FlushForTest(run_loop
.QuitClosure());
206 // Then make sure anything bounced back to the main thread has been handled.
207 base::RunLoop().RunUntilIdle();
210 // Runs the last download finder on all loaded profiles, returning the found
211 // download or an empty pointer if none was found.
212 scoped_ptr
<ClientIncidentReport_DownloadDetails
> RunLastDownloadFinder() {
213 base::RunLoop run_loop
;
215 scoped_ptr
<ClientIncidentReport_DownloadDetails
> last_download
;
217 scoped_ptr
<LastDownloadFinder
> finder(LastDownloadFinder::Create(
218 GetDownloadDetailsGetter(),
219 base::Bind(&LastDownloadFinderTest::OnLastDownload
,
220 base::Unretained(this),
222 run_loop
.QuitClosure())));
227 return last_download
.Pass();
230 history::DownloadRow
CreateTestDownloadRow() {
231 base::Time
now(base::Time::Now());
232 return history::DownloadRow(
233 base::FilePath(FILE_PATH_LITERAL("spam.exe")),
234 base::FilePath(FILE_PATH_LITERAL("spam.exe")),
235 std::vector
<GURL
>(1, GURL("http://www.google.com")), // url_chain
237 "application/octet-stream", // mime_type
238 "application/octet-stream", // original_mime_type
239 now
- base::TimeDelta::FromMinutes(10), // start
240 now
- base::TimeDelta::FromMinutes(9), // end
241 std::string(), // etag
242 std::string(), // last_modified
245 history::DownloadState::COMPLETE
, // download_state
246 history::DownloadDangerType::NOT_DANGEROUS
, // danger_type
247 history::ToHistoryDownloadInterruptReason(
248 content::DOWNLOAD_INTERRUPT_REASON_NONE
), // interrupt_reason,
250 false, // download_opened
251 std::string(), // ext_id
252 std::string()); // ext_name
255 void ExpectNoDownloadFound(
256 scoped_ptr
<ClientIncidentReport_DownloadDetails
> download
) {
257 EXPECT_FALSE(download
);
260 void ExpectFoundTestDownload(
261 scoped_ptr
<ClientIncidentReport_DownloadDetails
> download
) {
262 ASSERT_TRUE(download
);
265 content::TestBrowserThreadBundle browser_thread_bundle_
;
266 scoped_ptr
<TestingProfileManager
> profile_manager_
;
269 // A HistoryService::DownloadCreateCallback that asserts that the download was
270 // created and runs |closure|.
271 void ContinueOnDownloadCreated(const base::Closure
& closure
, bool created
) {
272 ASSERT_TRUE(created
);
276 // A HistoryService::DownloadCreateCallback that asserts that the download was
278 void OnDownloadCreated(bool created
) { ASSERT_TRUE(created
); }
280 void GetDownloadDetails(
281 content::BrowserContext
* context
,
282 const DownloadMetadataManager::GetDownloadDetailsCallback
& callback
) {
283 callback
.Run(scoped_ptr
<ClientIncidentReport_DownloadDetails
>());
289 // Tests that nothing happens if there are no profiles at all.
290 TEST_F(LastDownloadFinderTest
, NoProfiles
) {
291 ExpectNoDownloadFound(RunLastDownloadFinder());
294 // Tests that nothing happens other than the callback being invoked if there are
295 // no profiles participating in safe browsing.
296 TEST_F(LastDownloadFinderTest
, NoParticipatingProfiles
) {
297 // Create a profile with a history service that is opted-out
298 TestingProfile
* profile
= CreateProfile(SAFE_BROWSING_OPT_OUT
);
301 AddDownload(profile
, CreateTestDownloadRow());
303 ExpectNoDownloadFound(RunLastDownloadFinder());
306 // Tests that a download is found from a single profile.
307 TEST_F(LastDownloadFinderTest
, SimpleEndToEnd
) {
308 // Create a profile with a history service that is opted-in.
309 TestingProfile
* profile
= CreateProfile(SAFE_BROWSING_OPT_IN
);
312 AddDownload(profile
, CreateTestDownloadRow());
314 ExpectFoundTestDownload(RunLastDownloadFinder());
317 // Tests that there is no crash if the finder is deleted before results arrive.
318 TEST_F(LastDownloadFinderTest
, DeleteBeforeResults
) {
319 // Create a profile with a history service that is opted-in.
320 TestingProfile
* profile
= CreateProfile(SAFE_BROWSING_OPT_IN
);
323 AddDownload(profile
, CreateTestDownloadRow());
325 // Start a finder and kill it before the search completes.
326 LastDownloadFinder::Create(GetDownloadDetailsGetter(),
327 base::Bind(&LastDownloadFinderTest::NeverCalled
,
328 base::Unretained(this))).reset();
330 // Flush tasks on the history backend thread.
331 FlushHistoryBackend(profile
);
334 // Tests that a download in profile added after the search is begun is found.
335 TEST_F(LastDownloadFinderTest
, AddProfileAfterStarting
) {
336 // Create a profile with a history service that is opted-in.
337 CreateProfile(SAFE_BROWSING_OPT_IN
);
339 scoped_ptr
<ClientIncidentReport_DownloadDetails
> last_download
;
340 base::RunLoop run_loop
;
342 // Post a task that will create a second profile once the main loop is run.
343 base::ThreadTaskRunnerHandle::Get()->PostTask(
344 FROM_HERE
, base::Bind(&LastDownloadFinderTest::CreateProfileWithDownload
,
345 base::Unretained(this)));
347 // Create a finder that we expect will find a download in the second profile.
348 scoped_ptr
<LastDownloadFinder
> finder(LastDownloadFinder::Create(
349 GetDownloadDetailsGetter(),
350 base::Bind(&LastDownloadFinderTest::OnLastDownload
,
351 base::Unretained(this),
353 run_loop
.QuitClosure())));
357 ExpectFoundTestDownload(last_download
.Pass());
360 } // namespace safe_browsing