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