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 27 matching lines...) Expand all Loading... | |
38 using ::testing::SetArgumentPointee; | 38 using ::testing::SetArgumentPointee; |
39 using ::testing::StrictMock; | 39 using ::testing::StrictMock; |
40 | 40 |
41 namespace { | 41 namespace { |
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 namespace { | 47 namespace { |
48 MATCHER_P(EqualsProto, other, "") { | 48 MATCHER_P(PartiallyEqualVerdict, other, "") { |
mattm
2011/06/20 22:33:06
maybe add a comment about why we only match some p
noelutz
2011/06/20 22:58:12
Done.
| |
49 return other.SerializeAsString() == arg.SerializeAsString(); | 49 return (other.url() == arg.url() && |
50 other.client_score() == arg.client_score() && | |
51 other.is_phishing() == arg.is_phishing()); | |
50 } | 52 } |
51 | 53 |
52 ACTION(QuitUIMessageLoop) { | 54 ACTION(QuitUIMessageLoop) { |
53 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 55 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
54 MessageLoopForUI::current()->Quit(); | 56 MessageLoopForUI::current()->Quit(); |
55 } | 57 } |
56 | 58 |
57 class MockClientSideDetectionService : public ClientSideDetectionService { | 59 class MockClientSideDetectionService : public ClientSideDetectionService { |
58 public: | 60 public: |
59 MockClientSideDetectionService() : ClientSideDetectionService(NULL) {} | 61 MockClientSideDetectionService() : ClientSideDetectionService(NULL) {} |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 | 102 |
101 MOCK_METHOD0(IsOffTheRecord, bool()); | 103 MOCK_METHOD0(IsOffTheRecord, bool()); |
102 }; | 104 }; |
103 | 105 |
104 class MockBrowserFeatureExtractor : public BrowserFeatureExtractor { | 106 class MockBrowserFeatureExtractor : public BrowserFeatureExtractor { |
105 public: | 107 public: |
106 explicit MockBrowserFeatureExtractor(TabContents* tab) | 108 explicit MockBrowserFeatureExtractor(TabContents* tab) |
107 : BrowserFeatureExtractor(tab) {} | 109 : BrowserFeatureExtractor(tab) {} |
108 virtual ~MockBrowserFeatureExtractor() {} | 110 virtual ~MockBrowserFeatureExtractor() {} |
109 | 111 |
110 MOCK_METHOD2(ExtractFeatures, void(ClientPhishingRequest*, | 112 MOCK_METHOD3(ExtractFeatures, void(const BrowseInfo& info, |
113 ClientPhishingRequest*, | |
111 BrowserFeatureExtractor::DoneCallback*)); | 114 BrowserFeatureExtractor::DoneCallback*)); |
112 }; | 115 }; |
113 | 116 |
114 // Helper function which quits the UI message loop from the IO message loop. | 117 // Helper function which quits the UI message loop from the IO message loop. |
115 void QuitUIMessageLoopFromIO() { | 118 void QuitUIMessageLoopFromIO() { |
116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
117 BrowserThread::PostTask(BrowserThread::UI, | 120 BrowserThread::PostTask(BrowserThread::UI, |
118 FROM_HERE, | 121 FROM_HERE, |
119 new MessageLoop::QuitTask()); | 122 new MessageLoop::QuitTask()); |
120 } | 123 } |
(...skipping 23 matching lines...) Expand all Loading... | |
144 csd_host_->set_safe_browsing_service(sb_service_.get()); | 147 csd_host_->set_safe_browsing_service(sb_service_.get()); |
145 } | 148 } |
146 | 149 |
147 virtual void TearDown() { | 150 virtual void TearDown() { |
148 io_thread_.reset(); | 151 io_thread_.reset(); |
149 ui_thread_.reset(); | 152 ui_thread_.reset(); |
150 TabContentsWrapperTestHarness::TearDown(); | 153 TabContentsWrapperTestHarness::TearDown(); |
151 } | 154 } |
152 | 155 |
153 void OnDetectedPhishingSite(const std::string& verdict_str) { | 156 void OnDetectedPhishingSite(const std::string& verdict_str) { |
157 // Make sure we have a valid BrowseInfo object set before we call this | |
158 // method. | |
159 csd_host_->browse_info_.reset(new BrowseInfo); | |
154 csd_host_->OnDetectedPhishingSite(verdict_str); | 160 csd_host_->OnDetectedPhishingSite(verdict_str); |
155 } | 161 } |
156 | 162 |
157 void FlushIOMessageLoop() { | 163 void FlushIOMessageLoop() { |
158 // If there was a message posted on the IO thread to display the | 164 // If there was a message posted on the IO thread to display the |
159 // interstitial page we know that it would have been posted before | 165 // interstitial page we know that it would have been posted before |
160 // we put the quit message there. | 166 // we put the quit message there. |
161 BrowserThread::PostTask(BrowserThread::IO, | 167 BrowserThread::PostTask(BrowserThread::IO, |
162 FROM_HERE, | 168 FROM_HERE, |
163 NewRunnableFunction(&QuitUIMessageLoopFromIO)); | 169 NewRunnableFunction(&QuitUIMessageLoopFromIO)); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
221 scoped_ptr<BrowserThread> ui_thread_; | 227 scoped_ptr<BrowserThread> ui_thread_; |
222 scoped_ptr<BrowserThread> io_thread_; | 228 scoped_ptr<BrowserThread> io_thread_; |
223 }; | 229 }; |
224 | 230 |
225 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteInvalidVerdict) { | 231 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteInvalidVerdict) { |
226 // Case 0: renderer sends an invalid verdict string that we're unable to | 232 // Case 0: renderer sends an invalid verdict string that we're unable to |
227 // parse. | 233 // parse. |
228 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( | 234 MockBrowserFeatureExtractor* mock_extractor = new MockBrowserFeatureExtractor( |
229 contents()); | 235 contents()); |
230 SetFeatureExtractor(mock_extractor); // The host class takes ownership. | 236 SetFeatureExtractor(mock_extractor); // The host class takes ownership. |
231 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _)).Times(0); | 237 EXPECT_CALL(*mock_extractor, ExtractFeatures(_, _, _)).Times(0); |
232 OnDetectedPhishingSite("Invalid Protocol Buffer"); | 238 OnDetectedPhishingSite("Invalid Protocol Buffer"); |
233 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor)); | 239 EXPECT_TRUE(Mock::VerifyAndClear(mock_extractor)); |
234 } | 240 } |
235 | 241 |
236 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteNotPhishing) { | 242 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteNotPhishing) { |
237 // Case 1: client thinks the page is phishing. The server does not agree. | 243 // Case 1: client thinks the page is phishing. The server does not agree. |
238 // No interstitial is shown. | 244 // No interstitial is shown. |
239 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 245 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; |
240 ClientPhishingRequest verdict; | 246 ClientPhishingRequest verdict; |
241 verdict.set_url("http://phishingurl.com/"); | 247 verdict.set_url("http://phishingurl.com/"); |
242 verdict.set_client_score(1.0f); | 248 verdict.set_client_score(1.0f); |
243 verdict.set_is_phishing(true); | 249 verdict.set_is_phishing(true); |
244 | 250 |
245 EXPECT_CALL(*csd_service_, | 251 EXPECT_CALL(*csd_service_, |
246 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) | 252 SendClientReportPhishingRequest( |
253 Pointee(PartiallyEqualVerdict(verdict)), _)) | |
247 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop())); | 254 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop())); |
248 OnDetectedPhishingSite(verdict.SerializeAsString()); | 255 OnDetectedPhishingSite(verdict.SerializeAsString()); |
249 MessageLoop::current()->Run(); | 256 MessageLoop::current()->Run(); |
250 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 257 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
251 ASSERT_TRUE(cb); | 258 ASSERT_TRUE(cb); |
252 | 259 |
253 // Make sure DoDisplayBlockingPage is not going to be called. | 260 // Make sure DoDisplayBlockingPage is not going to be called. |
254 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); | 261 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); |
255 cb->Run(GURL(verdict.url()), false); | 262 cb->Run(GURL(verdict.url()), false); |
256 delete cb; | 263 delete cb; |
257 MessageLoop::current()->RunAllPending(); | 264 MessageLoop::current()->RunAllPending(); |
258 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 265 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
259 } | 266 } |
260 | 267 |
261 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteDisabled) { | 268 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteDisabled) { |
262 // Case 2: client thinks the page is phishing and so does the server but | 269 // Case 2: client thinks the page is phishing and so does the server but |
263 // showing the interstitial is disabled => no interstitial is shown. | 270 // showing the interstitial is disabled => no interstitial is shown. |
264 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 271 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; |
265 ClientPhishingRequest verdict; | 272 ClientPhishingRequest verdict; |
266 verdict.set_url("http://phishingurl.com/"); | 273 verdict.set_url("http://phishingurl.com/"); |
267 verdict.set_client_score(1.0f); | 274 verdict.set_client_score(1.0f); |
268 verdict.set_is_phishing(true); | 275 verdict.set_is_phishing(true); |
269 | 276 |
270 EXPECT_CALL(*csd_service_, | 277 EXPECT_CALL(*csd_service_, |
271 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) | 278 SendClientReportPhishingRequest( |
279 Pointee(PartiallyEqualVerdict(verdict)), _)) | |
272 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop())); | 280 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop())); |
273 OnDetectedPhishingSite(verdict.SerializeAsString()); | 281 OnDetectedPhishingSite(verdict.SerializeAsString()); |
274 MessageLoop::current()->Run(); | 282 MessageLoop::current()->Run(); |
275 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 283 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
276 ASSERT_TRUE(cb); | 284 ASSERT_TRUE(cb); |
277 | 285 |
278 // Make sure DoDisplayBlockingPage is not going to be called. | 286 // Make sure DoDisplayBlockingPage is not going to be called. |
279 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); | 287 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); |
280 cb->Run(GURL(verdict.url()), false); | 288 cb->Run(GURL(verdict.url()), false); |
281 delete cb; | 289 delete cb; |
282 MessageLoop::current()->RunAllPending(); | 290 MessageLoop::current()->RunAllPending(); |
283 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 291 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
284 } | 292 } |
285 | 293 |
286 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteShowInterstitial) { | 294 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteShowInterstitial) { |
287 // Case 3: client thinks the page is phishing and so does the server. | 295 // Case 3: client thinks the page is phishing and so does the server. |
288 // We show an interstitial. | 296 // We show an interstitial. |
289 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 297 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; |
290 GURL phishing_url("http://phishingurl.com/"); | 298 GURL phishing_url("http://phishingurl.com/"); |
291 ClientPhishingRequest verdict; | 299 ClientPhishingRequest verdict; |
292 verdict.set_url(phishing_url.spec()); | 300 verdict.set_url(phishing_url.spec()); |
293 verdict.set_client_score(1.0f); | 301 verdict.set_client_score(1.0f); |
294 verdict.set_is_phishing(true); | 302 verdict.set_is_phishing(true); |
295 | 303 |
296 EXPECT_CALL(*csd_service_, | 304 EXPECT_CALL(*csd_service_, |
297 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) | 305 SendClientReportPhishingRequest( |
306 Pointee(PartiallyEqualVerdict(verdict)), _)) | |
298 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop())); | 307 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop())); |
299 OnDetectedPhishingSite(verdict.SerializeAsString()); | 308 OnDetectedPhishingSite(verdict.SerializeAsString()); |
300 MessageLoop::current()->Run(); | 309 MessageLoop::current()->Run(); |
301 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 310 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
302 ASSERT_TRUE(cb); | 311 ASSERT_TRUE(cb); |
303 | 312 |
304 SafeBrowsingService::UnsafeResource resource; | 313 SafeBrowsingService::UnsafeResource resource; |
305 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) | 314 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) |
306 .WillOnce(SaveArg<0>(&resource)); | 315 .WillOnce(SaveArg<0>(&resource)); |
307 cb->Run(phishing_url, true); | 316 cb->Run(phishing_url, true); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
340 // server responds for both requests with a phishing verdict. Only | 349 // server responds for both requests with a phishing verdict. Only |
341 // a single interstitial is shown for the second URL. | 350 // a single interstitial is shown for the second URL. |
342 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 351 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; |
343 GURL phishing_url("http://phishingurl.com/"); | 352 GURL phishing_url("http://phishingurl.com/"); |
344 ClientPhishingRequest verdict; | 353 ClientPhishingRequest verdict; |
345 verdict.set_url(phishing_url.spec()); | 354 verdict.set_url(phishing_url.spec()); |
346 verdict.set_client_score(1.0f); | 355 verdict.set_client_score(1.0f); |
347 verdict.set_is_phishing(true); | 356 verdict.set_is_phishing(true); |
348 | 357 |
349 EXPECT_CALL(*csd_service_, | 358 EXPECT_CALL(*csd_service_, |
350 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) | 359 SendClientReportPhishingRequest( |
360 Pointee(PartiallyEqualVerdict(verdict)), _)) | |
351 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop())); | 361 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop())); |
352 OnDetectedPhishingSite(verdict.SerializeAsString()); | 362 OnDetectedPhishingSite(verdict.SerializeAsString()); |
353 MessageLoop::current()->Run(); | 363 MessageLoop::current()->Run(); |
354 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 364 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
355 ASSERT_TRUE(cb); | 365 ASSERT_TRUE(cb); |
356 GURL other_phishing_url("http://other_phishing_url.com/bla"); | 366 GURL other_phishing_url("http://other_phishing_url.com/bla"); |
357 ExpectPreClassificationChecks(other_phishing_url, &kFalse, &kFalse, &kFalse, | 367 ExpectPreClassificationChecks(other_phishing_url, &kFalse, &kFalse, &kFalse, |
358 &kFalse, &kFalse, &kFalse); | 368 &kFalse, &kFalse, &kFalse); |
359 // We navigate away. The callback cb should be revoked. | 369 // We navigate away. The callback cb should be revoked. |
360 NavigateAndCommit(other_phishing_url); | 370 NavigateAndCommit(other_phishing_url); |
361 // Wait for the pre-classification checks to finish for other_phishing_url. | 371 // Wait for the pre-classification checks to finish for other_phishing_url. |
362 WaitAndCheckPreClassificationChecks(); | 372 WaitAndCheckPreClassificationChecks(); |
363 | 373 |
364 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb_other; | 374 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb_other; |
365 verdict.set_url(other_phishing_url.spec()); | 375 verdict.set_url(other_phishing_url.spec()); |
366 verdict.set_client_score(0.8f); | 376 verdict.set_client_score(0.8f); |
367 EXPECT_CALL(*csd_service_, | 377 EXPECT_CALL(*csd_service_, |
368 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) | 378 SendClientReportPhishingRequest( |
379 Pointee(PartiallyEqualVerdict(verdict)), _)) | |
369 .WillOnce(DoAll(DeleteArg<0>(), | 380 .WillOnce(DoAll(DeleteArg<0>(), |
370 SaveArg<1>(&cb_other), | 381 SaveArg<1>(&cb_other), |
371 QuitUIMessageLoop())); | 382 QuitUIMessageLoop())); |
372 OnDetectedPhishingSite(verdict.SerializeAsString()); | 383 OnDetectedPhishingSite(verdict.SerializeAsString()); |
373 MessageLoop::current()->Run(); | 384 MessageLoop::current()->Run(); |
374 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 385 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
375 ASSERT_TRUE(cb_other); | 386 ASSERT_TRUE(cb_other); |
376 | 387 |
377 // We expect that the interstitial is shown for the second phishing URL and | 388 // We expect that the interstitial is shown for the second phishing URL and |
378 // not for the first phishing URL. | 389 // not for the first phishing URL. |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
592 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 603 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
593 EXPECT_EQ(url, resource.url); | 604 EXPECT_EQ(url, resource.url); |
594 EXPECT_EQ(url, resource.original_url); | 605 EXPECT_EQ(url, resource.original_url); |
595 delete resource.client; | 606 delete resource.client; |
596 msg = process()->sink().GetFirstMessageMatching( | 607 msg = process()->sink().GetFirstMessageMatching( |
597 SafeBrowsingMsg_StartPhishingDetection::ID); | 608 SafeBrowsingMsg_StartPhishingDetection::ID); |
598 ASSERT_FALSE(msg); | 609 ASSERT_FALSE(msg); |
599 } | 610 } |
600 | 611 |
601 } // namespace safe_browsing | 612 } // namespace safe_browsing |
OLD | NEW |