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/time.h" | 12 #include "base/time.h" |
13 #include "chrome/common/safe_browsing/csd.pb.h" | 13 #include "chrome/common/safe_browsing/csd.pb.h" |
14 #include "chrome/browser/history/history.h" | 14 #include "chrome/browser/history/history.h" |
15 #include "chrome/browser/history/history_backend.h" | 15 #include "chrome/browser/history/history_backend.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/test/testing_profile.h" | 17 #include "chrome/test/testing_profile.h" |
18 #include "content/browser/browser_thread.h" | 18 #include "content/browser/browser_thread.h" |
19 #include "content/browser/renderer_host/test_render_view_host.h" | 19 #include "content/browser/renderer_host/test_render_view_host.h" |
20 #include "content/browser/tab_contents/tab_contents.h" | 20 #include "content/browser/tab_contents/tab_contents.h" |
21 #include "content/browser/tab_contents/test_tab_contents.h" | 21 #include "content/browser/tab_contents/test_tab_contents.h" |
| 22 #include "content/common/page_transition_types.h" |
22 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
23 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 | 26 |
26 namespace safe_browsing { | 27 namespace safe_browsing { |
27 class BrowserFeatureExtractorTest : public RenderViewHostTestHarness { | 28 class BrowserFeatureExtractorTest : public RenderViewHostTestHarness { |
28 protected: | 29 protected: |
29 BrowserFeatureExtractorTest() | 30 BrowserFeatureExtractorTest() |
30 : ui_thread_(BrowserThread::UI, &message_loop_) { | 31 : ui_thread_(BrowserThread::UI, &message_loop_) { |
31 } | 32 } |
32 | 33 |
33 virtual void SetUp() { | 34 virtual void SetUp() { |
34 RenderViewHostTestHarness::SetUp(); | 35 RenderViewHostTestHarness::SetUp(); |
35 profile_->CreateHistoryService(true /* delete_file */, false /* no_db */); | 36 profile_->CreateHistoryService(true /* delete_file */, false /* no_db */); |
36 extractor_.reset(new BrowserFeatureExtractor(contents())); | 37 extractor_.reset(new BrowserFeatureExtractor(contents())); |
37 num_pending_ = 0; | 38 num_pending_ = 0; |
| 39 browse_info_.reset(new BrowseInfo); |
38 } | 40 } |
39 | 41 |
40 virtual void TearDown() { | 42 virtual void TearDown() { |
41 extractor_.reset(); | 43 extractor_.reset(); |
42 profile_->DestroyHistoryService(); | 44 profile_->DestroyHistoryService(); |
43 RenderViewHostTestHarness::TearDown(); | 45 RenderViewHostTestHarness::TearDown(); |
44 ASSERT_EQ(0, num_pending_); | 46 ASSERT_EQ(0, num_pending_); |
45 } | 47 } |
46 | 48 |
47 HistoryService* history_service() { | 49 HistoryService* history_service() { |
48 return profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 50 return profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
49 } | 51 } |
50 | 52 |
51 bool ExtractFeatures(ClientPhishingRequest* request) { | 53 bool ExtractFeatures(ClientPhishingRequest* request) { |
52 StartExtractFeatures(request); | 54 StartExtractFeatures(request); |
53 MessageLoop::current()->Run(); | 55 MessageLoop::current()->Run(); |
54 EXPECT_EQ(1U, success_.count(request)); | 56 EXPECT_EQ(1U, success_.count(request)); |
55 return success_.count(request) ? success_[request] : false; | 57 return success_.count(request) ? success_[request] : false; |
56 } | 58 } |
57 | 59 |
58 void StartExtractFeatures(ClientPhishingRequest* request) { | 60 void StartExtractFeatures(ClientPhishingRequest* request) { |
59 success_.erase(request); | 61 success_.erase(request); |
60 ++num_pending_; | 62 ++num_pending_; |
61 extractor_->ExtractFeatures( | 63 extractor_->ExtractFeatures( |
| 64 *browse_info_, |
62 request, | 65 request, |
63 NewCallback(this, | 66 NewCallback(this, |
64 &BrowserFeatureExtractorTest::ExtractFeaturesDone)); | 67 &BrowserFeatureExtractorTest::ExtractFeaturesDone)); |
65 } | 68 } |
66 | 69 |
| 70 void GetFeatureMap(const ClientPhishingRequest& request, |
| 71 std::map<std::string, double>* features) { |
| 72 for (int i = 0; i < request.non_model_feature_map_size(); ++i) { |
| 73 const ClientPhishingRequest::Feature& feature = |
| 74 request.non_model_feature_map(i); |
| 75 EXPECT_EQ(0U, features->count(feature.name())); |
| 76 (*features)[feature.name()] = feature.value(); |
| 77 } |
| 78 } |
| 79 |
67 BrowserThread ui_thread_; | 80 BrowserThread ui_thread_; |
68 int num_pending_; | 81 int num_pending_; |
69 scoped_ptr<BrowserFeatureExtractor> extractor_; | 82 scoped_ptr<BrowserFeatureExtractor> extractor_; |
70 std::map<ClientPhishingRequest*, bool> success_; | 83 std::map<ClientPhishingRequest*, bool> success_; |
| 84 scoped_ptr<BrowseInfo> browse_info_; |
71 | 85 |
72 private: | 86 private: |
73 void ExtractFeaturesDone(bool success, ClientPhishingRequest* request) { | 87 void ExtractFeaturesDone(bool success, ClientPhishingRequest* request) { |
74 ASSERT_EQ(0U, success_.count(request)); | 88 ASSERT_EQ(0U, success_.count(request)); |
75 success_[request] = success; | 89 success_[request] = success; |
76 if (--num_pending_ == 0) { | 90 if (--num_pending_ == 0) { |
77 MessageLoop::current()->Quit(); | 91 MessageLoop::current()->Quit(); |
78 } | 92 } |
79 } | 93 } |
80 }; | 94 }; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 base::Time::Now() - base::TimeDelta::FromDays(5), | 130 base::Time::Now() - base::TimeDelta::FromDays(5), |
117 NULL, 0, GURL(), PageTransition::TYPED, | 131 NULL, 0, GURL(), PageTransition::TYPED, |
118 history::RedirectList(), history::SOURCE_BROWSED, | 132 history::RedirectList(), history::SOURCE_BROWSED, |
119 false); | 133 false); |
120 | 134 |
121 ClientPhishingRequest request; | 135 ClientPhishingRequest request; |
122 request.set_url("http://www.foo.com/bar.html"); | 136 request.set_url("http://www.foo.com/bar.html"); |
123 request.set_client_score(0.5); | 137 request.set_client_score(0.5); |
124 EXPECT_TRUE(ExtractFeatures(&request)); | 138 EXPECT_TRUE(ExtractFeatures(&request)); |
125 std::map<std::string, double> features; | 139 std::map<std::string, double> features; |
126 for (int i = 0; i < request.non_model_feature_map_size(); ++i) { | 140 GetFeatureMap(request, &features); |
127 const ClientPhishingRequest::Feature& feature = | 141 |
128 request.non_model_feature_map(i); | 142 EXPECT_EQ(10U, features.size()); |
129 EXPECT_EQ(0U, features.count(feature.name())); | |
130 features[feature.name()] = feature.value(); | |
131 } | |
132 EXPECT_EQ(8U, features.size()); | |
133 EXPECT_DOUBLE_EQ(2.0, features[features::kUrlHistoryVisitCount]); | 143 EXPECT_DOUBLE_EQ(2.0, features[features::kUrlHistoryVisitCount]); |
134 EXPECT_DOUBLE_EQ(1.0, | 144 EXPECT_DOUBLE_EQ(1.0, |
135 features[features::kUrlHistoryVisitCountMoreThan24hAgo]); | 145 features[features::kUrlHistoryVisitCountMoreThan24hAgo]); |
136 EXPECT_DOUBLE_EQ(1.0, features[features::kUrlHistoryTypedCount]); | 146 EXPECT_DOUBLE_EQ(1.0, features[features::kUrlHistoryTypedCount]); |
137 EXPECT_DOUBLE_EQ(1.0, features[features::kUrlHistoryLinkCount]); | 147 EXPECT_DOUBLE_EQ(1.0, features[features::kUrlHistoryLinkCount]); |
138 EXPECT_DOUBLE_EQ(4.0, features[features::kHttpHostVisitCount]); | 148 EXPECT_DOUBLE_EQ(4.0, features[features::kHttpHostVisitCount]); |
139 EXPECT_DOUBLE_EQ(2.0, features[features::kHttpsHostVisitCount]); | 149 EXPECT_DOUBLE_EQ(2.0, features[features::kHttpsHostVisitCount]); |
140 EXPECT_DOUBLE_EQ(1.0, features[features::kFirstHttpHostVisitMoreThan24hAgo]); | 150 EXPECT_DOUBLE_EQ(1.0, features[features::kFirstHttpHostVisitMoreThan24hAgo]); |
141 EXPECT_DOUBLE_EQ(1.0, features[features::kFirstHttpsHostVisitMoreThan24hAgo]); | 151 EXPECT_DOUBLE_EQ(1.0, features[features::kFirstHttpsHostVisitMoreThan24hAgo]); |
142 } | 152 } |
(...skipping 10 matching lines...) Expand all Loading... |
153 request2.set_url("http://www.foo.com/goo.html"); | 163 request2.set_url("http://www.foo.com/goo.html"); |
154 request2.set_client_score(1.0); | 164 request2.set_client_score(1.0); |
155 StartExtractFeatures(&request2); | 165 StartExtractFeatures(&request2); |
156 | 166 |
157 MessageLoop::current()->Run(); | 167 MessageLoop::current()->Run(); |
158 EXPECT_TRUE(success_[&request]); | 168 EXPECT_TRUE(success_[&request]); |
159 // Success is false because the second URL is not in the history and we are | 169 // Success is false because the second URL is not in the history and we are |
160 // not able to distinguish between a missing URL in the history and an error. | 170 // not able to distinguish between a missing URL in the history and an error. |
161 EXPECT_FALSE(success_[&request2]); | 171 EXPECT_FALSE(success_[&request2]); |
162 } | 172 } |
| 173 |
| 174 TEST_F(BrowserFeatureExtractorTest, BrowseFeatures) { |
| 175 history_service()->AddPage(GURL("http://www.foo.com/"), |
| 176 history::SOURCE_BROWSED); |
| 177 ClientPhishingRequest request; |
| 178 request.set_url("http://www.foo.com/"); |
| 179 request.set_client_score(0.5); |
| 180 |
| 181 browse_info_->url = GURL("http://www.foo.com/"); |
| 182 browse_info_->referrer = GURL("http://google.com/"); |
| 183 browse_info_->transition = |
| 184 PageTransition::LINK | PageTransition::FORWARD_BACK; |
| 185 |
| 186 EXPECT_TRUE(ExtractFeatures(&request)); |
| 187 std::map<std::string, double> features; |
| 188 GetFeatureMap(request, &features); |
| 189 |
| 190 EXPECT_EQ("http://google.com/", request.referrer_url()); |
| 191 EXPECT_EQ(0.0, features[features::kHasSSLReferrer]); |
| 192 EXPECT_EQ(0.0, features[features::kPageTransitionType]); |
| 193 |
| 194 request.Clear(); |
| 195 request.set_url("http://www.foo.com/"); |
| 196 request.set_client_score(0.5); |
| 197 |
| 198 browse_info_->referrer = GURL("https://bankofamerica.com/"); |
| 199 browse_info_->transition = |
| 200 PageTransition::TYPED | PageTransition::FORWARD_BACK; |
| 201 |
| 202 EXPECT_TRUE(ExtractFeatures(&request)); |
| 203 features.clear(); |
| 204 GetFeatureMap(request, &features); |
| 205 |
| 206 EXPECT_FALSE(request.has_referrer_url()); |
| 207 EXPECT_EQ(1.0, features[features::kHasSSLReferrer]); |
| 208 EXPECT_EQ(1.0, features[features::kPageTransitionType]); |
| 209 } |
163 } // namespace safe_browsing | 210 } // namespace safe_browsing |
OLD | NEW |