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