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 |
113 | |
Paweł Hajdan Jr.
2011/06/09 09:13:10
nit: Why so many empty lines?
noelutz
2011/06/09 19:21:39
Removed.
| |
114 | |
115 | |
108 class ClientSideDetectionHostTest : public TabContentsWrapperTestHarness { | 116 class ClientSideDetectionHostTest : public TabContentsWrapperTestHarness { |
109 public: | 117 public: |
110 virtual void SetUp() { | 118 virtual void SetUp() { |
111 // Set custom profile object so that we can mock calls to IsOffTheRecord. | 119 // Set custom profile object so that we can mock calls to IsOffTheRecord. |
112 // This needs to happen before we call the parent SetUp() function. We use | 120 // This needs to happen before we call the parent SetUp() function. We use |
113 // a nice mock because other parts of the code are calling IsOffTheRecord. | 121 // a nice mock because other parts of the code are calling IsOffTheRecord. |
114 mock_profile_ = new NiceMock<MockTestingProfile>(); | 122 mock_profile_ = new NiceMock<MockTestingProfile>(); |
115 profile_.reset(mock_profile_); | 123 profile_.reset(mock_profile_); |
116 | 124 |
117 TabContentsWrapperTestHarness::SetUp(); | 125 TabContentsWrapperTestHarness::SetUp(); |
(...skipping 25 matching lines...) Expand all Loading... | |
143 void OnDetectedPhishingSite(const std::string& verdict_str) { | 151 void OnDetectedPhishingSite(const std::string& verdict_str) { |
144 csd_host_->OnDetectedPhishingSite(verdict_str); | 152 csd_host_->OnDetectedPhishingSite(verdict_str); |
145 } | 153 } |
146 | 154 |
147 void FlushIOMessageLoop() { | 155 void FlushIOMessageLoop() { |
148 // If there was a message posted on the IO thread to display the | 156 // 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 | 157 // interstitial page we know that it would have been posted before |
150 // we put the quit message there. | 158 // we put the quit message there. |
151 BrowserThread::PostTask(BrowserThread::IO, | 159 BrowserThread::PostTask(BrowserThread::IO, |
152 FROM_HERE, | 160 FROM_HERE, |
153 NewRunnableFunction(&QuitUIMessageLoop)); | 161 NewRunnableFunction(&QuitUIMessageLoopFromIO)); |
154 MessageLoop::current()->Run(); | 162 MessageLoop::current()->Run(); |
155 } | 163 } |
156 | 164 |
157 void ExpectPreClassificationChecks(const GURL& url, | 165 void ExpectPreClassificationChecks(const GURL& url, |
158 const bool* is_private, | 166 const bool* is_private, |
159 const bool* is_incognito, | 167 const bool* is_incognito, |
160 const bool* match_csd_whitelist, | 168 const bool* match_csd_whitelist, |
161 const bool* get_valid_cached_result, | 169 const bool* get_valid_cached_result, |
162 const bool* is_in_cache, | 170 const bool* is_in_cache, |
163 const bool* over_report_limit) { | 171 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. | 228 // Case 1: client thinks the page is phishing. The server does not agree. |
221 // No interstitial is shown. | 229 // No interstitial is shown. |
222 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 230 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; |
223 ClientPhishingRequest verdict; | 231 ClientPhishingRequest verdict; |
224 verdict.set_url("http://phishingurl.com/"); | 232 verdict.set_url("http://phishingurl.com/"); |
225 verdict.set_client_score(1.0f); | 233 verdict.set_client_score(1.0f); |
226 verdict.set_is_phishing(true); | 234 verdict.set_is_phishing(true); |
227 | 235 |
228 EXPECT_CALL(*csd_service_, | 236 EXPECT_CALL(*csd_service_, |
229 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) | 237 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) |
230 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); | 238 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop())); |
231 OnDetectedPhishingSite(verdict.SerializeAsString()); | 239 OnDetectedPhishingSite(verdict.SerializeAsString()); |
240 MessageLoop::current()->Run(); | |
232 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 241 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
233 ASSERT_TRUE(cb); | 242 ASSERT_TRUE(cb); |
234 | 243 |
235 // Make sure DoDisplayBlockingPage is not going to be called. | 244 // Make sure DoDisplayBlockingPage is not going to be called. |
236 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); | 245 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); |
237 cb->Run(GURL(verdict.url()), false); | 246 cb->Run(GURL(verdict.url()), false); |
238 delete cb; | 247 delete cb; |
239 MessageLoop::current()->RunAllPending(); | 248 MessageLoop::current()->RunAllPending(); |
240 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 249 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
241 } | 250 } |
242 | 251 |
243 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteDisabled) { | 252 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteDisabled) { |
244 // Case 2: client thinks the page is phishing and so does the server but | 253 // Case 2: client thinks the page is phishing and so does the server but |
245 // showing the interstitial is disabled => no interstitial is shown. | 254 // showing the interstitial is disabled => no interstitial is shown. |
246 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 255 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; |
247 ClientPhishingRequest verdict; | 256 ClientPhishingRequest verdict; |
248 verdict.set_url("http://phishingurl.com/"); | 257 verdict.set_url("http://phishingurl.com/"); |
249 verdict.set_client_score(1.0f); | 258 verdict.set_client_score(1.0f); |
250 verdict.set_is_phishing(true); | 259 verdict.set_is_phishing(true); |
251 | 260 |
252 EXPECT_CALL(*csd_service_, | 261 EXPECT_CALL(*csd_service_, |
253 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) | 262 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) |
254 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); | 263 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop())); |
255 OnDetectedPhishingSite(verdict.SerializeAsString()); | 264 OnDetectedPhishingSite(verdict.SerializeAsString()); |
265 MessageLoop::current()->Run(); | |
256 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 266 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
257 ASSERT_TRUE(cb); | 267 ASSERT_TRUE(cb); |
258 | 268 |
259 // Make sure DoDisplayBlockingPage is not going to be called. | 269 // Make sure DoDisplayBlockingPage is not going to be called. |
260 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); | 270 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)).Times(0); |
261 cb->Run(GURL(verdict.url()), false); | 271 cb->Run(GURL(verdict.url()), false); |
262 delete cb; | 272 delete cb; |
263 MessageLoop::current()->RunAllPending(); | 273 MessageLoop::current()->RunAllPending(); |
264 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 274 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
265 } | 275 } |
266 | 276 |
267 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteShowInterstitial) { | 277 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSiteShowInterstitial) { |
268 // Case 3: client thinks the page is phishing and so does the server. | 278 // Case 3: client thinks the page is phishing and so does the server. |
269 // We show an interstitial. | 279 // We show an interstitial. |
270 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 280 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; |
271 GURL phishing_url("http://phishingurl.com/"); | 281 GURL phishing_url("http://phishingurl.com/"); |
272 ClientPhishingRequest verdict; | 282 ClientPhishingRequest verdict; |
273 verdict.set_url(phishing_url.spec()); | 283 verdict.set_url(phishing_url.spec()); |
274 verdict.set_client_score(1.0f); | 284 verdict.set_client_score(1.0f); |
275 verdict.set_is_phishing(true); | 285 verdict.set_is_phishing(true); |
276 | 286 |
277 EXPECT_CALL(*csd_service_, | 287 EXPECT_CALL(*csd_service_, |
278 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) | 288 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) |
279 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); | 289 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop())); |
280 OnDetectedPhishingSite(verdict.SerializeAsString()); | 290 OnDetectedPhishingSite(verdict.SerializeAsString()); |
291 MessageLoop::current()->Run(); | |
281 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 292 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
282 ASSERT_TRUE(cb); | 293 ASSERT_TRUE(cb); |
283 | 294 |
284 SafeBrowsingService::UnsafeResource resource; | 295 SafeBrowsingService::UnsafeResource resource; |
285 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) | 296 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) |
286 .WillOnce(SaveArg<0>(&resource)); | 297 .WillOnce(SaveArg<0>(&resource)); |
287 cb->Run(phishing_url, true); | 298 cb->Run(phishing_url, true); |
288 delete cb; | 299 delete cb; |
289 | 300 |
290 MessageLoop::current()->RunAllPending(); | 301 MessageLoop::current()->RunAllPending(); |
(...skipping 30 matching lines...) Expand all Loading... | |
321 // a single interstitial is shown for the second URL. | 332 // a single interstitial is shown for the second URL. |
322 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | 333 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; |
323 GURL phishing_url("http://phishingurl.com/"); | 334 GURL phishing_url("http://phishingurl.com/"); |
324 ClientPhishingRequest verdict; | 335 ClientPhishingRequest verdict; |
325 verdict.set_url(phishing_url.spec()); | 336 verdict.set_url(phishing_url.spec()); |
326 verdict.set_client_score(1.0f); | 337 verdict.set_client_score(1.0f); |
327 verdict.set_is_phishing(true); | 338 verdict.set_is_phishing(true); |
328 | 339 |
329 EXPECT_CALL(*csd_service_, | 340 EXPECT_CALL(*csd_service_, |
330 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) | 341 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) |
331 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb))); | 342 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb), QuitUIMessageLoop())); |
332 OnDetectedPhishingSite(verdict.SerializeAsString()); | 343 OnDetectedPhishingSite(verdict.SerializeAsString()); |
344 MessageLoop::current()->Run(); | |
333 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 345 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
334 ASSERT_TRUE(cb); | 346 ASSERT_TRUE(cb); |
335 GURL other_phishing_url("http://other_phishing_url.com/bla"); | 347 GURL other_phishing_url("http://other_phishing_url.com/bla"); |
336 ExpectPreClassificationChecks(other_phishing_url, &kFalse, &kFalse, &kFalse, | 348 ExpectPreClassificationChecks(other_phishing_url, &kFalse, &kFalse, &kFalse, |
337 &kFalse, &kFalse, &kFalse); | 349 &kFalse, &kFalse, &kFalse); |
338 // We navigate away. The callback cb should be revoked. | 350 // We navigate away. The callback cb should be revoked. |
339 NavigateAndCommit(other_phishing_url); | 351 NavigateAndCommit(other_phishing_url); |
340 // Wait for the pre-classification checks to finish for other_phishing_url. | 352 // Wait for the pre-classification checks to finish for other_phishing_url. |
341 WaitAndCheckPreClassificationChecks(); | 353 WaitAndCheckPreClassificationChecks(); |
342 | 354 |
343 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb_other; | 355 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb_other; |
344 verdict.set_url(other_phishing_url.spec()); | 356 verdict.set_url(other_phishing_url.spec()); |
345 verdict.set_client_score(0.8f); | 357 verdict.set_client_score(0.8f); |
346 EXPECT_CALL(*csd_service_, | 358 EXPECT_CALL(*csd_service_, |
347 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) | 359 SendClientReportPhishingRequest(Pointee(EqualsProto(verdict)), _)) |
348 .WillOnce(DoAll(DeleteArg<0>(), SaveArg<1>(&cb_other))); | 360 .WillOnce(DoAll(DeleteArg<0>(), |
361 SaveArg<1>(&cb_other), | |
362 QuitUIMessageLoop())); | |
349 OnDetectedPhishingSite(verdict.SerializeAsString()); | 363 OnDetectedPhishingSite(verdict.SerializeAsString()); |
364 MessageLoop::current()->Run(); | |
350 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | 365 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); |
351 ASSERT_TRUE(cb_other); | 366 ASSERT_TRUE(cb_other); |
352 | 367 |
353 // We expect that the interstitial is shown for the second phishing URL and | 368 // We expect that the interstitial is shown for the second phishing URL and |
354 // not for the first phishing URL. | 369 // not for the first phishing URL. |
355 SafeBrowsingService::UnsafeResource resource; | 370 SafeBrowsingService::UnsafeResource resource; |
356 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) | 371 EXPECT_CALL(*sb_service_, DoDisplayBlockingPage(_)) |
357 .WillOnce(SaveArg<0>(&resource)); | 372 .WillOnce(SaveArg<0>(&resource)); |
358 | 373 |
359 cb->Run(phishing_url, true); // Should have no effect. | 374 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())); | 583 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
569 EXPECT_EQ(url, resource.url); | 584 EXPECT_EQ(url, resource.url); |
570 EXPECT_EQ(url, resource.original_url); | 585 EXPECT_EQ(url, resource.original_url); |
571 delete resource.client; | 586 delete resource.client; |
572 msg = process()->sink().GetFirstMessageMatching( | 587 msg = process()->sink().GetFirstMessageMatching( |
573 SafeBrowsingMsg_StartPhishingDetection::ID); | 588 SafeBrowsingMsg_StartPhishingDetection::ID); |
574 ASSERT_FALSE(msg); | 589 ASSERT_FALSE(msg); |
575 } | 590 } |
576 | 591 |
577 } // namespace safe_browsing | 592 } // namespace safe_browsing |
OLD | NEW |