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

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: Rebased 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/base/testing_browser_process_test.h" 18 #include "chrome/test/base/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;
31 using ::testing::StrictMock;
29 32
30 namespace safe_browsing { 33 namespace safe_browsing {
31 namespace { 34 namespace {
32 class MockClientSideDetectionService : public ClientSideDetectionService { 35 class MockClientSideDetectionService : public ClientSideDetectionService {
33 public: 36 public:
34 MockClientSideDetectionService() : ClientSideDetectionService(NULL) {} 37 MockClientSideDetectionService() : ClientSideDetectionService(NULL) {}
35 virtual ~MockClientSideDetectionService() {} 38 virtual ~MockClientSideDetectionService() {}
36 39
37 MOCK_METHOD1(EndFetchModel, void(ClientModelStatus)); 40 MOCK_METHOD1(EndFetchModel, void(ClientModelStatus));
41 MOCK_METHOD1(ScheduleFetchModel, void(int64));
42
43 void Schedule(int64) {
44 // Ignore the delay when testing.
45 StartFetchModel();
46 }
47
48 void Disable(int) {
49 // Ignore the status.
50 SetEnabled(false);
51 }
38 52
39 private: 53 private:
40 DISALLOW_COPY_AND_ASSIGN(MockClientSideDetectionService); 54 DISALLOW_COPY_AND_ASSIGN(MockClientSideDetectionService);
41 }; 55 };
42 56
43 ACTION(QuitCurrentMessageLoop) { 57 ACTION(QuitCurrentMessageLoop) {
44 MessageLoop::current()->Quit(); 58 MessageLoop::current()->Quit();
45 } 59 }
60
46 } // namespace 61 } // namespace
47 62
48 class ClientSideDetectionServiceTest : public TestingBrowserProcessTest { 63 class ClientSideDetectionServiceTest : public TestingBrowserProcessTest {
49 protected: 64 protected:
50 virtual void SetUp() { 65 virtual void SetUp() {
51 file_thread_.reset(new BrowserThread(BrowserThread::FILE, &msg_loop_)); 66 file_thread_.reset(new BrowserThread(BrowserThread::FILE, &msg_loop_));
52 67
53 factory_.reset(new FakeURLFetcherFactory()); 68 factory_.reset(new FakeURLFetcherFactory());
54 69
55 browser_thread_.reset(new BrowserThread(BrowserThread::UI, &msg_loop_)); 70 browser_thread_.reset(new BrowserThread(BrowserThread::UI, &msg_loop_));
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 180
166 GURL phishing_url_; 181 GURL phishing_url_;
167 bool is_phishing_; 182 bool is_phishing_;
168 }; 183 };
169 184
170 TEST_F(ClientSideDetectionServiceTest, FetchModelTest) { 185 TEST_F(ClientSideDetectionServiceTest, FetchModelTest) {
171 // We don't want to use a real service class here because we can't call 186 // 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 187 // the real EndFetchModel. It would reschedule a reload which might
173 // make the test flaky. 188 // make the test flaky.
174 MockClientSideDetectionService service; 189 MockClientSideDetectionService service;
190 EXPECT_CALL(service, ScheduleFetchModel(_)).Times(1);
191 service.SetEnabled(true);
175 192
176 // The model fetch failed. 193 // The model fetch failed.
177 SetModelFetchResponse("blamodel", false /* failure */); 194 SetModelFetchResponse("blamodel", false /* failure */);
178 EXPECT_CALL(service, EndFetchModel( 195 EXPECT_CALL(service, EndFetchModel(
179 ClientSideDetectionService::MODEL_FETCH_FAILED)) 196 ClientSideDetectionService::MODEL_FETCH_FAILED))
180 .WillOnce(QuitCurrentMessageLoop()); 197 .WillOnce(QuitCurrentMessageLoop());
181 service.StartFetchModel(); 198 service.StartFetchModel();
182 msg_loop_.Run(); // EndFetchModel will quit the message loop. 199 msg_loop_.Run(); // EndFetchModel will quit the message loop.
183 Mock::VerifyAndClearExpectations(&service); 200 Mock::VerifyAndClearExpectations(&service);
184 201
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 ClientSideDetectionService::MODEL_NOT_CHANGED)) 291 ClientSideDetectionService::MODEL_NOT_CHANGED))
275 .WillOnce(QuitCurrentMessageLoop()); 292 .WillOnce(QuitCurrentMessageLoop());
276 service.StartFetchModel(); 293 service.StartFetchModel();
277 msg_loop_.Run(); // EndFetchModel will quit the message loop. 294 msg_loop_.Run(); // EndFetchModel will quit the message loop.
278 Mock::VerifyAndClearExpectations(&service); 295 Mock::VerifyAndClearExpectations(&service);
279 } 296 }
280 297
281 TEST_F(ClientSideDetectionServiceTest, ServiceObjectDeletedBeforeCallbackDone) { 298 TEST_F(ClientSideDetectionServiceTest, ServiceObjectDeletedBeforeCallbackDone) {
282 SetModelFetchResponse("bogus model", true /* success */); 299 SetModelFetchResponse("bogus model", true /* success */);
283 csd_service_.reset(ClientSideDetectionService::Create(NULL)); 300 csd_service_.reset(ClientSideDetectionService::Create(NULL));
301 csd_service_->SetEnabled(true);
284 EXPECT_TRUE(csd_service_.get() != NULL); 302 EXPECT_TRUE(csd_service_.get() != NULL);
285 // We delete the client-side detection service class even though the callbacks 303 // We delete the client-side detection service class even though the callbacks
286 // haven't run yet. 304 // haven't run yet.
287 csd_service_.reset(); 305 csd_service_.reset();
288 // Waiting for the callbacks to run should not crash even if the service 306 // Waiting for the callbacks to run should not crash even if the service
289 // object is gone. 307 // object is gone.
290 msg_loop_.RunAllPending(); 308 msg_loop_.RunAllPending();
291 } 309 }
292 310
293 TEST_F(ClientSideDetectionServiceTest, SendClientReportPhishingRequest) { 311 TEST_F(ClientSideDetectionServiceTest, SendClientReportPhishingRequest) {
294 SetModelFetchResponse("bogus model", true /* success */); 312 SetModelFetchResponse("bogus model", true /* success */);
295 csd_service_.reset(ClientSideDetectionService::Create(NULL)); 313 csd_service_.reset(ClientSideDetectionService::Create(NULL));
314 csd_service_->SetEnabled(true);
296 315
297 GURL url("http://a.com/"); 316 GURL url("http://a.com/");
298 float score = 0.4f; // Some random client score. 317 float score = 0.4f; // Some random client score.
299 318
300 base::Time before = base::Time::Now(); 319 base::Time before = base::Time::Now();
301 320
302 // Invalid response body from the server. 321 // Invalid response body from the server.
303 SetClientReportPhishingResponse("invalid proto response", true /* success */); 322 SetClientReportPhishingResponse("invalid proto response", true /* success */);
304 EXPECT_FALSE(SendClientReportPhishingRequest(url, score)); 323 EXPECT_FALSE(SendClientReportPhishingRequest(url, score));
305 324
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 rule->add_feature(-1); 571 rule->add_feature(-1);
553 rule->set_weight(0.2f); 572 rule->set_weight(0.2f);
554 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model)); 573 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model));
555 574
556 rule->set_feature(2, 2); 575 rule->set_feature(2, 2);
557 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model)); 576 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model));
558 577
559 rule->set_feature(2, 1); 578 rule->set_feature(2, 1);
560 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model)); 579 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model));
561 } 580 }
581
582 TEST_F(ClientSideDetectionServiceTest, SetEnabled) {
583 // Check that the model isn't downloaded until the service is enabled.
584 csd_service_.reset(ClientSideDetectionService::Create(NULL));
585 EXPECT_FALSE(csd_service_->enabled());
586 EXPECT_TRUE(csd_service_->model_fetcher_.get() == NULL);
587
588 // Use a MockClientSideDetectionService for the rest of the test, to avoid
589 // the scheduling delay.
590 MockClientSideDetectionService* service =
591 new StrictMock<MockClientSideDetectionService>();
592 csd_service_.reset(service);
593 EXPECT_FALSE(csd_service_->enabled());
594 EXPECT_TRUE(csd_service_->model_fetcher_.get() == NULL);
595 // No calls expected yet.
596 Mock::VerifyAndClearExpectations(service);
597
598 ClientSideModel model;
599 model.set_version(10);
600 model.set_max_words_per_term(4);
601 SetModelFetchResponse(model.SerializeAsString(), true /* success */);
602 EXPECT_CALL(*service, ScheduleFetchModel(_))
603 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule));
604 EXPECT_CALL(*service, EndFetchModel(
605 ClientSideDetectionService::MODEL_SUCCESS))
606 .WillOnce(QuitCurrentMessageLoop());
607 csd_service_->SetEnabled(true);
608 EXPECT_TRUE(csd_service_->model_fetcher_.get() != NULL);
609 msg_loop_.Run(); // EndFetchModel will quit the message loop.
610 Mock::VerifyAndClearExpectations(service);
611
612 // Check that enabling again doesn't request the model.
613 csd_service_->SetEnabled(true);
614 // No calls expected.
615 Mock::VerifyAndClearExpectations(service);
616
617 // Check that disabling the service cancels pending requests.
618 EXPECT_CALL(*service, ScheduleFetchModel(_))
619 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule));
620 csd_service_->SetEnabled(false);
621 csd_service_->SetEnabled(true);
622 Mock::VerifyAndClearExpectations(service);
623 EXPECT_TRUE(csd_service_->model_fetcher_.get() != NULL);
624 csd_service_->SetEnabled(false);
625 EXPECT_TRUE(csd_service_->model_fetcher_.get() == NULL);
626 msg_loop_.RunAllPending();
627 // No calls expected.
628 Mock::VerifyAndClearExpectations(service);
629
630 // Requests always return false when the service is disabled.
631 ClientPhishingResponse response;
632 response.set_phishy(true);
633 SetClientReportPhishingResponse(response.SerializeAsString(),
634 true /* success */);
635 EXPECT_FALSE(SendClientReportPhishingRequest(GURL("http://a.com/"), 0.4f));
636
637 // Pending requests also return false if the service is disabled before they
638 // report back.
639 EXPECT_CALL(*service, ScheduleFetchModel(_))
640 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule));
641 EXPECT_CALL(*service, EndFetchModel(
642 ClientSideDetectionService::MODEL_NOT_CHANGED))
643 .WillOnce(Invoke(service, &MockClientSideDetectionService::Disable));
644 csd_service_->SetEnabled(true);
645 EXPECT_FALSE(SendClientReportPhishingRequest(GURL("http://a.com/"), 0.4f));
646 Mock::VerifyAndClearExpectations(service);
647 }
648
562 } // namespace safe_browsing 649 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/client_side_detection_service.cc ('k') | chrome/browser/safe_browsing/safe_browsing_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698