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

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: Address Pawel's comments 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_) {
Paweł Hajdan Jr. 2011/06/09 09:13:10 nit: Can it fit in the previous line?
noelutz 2011/06/09 19:21:39 81 chars :-(
31 }
Paweł Hajdan Jr. 2011/06/09 09:13:10 nit: Add an empty line below.
noelutz 2011/06/09 19:21:39 Done.
32 virtual void SetUp() {
33 RenderViewHostTestHarness::SetUp();
34 profile_->CreateHistoryService(true /* delete_file */, false /* no_db */);
35 extractor_.reset(new BrowserFeatureExtractor(contents()));
36 num_pending_ = 0;
37 }
38
39 virtual void TearDown() {
40 extractor_.reset();
41 profile_->DestroyHistoryService();
42 RenderViewHostTestHarness::TearDown();
43 ASSERT_EQ(0, num_pending_);
44 }
45
46 HistoryService* history_service() {
47 return profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
48 }
49
50 bool ExtractFeatures(ClientPhishingRequest* request) {
51 StartExtractFeatures(request);
52 MessageLoop::current()->Run();
53 EXPECT_EQ(1U, success_.count(request));
54 return success_.count(request) ? success_[request] : false;
55 }
56
57 void StartExtractFeatures(ClientPhishingRequest* request) {
58 success_.erase(request);
59 ++num_pending_;
60 extractor_->ExtractFeatures(
61 request,
62 NewCallback(this,
63 &BrowserFeatureExtractorTest::ExtractFeaturesDone));
64 }
65
66 protected:
67 BrowserThread ui_thread_;
68 int num_pending_;
69 scoped_ptr<BrowserFeatureExtractor> extractor_;
70 std::map<ClientPhishingRequest*, bool> success_;
71
72 private:
73 void ExtractFeaturesDone(bool success, ClientPhishingRequest* request) {
74 success_[request] = success;
75 if (--num_pending_ == 0) {
76 MessageLoop::current()->Quit();
77 }
78 }
79 };
80
81 TEST_F(BrowserFeatureExtractorTest, UrlNotInHistory) {
82 ClientPhishingRequest request;
83 request.set_url("http://www.google.com/");
84 request.set_client_score(0.5);
85 EXPECT_FALSE(ExtractFeatures(&request));
86 }
87
88 TEST_F(BrowserFeatureExtractorTest, RequestNotInitialized) {
89 ClientPhishingRequest request;
90 request.set_url("http://www.google.com/");
91 // Request is missing the score value.
92 EXPECT_FALSE(ExtractFeatures(&request));
93 }
94
95 TEST_F(BrowserFeatureExtractorTest, UrlInHistory) {
96 history_service()->AddPage(GURL("http://www.foo.com/bar.html"),
97 history::SOURCE_BROWSED);
98 history_service()->AddPage(GURL("https://www.foo.com/gaa.html"),
99 history::SOURCE_BROWSED); // same domain HTTPS.
100 history_service()->AddPage(GURL("http://www.foo.com/gaa.html"),
101 history::SOURCE_BROWSED); // same domain HTTP.
102 history_service()->AddPage(GURL("http://bar.foo.com/gaa.html"),
103 history::SOURCE_BROWSED); // different domain.
104 history_service()->AddPage(GURL("http://www.foo.com/bar.html?a=b"),
105 base::Time::Now() - base::TimeDelta::FromHours(23),
106 NULL, 0, GURL(), PageTransition::LINK,
107 history::RedirectList(), history::SOURCE_BROWSED,
108 false);
109 history_service()->AddPage(GURL("http://www.foo.com/bar.html"),
110 base::Time::Now() - base::TimeDelta::FromHours(25),
111 NULL, 0, GURL(), PageTransition::TYPED,
112 history::RedirectList(), history::SOURCE_BROWSED,
113 false);
114 history_service()->AddPage(GURL("https://www.foo.com/goo.html"),
115 base::Time::Now() - base::TimeDelta::FromDays(5),
116 NULL, 0, GURL(), PageTransition::TYPED,
117 history::RedirectList(), history::SOURCE_BROWSED,
118 false);
119
120 ClientPhishingRequest request;
121 request.set_url("http://www.foo.com/bar.html");
122 request.set_client_score(0.5);
123 EXPECT_TRUE(ExtractFeatures(&request));
124 std::map<std::string, double> features;
125 for (int i = 0; i < request.feature_map_size(); ++i) {
126 EXPECT_EQ(0U, features.count(request.feature_map(i).name()));
127 features[request.feature_map(i).name()] = request.feature_map(i).value();
128 }
129 EXPECT_EQ(8U, features.size());
130 EXPECT_DOUBLE_EQ(2.0, features[features::kUrlHistoryVisitCount]);
131 EXPECT_DOUBLE_EQ(1.0, features[features::kUrlHistoryVisitCount24hAgo]);
132 EXPECT_DOUBLE_EQ(1.0, features[features::kUrlHistoryTypedCount]);
133 EXPECT_DOUBLE_EQ(1.0, features[features::kUrlHistoryLinkCount]);
134 EXPECT_DOUBLE_EQ(4.0, features[features::kHttpHostVisitCount]);
135 EXPECT_DOUBLE_EQ(2.0, features[features::kHttpsHostVisitCount]);
136 EXPECT_DOUBLE_EQ(1.0, features[features::kFirstHttpHostVisitMoreThan24hAgo]);
137 EXPECT_DOUBLE_EQ(1.0, features[features::kFirstHttpsHostVisitMoreThan24hAgo]);
138 }
139
140 TEST_F(BrowserFeatureExtractorTest, MultipleRequestsAtOnce) {
141 history_service()->AddPage(GURL("http://www.foo.com/bar.html"),
142 history::SOURCE_BROWSED);
143 ClientPhishingRequest request;
144 request.set_url("http://www.foo.com/bar.html");
145 request.set_client_score(0.5);
146 StartExtractFeatures(&request);
147
148 ClientPhishingRequest request2;
149 request2.set_url("http://www.foo.com/goo.html");
150 request2.set_client_score(1.0);
151 StartExtractFeatures(&request2);
152
153 MessageLoop::current()->Run();
154 EXPECT_TRUE(success_[&request]);
155 EXPECT_FALSE(success_[&request2]);
mattm 2011/06/08 23:18:18 This one is expected to fail since it wasn't added
noelutz 2011/06/09 19:21:39 Done.
156 }
157 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698