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