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

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

Issue 6710004: Querying the history service to get the redirect information for urls.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 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" 7 #include "base/pickle.h"
8 #include "base/time.h" 8 #include "base/time.h"
9 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/safe_browsing/malware_details.h" 10 #include "chrome/browser/safe_browsing/malware_details.h"
11 #include "chrome/browser/safe_browsing/malware_details_history.h"
11 #include "chrome/browser/safe_browsing/report.pb.h" 12 #include "chrome/browser/safe_browsing/report.pb.h"
12 #include "chrome/common/render_messages.h" 13 #include "chrome/common/render_messages.h"
13 #include "chrome/common/safe_browsing/safebrowsing_messages.h" 14 #include "chrome/common/safe_browsing/safebrowsing_messages.h"
14 #include "chrome/test/test_url_request_context_getter.h" 15 #include "chrome/test/test_url_request_context_getter.h"
15 #include "chrome/test/testing_profile.h" 16 #include "chrome/test/testing_profile.h"
16 #include "content/browser/browser_thread.h" 17 #include "content/browser/browser_thread.h"
18 #include "chrome/browser/history/history_backend.h"
19 #include "chrome/browser/history/history.h"
17 #include "content/browser/renderer_host/test_render_view_host.h" 20 #include "content/browser/renderer_host/test_render_view_host.h"
18 #include "content/browser/tab_contents/navigation_entry.h" 21 #include "content/browser/tab_contents/navigation_entry.h"
19 #include "content/browser/tab_contents/test_tab_contents.h" 22 #include "content/browser/tab_contents/test_tab_contents.h"
20 #include "net/base/io_buffer.h" 23 #include "net/base/io_buffer.h"
21 #include "net/base/test_completion_callback.h" 24 #include "net/base/test_completion_callback.h"
22 #include "net/disk_cache/disk_cache.h" 25 #include "net/disk_cache/disk_cache.h"
23 #include "net/http/http_cache.h" 26 #include "net/http/http_cache.h"
24 #include "net/http/http_response_headers.h" 27 #include "net/http/http_response_headers.h"
25 #include "net/http/http_response_info.h" 28 #include "net/http/http_response_info.h"
26 #include "net/http/http_util.h" 29 #include "net/http/http_util.h"
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 sb_service_(new MockSafeBrowsingService()) { 171 sb_service_(new MockSafeBrowsingService()) {
169 } 172 }
170 173
171 virtual void SetUp() { 174 virtual void SetUp() {
172 RenderViewHostTestHarness::SetUp(); 175 RenderViewHostTestHarness::SetUp();
173 // request_context_getter_ = new TestURLRequestContextGetter(); 176 // request_context_getter_ = new TestURLRequestContextGetter();
174 177
175 // The URLFetcher checks that the messageloop type is IO. 178 // The URLFetcher checks that the messageloop type is IO.
176 ASSERT_TRUE(io_thread_.StartWithOptions( 179 ASSERT_TRUE(io_thread_.StartWithOptions(
177 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 180 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
181
182 profile_.reset(new TestingProfile);
mattm 2011/05/17 03:17:01 It looks like RenderViewHostTestHarness already cr
kewang 2011/05/18 05:47:40 Yes, and it got reset in SetUp() too. Remove the d
183 profile_->CreateHistoryService(false, false);
mattm 2011/05/17 03:17:01 include arg name comments (like false /* delete_fi
kewang 2011/05/18 05:47:40 Done.
178 } 184 }
179 185
180 virtual void TearDown() { 186 virtual void TearDown() {
187 profile_.reset();
mattm 2011/05/17 03:17:01 Unnecessary, unless there is some reason you need
kewang 2011/05/18 05:47:40 Done.
181 io_thread_.Stop(); 188 io_thread_.Stop();
182 RenderViewHostTestHarness::TearDown(); 189 RenderViewHostTestHarness::TearDown();
183 } 190 }
184 191
185 static bool ResourceLessThan( 192 static bool ResourceLessThan(
186 const ClientMalwareReportRequest::Resource* lhs, 193 const ClientMalwareReportRequest::Resource* lhs,
187 const ClientMalwareReportRequest::Resource* rhs) { 194 const ClientMalwareReportRequest::Resource* rhs) {
188 return lhs->id() < rhs->id(); 195 return lhs->id() < rhs->id();
189 } 196 }
190 197
191 std::string WaitForSerializedReport(MalwareDetails* report) { 198 std::string WaitForSerializedReport(MalwareDetails* report) {
192 BrowserThread::PostTask( 199 BrowserThread::PostTask(
193 BrowserThread::IO, 200 BrowserThread::IO,
194 FROM_HERE, 201 FROM_HERE,
195 NewRunnableMethod( 202 NewRunnableMethod(
196 report, &MalwareDetails::FinishCollection)); 203 report, &MalwareDetails::FinishCollection));
197 // Wait for the callback (SendSerializedMalwareDetails). 204 // Wait for the callback (SendSerializedMalwareDetails).
198 DVLOG(1) << "Waiting for SendSerializedMalwareDetails"; 205 DVLOG(1) << "Waiting for SendSerializedMalwareDetails";
199 MessageLoop::current()->Run(); 206 MessageLoop::current()->Run();
200 return sb_service_->GetSerialized(); 207 return sb_service_->GetSerialized();
201 } 208 }
202 209
210 HistoryService* history_service() {
211 return profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
212 }
213
203 protected: 214 protected:
204 void InitResource(SafeBrowsingService::UnsafeResource* resource, 215 void InitResource(SafeBrowsingService::UnsafeResource* resource,
205 ResourceType::Type resource_type, 216 ResourceType::Type resource_type,
206 const GURL& url) { 217 const GURL& url) {
207 resource->client = NULL; 218 resource->client = NULL;
208 resource->url = url; 219 resource->url = url;
209 resource->resource_type = resource_type; 220 resource->resource_type = resource_type;
210 resource->threat_type = SafeBrowsingService::URL_MALWARE; 221 resource->threat_type = SafeBrowsingService::URL_MALWARE;
211 resource->render_process_host_id = contents()->GetRenderProcessHost()->id(); 222 resource->render_process_host_id = contents()->GetRenderProcessHost()->id();
212 resource->render_view_id = contents()->render_view_host()->routing_id(); 223 resource->render_view_id = contents()->render_view_host()->routing_id();
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 } 282 }
272 283
273 EXPECT_EQ(expected->response().body(), resource->response().body()); 284 EXPECT_EQ(expected->response().body(), resource->response().body());
274 EXPECT_EQ(expected->response().bodylength(), 285 EXPECT_EQ(expected->response().bodylength(),
275 resource->response().bodylength()); 286 resource->response().bodylength());
276 EXPECT_EQ(expected->response().bodydigest(), 287 EXPECT_EQ(expected->response().bodydigest(),
277 resource->response().bodydigest()); 288 resource->response().bodydigest());
278 } 289 }
279 } 290 }
280 291
292 // Adds a page to history.
mattm 2011/05/17 03:17:01 Add a comment about the expected order of redirect
kewang 2011/05/18 05:47:40 Done.
293 void AddPageToHistory(const GURL& url,
294 const history::RedirectList& redirects) {
295 history_service()->AddPage(
296 url, static_cast<void*>(this), 0, GURL(), PageTransition::TYPED,
297 redirects, history::SOURCE_BROWSED, false);
298 }
299
281 BrowserThread ui_thread_; 300 BrowserThread ui_thread_;
282 BrowserThread io_thread_; 301 BrowserThread io_thread_;
283 scoped_refptr<MockSafeBrowsingService> sb_service_; 302 scoped_refptr<MockSafeBrowsingService> sb_service_;
303 scoped_ptr<TestingProfile> profile_;
mattm 2011/05/17 03:17:01 RenderViewHostTestHarness already has a profile_ m
kewang 2011/05/18 05:47:40 removed.
284 }; 304 };
285 305
286 // Tests creating a simple malware report. 306 // Tests creating a simple malware report.
287 TEST_F(MalwareDetailsTest, MalwareSubResource) { 307 TEST_F(MalwareDetailsTest, MalwareSubResource) {
288 // Start a load. 308 // Start a load.
289 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); 309 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED);
290 310
291 SafeBrowsingService::UnsafeResource resource; 311 SafeBrowsingService::UnsafeResource resource;
292 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); 312 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
293 313
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); 605 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources();
586 pb_resource->set_id(0); 606 pb_resource->set_id(0);
587 pb_resource->set_url(kLandingURL); 607 pb_resource->set_url(kLandingURL);
588 pb_resource = expected.add_resources(); 608 pb_resource = expected.add_resources();
589 pb_resource->set_id(1); 609 pb_resource->set_id(1);
590 pb_resource->set_url(kMalwareURL); 610 pb_resource->set_url(kMalwareURL);
591 expected.set_complete(true); 611 expected.set_complete(true);
592 612
593 VerifyResults(actual, expected); 613 VerifyResults(actual, expected);
594 } 614 }
615
616 // Test getting redirects from history service
Paweł Hajdan Jr. 2011/05/17 15:13:54 nit: Dot at the end please.
kewang 2011/05/18 05:47:40 Done.
617 TEST_F(MalwareDetailsTest, HistoryServiceUrls) {
618 // Add content to history service
Paweł Hajdan Jr. 2011/05/17 15:13:54 nit: Dot at the end.
kewang 2011/05/18 05:47:40 Done.
619 GURL baseurl(kMalwareURL);
620 history::RedirectList redirects;
621 redirects.push_back(GURL(kFirstRedirectURL));
622 redirects.push_back(GURL(kSecondRedirectURL));
623 // the last item of the redirect chain has to be the final URL
mattm 2011/05/17 03:17:01 I was a bit confused since that's opposite the ord
Paweł Hajdan Jr. 2011/05/17 15:13:54 nit: Start with a capital letter and end with a do
kewang 2011/05/18 05:47:40 yes, history backend has a check: request->redirec
kewang 2011/05/18 05:47:40 Done.
624 redirects.push_back(baseurl);
625 AddPageToHistory(baseurl, redirects);
626 // Wait for history service operation finished
Paweł Hajdan Jr. 2011/05/17 15:13:54 nit: Dot at the end.
kewang 2011/05/18 05:47:40 Done.
627 profile_->BlockUntilHistoryProcessesPendingRequests();
628
629 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED);
630
631 SafeBrowsingService::UnsafeResource resource;
632 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
633 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap(
634 sb_service_.get(), contents(), resource, NULL);
635
636 // Reset the history service pointer
637 report->redirects_collector_->SetHistory(history_service());
638
639 // The redirects collection starts after the IPC from the DOM is fired.
640 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node> params;
641 report->OnReceivedMalwareDOMDetails(params);
642
643 // Let the redirects callbacks complete
Paweł Hajdan Jr. 2011/05/17 15:13:54 nit: Dot at the end.
kewang 2011/05/18 05:47:40 Done.
644 MessageLoop::current()->RunAllPending();
645
646 std::string serialized = WaitForSerializedReport(report);
647 ClientMalwareReportRequest actual;
648 actual.ParseFromString(serialized);
649
650 ClientMalwareReportRequest expected;
651 expected.set_malware_url(kMalwareURL);
652 expected.set_page_url(kLandingURL);
653 expected.set_referrer_url("");
654
655 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources();
656 pb_resource->set_id(0);
657 pb_resource->set_url(kLandingURL);
658 pb_resource = expected.add_resources();
659 pb_resource->set_id(1);
660 pb_resource->set_parent_id(2);
661 pb_resource->set_url(kMalwareURL);
662 pb_resource = expected.add_resources();
663 pb_resource->set_id(2);
664 pb_resource->set_parent_id(3);
665 pb_resource->set_tag_name("redir");
666 pb_resource->set_url(kSecondRedirectURL);
667 pb_resource = expected.add_resources();
668 pb_resource->set_id(3);
669 pb_resource->set_tag_name("redir");
670 pb_resource->set_url(kFirstRedirectURL);
671
672 VerifyResults(actual, expected);
673 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698