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

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

Issue 6611023: The optional Malware Details also collects HTTP cache information. See design... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 9 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) 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"
8 #include "base/time.h"
9 #include "chrome/browser/profiles/profile.h"
7 #include "chrome/browser/safe_browsing/malware_details.h" 10 #include "chrome/browser/safe_browsing/malware_details.h"
8 #include "chrome/browser/safe_browsing/report.pb.h" 11 #include "chrome/browser/safe_browsing/report.pb.h"
12 #include "chrome/common/net/url_request_context_getter.h"
13 #include "chrome/common/render_messages.h"
14 #include "chrome/common/render_messages_params.h"
9 #include "chrome/common/safebrowsing_messages.h" 15 #include "chrome/common/safebrowsing_messages.h"
16 #include "chrome/test/testing_profile.h"
10 #include "content/browser/browser_thread.h" 17 #include "content/browser/browser_thread.h"
11 #include "content/browser/renderer_host/test_render_view_host.h" 18 #include "content/browser/renderer_host/test_render_view_host.h"
12 #include "content/browser/tab_contents/navigation_entry.h" 19 #include "content/browser/tab_contents/navigation_entry.h"
13 #include "content/browser/tab_contents/test_tab_contents.h" 20 #include "content/browser/tab_contents/test_tab_contents.h"
21 #include "net/base/io_buffer.h"
22 #include "net/base/test_completion_callback.h"
23 #include "net/disk_cache/disk_cache.h"
24 #include "net/http/http_cache.h"
25 #include "net/http/http_response_info.h"
26 #include "net/http/http_response_headers.h"
27 #include "net/http/http_util.h"
28 #include "net/url_request/url_request_context.h"
14 29
15 static const char* kOriginalLandingURL = "http://www.originallandingpage.com/"; 30 static const char* kOriginalLandingURL = "http://www.originallandingpage.com/";
16 static const char* kLandingURL = "http://www.landingpage.com/";
17 static const char* kMalwareURL = "http://www.malware.com/";
18 static const char* kHttpsURL = "https://www.url.com/"; 31 static const char* kHttpsURL = "https://www.url.com/";
19 static const char* kDOMChildURL = "http://www.domparent.com/"; 32 static const char* kDOMChildURL = "http://www.domparent.com/";
20 static const char* kDOMParentURL = "http://www.domchild.com/"; 33 static const char* kDOMParentURL = "http://www.domchild.com/";
21 static const char* kFirstRedirectURL = "http://redirectone.com/"; 34 static const char* kFirstRedirectURL = "http://redirectone.com/";
22 static const char* kSecondRedirectURL = "http://redirecttwo.com/"; 35 static const char* kSecondRedirectURL = "http://redirecttwo.com/";
23 36
37 static const char* kMalwareURL = "http://www.malware.com/";
38 static const char* kMalwareHeaders =
39 "HTTP/1.1 200 OK\n"
40 "Content-Type: image/jpeg\n";
41 static const char* kMalwareData = "exploit();";
42
43 static const char* kLandingURL = "http://www.landingpage.com/";
44 static const char* kLandingHeaders =
45 "HTTP/1.1 200 OK\n"
46 "Content-Type: text/html\n"
47 "Content-Length: 1024\n"
48 "Set-Cookie: tastycookie\n"; // This header is dropped.
49 static const char* kLandingData = "<iframe src='http://www.malware.com'>";
50
24 using safe_browsing::ClientMalwareReportRequest; 51 using safe_browsing::ClientMalwareReportRequest;
25 52
26 class MalwareDetailsTest : public RenderViewHostTestHarness { 53 namespace {
54
55 class MockURLRequestContext : public net::URLRequestContext {
56 public:
57 MockURLRequestContext()
58 : cache_(reinterpret_cast<net::HttpTransactionFactory*>(NULL), NULL,
59 net::HttpCache::DefaultBackend::InMemory(0)) {
60 set_http_transaction_factory(&cache_);
61 }
62
63 private:
64 net::HttpCache cache_;
65 };
66
67 void WriteHeaders(disk_cache::Entry* entry, const std::string headers) {
68 net::HttpResponseInfo responseinfo;
69 std::string raw_headers = net::HttpUtil::AssembleRawHeaders(
70 headers.c_str(), headers.size());
71 responseinfo.headers = new net::HttpResponseHeaders(raw_headers);
72
73 Pickle pickle;
74 responseinfo.Persist(&pickle, false, false);
75
76 scoped_refptr<net::WrappedIOBuffer> buf(new net::WrappedIOBuffer(
77 reinterpret_cast<const char*>(pickle.data())));
78 int len = static_cast<int>(pickle.size());
79
80 TestCompletionCallback cb;
81 int rv = entry->WriteData(0, 0, buf, len, &cb, true);
82 ASSERT_EQ(len, cb.GetResult(rv));
83 }
84
85 void WriteData(disk_cache::Entry* entry, const std::string data) {
86 if (data.empty())
87 return;
88
89 int len = data.length();
90 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(len));
91 memcpy(buf->data(), data.data(), data.length());
92
93 TestCompletionCallback cb;
94 int rv = entry->WriteData(1, 0, buf, len, &cb, true);
95 ASSERT_EQ(len, cb.GetResult(rv));
96 }
97
98 void WriteToEntry(disk_cache::Backend* cache, const std::string key,
99 const std::string headers, const std::string data) {
100 TestCompletionCallback cb;
101 disk_cache::Entry* entry;
102 int rv = cache->CreateEntry(key, &entry, &cb);
103 rv = cb.GetResult(rv);
104 if (rv != net::OK) {
105 rv = cache->OpenEntry(key, &entry, &cb);
106 ASSERT_EQ(net::OK, cb.GetResult(rv));
107 }
108
109 WriteHeaders(entry, headers);
110 WriteData(entry, data);
111
112 entry->Close();
113 }
114
115 void FillCache(net::URLRequestContext* context) {
116 TestCompletionCallback cb;
117 disk_cache::Backend* cache;
118 int rv =
119 context->http_transaction_factory()->GetCache()->GetBackend(&cache, &cb);
120 ASSERT_EQ(net::OK, cb.GetResult(rv));
121
122 std::string empty;
123 WriteToEntry(cache, kMalwareURL, kMalwareHeaders, kMalwareData);
124 WriteToEntry(cache, kLandingURL, kLandingHeaders, kLandingData);
125 }
126
127 class TestURLRequestContextGetter : public URLRequestContextGetter {
128 public:
129 virtual net::URLRequestContext* GetURLRequestContext() {
130 if (!context_)
131 context_ = new MockURLRequestContext();
132 return context_;
133 }
134 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const {
135 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
136 }
137
138 private:
139 ~TestURLRequestContextGetter() {}
140 scoped_refptr<MockURLRequestContext> context_;
141 };
142
143 // Lets us provide a MockURLRequestContext and an InMemory HTTP Cache.
144 // Also exposes the constructor.
145 class MalwareDetailsWrap : public MalwareDetails {
146 public:
147 MalwareDetailsWrap(SafeBrowsingService* sb_service,
148 TabContents* tab_contents,
149 const SafeBrowsingService::UnsafeResource& unsafe_resource,
150 bool provide_cache)
151 : MalwareDetails(sb_service, tab_contents, unsafe_resource),
152 // request_context_(new MockURLRequestContext()),
153 provide_cache_(provide_cache) {
154 if (provide_cache) {
155 request_context_getter_ = new TestURLRequestContextGetter();
156 }
157 }
158
159 virtual ~MalwareDetailsWrap() {}
160
161 net::URLRequestContext* GetURLRequestContext() {
162 return request_context_getter_->GetURLRequestContext();
163 }
164
165 private:
166 // MockURLRequestContext* request_context_;
167 bool provide_cache_;
168 };
169
170 class MockSafeBrowsingService : public SafeBrowsingService {
171 public:
172 MockSafeBrowsingService() {}
173 virtual ~MockSafeBrowsingService() {}
174
175 // When the MalwareDetails is done, this is called.
176 virtual void SendSerializedMalwareDetails(const std::string& serialized) {
177 // Notify WaitForSerializedReport.
178 MessageLoop::current()->Quit();
179 serialized_ = serialized;
180 }
181
182 const std::string& GetSerialized() {
183 return serialized_;
184 }
185
186 private:
187 std::string serialized_;
188 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingService);
189 };
190
191 } // namespace.
192
193 class MalwareDetailsTest :
194 public RenderViewHostTestHarness {
27 public: 195 public:
28 MalwareDetailsTest() 196 MalwareDetailsTest()
29 : ui_thread_(BrowserThread::UI, MessageLoop::current()), 197 : ui_thread_(BrowserThread::UI, MessageLoop::current()),
30 io_thread_(BrowserThread::IO, MessageLoop::current()) { 198 io_thread_(BrowserThread::IO, MessageLoop::current()),
199 sb_service_(new MockSafeBrowsingService()) {
31 } 200 }
32 201
33 virtual void SetUp() { 202 virtual void SetUp() {
34 RenderViewHostTestHarness::SetUp(); 203 RenderViewHostTestHarness::SetUp();
35 } 204 }
36 205
37 static bool ResourceLessThan( 206 static bool ResourceLessThan(
38 const ClientMalwareReportRequest::Resource* lhs, 207 const ClientMalwareReportRequest::Resource* lhs,
39 const ClientMalwareReportRequest::Resource* rhs) { 208 const ClientMalwareReportRequest::Resource* rhs) {
40 return lhs->id() < rhs->id(); 209 return lhs->id() < rhs->id();
41 } 210 }
42 211
212 std::string WaitForSerializedReport(MalwareDetails* report) {
213 report->FinishCollection();
214 // Wait for the callback (SendSerializedMalwareDetails).
215 MessageLoop::current()->Run();
216 return sb_service_->GetSerialized();
217 }
218
43 protected: 219 protected:
44 void InitResource(SafeBrowsingService::UnsafeResource* resource, 220 void InitResource(SafeBrowsingService::UnsafeResource* resource,
45 ResourceType::Type resource_type, 221 ResourceType::Type resource_type,
46 const GURL& url) { 222 const GURL& url) {
47 resource->client = NULL; 223 resource->client = NULL;
48 resource->url = url; 224 resource->url = url;
49 resource->resource_type = resource_type; 225 resource->resource_type = resource_type;
50 resource->threat_type = SafeBrowsingService::URL_MALWARE; 226 resource->threat_type = SafeBrowsingService::URL_MALWARE;
51 resource->render_process_host_id = contents_->GetRenderProcessHost()->id(); 227 resource->render_process_host_id = contents_->GetRenderProcessHost()->id();
52 resource->render_view_id = contents_->render_view_host()->routing_id(); 228 resource->render_view_id = contents_->render_view_host()->routing_id();
(...skipping 19 matching lines...) Expand all
72 std::vector<const ClientMalwareReportRequest::Resource*> expected; 248 std::vector<const ClientMalwareReportRequest::Resource*> expected;
73 for (int i = 0; i < report_pb.resources_size(); ++i) { 249 for (int i = 0; i < report_pb.resources_size(); ++i) {
74 const ClientMalwareReportRequest::Resource& resource = 250 const ClientMalwareReportRequest::Resource& resource =
75 expected_pb.resources(i); 251 expected_pb.resources(i);
76 expected.push_back(&resource); 252 expected.push_back(&resource);
77 } 253 }
78 std::sort(expected.begin(), expected.end(), 254 std::sort(expected.begin(), expected.end(),
79 &MalwareDetailsTest::ResourceLessThan); 255 &MalwareDetailsTest::ResourceLessThan);
80 256
81 for (uint32 i = 0; i < expected.size(); ++i) { 257 for (uint32 i = 0; i < expected.size(); ++i) {
82 EXPECT_EQ(expected[i]->id(), resources[i]->id()); 258 VerifyResource(resources[i], expected[i]);
83 EXPECT_EQ(expected[i]->url(), resources[i]->url()); 259 }
84 EXPECT_EQ(expected[i]->parent_id(), resources[i]->parent_id()); 260
85 ASSERT_EQ(expected[i]->child_ids_size(), resources[i]->child_ids_size()); 261 EXPECT_EQ(expected_pb.complete(), report_pb.complete());
86 for (int j = 0; j < expected[i]->child_ids_size(); j++) { 262 }
87 EXPECT_EQ(expected[i]->child_ids(j), resources[i]->child_ids(j)); 263
264 void VerifyResource(const ClientMalwareReportRequest::Resource* resource,
265 const ClientMalwareReportRequest::Resource* expected) {
266 EXPECT_EQ(expected->id(), resource->id());
267 EXPECT_EQ(expected->url(), resource->url());
268 EXPECT_EQ(expected->parent_id(), resource->parent_id());
269 ASSERT_EQ(expected->child_ids_size(), resource->child_ids_size());
270 for (int i = 0; i < expected->child_ids_size(); i++) {
271 EXPECT_EQ(expected->child_ids(i), resource->child_ids(i));
272 }
273
274 // Verify HTTP Responses
275 if (expected->has_response()) {
276 ASSERT_TRUE(resource->has_response());
277 EXPECT_EQ(expected->response().firstline().code(),
278 resource->response().firstline().code());
279
280 ASSERT_EQ(expected->response().headers_size(),
281 resource->response().headers_size());
282 for (int i = 0; i < expected->response().headers_size(); ++i) {
283 EXPECT_EQ(expected->response().headers(i).name(),
284 resource->response().headers(i).name());
285 EXPECT_EQ(expected->response().headers(i).value(),
286 resource->response().headers(i).value());
88 } 287 }
288
289 EXPECT_EQ(expected->response().body(), resource->response().body());
290 EXPECT_EQ(expected->response().bodylength(),
291 resource->response().bodylength());
292 EXPECT_EQ(expected->response().bodydigest(),
293 resource->response().bodydigest());
89 } 294 }
90 } 295 }
91 296
92 BrowserThread ui_thread_; 297 BrowserThread ui_thread_;
93 BrowserThread io_thread_; 298 BrowserThread io_thread_;
299 scoped_refptr<MockSafeBrowsingService> sb_service_;
94 }; 300 };
95 301
96 // Tests creating a simple malware report. 302 // Tests creating a simple malware report.
97 TEST_F(MalwareDetailsTest, MalwareSubResource) { 303 TEST_F(MalwareDetailsTest, MalwareSubResource) {
98 // Start a load. 304 // Start a load.
99 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); 305 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED);
100 306
101 SafeBrowsingService::UnsafeResource resource; 307 SafeBrowsingService::UnsafeResource resource;
102 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); 308 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
103 309
104 scoped_refptr<MalwareDetails> report = MalwareDetails::NewMalwareDetails( 310 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap(
105 contents(), resource); 311 sb_service_, contents(), resource, false);
106 312
107 scoped_ptr<const std::string> serialized(report->GetSerializedReport()); 313 std::string serialized = WaitForSerializedReport(report);
314
108 ClientMalwareReportRequest actual; 315 ClientMalwareReportRequest actual;
109 actual.ParseFromString(*serialized); 316 actual.ParseFromString(serialized);
110 317
111 ClientMalwareReportRequest expected; 318 ClientMalwareReportRequest expected;
112 expected.set_malware_url(kMalwareURL); 319 expected.set_malware_url(kMalwareURL);
113 expected.set_page_url(kLandingURL); 320 expected.set_page_url(kLandingURL);
114 expected.set_referrer_url(""); 321 expected.set_referrer_url("");
115 322
116 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); 323 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources();
117 pb_resource->set_id(0); 324 pb_resource->set_id(0);
118 pb_resource->set_url(kLandingURL); 325 pb_resource->set_url(kLandingURL);
119 pb_resource = expected.add_resources(); 326 pb_resource = expected.add_resources();
120 pb_resource->set_id(1); 327 pb_resource->set_id(1);
121 pb_resource->set_url(kMalwareURL); 328 pb_resource->set_url(kMalwareURL);
122 329
123 VerifyResults(actual, expected); 330 VerifyResults(actual, expected);
124 } 331 }
125 332
126 // Tests creating a simple malware report where the subresource has a 333 // Tests creating a simple malware report where the subresource has a
127 // different original_url. 334 // different original_url.
128 TEST_F(MalwareDetailsTest, MalwareSubResourceWithOriginalUrl) { 335 TEST_F(MalwareDetailsTest, MalwareSubResourceWithOriginalUrl) {
129 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); 336 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED);
130 337
131 SafeBrowsingService::UnsafeResource resource; 338 SafeBrowsingService::UnsafeResource resource;
132 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); 339 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
133 resource.original_url = GURL(kOriginalLandingURL); 340 resource.original_url = GURL(kOriginalLandingURL);
134 341
135 scoped_refptr<MalwareDetails> report = MalwareDetails::NewMalwareDetails( 342 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap(
136 contents(), resource); 343 sb_service_.get(), contents(), resource, false);
137 344
138 scoped_ptr<const std::string> serialized(report->GetSerializedReport()); 345 std::string serialized = WaitForSerializedReport(report);
346
139 ClientMalwareReportRequest actual; 347 ClientMalwareReportRequest actual;
140 actual.ParseFromString(*serialized); 348 actual.ParseFromString(serialized);
141 349
142 ClientMalwareReportRequest expected; 350 ClientMalwareReportRequest expected;
143 expected.set_malware_url(kMalwareURL); 351 expected.set_malware_url(kMalwareURL);
144 expected.set_page_url(kLandingURL); 352 expected.set_page_url(kLandingURL);
145 expected.set_referrer_url(""); 353 expected.set_referrer_url("");
146 354
147 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); 355 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources();
148 pb_resource->set_id(0); 356 pb_resource->set_id(0);
149 pb_resource->set_url(kLandingURL); 357 pb_resource->set_url(kLandingURL);
150 358
(...skipping 11 matching lines...) Expand all
162 VerifyResults(actual, expected); 370 VerifyResults(actual, expected);
163 } 371 }
164 372
165 // Tests creating a malware report with data from the renderer. 373 // Tests creating a malware report with data from the renderer.
166 TEST_F(MalwareDetailsTest, MalwareDOMDetails) { 374 TEST_F(MalwareDetailsTest, MalwareDOMDetails) {
167 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); 375 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED);
168 376
169 SafeBrowsingService::UnsafeResource resource; 377 SafeBrowsingService::UnsafeResource resource;
170 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); 378 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
171 379
172 scoped_refptr<MalwareDetails> report = MalwareDetails::NewMalwareDetails( 380 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap(
173 contents(), resource); 381 sb_service_.get(), contents(), resource, false);
174 382
175 // Send a message from the DOM, with 2 nodes, a parent and a child. 383 // Send a message from the DOM, with 2 nodes, a parent and a child.
176 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node> params; 384 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node> params;
177 SafeBrowsingHostMsg_MalwareDOMDetails_Node child_node; 385 SafeBrowsingHostMsg_MalwareDOMDetails_Node child_node;
178 child_node.url = GURL(kDOMChildURL); 386 child_node.url = GURL(kDOMChildURL);
179 child_node.tag_name = "iframe"; 387 child_node.tag_name = "iframe";
180 child_node.parent = GURL(kDOMParentURL); 388 child_node.parent = GURL(kDOMParentURL);
181 params.push_back(child_node); 389 params.push_back(child_node);
182 SafeBrowsingHostMsg_MalwareDOMDetails_Node parent_node; 390 SafeBrowsingHostMsg_MalwareDOMDetails_Node parent_node;
183 parent_node.url = GURL(kDOMParentURL); 391 parent_node.url = GURL(kDOMParentURL);
184 parent_node.children.push_back(GURL(kDOMChildURL)); 392 parent_node.children.push_back(GURL(kDOMChildURL));
185 params.push_back(parent_node); 393 params.push_back(parent_node);
186 report->OnReceivedMalwareDOMDetails(params); 394 report->OnReceivedMalwareDOMDetails(params);
187 395
188 MessageLoop::current()->RunAllPending(); 396 MessageLoop::current()->RunAllPending();
189 397
190 scoped_ptr<const std::string> serialized(report->GetSerializedReport()); 398 std::string serialized = WaitForSerializedReport(report);
191 ClientMalwareReportRequest actual; 399 ClientMalwareReportRequest actual;
192 actual.ParseFromString(*serialized); 400 actual.ParseFromString(serialized);
193 401
194 ClientMalwareReportRequest expected; 402 ClientMalwareReportRequest expected;
195 expected.set_malware_url(kMalwareURL); 403 expected.set_malware_url(kMalwareURL);
196 expected.set_page_url(kLandingURL); 404 expected.set_page_url(kLandingURL);
197 expected.set_referrer_url(""); 405 expected.set_referrer_url("");
198 406
199 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); 407 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources();
200 pb_resource->set_id(0); 408 pb_resource->set_id(0);
201 pb_resource->set_url(kLandingURL); 409 pb_resource->set_url(kLandingURL);
202 410
203 pb_resource = expected.add_resources(); 411 pb_resource = expected.add_resources();
204 pb_resource->set_id(1); 412 pb_resource->set_id(1);
205 pb_resource->set_url(kMalwareURL); 413 pb_resource->set_url(kMalwareURL);
206 414
207 pb_resource = expected.add_resources(); 415 pb_resource = expected.add_resources();
208 pb_resource->set_id(2); 416 pb_resource->set_id(2);
209 pb_resource->set_url(kDOMChildURL); 417 pb_resource->set_url(kDOMChildURL);
210 pb_resource->set_parent_id(3); 418 pb_resource->set_parent_id(3);
211 419
212 pb_resource = expected.add_resources(); 420 pb_resource = expected.add_resources();
213 pb_resource->set_id(3); 421 pb_resource->set_id(3);
214 pb_resource->set_url(kDOMParentURL); 422 pb_resource->set_url(kDOMParentURL);
215 pb_resource->add_child_ids(2); 423 pb_resource->add_child_ids(2);
424 expected.set_complete(false); // Since the cache was missing.
216 425
217 VerifyResults(actual, expected); 426 VerifyResults(actual, expected);
218 } 427 }
219 428
220 // Verify that https:// urls are dropped. 429 // Verify that https:// urls are dropped.
221 TEST_F(MalwareDetailsTest, NotPublicUrl) { 430 TEST_F(MalwareDetailsTest, NotPublicUrl) {
222 controller().LoadURL(GURL(kHttpsURL), GURL(), PageTransition::TYPED); 431 controller().LoadURL(GURL(kHttpsURL), GURL(), PageTransition::TYPED);
223 SafeBrowsingService::UnsafeResource resource; 432 SafeBrowsingService::UnsafeResource resource;
224 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); 433 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
225 scoped_refptr<MalwareDetails> report = MalwareDetails::NewMalwareDetails( 434 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap(
226 contents(), resource); 435 sb_service_.get(), contents(), resource, false);
227 436
228 scoped_ptr<const std::string> serialized(report->GetSerializedReport()); 437 std::string serialized = WaitForSerializedReport(report);
229 ClientMalwareReportRequest actual; 438 ClientMalwareReportRequest actual;
230 actual.ParseFromString(*serialized); 439 actual.ParseFromString(serialized);
231 440
232 ClientMalwareReportRequest expected; 441 ClientMalwareReportRequest expected;
233 expected.set_malware_url(kMalwareURL); // No page_url 442 expected.set_malware_url(kMalwareURL); // No page_url
234 expected.set_referrer_url(""); 443 expected.set_referrer_url("");
235 444
236 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); 445 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources();
237 pb_resource->set_url(kMalwareURL); // Only one resource 446 pb_resource->set_url(kMalwareURL); // Only one resource
238 447
239 VerifyResults(actual, expected); 448 VerifyResults(actual, expected);
240 } 449 }
241 450
242 // Tests creating a malware report where there are redirect urls to an unsafe 451 // Tests creating a malware report where there are redirect urls to an unsafe
243 // resource url 452 // resource url
244 TEST_F(MalwareDetailsTest, MalwareWithRedirectUrl) { 453 TEST_F(MalwareDetailsTest, MalwareWithRedirectUrl) {
245 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); 454 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED);
246 455
247 SafeBrowsingService::UnsafeResource resource; 456 SafeBrowsingService::UnsafeResource resource;
248 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); 457 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
249 resource.original_url = GURL(kOriginalLandingURL); 458 resource.original_url = GURL(kOriginalLandingURL);
250 459
251 // add some redirect urls 460 // add some redirect urls
252 resource.redirect_urls.push_back(GURL(kFirstRedirectURL)); 461 resource.redirect_urls.push_back(GURL(kFirstRedirectURL));
253 resource.redirect_urls.push_back(GURL(kSecondRedirectURL)); 462 resource.redirect_urls.push_back(GURL(kSecondRedirectURL));
254 resource.redirect_urls.push_back(GURL(kMalwareURL)); 463 resource.redirect_urls.push_back(GURL(kMalwareURL));
255 464
256 scoped_refptr<MalwareDetails> report = MalwareDetails::NewMalwareDetails( 465 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap(
257 contents(), resource); 466 sb_service_.get(), contents(), resource, false);
258 467
259 scoped_ptr<const std::string> serialized(report->GetSerializedReport()); 468 std::string serialized = WaitForSerializedReport(report);
260 ClientMalwareReportRequest actual; 469 ClientMalwareReportRequest actual;
261 actual.ParseFromString(*serialized); 470 actual.ParseFromString(serialized);
262 471
263 ClientMalwareReportRequest expected; 472 ClientMalwareReportRequest expected;
264 expected.set_malware_url(kMalwareURL); 473 expected.set_malware_url(kMalwareURL);
265 expected.set_page_url(kLandingURL); 474 expected.set_page_url(kLandingURL);
266 expected.set_referrer_url(""); 475 expected.set_referrer_url("");
267 476
268 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); 477 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources();
269 pb_resource->set_id(0); 478 pb_resource->set_id(0);
270 pb_resource->set_url(kLandingURL); 479 pb_resource->set_url(kLandingURL);
271 480
(...skipping 11 matching lines...) Expand all
283 pb_resource->set_url(kFirstRedirectURL); 492 pb_resource->set_url(kFirstRedirectURL);
284 pb_resource->set_parent_id(1); 493 pb_resource->set_parent_id(1);
285 494
286 pb_resource = expected.add_resources(); 495 pb_resource = expected.add_resources();
287 pb_resource->set_id(4); 496 pb_resource->set_id(4);
288 pb_resource->set_url(kSecondRedirectURL); 497 pb_resource->set_url(kSecondRedirectURL);
289 pb_resource->set_parent_id(3); 498 pb_resource->set_parent_id(3);
290 499
291 VerifyResults(actual, expected); 500 VerifyResults(actual, expected);
292 } 501 }
502
503 // Tests the interaction with the HTTP cache.
504 TEST_F(MalwareDetailsTest, HTTPCache) {
505 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED);
506
507 SafeBrowsingService::UnsafeResource resource;
508 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
509
510 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap(
511 sb_service_.get(), contents(), resource, true);
512
513 FillCache(report->GetURLRequestContext());
514
515 // The cache collection starts after the IPC from the DOM is fired.
516 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node> params;
517 report->OnReceivedMalwareDOMDetails(params);
518
519 // Let the cache callbacks complete
520 MessageLoop::current()->RunAllPending();
521
522 DVLOG(1) << "Getting serialized report";
523 std::string serialized = WaitForSerializedReport(report);
524 ClientMalwareReportRequest actual;
525 actual.ParseFromString(serialized);
526
527 ClientMalwareReportRequest expected;
528 expected.set_malware_url(kMalwareURL);
529 expected.set_page_url(kLandingURL);
530 expected.set_referrer_url("");
531
532 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources();
533 pb_resource->set_id(0);
534 pb_resource->set_url(kLandingURL);
535 safe_browsing::ClientMalwareReportRequest::HTTPResponse* pb_response =
536 pb_resource->mutable_response();
537 pb_response->mutable_firstline()->set_code(200);
538 safe_browsing::ClientMalwareReportRequest::HTTPHeader* pb_header =
539 pb_response->add_headers();
540 pb_header->set_name("Content-Type");
541 pb_header->set_value("text/html");
542 pb_header = pb_response->add_headers();
543 pb_header->set_name("Content-Length");
544 pb_header->set_value("1024");
545 pb_response->set_body(kLandingData);
546 pb_response->set_bodylength(37);
547 pb_response->set_bodydigest("9ca97475598a79bc1e8fc9bd6c72cd35");
548
549 pb_resource = expected.add_resources();
550 pb_resource->set_id(1);
551 pb_resource->set_url(kMalwareURL);
552 pb_response = pb_resource->mutable_response();
553 pb_response->mutable_firstline()->set_code(200);
554 pb_header = pb_response->add_headers();
555 pb_header->set_name("Content-Type");
556 pb_header->set_value("image/jpeg");
557 pb_response->set_body(kMalwareData);
558 pb_response->set_bodylength(10);
559 pb_response->set_bodydigest("581373551c43d4cf33bfb3b26838ff95");
560 expected.set_complete(true);
561
562 VerifyResults(actual, expected);
563 }
564
565 // Tests the interaction with the HTTP cache (where the cache is empty).
566 TEST_F(MalwareDetailsTest, HTTPCacheNoEntries) {
567 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED);
568
569 SafeBrowsingService::UnsafeResource resource;
570 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
571
572 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap(
573 sb_service_.get(), contents(), resource, true);
574
575 // No call to FillCache
576
577 // The cache collection starts after the IPC from the DOM is fired.
578 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node> params;
579 report->OnReceivedMalwareDOMDetails(params);
580
581 // Let the cache callbacks complete
582 MessageLoop::current()->RunAllPending();
583
584 DVLOG(1) << "Getting serialized report";
585 std::string serialized = WaitForSerializedReport(report);
586 ClientMalwareReportRequest actual;
587 actual.ParseFromString(serialized);
588
589 ClientMalwareReportRequest expected;
590 expected.set_malware_url(kMalwareURL);
591 expected.set_page_url(kLandingURL);
592 expected.set_referrer_url("");
593
594 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources();
595 pb_resource->set_id(0);
596 pb_resource->set_url(kLandingURL);
597 pb_resource = expected.add_resources();
598 pb_resource->set_id(1);
599 pb_resource->set_url(kMalwareURL);
600 expected.set_complete(true);
601
602 VerifyResults(actual, expected);
603 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698