| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/browser/ssl/chrome_security_state_model_client.h" | 5 #include "chrome/browser/ssl/chrome_security_state_model_client.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/test/histogram_tester.h" | 8 #include "base/test/histogram_tester.h" |
| 9 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 9 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 10 #include "components/security_state/security_state_model.h" | 10 #include "components/security_state/security_state_model.h" |
| 11 #include "components/security_state/switches.h" | 11 #include "components/security_state/switches.h" |
| 12 #include "content/public/browser/security_style_explanation.h" | 12 #include "content/public/browser/security_style_explanation.h" |
| 13 #include "content/public/browser/security_style_explanations.h" | 13 #include "content/public/browser/security_style_explanations.h" |
| 14 #include "net/cert/cert_status_flags.h" | 14 #include "net/cert/cert_status_flags.h" |
| 15 #include "net/ssl/ssl_cipher_suite_names.h" | 15 #include "net/ssl/ssl_cipher_suite_names.h" |
| 16 #include "net/ssl/ssl_connection_status_flags.h" | 16 #include "net/ssl/ssl_connection_status_flags.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 const char kHTTPBadHistogram[] = | |
| 22 "Security.HTTPBad.UserWarnedAboutSensitiveInput"; | |
| 23 | |
| 24 // Tests that SecurityInfo flags for subresources with certificate | 21 // Tests that SecurityInfo flags for subresources with certificate |
| 25 // errors are reflected in the SecurityStyleExplanations produced by | 22 // errors are reflected in the SecurityStyleExplanations produced by |
| 26 // ChromeSecurityStateModelClient. | 23 // ChromeSecurityStateModelClient. |
| 27 TEST(ChromeSecurityStateModelClientTest, | 24 TEST(ChromeSecurityStateModelClientTest, |
| 28 GetSecurityStyleForContentWithCertErrors) { | 25 GetSecurityStyleForContentWithCertErrors) { |
| 29 content::SecurityStyleExplanations explanations; | 26 content::SecurityStyleExplanations explanations; |
| 30 security_state::SecurityStateModel::SecurityInfo security_info; | 27 security_state::SecurityStateModel::SecurityInfo security_info; |
| 31 security_info.cert_status = 0; | 28 security_info.cert_status = 0; |
| 32 security_info.scheme_is_cryptographic = true; | 29 security_info.scheme_is_cryptographic = true; |
| 33 | 30 |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 security_state::SecurityStateModel::HTTP_SHOW_WARNING; | 226 security_state::SecurityStateModel::HTTP_SHOW_WARNING; |
| 230 blink::WebSecurityStyle security_style = | 227 blink::WebSecurityStyle security_style = |
| 231 ChromeSecurityStateModelClient::GetSecurityStyle(security_info, | 228 ChromeSecurityStateModelClient::GetSecurityStyle(security_info, |
| 232 &explanations); | 229 &explanations); |
| 233 EXPECT_EQ(blink::WebSecurityStyleUnauthenticated, security_style); | 230 EXPECT_EQ(blink::WebSecurityStyleUnauthenticated, security_style); |
| 234 EXPECT_EQ(1u, explanations.unauthenticated_explanations.size()); | 231 EXPECT_EQ(1u, explanations.unauthenticated_explanations.size()); |
| 235 } | 232 } |
| 236 | 233 |
| 237 // Tests that a security level of NONE when there is a password or | 234 // Tests that a security level of NONE when there is a password or |
| 238 // credit card field on HTTP produces a content::SecurityStyle of | 235 // credit card field on HTTP produces a content::SecurityStyle of |
| 239 // UNAUTHENTICATED, with an info explanation. | 236 // UNAUTHENTICATED, with an info explanation for each. |
| 240 TEST(ChromeSecurityStateModelClientTest, HTTPWarningInFuture) { | 237 TEST(ChromeSecurityStateModelClientTest, HTTPWarningInFuture) { |
| 241 security_state::SecurityStateModel::SecurityInfo security_info; | 238 security_state::SecurityStateModel::SecurityInfo security_info; |
| 242 content::SecurityStyleExplanations explanations; | 239 content::SecurityStyleExplanations explanations; |
| 243 security_info.security_level = security_state::SecurityStateModel::NONE; | 240 security_info.security_level = security_state::SecurityStateModel::NONE; |
| 244 security_info.displayed_private_user_data_input_on_http = true; | 241 security_info.displayed_password_field_on_http = true; |
| 245 blink::WebSecurityStyle security_style = | 242 blink::WebSecurityStyle security_style = |
| 246 ChromeSecurityStateModelClient::GetSecurityStyle(security_info, | 243 ChromeSecurityStateModelClient::GetSecurityStyle(security_info, |
| 247 &explanations); | 244 &explanations); |
| 248 EXPECT_EQ(blink::WebSecurityStyleUnauthenticated, security_style); | 245 EXPECT_EQ(blink::WebSecurityStyleUnauthenticated, security_style); |
| 249 EXPECT_EQ(1u, explanations.info_explanations.size()); | 246 EXPECT_EQ(1u, explanations.info_explanations.size()); |
| 247 |
| 248 explanations.info_explanations.clear(); |
| 249 security_info.displayed_credit_card_field_on_http = true; |
| 250 security_style = ChromeSecurityStateModelClient::GetSecurityStyle( |
| 251 security_info, &explanations); |
| 252 EXPECT_EQ(blink::WebSecurityStyleUnauthenticated, security_style); |
| 253 EXPECT_EQ(1u, explanations.info_explanations.size()); |
| 254 |
| 255 // Check that when both password and credit card fields get displayed, only |
| 256 // one explanation is added. |
| 257 explanations.info_explanations.clear(); |
| 258 security_info.displayed_credit_card_field_on_http = true; |
| 259 security_info.displayed_password_field_on_http = true; |
| 260 security_style = ChromeSecurityStateModelClient::GetSecurityStyle( |
| 261 security_info, &explanations); |
| 262 EXPECT_EQ(blink::WebSecurityStyleUnauthenticated, security_style); |
| 263 EXPECT_EQ(1u, explanations.info_explanations.size()); |
| 250 } | 264 } |
| 251 | 265 |
| 252 class ChromeSecurityStateModelClientHistogramTest | 266 class ChromeSecurityStateModelClientHistogramTest |
| 253 : public ChromeRenderViewHostTestHarness { | 267 : public ChromeRenderViewHostTestHarness, |
| 268 public testing::WithParamInterface<bool> { |
| 254 public: | 269 public: |
| 255 ChromeSecurityStateModelClientHistogramTest() {} | 270 ChromeSecurityStateModelClientHistogramTest() {} |
| 256 ~ChromeSecurityStateModelClientHistogramTest() override {} | 271 ~ChromeSecurityStateModelClientHistogramTest() override {} |
| 257 | 272 |
| 258 void SetUp() override { | 273 void SetUp() override { |
| 259 ChromeRenderViewHostTestHarness::SetUp(); | 274 ChromeRenderViewHostTestHarness::SetUp(); |
| 260 | 275 |
| 261 ChromeSecurityStateModelClient::CreateForWebContents(web_contents()); | 276 ChromeSecurityStateModelClient::CreateForWebContents(web_contents()); |
| 262 client_ = ChromeSecurityStateModelClient::FromWebContents(web_contents()); | 277 client_ = ChromeSecurityStateModelClient::FromWebContents(web_contents()); |
| 263 navigate_to_http(); | 278 navigate_to_http(); |
| 264 } | 279 } |
| 265 | 280 |
| 266 protected: | 281 protected: |
| 267 ChromeSecurityStateModelClient* client() { return client_; } | 282 ChromeSecurityStateModelClient* client() { return client_; } |
| 268 | 283 |
| 269 void signal_password() { | 284 void signal_sensitive_input() { |
| 270 web_contents()->OnPasswordInputShownOnHttp(); | 285 if (GetParam()) |
| 286 web_contents()->OnPasswordInputShownOnHttp(); |
| 287 else |
| 288 web_contents()->OnCreditCardInputShownOnHttp(); |
| 271 client_->VisibleSecurityStateChanged(); | 289 client_->VisibleSecurityStateChanged(); |
| 272 } | 290 } |
| 273 | 291 |
| 292 const std::string histogram_name() { |
| 293 if (GetParam()) |
| 294 return "Security.HTTPBad.UserWarnedAboutSensitiveInput.Password"; |
| 295 else |
| 296 return "Security.HTTPBad.UserWarnedAboutSensitiveInput.CreditCard"; |
| 297 } |
| 298 |
| 274 void navigate_to_http() { NavigateAndCommit(GURL("http://example.test")); } | 299 void navigate_to_http() { NavigateAndCommit(GURL("http://example.test")); } |
| 275 | 300 |
| 276 void navigate_to_different_http_page() { | 301 void navigate_to_different_http_page() { |
| 277 NavigateAndCommit(GURL("http://example2.test")); | 302 NavigateAndCommit(GURL("http://example2.test")); |
| 278 } | 303 } |
| 279 | 304 |
| 280 private: | 305 private: |
| 281 ChromeSecurityStateModelClient* client_; | 306 ChromeSecurityStateModelClient* client_; |
| 282 DISALLOW_COPY_AND_ASSIGN(ChromeSecurityStateModelClientHistogramTest); | 307 DISALLOW_COPY_AND_ASSIGN(ChromeSecurityStateModelClientHistogramTest); |
| 283 }; | 308 }; |
| 284 | 309 |
| 285 // Tests that UMA logs the omnibox warning when security level is | 310 // Tests that UMA logs the omnibox warning when security level is |
| 286 // HTTP_SHOW_WARNING. | 311 // HTTP_SHOW_WARNING. |
| 287 TEST_F(ChromeSecurityStateModelClientHistogramTest, | 312 TEST_P(ChromeSecurityStateModelClientHistogramTest, |
| 288 HTTPOmniboxWarningHistogram) { | 313 HTTPOmniboxWarningHistogram) { |
| 289 // Show Warning Chip. | 314 // Show Warning Chip. |
| 290 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 315 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 291 security_state::switches::kMarkHttpAs, | 316 security_state::switches::kMarkHttpAs, |
| 292 security_state::switches::kMarkHttpWithPasswordsOrCcWithChip); | 317 security_state::switches::kMarkHttpWithPasswordsOrCcWithChip); |
| 293 | 318 |
| 294 base::HistogramTester histograms; | 319 base::HistogramTester histograms; |
| 295 signal_password(); | 320 signal_sensitive_input(); |
| 296 histograms.ExpectUniqueSample(kHTTPBadHistogram, true, 1); | 321 histograms.ExpectUniqueSample(histogram_name(), true, 1); |
| 297 | 322 |
| 298 // Fire again and ensure no sample is recorded. | 323 // Fire again and ensure no sample is recorded. |
| 299 signal_password(); | 324 signal_sensitive_input(); |
| 300 histograms.ExpectUniqueSample(kHTTPBadHistogram, true, 1); | 325 histograms.ExpectUniqueSample(histogram_name(), true, 1); |
| 301 | 326 |
| 302 // Navigate to a new page and ensure a sample is recorded. | 327 // Navigate to a new page and ensure a sample is recorded. |
| 303 navigate_to_different_http_page(); | 328 navigate_to_different_http_page(); |
| 304 histograms.ExpectUniqueSample(kHTTPBadHistogram, true, 1); | 329 histograms.ExpectUniqueSample(histogram_name(), true, 1); |
| 305 signal_password(); | 330 signal_sensitive_input(); |
| 306 histograms.ExpectUniqueSample(kHTTPBadHistogram, true, 2); | 331 histograms.ExpectUniqueSample(histogram_name(), true, 2); |
| 307 } | 332 } |
| 308 | 333 |
| 309 // Tests that UMA logs the console warning when security level is NONE. | 334 // Tests that UMA logs the console warning when security level is NONE. |
| 310 TEST_F(ChromeSecurityStateModelClientHistogramTest, | 335 TEST_P(ChromeSecurityStateModelClientHistogramTest, |
| 311 HTTPConsoleWarningHistogram) { | 336 HTTPConsoleWarningHistogram) { |
| 312 // Show Neutral for HTTP | 337 // Show Neutral for HTTP |
| 313 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 338 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 314 security_state::switches::kMarkHttpAs, | 339 security_state::switches::kMarkHttpAs, |
| 315 security_state::switches::kMarkHttpAsNeutral); | 340 security_state::switches::kMarkHttpAsNeutral); |
| 316 | 341 |
| 317 base::HistogramTester histograms; | 342 base::HistogramTester histograms; |
| 318 signal_password(); | 343 signal_sensitive_input(); |
| 319 histograms.ExpectUniqueSample(kHTTPBadHistogram, false, 1); | 344 histograms.ExpectUniqueSample(histogram_name(), false, 1); |
| 320 | 345 |
| 321 // Fire again and ensure no sample is recorded. | 346 // Fire again and ensure no sample is recorded. |
| 322 signal_password(); | 347 signal_sensitive_input(); |
| 323 histograms.ExpectUniqueSample(kHTTPBadHistogram, false, 1); | 348 histograms.ExpectUniqueSample(histogram_name(), false, 1); |
| 324 | 349 |
| 325 // Navigate to a new page and ensure a sample is recorded. | 350 // Navigate to a new page and ensure a sample is recorded. |
| 326 navigate_to_different_http_page(); | 351 navigate_to_different_http_page(); |
| 327 histograms.ExpectUniqueSample(kHTTPBadHistogram, false, 1); | 352 histograms.ExpectUniqueSample(histogram_name(), false, 1); |
| 328 signal_password(); | 353 signal_sensitive_input(); |
| 329 histograms.ExpectUniqueSample(kHTTPBadHistogram, false, 2); | 354 histograms.ExpectUniqueSample(histogram_name(), false, 2); |
| 330 } | 355 } |
| 331 | 356 |
| 357 INSTANTIATE_TEST_CASE_P(ChromeSecurityStateModelClientHistogramTest, |
| 358 ChromeSecurityStateModelClientHistogramTest, |
| 359 // Here 'true' to test password field triggered |
| 360 // histogram and 'false' to test credit card field. |
| 361 testing::Bool()); |
| 362 |
| 332 } // namespace | 363 } // namespace |
| OLD | NEW |