| 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/security_state_tab_helper.h" | 5 #include "chrome/browser/ssl/security_state_tab_helper.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/core/switches.h" | 10 #include "components/security_state/core/switches.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 const char kHTTPBadNavigationHistogram[] = |
| 16 "Security.HTTPBad.NavigationStartedAfterUserWarnedAboutSensitiveInput"; |
| 17 const char kHTTPBadWebContentsDestroyedHistogram[] = |
| 18 "Security.HTTPBad.WebContentsDestroyedAfterUserWarnedAboutSensitiveInput"; |
| 19 |
| 15 class SecurityStateTabHelperHistogramTest | 20 class SecurityStateTabHelperHistogramTest |
| 16 : public ChromeRenderViewHostTestHarness, | 21 : public ChromeRenderViewHostTestHarness, |
| 17 public testing::WithParamInterface<bool> { | 22 public testing::WithParamInterface<bool> { |
| 18 public: | 23 public: |
| 19 SecurityStateTabHelperHistogramTest() : helper_(nullptr) {} | 24 SecurityStateTabHelperHistogramTest() : helper_(nullptr) {} |
| 20 ~SecurityStateTabHelperHistogramTest() override {} | 25 ~SecurityStateTabHelperHistogramTest() override {} |
| 21 | 26 |
| 22 void SetUp() override { | 27 void SetUp() override { |
| 23 ChromeRenderViewHostTestHarness::SetUp(); | 28 ChromeRenderViewHostTestHarness::SetUp(); |
| 24 | 29 |
| 25 SecurityStateTabHelper::CreateForWebContents(web_contents()); | 30 SecurityStateTabHelper::CreateForWebContents(web_contents()); |
| 26 helper_ = SecurityStateTabHelper::FromWebContents(web_contents()); | 31 helper_ = SecurityStateTabHelper::FromWebContents(web_contents()); |
| 27 navigate_to_http(); | 32 NavigateToHTTP(); |
| 28 } | 33 } |
| 29 | 34 |
| 30 protected: | 35 protected: |
| 31 void signal_sensitive_input() { | 36 void SignalSensitiveInput() { |
| 32 if (GetParam()) | 37 if (GetParam()) |
| 33 web_contents()->OnPasswordInputShownOnHttp(); | 38 web_contents()->OnPasswordInputShownOnHttp(); |
| 34 else | 39 else |
| 35 web_contents()->OnCreditCardInputShownOnHttp(); | 40 web_contents()->OnCreditCardInputShownOnHttp(); |
| 36 helper_->VisibleSecurityStateChanged(); | 41 helper_->VisibleSecurityStateChanged(); |
| 37 } | 42 } |
| 38 | 43 |
| 39 const std::string histogram_name() { | 44 const std::string HistogramName() { |
| 40 if (GetParam()) | 45 if (GetParam()) |
| 41 return "Security.HTTPBad.UserWarnedAboutSensitiveInput.Password"; | 46 return "Security.HTTPBad.UserWarnedAboutSensitiveInput.Password"; |
| 42 else | 47 else |
| 43 return "Security.HTTPBad.UserWarnedAboutSensitiveInput.CreditCard"; | 48 return "Security.HTTPBad.UserWarnedAboutSensitiveInput.CreditCard"; |
| 44 } | 49 } |
| 45 | 50 |
| 46 void navigate_to_http() { NavigateAndCommit(GURL("http://example.test")); } | 51 void NavigateToHTTP() { NavigateAndCommit(GURL("http://example.test")); } |
| 47 | 52 |
| 48 void navigate_to_different_http_page() { | 53 void NavigateToDifferentHTTPPage() { |
| 49 NavigateAndCommit(GURL("http://example2.test")); | 54 NavigateAndCommit(GURL("http://example2.test")); |
| 50 } | 55 } |
| 51 | 56 |
| 52 private: | 57 private: |
| 53 SecurityStateTabHelper* helper_; | 58 SecurityStateTabHelper* helper_; |
| 54 DISALLOW_COPY_AND_ASSIGN(SecurityStateTabHelperHistogramTest); | 59 DISALLOW_COPY_AND_ASSIGN(SecurityStateTabHelperHistogramTest); |
| 55 }; | 60 }; |
| 56 | 61 |
| 62 // Tests that an UMA histogram is recorded after setting the security |
| 63 // level to HTTP_SHOW_WARNING and navigating away. |
| 64 TEST_P(SecurityStateTabHelperHistogramTest, |
| 65 HTTPOmniboxWarningNavigationHistogram) { |
| 66 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 67 security_state::switches::kMarkHttpAs, |
| 68 security_state::switches::kMarkHttpWithPasswordsOrCcWithChip); |
| 69 |
| 70 base::HistogramTester histograms; |
| 71 SignalSensitiveInput(); |
| 72 // Make sure that if the omnibox warning gets dynamically hidden, the |
| 73 // histogram still gets recorded. |
| 74 NavigateToDifferentHTTPPage(); |
| 75 if (GetParam()) |
| 76 web_contents()->OnAllPasswordInputsHiddenOnHttp(); |
| 77 // Destroy the WebContents to simulate the tab being closed after a |
| 78 // navigation. |
| 79 SetContents(nullptr); |
| 80 histograms.ExpectTotalCount(kHTTPBadNavigationHistogram, 1); |
| 81 histograms.ExpectTotalCount(kHTTPBadWebContentsDestroyedHistogram, 0); |
| 82 } |
| 83 |
| 84 // Tests that an UMA histogram is recorded after showing a console |
| 85 // warning for a sensitive input on HTTP and navigating away. |
| 86 TEST_P(SecurityStateTabHelperHistogramTest, |
| 87 HTTPConsoleWarningNavigationHistogram) { |
| 88 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 89 security_state::switches::kMarkHttpAs, |
| 90 security_state::switches::kMarkHttpAsNeutral); |
| 91 |
| 92 // Same as HTTPOmniboxWarningNavigationHistogram, but ensuring that |
| 93 // the histogram gets recorded even if the command-line switch to show |
| 94 // the omnibox warning is not set. |
| 95 base::HistogramTester histograms; |
| 96 SignalSensitiveInput(); |
| 97 NavigateToDifferentHTTPPage(); |
| 98 // Destroy the WebContents to simulate the tab being closed after a |
| 99 // navigation. |
| 100 SetContents(nullptr); |
| 101 histograms.ExpectTotalCount(kHTTPBadNavigationHistogram, 1); |
| 102 histograms.ExpectTotalCount(kHTTPBadWebContentsDestroyedHistogram, 0); |
| 103 } |
| 104 |
| 105 // Tests that an UMA histogram is recorded after setting the security |
| 106 // level to HTTP_SHOW_WARNING and closing the tab. |
| 107 TEST_P(SecurityStateTabHelperHistogramTest, |
| 108 HTTPOmniboxWarningTabClosedHistogram) { |
| 109 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 110 security_state::switches::kMarkHttpAs, |
| 111 security_state::switches::kMarkHttpWithPasswordsOrCcWithChip); |
| 112 |
| 113 base::HistogramTester histograms; |
| 114 SignalSensitiveInput(); |
| 115 // Destroy the WebContents to simulate the tab being closed. |
| 116 SetContents(nullptr); |
| 117 histograms.ExpectTotalCount(kHTTPBadNavigationHistogram, 0); |
| 118 histograms.ExpectTotalCount(kHTTPBadWebContentsDestroyedHistogram, 1); |
| 119 } |
| 120 |
| 121 // Tests that an UMA histogram is recorded after showing a console |
| 122 // warning for a sensitive input on HTTP and closing the tab. |
| 123 TEST_P(SecurityStateTabHelperHistogramTest, |
| 124 HTTPConsoleWarningTabClosedHistogram) { |
| 125 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 126 security_state::switches::kMarkHttpAs, |
| 127 security_state::switches::kMarkHttpAsNeutral); |
| 128 |
| 129 // Same as HTTPOmniboxWarningTabClosedHistogram, but ensuring that the |
| 130 // histogram gets recorded even if the command-line switch to show the |
| 131 // omnibox warning is not set. |
| 132 base::HistogramTester histograms; |
| 133 SignalSensitiveInput(); |
| 134 // Destroy the WebContents to simulate the tab being closed. |
| 135 SetContents(nullptr); |
| 136 histograms.ExpectTotalCount(kHTTPBadNavigationHistogram, 0); |
| 137 histograms.ExpectTotalCount(kHTTPBadWebContentsDestroyedHistogram, 1); |
| 138 } |
| 139 |
| 57 // Tests that UMA logs the omnibox warning when security level is | 140 // Tests that UMA logs the omnibox warning when security level is |
| 58 // HTTP_SHOW_WARNING. | 141 // HTTP_SHOW_WARNING. |
| 59 TEST_P(SecurityStateTabHelperHistogramTest, HTTPOmniboxWarningHistogram) { | 142 TEST_P(SecurityStateTabHelperHistogramTest, HTTPOmniboxWarningHistogram) { |
| 60 // Show Warning Chip. | 143 // Show Warning Chip. |
| 61 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 144 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 62 security_state::switches::kMarkHttpAs, | 145 security_state::switches::kMarkHttpAs, |
| 63 security_state::switches::kMarkHttpWithPasswordsOrCcWithChip); | 146 security_state::switches::kMarkHttpWithPasswordsOrCcWithChip); |
| 64 | 147 |
| 65 base::HistogramTester histograms; | 148 base::HistogramTester histograms; |
| 66 signal_sensitive_input(); | 149 SignalSensitiveInput(); |
| 67 histograms.ExpectUniqueSample(histogram_name(), true, 1); | 150 histograms.ExpectUniqueSample(HistogramName(), true, 1); |
| 68 | 151 |
| 69 // Fire again and ensure no sample is recorded. | 152 // Fire again and ensure no sample is recorded. |
| 70 signal_sensitive_input(); | 153 SignalSensitiveInput(); |
| 71 histograms.ExpectUniqueSample(histogram_name(), true, 1); | 154 histograms.ExpectUniqueSample(HistogramName(), true, 1); |
| 72 | 155 |
| 73 // Navigate to a new page and ensure a sample is recorded. | 156 // Navigate to a new page and ensure a sample is recorded. |
| 74 navigate_to_different_http_page(); | 157 NavigateToDifferentHTTPPage(); |
| 75 histograms.ExpectUniqueSample(histogram_name(), true, 1); | 158 histograms.ExpectUniqueSample(HistogramName(), true, 1); |
| 76 signal_sensitive_input(); | 159 SignalSensitiveInput(); |
| 77 histograms.ExpectUniqueSample(histogram_name(), true, 2); | 160 histograms.ExpectUniqueSample(HistogramName(), true, 2); |
| 78 } | 161 } |
| 79 | 162 |
| 80 // Tests that UMA logs the console warning when security level is NONE. | 163 // Tests that UMA logs the console warning when security level is NONE. |
| 81 TEST_P(SecurityStateTabHelperHistogramTest, HTTPConsoleWarningHistogram) { | 164 TEST_P(SecurityStateTabHelperHistogramTest, HTTPConsoleWarningHistogram) { |
| 82 // Show Neutral for HTTP | 165 // Show Neutral for HTTP |
| 83 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 166 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 84 security_state::switches::kMarkHttpAs, | 167 security_state::switches::kMarkHttpAs, |
| 85 security_state::switches::kMarkHttpAsNeutral); | 168 security_state::switches::kMarkHttpAsNeutral); |
| 86 | 169 |
| 87 base::HistogramTester histograms; | 170 base::HistogramTester histograms; |
| 88 signal_sensitive_input(); | 171 SignalSensitiveInput(); |
| 89 histograms.ExpectUniqueSample(histogram_name(), false, 1); | 172 histograms.ExpectUniqueSample(HistogramName(), false, 1); |
| 90 | 173 |
| 91 // Fire again and ensure no sample is recorded. | 174 // Fire again and ensure no sample is recorded. |
| 92 signal_sensitive_input(); | 175 SignalSensitiveInput(); |
| 93 histograms.ExpectUniqueSample(histogram_name(), false, 1); | 176 histograms.ExpectUniqueSample(HistogramName(), false, 1); |
| 94 | 177 |
| 95 // Navigate to a new page and ensure a sample is recorded. | 178 // Navigate to a new page and ensure a sample is recorded. |
| 96 navigate_to_different_http_page(); | 179 NavigateToDifferentHTTPPage(); |
| 97 histograms.ExpectUniqueSample(histogram_name(), false, 1); | 180 histograms.ExpectUniqueSample(HistogramName(), false, 1); |
| 98 signal_sensitive_input(); | 181 SignalSensitiveInput(); |
| 99 histograms.ExpectUniqueSample(histogram_name(), false, 2); | 182 histograms.ExpectUniqueSample(HistogramName(), false, 2); |
| 100 } | 183 } |
| 101 | 184 |
| 102 INSTANTIATE_TEST_CASE_P(SecurityStateTabHelperHistogramTest, | 185 INSTANTIATE_TEST_CASE_P(SecurityStateTabHelperHistogramTest, |
| 103 SecurityStateTabHelperHistogramTest, | 186 SecurityStateTabHelperHistogramTest, |
| 104 // Here 'true' to test password field triggered | 187 // Here 'true' to test password field triggered |
| 105 // histogram and 'false' to test credit card field. | 188 // histogram and 'false' to test credit card field. |
| 106 testing::Bool()); | 189 testing::Bool()); |
| 107 | 190 |
| 108 } // namespace | 191 } // namespace |
| OLD | NEW |