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

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

Issue 10963018: Rework arguments of HistoryService::AddPage() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix Windows compile Created 8 years, 3 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include <vector> 9 #include <vector>
10 10
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/stringprintf.h" 13 #include "base/stringprintf.h"
14 #include "base/time.h" 14 #include "base/time.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/history/history_service_factory.h" 17 #include "chrome/browser/history/history_service_factory.h"
18 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/safe_browsing/browser_features.h" 19 #include "chrome/browser/safe_browsing/browser_features.h"
20 #include "chrome/browser/safe_browsing/client_side_detection_service.h" 20 #include "chrome/browser/safe_browsing/client_side_detection_service.h"
21 #include "chrome/common/safe_browsing/csd.pb.h" 21 #include "chrome/common/safe_browsing/csd.pb.h"
22 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 22 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
23 #include "chrome/test/base/testing_profile.h" 23 #include "chrome/test/base/testing_profile.h"
24 #include "content/public/browser/navigation_controller.h"
24 #include "content/public/browser/web_contents.h" 25 #include "content/public/browser/web_contents.h"
25 #include "content/public/common/page_transition_types.h" 26 #include "content/public/common/page_transition_types.h"
26 #include "content/public/common/referrer.h" 27 #include "content/public/common/referrer.h"
27 #include "content/public/browser/navigation_controller.h"
28 #include "content/public/test/test_browser_thread.h" 28 #include "content/public/test/test_browser_thread.h"
29 #include "content/public/test/web_contents_tester.h" 29 #include "content/public/test/web_contents_tester.h"
30 #include "googleurl/src/gurl.h" 30 #include "googleurl/src/gurl.h"
31 #include "testing/gmock/include/gmock/gmock.h" 31 #include "testing/gmock/include/gmock/gmock.h"
32 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
33 33
34 using ::testing::Return; 34 using ::testing::Return;
35 using ::testing::StrictMock; 35 using ::testing::StrictMock;
36 using content::BrowserThread; 36 using content::BrowserThread;
37 using content::WebContentsTester; 37 using content::WebContentsTester;
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 TEST_F(BrowserFeatureExtractorTest, RequestNotInitialized) { 167 TEST_F(BrowserFeatureExtractorTest, RequestNotInitialized) {
168 ClientPhishingRequest request; 168 ClientPhishingRequest request;
169 request.set_url("http://www.google.com/"); 169 request.set_url("http://www.google.com/");
170 // Request is missing the score value. 170 // Request is missing the score value.
171 SimpleNavigateAndCommit(GURL("http://www.google.com")); 171 SimpleNavigateAndCommit(GURL("http://www.google.com"));
172 EXPECT_FALSE(ExtractFeatures(&request)); 172 EXPECT_FALSE(ExtractFeatures(&request));
173 } 173 }
174 174
175 TEST_F(BrowserFeatureExtractorTest, UrlInHistory) { 175 TEST_F(BrowserFeatureExtractorTest, UrlInHistory) {
176 history_service()->AddPage(GURL("http://www.foo.com/bar.html"), 176 history_service()->AddPage(GURL("http://www.foo.com/bar.html"),
177 base::Time::Now(),
177 history::SOURCE_BROWSED); 178 history::SOURCE_BROWSED);
178 history_service()->AddPage(GURL("https://www.foo.com/gaa.html"), 179 history_service()->AddPage(GURL("https://www.foo.com/gaa.html"),
180 base::Time::Now(),
179 history::SOURCE_BROWSED); // same host HTTPS. 181 history::SOURCE_BROWSED); // same host HTTPS.
180 history_service()->AddPage(GURL("http://www.foo.com/gaa.html"), 182 history_service()->AddPage(GURL("http://www.foo.com/gaa.html"),
183 base::Time::Now(),
181 history::SOURCE_BROWSED); // same host HTTP. 184 history::SOURCE_BROWSED); // same host HTTP.
182 history_service()->AddPage(GURL("http://bar.foo.com/gaa.html"), 185 history_service()->AddPage(GURL("http://bar.foo.com/gaa.html"),
186 base::Time::Now(),
183 history::SOURCE_BROWSED); // different host. 187 history::SOURCE_BROWSED); // different host.
184 history_service()->AddPage(GURL("http://www.foo.com/bar.html?a=b"), 188 history_service()->AddPage(GURL("http://www.foo.com/bar.html?a=b"),
185 base::Time::Now() - base::TimeDelta::FromHours(23), 189 base::Time::Now() - base::TimeDelta::FromHours(23),
186 NULL, 0, GURL(), content::PAGE_TRANSITION_LINK, 190 NULL, 0, GURL(), history::RedirectList(),
187 history::RedirectList(), history::SOURCE_BROWSED, 191 content::PAGE_TRANSITION_LINK,
188 false); 192 history::SOURCE_BROWSED, false);
189 history_service()->AddPage(GURL("http://www.foo.com/bar.html"), 193 history_service()->AddPage(GURL("http://www.foo.com/bar.html"),
190 base::Time::Now() - base::TimeDelta::FromHours(25), 194 base::Time::Now() - base::TimeDelta::FromHours(25),
191 NULL, 0, GURL(), content::PAGE_TRANSITION_TYPED, 195 NULL, 0, GURL(), history::RedirectList(),
192 history::RedirectList(), history::SOURCE_BROWSED, 196 content::PAGE_TRANSITION_TYPED,
193 false); 197 history::SOURCE_BROWSED, false);
194 history_service()->AddPage(GURL("https://www.foo.com/goo.html"), 198 history_service()->AddPage(GURL("https://www.foo.com/goo.html"),
195 base::Time::Now() - base::TimeDelta::FromDays(5), 199 base::Time::Now() - base::TimeDelta::FromDays(5),
196 NULL, 0, GURL(), content::PAGE_TRANSITION_TYPED, 200 NULL, 0, GURL(), history::RedirectList(),
197 history::RedirectList(), history::SOURCE_BROWSED, 201 content::PAGE_TRANSITION_TYPED,
198 false); 202 history::SOURCE_BROWSED, false);
199 203
200 SimpleNavigateAndCommit(GURL("http://www.foo.com/bar.html")); 204 SimpleNavigateAndCommit(GURL("http://www.foo.com/bar.html"));
201 205
202 ClientPhishingRequest request; 206 ClientPhishingRequest request;
203 request.set_url("http://www.foo.com/bar.html"); 207 request.set_url("http://www.foo.com/bar.html");
204 request.set_client_score(0.5); 208 request.set_client_score(0.5);
205 EXPECT_TRUE(ExtractFeatures(&request)); 209 EXPECT_TRUE(ExtractFeatures(&request));
206 std::map<std::string, double> features; 210 std::map<std::string, double> features;
207 GetFeatureMap(request, &features); 211 GetFeatureMap(request, &features);
208 212
(...skipping 23 matching lines...) Expand all
232 EXPECT_DOUBLE_EQ(0.0, features[features::kUrlHistoryTypedCount]); 236 EXPECT_DOUBLE_EQ(0.0, features[features::kUrlHistoryTypedCount]);
233 EXPECT_DOUBLE_EQ(1.0, features[features::kUrlHistoryLinkCount]); 237 EXPECT_DOUBLE_EQ(1.0, features[features::kUrlHistoryLinkCount]);
234 EXPECT_DOUBLE_EQ(1.0, features[features::kHttpHostVisitCount]); 238 EXPECT_DOUBLE_EQ(1.0, features[features::kHttpHostVisitCount]);
235 EXPECT_DOUBLE_EQ(0.0, features[features::kHttpsHostVisitCount]); 239 EXPECT_DOUBLE_EQ(0.0, features[features::kHttpsHostVisitCount]);
236 EXPECT_DOUBLE_EQ(0.0, features[features::kFirstHttpHostVisitMoreThan24hAgo]); 240 EXPECT_DOUBLE_EQ(0.0, features[features::kFirstHttpHostVisitMoreThan24hAgo]);
237 EXPECT_FALSE(features.count(features::kFirstHttpsHostVisitMoreThan24hAgo)); 241 EXPECT_FALSE(features.count(features::kFirstHttpsHostVisitMoreThan24hAgo));
238 } 242 }
239 243
240 TEST_F(BrowserFeatureExtractorTest, MultipleRequestsAtOnce) { 244 TEST_F(BrowserFeatureExtractorTest, MultipleRequestsAtOnce) {
241 history_service()->AddPage(GURL("http://www.foo.com/bar.html"), 245 history_service()->AddPage(GURL("http://www.foo.com/bar.html"),
246 base::Time::Now(),
242 history::SOURCE_BROWSED); 247 history::SOURCE_BROWSED);
243 SimpleNavigateAndCommit(GURL("http:/www.foo.com/bar.html")); 248 SimpleNavigateAndCommit(GURL("http:/www.foo.com/bar.html"));
244 ClientPhishingRequest request; 249 ClientPhishingRequest request;
245 request.set_url("http://www.foo.com/bar.html"); 250 request.set_url("http://www.foo.com/bar.html");
246 request.set_client_score(0.5); 251 request.set_client_score(0.5);
247 StartExtractFeatures(&request); 252 StartExtractFeatures(&request);
248 253
249 SimpleNavigateAndCommit(GURL("http://www.foo.com/goo.html")); 254 SimpleNavigateAndCommit(GURL("http://www.foo.com/goo.html"));
250 ClientPhishingRequest request2; 255 ClientPhishingRequest request2;
251 request2.set_url("http://www.foo.com/goo.html"); 256 request2.set_url("http://www.foo.com/goo.html");
252 request2.set_client_score(1.0); 257 request2.set_client_score(1.0);
253 StartExtractFeatures(&request2); 258 StartExtractFeatures(&request2);
254 259
255 MessageLoop::current()->Run(); 260 MessageLoop::current()->Run();
256 EXPECT_TRUE(success_[&request]); 261 EXPECT_TRUE(success_[&request]);
257 // Success is false because the second URL is not in the history and we are 262 // Success is false because the second URL is not in the history and we are
258 // not able to distinguish between a missing URL in the history and an error. 263 // not able to distinguish between a missing URL in the history and an error.
259 EXPECT_FALSE(success_[&request2]); 264 EXPECT_FALSE(success_[&request2]);
260 } 265 }
261 266
262 TEST_F(BrowserFeatureExtractorTest, BrowseFeatures) { 267 TEST_F(BrowserFeatureExtractorTest, BrowseFeatures) {
263 history_service()->AddPage(GURL("http://www.foo.com/"), 268 history_service()->AddPage(GURL("http://www.foo.com/"),
269 base::Time::Now(),
264 history::SOURCE_BROWSED); 270 history::SOURCE_BROWSED);
265 history_service()->AddPage(GURL("http://www.foo.com/page.html"), 271 history_service()->AddPage(GURL("http://www.foo.com/page.html"),
272 base::Time::Now(),
266 history::SOURCE_BROWSED); 273 history::SOURCE_BROWSED);
267 history_service()->AddPage(GURL("http://www.bar.com/"), 274 history_service()->AddPage(GURL("http://www.bar.com/"),
275 base::Time::Now(),
268 history::SOURCE_BROWSED); 276 history::SOURCE_BROWSED);
269 history_service()->AddPage(GURL("http://www.bar.com/other_page.html"), 277 history_service()->AddPage(GURL("http://www.bar.com/other_page.html"),
278 base::Time::Now(),
270 history::SOURCE_BROWSED); 279 history::SOURCE_BROWSED);
271 history_service()->AddPage(GURL("http://www.baz.com/"), 280 history_service()->AddPage(GURL("http://www.baz.com/"),
281 base::Time::Now(),
272 history::SOURCE_BROWSED); 282 history::SOURCE_BROWSED);
273 283
274 ClientPhishingRequest request; 284 ClientPhishingRequest request;
275 request.set_url("http://www.foo.com/"); 285 request.set_url("http://www.foo.com/");
276 request.set_client_score(0.5); 286 request.set_client_score(0.5);
277 std::vector<GURL> redirect_chain; 287 std::vector<GURL> redirect_chain;
278 redirect_chain.push_back(GURL("http://somerandomwebsite.com/")); 288 redirect_chain.push_back(GURL("http://somerandomwebsite.com/"));
279 redirect_chain.push_back(GURL("http://www.foo.com/")); 289 redirect_chain.push_back(GURL("http://www.foo.com/"));
280 SetRedirectChain(redirect_chain, true); 290 SetRedirectChain(redirect_chain, true);
281 browse_info_->http_status_code = 200; 291 browse_info_->http_status_code = 200;
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 EXPECT_TRUE(features.count(StringPrintf("%s%s", 511 EXPECT_TRUE(features.count(StringPrintf("%s%s",
502 features::kSafeBrowsingMaliciousUrl, 512 features::kSafeBrowsingMaliciousUrl,
503 "http://www.malware.com/"))); 513 "http://www.malware.com/")));
504 EXPECT_TRUE(features.count(StringPrintf("%s%s", 514 EXPECT_TRUE(features.count(StringPrintf("%s%s",
505 features::kSafeBrowsingOriginalUrl, 515 features::kSafeBrowsingOriginalUrl,
506 "http://www.good.com/"))); 516 "http://www.good.com/")));
507 EXPECT_DOUBLE_EQ(1.0, features[features::kSafeBrowsingIsSubresource]); 517 EXPECT_DOUBLE_EQ(1.0, features[features::kSafeBrowsingIsSubresource]);
508 EXPECT_DOUBLE_EQ(2.0, features[features::kSafeBrowsingThreatType]); 518 EXPECT_DOUBLE_EQ(2.0, features[features::kSafeBrowsingThreatType]);
509 } 519 }
510 } // namespace safe_browsing 520 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/browser/prerender/prerender_contents.cc ('k') | chrome/browser/safe_browsing/malware_details_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698