OLD | NEW |
| (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 protected: | |
29 BrowserFeatureExtractorTest() | |
30 : ui_thread_(BrowserThread::UI, &message_loop_) { | |
31 } | |
32 | |
33 virtual void SetUp() { | |
34 RenderViewHostTestHarness::SetUp(); | |
35 profile_->CreateHistoryService(true /* delete_file */, false /* no_db */); | |
36 extractor_.reset(new BrowserFeatureExtractor(contents())); | |
37 num_pending_ = 0; | |
38 } | |
39 | |
40 virtual void TearDown() { | |
41 extractor_.reset(); | |
42 profile_->DestroyHistoryService(); | |
43 RenderViewHostTestHarness::TearDown(); | |
44 ASSERT_EQ(0, num_pending_); | |
45 } | |
46 | |
47 HistoryService* history_service() { | |
48 return profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | |
49 } | |
50 | |
51 bool ExtractFeatures(ClientPhishingRequest* request) { | |
52 StartExtractFeatures(request); | |
53 MessageLoop::current()->Run(); | |
54 EXPECT_EQ(1U, success_.count(request)); | |
55 return success_.count(request) ? success_[request] : false; | |
56 } | |
57 | |
58 void StartExtractFeatures(ClientPhishingRequest* request) { | |
59 success_.erase(request); | |
60 ++num_pending_; | |
61 extractor_->ExtractFeatures( | |
62 request, | |
63 NewCallback(this, | |
64 &BrowserFeatureExtractorTest::ExtractFeaturesDone)); | |
65 } | |
66 | |
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 ASSERT_EQ(0U, success_.count(request)); | |
75 success_[request] = success; | |
76 if (--num_pending_ == 0) { | |
77 MessageLoop::current()->Quit(); | |
78 } | |
79 } | |
80 }; | |
81 | |
82 TEST_F(BrowserFeatureExtractorTest, UrlNotInHistory) { | |
83 ClientPhishingRequest request; | |
84 request.set_url("http://www.google.com/"); | |
85 request.set_client_score(0.5); | |
86 EXPECT_FALSE(ExtractFeatures(&request)); | |
87 } | |
88 | |
89 TEST_F(BrowserFeatureExtractorTest, RequestNotInitialized) { | |
90 ClientPhishingRequest request; | |
91 request.set_url("http://www.google.com/"); | |
92 // Request is missing the score value. | |
93 EXPECT_FALSE(ExtractFeatures(&request)); | |
94 } | |
95 | |
96 TEST_F(BrowserFeatureExtractorTest, UrlInHistory) { | |
97 history_service()->AddPage(GURL("http://www.foo.com/bar.html"), | |
98 history::SOURCE_BROWSED); | |
99 history_service()->AddPage(GURL("https://www.foo.com/gaa.html"), | |
100 history::SOURCE_BROWSED); // same host HTTPS. | |
101 history_service()->AddPage(GURL("http://www.foo.com/gaa.html"), | |
102 history::SOURCE_BROWSED); // same host HTTP. | |
103 history_service()->AddPage(GURL("http://bar.foo.com/gaa.html"), | |
104 history::SOURCE_BROWSED); // different host. | |
105 history_service()->AddPage(GURL("http://www.foo.com/bar.html?a=b"), | |
106 base::Time::Now() - base::TimeDelta::FromHours(23), | |
107 NULL, 0, GURL(), PageTransition::LINK, | |
108 history::RedirectList(), history::SOURCE_BROWSED, | |
109 false); | |
110 history_service()->AddPage(GURL("http://www.foo.com/bar.html"), | |
111 base::Time::Now() - base::TimeDelta::FromHours(25), | |
112 NULL, 0, GURL(), PageTransition::TYPED, | |
113 history::RedirectList(), history::SOURCE_BROWSED, | |
114 false); | |
115 history_service()->AddPage(GURL("https://www.foo.com/goo.html"), | |
116 base::Time::Now() - base::TimeDelta::FromDays(5), | |
117 NULL, 0, GURL(), PageTransition::TYPED, | |
118 history::RedirectList(), history::SOURCE_BROWSED, | |
119 false); | |
120 | |
121 ClientPhishingRequest request; | |
122 request.set_url("http://www.foo.com/bar.html"); | |
123 request.set_client_score(0.5); | |
124 EXPECT_TRUE(ExtractFeatures(&request)); | |
125 std::map<std::string, double> features; | |
126 for (int i = 0; i < request.non_model_feature_map_size(); ++i) { | |
127 const ClientPhishingRequest::Feature& feature = | |
128 request.non_model_feature_map(i); | |
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]); | |
134 EXPECT_DOUBLE_EQ(1.0, | |
135 features[features::kUrlHistoryVisitCountMoreThan24hAgo]); | |
136 EXPECT_DOUBLE_EQ(1.0, features[features::kUrlHistoryTypedCount]); | |
137 EXPECT_DOUBLE_EQ(1.0, features[features::kUrlHistoryLinkCount]); | |
138 EXPECT_DOUBLE_EQ(4.0, features[features::kHttpHostVisitCount]); | |
139 EXPECT_DOUBLE_EQ(2.0, features[features::kHttpsHostVisitCount]); | |
140 EXPECT_DOUBLE_EQ(1.0, features[features::kFirstHttpHostVisitMoreThan24hAgo]); | |
141 EXPECT_DOUBLE_EQ(1.0, features[features::kFirstHttpsHostVisitMoreThan24hAgo]); | |
142 } | |
143 | |
144 TEST_F(BrowserFeatureExtractorTest, MultipleRequestsAtOnce) { | |
145 history_service()->AddPage(GURL("http://www.foo.com/bar.html"), | |
146 history::SOURCE_BROWSED); | |
147 ClientPhishingRequest request; | |
148 request.set_url("http://www.foo.com/bar.html"); | |
149 request.set_client_score(0.5); | |
150 StartExtractFeatures(&request); | |
151 | |
152 ClientPhishingRequest request2; | |
153 request2.set_url("http://www.foo.com/goo.html"); | |
154 request2.set_client_score(1.0); | |
155 StartExtractFeatures(&request2); | |
156 | |
157 MessageLoop::current()->Run(); | |
158 EXPECT_TRUE(success_[&request]); | |
159 // 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. | |
161 EXPECT_FALSE(success_[&request2]); | |
162 } | |
163 } // namespace safe_browsing | |
OLD | NEW |