Chromium Code Reviews| 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(); |
|
mmenke
2015/11/05 18:21:03
Per comment in other file, can also switch this to
svaldez
2015/11/05 18:34:51
Done.
| |
| 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 GURL url = https_server.GetURL("/safe_browsing/secure_link_features.html"); |
| 293 url_str += base::UintToString(https_server.host_port_pair().port()); | 291 GURL::Replacements replace_host; |
| 294 url_str += "/files/safe_browsing/secure_link_features.html"; | 292 replace_host.SetHostStr("host.com"); |
| 295 ui_test_utils::NavigateToURL(browser(), GURL(url_str)); | 293 ui_test_utils::NavigateToURL(browser(), url.ReplaceComponents(replace_host)); |
| 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)); |
| 305 ExpectFeatureMapsAreEqual(features, expected_features); | 303 ExpectFeatureMapsAreEqual(features, expected_features); |
| (...skipping 19 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()); |
|
mmenke
2015/11/05 18:21:03
Again, use GURL::Replacements.
svaldez
2015/11/05 18:34:51
Done.
| |
| 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); |
| 356 } | 352 } |
| 357 | 353 |
| 358 IN_PROC_BROWSER_TEST_F(PhishingDOMFeatureExtractorTest, SubFrames) { | 354 IN_PROC_BROWSER_TEST_F(PhishingDOMFeatureExtractorTest, SubFrames) { |
| 359 // This test doesn't exercise the extraction timing. | 355 // This test doesn't exercise the extraction timing. |
| 360 EXPECT_CALL(clock_, Now()).WillRepeatedly(Return(base::TimeTicks::Now())); | 356 EXPECT_CALL(clock_, Now()).WillRepeatedly(Return(base::TimeTicks::Now())); |
| 361 | 357 |
| 362 // Test that features are aggregated across all frames. | 358 // Test that features are aggregated across all frames. |
| 363 | 359 |
| 364 std::string port = base::UintToString(embedded_test_server_->port()); | 360 std::string port = base::UintToString(embedded_test_server_->port()); |
|
mmenke
2015/11/05 18:21:03
Use GURL::Replacements.
svaldez
2015/11/05 18:34:51
Done.
| |
| 365 responses_[GetURL("host2.com", "").spec()] = | 361 responses_[GetURL("host2.com", "").spec()] = |
| 366 "<html><head><script></script><body>" | 362 "<html><head><script></script><body>" |
| 367 "<form action=\"http://host4.com/\"><input type=checkbox></form>" | 363 "<form action=\"http://host4.com/\"><input type=checkbox></form>" |
| 368 "<form action=\"http://host2.com/submit\"></form>" | 364 "<form action=\"http://host2.com/submit\"></form>" |
| 369 "<a href=\"http://www.host2.com/home\">link</a>" | 365 "<a href=\"http://www.host2.com/home\">link</a>" |
| 370 "<iframe src=\"nested.html\"></iframe>" | 366 "<iframe src=\"nested.html\"></iframe>" |
| 371 "<body></html>"; | 367 "<body></html>"; |
| 372 | 368 |
| 373 responses_[GetURL("host2.com", "nested.html").spec()] = | 369 responses_[GetURL("host2.com", "nested.html").spec()] = |
| 374 "<html><body><input type=password>" | 370 "<html><body><input type=password>" |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 468 // Time check after the last 10 elements. | 464 // Time check after the last 10 elements. |
| 469 .WillOnce(Return(now + base::TimeDelta::FromMilliseconds(54))) | 465 .WillOnce(Return(now + base::TimeDelta::FromMilliseconds(54))) |
| 470 // A final time check for the histograms. | 466 // A final time check for the histograms. |
| 471 .WillOnce(Return(now + base::TimeDelta::FromMilliseconds(56))); | 467 .WillOnce(Return(now + base::TimeDelta::FromMilliseconds(56))); |
| 472 | 468 |
| 473 FeatureMap expected_features; | 469 FeatureMap expected_features; |
| 474 expected_features.AddBooleanFeature(features::kPageHasForms); | 470 expected_features.AddBooleanFeature(features::kPageHasForms); |
| 475 expected_features.AddRealFeature(features::kPageActionOtherDomainFreq, 0.5); | 471 expected_features.AddRealFeature(features::kPageActionOtherDomainFreq, 0.5); |
| 476 expected_features.AddBooleanFeature(features::kPageActionURL + | 472 expected_features.AddBooleanFeature(features::kPageActionURL + |
| 477 std::string("http://host.com:") + | 473 std::string("http://host.com:") + |
| 478 base::UintToString(embedded_test_server_->port()) + | 474 base::UintToString(embedded_test_server_->port()) + |
|
mmenke
2015/11/05 18:21:03
Use GURL::Replacements.
svaldez
2015/11/05 18:34:51
Done.
| |
| 479 std::string("/ondomain")); | 475 std::string("/ondomain")); |
| 480 expected_features.AddBooleanFeature(features::kPageActionURL + | 476 expected_features.AddBooleanFeature(features::kPageActionURL + |
| 481 std::string("http://host2.com/")); | 477 std::string("http://host2.com/")); |
| 482 | 478 |
| 483 FeatureMap features; | 479 FeatureMap features; |
| 484 LoadHtml("host.com", response); | 480 LoadHtml("host.com", response); |
| 485 ASSERT_TRUE(ExtractFeatures(&features)); | 481 ASSERT_TRUE(ExtractFeatures(&features)); |
| 486 ExpectFeatureMapsAreEqual(features, expected_features); | 482 ExpectFeatureMapsAreEqual(features, expected_features); |
| 487 // Make sure none of the mock expectations carry over to the next test. | 483 // Make sure none of the mock expectations carry over to the next test. |
| 488 ::testing::Mock::VerifyAndClearExpectations(&clock_); | 484 ::testing::Mock::VerifyAndClearExpectations(&clock_); |
| (...skipping 58 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 |