| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/pickle.h" | 8 #include "base/pickle.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 #include "chrome/browser/history/history_backend.h" | 10 #include "chrome/browser/history/history_backend.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 responseinfo.headers = new net::HttpResponseHeaders(raw_headers); | 69 responseinfo.headers = new net::HttpResponseHeaders(raw_headers); |
| 70 | 70 |
| 71 Pickle pickle; | 71 Pickle pickle; |
| 72 responseinfo.Persist(&pickle, false, false); | 72 responseinfo.Persist(&pickle, false, false); |
| 73 | 73 |
| 74 scoped_refptr<net::WrappedIOBuffer> buf(new net::WrappedIOBuffer( | 74 scoped_refptr<net::WrappedIOBuffer> buf(new net::WrappedIOBuffer( |
| 75 reinterpret_cast<const char*>(pickle.data()))); | 75 reinterpret_cast<const char*>(pickle.data()))); |
| 76 int len = static_cast<int>(pickle.size()); | 76 int len = static_cast<int>(pickle.size()); |
| 77 | 77 |
| 78 net::TestCompletionCallback cb; | 78 net::TestCompletionCallback cb; |
| 79 int rv = entry->WriteData(0, 0, buf, len, cb.callback(), true); | 79 int rv = entry->WriteData(0, 0, buf.get(), len, cb.callback(), true); |
| 80 ASSERT_EQ(len, cb.GetResult(rv)); | 80 ASSERT_EQ(len, cb.GetResult(rv)); |
| 81 } | 81 } |
| 82 | 82 |
| 83 void WriteData(disk_cache::Entry* entry, const std::string data) { | 83 void WriteData(disk_cache::Entry* entry, const std::string data) { |
| 84 if (data.empty()) | 84 if (data.empty()) |
| 85 return; | 85 return; |
| 86 | 86 |
| 87 int len = data.length(); | 87 int len = data.length(); |
| 88 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(len)); | 88 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(len)); |
| 89 memcpy(buf->data(), data.data(), data.length()); | 89 memcpy(buf->data(), data.data(), data.length()); |
| 90 | 90 |
| 91 net::TestCompletionCallback cb; | 91 net::TestCompletionCallback cb; |
| 92 int rv = entry->WriteData(1, 0, buf, len, cb.callback(), true); | 92 int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true); |
| 93 ASSERT_EQ(len, cb.GetResult(rv)); | 93 ASSERT_EQ(len, cb.GetResult(rv)); |
| 94 } | 94 } |
| 95 | 95 |
| 96 void WriteToEntry(disk_cache::Backend* cache, const std::string key, | 96 void WriteToEntry(disk_cache::Backend* cache, const std::string key, |
| 97 const std::string headers, const std::string data) { | 97 const std::string headers, const std::string data) { |
| 98 net::TestCompletionCallback cb; | 98 net::TestCompletionCallback cb; |
| 99 disk_cache::Entry* entry; | 99 disk_cache::Entry* entry; |
| 100 int rv = cache->CreateEntry(key, &entry, cb.callback()); | 100 int rv = cache->CreateEntry(key, &entry, cb.callback()); |
| 101 rv = cb.GetResult(rv); | 101 rv = cb.GetResult(rv); |
| 102 if (rv != net::OK) { | 102 if (rv != net::OK) { |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 | 326 |
| 327 // Tests creating a simple malware report. | 327 // Tests creating a simple malware report. |
| 328 TEST_F(MalwareDetailsTest, MalwareSubResource) { | 328 TEST_F(MalwareDetailsTest, MalwareSubResource) { |
| 329 // Start a load. | 329 // Start a load. |
| 330 controller().LoadURL(GURL(kLandingURL), content::Referrer(), | 330 controller().LoadURL(GURL(kLandingURL), content::Referrer(), |
| 331 content::PAGE_TRANSITION_TYPED, std::string()); | 331 content::PAGE_TRANSITION_TYPED, std::string()); |
| 332 | 332 |
| 333 UnsafeResource resource; | 333 UnsafeResource resource; |
| 334 InitResource(&resource, true, GURL(kMalwareURL)); | 334 InitResource(&resource, true, GURL(kMalwareURL)); |
| 335 | 335 |
| 336 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap( | 336 scoped_refptr<MalwareDetailsWrap> report = |
| 337 ui_manager_, web_contents(), resource, NULL); | 337 new MalwareDetailsWrap(ui_manager_.get(), web_contents(), resource, NULL); |
| 338 | 338 |
| 339 std::string serialized = WaitForSerializedReport(report); | 339 std::string serialized = WaitForSerializedReport(report.get()); |
| 340 | 340 |
| 341 ClientMalwareReportRequest actual; | 341 ClientMalwareReportRequest actual; |
| 342 actual.ParseFromString(serialized); | 342 actual.ParseFromString(serialized); |
| 343 | 343 |
| 344 ClientMalwareReportRequest expected; | 344 ClientMalwareReportRequest expected; |
| 345 expected.set_malware_url(kMalwareURL); | 345 expected.set_malware_url(kMalwareURL); |
| 346 expected.set_page_url(kLandingURL); | 346 expected.set_page_url(kLandingURL); |
| 347 expected.set_referrer_url(""); | 347 expected.set_referrer_url(""); |
| 348 | 348 |
| 349 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); | 349 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 362 controller().LoadURL(GURL(kLandingURL), content::Referrer(), | 362 controller().LoadURL(GURL(kLandingURL), content::Referrer(), |
| 363 content::PAGE_TRANSITION_TYPED, std::string()); | 363 content::PAGE_TRANSITION_TYPED, std::string()); |
| 364 | 364 |
| 365 UnsafeResource resource; | 365 UnsafeResource resource; |
| 366 InitResource(&resource, true, GURL(kMalwareURL)); | 366 InitResource(&resource, true, GURL(kMalwareURL)); |
| 367 resource.original_url = GURL(kOriginalLandingURL); | 367 resource.original_url = GURL(kOriginalLandingURL); |
| 368 | 368 |
| 369 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap( | 369 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap( |
| 370 ui_manager_.get(), web_contents(), resource, NULL); | 370 ui_manager_.get(), web_contents(), resource, NULL); |
| 371 | 371 |
| 372 std::string serialized = WaitForSerializedReport(report); | 372 std::string serialized = WaitForSerializedReport(report.get()); |
| 373 | 373 |
| 374 ClientMalwareReportRequest actual; | 374 ClientMalwareReportRequest actual; |
| 375 actual.ParseFromString(serialized); | 375 actual.ParseFromString(serialized); |
| 376 | 376 |
| 377 ClientMalwareReportRequest expected; | 377 ClientMalwareReportRequest expected; |
| 378 expected.set_malware_url(kMalwareURL); | 378 expected.set_malware_url(kMalwareURL); |
| 379 expected.set_page_url(kLandingURL); | 379 expected.set_page_url(kLandingURL); |
| 380 expected.set_referrer_url(""); | 380 expected.set_referrer_url(""); |
| 381 | 381 |
| 382 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); | 382 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 child_node.parent = GURL(kDOMParentURL); | 416 child_node.parent = GURL(kDOMParentURL); |
| 417 params.push_back(child_node); | 417 params.push_back(child_node); |
| 418 SafeBrowsingHostMsg_MalwareDOMDetails_Node parent_node; | 418 SafeBrowsingHostMsg_MalwareDOMDetails_Node parent_node; |
| 419 parent_node.url = GURL(kDOMParentURL); | 419 parent_node.url = GURL(kDOMParentURL); |
| 420 parent_node.children.push_back(GURL(kDOMChildURL)); | 420 parent_node.children.push_back(GURL(kDOMChildURL)); |
| 421 params.push_back(parent_node); | 421 params.push_back(parent_node); |
| 422 report->OnReceivedMalwareDOMDetails(params); | 422 report->OnReceivedMalwareDOMDetails(params); |
| 423 | 423 |
| 424 base::MessageLoop::current()->RunUntilIdle(); | 424 base::MessageLoop::current()->RunUntilIdle(); |
| 425 | 425 |
| 426 std::string serialized = WaitForSerializedReport(report); | 426 std::string serialized = WaitForSerializedReport(report.get()); |
| 427 ClientMalwareReportRequest actual; | 427 ClientMalwareReportRequest actual; |
| 428 actual.ParseFromString(serialized); | 428 actual.ParseFromString(serialized); |
| 429 | 429 |
| 430 ClientMalwareReportRequest expected; | 430 ClientMalwareReportRequest expected; |
| 431 expected.set_malware_url(kMalwareURL); | 431 expected.set_malware_url(kMalwareURL); |
| 432 expected.set_page_url(kLandingURL); | 432 expected.set_page_url(kLandingURL); |
| 433 expected.set_referrer_url(""); | 433 expected.set_referrer_url(""); |
| 434 | 434 |
| 435 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); | 435 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); |
| 436 pb_resource->set_id(0); | 436 pb_resource->set_id(0); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 456 | 456 |
| 457 // Verify that https:// urls are dropped. | 457 // Verify that https:// urls are dropped. |
| 458 TEST_F(MalwareDetailsTest, NotPublicUrl) { | 458 TEST_F(MalwareDetailsTest, NotPublicUrl) { |
| 459 controller().LoadURL(GURL(kHttpsURL), content::Referrer(), | 459 controller().LoadURL(GURL(kHttpsURL), content::Referrer(), |
| 460 content::PAGE_TRANSITION_TYPED, std::string()); | 460 content::PAGE_TRANSITION_TYPED, std::string()); |
| 461 UnsafeResource resource; | 461 UnsafeResource resource; |
| 462 InitResource(&resource, true, GURL(kMalwareURL)); | 462 InitResource(&resource, true, GURL(kMalwareURL)); |
| 463 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap( | 463 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap( |
| 464 ui_manager_.get(), web_contents(), resource, NULL); | 464 ui_manager_.get(), web_contents(), resource, NULL); |
| 465 | 465 |
| 466 std::string serialized = WaitForSerializedReport(report); | 466 std::string serialized = WaitForSerializedReport(report.get()); |
| 467 ClientMalwareReportRequest actual; | 467 ClientMalwareReportRequest actual; |
| 468 actual.ParseFromString(serialized); | 468 actual.ParseFromString(serialized); |
| 469 | 469 |
| 470 ClientMalwareReportRequest expected; | 470 ClientMalwareReportRequest expected; |
| 471 expected.set_malware_url(kMalwareURL); // No page_url | 471 expected.set_malware_url(kMalwareURL); // No page_url |
| 472 expected.set_referrer_url(""); | 472 expected.set_referrer_url(""); |
| 473 | 473 |
| 474 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); | 474 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); |
| 475 pb_resource->set_url(kMalwareURL); // Only one resource | 475 pb_resource->set_url(kMalwareURL); // Only one resource |
| 476 | 476 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 488 resource.original_url = GURL(kOriginalLandingURL); | 488 resource.original_url = GURL(kOriginalLandingURL); |
| 489 | 489 |
| 490 // add some redirect urls | 490 // add some redirect urls |
| 491 resource.redirect_urls.push_back(GURL(kFirstRedirectURL)); | 491 resource.redirect_urls.push_back(GURL(kFirstRedirectURL)); |
| 492 resource.redirect_urls.push_back(GURL(kSecondRedirectURL)); | 492 resource.redirect_urls.push_back(GURL(kSecondRedirectURL)); |
| 493 resource.redirect_urls.push_back(GURL(kMalwareURL)); | 493 resource.redirect_urls.push_back(GURL(kMalwareURL)); |
| 494 | 494 |
| 495 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap( | 495 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap( |
| 496 ui_manager_.get(), web_contents(), resource, NULL); | 496 ui_manager_.get(), web_contents(), resource, NULL); |
| 497 | 497 |
| 498 std::string serialized = WaitForSerializedReport(report); | 498 std::string serialized = WaitForSerializedReport(report.get()); |
| 499 ClientMalwareReportRequest actual; | 499 ClientMalwareReportRequest actual; |
| 500 actual.ParseFromString(serialized); | 500 actual.ParseFromString(serialized); |
| 501 | 501 |
| 502 ClientMalwareReportRequest expected; | 502 ClientMalwareReportRequest expected; |
| 503 expected.set_malware_url(kMalwareURL); | 503 expected.set_malware_url(kMalwareURL); |
| 504 expected.set_page_url(kLandingURL); | 504 expected.set_page_url(kLandingURL); |
| 505 expected.set_referrer_url(""); | 505 expected.set_referrer_url(""); |
| 506 | 506 |
| 507 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); | 507 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); |
| 508 pb_resource->set_id(0); | 508 pb_resource->set_id(0); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 make_scoped_refptr(profile()->GetRequestContext()))); | 549 make_scoped_refptr(profile()->GetRequestContext()))); |
| 550 | 550 |
| 551 // The cache collection starts after the IPC from the DOM is fired. | 551 // The cache collection starts after the IPC from the DOM is fired. |
| 552 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node> params; | 552 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node> params; |
| 553 report->OnReceivedMalwareDOMDetails(params); | 553 report->OnReceivedMalwareDOMDetails(params); |
| 554 | 554 |
| 555 // Let the cache callbacks complete | 555 // Let the cache callbacks complete |
| 556 base::MessageLoop::current()->RunUntilIdle(); | 556 base::MessageLoop::current()->RunUntilIdle(); |
| 557 | 557 |
| 558 DVLOG(1) << "Getting serialized report"; | 558 DVLOG(1) << "Getting serialized report"; |
| 559 std::string serialized = WaitForSerializedReport(report); | 559 std::string serialized = WaitForSerializedReport(report.get()); |
| 560 ClientMalwareReportRequest actual; | 560 ClientMalwareReportRequest actual; |
| 561 actual.ParseFromString(serialized); | 561 actual.ParseFromString(serialized); |
| 562 | 562 |
| 563 ClientMalwareReportRequest expected; | 563 ClientMalwareReportRequest expected; |
| 564 expected.set_malware_url(kMalwareURL); | 564 expected.set_malware_url(kMalwareURL); |
| 565 expected.set_page_url(kLandingURL); | 565 expected.set_page_url(kLandingURL); |
| 566 expected.set_referrer_url(""); | 566 expected.set_referrer_url(""); |
| 567 | 567 |
| 568 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); | 568 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); |
| 569 pb_resource->set_id(0); | 569 pb_resource->set_id(0); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 620 // No call to FillCache | 620 // No call to FillCache |
| 621 | 621 |
| 622 // The cache collection starts after the IPC from the DOM is fired. | 622 // The cache collection starts after the IPC from the DOM is fired. |
| 623 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node> params; | 623 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node> params; |
| 624 report->OnReceivedMalwareDOMDetails(params); | 624 report->OnReceivedMalwareDOMDetails(params); |
| 625 | 625 |
| 626 // Let the cache callbacks complete | 626 // Let the cache callbacks complete |
| 627 base::MessageLoop::current()->RunUntilIdle(); | 627 base::MessageLoop::current()->RunUntilIdle(); |
| 628 | 628 |
| 629 DVLOG(1) << "Getting serialized report"; | 629 DVLOG(1) << "Getting serialized report"; |
| 630 std::string serialized = WaitForSerializedReport(report); | 630 std::string serialized = WaitForSerializedReport(report.get()); |
| 631 ClientMalwareReportRequest actual; | 631 ClientMalwareReportRequest actual; |
| 632 actual.ParseFromString(serialized); | 632 actual.ParseFromString(serialized); |
| 633 | 633 |
| 634 ClientMalwareReportRequest expected; | 634 ClientMalwareReportRequest expected; |
| 635 expected.set_malware_url(kMalwareURL); | 635 expected.set_malware_url(kMalwareURL); |
| 636 expected.set_page_url(kLandingURL); | 636 expected.set_page_url(kLandingURL); |
| 637 expected.set_referrer_url(""); | 637 expected.set_referrer_url(""); |
| 638 | 638 |
| 639 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); | 639 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); |
| 640 pb_resource->set_id(0); | 640 pb_resource->set_id(0); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 668 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap( | 668 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap( |
| 669 ui_manager_.get(), web_contents(), resource, NULL); | 669 ui_manager_.get(), web_contents(), resource, NULL); |
| 670 | 670 |
| 671 // The redirects collection starts after the IPC from the DOM is fired. | 671 // The redirects collection starts after the IPC from the DOM is fired. |
| 672 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node> params; | 672 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node> params; |
| 673 report->OnReceivedMalwareDOMDetails(params); | 673 report->OnReceivedMalwareDOMDetails(params); |
| 674 | 674 |
| 675 // Let the redirects callbacks complete. | 675 // Let the redirects callbacks complete. |
| 676 base::MessageLoop::current()->RunUntilIdle(); | 676 base::MessageLoop::current()->RunUntilIdle(); |
| 677 | 677 |
| 678 std::string serialized = WaitForSerializedReport(report); | 678 std::string serialized = WaitForSerializedReport(report.get()); |
| 679 ClientMalwareReportRequest actual; | 679 ClientMalwareReportRequest actual; |
| 680 actual.ParseFromString(serialized); | 680 actual.ParseFromString(serialized); |
| 681 | 681 |
| 682 ClientMalwareReportRequest expected; | 682 ClientMalwareReportRequest expected; |
| 683 expected.set_malware_url(kMalwareURL); | 683 expected.set_malware_url(kMalwareURL); |
| 684 expected.set_page_url(kLandingURL); | 684 expected.set_page_url(kLandingURL); |
| 685 expected.set_referrer_url(""); | 685 expected.set_referrer_url(""); |
| 686 | 686 |
| 687 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); | 687 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); |
| 688 pb_resource->set_id(0); | 688 pb_resource->set_id(0); |
| 689 pb_resource->set_url(kLandingURL); | 689 pb_resource->set_url(kLandingURL); |
| 690 pb_resource = expected.add_resources(); | 690 pb_resource = expected.add_resources(); |
| 691 pb_resource->set_id(1); | 691 pb_resource->set_id(1); |
| 692 pb_resource->set_parent_id(2); | 692 pb_resource->set_parent_id(2); |
| 693 pb_resource->set_url(kMalwareURL); | 693 pb_resource->set_url(kMalwareURL); |
| 694 pb_resource = expected.add_resources(); | 694 pb_resource = expected.add_resources(); |
| 695 pb_resource->set_id(2); | 695 pb_resource->set_id(2); |
| 696 pb_resource->set_parent_id(3); | 696 pb_resource->set_parent_id(3); |
| 697 pb_resource->set_url(kSecondRedirectURL); | 697 pb_resource->set_url(kSecondRedirectURL); |
| 698 pb_resource = expected.add_resources(); | 698 pb_resource = expected.add_resources(); |
| 699 pb_resource->set_id(3); | 699 pb_resource->set_id(3); |
| 700 pb_resource->set_url(kFirstRedirectURL); | 700 pb_resource->set_url(kFirstRedirectURL); |
| 701 | 701 |
| 702 VerifyResults(actual, expected); | 702 VerifyResults(actual, expected); |
| 703 } | 703 } |
| OLD | NEW |