| 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 "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/task.h" | 8 #include "base/task.h" |
| 9 #include "chrome/browser/safe_browsing/browser_feature_extractor.h" | 9 #include "chrome/browser/safe_browsing/browser_feature_extractor.h" |
| 10 #include "chrome/browser/safe_browsing/client_side_detection_host.h" | 10 #include "chrome/browser/safe_browsing/client_side_detection_host.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "content/browser/tab_contents/test_tab_contents.h" | 22 #include "content/browser/tab_contents/test_tab_contents.h" |
| 23 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
| 24 #include "ipc/ipc_test_sink.h" | 24 #include "ipc/ipc_test_sink.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 27 |
| 28 using ::testing::_; | 28 using ::testing::_; |
| 29 using ::testing::DeleteArg; | 29 using ::testing::DeleteArg; |
| 30 using ::testing::DoAll; | 30 using ::testing::DoAll; |
| 31 using ::testing::Eq; | 31 using ::testing::Eq; |
| 32 using ::testing::IsNull; |
| 32 using ::testing::Mock; | 33 using ::testing::Mock; |
| 33 using ::testing::NiceMock; | 34 using ::testing::NiceMock; |
| 34 using ::testing::NotNull; | 35 using ::testing::NotNull; |
| 35 using ::testing::Pointee; | 36 using ::testing::Pointee; |
| 36 using ::testing::Return; | 37 using ::testing::Return; |
| 37 using ::testing::SaveArg; | 38 using ::testing::SaveArg; |
| 38 using ::testing::SetArgumentPointee; | 39 using ::testing::SetArgumentPointee; |
| 39 using ::testing::StrictMock; | 40 using ::testing::StrictMock; |
| 40 | 41 |
| 41 namespace { | 42 namespace { |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 // Inject service classes. | 158 // Inject service classes. |
| 158 csd_service_.reset(new StrictMock<MockClientSideDetectionService>()); | 159 csd_service_.reset(new StrictMock<MockClientSideDetectionService>()); |
| 159 sb_service_ = new StrictMock<MockSafeBrowsingService>(); | 160 sb_service_ = new StrictMock<MockSafeBrowsingService>(); |
| 160 csd_host_.reset(safe_browsing::ClientSideDetectionHost::Create( | 161 csd_host_.reset(safe_browsing::ClientSideDetectionHost::Create( |
| 161 contents_wrapper()->tab_contents())); | 162 contents_wrapper()->tab_contents())); |
| 162 csd_host_->set_client_side_detection_service(csd_service_.get()); | 163 csd_host_->set_client_side_detection_service(csd_service_.get()); |
| 163 csd_host_->set_safe_browsing_service(sb_service_.get()); | 164 csd_host_->set_safe_browsing_service(sb_service_.get()); |
| 164 } | 165 } |
| 165 | 166 |
| 166 virtual void TearDown() { | 167 virtual void TearDown() { |
| 168 // Delete the host object on the UI thread. |
| 169 BrowserThread::PostTask( |
| 170 BrowserThread::UI, |
| 171 FROM_HERE, |
| 172 new DeleteTask<ClientSideDetectionHost>(csd_host_.release())); |
| 173 message_loop_.RunAllPending(); |
| 167 TabContentsWrapperTestHarness::TearDown(); | 174 TabContentsWrapperTestHarness::TearDown(); |
| 168 io_thread_.reset(); | 175 io_thread_.reset(); |
| 169 ui_thread_.reset(); | 176 ui_thread_.reset(); |
| 170 } | 177 } |
| 171 | 178 |
| 172 void OnDetectedPhishingSite(const std::string& verdict_str) { | 179 void OnPhishingDetectionDone(const std::string& verdict_str) { |
| 173 // Make sure we have a valid BrowseInfo object set before we call this | 180 // Make sure we have a valid BrowseInfo object set before we call this |
| 174 // method. | 181 // method. |
| 175 csd_host_->browse_info_.reset(new BrowseInfo); | 182 csd_host_->browse_info_.reset(new BrowseInfo); |
| 176 csd_host_->OnDetectedPhishingSite(verdict_str); | 183 csd_host_->OnPhishingDetectionDone(verdict_str); |
| 177 } | 184 } |
| 178 | 185 |
| 179 void FlushIOMessageLoop() { | 186 void FlushIOMessageLoop() { |
| 180 // If there was a message posted on the IO thread to display the | 187 // If there was a message posted on the IO thread to display the |
| 181 // interstitial page we know that it would have been posted before | 188 // interstitial page we know that it would have been posted before |
| 182 // we put the quit message there. | 189 // we put the quit message there. |
| 183 BrowserThread::PostTask(BrowserThread::IO, | 190 BrowserThread::PostTask(BrowserThread::IO, |
| 184 FROM_HERE, | 191 FROM_HERE, |
| 185 NewRunnableFunction(&QuitUIMessageLoopFromIO)); | 192 NewRunnableFunction(&QuitUIMessageLoopFromIO)); |
| 186 MessageLoop::current()->Run(); | 193 MessageLoop::current()->Run(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 MessageLoop::current()->RunAllPending(); | 233 MessageLoop::current()->RunAllPending(); |
| 227 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 234 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
| 228 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 235 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
| 229 EXPECT_TRUE(Mock::VerifyAndClear(mock_profile_)); | 236 EXPECT_TRUE(Mock::VerifyAndClear(mock_profile_)); |
| 230 } | 237 } |
| 231 | 238 |
| 232 void SetFeatureExtractor(BrowserFeatureExtractor* extractor) { | 239 void SetFeatureExtractor(BrowserFeatureExtractor* extractor) { |
| 233 csd_host_->feature_extractor_.reset(extractor); | 240 csd_host_->feature_extractor_.reset(extractor); |
| 234 } | 241 } |
| 235 | 242 |
| 243 void SetUnsafeUniquePageIdToCurrent() { |
| 244 csd_host_->unsafe_unique_page_id_ = |
| 245 contents()->controller().GetActiveEntry()->unique_id(); |
| 246 ASSERT_TRUE(csd_host_->DidShowSBInterstitial()); |
| 247 } |
| 248 |
| 236 protected: | 249 protected: |
| 237 scoped_ptr<ClientSideDetectionHost> csd_host_; | 250 scoped_ptr<ClientSideDetectionHost> csd_host_; |
| 238 scoped_ptr<StrictMock<MockClientSideDetectionService> > csd_service_; | 251 scoped_ptr<StrictMock<MockClientSideDetectionService> > csd_service_; |
| 239 scoped_refptr<StrictMock<MockSafeBrowsingService> > sb_service_; | 252 scoped_refptr<StrictMock<MockSafeBrowsingService> > sb_service_; |
| 240 MockTestingProfile* mock_profile_; // We don't own this object | 253 MockTestingProfile* mock_profile_; // We don't own this object |
| 241 | 254 |
| 242 private: | 255 private: |
| 243 scoped_ptr<BrowserThread> ui_thread_; | 256 scoped_ptr<BrowserThread> ui_thread_; |
| 244 scoped_ptr<BrowserThread> io_thread_; | 257 scoped_ptr<BrowserThread> io_thread_; |
| 245 }; | 258 }; |
| 246 | 259 |
| 247 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteInvalidVerdict) { | 260 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneInvalidVerdict) { |
| 248 // Case 0: renderer sends an invalid verdict string that we're unable to | 261 // Case 0: renderer sends an invalid verdict string that we're unable to |
| 249 // parse. | 262 // parse. |
| 250 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 263 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
| 251 contents(), | 264 contents(), |
| 252 csd_service_.get()); | 265 csd_service_.get()); |
| 253 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 266 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
| 254 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)).Times(0); | 267 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)).Times(0); |
| 255 OnDetectedPhishingSite("Invalid Protocol Buffer"); | 268 OnPhishingDetectionDone("Invalid Protocol Buffer"); |
| 256 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor)); | 269 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor)); |
| 257 } | 270 } |
| 258 | 271 |
| 259 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteNotPhishing) { | 272 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneNotPhishing) { |
| 260 // Case 1: client thinks the page is phishing. The server does not agree. | 273 // Case 1: client thinks the page is phishing. The server does not agree. |
| 261 // No interstitial is shown. | 274 // No interstitial is shown. |
| 262 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 275 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
| 263 contents(), | 276 contents(), |
| 264 csd_service_.get()); | 277 csd_service_.get()); |
| 265 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 278 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
| 266 | 279 |
| 267 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 280 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; |
| 268 ClientPhishingRequest verdict; | 281 ClientPhishingRequest verdict; |
| 269 verdict.set_url("http://phishingurl.com/"); | 282 verdict.set_url("http://phishingurl.com/"); |
| 270 verdict.set_client_score(1.0f); | 283 verdict.set_client_score(1.0f); |
| 271 verdict.set_is_phishing(true); | 284 verdict.set_is_phishing(true); |
| 272 | 285 |
| 273 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) | 286 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) |
| 274 .WillOnce(InvokeCallbackArgument<2>(true, &verdict)); | 287 .WillOnce(InvokeCallbackArgument<2>(true, &verdict)); |
| 275 EXPECT_CALL(*csd_service_, | 288 EXPECT_CALL(*csd_service_, |
| 276 SendClientReportPhishingRequest( | 289 SendClientReportPhishingRequest( |
| 277 Pointee(PartiallyEqualVerdict(verdict)), _)) | 290 Pointee(PartiallyEqualVerdict(verdict)), _)) |
| 278 .WillOnce(SaveArg<1>(&cb)); | 291 .WillOnce(SaveArg<1>(&cb)); |
| 279 OnDetectedPhishingSite(verdict.SerializeAsString()); | 292 OnPhishingDetectionDone(verdict.SerializeAsString()); |
| 280 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 293 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
| 281 ASSERT_TRUE(cb); | 294 ASSERT_TRUE(cb); |
| 282 | 295 |
| 283 // Make sure DoDisplayBlockingPage is not going to be called. | 296 // Make sure DoDisplayBlockingPage is not going to be called. |
| 284 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); | 297 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); |
| 285 cb->Run(GURL(verdict.url()), false); | 298 cb->Run(GURL(verdict.url()), false); |
| 286 delete cb; | 299 delete cb; |
| 287 MessageLoop::current()->RunAllPending(); | 300 MessageLoop::current()->RunAllPending(); |
| 288 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 301 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
| 289 } | 302 } |
| 290 | 303 |
| 291 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteDisabled) { | 304 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneDisabled) { |
| 292 // Case 2: client thinks the page is phishing and so does the server but | 305 // Case 2: client thinks the page is phishing and so does the server but |
| 293 // showing the interstitial is disabled => no interstitial is shown. | 306 // showing the interstitial is disabled => no interstitial is shown. |
| 294 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 307 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
| 295 contents(), | 308 contents(), |
| 296 csd_service_.get()); | 309 csd_service_.get()); |
| 297 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 310 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
| 298 | 311 |
| 299 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 312 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; |
| 300 ClientPhishingRequest verdict; | 313 ClientPhishingRequest verdict; |
| 301 verdict.set_url("http://phishingurl.com/"); | 314 verdict.set_url("http://phishingurl.com/"); |
| 302 verdict.set_client_score(1.0f); | 315 verdict.set_client_score(1.0f); |
| 303 verdict.set_is_phishing(true); | 316 verdict.set_is_phishing(true); |
| 304 | 317 |
| 305 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) | 318 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) |
| 306 .WillOnce(InvokeCallbackArgument<2>(true, &verdict)); | 319 .WillOnce(InvokeCallbackArgument<2>(true, &verdict)); |
| 307 EXPECT_CALL(*csd_service_, | 320 EXPECT_CALL(*csd_service_, |
| 308 SendClientReportPhishingRequest( | 321 SendClientReportPhishingRequest( |
| 309 Pointee(PartiallyEqualVerdict(verdict)), _)) | 322 Pointee(PartiallyEqualVerdict(verdict)), _)) |
| 310 .WillOnce(SaveArg<1>(&cb)); | 323 .WillOnce(SaveArg<1>(&cb)); |
| 311 OnDetectedPhishingSite(verdict.SerializeAsString()); | 324 OnPhishingDetectionDone(verdict.SerializeAsString()); |
| 312 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 325 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
| 313 ASSERT_TRUE(cb); | 326 ASSERT_TRUE(cb); |
| 314 | 327 |
| 315 // Make sure DoDisplayBlockingPage is not going to be called. | 328 // Make sure DoDisplayBlockingPage is not going to be called. |
| 316 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); | 329 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); |
| 317 cb->Run(GURL(verdict.url()), false); | 330 cb->Run(GURL(verdict.url()), false); |
| 318 delete cb; | 331 delete cb; |
| 319 MessageLoop::current()->RunAllPending(); | 332 MessageLoop::current()->RunAllPending(); |
| 320 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 333 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
| 321 } | 334 } |
| 322 | 335 |
| 323 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteShowInterstitial) { | 336 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) { |
| 324 // Case 3: client thinks the page is phishing and so does the server. | 337 // Case 3: client thinks the page is phishing and so does the server. |
| 325 // We show an interstitial. | 338 // We show an interstitial. |
| 326 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 339 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
| 327 contents(), | 340 contents(), |
| 328 csd_service_.get()); | 341 csd_service_.get()); |
| 329 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 342 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
| 330 | 343 |
| 331 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 344 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; |
| 332 GURL phishing_url("http://phishingurl.com/"); | 345 GURL phishing_url("http://phishingurl.com/"); |
| 333 ClientPhishingRequest verdict; | 346 ClientPhishingRequest verdict; |
| 334 verdict.set_url(phishing_url.spec()); | 347 verdict.set_url(phishing_url.spec()); |
| 335 verdict.set_client_score(1.0f); | 348 verdict.set_client_score(1.0f); |
| 336 verdict.set_is_phishing(true); | 349 verdict.set_is_phishing(true); |
| 337 | 350 |
| 338 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) | 351 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) |
| 339 .WillOnce(InvokeCallbackArgument<2>(true, &verdict)); | 352 .WillOnce(InvokeCallbackArgument<2>(true, &verdict)); |
| 340 EXPECT_CALL(*csd_service_, | 353 EXPECT_CALL(*csd_service_, |
| 341 SendClientReportPhishingRequest( | 354 SendClientReportPhishingRequest( |
| 342 Pointee(PartiallyEqualVerdict(verdict)), _)) | 355 Pointee(PartiallyEqualVerdict(verdict)), _)) |
| 343 .WillOnce(SaveArg<1>(&cb)); | 356 .WillOnce(SaveArg<1>(&cb)); |
| 344 OnDetectedPhishingSite(verdict.SerializeAsString()); | 357 OnPhishingDetectionDone(verdict.SerializeAsString()); |
| 345 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 358 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
| 346 ASSERT_TRUE(cb); | 359 ASSERT_TRUE(cb); |
| 347 | 360 |
| 348 SafeBrowsingService::UnsafeResource resource; | 361 SafeBrowsingService::UnsafeResource resource; |
| 349 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) | 362 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) |
| 350 .WillOnce(SaveArg<0>(&resource)); | 363 .WillOnce(SaveArg<0>(&resource)); |
| 351 cb->Run(phishing_url, true); | 364 cb->Run(phishing_url, true); |
| 352 delete cb; | 365 delete cb; |
| 353 | 366 |
| 354 MessageLoop::current()->RunAllPending(); | 367 MessageLoop::current()->RunAllPending(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 370 NewRunnableMethod( | 383 NewRunnableMethod( |
| 371 sb_service_.get(), | 384 sb_service_.get(), |
| 372 &MockSafeBrowsingService::InvokeOnBlockingPageComplete, | 385 &MockSafeBrowsingService::InvokeOnBlockingPageComplete, |
| 373 resource.client)); | 386 resource.client)); |
| 374 // Since the CsdClient object will be deleted on the UI thread I need | 387 // Since the CsdClient object will be deleted on the UI thread I need |
| 375 // to run the UI message loop. Post a task to stop the UI message loop | 388 // to run the UI message loop. Post a task to stop the UI message loop |
| 376 // after the client object destructor is called. | 389 // after the client object destructor is called. |
| 377 FlushIOMessageLoop(); | 390 FlushIOMessageLoop(); |
| 378 } | 391 } |
| 379 | 392 |
| 380 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteMultiplePings) { | 393 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneMultiplePings) { |
| 381 // Case 4 & 5: client thinks a page is phishing then navigates to | 394 // Case 4 & 5: client thinks a page is phishing then navigates to |
| 382 // another page which is also considered phishing by the client | 395 // another page which is also considered phishing by the client |
| 383 // before the server responds with a verdict. After a while the | 396 // before the server responds with a verdict. After a while the |
| 384 // server responds for both requests with a phishing verdict. Only | 397 // server responds for both requests with a phishing verdict. Only |
| 385 // a single interstitial is shown for the second URL. | 398 // a single interstitial is shown for the second URL. |
| 386 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 399 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
| 387 contents(), | 400 contents(), |
| 388 csd_service_.get()); | 401 csd_service_.get()); |
| 389 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 402 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
| 390 | 403 |
| 391 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 404 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; |
| 392 GURL phishing_url("http://phishingurl.com/"); | 405 GURL phishing_url("http://phishingurl.com/"); |
| 393 ClientPhishingRequest verdict; | 406 ClientPhishingRequest verdict; |
| 394 verdict.set_url(phishing_url.spec()); | 407 verdict.set_url(phishing_url.spec()); |
| 395 verdict.set_client_score(1.0f); | 408 verdict.set_client_score(1.0f); |
| 396 verdict.set_is_phishing(true); | 409 verdict.set_is_phishing(true); |
| 397 | 410 |
| 398 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) | 411 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)) |
| 399 .WillOnce(InvokeCallbackArgument<2>(true, &verdict)); | 412 .WillOnce(InvokeCallbackArgument<2>(true, &verdict)); |
| 400 EXPECT_CALL(*csd_service_, | 413 EXPECT_CALL(*csd_service_, |
| 401 SendClientReportPhishingRequest( | 414 SendClientReportPhishingRequest( |
| 402 Pointee(PartiallyEqualVerdict(verdict)), _)) | 415 Pointee(PartiallyEqualVerdict(verdict)), _)) |
| 403 .WillOnce(SaveArg<1>(&cb)); | 416 .WillOnce(SaveArg<1>(&cb)); |
| 404 OnDetectedPhishingSite(verdict.SerializeAsString()); | 417 OnPhishingDetectionDone(verdict.SerializeAsString()); |
| 405 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 418 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
| 406 ASSERT_TRUE(cb); | 419 ASSERT_TRUE(cb); |
| 407 | 420 |
| 408 // Set this back to a normal browser feature extractor since we're using | 421 // Set this back to a normal browser feature extractor since we're using |
| 409 // NavigateAndCommit() and it's easier to use the real thing than setting up | 422 // NavigateAndCommit() and it's easier to use the real thing than setting up |
| 410 // mock expectations. | 423 // mock expectations. |
| 411 SetFeatureExtractor(new BrowserFeatureExtractor(contents(), | 424 SetFeatureExtractor(new BrowserFeatureExtractor(contents(), |
| 412 csd_service_.get())); | 425 csd_service_.get())); |
| 413 GURL other_phishing_url("http://other_phishing_url.com/bla"); | 426 GURL other_phishing_url("http://other_phishing_url.com/bla"); |
| 414 ExpectPreClassificationChecks(other_phishing_url, &kFalse, &kFalse, &kFalse, | 427 ExpectPreClassificationChecks(other_phishing_url, &kFalse, &kFalse, &kFalse, |
| 415 &kFalse, &kFalse, &kFalse); | 428 &kFalse, &kFalse, &kFalse); |
| 416 // We navigate away. The callback cb should be revoked. | 429 // We navigate away. The callback cb should be revoked. |
| 417 NavigateAndCommit(other_phishing_url); | 430 NavigateAndCommit(other_phishing_url); |
| 418 // Wait for the pre-classification checks to finish for other_phishing_url. | 431 // Wait for the pre-classification checks to finish for other_phishing_url. |
| 419 WaitAndCheckPreClassificationChecks(); | 432 WaitAndCheckPreClassificationChecks(); |
| 420 | 433 |
| 421 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb_other; | 434 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb_other; |
| 422 verdict.set_url(other_phishing_url.spec()); | 435 verdict.set_url(other_phishing_url.spec()); |
| 423 verdict.set_client_score(0.8f); | 436 verdict.set_client_score(0.8f); |
| 424 EXPECT_CALL(*csd_service_, | 437 EXPECT_CALL(*csd_service_, |
| 425 SendClientReportPhishingRequest( | 438 SendClientReportPhishingRequest( |
| 426 Pointee(PartiallyEqualVerdict(verdict)), _)) | 439 Pointee(PartiallyEqualVerdict(verdict)), _)) |
| 427 .WillOnce(DoAll(DeleteArg<0>(), | 440 .WillOnce(DoAll(DeleteArg<0>(), |
| 428 SaveArg<1>(&cb_other), | 441 SaveArg<1>(&cb_other), |
| 429 QuitUIMessageLoop())); | 442 QuitUIMessageLoop())); |
| 430 OnDetectedPhishingSite(verdict.SerializeAsString()); | 443 OnPhishingDetectionDone(verdict.SerializeAsString()); |
| 431 MessageLoop::current()->Run(); | 444 MessageLoop::current()->Run(); |
| 432 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 445 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
| 433 ASSERT_TRUE(cb_other); | 446 ASSERT_TRUE(cb_other); |
| 434 | 447 |
| 435 // We expect that the interstitial is shown for the second phishing URL and | 448 // We expect that the interstitial is shown for the second phishing URL and |
| 436 // not for the first phishing URL. | 449 // not for the first phishing URL. |
| 437 SafeBrowsingService::UnsafeResource resource; | 450 SafeBrowsingService::UnsafeResource resource; |
| 438 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) | 451 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) |
| 439 .WillOnce(SaveArg<0>(&resource)); | 452 .WillOnce(SaveArg<0>(&resource)); |
| 440 | 453 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 462 NewRunnableMethod( | 475 NewRunnableMethod( |
| 463 sb_service_.get(), | 476 sb_service_.get(), |
| 464 &MockSafeBrowsingService::InvokeOnBlockingPageComplete, | 477 &MockSafeBrowsingService::InvokeOnBlockingPageComplete, |
| 465 resource.client)); | 478 resource.client)); |
| 466 // Since the CsdClient object will be deleted on the UI thread I need | 479 // Since the CsdClient object will be deleted on the UI thread I need |
| 467 // to run the UI message loop. Post a task to stop the UI message loop | 480 // to run the UI message loop. Post a task to stop the UI message loop |
| 468 // after the client object destructor is called. | 481 // after the client object destructor is called. |
| 469 FlushIOMessageLoop(); | 482 FlushIOMessageLoop(); |
| 470 } | 483 } |
| 471 | 484 |
| 485 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneVerdictNotPhishing) { |
| 486 // Case 6: renderer sends a verdict string that isn't phishing. |
| 487 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
| 488 contents(), |
| 489 csd_service_.get()); |
| 490 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
| 491 |
| 492 ClientPhishingRequest verdict; |
| 493 verdict.set_url("http://not-phishing.com/"); |
| 494 verdict.set_client_score(0.1f); |
| 495 verdict.set_is_phishing(false); |
| 496 |
| 497 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)).Times(0); |
| 498 OnPhishingDetectionDone(verdict.SerializeAsString()); |
| 499 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor)); |
| 500 } |
| 501 |
| 502 TEST_F(ClientSideDetectionHostTest, |
| 503 OnPhishingDetectionDoneVerdictNotPhishingButSBMatch) { |
| 504 // Case 7: renderer sends a verdict string that isn't phishing but the URL |
| 505 // was on the regular phishing or malware lists. |
| 506 GURL url("http://not-phishing.com/"); |
| 507 ClientPhishingRequest verdict; |
| 508 verdict.set_url(url.spec()); |
| 509 verdict.set_client_score(0.1f); |
| 510 verdict.set_is_phishing(false); |
| 511 |
| 512 // First we have to navigate to the URL to set the unique page ID. |
| 513 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kFalse, |
| 514 &kFalse, &kFalse); |
| 515 NavigateAndCommit(url); |
| 516 WaitAndCheckPreClassificationChecks(); |
| 517 SetUnsafeUniquePageIdToCurrent(); |
| 518 |
| 519 EXPECT_CALL(*csd_service_, |
| 520 SendClientReportPhishingRequest( |
| 521 Pointee(PartiallyEqualVerdict(verdict)), IsNull())) |
| 522 .WillOnce(DoAll(DeleteArg<0>(), QuitUIMessageLoop())); |
| 523 OnPhishingDetectionDone(verdict.SerializeAsString()); |
| 524 MessageLoop::current()->Run(); |
| 525 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
| 526 } |
| 527 |
| 472 TEST_F(ClientSideDetectionHostTest, NavigationCancelsShouldClassifyUrl) { | 528 TEST_F(ClientSideDetectionHostTest, NavigationCancelsShouldClassifyUrl) { |
| 473 // Test that canceling pending should classify requests works as expected. | 529 // Test that canceling pending should classify requests works as expected. |
| 474 | 530 |
| 475 GURL first_url("http://first.phishy.url.com"); | 531 GURL first_url("http://first.phishy.url.com"); |
| 476 // The proxy checks is done synchronously so check that it has been done | 532 // The proxy checks is done synchronously so check that it has been done |
| 477 // for the first URL. | 533 // for the first URL. |
| 478 ExpectPreClassificationChecks(first_url, &kFalse, &kFalse, &kFalse, NULL, | 534 ExpectPreClassificationChecks(first_url, &kFalse, &kFalse, &kFalse, NULL, |
| 479 NULL, NULL); | 535 NULL, NULL); |
| 480 NavigateAndCommit(first_url); | 536 NavigateAndCommit(first_url); |
| 481 | 537 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 706 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
| 651 EXPECT_EQ(url, resource.url); | 707 EXPECT_EQ(url, resource.url); |
| 652 EXPECT_EQ(url, resource.original_url); | 708 EXPECT_EQ(url, resource.original_url); |
| 653 delete resource.client; | 709 delete resource.client; |
| 654 msg = process()->sink().GetFirstMessageMatching( | 710 msg = process()->sink().GetFirstMessageMatching( |
| 655 SafeBrowsingMsg_StartPhishingDetection::ID); | 711 SafeBrowsingMsg_StartPhishingDetection::ID); |
| 656 ASSERT_FALSE(msg); | 712 ASSERT_FALSE(msg); |
| 657 } | 713 } |
| 658 | 714 |
| 659 } // namespace safe_browsing | 715 } // namespace safe_browsing |
| OLD | NEW |