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/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 Loading... |
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 Loading... |
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. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 void OnDetectedPhishingSite(const std::string& verdict_str) { | 148 void OnDetectedPhishingSite(const std::string& verdict_str) { |
144 csd_host_->OnDetectedPhishingSite(verdict_str); | 149 csd_host_->OnDetectedPhishingSite(verdict_str); |
145 } | 150 } |
146 | 151 |
147 void FlushIOMessageLoop() { | 152 void FlushIOMessageLoop() { |
148 // If there was a message posted on the IO thread to display the | 153 // 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 | 154 // interstitial page we know that it would have been posted before |
150 // we put the quit message there. | 155 // we put the quit message there. |
151 BrowserThread::PostTask(BrowserThread::IO, | 156 BrowserThread::PostTask(BrowserThread::IO, |
152 FROM_HERE, | 157 FROM_HERE, |
153 NewRunnableFunction(&QuitUIMessageLoop)); | 158 NewRunnableFunction(&QuitUIMessageLoopFromIO)); |
154 MessageLoop::current()->Run(); | 159 MessageLoop::current()->Run(); |
155 } | 160 } |
156 | 161 |
157 void ExpectPreClassificationChecks(const GURL& url, | 162 void ExpectPreClassificationChecks(const GURL& url, |
158 const bool* is_private, | 163 const bool* is_private, |
159 const bool* is_incognito, | 164 const bool* is_incognito, |
160 const bool* match_csd_whitelist, | 165 const bool* match_csd_whitelist, |
161 const bool* get_valid_cached_result, | 166 const bool* get_valid_cached_result, |
162 const bool* is_in_cache, | 167 const bool* is_in_cache, |
163 const bool* over_report_limit) { | 168 const bool* over_report_limit) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 // Case 1: client thinks the page is phishing. The server does not agree. | 225 // Case 1: client thinks the page is phishing. The server does not agree. |
221 // No interstitial is shown. | 226 // No interstitial is shown. |
222 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 227 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; |
223 ClientPhishingRequest verdict; | 228 ClientPhishingRequest verdict; |
224 verdict.set_url("http://phishingurl.com/"); | 229 verdict.set_url("http://phishingurl.com/"); |
225 verdict.set_client_score(1.0f); | 230 verdict.set_client_score(1.0f); |
226 verdict.set_is_phishing(true); | 231 verdict.set_is_phishing(true); |
227 | 232 |
228 EXPECT_CALL(*csd_service_, | 233 EXPECT_CALL(*csd_service_, |
229 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) | 234 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) |
230 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); | 235 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop())); |
231 OnDetectedPhishingSite(verdict.SerializeAsString()); | 236 OnDetectedPhishingSite(verdict.SerializeAsString()); |
| 237 MessageLoop::current()->Run(); |
232 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 238 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
233 ASSERT_TRUE(cb); | 239 ASSERT_TRUE(cb); |
234 | 240 |
235 // Make sure DoDisplayBlockingPage is not going to be called. | 241 // Make sure DoDisplayBlockingPage is not going to be called. |
236 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); | 242 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); |
237 cb->Run(GURL(verdict.url()), false); | 243 cb->Run(GURL(verdict.url()), false); |
238 delete cb; | 244 delete cb; |
239 MessageLoop::current()->RunAllPending(); | 245 MessageLoop::current()->RunAllPending(); |
240 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 246 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
241 } | 247 } |
242 | 248 |
243 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteDisabled) { | 249 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteDisabled) { |
244 // Case 2: client thinks the page is phishing and so does the server but | 250 // Case 2: client thinks the page is phishing and so does the server but |
245 // showing the interstitial is disabled => no interstitial is shown. | 251 // showing the interstitial is disabled => no interstitial is shown. |
246 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 252 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; |
247 ClientPhishingRequest verdict; | 253 ClientPhishingRequest verdict; |
248 verdict.set_url("http://phishingurl.com/"); | 254 verdict.set_url("http://phishingurl.com/"); |
249 verdict.set_client_score(1.0f); | 255 verdict.set_client_score(1.0f); |
250 verdict.set_is_phishing(true); | 256 verdict.set_is_phishing(true); |
251 | 257 |
252 EXPECT_CALL(*csd_service_, | 258 EXPECT_CALL(*csd_service_, |
253 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) | 259 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) |
254 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); | 260 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop())); |
255 OnDetectedPhishingSite(verdict.SerializeAsString()); | 261 OnDetectedPhishingSite(verdict.SerializeAsString()); |
| 262 MessageLoop::current()->Run(); |
256 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 263 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
257 ASSERT_TRUE(cb); | 264 ASSERT_TRUE(cb); |
258 | 265 |
259 // Make sure DoDisplayBlockingPage is not going to be called. | 266 // Make sure DoDisplayBlockingPage is not going to be called. |
260 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); | 267 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); |
261 cb->Run(GURL(verdict.url()), false); | 268 cb->Run(GURL(verdict.url()), false); |
262 delete cb; | 269 delete cb; |
263 MessageLoop::current()->RunAllPending(); | 270 MessageLoop::current()->RunAllPending(); |
264 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 271 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
265 } | 272 } |
266 | 273 |
267 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteShowInterstitial) { | 274 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteShowInterstitial) { |
268 // Case 3: client thinks the page is phishing and so does the server. | 275 // Case 3: client thinks the page is phishing and so does the server. |
269 // We show an interstitial. | 276 // We show an interstitial. |
270 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 277 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; |
271 GURL phishing_url("http://phishingurl.com/"); | 278 GURL phishing_url("http://phishingurl.com/"); |
272 ClientPhishingRequest verdict; | 279 ClientPhishingRequest verdict; |
273 verdict.set_url(phishing_url.spec()); | 280 verdict.set_url(phishing_url.spec()); |
274 verdict.set_client_score(1.0f); | 281 verdict.set_client_score(1.0f); |
275 verdict.set_is_phishing(true); | 282 verdict.set_is_phishing(true); |
276 | 283 |
277 EXPECT_CALL(*csd_service_, | 284 EXPECT_CALL(*csd_service_, |
278 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) | 285 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) |
279 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); | 286 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop())); |
280 OnDetectedPhishingSite(verdict.SerializeAsString()); | 287 OnDetectedPhishingSite(verdict.SerializeAsString()); |
| 288 MessageLoop::current()->Run(); |
281 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 289 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
282 ASSERT_TRUE(cb); | 290 ASSERT_TRUE(cb); |
283 | 291 |
284 SafeBrowsingService::UnsafeResource resource; | 292 SafeBrowsingService::UnsafeResource resource; |
285 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) | 293 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) |
286 .WillOnce(SaveArg<0>(&resource)); | 294 .WillOnce(SaveArg<0>(&resource)); |
287 cb->Run(phishing_url, true); | 295 cb->Run(phishing_url, true); |
288 delete cb; | 296 delete cb; |
289 | 297 |
290 MessageLoop::current()->RunAllPending(); | 298 MessageLoop::current()->RunAllPending(); |
(...skipping 30 matching lines...) Expand all Loading... |
321 // a single interstitial is shown for the second URL. | 329 // a single interstitial is shown for the second URL. |
322 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 330 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; |
323 GURL phishing_url("http://phishingurl.com/"); | 331 GURL phishing_url("http://phishingurl.com/"); |
324 ClientPhishingRequest verdict; | 332 ClientPhishingRequest verdict; |
325 verdict.set_url(phishing_url.spec()); | 333 verdict.set_url(phishing_url.spec()); |
326 verdict.set_client_score(1.0f); | 334 verdict.set_client_score(1.0f); |
327 verdict.set_is_phishing(true); | 335 verdict.set_is_phishing(true); |
328 | 336 |
329 EXPECT_CALL(*csd_service_, | 337 EXPECT_CALL(*csd_service_, |
330 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) | 338 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) |
331 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); | 339 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop())); |
332 OnDetectedPhishingSite(verdict.SerializeAsString()); | 340 OnDetectedPhishingSite(verdict.SerializeAsString()); |
| 341 MessageLoop::current()->Run(); |
333 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 342 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
334 ASSERT_TRUE(cb); | 343 ASSERT_TRUE(cb); |
335 GURL other_phishing_url("http://other_phishing_url.com/bla"); | 344 GURL other_phishing_url("http://other_phishing_url.com/bla"); |
336 ExpectPreClassificationChecks(other_phishing_url, &kFalse, &kFalse, &kFalse, | 345 ExpectPreClassificationChecks(other_phishing_url, &kFalse, &kFalse, &kFalse, |
337 &kFalse, &kFalse, &kFalse); | 346 &kFalse, &kFalse, &kFalse); |
338 // We navigate away. The callback cb should be revoked. | 347 // We navigate away. The callback cb should be revoked. |
339 NavigateAndCommit(other_phishing_url); | 348 NavigateAndCommit(other_phishing_url); |
340 // Wait for the pre-classification checks to finish for other_phishing_url. | 349 // Wait for the pre-classification checks to finish for other_phishing_url. |
341 WaitAndCheckPreClassificationChecks(); | 350 WaitAndCheckPreClassificationChecks(); |
342 | 351 |
343 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb_other; | 352 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb_other; |
344 verdict.set_url(other_phishing_url.spec()); | 353 verdict.set_url(other_phishing_url.spec()); |
345 verdict.set_client_score(0.8f); | 354 verdict.set_client_score(0.8f); |
346 EXPECT_CALL(*csd_service_, | 355 EXPECT_CALL(*csd_service_, |
347 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) | 356 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) |
348 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb_other))); | 357 .WillOnce(DoAll(DeleteArg<0>(), |
| 358 SaveArg<1>(&cb_other), |
| 359 QuitUIMessageLoop())); |
349 OnDetectedPhishingSite(verdict.SerializeAsString()); | 360 OnDetectedPhishingSite(verdict.SerializeAsString()); |
| 361 MessageLoop::current()->Run(); |
350 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 362 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
351 ASSERT_TRUE(cb_other); | 363 ASSERT_TRUE(cb_other); |
352 | 364 |
353 // We expect that the interstitial is shown for the second phishing URL and | 365 // We expect that the interstitial is shown for the second phishing URL and |
354 // not for the first phishing URL. | 366 // not for the first phishing URL. |
355 SafeBrowsingService::UnsafeResource resource; | 367 SafeBrowsingService::UnsafeResource resource; |
356 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) | 368 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) |
357 .WillOnce(SaveArg<0>(&resource)); | 369 .WillOnce(SaveArg<0>(&resource)); |
358 | 370 |
359 cb->Run(phishing_url, true); // Should have no effect. | 371 cb->Run(phishing_url, true); // Should have no effect. |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 580 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
569 EXPECT_EQ(url, resource.url); | 581 EXPECT_EQ(url, resource.url); |
570 EXPECT_EQ(url, resource.original_url); | 582 EXPECT_EQ(url, resource.original_url); |
571 delete resource.client; | 583 delete resource.client; |
572 msg = process()->sink().GetFirstMessageMatching( | 584 msg = process()->sink().GetFirstMessageMatching( |
573 SafeBrowsingMsg_StartPhishingDetection::ID); | 585 SafeBrowsingMsg_StartPhishingDetection::ID); |
574 ASSERT_FALSE(msg); | 586 ASSERT_FALSE(msg); |
575 } | 587 } |
576 | 588 |
577 } // namespace safe_browsing | 589 } // namespace safe_browsing |
OLD | NEW |