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 // Note that although this is not a "browser" test, it runs as part of | 5 // Note that although this is not a "browser" test, it runs as part of |
6 // browser_tests. This is because WebKit does not work properly if it is | 6 // browser_tests. This is because WebKit does not work properly if it is |
7 // shutdown and re-initialized. Since browser_tests runs each test in a | 7 // shutdown and re-initialized. Since browser_tests runs each test in a |
8 // new process, this avoids the problem. | 8 // new process, this avoids the problem. |
9 | 9 |
10 #include "chrome/renderer/safe_browsing/phishing_dom_feature_extractor.h" | 10 #include "chrome/renderer/safe_browsing/phishing_dom_feature_extractor.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 void RemoveIframe() { | 127 void RemoveIframe() { |
128 blink::WebFrame* main_frame = GetWebFrame(); | 128 blink::WebFrame* main_frame = GetWebFrame(); |
129 ASSERT_TRUE(main_frame); | 129 ASSERT_TRUE(main_frame); |
130 main_frame->executeScript( | 130 main_frame->executeScript( |
131 blink::WebString( | 131 blink::WebString( |
132 "document.body.removeChild(document.getElementById('frame1'));")); | 132 "document.body.removeChild(document.getElementById('frame1'));")); |
133 } | 133 } |
134 | 134 |
135 bool StartTestServer() { | 135 bool StartTestServer() { |
136 CHECK(!embedded_test_server_); | 136 CHECK(!embedded_test_server_); |
137 embedded_test_server_.reset(new net::test_server::EmbeddedTestServer()); | 137 embedded_test_server_.reset(new net::EmbeddedTestServer()); |
138 embedded_test_server_->RegisterRequestHandler( | 138 embedded_test_server_->RegisterRequestHandler( |
139 base::Bind(&PhishingDOMFeatureExtractorTest::HandleRequest, | 139 base::Bind(&PhishingDOMFeatureExtractorTest::HandleRequest, |
140 base::Unretained(this))); | 140 base::Unretained(this))); |
141 return embedded_test_server_->InitializeAndWaitUntilReady(); | 141 return embedded_test_server_->Start(); |
142 } | 142 } |
143 | 143 |
144 scoped_ptr<net::test_server::HttpResponse> HandleRequest( | 144 scoped_ptr<net::test_server::HttpResponse> HandleRequest( |
145 const net::test_server::HttpRequest& request) { | 145 const net::test_server::HttpRequest& request) { |
146 std::map<std::string, std::string>::const_iterator host_it = | 146 std::map<std::string, std::string>::const_iterator host_it = |
147 request.headers.find("Host"); | 147 request.headers.find("Host"); |
148 if (host_it == request.headers.end()) | 148 if (host_it == request.headers.end()) |
149 return scoped_ptr<net::test_server::HttpResponse>(); | 149 return scoped_ptr<net::test_server::HttpResponse>(); |
150 | 150 |
151 std::string url = | 151 std::string url = |
(...skipping 25 matching lines...) Expand all Loading... | |
177 ui_test_utils::NavigateToURL(browser(), url); | 177 ui_test_utils::NavigateToURL(browser(), url); |
178 return url; | 178 return url; |
179 } | 179 } |
180 | 180 |
181 int32 render_view_routing_id_; | 181 int32 render_view_routing_id_; |
182 | 182 |
183 // Map of url -> response body for network requests from the renderer. | 183 // Map of url -> response body for network requests from the renderer. |
184 // Any urls not in this map are served a 404 error. | 184 // Any urls not in this map are served a 404 error. |
185 std::map<std::string, std::string> responses_; | 185 std::map<std::string, std::string> responses_; |
186 | 186 |
187 scoped_ptr<net::test_server::EmbeddedTestServer> embedded_test_server_; | 187 scoped_ptr<net::EmbeddedTestServer> embedded_test_server_; |
188 MockFeatureExtractorClock clock_; | 188 MockFeatureExtractorClock clock_; |
189 scoped_ptr<PhishingDOMFeatureExtractor> extractor_; | 189 scoped_ptr<PhishingDOMFeatureExtractor> extractor_; |
190 bool success_; // holds the success value from ExtractFeatures | 190 bool success_; // holds the success value from ExtractFeatures |
191 base::WeakPtrFactory<PhishingDOMFeatureExtractorTest> weak_factory_; | 191 base::WeakPtrFactory<PhishingDOMFeatureExtractorTest> weak_factory_; |
192 }; | 192 }; |
193 | 193 |
194 IN_PROC_BROWSER_TEST_F(PhishingDOMFeatureExtractorTest, FormFeatures) { | 194 IN_PROC_BROWSER_TEST_F(PhishingDOMFeatureExtractorTest, FormFeatures) { |
195 // This test doesn't exercise the extraction timing. | 195 // This test doesn't exercise the extraction timing. |
196 EXPECT_CALL(clock_, Now()).WillRepeatedly(Return(base::TimeTicks::Now())); | 196 EXPECT_CALL(clock_, Now()).WillRepeatedly(Return(base::TimeTicks::Now())); |
197 | 197 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
274 "</body></html"); | 274 "</body></html"); |
275 ASSERT_TRUE(ExtractFeatures(&features)); | 275 ASSERT_TRUE(ExtractFeatures(&features)); |
276 ExpectFeatureMapsAreEqual(features, expected_features); | 276 ExpectFeatureMapsAreEqual(features, expected_features); |
277 | 277 |
278 expected_features.Clear(); | 278 expected_features.Clear(); |
279 expected_features.AddRealFeature(features::kPageExternalLinksFreq, 0.25); | 279 expected_features.AddRealFeature(features::kPageExternalLinksFreq, 0.25); |
280 expected_features.AddRealFeature(features::kPageSecureLinksFreq, 0.5); | 280 expected_features.AddRealFeature(features::kPageSecureLinksFreq, 0.5); |
281 expected_features.AddBooleanFeature(features::kPageLinkDomain + | 281 expected_features.AddBooleanFeature(features::kPageLinkDomain + |
282 std::string("chromium.org")); | 282 std::string("chromium.org")); |
283 | 283 |
284 net::SpawnedTestServer https_server( | 284 net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS); |
285 net::SpawnedTestServer::TYPE_HTTPS, | 285 https_server.ServeFilesFromSourceDirectory("chrome/test/data"); |
286 net::SpawnedTestServer::kLocalhost, | |
287 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | |
288 ASSERT_TRUE(https_server.Start()); | 286 ASSERT_TRUE(https_server.Start()); |
289 | 287 |
290 // The PhishingDOMFeatureExtractor depends on URLs being domains and not IPs, | 288 // The PhishingDOMFeatureExtractor depends on URLs being domains and not IPs, |
291 // so use a domain. | 289 // so use a domain. |
292 std::string url_str = "https://host.com:"; | 290 std::string url_str = "https://host.com:"; |
293 url_str += base::UintToString(https_server.host_port_pair().port()); | 291 url_str += base::UintToString(https_server.host_port_pair().port()); |
294 url_str += "/files/safe_browsing/secure_link_features.html"; | 292 url_str += "/safe_browsing/secure_link_features.html"; |
mmenke
2015/11/05 17:38:13
Not a big fan of manually manupulating URLs if we
svaldez
2015/11/05 18:15:55
Done.
| |
295 ui_test_utils::NavigateToURL(browser(), GURL(url_str)); | 293 ui_test_utils::NavigateToURL(browser(), GURL(url_str)); |
296 | 294 |
297 // Click through the certificate error interstitial. | 295 // Click through the certificate error interstitial. |
298 content::InterstitialPage* interstitial_page = | 296 content::InterstitialPage* interstitial_page = |
299 GetWebContents()->GetInterstitialPage(); | 297 GetWebContents()->GetInterstitialPage(); |
300 interstitial_page->Proceed(); | 298 interstitial_page->Proceed(); |
301 content::WaitForLoadStop(GetWebContents()); | 299 content::WaitForLoadStop(GetWebContents()); |
302 | 300 |
303 features.Clear(); | 301 features.Clear(); |
304 ASSERT_TRUE(ExtractFeatures(&features)); | 302 ASSERT_TRUE(ExtractFeatures(&features)); |
(...skipping 20 matching lines...) Expand all Loading... | |
325 "<html><head><script></script><script></script></head></html>"); | 323 "<html><head><script></script><script></script></head></html>"); |
326 ASSERT_TRUE(ExtractFeatures(&features)); | 324 ASSERT_TRUE(ExtractFeatures(&features)); |
327 ExpectFeatureMapsAreEqual(features, expected_features); | 325 ExpectFeatureMapsAreEqual(features, expected_features); |
328 | 326 |
329 expected_features.Clear(); | 327 expected_features.Clear(); |
330 expected_features.AddBooleanFeature(features::kPageNumScriptTagsGTOne); | 328 expected_features.AddBooleanFeature(features::kPageNumScriptTagsGTOne); |
331 expected_features.AddBooleanFeature(features::kPageNumScriptTagsGTSix); | 329 expected_features.AddBooleanFeature(features::kPageNumScriptTagsGTSix); |
332 expected_features.AddRealFeature(features::kPageImgOtherDomainFreq, 0.5); | 330 expected_features.AddRealFeature(features::kPageImgOtherDomainFreq, 0.5); |
333 | 331 |
334 features.Clear(); | 332 features.Clear(); |
335 net::SpawnedTestServer https_server( | 333 net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS); |
336 net::SpawnedTestServer::TYPE_HTTPS, | 334 https_server.ServeFilesFromSourceDirectory("chrome/test/data"); |
337 net::SpawnedTestServer::kLocalhost, | |
338 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | |
339 ASSERT_TRUE(https_server.Start()); | 335 ASSERT_TRUE(https_server.Start()); |
340 | 336 |
341 // The PhishingDOMFeatureExtractor depends on URLs being domains and not IPs, | 337 // The PhishingDOMFeatureExtractor depends on URLs being domains and not IPs, |
342 // so use a domain. | 338 // so use a domain. |
343 std::string url_str = "https://host.com:"; | 339 std::string url_str = "https://host.com:"; |
344 url_str += base::UintToString(https_server.host_port_pair().port()); | 340 url_str += base::UintToString(https_server.host_port_pair().port()); |
345 url_str += "/files/safe_browsing/secure_script_and_image.html"; | 341 url_str += "/safe_browsing/secure_script_and_image.html"; |
346 ui_test_utils::NavigateToURL(browser(), GURL(url_str)); | 342 ui_test_utils::NavigateToURL(browser(), GURL(url_str)); |
347 | 343 |
348 // Click through the certificate error interstitial. | 344 // Click through the certificate error interstitial. |
349 content::InterstitialPage* interstitial_page = | 345 content::InterstitialPage* interstitial_page = |
350 GetWebContents()->GetInterstitialPage(); | 346 GetWebContents()->GetInterstitialPage(); |
351 interstitial_page->Proceed(); | 347 interstitial_page->Proceed(); |
352 content::WaitForLoadStop(GetWebContents()); | 348 content::WaitForLoadStop(GetWebContents()); |
353 | 349 |
354 ASSERT_TRUE(ExtractFeatures(&features)); | 350 ASSERT_TRUE(ExtractFeatures(&features)); |
355 ExpectFeatureMapsAreEqual(features, expected_features); | 351 ExpectFeatureMapsAreEqual(features, expected_features); |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
547 LoadHtml( | 543 LoadHtml( |
548 "host.com", | 544 "host.com", |
549 "<html><head></head><body>" | 545 "<html><head></head><body>" |
550 "<iframe src=\"frame.html\" id=\"frame1\"></iframe>" | 546 "<iframe src=\"frame.html\" id=\"frame1\"></iframe>" |
551 "<form></form></body></html>"); | 547 "<form></form></body></html>"); |
552 ASSERT_TRUE(ExtractFeatures(&features)); | 548 ASSERT_TRUE(ExtractFeatures(&features)); |
553 ExpectFeatureMapsAreEqual(features, expected_features); | 549 ExpectFeatureMapsAreEqual(features, expected_features); |
554 } | 550 } |
555 | 551 |
556 } // namespace safe_browsing | 552 } // namespace safe_browsing |
OLD | NEW |