OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 #include <algorithm> |
| 6 #include "chrome/browser/renderer_host/test/test_render_view_host.h" |
| 7 |
| 8 #include "chrome/browser/browser_thread.h" |
| 9 #include "chrome/browser/safe_browsing/malware_details.h" |
| 10 #include "chrome/browser/safe_browsing/report.pb.h" |
| 11 #include "chrome/browser/tab_contents/navigation_entry.h" |
| 12 #include "chrome/browser/tab_contents/test_tab_contents.h" |
| 13 #include "chrome/common/render_messages.h" |
| 14 #include "chrome/common/render_messages_params.h" |
| 15 |
| 16 static const char* kOriginalLandingURL = "http://www.originallandingpage.com/"; |
| 17 static const char* kLandingURL = "http://www.landingpage.com/"; |
| 18 static const char* kMalwareURL = "http://www.malware.com/"; |
| 19 static const char* kHttpsURL = "https://www.url.com/"; |
| 20 |
| 21 class MalwareDetailsTest : public RenderViewHostTestHarness { |
| 22 public: |
| 23 MalwareDetailsTest() |
| 24 : ui_thread_(BrowserThread::UI, MessageLoop::current()), |
| 25 io_thread_(BrowserThread::IO, MessageLoop::current()) { |
| 26 } |
| 27 |
| 28 virtual void SetUp() { |
| 29 RenderViewHostTestHarness::SetUp(); |
| 30 } |
| 31 |
| 32 static bool ResourceLessThan( |
| 33 const safe_browsing::ClientMalwareReportRequest::Resource* lhs, |
| 34 const safe_browsing::ClientMalwareReportRequest::Resource* rhs) { |
| 35 return lhs->url() < rhs->url(); |
| 36 } |
| 37 |
| 38 protected: |
| 39 void InitResource(SafeBrowsingService::UnsafeResource* resource, |
| 40 ResourceType::Type resource_type, |
| 41 const GURL& url) { |
| 42 resource->client = NULL; |
| 43 resource->url = url; |
| 44 resource->resource_type = resource_type; |
| 45 resource->threat_type = SafeBrowsingService::URL_MALWARE; |
| 46 resource->render_process_host_id = contents_->GetRenderProcessHost()->id(); |
| 47 resource->render_view_id = contents_->render_view_host()->routing_id(); |
| 48 } |
| 49 |
| 50 void VerifyResults( |
| 51 const safe_browsing::ClientMalwareReportRequest& report_pb, |
| 52 const safe_browsing::ClientMalwareReportRequest& expected_pb) { |
| 53 EXPECT_EQ(expected_pb.malware_url(), report_pb.malware_url()); |
| 54 EXPECT_EQ(expected_pb.page_url(), report_pb.page_url()); |
| 55 EXPECT_EQ(expected_pb.referrer_url(), report_pb.referrer_url()); |
| 56 |
| 57 ASSERT_EQ(expected_pb.nodes_size(), report_pb.nodes_size()); |
| 58 // Sort the nodes, to make the test deterministic |
| 59 std::vector<const safe_browsing::ClientMalwareReportRequest::Resource*> |
| 60 nodes; |
| 61 for (int i = 0; i < report_pb.nodes_size(); ++i) { |
| 62 const safe_browsing::ClientMalwareReportRequest::Resource& resource = |
| 63 report_pb.nodes(i); |
| 64 nodes.push_back(&resource); |
| 65 } |
| 66 std::sort(nodes.begin(), nodes.end(), |
| 67 &MalwareDetailsTest::ResourceLessThan); |
| 68 |
| 69 std::vector<const safe_browsing::ClientMalwareReportRequest::Resource*> |
| 70 expected; |
| 71 for (int i = 0; i < report_pb.nodes_size(); ++i) { |
| 72 const safe_browsing::ClientMalwareReportRequest::Resource& resource = |
| 73 expected_pb.nodes(i); |
| 74 expected.push_back(&resource); |
| 75 } |
| 76 std::sort(expected.begin(), expected.end(), |
| 77 &MalwareDetailsTest::ResourceLessThan); |
| 78 |
| 79 for (uint32 i = 0; i < expected.size(); ++i) { |
| 80 EXPECT_EQ(expected[i]->url(), nodes[i]->url()); |
| 81 EXPECT_EQ(expected[i]->parent(), nodes[i]->parent()); |
| 82 } |
| 83 } |
| 84 |
| 85 BrowserThread ui_thread_; |
| 86 BrowserThread io_thread_; |
| 87 }; |
| 88 |
| 89 // Tests creating a simple malware report. |
| 90 TEST_F(MalwareDetailsTest, MalwareSubResource) { |
| 91 // Start a load. |
| 92 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); |
| 93 |
| 94 SafeBrowsingService::UnsafeResource resource; |
| 95 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); |
| 96 |
| 97 MalwareDetails* report = new MalwareDetails(contents(), resource); |
| 98 |
| 99 scoped_ptr<const std::string> serialized(report->GetSerializedReport()); |
| 100 safe_browsing::ClientMalwareReportRequest actual; |
| 101 actual.ParseFromString(*serialized); |
| 102 |
| 103 safe_browsing::ClientMalwareReportRequest expected; |
| 104 expected.set_malware_url(kMalwareURL); |
| 105 expected.set_page_url(kLandingURL); |
| 106 expected.set_referrer_url(""); |
| 107 |
| 108 safe_browsing::ClientMalwareReportRequest::Resource* node = |
| 109 expected.add_nodes(); |
| 110 node->set_url(kLandingURL); |
| 111 node = expected.add_nodes(); |
| 112 node->set_url(kMalwareURL); |
| 113 |
| 114 VerifyResults(actual, expected); |
| 115 } |
| 116 |
| 117 // Tests creating a simple malware report where the subresource has a |
| 118 // different original_url. |
| 119 TEST_F(MalwareDetailsTest, MalwareSubResourceWithOriginalUrl) { |
| 120 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); |
| 121 |
| 122 SafeBrowsingService::UnsafeResource resource; |
| 123 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); |
| 124 resource.original_url = GURL(kOriginalLandingURL); |
| 125 |
| 126 MalwareDetails* report = new MalwareDetails(contents(), resource); |
| 127 |
| 128 scoped_ptr<const std::string> serialized(report->GetSerializedReport()); |
| 129 safe_browsing::ClientMalwareReportRequest actual; |
| 130 actual.ParseFromString(*serialized); |
| 131 |
| 132 safe_browsing::ClientMalwareReportRequest expected; |
| 133 expected.set_malware_url(kMalwareURL); |
| 134 expected.set_page_url(kLandingURL); |
| 135 expected.set_referrer_url(""); |
| 136 |
| 137 safe_browsing::ClientMalwareReportRequest::Resource* node = |
| 138 expected.add_nodes(); |
| 139 node->set_url(kLandingURL); |
| 140 |
| 141 // Malware url should have originalurl as parent. |
| 142 node = expected.add_nodes(); |
| 143 node->set_url(kMalwareURL); |
| 144 node->set_parent(kOriginalLandingURL); |
| 145 |
| 146 node = expected.add_nodes(); |
| 147 node->set_url(kOriginalLandingURL); |
| 148 |
| 149 VerifyResults(actual, expected); |
| 150 } |
| 151 |
| 152 // Verify that https:// urls are dropped. |
| 153 TEST_F(MalwareDetailsTest, NotPublicUrl) { |
| 154 controller().LoadURL(GURL(kHttpsURL), GURL(), PageTransition::TYPED); |
| 155 SafeBrowsingService::UnsafeResource resource; |
| 156 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); |
| 157 MalwareDetails* report = new MalwareDetails(contents(), resource); |
| 158 |
| 159 scoped_ptr<const std::string> serialized(report->GetSerializedReport()); |
| 160 safe_browsing::ClientMalwareReportRequest actual; |
| 161 actual.ParseFromString(*serialized); |
| 162 |
| 163 safe_browsing::ClientMalwareReportRequest expected; |
| 164 expected.set_malware_url(kMalwareURL); // No page_url |
| 165 expected.set_referrer_url(""); |
| 166 |
| 167 safe_browsing::ClientMalwareReportRequest::Resource* node = |
| 168 expected.add_nodes(); |
| 169 node->set_url(kMalwareURL); // Only one node |
| 170 |
| 171 VerifyResults(actual, expected); |
| 172 } |
OLD | NEW |