Chromium Code Reviews| 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.h" | |
| 19 #include "chrome/browser/history/history_backend.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 18 matching lines...) Expand all Loading... | |
| 45 "HTTP/1.1 200 OK\n" | 48 "HTTP/1.1 200 OK\n" |
| 46 "Content-Type: text/html\n" | 49 "Content-Type: text/html\n" |
| 47 "Content-Length: 1024\n" | 50 "Content-Length: 1024\n" |
| 48 "Set-Cookie: tastycookie\n"; // This header is stripped. | 51 "Set-Cookie: tastycookie\n"; // This header is stripped. |
| 49 static const char* kLandingData = "<iframe src='http://www.malware.com'>"; | 52 static const char* kLandingData = "<iframe src='http://www.malware.com'>"; |
| 50 | 53 |
| 51 using safe_browsing::ClientMalwareReportRequest; | 54 using safe_browsing::ClientMalwareReportRequest; |
| 52 | 55 |
| 53 namespace { | 56 namespace { |
| 54 | 57 |
| 58 // Used by WaitForHistory, see it for details. | |
| 59 class WaitForHistoryTask : public HistoryDBTask { | |
| 60 public: | |
| 61 WaitForHistoryTask() {} | |
| 62 | |
| 63 virtual bool RunOnDBThread(history::HistoryBackend* backend, | |
| 64 history::HistoryDatabase* db) { | |
| 65 return true; | |
| 66 } | |
| 67 | |
| 68 virtual void DoneRunOnMainThread() { | |
| 69 MessageLoop::current()->Quit(); | |
| 70 } | |
| 71 | |
| 72 private: | |
| 73 DISALLOW_COPY_AND_ASSIGN(WaitForHistoryTask); | |
| 74 }; | |
| 75 | |
| 55 void WriteHeaders(disk_cache::Entry* entry, const std::string headers) { | 76 void WriteHeaders(disk_cache::Entry* entry, const std::string headers) { |
| 56 net::HttpResponseInfo responseinfo; | 77 net::HttpResponseInfo responseinfo; |
| 57 std::string raw_headers = net::HttpUtil::AssembleRawHeaders( | 78 std::string raw_headers = net::HttpUtil::AssembleRawHeaders( |
| 58 headers.c_str(), headers.size()); | 79 headers.c_str(), headers.size()); |
| 59 responseinfo.headers = new net::HttpResponseHeaders(raw_headers); | 80 responseinfo.headers = new net::HttpResponseHeaders(raw_headers); |
| 60 | 81 |
| 61 Pickle pickle; | 82 Pickle pickle; |
| 62 responseinfo.Persist(&pickle, false, false); | 83 responseinfo.Persist(&pickle, false, false); |
| 63 | 84 |
| 64 scoped_refptr<net::WrappedIOBuffer> buf(new net::WrappedIOBuffer( | 85 scoped_refptr<net::WrappedIOBuffer> buf(new net::WrappedIOBuffer( |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 sb_service_(new MockSafeBrowsingService()) { | 189 sb_service_(new MockSafeBrowsingService()) { |
| 169 } | 190 } |
| 170 | 191 |
| 171 virtual void SetUp() { | 192 virtual void SetUp() { |
| 172 RenderViewHostTestHarness::SetUp(); | 193 RenderViewHostTestHarness::SetUp(); |
| 173 // request_context_getter_ = new TestURLRequestContextGetter(); | 194 // request_context_getter_ = new TestURLRequestContextGetter(); |
| 174 | 195 |
| 175 // The URLFetcher checks that the messageloop type is IO. | 196 // The URLFetcher checks that the messageloop type is IO. |
| 176 ASSERT_TRUE(io_thread_.StartWithOptions( | 197 ASSERT_TRUE(io_thread_.StartWithOptions( |
| 177 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 198 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
| 199 | |
| 200 profile_.reset(new TestingProfile); | |
| 201 profile_->CreateHistoryService(false, false); | |
| 178 } | 202 } |
| 179 | 203 |
| 180 virtual void TearDown() { | 204 virtual void TearDown() { |
| 205 profile_.reset(); | |
| 181 io_thread_.Stop(); | 206 io_thread_.Stop(); |
| 182 RenderViewHostTestHarness::TearDown(); | 207 RenderViewHostTestHarness::TearDown(); |
| 183 } | 208 } |
| 184 | 209 |
| 185 static bool ResourceLessThan( | 210 static bool ResourceLessThan( |
| 186 const ClientMalwareReportRequest::Resource* lhs, | 211 const ClientMalwareReportRequest::Resource* lhs, |
| 187 const ClientMalwareReportRequest::Resource* rhs) { | 212 const ClientMalwareReportRequest::Resource* rhs) { |
| 188 return lhs->id() < rhs->id(); | 213 return lhs->id() < rhs->id(); |
| 189 } | 214 } |
| 190 | 215 |
| 191 std::string WaitForSerializedReport(MalwareDetails* report) { | 216 std::string WaitForSerializedReport(MalwareDetails* report) { |
| 192 BrowserThread::PostTask( | 217 BrowserThread::PostTask( |
| 193 BrowserThread::IO, | 218 BrowserThread::IO, |
| 194 FROM_HERE, | 219 FROM_HERE, |
| 195 NewRunnableMethod( | 220 NewRunnableMethod( |
| 196 report, &MalwareDetails::FinishCollection)); | 221 report, &MalwareDetails::FinishCollection)); |
| 197 // Wait for the callback (SendSerializedMalwareDetails). | 222 // Wait for the callback (SendSerializedMalwareDetails). |
| 198 DVLOG(1) << "Waiting for SendSerializedMalwareDetails"; | 223 DVLOG(1) << "Waiting for SendSerializedMalwareDetails"; |
| 199 MessageLoop::current()->Run(); | 224 MessageLoop::current()->Run(); |
| 200 return sb_service_->GetSerialized(); | 225 return sb_service_->GetSerialized(); |
| 201 } | 226 } |
| 202 | 227 |
| 228 //TestingProfile* profile() {return profile_.get();} | |
| 229 HistoryService* history_service() { | |
| 230 return profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | |
| 231 } | |
| 232 | |
| 233 // Blocks the caller until history processes a task. This is useful if you | |
| 234 // need to wait until you know history has processed a task. | |
| 235 void WaitForHistory() { | |
|
Paweł Hajdan Jr.
2011/05/14 09:33:49
Isn't that TestingProfile::BlockUntilHistoryProces
kewang
2011/05/16 07:11:51
Good to know this! Fixed.
| |
| 236 history_service()->ScheduleDBTask(new WaitForHistoryTask(), &consumer_); | |
| 237 MessageLoop::current()->Run(); | |
| 238 } | |
| 239 | |
| 203 protected: | 240 protected: |
| 204 void InitResource(SafeBrowsingService::UnsafeResource* resource, | 241 void InitResource(SafeBrowsingService::UnsafeResource* resource, |
| 205 ResourceType::Type resource_type, | 242 ResourceType::Type resource_type, |
| 206 const GURL& url) { | 243 const GURL& url) { |
| 207 resource->client = NULL; | 244 resource->client = NULL; |
| 208 resource->url = url; | 245 resource->url = url; |
| 209 resource->resource_type = resource_type; | 246 resource->resource_type = resource_type; |
| 210 resource->threat_type = SafeBrowsingService::URL_MALWARE; | 247 resource->threat_type = SafeBrowsingService::URL_MALWARE; |
| 211 resource->render_process_host_id = contents()->GetRenderProcessHost()->id(); | 248 resource->render_process_host_id = contents()->GetRenderProcessHost()->id(); |
| 212 resource->render_view_id = contents()->render_view_host()->routing_id(); | 249 resource->render_view_id = contents()->render_view_host()->routing_id(); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 271 } | 308 } |
| 272 | 309 |
| 273 EXPECT_EQ(expected->response().body(), resource->response().body()); | 310 EXPECT_EQ(expected->response().body(), resource->response().body()); |
| 274 EXPECT_EQ(expected->response().bodylength(), | 311 EXPECT_EQ(expected->response().bodylength(), |
| 275 resource->response().bodylength()); | 312 resource->response().bodylength()); |
| 276 EXPECT_EQ(expected->response().bodydigest(), | 313 EXPECT_EQ(expected->response().bodydigest(), |
| 277 resource->response().bodydigest()); | 314 resource->response().bodydigest()); |
| 278 } | 315 } |
| 279 } | 316 } |
| 280 | 317 |
| 318 // Adds a page to history. | |
| 319 void AddPageToHistory(const GURL& url, | |
| 320 const history::RedirectList& redirects) { | |
| 321 history_service()->AddPage( | |
| 322 url, static_cast<void*>(this), 0, GURL(), PageTransition::TYPED, | |
| 323 redirects, history::SOURCE_BROWSED, false); | |
| 324 } | |
| 325 | |
| 281 BrowserThread ui_thread_; | 326 BrowserThread ui_thread_; |
| 282 BrowserThread io_thread_; | 327 BrowserThread io_thread_; |
| 283 scoped_refptr<MockSafeBrowsingService> sb_service_; | 328 scoped_refptr<MockSafeBrowsingService> sb_service_; |
| 329 scoped_ptr<TestingProfile> profile_; | |
| 330 CancelableRequestConsumer consumer_; | |
| 284 }; | 331 }; |
| 285 | 332 |
| 286 // Tests creating a simple malware report. | 333 // Tests creating a simple malware report. |
| 287 TEST_F(MalwareDetailsTest, MalwareSubResource) { | 334 TEST_F(MalwareDetailsTest, MalwareSubResource) { |
| 288 // Start a load. | 335 // Start a load. |
| 289 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); | 336 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); |
| 290 | 337 |
| 291 SafeBrowsingService::UnsafeResource resource; | 338 SafeBrowsingService::UnsafeResource resource; |
| 292 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); | 339 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); |
| 293 | 340 |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 585 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); | 632 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); |
| 586 pb_resource->set_id(0); | 633 pb_resource->set_id(0); |
| 587 pb_resource->set_url(kLandingURL); | 634 pb_resource->set_url(kLandingURL); |
| 588 pb_resource = expected.add_resources(); | 635 pb_resource = expected.add_resources(); |
| 589 pb_resource->set_id(1); | 636 pb_resource->set_id(1); |
| 590 pb_resource->set_url(kMalwareURL); | 637 pb_resource->set_url(kMalwareURL); |
| 591 expected.set_complete(true); | 638 expected.set_complete(true); |
| 592 | 639 |
| 593 VerifyResults(actual, expected); | 640 VerifyResults(actual, expected); |
| 594 } | 641 } |
| 642 | |
| 643 // Test getting redirects from history service | |
| 644 TEST_F(MalwareDetailsTest, HistoryServiceUrls) { | |
| 645 // Add content to history service | |
| 646 GURL baseurl(kMalwareURL); | |
| 647 history::RedirectList redirects; | |
| 648 redirects.push_back(GURL(kFirstRedirectURL)); | |
| 649 redirects.push_back(GURL(kSecondRedirectURL)); | |
| 650 // the last item of the redirect chain has to be the final URL | |
| 651 redirects.push_back(baseurl); | |
| 652 AddPageToHistory(baseurl, redirects); | |
| 653 WaitForHistory(); | |
| 654 | |
| 655 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); | |
| 656 | |
| 657 SafeBrowsingService::UnsafeResource resource; | |
| 658 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); | |
| 659 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap( | |
| 660 sb_service_.get(), contents(), resource, NULL); | |
| 661 | |
| 662 // Reset the history service pointer | |
| 663 report->redirects_collector_->history_ = history_service(); | |
| 664 | |
| 665 // The redirects collection starts after the IPC from the DOM is fired. | |
| 666 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node> params; | |
| 667 report->OnReceivedMalwareDOMDetails(params); | |
| 668 | |
| 669 // Let the redirects callbacks complete | |
| 670 MessageLoop::current()->RunAllPending(); | |
| 671 | |
| 672 std::string serialized = WaitForSerializedReport(report); | |
| 673 ClientMalwareReportRequest actual; | |
| 674 actual.ParseFromString(serialized); | |
| 675 | |
| 676 ClientMalwareReportRequest expected; | |
| 677 expected.set_malware_url(kMalwareURL); | |
| 678 expected.set_page_url(kLandingURL); | |
| 679 expected.set_referrer_url(""); | |
| 680 | |
| 681 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); | |
| 682 pb_resource->set_id(0); | |
| 683 pb_resource->set_url(kLandingURL); | |
| 684 pb_resource = expected.add_resources(); | |
| 685 pb_resource->set_id(1); | |
| 686 pb_resource->set_parent_id(2); | |
| 687 pb_resource->set_url(kMalwareURL); | |
| 688 pb_resource = expected.add_resources(); | |
| 689 pb_resource->set_id(2); | |
| 690 pb_resource->set_parent_id(3); | |
| 691 pb_resource->set_url(kSecondRedirectURL); | |
| 692 pb_resource = expected.add_resources(); | |
| 693 pb_resource->set_id(3); | |
| 694 pb_resource->set_url(kFirstRedirectURL); | |
| 695 | |
| 696 VerifyResults(actual, expected); | |
| 697 } | |
| OLD | NEW |