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

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

Issue 7119003: Create a browser feature extractor that runs after the renderer has (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: More testing warnings Created 9 years, 6 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/safe_browsing/browser_feature_extractor.h"
6
7 #include <map>
8 #include <string>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop.h"
12 #include "base/time.h"
13 #include "chrome/common/safe_browsing/csd.pb.h"
14 #include "chrome/browser/history/history.h"
15 #include "chrome/browser/history/history_backend.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/test/testing_profile.h"
18 #include "content/browser/browser_thread.h"
19 #include "content/browser/renderer_host/test_render_view_host.h"
20 #include "content/browser/tab_contents/tab_contents.h"
21 #include "content/browser/tab_contents/test_tab_contents.h"
22 #include "googleurl/src/gurl.h"
23 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25
26 namespace safe_browsing {
27 class BrowserFeatureExtractorTest : public RenderViewHostTestHarness {
28 public:
29 BrowserFeatureExtractorTest()
30 : ui_thread_(BrowserThread::UI, &message_loop_) {
31 }
32 virtual void SetUp() {
33 RenderViewHostTestHarness::SetUp();
34 profile_->CreateHistoryService(true /* delete_file */, false /* no_db */);
35 extractor_.reset(new BrowserFeatureExtractor(contents()));
36 }
37
38 virtual void TearDown() {
39 extractor_.reset();
40 profile_->DestroyHistoryService();
41 RenderViewHostTestHarness::TearDown();
42 }
43
44 HistoryService* history_service() {
45 return profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
46 }
47
48 bool ExtractFeatures(ClientPhishingRequest* request) {
49 StartExtractFeatures(request);
50 MessageLoop::current()->Run();
51 EXPECT_EQ(1U, success_.count(request));
52 return success_.count(request) ? success_[request] : false;
53 }
54
55 void StartExtractFeatures(ClientPhishingRequest* request) {
56 success_.erase(request);
57 extractor_->ExtractFeatures(
58 request,
59 NewCallback(this,
60 &BrowserFeatureExtractorTest::ExtractFeaturesDone));
61 }
62
63 protected:
64 BrowserThread ui_thread_;
65 scoped_ptr<BrowserFeatureExtractor> extractor_;
66 std::map<ClientPhishingRequest*, bool> success_;
67
68 private:
69 void ExtractFeaturesDone(bool success, ClientPhishingRequest* request) {
70 success_[request] = success;
71 MessageLoop::current()->Quit();
72 }
73 };
74
75 TEST_F(BrowserFeatureExtractorTest, UrlNotInHistory) {
76 ClientPhishingRequest request;
77 request.set_url("http://www.google.com/");
78 request.set_client_score(0.5);
79 EXPECT_FALSE(ExtractFeatures(&request));
80 }
81
82 TEST_F(BrowserFeatureExtractorTest, RequestNotInitialized) {
83 ClientPhishingRequest request;
84 request.set_url("http://www.google.com/");
85 // Request is missing the score value.
86 EXPECT_FALSE(ExtractFeatures(&request));
87 }
88
89 TEST_F(BrowserFeatureExtractorTest, UrlInHistory) {
90 history_service()->AddPage(GURL("http://www.foo.com/bar.html"),
91 history::SOURCE_BROWSED);
92 history_service()->AddPage(GURL("https://www.foo.com/gaa.html"),
93 history::SOURCE_BROWSED); // same domain HTTPS.
94 history_service()->AddPage(GURL("http://www.foo.com/gaa.html"),
95 history::SOURCE_BROWSED); // same domain HTTP.
96 history_service()->AddPage(GURL("http://bar.foo.com/gaa.html"),
97 history::SOURCE_BROWSED); // different domain.
98 history_service()->AddPage(GURL("http://www.foo.com/bar.html?a=b"),
99 base::Time::Now() - base::TimeDelta::FromHours(23),
100 NULL, 0, GURL(), PageTransition::LINK,
101 history::RedirectList(), history::SOURCE_BROWSED,
102 false);
103 history_service()->AddPage(GURL("http://www.foo.com/bar.html"),
104 base::Time::Now() - base::TimeDelta::FromHours(25),
105 NULL, 0, GURL(), PageTransition::TYPED,
106 history::RedirectList(), history::SOURCE_BROWSED,
107 false);
108 history_service()->AddPage(GURL("https://www.foo.com/goo.html"),
109 base::Time::Now() - base::TimeDelta::FromDays(5),
110 NULL, 0, GURL(), PageTransition::TYPED,
111 history::RedirectList(), history::SOURCE_BROWSED,
112 false);
113
114 ClientPhishingRequest request;
115 request.set_url("http://www.foo.com/bar.html");
116 request.set_client_score(0.5);
117 EXPECT_TRUE(ExtractFeatures(&request));
118 std::map<std::string, double> features;
119 for (int i = 0; i < request.feature_map_size(); ++i) {
120 EXPECT_EQ(0U, features.count(request.feature_map(i).name()));
121 features[request.feature_map(i).name()] = request.feature_map(i).value();
122 }
123 EXPECT_EQ(8U, features.size());
124 EXPECT_DOUBLE_EQ(2.0, features[features::kUrlHistoryVisitCount]);
125 EXPECT_DOUBLE_EQ(1.0, features[features::kUrlHistoryVisitCount24hAgo]);
126 EXPECT_DOUBLE_EQ(1.0, features[features::kUrlHistoryTypedCount]);
127 EXPECT_DOUBLE_EQ(1.0, features[features::kUrlHistoryLinkCount]);
128 EXPECT_DOUBLE_EQ(4.0, features[features::kHttpHostVisitCount]);
129 EXPECT_DOUBLE_EQ(2.0, features[features::kHttpsHostVisitCount]);
130 EXPECT_DOUBLE_EQ(1.0, features[features::kFirstHttpHostVisitMoreThan24hAgo]);
131 EXPECT_DOUBLE_EQ(1.0, features[features::kFirstHttpsHostVisitMoreThan24hAgo]);
132 }
133
134 TEST_F(BrowserFeatureExtractorTest, MultipleRequestsAtOnce) {
135 history_service()->AddPage(GURL("http://www.foo.com/bar.html"),
136 history::SOURCE_BROWSED);
137 ClientPhishingRequest request;
138 request.set_url("http://www.foo.com/bar.html");
139 request.set_client_score(0.5);
140 StartExtractFeatures(&request);
141
142 ClientPhishingRequest request2;
143 request2.set_url("http://www.foo.com/goo.html");
144 request2.set_client_score(1.0);
145 StartExtractFeatures(&request2);
146
147 // We have to call Run() twice because we expect ExtractFeaturesDone
Paweł Hajdan Jr. 2011/06/08 08:54:46 Could you rather count the number of finished requ
noelutz 2011/06/08 16:39:30 Done.
148 // to be called twice.
149 MessageLoop::current()->Run();
150 MessageLoop::current()->Run();
151
152 EXPECT_TRUE(success_[&request]);
153 EXPECT_FALSE(success_[&request2]);
154 }
155 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/browser_feature_extractor.cc ('k') | chrome/browser/safe_browsing/client_side_detection_host.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698