| 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/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/synchronization/waitable_event.h" |
| 8 #include "chrome/browser/safe_browsing/browser_feature_extractor.h" | 9 #include "chrome/browser/safe_browsing/browser_feature_extractor.h" |
| 9 #include "chrome/browser/safe_browsing/client_side_detection_host.h" | 10 #include "chrome/browser/safe_browsing/client_side_detection_host.h" |
| 10 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | 11 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
| 11 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 12 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 13 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 13 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" | 14 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" |
| 14 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/safe_browsing/csd.pb.h" | 16 #include "chrome/common/safe_browsing/csd.pb.h" |
| 16 #include "chrome/common/safe_browsing/safebrowsing_messages.h" | 17 #include "chrome/common/safe_browsing/safebrowsing_messages.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 public: | 153 public: |
| 153 virtual void SetUp() { | 154 virtual void SetUp() { |
| 154 // Set custom profile object so that we can mock calls to IsOffTheRecord. | 155 // Set custom profile object so that we can mock calls to IsOffTheRecord. |
| 155 // This needs to happen before we call the parent SetUp() function. We use | 156 // This needs to happen before we call the parent SetUp() function. We use |
| 156 // a nice mock because other parts of the code are calling IsOffTheRecord. | 157 // a nice mock because other parts of the code are calling IsOffTheRecord. |
| 157 mock_profile_ = new NiceMock<MockTestingProfile>(); | 158 mock_profile_ = new NiceMock<MockTestingProfile>(); |
| 158 browser_context_.reset(mock_profile_); | 159 browser_context_.reset(mock_profile_); |
| 159 | 160 |
| 160 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, | 161 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, |
| 161 &message_loop_)); | 162 &message_loop_)); |
| 163 file_user_blocking_thread_.reset( |
| 164 new content::TestBrowserThread(BrowserThread::FILE_USER_BLOCKING, |
| 165 &message_loop_)); |
| 162 // Note: we're starting a real IO thread to make sure our DCHECKs that | 166 // Note: we're starting a real IO thread to make sure our DCHECKs that |
| 163 // verify which thread is running are actually tested. | 167 // verify which thread is running are actually tested. |
| 164 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO)); | 168 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO)); |
| 165 ASSERT_TRUE(io_thread_->Start()); | 169 ASSERT_TRUE(io_thread_->Start()); |
| 166 | 170 |
| 167 TabContentsWrapperTestHarness::SetUp(); | 171 TabContentsWrapperTestHarness::SetUp(); |
| 168 | 172 |
| 169 // Inject service classes. | 173 // Inject service classes. |
| 170 csd_service_.reset(new StrictMock<MockClientSideDetectionService>()); | 174 csd_service_.reset(new StrictMock<MockClientSideDetectionService>()); |
| 171 sb_service_ = new StrictMock<MockSafeBrowsingService>(); | 175 sb_service_ = new StrictMock<MockSafeBrowsingService>(); |
| 172 csd_host_.reset(safe_browsing::ClientSideDetectionHost::Create( | 176 csd_host_.reset(safe_browsing::ClientSideDetectionHost::Create( |
| 173 contents_wrapper()->web_contents())); | 177 contents_wrapper()->web_contents())); |
| 174 csd_host_->set_client_side_detection_service(csd_service_.get()); | 178 csd_host_->set_client_side_detection_service(csd_service_.get()); |
| 175 csd_host_->set_safe_browsing_service(sb_service_.get()); | 179 csd_host_->set_safe_browsing_service(sb_service_.get()); |
| 176 // We need to create this here since we don't call | 180 // We need to create this here since we don't call |
| 177 // DidNavigateMainFramePostCommit in this test. | 181 // DidNavigateMainFramePostCommit in this test. |
| 178 csd_host_->browse_info_.reset(new BrowseInfo); | 182 csd_host_->browse_info_.reset(new BrowseInfo); |
| 179 } | 183 } |
| 180 | 184 |
| 185 static void RunAllPendingOnIO(base::WaitableEvent* event) { |
| 186 MessageLoop::current()->RunAllPending(); |
| 187 event->Signal(); |
| 188 } |
| 189 |
| 181 virtual void TearDown() { | 190 virtual void TearDown() { |
| 182 // Delete the host object on the UI thread and release the | 191 // Delete the host object on the UI thread and release the |
| 183 // SafeBrowsingService. | 192 // SafeBrowsingService. |
| 184 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, | 193 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, |
| 185 csd_host_.release()); | 194 csd_host_.release()); |
| 186 sb_service_ = NULL; | 195 sb_service_ = NULL; |
| 187 message_loop_.RunAllPending(); | 196 message_loop_.RunAllPending(); |
| 188 TabContentsWrapperTestHarness::TearDown(); | 197 TabContentsWrapperTestHarness::TearDown(); |
| 198 |
| 199 // Let the tasks on the IO thread run to avoid memory leaks. |
| 200 base::WaitableEvent done(false, false); |
| 201 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 202 base::Bind(RunAllPendingOnIO, &done)); |
| 203 done.Wait(); |
| 189 io_thread_.reset(); | 204 io_thread_.reset(); |
| 205 message_loop_.RunAllPending(); |
| 206 file_user_blocking_thread_.reset(); |
| 190 ui_thread_.reset(); | 207 ui_thread_.reset(); |
| 191 } | 208 } |
| 192 | 209 |
| 193 void OnPhishingDetectionDone(const std::string& verdict_str) { | 210 void OnPhishingDetectionDone(const std::string& verdict_str) { |
| 194 csd_host_->OnPhishingDetectionDone(verdict_str); | 211 csd_host_->OnPhishingDetectionDone(verdict_str); |
| 195 } | 212 } |
| 196 | 213 |
| 197 void FlushIOMessageLoop() { | 214 void FlushIOMessageLoop() { |
| 198 // If there was a message posted on the IO thread to display the | 215 // If there was a message posted on the IO thread to display the |
| 199 // interstitial page we know that it would have been posted before | 216 // interstitial page we know that it would have been posted before |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 } | 300 } |
| 284 | 301 |
| 285 protected: | 302 protected: |
| 286 scoped_ptr<ClientSideDetectionHost> csd_host_; | 303 scoped_ptr<ClientSideDetectionHost> csd_host_; |
| 287 scoped_ptr<StrictMock<MockClientSideDetectionService> > csd_service_; | 304 scoped_ptr<StrictMock<MockClientSideDetectionService> > csd_service_; |
| 288 scoped_refptr<StrictMock<MockSafeBrowsingService> > sb_service_; | 305 scoped_refptr<StrictMock<MockSafeBrowsingService> > sb_service_; |
| 289 MockTestingProfile* mock_profile_; // We don't own this object | 306 MockTestingProfile* mock_profile_; // We don't own this object |
| 290 | 307 |
| 291 private: | 308 private: |
| 292 scoped_ptr<content::TestBrowserThread> ui_thread_; | 309 scoped_ptr<content::TestBrowserThread> ui_thread_; |
| 310 scoped_ptr<content::TestBrowserThread> file_user_blocking_thread_; |
| 293 scoped_ptr<content::TestBrowserThread> io_thread_; | 311 scoped_ptr<content::TestBrowserThread> io_thread_; |
| 294 }; | 312 }; |
| 295 | 313 |
| 296 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneInvalidVerdict) { | 314 TEST_F(ClientSideDetectionHostTest, OnPhishingDetectionDoneInvalidVerdict) { |
| 297 // Case 0: renderer sends an invalid verdict string that we're unable to | 315 // Case 0: renderer sends an invalid verdict string that we're unable to |
| 298 // parse. | 316 // parse. |
| 299 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 317 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
| 300 contents(), | 318 contents(), |
| 301 csd_service_.get()); | 319 csd_service_.get()); |
| 302 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 320 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 730 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 748 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
| 731 EXPECT_EQ(url, resource.url); | 749 EXPECT_EQ(url, resource.url); |
| 732 EXPECT_EQ(url, resource.original_url); | 750 EXPECT_EQ(url, resource.original_url); |
| 733 resource.callback.Reset(); | 751 resource.callback.Reset(); |
| 734 msg = process()->sink().GetFirstMessageMatching( | 752 msg = process()->sink().GetFirstMessageMatching( |
| 735 SafeBrowsingMsg_StartPhishingDetection::ID); | 753 SafeBrowsingMsg_StartPhishingDetection::ID); |
| 736 ASSERT_FALSE(msg); | 754 ASSERT_FALSE(msg); |
| 737 } | 755 } |
| 738 | 756 |
| 739 } // namespace safe_browsing | 757 } // namespace safe_browsing |
| OLD | NEW |