| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/files/file_path.h" | 5 #include "base/files/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/run_loop.h" |
| 8 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 9 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| 10 #include "chrome/browser/safe_browsing/browser_feature_extractor.h" | 11 #include "chrome/browser/safe_browsing/browser_feature_extractor.h" |
| 11 #include "chrome/browser/safe_browsing/client_side_detection_host.h" | 12 #include "chrome/browser/safe_browsing/client_side_detection_host.h" |
| 12 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | 13 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
| 13 #include "chrome/browser/safe_browsing/database_manager.h" | 14 #include "chrome/browser/safe_browsing/database_manager.h" |
| 14 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 15 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 15 #include "chrome/browser/safe_browsing/ui_manager.h" | 16 #include "chrome/browser/safe_browsing/ui_manager.h" |
| 16 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/safe_browsing/csd.pb.h" | 18 #include "chrome/common/safe_browsing/csd.pb.h" |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 MOCK_METHOD3(ExtractFeatures, | 166 MOCK_METHOD3(ExtractFeatures, |
| 166 void(const BrowseInfo* info, | 167 void(const BrowseInfo* info, |
| 167 ClientPhishingRequest*, | 168 ClientPhishingRequest*, |
| 168 const BrowserFeatureExtractor::DoneCallback&)); | 169 const BrowserFeatureExtractor::DoneCallback&)); |
| 169 | 170 |
| 170 MOCK_METHOD2(ExtractMalwareFeatures, | 171 MOCK_METHOD2(ExtractMalwareFeatures, |
| 171 void(const BrowseInfo* info, | 172 void(const BrowseInfo* info, |
| 172 ClientMalwareRequest*)); | 173 ClientMalwareRequest*)); |
| 173 }; | 174 }; |
| 174 | 175 |
| 175 // Helper function which quits the UI message loop from the IO message loop. | |
| 176 void QuitUIMessageLoopFromIO() { | |
| 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 178 BrowserThread::PostTask(BrowserThread::UI, | |
| 179 FROM_HERE, | |
| 180 base::MessageLoop::QuitClosure()); | |
| 181 } | |
| 182 } // namespace | 176 } // namespace |
| 183 | 177 |
| 184 class ClientSideDetectionHostTest : public ChromeRenderViewHostTestHarness { | 178 class ClientSideDetectionHostTest : public ChromeRenderViewHostTestHarness { |
| 185 public: | 179 public: |
| 186 typedef SafeBrowsingUIManager::UnsafeResource UnsafeResource; | 180 typedef SafeBrowsingUIManager::UnsafeResource UnsafeResource; |
| 187 | 181 |
| 188 virtual void SetUp() { | 182 virtual void SetUp() { |
| 189 // Set custom profile object so that we can mock calls to IsOffTheRecord. | 183 // Set custom profile object so that we can mock calls to IsOffTheRecord. |
| 190 // This needs to happen before we call the parent SetUp() function. We use | 184 // This needs to happen before we call the parent SetUp() function. We use |
| 191 // a nice mock because other parts of the code are calling IsOffTheRecord. | 185 // a nice mock because other parts of the code are calling IsOffTheRecord. |
| 192 mock_profile_ = new NiceMock<MockTestingProfile>(); | 186 mock_profile_ = new NiceMock<MockTestingProfile>(); |
| 193 browser_context_.reset(mock_profile_); | 187 browser_context_.reset(mock_profile_); |
| 194 | 188 |
| 195 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, | |
| 196 &message_loop_)); | |
| 197 file_user_blocking_thread_.reset( | |
| 198 new content::TestBrowserThread(BrowserThread::FILE_USER_BLOCKING, | |
| 199 &message_loop_)); | |
| 200 // Note: we're starting a real IO thread to make sure our DCHECKs that | |
| 201 // verify which thread is running are actually tested. | |
| 202 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO)); | |
| 203 ASSERT_TRUE(io_thread_->Start()); | |
| 204 | |
| 205 ChromeRenderViewHostTestHarness::SetUp(); | 189 ChromeRenderViewHostTestHarness::SetUp(); |
| 206 | 190 |
| 207 // Inject service classes. | 191 // Inject service classes. |
| 208 csd_service_.reset(new StrictMock<MockClientSideDetectionService>()); | 192 csd_service_.reset(new StrictMock<MockClientSideDetectionService>()); |
| 209 // Only used for initializing mock objects. | 193 // Only used for initializing mock objects. |
| 210 SafeBrowsingService* sb_service = | 194 SafeBrowsingService* sb_service = |
| 211 SafeBrowsingService::CreateSafeBrowsingService(); | 195 SafeBrowsingService::CreateSafeBrowsingService(); |
| 212 database_manager_ = | 196 database_manager_ = |
| 213 new StrictMock<MockSafeBrowsingDatabaseManager>(sb_service); | 197 new StrictMock<MockSafeBrowsingDatabaseManager>(sb_service); |
| 214 ui_manager_ = new StrictMock<MockSafeBrowsingUIManager>(sb_service); | 198 ui_manager_ = new StrictMock<MockSafeBrowsingUIManager>(sb_service); |
| 215 csd_host_.reset(safe_browsing::ClientSideDetectionHost::Create( | 199 csd_host_.reset(safe_browsing::ClientSideDetectionHost::Create( |
| 216 web_contents())); | 200 web_contents())); |
| 217 csd_host_->set_client_side_detection_service(csd_service_.get()); | 201 csd_host_->set_client_side_detection_service(csd_service_.get()); |
| 218 csd_host_->set_safe_browsing_managers(ui_manager_.get(), | 202 csd_host_->set_safe_browsing_managers(ui_manager_.get(), |
| 219 database_manager_.get()); | 203 database_manager_.get()); |
| 220 // We need to create this here since we don't call | 204 // We need to create this here since we don't call |
| 221 // DidNavigateMainFramePostCommit in this test. | 205 // DidNavigateMainFramePostCommit in this test. |
| 222 csd_host_->browse_info_.reset(new BrowseInfo); | 206 csd_host_->browse_info_.reset(new BrowseInfo); |
| 223 | 207 |
| 224 // By default this is set to false. Turn it on as if we are in canary or | 208 // By default this is set to false. Turn it on as if we are in canary or |
| 225 // dev channel | 209 // dev channel |
| 226 csd_host_->malware_report_enabled_ = true; | 210 csd_host_->malware_report_enabled_ = true; |
| 227 } | 211 } |
| 228 | 212 |
| 229 static void RunAllPendingOnIO(base::WaitableEvent* event) { | |
| 230 base::MessageLoop::current()->RunUntilIdle(); | |
| 231 event->Signal(); | |
| 232 } | |
| 233 | |
| 234 virtual void TearDown() { | 213 virtual void TearDown() { |
| 235 // Delete the host object on the UI thread and release the | 214 // Delete the host object on the UI thread and release the |
| 236 // SafeBrowsingService. | 215 // SafeBrowsingService. |
| 237 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, | 216 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, |
| 238 csd_host_.release()); | 217 csd_host_.release()); |
| 239 database_manager_ = NULL; | 218 database_manager_ = NULL; |
| 240 ui_manager_ = NULL; | 219 ui_manager_ = NULL; |
| 241 message_loop_.RunUntilIdle(); | 220 base::RunLoop().RunUntilIdle(); |
| 242 ChromeRenderViewHostTestHarness::TearDown(); | 221 ChromeRenderViewHostTestHarness::TearDown(); |
| 243 | |
| 244 // Let the tasks on the IO thread run to avoid memory leaks. | |
| 245 base::WaitableEvent done(false, false); | |
| 246 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 247 base::Bind(RunAllPendingOnIO, &done)); | |
| 248 done.Wait(); | |
| 249 io_thread_.reset(); | |
| 250 message_loop_.RunUntilIdle(); | |
| 251 file_user_blocking_thread_.reset(); | |
| 252 ui_thread_.reset(); | |
| 253 } | 222 } |
| 254 | 223 |
| 255 void OnPhishingDetectionDone(const std::string& verdict_str) { | 224 void OnPhishingDetectionDone(const std::string& verdict_str) { |
| 256 csd_host_->OnPhishingDetectionDone(verdict_str); | 225 csd_host_->OnPhishingDetectionDone(verdict_str); |
| 257 } | 226 } |
| 258 | 227 |
| 259 void UpdateIPHostMap(const std::string& ip, const std::string& host) { | 228 void UpdateIPHostMap(const std::string& ip, const std::string& host) { |
| 260 csd_host_->UpdateIPHostMap(ip, host); | 229 csd_host_->UpdateIPHostMap(ip, host); |
| 261 } | 230 } |
| 262 | 231 |
| 263 BrowseInfo* GetBrowseInfo() { | 232 BrowseInfo* GetBrowseInfo() { |
| 264 return csd_host_->browse_info_.get(); | 233 return csd_host_->browse_info_.get(); |
| 265 } | 234 } |
| 266 | 235 |
| 267 void FlushIOMessageLoop() { | |
| 268 // If there was a message posted on the IO thread to display the | |
| 269 // interstitial page we know that it would have been posted before | |
| 270 // we put the quit message there. | |
| 271 BrowserThread::PostTask(BrowserThread::IO, | |
| 272 FROM_HERE, | |
| 273 base::Bind(&QuitUIMessageLoopFromIO)); | |
| 274 base::MessageLoop::current()->Run(); | |
| 275 } | |
| 276 | |
| 277 void ExpectPreClassificationChecks(const GURL& url, | 236 void ExpectPreClassificationChecks(const GURL& url, |
| 278 const bool* is_private, | 237 const bool* is_private, |
| 279 const bool* is_incognito, | 238 const bool* is_incognito, |
| 280 const bool* match_csd_whitelist, | 239 const bool* match_csd_whitelist, |
| 281 const bool* get_valid_cached_result, | 240 const bool* get_valid_cached_result, |
| 282 const bool* is_in_cache, | 241 const bool* is_in_cache, |
| 283 const bool* over_report_limit) { | 242 const bool* over_report_limit) { |
| 284 if (is_private) { | 243 if (is_private) { |
| 285 EXPECT_CALL(*csd_service_, IsPrivateIPAddress(_)) | 244 EXPECT_CALL(*csd_service_, IsPrivateIPAddress(_)) |
| 286 .WillOnce(Return(*is_private)); | 245 .WillOnce(Return(*is_private)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 301 if (is_in_cache) { | 260 if (is_in_cache) { |
| 302 EXPECT_CALL(*csd_service_, IsInCache(url)).WillOnce(Return(*is_in_cache)); | 261 EXPECT_CALL(*csd_service_, IsInCache(url)).WillOnce(Return(*is_in_cache)); |
| 303 } | 262 } |
| 304 if (over_report_limit) { | 263 if (over_report_limit) { |
| 305 EXPECT_CALL(*csd_service_, OverPhishingReportLimit()) | 264 EXPECT_CALL(*csd_service_, OverPhishingReportLimit()) |
| 306 .WillOnce(Return(*over_report_limit)); | 265 .WillOnce(Return(*over_report_limit)); |
| 307 } | 266 } |
| 308 } | 267 } |
| 309 | 268 |
| 310 void WaitAndCheckPreClassificationChecks() { | 269 void WaitAndCheckPreClassificationChecks() { |
| 311 // Wait for CheckCsdWhitelist to be called if at all. | 270 // Wait for CheckCsdWhitelist and CheckCache() to be called if at all. |
| 312 FlushIOMessageLoop(); | 271 base::RunLoop().RunUntilIdle(); |
| 313 // Checks for CheckCache() to be called if at all. | |
| 314 base::MessageLoop::current()->RunUntilIdle(); | |
| 315 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 272 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
| 316 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); | 273 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); |
| 317 EXPECT_TRUE(Mock::VerifyAndClear(database_manager_.get())); | 274 EXPECT_TRUE(Mock::VerifyAndClear(database_manager_.get())); |
| 318 EXPECT_TRUE(Mock::VerifyAndClear(mock_profile_)); | 275 EXPECT_TRUE(Mock::VerifyAndClear(mock_profile_)); |
| 319 } | 276 } |
| 320 | 277 |
| 321 void SetFeatureExtractor(BrowserFeatureExtractor* extractor) { | 278 void SetFeatureExtractor(BrowserFeatureExtractor* extractor) { |
| 322 csd_host_->feature_extractor_.reset(extractor); | 279 csd_host_->feature_extractor_.reset(extractor); |
| 323 } | 280 } |
| 324 | 281 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 353 EXPECT_EQ(resource.render_view_id, | 310 EXPECT_EQ(resource.render_view_id, |
| 354 csd_host_->unsafe_resource_->render_view_id); | 311 csd_host_->unsafe_resource_->render_view_id); |
| 355 } | 312 } |
| 356 | 313 |
| 357 protected: | 314 protected: |
| 358 scoped_ptr<ClientSideDetectionHost> csd_host_; | 315 scoped_ptr<ClientSideDetectionHost> csd_host_; |
| 359 scoped_ptr<StrictMock<MockClientSideDetectionService> > csd_service_; | 316 scoped_ptr<StrictMock<MockClientSideDetectionService> > csd_service_; |
| 360 scoped_refptr<StrictMock<MockSafeBrowsingUIManager> > ui_manager_; | 317 scoped_refptr<StrictMock<MockSafeBrowsingUIManager> > ui_manager_; |
| 361 scoped_refptr<StrictMock<MockSafeBrowsingDatabaseManager> > database_manager_; | 318 scoped_refptr<StrictMock<MockSafeBrowsingDatabaseManager> > database_manager_; |
| 362 MockTestingProfile* mock_profile_; // We don't own this object | 319 MockTestingProfile* mock_profile_; // We don't own this object |
| 363 | |
| 364 private: | |
| 365 scoped_ptr<content::TestBrowserThread> ui_thread_; | |
| 366 scoped_ptr<content::TestBrowserThread> file_user_blocking_thread_; | |
| 367 scoped_ptr<content::TestBrowserThread> io_thread_; | |
| 368 }; | 320 }; |
| 369 | 321 |
| 370 | 322 |
| 371 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneInvalidVerdict) { | 323 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneInvalidVerdict) { |
| 372 // Case 0: renderer sends an invalid verdict string that we're unable to | 324 // Case 0: renderer sends an invalid verdict string that we're unable to |
| 373 // parse. | 325 // parse. |
| 374 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 326 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
| 375 web_contents(), | 327 web_contents(), |
| 376 csd_service_.get()); | 328 csd_service_.get()); |
| 377 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 329 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 401 SendClientReportPhishingRequest( | 353 SendClientReportPhishingRequest( |
| 402 Pointee(PartiallyEqualVerdict(verdict)), _)) | 354 Pointee(PartiallyEqualVerdict(verdict)), _)) |
| 403 .WillOnce(SaveArg<1>(&cb)); | 355 .WillOnce(SaveArg<1>(&cb)); |
| 404 OnPhishingDetectionDone(verdict.SerializeAsString()); | 356 OnPhishingDetectionDone(verdict.SerializeAsString()); |
| 405 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 357 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
| 406 ASSERT_FALSE(cb.is_null()); | 358 ASSERT_FALSE(cb.is_null()); |
| 407 | 359 |
| 408 // Make sure DoDisplayBlockingPage is not going to be called. | 360 // Make sure DoDisplayBlockingPage is not going to be called. |
| 409 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)).Times(0); | 361 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)).Times(0); |
| 410 cb.Run(GURL(verdict.url()), false); | 362 cb.Run(GURL(verdict.url()), false); |
| 411 base::MessageLoop::current()->RunUntilIdle(); | 363 base::RunLoop().RunUntilIdle(); |
| 412 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); | 364 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); |
| 413 } | 365 } |
| 414 | 366 |
| 415 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneDisabled) { | 367 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneDisabled) { |
| 416 // Case 2: client thinks the page is phishing and so does the server but | 368 // Case 2: client thinks the page is phishing and so does the server but |
| 417 // showing the interstitial is disabled => no interstitial is shown. | 369 // showing the interstitial is disabled => no interstitial is shown. |
| 418 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 370 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
| 419 web_contents(), | 371 web_contents(), |
| 420 csd_service_.get()); | 372 csd_service_.get()); |
| 421 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 373 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 433 SendClientReportPhishingRequest( | 385 SendClientReportPhishingRequest( |
| 434 Pointee(PartiallyEqualVerdict(verdict)), _)) | 386 Pointee(PartiallyEqualVerdict(verdict)), _)) |
| 435 .WillOnce(SaveArg<1>(&cb)); | 387 .WillOnce(SaveArg<1>(&cb)); |
| 436 OnPhishingDetectionDone(verdict.SerializeAsString()); | 388 OnPhishingDetectionDone(verdict.SerializeAsString()); |
| 437 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 389 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
| 438 ASSERT_FALSE(cb.is_null()); | 390 ASSERT_FALSE(cb.is_null()); |
| 439 | 391 |
| 440 // Make sure DoDisplayBlockingPage is not going to be called. | 392 // Make sure DoDisplayBlockingPage is not going to be called. |
| 441 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)).Times(0); | 393 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)).Times(0); |
| 442 cb.Run(GURL(verdict.url()), false); | 394 cb.Run(GURL(verdict.url()), false); |
| 443 base::MessageLoop::current()->RunUntilIdle(); | 395 base::RunLoop().RunUntilIdle(); |
| 444 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); | 396 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); |
| 445 } | 397 } |
| 446 | 398 |
| 447 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) { | 399 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneShowInterstitial) { |
| 448 // Case 3: client thinks the page is phishing and so does the server. | 400 // Case 3: client thinks the page is phishing and so does the server. |
| 449 // We show an interstitial. | 401 // We show an interstitial. |
| 450 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 402 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
| 451 web_contents(), | 403 web_contents(), |
| 452 csd_service_.get()); | 404 csd_service_.get()); |
| 453 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 405 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 468 .WillOnce(SaveArg<1>(&cb)); | 420 .WillOnce(SaveArg<1>(&cb)); |
| 469 OnPhishingDetectionDone(verdict.SerializeAsString()); | 421 OnPhishingDetectionDone(verdict.SerializeAsString()); |
| 470 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 422 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
| 471 ASSERT_FALSE(cb.is_null()); | 423 ASSERT_FALSE(cb.is_null()); |
| 472 | 424 |
| 473 UnsafeResource resource; | 425 UnsafeResource resource; |
| 474 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)) | 426 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)) |
| 475 .WillOnce(SaveArg<0>(&resource)); | 427 .WillOnce(SaveArg<0>(&resource)); |
| 476 cb.Run(phishing_url, true); | 428 cb.Run(phishing_url, true); |
| 477 | 429 |
| 478 base::MessageLoop::current()->RunUntilIdle(); | 430 base::RunLoop().RunUntilIdle(); |
| 479 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); | 431 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); |
| 480 EXPECT_EQ(phishing_url, resource.url); | 432 EXPECT_EQ(phishing_url, resource.url); |
| 481 EXPECT_EQ(phishing_url, resource.original_url); | 433 EXPECT_EQ(phishing_url, resource.original_url); |
| 482 EXPECT_FALSE(resource.is_subresource); | 434 EXPECT_FALSE(resource.is_subresource); |
| 483 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL, resource.threat_type); | 435 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL, resource.threat_type); |
| 484 EXPECT_EQ(web_contents()->GetRenderProcessHost()->GetID(), | 436 EXPECT_EQ(web_contents()->GetRenderProcessHost()->GetID(), |
| 485 resource.render_process_host_id); | 437 resource.render_process_host_id); |
| 486 EXPECT_EQ(web_contents()->GetRenderViewHost()->GetRoutingID(), | 438 EXPECT_EQ(web_contents()->GetRenderViewHost()->GetRoutingID(), |
| 487 resource.render_view_id); | 439 resource.render_view_id); |
| 488 | 440 |
| 489 // Make sure the client object will be deleted. | 441 // Make sure the client object will be deleted. |
| 490 BrowserThread::PostTask( | 442 BrowserThread::PostTask( |
| 491 BrowserThread::IO, | 443 BrowserThread::IO, |
| 492 FROM_HERE, | 444 FROM_HERE, |
| 493 base::Bind(&MockSafeBrowsingUIManager::InvokeOnBlockingPageComplete, | 445 base::Bind(&MockSafeBrowsingUIManager::InvokeOnBlockingPageComplete, |
| 494 ui_manager_, resource.callback)); | 446 ui_manager_, resource.callback)); |
| 495 // Since the CsdClient object will be deleted on the UI thread I need | |
| 496 // to run the UI message loop. Post a task to stop the UI message loop | |
| 497 // after the client object destructor is called. | |
| 498 FlushIOMessageLoop(); | |
| 499 } | 447 } |
| 500 | 448 |
| 501 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneMultiplePings) { | 449 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneMultiplePings) { |
| 502 // Case 4 & 5: client thinks a page is phishing then navigates to | 450 // Case 4 & 5: client thinks a page is phishing then navigates to |
| 503 // another page which is also considered phishing by the client | 451 // another page which is also considered phishing by the client |
| 504 // before the server responds with a verdict. After a while the | 452 // before the server responds with a verdict. After a while the |
| 505 // server responds for both requests with a phishing verdict. Only | 453 // server responds for both requests with a phishing verdict. Only |
| 506 // a single interstitial is shown for the second URL. | 454 // a single interstitial is shown for the second URL. |
| 507 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 455 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
| 508 web_contents(), | 456 web_contents(), |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 | 507 |
| 560 // We expect that the interstitial is shown for the second phishing URL and | 508 // We expect that the interstitial is shown for the second phishing URL and |
| 561 // not for the first phishing URL. | 509 // not for the first phishing URL. |
| 562 UnsafeResource resource; | 510 UnsafeResource resource; |
| 563 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)) | 511 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)) |
| 564 .WillOnce(SaveArg<0>(&resource)); | 512 .WillOnce(SaveArg<0>(&resource)); |
| 565 | 513 |
| 566 cb.Run(phishing_url, true); // Should have no effect. | 514 cb.Run(phishing_url, true); // Should have no effect. |
| 567 cb_other.Run(other_phishing_url, true); // Should show interstitial. | 515 cb_other.Run(other_phishing_url, true); // Should show interstitial. |
| 568 | 516 |
| 569 base::MessageLoop::current()->RunUntilIdle(); | 517 base::RunLoop().RunUntilIdle(); |
| 570 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); | 518 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); |
| 571 EXPECT_EQ(other_phishing_url, resource.url); | 519 EXPECT_EQ(other_phishing_url, resource.url); |
| 572 EXPECT_EQ(other_phishing_url, resource.original_url); | 520 EXPECT_EQ(other_phishing_url, resource.original_url); |
| 573 EXPECT_FALSE(resource.is_subresource); | 521 EXPECT_FALSE(resource.is_subresource); |
| 574 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL, resource.threat_type); | 522 EXPECT_EQ(SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL, resource.threat_type); |
| 575 EXPECT_EQ(web_contents()->GetRenderProcessHost()->GetID(), | 523 EXPECT_EQ(web_contents()->GetRenderProcessHost()->GetID(), |
| 576 resource.render_process_host_id); | 524 resource.render_process_host_id); |
| 577 EXPECT_EQ(web_contents()->GetRenderViewHost()->GetRoutingID(), | 525 EXPECT_EQ(web_contents()->GetRenderViewHost()->GetRoutingID(), |
| 578 resource.render_view_id); | 526 resource.render_view_id); |
| 579 | 527 |
| 580 // Make sure the client object will be deleted. | 528 // Make sure the client object will be deleted. |
| 581 BrowserThread::PostTask( | 529 BrowserThread::PostTask( |
| 582 BrowserThread::IO, | 530 BrowserThread::IO, |
| 583 FROM_HERE, | 531 FROM_HERE, |
| 584 base::Bind(&MockSafeBrowsingUIManager::InvokeOnBlockingPageComplete, | 532 base::Bind(&MockSafeBrowsingUIManager::InvokeOnBlockingPageComplete, |
| 585 ui_manager_, resource.callback)); | 533 ui_manager_, resource.callback)); |
| 586 // Since the CsdClient object will be deleted on the UI thread I need | |
| 587 // to run the UI message loop. Post a task to stop the UI message loop | |
| 588 // after the client object destructor is called. | |
| 589 FlushIOMessageLoop(); | |
| 590 } | 534 } |
| 591 | 535 |
| 592 TEST_F(ClientSideDetectionHostTest, | 536 TEST_F(ClientSideDetectionHostTest, |
| 593 OnPhishingDetectionDoneVerdictNotPhishing) { | 537 OnPhishingDetectionDoneVerdictNotPhishing) { |
| 594 // Case 6: renderer sends a verdict string that isn't phishing. | 538 // Case 6: renderer sends a verdict string that isn't phishing. |
| 595 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 539 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
| 596 web_contents(), | 540 web_contents(), |
| 597 csd_service_.get()); | 541 csd_service_.get()); |
| 598 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 542 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
| 599 | 543 |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 949 // with no start classification message. | 893 // with no start classification message. |
| 950 url = GURL("http://host8.com/"); | 894 url = GURL("http://host8.com/"); |
| 951 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kTrue, NULL, | 895 ExpectPreClassificationChecks(url, &kFalse, &kFalse, &kFalse, &kTrue, NULL, |
| 952 NULL); | 896 NULL); |
| 953 | 897 |
| 954 UnsafeResource resource; | 898 UnsafeResource resource; |
| 955 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)) | 899 EXPECT_CALL(*ui_manager_.get(), DoDisplayBlockingPage(_)) |
| 956 .WillOnce(SaveArg<0>(&resource)); | 900 .WillOnce(SaveArg<0>(&resource)); |
| 957 | 901 |
| 958 NavigateAndCommit(url); | 902 NavigateAndCommit(url); |
| 959 // Wait for CheckCsdWhitelist to be called on the IO thread. | 903 // Wait for CheckCsdWhitelist and CheckCache() to be called. |
| 960 FlushIOMessageLoop(); | 904 base::RunLoop().RunUntilIdle(); |
| 961 // Wait for CheckCache() to be called on the UI thread. | |
| 962 base::MessageLoop::current()->RunUntilIdle(); | |
| 963 // Now we check that all expected functions were indeed called on the two | 905 // Now we check that all expected functions were indeed called on the two |
| 964 // service objects. | 906 // service objects. |
| 965 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 907 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
| 966 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); | 908 EXPECT_TRUE(Mock::VerifyAndClear(ui_manager_.get())); |
| 967 EXPECT_EQ(url, resource.url); | 909 EXPECT_EQ(url, resource.url); |
| 968 EXPECT_EQ(url, resource.original_url); | 910 EXPECT_EQ(url, resource.original_url); |
| 969 resource.callback.Reset(); | 911 resource.callback.Reset(); |
| 970 msg = process()->sink().GetFirstMessageMatching( | 912 msg = process()->sink().GetFirstMessageMatching( |
| 971 SafeBrowsingMsg_StartPhishingDetection::ID); | 913 SafeBrowsingMsg_StartPhishingDetection::ID); |
| 972 ASSERT_FALSE(msg); | 914 ASSERT_FALSE(msg); |
| 973 } | 915 } |
| 974 | 916 |
| 975 } // namespace safe_browsing | 917 } // namespace safe_browsing |
| OLD | NEW |