OLD | NEW |
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/renderer/safe_browsing/phishing_classifier.h" | 5 #include "chrome/renderer/safe_browsing/phishing_classifier.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 | 96 |
97 clock_ = new MockFeatureExtractorClock; | 97 clock_ = new MockFeatureExtractorClock; |
98 scorer_.reset(Scorer::Create(model.SerializeAsString())); | 98 scorer_.reset(Scorer::Create(model.SerializeAsString())); |
99 ASSERT_TRUE(scorer_.get()); | 99 ASSERT_TRUE(scorer_.get()); |
100 | 100 |
101 content::WebContents* web_contents = | 101 content::WebContents* web_contents = |
102 browser()->tab_strip_model()->GetActiveWebContents(); | 102 browser()->tab_strip_model()->GetActiveWebContents(); |
103 content::RenderFrame* render_frame = content::RenderFrame::FromRoutingID( | 103 content::RenderFrame* render_frame = content::RenderFrame::FromRoutingID( |
104 web_contents->GetMainFrame()->GetRoutingID()); | 104 web_contents->GetMainFrame()->GetRoutingID()); |
105 classifier_.reset(new PhishingClassifier(render_frame, clock_)); | 105 classifier_.reset(new PhishingClassifier(render_frame, clock_)); |
| 106 |
| 107 embedded_test_server()->RegisterRequestHandler( |
| 108 base::Bind(&PhishingClassifierTest::HandleRequest, |
| 109 base::Unretained(this))); |
| 110 ASSERT_TRUE(embedded_test_server()->Start()); |
106 } | 111 } |
107 | 112 |
108 void TearDownOnMainThread() override { | 113 void TearDownOnMainThread() override { |
109 content::RunAllPendingInMessageLoop(); | 114 content::RunAllPendingInMessageLoop(); |
110 } | 115 } |
111 | 116 |
112 // Helper method to start phishing classification and wait for it to | 117 // Helper method to start phishing classification and wait for it to |
113 // complete. Returns the true if the page is classified as phishy and | 118 // complete. Returns the true if the page is classified as phishy and |
114 // false otherwise. | 119 // false otherwise. |
115 bool RunPhishingClassifier(const base::string16* page_text, | 120 bool RunPhishingClassifier(const base::string16* page_text, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 } | 152 } |
148 | 153 |
149 // Completion callback for classification. | 154 // Completion callback for classification. |
150 void ClassificationFinished(base::RunLoop* run_loop, | 155 void ClassificationFinished(base::RunLoop* run_loop, |
151 ClientPhishingRequest* verdict_out, | 156 ClientPhishingRequest* verdict_out, |
152 const ClientPhishingRequest& verdict) { | 157 const ClientPhishingRequest& verdict) { |
153 *verdict_out = verdict; // Copy the verdict. | 158 *verdict_out = verdict; // Copy the verdict. |
154 run_loop->Quit(); | 159 run_loop->Quit(); |
155 } | 160 } |
156 | 161 |
157 scoped_ptr<net::test_server::EmbeddedTestServer> embedded_test_server_; | |
158 net::test_server::EmbeddedTestServer* embedded_test_server() { | |
159 // TODO(ajwong): Merge this into BrowserTestBase. | |
160 if (!embedded_test_server_) { | |
161 embedded_test_server_.reset(new net::test_server::EmbeddedTestServer()); | |
162 embedded_test_server_->RegisterRequestHandler( | |
163 base::Bind(&PhishingClassifierTest::HandleRequest, | |
164 base::Unretained(this))); | |
165 CHECK(embedded_test_server_->InitializeAndWaitUntilReady()); | |
166 } | |
167 return embedded_test_server_.get(); | |
168 } | |
169 | |
170 void LoadHtml(const std::string& host, const std::string& content) { | 162 void LoadHtml(const std::string& host, const std::string& content) { |
171 GURL::Replacements replace_host; | 163 GURL::Replacements replace_host; |
172 replace_host.SetHostStr(host); | 164 replace_host.SetHostStr(host); |
173 response_content_ = content; | 165 response_content_ = content; |
174 ui_test_utils::NavigateToURL( | 166 ui_test_utils::NavigateToURL( |
175 browser(), | 167 browser(), |
176 embedded_test_server()->base_url().ReplaceComponents(replace_host)); | 168 embedded_test_server()->base_url().ReplaceComponents(replace_host)); |
177 } | 169 } |
178 | 170 |
179 void LoadHtmlPost(const std::string& host, const std::string& content) { | 171 void LoadHtmlPost(const std::string& host, const std::string& content) { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 EXPECT_GE(phishy_score, 0.0); | 248 EXPECT_GE(phishy_score, 0.0); |
257 EXPECT_LT(phishy_score, 0.5); | 249 EXPECT_LT(phishy_score, 0.5); |
258 | 250 |
259 // Extraction should fail for this case since there is no TLD. | 251 // Extraction should fail for this case since there is no TLD. |
260 LoadHtml("localhost", "<html><body>content</body></html>"); | 252 LoadHtml("localhost", "<html><body>content</body></html>"); |
261 EXPECT_FALSE(RunPhishingClassifier(&page_text, &phishy_score, &features)); | 253 EXPECT_FALSE(RunPhishingClassifier(&page_text, &phishy_score, &features)); |
262 EXPECT_EQ(0U, features.features().size()); | 254 EXPECT_EQ(0U, features.features().size()); |
263 EXPECT_EQ(PhishingClassifier::kInvalidScore, phishy_score); | 255 EXPECT_EQ(PhishingClassifier::kInvalidScore, phishy_score); |
264 | 256 |
265 // Extraction should also fail for this case because the URL is not http. | 257 // Extraction should also fail for this case because the URL is not http. |
266 net::SpawnedTestServer https_server( | 258 net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS); |
267 net::SpawnedTestServer::TYPE_HTTPS, | 259 https_server.ServeFilesFromSourceDirectory("chrome/test/data"); |
268 net::SpawnedTestServer::kLocalhost, | |
269 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | |
270 ASSERT_TRUE(https_server.Start()); | 260 ASSERT_TRUE(https_server.Start()); |
271 GURL::Replacements replace_host; | 261 GURL::Replacements replace_host; |
272 replace_host.SetHostStr("host.net"); | 262 replace_host.SetHostStr("host.net"); |
273 GURL test_url = https_server.GetURL("/files/title1.html"); | 263 GURL test_url = https_server.GetURL("/title1.html"); |
274 ui_test_utils::NavigateToURL(browser(), | 264 ui_test_utils::NavigateToURL(browser(), |
275 test_url.ReplaceComponents(replace_host)); | 265 test_url.ReplaceComponents(replace_host)); |
276 EXPECT_FALSE(RunPhishingClassifier(&page_text, &phishy_score, &features)); | 266 EXPECT_FALSE(RunPhishingClassifier(&page_text, &phishy_score, &features)); |
277 EXPECT_EQ(0U, features.features().size()); | 267 EXPECT_EQ(0U, features.features().size()); |
278 EXPECT_EQ(PhishingClassifier::kInvalidScore, phishy_score); | 268 EXPECT_EQ(PhishingClassifier::kInvalidScore, phishy_score); |
279 | 269 |
280 // Extraction should fail for this case because the URL is a POST request. | 270 // Extraction should fail for this case because the URL is a POST request. |
281 LoadHtmlPost("host.net", "<html><body>content</body></html>"); | 271 LoadHtmlPost("host.net", "<html><body>content</body></html>"); |
282 EXPECT_FALSE(RunPhishingClassifier(&page_text, &phishy_score, &features)); | 272 EXPECT_FALSE(RunPhishingClassifier(&page_text, &phishy_score, &features)); |
283 EXPECT_EQ(0U, features.features().size()); | 273 EXPECT_EQ(0U, features.features().size()); |
(...skipping 13 matching lines...) Expand all Loading... |
297 // Now set the scorer. | 287 // Now set the scorer. |
298 classifier_->set_phishing_scorer(scorer_.get()); | 288 classifier_->set_phishing_scorer(scorer_.get()); |
299 EXPECT_TRUE(classifier_->is_ready()); | 289 EXPECT_TRUE(classifier_->is_ready()); |
300 | 290 |
301 // Set a NULL scorer, which turns detection back off. | 291 // Set a NULL scorer, which turns detection back off. |
302 classifier_->set_phishing_scorer(NULL); | 292 classifier_->set_phishing_scorer(NULL); |
303 EXPECT_FALSE(classifier_->is_ready()); | 293 EXPECT_FALSE(classifier_->is_ready()); |
304 } | 294 } |
305 | 295 |
306 } // namespace safe_browsing | 296 } // namespace safe_browsing |
OLD | NEW |