Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(574)

Side by Side Diff: chrome/browser/safe_browsing/malware_details_unittest.cc

Issue 6208003: Add a Node message in the malware details protocol buffer. This allows us to ... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 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 #include "chrome/browser/renderer_host/test/test_render_view_host.h" 6 #include "chrome/browser/renderer_host/test/test_render_view_host.h"
7 7
8 #include "chrome/browser/browser_thread.h" 8 #include "chrome/browser/browser_thread.h"
9 #include "chrome/browser/safe_browsing/malware_details.h" 9 #include "chrome/browser/safe_browsing/malware_details.h"
10 #include "chrome/browser/safe_browsing/report.pb.h" 10 #include "chrome/browser/safe_browsing/report.pb.h"
11 #include "chrome/browser/tab_contents/navigation_entry.h" 11 #include "chrome/browser/tab_contents/navigation_entry.h"
12 #include "chrome/browser/tab_contents/test_tab_contents.h" 12 #include "chrome/browser/tab_contents/test_tab_contents.h"
13 #include "chrome/common/render_messages.h" 13 #include "chrome/common/render_messages.h"
14 #include "chrome/common/render_messages_params.h" 14 #include "chrome/common/render_messages_params.h"
15 15
16 static const char* kOriginalLandingURL = "http://www.originallandingpage.com/"; 16 static const char* kOriginalLandingURL = "http://www.originallandingpage.com/";
17 static const char* kLandingURL = "http://www.landingpage.com/"; 17 static const char* kLandingURL = "http://www.landingpage.com/";
18 static const char* kMalwareURL = "http://www.malware.com/"; 18 static const char* kMalwareURL = "http://www.malware.com/";
19 static const char* kHttpsURL = "https://www.url.com/"; 19 static const char* kHttpsURL = "https://www.url.com/";
20 20
21 using safe_browsing::ClientMalwareReportRequest;
22
21 class MalwareDetailsTest : public RenderViewHostTestHarness { 23 class MalwareDetailsTest : public RenderViewHostTestHarness {
22 public: 24 public:
23 MalwareDetailsTest() 25 MalwareDetailsTest()
24 : ui_thread_(BrowserThread::UI, MessageLoop::current()), 26 : ui_thread_(BrowserThread::UI, MessageLoop::current()),
25 io_thread_(BrowserThread::IO, MessageLoop::current()) { 27 io_thread_(BrowserThread::IO, MessageLoop::current()) {
26 } 28 }
27 29
28 virtual void SetUp() { 30 virtual void SetUp() {
29 RenderViewHostTestHarness::SetUp(); 31 RenderViewHostTestHarness::SetUp();
30 } 32 }
31 33
32 static bool ResourceLessThan( 34 static bool ResourceLessThan(
33 const safe_browsing::ClientMalwareReportRequest::Resource* lhs, 35 const ClientMalwareReportRequest::Resource* lhs,
34 const safe_browsing::ClientMalwareReportRequest::Resource* rhs) { 36 const ClientMalwareReportRequest::Resource* rhs) {
35 return lhs->url() < rhs->url(); 37 return lhs->id() < rhs->id();
38 }
39
40 static bool NodeLessThan(
41 const ClientMalwareReportRequest::Node* lhs,
42 const ClientMalwareReportRequest::Node* rhs) {
43 return lhs->id() < rhs->id();
36 } 44 }
37 45
38 protected: 46 protected:
39 void InitResource(SafeBrowsingService::UnsafeResource* resource, 47 void InitResource(SafeBrowsingService::UnsafeResource* resource,
40 ResourceType::Type resource_type, 48 ResourceType::Type resource_type,
41 const GURL& url) { 49 const GURL& url) {
42 resource->client = NULL; 50 resource->client = NULL;
43 resource->url = url; 51 resource->url = url;
44 resource->resource_type = resource_type; 52 resource->resource_type = resource_type;
45 resource->threat_type = SafeBrowsingService::URL_MALWARE; 53 resource->threat_type = SafeBrowsingService::URL_MALWARE;
46 resource->render_process_host_id = contents_->GetRenderProcessHost()->id(); 54 resource->render_process_host_id = contents_->GetRenderProcessHost()->id();
47 resource->render_view_id = contents_->render_view_host()->routing_id(); 55 resource->render_view_id = contents_->render_view_host()->routing_id();
48 } 56 }
49 57
50 void VerifyResults( 58 void VerifyResults(const ClientMalwareReportRequest& report_pb,
51 const safe_browsing::ClientMalwareReportRequest& report_pb, 59 const ClientMalwareReportRequest& expected_pb) {
52 const safe_browsing::ClientMalwareReportRequest& expected_pb) {
53 EXPECT_EQ(expected_pb.malware_url(), report_pb.malware_url()); 60 EXPECT_EQ(expected_pb.malware_url(), report_pb.malware_url());
54 EXPECT_EQ(expected_pb.page_url(), report_pb.page_url()); 61 EXPECT_EQ(expected_pb.page_url(), report_pb.page_url());
55 EXPECT_EQ(expected_pb.referrer_url(), report_pb.referrer_url()); 62 EXPECT_EQ(expected_pb.referrer_url(), report_pb.referrer_url());
56 63
57 ASSERT_EQ(expected_pb.nodes_size(), report_pb.nodes_size()); 64 ASSERT_EQ(expected_pb.resources_size(), report_pb.resources_size());
58 // Sort the nodes, to make the test deterministic 65 // Sort the resources and nodes, to make the test deterministic
59 std::vector<const safe_browsing::ClientMalwareReportRequest::Resource*> 66 std::vector<const ClientMalwareReportRequest::Resource*> resources;
60 nodes; 67 for (int i = 0; i < report_pb.resources_size(); ++i) {
61 for (int i = 0; i < report_pb.nodes_size(); ++i) { 68 const ClientMalwareReportRequest::Resource& resource =
62 const safe_browsing::ClientMalwareReportRequest::Resource& resource = 69 report_pb.resources(i);
63 report_pb.nodes(i); 70 resources.push_back(&resource);
64 nodes.push_back(&resource);
65 } 71 }
66 std::sort(nodes.begin(), nodes.end(), 72 std::sort(resources.begin(), resources.end(),
67 &MalwareDetailsTest::ResourceLessThan); 73 &MalwareDetailsTest::ResourceLessThan);
68 74
69 std::vector<const safe_browsing::ClientMalwareReportRequest::Resource*> 75 std::vector<const ClientMalwareReportRequest::Resource*> expected;
70 expected; 76 for (int i = 0; i < report_pb.resources_size(); ++i) {
71 for (int i = 0; i < report_pb.nodes_size(); ++i) { 77 const ClientMalwareReportRequest::Resource& resource =
72 const safe_browsing::ClientMalwareReportRequest::Resource& resource = 78 expected_pb.resources(i);
73 expected_pb.nodes(i);
74 expected.push_back(&resource); 79 expected.push_back(&resource);
75 } 80 }
76 std::sort(expected.begin(), expected.end(), 81 std::sort(expected.begin(), expected.end(),
77 &MalwareDetailsTest::ResourceLessThan); 82 &MalwareDetailsTest::ResourceLessThan);
78 83
79 for (uint32 i = 0; i < expected.size(); ++i) { 84 for (uint32 i = 0; i < expected.size(); ++i) {
80 EXPECT_EQ(expected[i]->url(), nodes[i]->url()); 85 EXPECT_EQ(expected[i]->id(), resources[i]->id());
81 EXPECT_EQ(expected[i]->parent(), nodes[i]->parent()); 86 EXPECT_EQ(expected[i]->url(), resources[i]->url());
87 }
88
89 ASSERT_EQ(expected_pb.nodes_size(), report_pb.nodes_size());
90 // Sort the nodes, to make the test deterministic
91 std::vector<const ClientMalwareReportRequest::Node*> nodes;
92 for (int i = 0; i < report_pb.nodes_size(); ++i) {
93 const ClientMalwareReportRequest::Node& node = report_pb.nodes(i);
94 nodes.push_back(&node);
95 }
96 std::sort(nodes.begin(), nodes.end(), &MalwareDetailsTest::NodeLessThan);
97
98 std::vector<const ClientMalwareReportRequest::Node*> expected_nodes;
99 for (int i = 0; i < report_pb.nodes_size(); ++i) {
100 const ClientMalwareReportRequest::Node& node = expected_pb.nodes(i);
101 expected_nodes.push_back(&node);
102 }
103 std::sort(expected_nodes.begin(), expected_nodes.end(),
104 &MalwareDetailsTest::NodeLessThan);
105
106 for (uint32 i = 0; i < expected_nodes.size(); ++i) {
107 EXPECT_EQ(expected_nodes[i]->id(), nodes[i]->id());
108 EXPECT_EQ(expected_nodes[i]->parent_id(), nodes[i]->parent_id());
82 } 109 }
83 } 110 }
84 111
85 BrowserThread ui_thread_; 112 BrowserThread ui_thread_;
86 BrowserThread io_thread_; 113 BrowserThread io_thread_;
87 }; 114 };
88 115
89 // Tests creating a simple malware report. 116 // Tests creating a simple malware report.
90 TEST_F(MalwareDetailsTest, MalwareSubResource) { 117 TEST_F(MalwareDetailsTest, MalwareSubResource) {
91 // Start a load. 118 // Start a load.
92 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); 119 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED);
93 120
94 SafeBrowsingService::UnsafeResource resource; 121 SafeBrowsingService::UnsafeResource resource;
95 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); 122 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
96 123
97 scoped_refptr<MalwareDetails> report = new MalwareDetails( 124 scoped_refptr<MalwareDetails> report = new MalwareDetails(
98 contents(), resource); 125 contents(), resource);
99 126
100 scoped_ptr<const std::string> serialized(report->GetSerializedReport()); 127 scoped_ptr<const std::string> serialized(report->GetSerializedReport());
101 safe_browsing::ClientMalwareReportRequest actual; 128 ClientMalwareReportRequest actual;
102 actual.ParseFromString(*serialized); 129 actual.ParseFromString(*serialized);
103 130
104 safe_browsing::ClientMalwareReportRequest expected; 131 ClientMalwareReportRequest expected;
105 expected.set_malware_url(kMalwareURL); 132 expected.set_malware_url(kMalwareURL);
106 expected.set_page_url(kLandingURL); 133 expected.set_page_url(kLandingURL);
107 expected.set_referrer_url(""); 134 expected.set_referrer_url("");
108 135
109 safe_browsing::ClientMalwareReportRequest::Resource* node = 136 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources();
110 expected.add_nodes(); 137 pb_resource->set_id(0);
111 node->set_url(kLandingURL); 138 pb_resource->set_url(kLandingURL);
139 pb_resource = expected.add_resources();
140 pb_resource->set_id(1);
141 pb_resource->set_url(kMalwareURL);
142
143 ClientMalwareReportRequest::Node* node = expected.add_nodes();
144 node->set_id(0);
112 node = expected.add_nodes(); 145 node = expected.add_nodes();
113 node->set_url(kMalwareURL); 146 node->set_id(1);
114 147
115 VerifyResults(actual, expected); 148 VerifyResults(actual, expected);
116 } 149 }
117 150
118 // Tests creating a simple malware report where the subresource has a 151 // Tests creating a simple malware report where the subresource has a
119 // different original_url. 152 // different original_url.
120 TEST_F(MalwareDetailsTest, MalwareSubResourceWithOriginalUrl) { 153 TEST_F(MalwareDetailsTest, MalwareSubResourceWithOriginalUrl) {
121 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); 154 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED);
122 155
123 SafeBrowsingService::UnsafeResource resource; 156 SafeBrowsingService::UnsafeResource resource;
124 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); 157 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
125 resource.original_url = GURL(kOriginalLandingURL); 158 resource.original_url = GURL(kOriginalLandingURL);
126 159
127 scoped_refptr<MalwareDetails> report = new MalwareDetails( 160 scoped_refptr<MalwareDetails> report = new MalwareDetails(
128 contents(), resource); 161 contents(), resource);
129 162
130 scoped_ptr<const std::string> serialized(report->GetSerializedReport()); 163 scoped_ptr<const std::string> serialized(report->GetSerializedReport());
131 safe_browsing::ClientMalwareReportRequest actual; 164 ClientMalwareReportRequest actual;
132 actual.ParseFromString(*serialized); 165 actual.ParseFromString(*serialized);
133 166
134 safe_browsing::ClientMalwareReportRequest expected; 167 ClientMalwareReportRequest expected;
135 expected.set_malware_url(kMalwareURL); 168 expected.set_malware_url(kMalwareURL);
136 expected.set_page_url(kLandingURL); 169 expected.set_page_url(kLandingURL);
137 expected.set_referrer_url(""); 170 expected.set_referrer_url("");
138 171
139 safe_browsing::ClientMalwareReportRequest::Resource* node = 172 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources();
140 expected.add_nodes(); 173 pb_resource->set_id(0);
141 node->set_url(kLandingURL); 174 pb_resource->set_url(kLandingURL);
142 175
143 // Malware url should have originalurl as parent. 176 pb_resource = expected.add_resources();
177 pb_resource->set_id(1);
178 pb_resource->set_url(kOriginalLandingURL);
179
180 pb_resource = expected.add_resources();
181 pb_resource->set_id(2);
182 pb_resource->set_url(kMalwareURL);
183
184 ClientMalwareReportRequest::Node* node = expected.add_nodes();
185 node->set_id(0);
144 node = expected.add_nodes(); 186 node = expected.add_nodes();
145 node->set_url(kMalwareURL); 187 node->set_id(1);
146 node->set_parent(kOriginalLandingURL); 188 // The Node for kMmalwareUrl (with id 2) should have the Node for
147 189 // kOriginalLandingURL (with id 1) as parent.
148 node = expected.add_nodes(); 190 node = expected.add_nodes();
149 node->set_url(kOriginalLandingURL); 191 node->set_id(2);
192 node->set_parent_id(1);
150 193
151 VerifyResults(actual, expected); 194 VerifyResults(actual, expected);
152 } 195 }
153 196
154 // Verify that https:// urls are dropped. 197 // Verify that https:// urls are dropped.
155 TEST_F(MalwareDetailsTest, NotPublicUrl) { 198 TEST_F(MalwareDetailsTest, NotPublicUrl) {
156 controller().LoadURL(GURL(kHttpsURL), GURL(), PageTransition::TYPED); 199 controller().LoadURL(GURL(kHttpsURL), GURL(), PageTransition::TYPED);
157 SafeBrowsingService::UnsafeResource resource; 200 SafeBrowsingService::UnsafeResource resource;
158 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); 201 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
159 scoped_refptr<MalwareDetails> report = new MalwareDetails( 202 scoped_refptr<MalwareDetails> report = new MalwareDetails(
160 contents(), resource); 203 contents(), resource);
161 204
162 scoped_ptr<const std::string> serialized(report->GetSerializedReport()); 205 scoped_ptr<const std::string> serialized(report->GetSerializedReport());
163 safe_browsing::ClientMalwareReportRequest actual; 206 ClientMalwareReportRequest actual;
164 actual.ParseFromString(*serialized); 207 actual.ParseFromString(*serialized);
165 208
166 safe_browsing::ClientMalwareReportRequest expected; 209 ClientMalwareReportRequest expected;
167 expected.set_malware_url(kMalwareURL); // No page_url 210 expected.set_malware_url(kMalwareURL); // No page_url
168 expected.set_referrer_url(""); 211 expected.set_referrer_url("");
169 212
170 safe_browsing::ClientMalwareReportRequest::Resource* node = 213 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources();
171 expected.add_nodes(); 214 pb_resource->set_url(kMalwareURL); // Only one resource
172 node->set_url(kMalwareURL); // Only one node 215
216 ClientMalwareReportRequest::Node* node = expected.add_nodes();
217 node->set_id(0);
173 218
174 VerifyResults(actual, expected); 219 VerifyResults(actual, expected);
175 } 220 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698