OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/safe_browsing/incident_reporting/download_metadata_mana
ger.h" | 5 #include "chrome/browser/safe_browsing/incident_reporting/download_metadata_mana
ger.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
| 9 #include <memory> |
9 #include <string> | 10 #include <string> |
10 | 11 |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/callback.h" | 13 #include "base/callback.h" |
13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
14 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
18 #include "chrome/common/safe_browsing/csd.pb.h" | 18 #include "chrome/common/safe_browsing/csd.pb.h" |
19 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" |
20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/download_manager.h" | 21 #include "content/public/browser/download_manager.h" |
22 #include "content/public/test/mock_download_item.h" | 22 #include "content/public/test/mock_download_item.h" |
23 #include "content/public/test/mock_download_manager.h" | 23 #include "content/public/test/mock_download_manager.h" |
24 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
25 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
(...skipping 30 matching lines...) Expand all Loading... |
56 virtual ~DownloadDetailsGetter() {} | 56 virtual ~DownloadDetailsGetter() {} |
57 virtual void OnDownloadDetails( | 57 virtual void OnDownloadDetails( |
58 ClientIncidentReport_DownloadDetails* details) = 0; | 58 ClientIncidentReport_DownloadDetails* details) = 0; |
59 DownloadMetadataManager::GetDownloadDetailsCallback GetCallback() { | 59 DownloadMetadataManager::GetDownloadDetailsCallback GetCallback() { |
60 return base::Bind(&DownloadDetailsGetter::DownloadDetailsCallback, | 60 return base::Bind(&DownloadDetailsGetter::DownloadDetailsCallback, |
61 base::Unretained(this)); | 61 base::Unretained(this)); |
62 } | 62 } |
63 | 63 |
64 private: | 64 private: |
65 void DownloadDetailsCallback( | 65 void DownloadDetailsCallback( |
66 scoped_ptr<ClientIncidentReport_DownloadDetails> details) { | 66 std::unique_ptr<ClientIncidentReport_DownloadDetails> details) { |
67 OnDownloadDetails(details.get()); | 67 OnDownloadDetails(details.get()); |
68 } | 68 } |
69 }; | 69 }; |
70 | 70 |
71 // A mock DownloadDetailsGetter. | 71 // A mock DownloadDetailsGetter. |
72 class MockDownloadDetailsGetter : public DownloadDetailsGetter { | 72 class MockDownloadDetailsGetter : public DownloadDetailsGetter { |
73 public: | 73 public: |
74 MOCK_METHOD1(OnDownloadDetails, void(ClientIncidentReport_DownloadDetails*)); | 74 MOCK_METHOD1(OnDownloadDetails, void(ClientIncidentReport_DownloadDetails*)); |
75 }; | 75 }; |
76 | 76 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 base::ThreadTaskRunnerHandle::Get())), | 110 base::ThreadTaskRunnerHandle::Get())), |
111 download_manager_(), | 111 download_manager_(), |
112 dm_observer_() {} | 112 dm_observer_() {} |
113 | 113 |
114 // Returns the path to the test profile's DownloadMetadata file. | 114 // Returns the path to the test profile's DownloadMetadata file. |
115 base::FilePath GetMetadataPath() const { | 115 base::FilePath GetMetadataPath() const { |
116 return profile_.GetPath().Append(FILE_PATH_LITERAL("DownloadMetadata")); | 116 return profile_.GetPath().Append(FILE_PATH_LITERAL("DownloadMetadata")); |
117 } | 117 } |
118 | 118 |
119 // Returns a new ClientDownloadRequest for the given download URL. | 119 // Returns a new ClientDownloadRequest for the given download URL. |
120 static scoped_ptr<ClientDownloadRequest> MakeTestRequest(const char* url) { | 120 static std::unique_ptr<ClientDownloadRequest> MakeTestRequest( |
121 scoped_ptr<ClientDownloadRequest> request(new ClientDownloadRequest()); | 121 const char* url) { |
| 122 std::unique_ptr<ClientDownloadRequest> request(new ClientDownloadRequest()); |
122 request->set_url(url); | 123 request->set_url(url); |
123 request->mutable_digests(); | 124 request->mutable_digests(); |
124 request->set_length(kTestDownloadLength); | 125 request->set_length(kTestDownloadLength); |
125 return request; | 126 return request; |
126 } | 127 } |
127 | 128 |
128 // Returns a new DownloadMetdata for the given download id. | 129 // Returns a new DownloadMetdata for the given download id. |
129 static scoped_ptr<DownloadMetadata> GetTestMetadata(uint32_t download_id) { | 130 static std::unique_ptr<DownloadMetadata> GetTestMetadata( |
130 scoped_ptr<DownloadMetadata> metadata(new DownloadMetadata()); | 131 uint32_t download_id) { |
| 132 std::unique_ptr<DownloadMetadata> metadata(new DownloadMetadata()); |
131 metadata->set_download_id(download_id); | 133 metadata->set_download_id(download_id); |
132 ClientIncidentReport_DownloadDetails* details = | 134 ClientIncidentReport_DownloadDetails* details = |
133 metadata->mutable_download(); | 135 metadata->mutable_download(); |
134 details->set_download_time_msec(kTestDownloadTimeMsec); | 136 details->set_download_time_msec(kTestDownloadTimeMsec); |
135 details->set_allocated_download(MakeTestRequest(kTestUrl).release()); | 137 details->set_allocated_download(MakeTestRequest(kTestUrl).release()); |
136 return metadata; | 138 return metadata; |
137 } | 139 } |
138 | 140 |
139 // Writes a test DownloadMetadata file for the given download id to the | 141 // Writes a test DownloadMetadata file for the given download id to the |
140 // test profile directory. | 142 // test profile directory. |
141 void WriteTestMetadataFileForItem(uint32_t download_id) { | 143 void WriteTestMetadataFileForItem(uint32_t download_id) { |
142 std::string data; | 144 std::string data; |
143 ASSERT_TRUE(GetTestMetadata(download_id)->SerializeToString(&data)); | 145 ASSERT_TRUE(GetTestMetadata(download_id)->SerializeToString(&data)); |
144 ASSERT_TRUE(base::WriteFile(GetMetadataPath(), data.data(), data.size())); | 146 ASSERT_TRUE(base::WriteFile(GetMetadataPath(), data.data(), data.size())); |
145 } | 147 } |
146 | 148 |
147 // Writes a test DownloadMetadata file for kTestDownloadId to the test profile | 149 // Writes a test DownloadMetadata file for kTestDownloadId to the test profile |
148 // directory. | 150 // directory. |
149 void WriteTestMetadataFile() { | 151 void WriteTestMetadataFile() { |
150 WriteTestMetadataFileForItem(kTestDownloadId); | 152 WriteTestMetadataFileForItem(kTestDownloadId); |
151 } | 153 } |
152 | 154 |
153 // Returns the DownloadMetadata read from the test profile's directory. | 155 // Returns the DownloadMetadata read from the test profile's directory. |
154 scoped_ptr<DownloadMetadata> ReadTestMetadataFile() const { | 156 std::unique_ptr<DownloadMetadata> ReadTestMetadataFile() const { |
155 std::string data; | 157 std::string data; |
156 if (!base::ReadFileToString(GetMetadataPath(), &data)) | 158 if (!base::ReadFileToString(GetMetadataPath(), &data)) |
157 return scoped_ptr<DownloadMetadata>(); | 159 return std::unique_ptr<DownloadMetadata>(); |
158 scoped_ptr<DownloadMetadata> result(new DownloadMetadata); | 160 std::unique_ptr<DownloadMetadata> result(new DownloadMetadata); |
159 EXPECT_TRUE(result->ParseFromString(data)); | 161 EXPECT_TRUE(result->ParseFromString(data)); |
160 return result; | 162 return result; |
161 } | 163 } |
162 | 164 |
163 // Runs all tasks posted to the test thread's message loop. | 165 // Runs all tasks posted to the test thread's message loop. |
164 void RunAllTasks() { base::MessageLoop::current()->RunUntilIdle(); } | 166 void RunAllTasks() { base::MessageLoop::current()->RunUntilIdle(); } |
165 | 167 |
166 // Adds a DownloadManager for the test profile. The DownloadMetadataManager's | 168 // Adds a DownloadManager for the test profile. The DownloadMetadataManager's |
167 // observer is stashed for later use. Only call once per call to | 169 // observer is stashed for later use. Only call once per call to |
168 // ShutdownDownloadManager. | 170 // ShutdownDownloadManager. |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 if (other_item_) | 249 if (other_item_) |
248 downloads->push_back(other_item_.get()); | 250 downloads->push_back(other_item_.get()); |
249 if (zero_item_) | 251 if (zero_item_) |
250 downloads->push_back(zero_item_.get()); | 252 downloads->push_back(zero_item_.get()); |
251 } | 253 } |
252 | 254 |
253 content::TestBrowserThreadBundle thread_bundle_; | 255 content::TestBrowserThreadBundle thread_bundle_; |
254 NiceMock<MockDownloadMetadataManager> manager_; | 256 NiceMock<MockDownloadMetadataManager> manager_; |
255 TestingProfile profile_; | 257 TestingProfile profile_; |
256 NiceMock<content::MockDownloadManager> download_manager_; | 258 NiceMock<content::MockDownloadManager> download_manager_; |
257 scoped_ptr<content::MockDownloadItem> test_item_; | 259 std::unique_ptr<content::MockDownloadItem> test_item_; |
258 scoped_ptr<content::MockDownloadItem> other_item_; | 260 std::unique_ptr<content::MockDownloadItem> other_item_; |
259 scoped_ptr<content::MockDownloadItem> zero_item_; | 261 std::unique_ptr<content::MockDownloadItem> zero_item_; |
260 | 262 |
261 // The DownloadMetadataManager's content::DownloadManager::Observer. Captured | 263 // The DownloadMetadataManager's content::DownloadManager::Observer. Captured |
262 // by download_manager_'s AddObserver action. | 264 // by download_manager_'s AddObserver action. |
263 content::DownloadManager::Observer* dm_observer_; | 265 content::DownloadManager::Observer* dm_observer_; |
264 }; | 266 }; |
265 | 267 |
266 // A parameterized test that exercises GetDownloadDetails. The parameters | 268 // A parameterized test that exercises GetDownloadDetails. The parameters |
267 // dictate the exact state of affairs leading up to the call as follows: | 269 // dictate the exact state of affairs leading up to the call as follows: |
268 // 0: if "present", the profile has a pre-existing DownloadMetadata file. | 270 // 0: if "present", the profile has a pre-existing DownloadMetadata file. |
269 // 1: if "managed", the profile's DownloadManager has been created. | 271 // 1: if "managed", the profile's DownloadManager has been created. |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 details_getter, | 497 details_getter, |
496 OnDownloadDetails(ResultOf(GetDetailsDownloadUrl, StrEq(kNewUrl)))); | 498 OnDownloadDetails(ResultOf(GetDetailsDownloadUrl, StrEq(kNewUrl)))); |
497 manager_.GetDownloadDetails(&profile_, details_getter.GetCallback()); | 499 manager_.GetDownloadDetails(&profile_, details_getter.GetCallback()); |
498 } | 500 } |
499 | 501 |
500 // In http://crbug.com/433928, open after SetRequest(nullpr) caused a crash. | 502 // In http://crbug.com/433928, open after SetRequest(nullpr) caused a crash. |
501 test_item_->NotifyObserversDownloadOpened(); | 503 test_item_->NotifyObserversDownloadOpened(); |
502 | 504 |
503 ShutdownDownloadManager(); | 505 ShutdownDownloadManager(); |
504 | 506 |
505 scoped_ptr<DownloadMetadata> metadata(ReadTestMetadataFile()); | 507 std::unique_ptr<DownloadMetadata> metadata(ReadTestMetadataFile()); |
506 if (set_request_) { | 508 if (set_request_) { |
507 // Expect that the file contains metadata for the download. | 509 // Expect that the file contains metadata for the download. |
508 ASSERT_TRUE(metadata); | 510 ASSERT_TRUE(metadata); |
509 EXPECT_EQ(kTestDownloadId, metadata->download_id()); | 511 EXPECT_EQ(kTestDownloadId, metadata->download_id()); |
510 EXPECT_STREQ(kNewUrl, metadata->download().download().url().c_str()); | 512 EXPECT_STREQ(kNewUrl, metadata->download().download().url().c_str()); |
511 } else if (metadata_file_present_) { | 513 } else if (metadata_file_present_) { |
512 // Expect that the metadata file has not been overwritten. | 514 // Expect that the metadata file has not been overwritten. |
513 ASSERT_TRUE(metadata); | 515 ASSERT_TRUE(metadata); |
514 } else { | 516 } else { |
515 // Expect that the file is not present. | 517 // Expect that the file is not present. |
(...skipping 28 matching lines...) Expand all Loading... |
544 | 546 |
545 // The test item completes. | 547 // The test item completes. |
546 ON_CALL(*test_item_, GetState()) | 548 ON_CALL(*test_item_, GetState()) |
547 .WillByDefault(Return(content::DownloadItem::COMPLETE)); | 549 .WillByDefault(Return(content::DownloadItem::COMPLETE)); |
548 test_item_->NotifyObserversDownloadUpdated(); | 550 test_item_->NotifyObserversDownloadUpdated(); |
549 | 551 |
550 RunAllTasks(); | 552 RunAllTasks(); |
551 ShutdownDownloadManager(); | 553 ShutdownDownloadManager(); |
552 | 554 |
553 // Expect that the metadata file is still present. | 555 // Expect that the metadata file is still present. |
554 scoped_ptr<DownloadMetadata> metadata(ReadTestMetadataFile()); | 556 std::unique_ptr<DownloadMetadata> metadata(ReadTestMetadataFile()); |
555 ASSERT_TRUE(metadata); | 557 ASSERT_TRUE(metadata); |
556 EXPECT_EQ(kOtherDownloadId, metadata->download_id()); | 558 EXPECT_EQ(kOtherDownloadId, metadata->download_id()); |
557 } | 559 } |
558 | 560 |
559 TEST_F(DownloadMetadataManagerTestBase, ActiveDownloadWithRequest) { | 561 TEST_F(DownloadMetadataManagerTestBase, ActiveDownloadWithRequest) { |
560 // Put some metadata on disk from a previous download. | 562 // Put some metadata on disk from a previous download. |
561 WriteTestMetadataFileForItem(kOtherDownloadId); | 563 WriteTestMetadataFileForItem(kOtherDownloadId); |
562 | 564 |
563 AddDownloadManager(); | 565 AddDownloadManager(); |
564 AddDownloadItems(); | 566 AddDownloadItems(); |
(...skipping 22 matching lines...) Expand all Loading... |
587 MockDownloadDetailsGetter details_getter; | 589 MockDownloadDetailsGetter details_getter; |
588 // Expect that the callback is invoked with details for this item. | 590 // Expect that the callback is invoked with details for this item. |
589 EXPECT_CALL( | 591 EXPECT_CALL( |
590 details_getter, | 592 details_getter, |
591 OnDownloadDetails(ResultOf(GetDetailsDownloadUrl, StrEq(kNewUrl)))); | 593 OnDownloadDetails(ResultOf(GetDetailsDownloadUrl, StrEq(kNewUrl)))); |
592 manager_.GetDownloadDetails(&profile_, details_getter.GetCallback()); | 594 manager_.GetDownloadDetails(&profile_, details_getter.GetCallback()); |
593 | 595 |
594 ShutdownDownloadManager(); | 596 ShutdownDownloadManager(); |
595 | 597 |
596 // Expect that the file contains metadata for the download. | 598 // Expect that the file contains metadata for the download. |
597 scoped_ptr<DownloadMetadata> metadata(ReadTestMetadataFile()); | 599 std::unique_ptr<DownloadMetadata> metadata(ReadTestMetadataFile()); |
598 ASSERT_TRUE(metadata); | 600 ASSERT_TRUE(metadata); |
599 EXPECT_EQ(kTestDownloadId, metadata->download_id()); | 601 EXPECT_EQ(kTestDownloadId, metadata->download_id()); |
600 EXPECT_STREQ(kNewUrl, metadata->download().download().url().c_str()); | 602 EXPECT_STREQ(kNewUrl, metadata->download().download().url().c_str()); |
601 } | 603 } |
602 | 604 |
603 // Regression test for http://crbug.com/504092: open an item with id==0 when | 605 // Regression test for http://crbug.com/504092: open an item with id==0 when |
604 // there is no metadata loaded. | 606 // there is no metadata loaded. |
605 TEST_F(DownloadMetadataManagerTestBase, OpenItemWithZeroId) { | 607 TEST_F(DownloadMetadataManagerTestBase, OpenItemWithZeroId) { |
606 AddDownloadManager(); | 608 AddDownloadManager(); |
607 AddDownloadItems(); | 609 AddDownloadItems(); |
608 | 610 |
609 // Allow everything to load into steady-state. | 611 // Allow everything to load into steady-state. |
610 RunAllTasks(); | 612 RunAllTasks(); |
611 | 613 |
612 // Open the zero-id item. | 614 // Open the zero-id item. |
613 zero_item_->NotifyObserversDownloadOpened(); | 615 zero_item_->NotifyObserversDownloadOpened(); |
614 | 616 |
615 ShutdownDownloadManager(); | 617 ShutdownDownloadManager(); |
616 } | 618 } |
617 | 619 |
618 } // namespace safe_browsing | 620 } // namespace safe_browsing |
OLD | NEW |