| 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 "chrome/browser/safe_browsing/browser_feature_extractor.h" | 5 #include "chrome/browser/safe_browsing/browser_feature_extractor.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "base/time.h" | 13 #include "base/time.h" |
| 14 #include "chrome/common/safe_browsing/csd.pb.h" | 14 #include "chrome/common/safe_browsing/csd.pb.h" |
| 15 #include "chrome/browser/history/history.h" | 15 #include "chrome/browser/history/history.h" |
| 16 #include "chrome/browser/history/history_backend.h" | 16 #include "chrome/browser/history/history_backend.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/safe_browsing/browser_features.h" | 18 #include "chrome/browser/safe_browsing/browser_features.h" |
| 19 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | 19 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
| 20 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 20 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
| 21 #include "content/browser/browser_thread.h" | 22 #include "content/browser/browser_thread.h" |
| 22 #include "content/browser/renderer_host/test_render_view_host.h" | |
| 23 #include "content/browser/tab_contents/tab_contents.h" | 23 #include "content/browser/tab_contents/tab_contents.h" |
| 24 #include "content/browser/tab_contents/test_tab_contents.h" | 24 #include "content/browser/tab_contents/test_tab_contents.h" |
| 25 #include "content/common/page_transition_types.h" | 25 #include "content/common/page_transition_types.h" |
| 26 #include "content/common/view_messages.h" | 26 #include "content/common/view_messages.h" |
| 27 #include "crypto/sha2.h" | 27 #include "crypto/sha2.h" |
| 28 #include "googleurl/src/gurl.h" | 28 #include "googleurl/src/gurl.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" | 29 #include "testing/gmock/include/gmock/gmock.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 31 |
| 32 using ::testing::Return; | 32 using ::testing::Return; |
| 33 using ::testing::StrictMock; | 33 using ::testing::StrictMock; |
| 34 | 34 |
| 35 namespace safe_browsing { | 35 namespace safe_browsing { |
| 36 namespace { | 36 namespace { |
| 37 class MockClientSideDetectionService : public ClientSideDetectionService { | 37 class MockClientSideDetectionService : public ClientSideDetectionService { |
| 38 public: | 38 public: |
| 39 MockClientSideDetectionService() : ClientSideDetectionService(NULL) {} | 39 MockClientSideDetectionService() : ClientSideDetectionService(NULL) {} |
| 40 virtual ~MockClientSideDetectionService() {}; | 40 virtual ~MockClientSideDetectionService() {}; |
| 41 | 41 |
| 42 MOCK_CONST_METHOD1(IsBadIpAddress, bool(const std::string&)); | 42 MOCK_CONST_METHOD1(IsBadIpAddress, bool(const std::string&)); |
| 43 }; | 43 }; |
| 44 } // namespace | 44 } // namespace |
| 45 | 45 |
| 46 class BrowserFeatureExtractorTest : public RenderViewHostTestHarness { | 46 class BrowserFeatureExtractorTest : public ChromeRenderViewHostTestHarness { |
| 47 protected: | 47 protected: |
| 48 BrowserFeatureExtractorTest() | 48 BrowserFeatureExtractorTest() |
| 49 : ui_thread_(BrowserThread::UI, &message_loop_) { | 49 : ui_thread_(BrowserThread::UI, &message_loop_) { |
| 50 } | 50 } |
| 51 | 51 |
| 52 virtual void SetUp() { | 52 virtual void SetUp() { |
| 53 RenderViewHostTestHarness::SetUp(); | 53 ChromeRenderViewHostTestHarness::SetUp(); |
| 54 profile_->CreateHistoryService(true /* delete_file */, false /* no_db */); | 54 profile()->CreateHistoryService(true /* delete_file */, false /* no_db */); |
| 55 service_.reset(new StrictMock<MockClientSideDetectionService>()); | 55 service_.reset(new StrictMock<MockClientSideDetectionService>()); |
| 56 extractor_.reset(new BrowserFeatureExtractor(contents(), service_.get())); | 56 extractor_.reset(new BrowserFeatureExtractor(contents(), service_.get())); |
| 57 num_pending_ = 0; | 57 num_pending_ = 0; |
| 58 browse_info_.reset(new BrowseInfo); | 58 browse_info_.reset(new BrowseInfo); |
| 59 } | 59 } |
| 60 | 60 |
| 61 virtual void TearDown() { | 61 virtual void TearDown() { |
| 62 extractor_.reset(); | 62 extractor_.reset(); |
| 63 profile_->DestroyHistoryService(); | 63 profile()->DestroyHistoryService(); |
| 64 RenderViewHostTestHarness::TearDown(); | 64 ChromeRenderViewHostTestHarness::TearDown(); |
| 65 ASSERT_EQ(0, num_pending_); | 65 ASSERT_EQ(0, num_pending_); |
| 66 } | 66 } |
| 67 | 67 |
| 68 HistoryService* history_service() { | 68 HistoryService* history_service() { |
| 69 return profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 69 return profile()->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 70 } | 70 } |
| 71 | 71 |
| 72 // This is similar to NavigateAndCommit that is in test_tab_contents, but | 72 // This is similar to NavigateAndCommit that is in test_tab_contents, but |
| 73 // allows us to specify the referrer and page_transition_type. | 73 // allows us to specify the referrer and page_transition_type. |
| 74 void NavigateAndCommit(const GURL& url, | 74 void NavigateAndCommit(const GURL& url, |
| 75 const GURL& referrer, | 75 const GURL& referrer, |
| 76 PageTransition::Type type) { | 76 PageTransition::Type type) { |
| 77 contents()->controller().LoadURL(url, referrer, type, std::string()); | 77 contents()->controller().LoadURL(url, referrer, type, std::string()); |
| 78 | 78 |
| 79 static int page_id = 0; | 79 static int page_id = 0; |
| (...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 history_service()->AddPage(GURL("http://www.host.com/A%21//B"), | 518 history_service()->AddPage(GURL("http://www.host.com/A%21//B"), |
| 519 history::SOURCE_BROWSED); | 519 history::SOURCE_BROWSED); |
| 520 contents()->NavigateAndCommit(GURL("http://www.host.com/A%21//B")); | 520 contents()->NavigateAndCommit(GURL("http://www.host.com/A%21//B")); |
| 521 | 521 |
| 522 EXPECT_TRUE(ExtractFeatures(&request)); | 522 EXPECT_TRUE(ExtractFeatures(&request)); |
| 523 EXPECT_EQ(crypto::SHA256HashString("host.com/a!/").substr( | 523 EXPECT_EQ(crypto::SHA256HashString("host.com/a!/").substr( |
| 524 0, BrowserFeatureExtractor::kHashPrefixLength), | 524 0, BrowserFeatureExtractor::kHashPrefixLength), |
| 525 request.hash_prefix()); | 525 request.hash_prefix()); |
| 526 } | 526 } |
| 527 } // namespace safe_browsing | 527 } // namespace safe_browsing |
| OLD | NEW |