OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "base/pickle.h" | 7 #include "base/pickle.h" |
8 #include "base/time.h" | 8 #include "base/time.h" |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/safe_browsing/malware_details.h" | 10 #include "chrome/browser/safe_browsing/malware_details.h" |
11 #include "chrome/browser/safe_browsing/malware_details_history.h" | |
11 #include "chrome/browser/safe_browsing/report.pb.h" | 12 #include "chrome/browser/safe_browsing/report.pb.h" |
12 #include "chrome/common/render_messages.h" | 13 #include "chrome/common/render_messages.h" |
13 #include "chrome/common/safe_browsing/safebrowsing_messages.h" | 14 #include "chrome/common/safe_browsing/safebrowsing_messages.h" |
14 #include "chrome/test/test_url_request_context_getter.h" | 15 #include "chrome/test/test_url_request_context_getter.h" |
15 #include "chrome/test/testing_profile.h" | 16 #include "chrome/test/testing_profile.h" |
16 #include "content/browser/browser_thread.h" | 17 #include "content/browser/browser_thread.h" |
18 #include "chrome/browser/history/history_backend.h" | |
19 #include "chrome/browser/history/history.h" | |
17 #include "content/browser/renderer_host/test_render_view_host.h" | 20 #include "content/browser/renderer_host/test_render_view_host.h" |
18 #include "content/browser/tab_contents/navigation_entry.h" | 21 #include "content/browser/tab_contents/navigation_entry.h" |
19 #include "content/browser/tab_contents/test_tab_contents.h" | 22 #include "content/browser/tab_contents/test_tab_contents.h" |
20 #include "net/base/io_buffer.h" | 23 #include "net/base/io_buffer.h" |
21 #include "net/base/test_completion_callback.h" | 24 #include "net/base/test_completion_callback.h" |
22 #include "net/disk_cache/disk_cache.h" | 25 #include "net/disk_cache/disk_cache.h" |
23 #include "net/http/http_cache.h" | 26 #include "net/http/http_cache.h" |
24 #include "net/http/http_response_headers.h" | 27 #include "net/http/http_response_headers.h" |
25 #include "net/http/http_response_info.h" | 28 #include "net/http/http_response_info.h" |
26 #include "net/http/http_util.h" | 29 #include "net/http/http_util.h" |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 sb_service_(new MockSafeBrowsingService()) { | 171 sb_service_(new MockSafeBrowsingService()) { |
169 } | 172 } |
170 | 173 |
171 virtual void SetUp() { | 174 virtual void SetUp() { |
172 RenderViewHostTestHarness::SetUp(); | 175 RenderViewHostTestHarness::SetUp(); |
173 // request_context_getter_ = new TestURLRequestContextGetter(); | 176 // request_context_getter_ = new TestURLRequestContextGetter(); |
174 | 177 |
175 // The URLFetcher checks that the messageloop type is IO. | 178 // The URLFetcher checks that the messageloop type is IO. |
176 ASSERT_TRUE(io_thread_.StartWithOptions( | 179 ASSERT_TRUE(io_thread_.StartWithOptions( |
177 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 180 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
181 | |
182 profile_->CreateHistoryService(true /* delete_file */, false /* no_db */); | |
178 } | 183 } |
179 | 184 |
180 virtual void TearDown() { | 185 virtual void TearDown() { |
181 io_thread_.Stop(); | 186 io_thread_.Stop(); |
187 profile_->DestroyHistoryService(); | |
182 RenderViewHostTestHarness::TearDown(); | 188 RenderViewHostTestHarness::TearDown(); |
183 } | 189 } |
184 | 190 |
185 static bool ResourceLessThan( | 191 static bool ResourceLessThan( |
186 const ClientMalwareReportRequest::Resource* lhs, | 192 const ClientMalwareReportRequest::Resource* lhs, |
187 const ClientMalwareReportRequest::Resource* rhs) { | 193 const ClientMalwareReportRequest::Resource* rhs) { |
188 return lhs->id() < rhs->id(); | 194 return lhs->id() < rhs->id(); |
189 } | 195 } |
190 | 196 |
191 std::string WaitForSerializedReport(MalwareDetails* report) { | 197 std::string WaitForSerializedReport(MalwareDetails* report) { |
192 BrowserThread::PostTask( | 198 BrowserThread::PostTask( |
193 BrowserThread::IO, | 199 BrowserThread::IO, |
194 FROM_HERE, | 200 FROM_HERE, |
195 NewRunnableMethod( | 201 NewRunnableMethod( |
196 report, &MalwareDetails::FinishCollection)); | 202 report, &MalwareDetails::FinishCollection)); |
197 // Wait for the callback (SendSerializedMalwareDetails). | 203 // Wait for the callback (SendSerializedMalwareDetails). |
198 DVLOG(1) << "Waiting for SendSerializedMalwareDetails"; | 204 DVLOG(1) << "Waiting for SendSerializedMalwareDetails"; |
199 MessageLoop::current()->Run(); | 205 MessageLoop::current()->Run(); |
200 return sb_service_->GetSerialized(); | 206 return sb_service_->GetSerialized(); |
201 } | 207 } |
202 | 208 |
209 HistoryService* history_service() { | |
210 return profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | |
211 } | |
212 | |
203 protected: | 213 protected: |
204 void InitResource(SafeBrowsingService::UnsafeResource* resource, | 214 void InitResource(SafeBrowsingService::UnsafeResource* resource, |
205 ResourceType::Type resource_type, | 215 ResourceType::Type resource_type, |
206 const GURL& url) { | 216 const GURL& url) { |
207 resource->client = NULL; | 217 resource->client = NULL; |
208 resource->url = url; | 218 resource->url = url; |
209 resource->resource_type = resource_type; | 219 resource->resource_type = resource_type; |
210 resource->threat_type = SafeBrowsingService::URL_MALWARE; | 220 resource->threat_type = SafeBrowsingService::URL_MALWARE; |
211 resource->render_process_host_id = contents()->GetRenderProcessHost()->id(); | 221 resource->render_process_host_id = contents()->GetRenderProcessHost()->id(); |
212 resource->render_view_id = contents()->render_view_host()->routing_id(); | 222 resource->render_view_id = contents()->render_view_host()->routing_id(); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
271 } | 281 } |
272 | 282 |
273 EXPECT_EQ(expected->response().body(), resource->response().body()); | 283 EXPECT_EQ(expected->response().body(), resource->response().body()); |
274 EXPECT_EQ(expected->response().bodylength(), | 284 EXPECT_EQ(expected->response().bodylength(), |
275 resource->response().bodylength()); | 285 resource->response().bodylength()); |
276 EXPECT_EQ(expected->response().bodydigest(), | 286 EXPECT_EQ(expected->response().bodydigest(), |
277 resource->response().bodydigest()); | 287 resource->response().bodydigest()); |
278 } | 288 } |
279 } | 289 } |
280 | 290 |
291 // Adds a page to history. | |
292 // The redirects is the redirect url chain leading to the url. | |
293 void AddPageToHistory(const GURL& url, | |
294 history::RedirectList& redirects) { | |
295 // The last item of the redirect chain has to be the final url when adding | |
296 // to history backend. | |
297 redirects.push_back(url); | |
mattm
2011/05/18 22:31:47
Arguments that are modified should be passed as po
kewang
2011/05/19 06:03:10
Done.
| |
298 history_service()->AddPage( | |
299 url, static_cast<void*>(this), 0, GURL(), PageTransition::TYPED, | |
300 redirects, history::SOURCE_BROWSED, false); | |
301 } | |
302 | |
281 BrowserThread ui_thread_; | 303 BrowserThread ui_thread_; |
282 BrowserThread io_thread_; | 304 BrowserThread io_thread_; |
283 scoped_refptr<MockSafeBrowsingService> sb_service_; | 305 scoped_refptr<MockSafeBrowsingService> sb_service_; |
284 }; | 306 }; |
285 | 307 |
286 // Tests creating a simple malware report. | 308 // Tests creating a simple malware report. |
287 TEST_F(MalwareDetailsTest, MalwareSubResource) { | 309 TEST_F(MalwareDetailsTest, MalwareSubResource) { |
288 // Start a load. | 310 // Start a load. |
289 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); | 311 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); |
290 | 312 |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
585 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); | 607 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); |
586 pb_resource->set_id(0); | 608 pb_resource->set_id(0); |
587 pb_resource->set_url(kLandingURL); | 609 pb_resource->set_url(kLandingURL); |
588 pb_resource = expected.add_resources(); | 610 pb_resource = expected.add_resources(); |
589 pb_resource->set_id(1); | 611 pb_resource->set_id(1); |
590 pb_resource->set_url(kMalwareURL); | 612 pb_resource->set_url(kMalwareURL); |
591 expected.set_complete(true); | 613 expected.set_complete(true); |
592 | 614 |
593 VerifyResults(actual, expected); | 615 VerifyResults(actual, expected); |
594 } | 616 } |
617 | |
618 // Test getting redirects from history service. | |
619 TEST_F(MalwareDetailsTest, HistoryServiceUrls) { | |
620 // Add content to history service. | |
621 // There are two redirect urls before reacing malware url: | |
622 // kFirstRedirectURL -> kSecondRedirectURL -> kMalwareURL | |
623 GURL baseurl(kMalwareURL); | |
624 history::RedirectList redirects; | |
625 redirects.push_back(GURL(kFirstRedirectURL)); | |
626 redirects.push_back(GURL(kSecondRedirectURL)); | |
627 AddPageToHistory(baseurl, redirects); | |
628 // Wait for history service operation finished. | |
629 profile_->BlockUntilHistoryProcessesPendingRequests(); | |
630 | |
631 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); | |
632 | |
633 SafeBrowsingService::UnsafeResource resource; | |
634 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); | |
635 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap( | |
636 sb_service_.get(), contents(), resource, NULL); | |
637 | |
638 // Reset the history service pointer. | |
639 report->redirects_collector_->SetHistory(history_service()); | |
640 | |
641 // The redirects collection starts after the IPC from the DOM is fired. | |
642 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node> params; | |
643 report->OnReceivedMalwareDOMDetails(params); | |
644 | |
645 // Let the redirects callbacks complete. | |
646 MessageLoop::current()->RunAllPending(); | |
647 | |
648 std::string serialized = WaitForSerializedReport(report); | |
649 ClientMalwareReportRequest actual; | |
650 actual.ParseFromString(serialized); | |
651 | |
652 ClientMalwareReportRequest expected; | |
653 expected.set_malware_url(kMalwareURL); | |
654 expected.set_page_url(kLandingURL); | |
655 expected.set_referrer_url(""); | |
656 | |
657 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); | |
658 pb_resource->set_id(0); | |
659 pb_resource->set_url(kLandingURL); | |
660 pb_resource = expected.add_resources(); | |
661 pb_resource->set_id(1); | |
662 pb_resource->set_parent_id(2); | |
663 pb_resource->set_url(kMalwareURL); | |
664 pb_resource = expected.add_resources(); | |
665 pb_resource->set_id(2); | |
666 pb_resource->set_parent_id(3); | |
667 pb_resource->set_url(kSecondRedirectURL); | |
668 pb_resource = expected.add_resources(); | |
669 pb_resource->set_id(3); | |
670 pb_resource->set_url(kFirstRedirectURL); | |
671 | |
672 VerifyResults(actual, expected); | |
673 } | |
OLD | NEW |