Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: chrome/browser/safe_browsing/client_side_detection_service_unittest.cc

Issue 7583007: Add "enabled" state to the ClientSideDetectionService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: nit: removed unused code Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <map> 5 #include <map>
6 #include <queue> 6 #include <queue>
7 #include <string> 7 #include <string>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop.h" 12 #include "base/message_loop.h"
13 #include "base/task.h" 13 #include "base/task.h"
14 #include "base/time.h" 14 #include "base/time.h"
15 #include "chrome/browser/safe_browsing/client_side_detection_service.h" 15 #include "chrome/browser/safe_browsing/client_side_detection_service.h"
16 #include "chrome/common/safe_browsing/client_model.pb.h" 16 #include "chrome/common/safe_browsing/client_model.pb.h"
17 #include "chrome/common/safe_browsing/csd.pb.h" 17 #include "chrome/common/safe_browsing/csd.pb.h"
18 #include "chrome/test/testing_browser_process_test.h" 18 #include "chrome/test/testing_browser_process_test.h"
19 #include "content/browser/browser_thread.h" 19 #include "content/browser/browser_thread.h"
20 #include "content/common/url_fetcher.h" 20 #include "content/common/url_fetcher.h"
21 #include "content/test/test_url_fetcher_factory.h" 21 #include "content/test/test_url_fetcher_factory.h"
22 #include "crypto/sha2.h" 22 #include "crypto/sha2.h"
23 #include "googleurl/src/gurl.h" 23 #include "googleurl/src/gurl.h"
24 #include "net/url_request/url_request_status.h" 24 #include "net/url_request/url_request_status.h"
25 #include "testing/gmock/include/gmock/gmock.h" 25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
27 27
28 using ::testing::_;
29 using ::testing::Invoke;
28 using ::testing::Mock; 30 using ::testing::Mock;
29 31
30 namespace safe_browsing { 32 namespace safe_browsing {
31 namespace { 33 namespace {
32 class MockClientSideDetectionService : public ClientSideDetectionService { 34 class MockClientSideDetectionService : public ClientSideDetectionService {
33 public: 35 public:
34 MockClientSideDetectionService() : ClientSideDetectionService(NULL) {} 36 MockClientSideDetectionService() : ClientSideDetectionService(NULL) {}
35 virtual ~MockClientSideDetectionService() {} 37 virtual ~MockClientSideDetectionService() {}
36 38
37 MOCK_METHOD1(EndFetchModel, void(ClientModelStatus)); 39 MOCK_METHOD1(EndFetchModel, void(ClientModelStatus));
40 MOCK_METHOD1(ScheduleFetchModel, void(int64));
41
42 void Schedule(int64) {
43 // Ignore the delay when testing.
44 StartFetchModel();
45 }
46
47 void Disable(int) {
48 // Ignore the status.
49 SetEnabled(false);
50 }
38 51
39 private: 52 private:
40 DISALLOW_COPY_AND_ASSIGN(MockClientSideDetectionService); 53 DISALLOW_COPY_AND_ASSIGN(MockClientSideDetectionService);
41 }; 54 };
42 55
43 ACTION(QuitCurrentMessageLoop) { 56 ACTION(QuitCurrentMessageLoop) {
44 MessageLoop::current()->Quit(); 57 MessageLoop::current()->Quit();
45 } 58 }
59
46 } // namespace 60 } // namespace
47 61
48 class ClientSideDetectionServiceTest : public TestingBrowserProcessTest { 62 class ClientSideDetectionServiceTest : public TestingBrowserProcessTest {
49 protected: 63 protected:
50 virtual void SetUp() { 64 virtual void SetUp() {
51 file_thread_.reset(new BrowserThread(BrowserThread::FILE, &msg_loop_)); 65 file_thread_.reset(new BrowserThread(BrowserThread::FILE, &msg_loop_));
52 66
53 factory_.reset(new FakeURLFetcherFactory()); 67 factory_.reset(new FakeURLFetcherFactory());
54 68
55 browser_thread_.reset(new BrowserThread(BrowserThread::UI, &msg_loop_)); 69 browser_thread_.reset(new BrowserThread(BrowserThread::UI, &msg_loop_));
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 179
166 GURL phishing_url_; 180 GURL phishing_url_;
167 bool is_phishing_; 181 bool is_phishing_;
168 }; 182 };
169 183
170 TEST_F(ClientSideDetectionServiceTest, FetchModelTest) { 184 TEST_F(ClientSideDetectionServiceTest, FetchModelTest) {
171 // We don't want to use a real service class here because we can't call 185 // We don't want to use a real service class here because we can't call
172 // the real EndFetchModel. It would reschedule a reload which might 186 // the real EndFetchModel. It would reschedule a reload which might
173 // make the test flaky. 187 // make the test flaky.
174 MockClientSideDetectionService service; 188 MockClientSideDetectionService service;
189 EXPECT_CALL(service, ScheduleFetchModel(_)).Times(1);
190 service.SetEnabled(true);
175 191
176 // The model fetch failed. 192 // The model fetch failed.
177 SetModelFetchResponse("blamodel", false /* failure */); 193 SetModelFetchResponse("blamodel", false /* failure */);
178 EXPECT_CALL(service, EndFetchModel( 194 EXPECT_CALL(service, EndFetchModel(
179 ClientSideDetectionService::MODEL_FETCH_FAILED)) 195 ClientSideDetectionService::MODEL_FETCH_FAILED))
180 .WillOnce(QuitCurrentMessageLoop()); 196 .WillOnce(QuitCurrentMessageLoop());
181 service.StartFetchModel(); 197 service.StartFetchModel();
182 msg_loop_.Run(); // EndFetchModel will quit the message loop. 198 msg_loop_.Run(); // EndFetchModel will quit the message loop.
183 Mock::VerifyAndClearExpectations(&service); 199 Mock::VerifyAndClearExpectations(&service);
184 200
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 ClientSideDetectionService::MODEL_NOT_CHANGED)) 290 ClientSideDetectionService::MODEL_NOT_CHANGED))
275 .WillOnce(QuitCurrentMessageLoop()); 291 .WillOnce(QuitCurrentMessageLoop());
276 service.StartFetchModel(); 292 service.StartFetchModel();
277 msg_loop_.Run(); // EndFetchModel will quit the message loop. 293 msg_loop_.Run(); // EndFetchModel will quit the message loop.
278 Mock::VerifyAndClearExpectations(&service); 294 Mock::VerifyAndClearExpectations(&service);
279 } 295 }
280 296
281 TEST_F(ClientSideDetectionServiceTest, ServiceObjectDeletedBeforeCallbackDone) { 297 TEST_F(ClientSideDetectionServiceTest, ServiceObjectDeletedBeforeCallbackDone) {
282 SetModelFetchResponse("bogus model", true /* success */); 298 SetModelFetchResponse("bogus model", true /* success */);
283 csd_service_.reset(ClientSideDetectionService::Create(NULL)); 299 csd_service_.reset(ClientSideDetectionService::Create(NULL));
300 csd_service_->SetEnabled(true);
284 EXPECT_TRUE(csd_service_.get() != NULL); 301 EXPECT_TRUE(csd_service_.get() != NULL);
285 // We delete the client-side detection service class even though the callbacks 302 // We delete the client-side detection service class even though the callbacks
286 // haven't run yet. 303 // haven't run yet.
287 csd_service_.reset(); 304 csd_service_.reset();
288 // Waiting for the callbacks to run should not crash even if the service 305 // Waiting for the callbacks to run should not crash even if the service
289 // object is gone. 306 // object is gone.
290 msg_loop_.RunAllPending(); 307 msg_loop_.RunAllPending();
291 } 308 }
292 309
293 TEST_F(ClientSideDetectionServiceTest, SendClientReportPhishingRequest) { 310 TEST_F(ClientSideDetectionServiceTest, SendClientReportPhishingRequest) {
294 SetModelFetchResponse("bogus model", true /* success */); 311 SetModelFetchResponse("bogus model", true /* success */);
295 csd_service_.reset(ClientSideDetectionService::Create(NULL)); 312 csd_service_.reset(ClientSideDetectionService::Create(NULL));
313 csd_service_->SetEnabled(true);
296 314
297 GURL url("http://a.com/"); 315 GURL url("http://a.com/");
298 float score = 0.4f; // Some random client score. 316 float score = 0.4f; // Some random client score.
299 317
300 base::Time before = base::Time::Now(); 318 base::Time before = base::Time::Now();
301 319
302 // Invalid response body from the server. 320 // Invalid response body from the server.
303 SetClientReportPhishingResponse("invalid proto response", true /* success */); 321 SetClientReportPhishingResponse("invalid proto response", true /* success */);
304 EXPECT_FALSE(SendClientReportPhishingRequest(url, score)); 322 EXPECT_FALSE(SendClientReportPhishingRequest(url, score));
305 323
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 rule->add_feature(-1); 570 rule->add_feature(-1);
553 rule->set_weight(0.2f); 571 rule->set_weight(0.2f);
554 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model)); 572 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model));
555 573
556 rule->set_feature(2, 2); 574 rule->set_feature(2, 2);
557 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model)); 575 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model));
558 576
559 rule->set_feature(2, 1); 577 rule->set_feature(2, 1);
560 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model)); 578 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model));
561 } 579 }
580
581 TEST_F(ClientSideDetectionServiceTest, SetEnabled) {
582 // Check that the model isn't downloaded until the service is enabled.
583 csd_service_.reset(ClientSideDetectionService::Create(NULL));
584 EXPECT_FALSE(csd_service_->enabled());
585 EXPECT_TRUE(csd_service_->model_fetcher_.get() == NULL);
586
587 // Use a MockClientSideDetectionService for the rest of the test, to avoid
588 // the scheduling delay.
589 MockClientSideDetectionService* service = new MockClientSideDetectionService;
mattm 2011/08/08 21:59:46 Need to use StrictMock for the checks of unexpecte
Joao da Silva 2011/08/09 09:16:12 Done.
590 csd_service_.reset(service);
591 EXPECT_FALSE(csd_service_->enabled());
592 EXPECT_TRUE(csd_service_->model_fetcher_.get() == NULL);
593
mattm 2011/08/08 21:59:46 Should do a Mock::VerifyAndClearExpectations(servi
Joao da Silva 2011/08/09 09:16:12 Done.
594 ClientSideModel model;
595 model.set_version(10);
596 model.set_max_words_per_term(4);
597 SetModelFetchResponse(model.SerializeAsString(), true /* success */);
598 EXPECT_CALL(*service, ScheduleFetchModel(_))
599 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule));
600 EXPECT_CALL(*service, EndFetchModel(
601 ClientSideDetectionService::MODEL_SUCCESS))
602 .WillOnce(QuitCurrentMessageLoop());
603 csd_service_->SetEnabled(true);
604 EXPECT_TRUE(csd_service_->model_fetcher_.get() != NULL);
605 msg_loop_.Run(); // EndFetchModel will quit the message loop.
606 Mock::VerifyAndClearExpectations(service);
607
608 // Check that enabling again doesn't request the model.
609 csd_service_->SetEnabled(true);
610 // No calls expected.
611 Mock::VerifyAndClearExpectations(service);
612
613 // Check that disabling the service cancels pending requests.
614 EXPECT_CALL(*service, ScheduleFetchModel(_))
615 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule));
616 csd_service_->SetEnabled(false);
617 csd_service_->SetEnabled(true);
618 Mock::VerifyAndClearExpectations(service);
619 EXPECT_TRUE(csd_service_->model_fetcher_.get() != NULL);
620 csd_service_->SetEnabled(false);
621 EXPECT_TRUE(csd_service_->model_fetcher_.get() == NULL);
622 msg_loop_.RunAllPending();
623 // No calls expected.
624 Mock::VerifyAndClearExpectations(service);
625
626 // Requests always return false when the service is disabled.
627 ClientPhishingResponse response;
628 response.set_phishy(true);
629 SetClientReportPhishingResponse(response.SerializeAsString(),
630 true /* success */);
631 EXPECT_FALSE(SendClientReportPhishingRequest(GURL("http://a.com/"), 0.4f));
632
633 // Pending requests also return false if the service is disabled before they
634 // report back.
635 EXPECT_CALL(*service, ScheduleFetchModel(_))
636 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule));
637 EXPECT_CALL(*service, EndFetchModel(
638 ClientSideDetectionService::MODEL_NOT_CHANGED))
639 .WillOnce(Invoke(service, &MockClientSideDetectionService::Disable));
640 csd_service_->SetEnabled(true);
641 EXPECT_FALSE(SendClientReportPhishingRequest(GURL("http://a.com/"), 0.4f));
642 Mock::VerifyAndClearExpectations(service);
643 }
644
562 } // namespace safe_browsing 645 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698