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

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

Issue 7119003: Create a browser feature extractor that runs after the renderer has (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Add missing delete Created 9 years, 6 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 "base/file_path.h" 5 #include "base/file_path.h"
6 #include "base/memory/ref_counted.h" 6 #include "base/memory/ref_counted.h"
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/scoped_temp_dir.h" 8 #include "base/scoped_temp_dir.h"
9 #include "base/task.h" 9 #include "base/task.h"
10 #include "chrome/browser/safe_browsing/client_side_detection_host.h" 10 #include "chrome/browser/safe_browsing/client_side_detection_host.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 const bool kFalse = false; 42 const bool kFalse = false;
43 const bool kTrue = true; 43 const bool kTrue = true;
44 } 44 }
45 45
46 namespace safe_browsing { 46 namespace safe_browsing {
47 47
48 MATCHER_P(EqualsProto, other, "") { 48 MATCHER_P(EqualsProto, other, "") {
49 return other.SerializeAsString() == arg.SerializeAsString(); 49 return other.SerializeAsString() == arg.SerializeAsString();
50 } 50 }
51 51
52 ACTION(QuitUIMessageLoop) {
53 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI));
54 MessageLoopForUI::current()->Quit();
55 }
56
52 class MockClientSideDetectionService : public ClientSideDetectionService { 57 class MockClientSideDetectionService : public ClientSideDetectionService {
53 public: 58 public:
54 explicit MockClientSideDetectionService(const FilePath& model_path) 59 explicit MockClientSideDetectionService(const FilePath& model_path)
55 : ClientSideDetectionService(model_path, NULL) {} 60 : ClientSideDetectionService(model_path, NULL) {}
56 virtual ~MockClientSideDetectionService() {}; 61 virtual ~MockClientSideDetectionService() {};
57 62
58 MOCK_METHOD2(SendClientReportPhishingRequest, 63 MOCK_METHOD2(SendClientReportPhishingRequest,
59 void(ClientPhishingRequest*, 64 void(ClientPhishingRequest*,
60 ClientReportPhishingRequestCallback*)); 65 ClientReportPhishingRequestCallback*));
61 MOCK_CONST_METHOD1(IsPrivateIPAddress, bool(const std::string&)); 66 MOCK_CONST_METHOD1(IsPrivateIPAddress, bool(const std::string&));
(...skipping 29 matching lines...) Expand all
91 96
92 class MockTestingProfile : public TestingProfile { 97 class MockTestingProfile : public TestingProfile {
93 public: 98 public:
94 MockTestingProfile() {} 99 MockTestingProfile() {}
95 virtual ~MockTestingProfile() {} 100 virtual ~MockTestingProfile() {}
96 101
97 MOCK_METHOD0(IsOffTheRecord, bool()); 102 MOCK_METHOD0(IsOffTheRecord, bool());
98 }; 103 };
99 104
100 // Helper function which quits the UI message loop from the IO message loop. 105 // Helper function which quits the UI message loop from the IO message loop.
101 void QuitUIMessageLoop() { 106 void QuitUIMessageLoopFromIO() {
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
103 BrowserThread::PostTask(BrowserThread::UI, 108 BrowserThread::PostTask(BrowserThread::UI,
104 FROM_HERE, 109 FROM_HERE,
105 new MessageLoop::QuitTask()); 110 new MessageLoop::QuitTask());
106 } 111 }
107 112
108 class ClientSideDetectionHostTest : public TabContentsWrapperTestHarness { 113 class ClientSideDetectionHostTest : public TabContentsWrapperTestHarness {
109 public: 114 public:
110 virtual void SetUp() { 115 virtual void SetUp() {
111 // Set custom profile object so that we can mock calls to IsOffTheRecord. 116 // Set custom profile object so that we can mock calls to IsOffTheRecord.
112 // This needs to happen before we call the parent SetUp() function. We use 117 // This needs to happen before we call the parent SetUp() function. We use
113 // a nice mock because other parts of the code are calling IsOffTheRecord. 118 // a nice mock because other parts of the code are calling IsOffTheRecord.
114 mock_profile_ = new NiceMock<MockTestingProfile>(); 119 mock_profile_ = new NiceMock<MockTestingProfile>();
115 profile_.reset(mock_profile_); 120 profile_.reset(mock_profile_);
121 profile_->CreateHistoryService(true /* delete_file */, false /* no_db */);
116 122
117 TabContentsWrapperTestHarness::SetUp(); 123 TabContentsWrapperTestHarness::SetUp();
118 ui_thread_.reset(new BrowserThread(BrowserThread::UI, &message_loop_)); 124 ui_thread_.reset(new BrowserThread(BrowserThread::UI, &message_loop_));
119 // Note: we're starting a real IO thread to make sure our DCHECKs that 125 // Note: we're starting a real IO thread to make sure our DCHECKs that
120 // verify which thread is running are actually tested. 126 // verify which thread is running are actually tested.
121 io_thread_.reset(new BrowserThread(BrowserThread::IO)); 127 io_thread_.reset(new BrowserThread(BrowserThread::IO));
122 ASSERT_TRUE(io_thread_->Start()); 128 ASSERT_TRUE(io_thread_->Start());
123 129
124 // Inject service classes. 130 // Inject service classes.
125 ScopedTempDir tmp_dir; 131 ScopedTempDir tmp_dir;
(...skipping 17 matching lines...) Expand all
143 void OnDetectedPhishingSite(const std::string& verdict_str) { 149 void OnDetectedPhishingSite(const std::string& verdict_str) {
144 csd_host_->OnDetectedPhishingSite(verdict_str); 150 csd_host_->OnDetectedPhishingSite(verdict_str);
145 } 151 }
146 152
147 void FlushIOMessageLoop() { 153 void FlushIOMessageLoop() {
148 // If there was a message posted on the IO thread to display the 154 // If there was a message posted on the IO thread to display the
149 // interstitial page we know that it would have been posted before 155 // interstitial page we know that it would have been posted before
150 // we put the quit message there. 156 // we put the quit message there.
151 BrowserThread::PostTask(BrowserThread::IO, 157 BrowserThread::PostTask(BrowserThread::IO,
152 FROM_HERE, 158 FROM_HERE,
153 NewRunnableFunction(&QuitUIMessageLoop)); 159 NewRunnableFunction(&QuitUIMessageLoopFromIO));
154 MessageLoop::current()->Run(); 160 MessageLoop::current()->Run();
155 } 161 }
156 162
157 void ExpectPreClassificationChecks(const GURL& url, 163 void ExpectPreClassificationChecks(const GURL& url,
158 const bool* is_private, 164 const bool* is_private,
159 const bool* is_incognito, 165 const bool* is_incognito,
160 const bool* match_csd_whitelist, 166 const bool* match_csd_whitelist,
161 const bool* get_valid_cached_result, 167 const bool* get_valid_cached_result,
162 const bool* is_in_cache, 168 const bool* is_in_cache,
163 const bool* over_report_limit) { 169 const bool* over_report_limit) {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 // Case 1: client thinks the page is phishing. The server does not agree. 226 // Case 1: client thinks the page is phishing. The server does not agree.
221 // No interstitial is shown. 227 // No interstitial is shown.
222 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; 228 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb;
223 ClientPhishingRequest verdict; 229 ClientPhishingRequest verdict;
224 verdict.set_url("http://phishingurl.com/"); 230 verdict.set_url("http://phishingurl.com/");
225 verdict.set_client_score(1.0f); 231 verdict.set_client_score(1.0f);
226 verdict.set_is_phishing(true); 232 verdict.set_is_phishing(true);
227 233
228 EXPECT_CALL(*csd_service_, 234 EXPECT_CALL(*csd_service_,
229 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) 235 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _))
230 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); 236 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop()));
231 OnDetectedPhishingSite(verdict.SerializeAsString()); 237 OnDetectedPhishingSite(verdict.SerializeAsString());
238 MessageLoop::current()->Run();
232 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 239 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get()));
233 ASSERT_TRUE(cb); 240 ASSERT_TRUE(cb);
234 241
235 // Make sure DoDisplayBlockingPage is not going to be called. 242 // Make sure DoDisplayBlockingPage is not going to be called.
236 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); 243 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0);
237 cb->Run(GURL(verdict.url()), false); 244 cb->Run(GURL(verdict.url()), false);
238 delete cb; 245 delete cb;
239 MessageLoop::current()->RunAllPending(); 246 MessageLoop::current()->RunAllPending();
240 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); 247 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get()));
241 } 248 }
242 249
243 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteDisabled) { 250 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteDisabled) {
244 // Case 2: client thinks the page is phishing and so does the server but 251 // Case 2: client thinks the page is phishing and so does the server but
245 // showing the interstitial is disabled => no interstitial is shown. 252 // showing the interstitial is disabled => no interstitial is shown.
246 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; 253 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb;
247 ClientPhishingRequest verdict; 254 ClientPhishingRequest verdict;
248 verdict.set_url("http://phishingurl.com/"); 255 verdict.set_url("http://phishingurl.com/");
249 verdict.set_client_score(1.0f); 256 verdict.set_client_score(1.0f);
250 verdict.set_is_phishing(true); 257 verdict.set_is_phishing(true);
251 258
252 EXPECT_CALL(*csd_service_, 259 EXPECT_CALL(*csd_service_,
253 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) 260 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _))
254 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); 261 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop()));
255 OnDetectedPhishingSite(verdict.SerializeAsString()); 262 OnDetectedPhishingSite(verdict.SerializeAsString());
263 MessageLoop::current()->Run();
256 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 264 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get()));
257 ASSERT_TRUE(cb); 265 ASSERT_TRUE(cb);
258 266
259 // Make sure DoDisplayBlockingPage is not going to be called. 267 // Make sure DoDisplayBlockingPage is not going to be called.
260 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); 268 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0);
261 cb->Run(GURL(verdict.url()), false); 269 cb->Run(GURL(verdict.url()), false);
262 delete cb; 270 delete cb;
263 MessageLoop::current()->RunAllPending(); 271 MessageLoop::current()->RunAllPending();
264 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); 272 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get()));
265 } 273 }
266 274
267 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteShowInterstitial) { 275 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteShowInterstitial) {
268 // Case 3: client thinks the page is phishing and so does the server. 276 // Case 3: client thinks the page is phishing and so does the server.
269 // We show an interstitial. 277 // We show an interstitial.
270 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; 278 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb;
271 GURL phishing_url("http://phishingurl.com/"); 279 GURL phishing_url("http://phishingurl.com/");
272 ClientPhishingRequest verdict; 280 ClientPhishingRequest verdict;
273 verdict.set_url(phishing_url.spec()); 281 verdict.set_url(phishing_url.spec());
274 verdict.set_client_score(1.0f); 282 verdict.set_client_score(1.0f);
275 verdict.set_is_phishing(true); 283 verdict.set_is_phishing(true);
276 284
277 EXPECT_CALL(*csd_service_, 285 EXPECT_CALL(*csd_service_,
278 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) 286 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _))
279 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); 287 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop()));
280 OnDetectedPhishingSite(verdict.SerializeAsString()); 288 OnDetectedPhishingSite(verdict.SerializeAsString());
289 MessageLoop::current()->Run();
281 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 290 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get()));
282 ASSERT_TRUE(cb); 291 ASSERT_TRUE(cb);
283 292
284 SafeBrowsingService::UnsafeResource resource; 293 SafeBrowsingService::UnsafeResource resource;
285 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) 294 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_))
286 .WillOnce(SaveArg<0>(&resource)); 295 .WillOnce(SaveArg<0>(&resource));
287 cb->Run(phishing_url, true); 296 cb->Run(phishing_url, true);
288 delete cb; 297 delete cb;
289 298
290 MessageLoop::current()->RunAllPending(); 299 MessageLoop::current()->RunAllPending();
(...skipping 30 matching lines...) Expand all
321 // a single interstitial is shown for the second URL. 330 // a single interstitial is shown for the second URL.
322 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; 331 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb;
323 GURL phishing_url("http://phishingurl.com/"); 332 GURL phishing_url("http://phishingurl.com/");
324 ClientPhishingRequest verdict; 333 ClientPhishingRequest verdict;
325 verdict.set_url(phishing_url.spec()); 334 verdict.set_url(phishing_url.spec());
326 verdict.set_client_score(1.0f); 335 verdict.set_client_score(1.0f);
327 verdict.set_is_phishing(true); 336 verdict.set_is_phishing(true);
328 337
329 EXPECT_CALL(*csd_service_, 338 EXPECT_CALL(*csd_service_,
330 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) 339 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _))
331 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); 340 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop()));
332 OnDetectedPhishingSite(verdict.SerializeAsString()); 341 OnDetectedPhishingSite(verdict.SerializeAsString());
342 MessageLoop::current()->Run();
333 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 343 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get()));
334 ASSERT_TRUE(cb); 344 ASSERT_TRUE(cb);
335 GURL other_phishing_url("http://other_phishing_url.com/bla"); 345 GURL other_phishing_url("http://other_phishing_url.com/bla");
336 ExpectPreClassificationChecks(other_phishing_url, &kFalse, &kFalse, &kFalse, 346 ExpectPreClassificationChecks(other_phishing_url, &kFalse, &kFalse, &kFalse,
337 &kFalse, &kFalse, &kFalse); 347 &kFalse, &kFalse, &kFalse);
338 // We navigate away. The callback cb should be revoked. 348 // We navigate away. The callback cb should be revoked.
339 NavigateAndCommit(other_phishing_url); 349 NavigateAndCommit(other_phishing_url);
340 // Wait for the pre-classification checks to finish for other_phishing_url. 350 // Wait for the pre-classification checks to finish for other_phishing_url.
341 WaitAndCheckPreClassificationChecks(); 351 WaitAndCheckPreClassificationChecks();
342 352
343 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb_other; 353 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb_other;
344 verdict.set_url(other_phishing_url.spec()); 354 verdict.set_url(other_phishing_url.spec());
345 verdict.set_client_score(0.8f); 355 verdict.set_client_score(0.8f);
346 EXPECT_CALL(*csd_service_, 356 EXPECT_CALL(*csd_service_,
347 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) 357 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _))
348 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb_other))); 358 .WillOnce(DoAll(DeleteArg<0>(),
359 SaveArg<1>(&cb_other),
360 QuitUIMessageLoop()));
349 OnDetectedPhishingSite(verdict.SerializeAsString()); 361 OnDetectedPhishingSite(verdict.SerializeAsString());
362 MessageLoop::current()->Run();
350 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); 363 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get()));
351 ASSERT_TRUE(cb_other); 364 ASSERT_TRUE(cb_other);
352 365
353 // We expect that the interstitial is shown for the second phishing URL and 366 // We expect that the interstitial is shown for the second phishing URL and
354 // not for the first phishing URL. 367 // not for the first phishing URL.
355 SafeBrowsingService::UnsafeResource resource; 368 SafeBrowsingService::UnsafeResource resource;
356 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) 369 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_))
357 .WillOnce(SaveArg<0>(&resource)); 370 .WillOnce(SaveArg<0>(&resource));
358 371
359 cb->Run(phishing_url, true); // Should have no effect. 372 cb->Run(phishing_url, true); // Should have no effect.
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); 581 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get()));
569 EXPECT_EQ(url, resource.url); 582 EXPECT_EQ(url, resource.url);
570 EXPECT_EQ(url, resource.original_url); 583 EXPECT_EQ(url, resource.original_url);
571 delete resource.client; 584 delete resource.client;
572 msg = process()->sink().GetFirstMessageMatching( 585 msg = process()->sink().GetFirstMessageMatching(
573 SafeBrowsingMsg_StartPhishingDetection::ID); 586 SafeBrowsingMsg_StartPhishingDetection::ID);
574 ASSERT_FALSE(msg); 587 ASSERT_FALSE(msg);
575 } 588 }
576 589
577 } // namespace safe_browsing 590 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698