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