| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_model.h" | 5 #include "components/security_state/security_state_model.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "chrome/browser/ssl/security_state_model_client.h" | 9 #include "components/security_state/security_state_model_client.h" |
| 10 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
| 11 #include "chrome/test/base/testing_profile.h" | |
| 12 #include "content/public/browser/cert_store.h" | |
| 13 #include "content/public/common/origin_util.h" | |
| 14 #include "content/public/test/mock_render_process_host.h" | |
| 15 #include "content/public/test/test_browser_thread_bundle.h" | |
| 16 #include "net/base/test_data_directory.h" | 10 #include "net/base/test_data_directory.h" |
| 17 #include "net/cert/x509_certificate.h" | 11 #include "net/cert/x509_certificate.h" |
| 18 #include "net/ssl/ssl_connection_status_flags.h" | 12 #include "net/ssl/ssl_connection_status_flags.h" |
| 19 #include "net/test/cert_test_util.h" | 13 #include "net/test/cert_test_util.h" |
| 20 #include "net/test/test_certificate_data.h" | 14 #include "net/test/test_certificate_data.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 16 |
| 17 namespace security_state { |
| 18 |
| 23 namespace { | 19 namespace { |
| 24 | 20 |
| 25 const char kUrl[] = "https://foo.test"; | 21 const char kUrl[] = "https://foo.test"; |
| 26 | 22 |
| 27 class TestSecurityStateModelClient : public SecurityStateModelClient { | 23 class TestSecurityStateModelClient : public SecurityStateModelClient { |
| 28 public: | 24 public: |
| 29 TestSecurityStateModelClient() | 25 TestSecurityStateModelClient() |
| 30 : initial_security_level_(SecurityStateModel::SECURE), | 26 : initial_security_level_(SecurityStateModel::SECURE), |
| 31 connection_status_(net::SSL_CONNECTION_VERSION_TLS1_2 | 27 connection_status_(net::SSL_CONNECTION_VERSION_TLS1_2 |
| 32 << net::SSL_CONNECTION_VERSION_SHIFT), | 28 << net::SSL_CONNECTION_VERSION_SHIFT), |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 state->ran_mixed_content = ran_mixed_content_; | 68 state->ran_mixed_content = ran_mixed_content_; |
| 73 } | 69 } |
| 74 | 70 |
| 75 bool RetrieveCert(scoped_refptr<net::X509Certificate>* cert) override { | 71 bool RetrieveCert(scoped_refptr<net::X509Certificate>* cert) override { |
| 76 *cert = cert_; | 72 *cert = cert_; |
| 77 return true; | 73 return true; |
| 78 } | 74 } |
| 79 | 75 |
| 80 bool UsedPolicyInstalledCertificate() override { return false; } | 76 bool UsedPolicyInstalledCertificate() override { return false; } |
| 81 | 77 |
| 82 bool IsOriginSecure(const GURL& url) override { | 78 // Always returns true because all unit tests in this file test |
| 83 return content::IsOriginSecure(url); | 79 // scenarios in which the origin is secure. |
| 84 } | 80 bool IsOriginSecure(const GURL& url) override { return true; } |
| 85 | 81 |
| 86 private: | 82 private: |
| 87 SecurityStateModel::SecurityLevel initial_security_level_; | 83 SecurityStateModel::SecurityLevel initial_security_level_; |
| 88 scoped_refptr<net::X509Certificate> cert_; | 84 scoped_refptr<net::X509Certificate> cert_; |
| 89 int connection_status_; | 85 int connection_status_; |
| 90 net::CertStatus cert_status_; | 86 net::CertStatus cert_status_; |
| 91 bool displayed_mixed_content_; | 87 bool displayed_mixed_content_; |
| 92 bool ran_mixed_content_; | 88 bool ran_mixed_content_; |
| 93 }; | 89 }; |
| 94 | 90 |
| 95 class SecurityStateModelTest : public ChromeRenderViewHostTestHarness {}; | |
| 96 | |
| 97 // Tests that SHA1-signed certificates expiring in 2016 downgrade the | 91 // Tests that SHA1-signed certificates expiring in 2016 downgrade the |
| 98 // security state of the page. | 92 // security state of the page. |
| 99 TEST_F(SecurityStateModelTest, SHA1Warning) { | 93 TEST(SecurityStateModelTest, SHA1Warning) { |
| 100 TestSecurityStateModelClient client; | 94 TestSecurityStateModelClient client; |
| 101 SecurityStateModel model; | 95 SecurityStateModel model; |
| 102 model.SetClient(&client); | 96 model.SetClient(&client); |
| 103 const SecurityStateModel::SecurityInfo& security_info = | 97 const SecurityStateModel::SecurityInfo& security_info = |
| 104 model.GetSecurityInfo(); | 98 model.GetSecurityInfo(); |
| 105 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR, | 99 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR, |
| 106 security_info.sha1_deprecation_status); | 100 security_info.sha1_deprecation_status); |
| 107 EXPECT_EQ(SecurityStateModel::NONE, security_info.security_level); | 101 EXPECT_EQ(SecurityStateModel::NONE, security_info.security_level); |
| 108 } | 102 } |
| 109 | 103 |
| 110 // Tests that SHA1 warnings don't interfere with the handling of mixed | 104 // Tests that SHA1 warnings don't interfere with the handling of mixed |
| 111 // content. | 105 // content. |
| 112 TEST_F(SecurityStateModelTest, SHA1WarningMixedContent) { | 106 TEST(SecurityStateModelTest, SHA1WarningMixedContent) { |
| 113 TestSecurityStateModelClient client; | 107 TestSecurityStateModelClient client; |
| 114 SecurityStateModel model; | 108 SecurityStateModel model; |
| 115 model.SetClient(&client); | 109 model.SetClient(&client); |
| 116 client.SetDisplayedMixedContent(true); | 110 client.SetDisplayedMixedContent(true); |
| 117 const SecurityStateModel::SecurityInfo& security_info1 = | 111 const SecurityStateModel::SecurityInfo& security_info1 = |
| 118 model.GetSecurityInfo(); | 112 model.GetSecurityInfo(); |
| 119 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR, | 113 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR, |
| 120 security_info1.sha1_deprecation_status); | 114 security_info1.sha1_deprecation_status); |
| 121 EXPECT_EQ(SecurityStateModel::DISPLAYED_MIXED_CONTENT, | 115 EXPECT_EQ(SecurityStateModel::DISPLAYED_MIXED_CONTENT, |
| 122 security_info1.mixed_content_status); | 116 security_info1.mixed_content_status); |
| 123 EXPECT_EQ(SecurityStateModel::NONE, security_info1.security_level); | 117 EXPECT_EQ(SecurityStateModel::NONE, security_info1.security_level); |
| 124 | 118 |
| 125 client.set_initial_security_level(SecurityStateModel::SECURITY_ERROR); | 119 client.set_initial_security_level(SecurityStateModel::SECURITY_ERROR); |
| 126 client.SetDisplayedMixedContent(false); | 120 client.SetDisplayedMixedContent(false); |
| 127 client.SetRanMixedContent(true); | 121 client.SetRanMixedContent(true); |
| 128 const SecurityStateModel::SecurityInfo& security_info2 = | 122 const SecurityStateModel::SecurityInfo& security_info2 = |
| 129 model.GetSecurityInfo(); | 123 model.GetSecurityInfo(); |
| 130 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR, | 124 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR, |
| 131 security_info2.sha1_deprecation_status); | 125 security_info2.sha1_deprecation_status); |
| 132 EXPECT_EQ(SecurityStateModel::RAN_MIXED_CONTENT, | 126 EXPECT_EQ(SecurityStateModel::RAN_MIXED_CONTENT, |
| 133 security_info2.mixed_content_status); | 127 security_info2.mixed_content_status); |
| 134 EXPECT_EQ(SecurityStateModel::SECURITY_ERROR, security_info2.security_level); | 128 EXPECT_EQ(SecurityStateModel::SECURITY_ERROR, security_info2.security_level); |
| 135 } | 129 } |
| 136 | 130 |
| 137 // Tests that SHA1 warnings don't interfere with the handling of major | 131 // Tests that SHA1 warnings don't interfere with the handling of major |
| 138 // cert errors. | 132 // cert errors. |
| 139 TEST_F(SecurityStateModelTest, SHA1WarningBrokenHTTPS) { | 133 TEST(SecurityStateModelTest, SHA1WarningBrokenHTTPS) { |
| 140 TestSecurityStateModelClient client; | 134 TestSecurityStateModelClient client; |
| 141 SecurityStateModel model; | 135 SecurityStateModel model; |
| 142 model.SetClient(&client); | 136 model.SetClient(&client); |
| 143 client.set_initial_security_level(SecurityStateModel::SECURITY_ERROR); | 137 client.set_initial_security_level(SecurityStateModel::SECURITY_ERROR); |
| 144 client.AddCertStatus(net::CERT_STATUS_DATE_INVALID); | 138 client.AddCertStatus(net::CERT_STATUS_DATE_INVALID); |
| 145 const SecurityStateModel::SecurityInfo& security_info = | 139 const SecurityStateModel::SecurityInfo& security_info = |
| 146 model.GetSecurityInfo(); | 140 model.GetSecurityInfo(); |
| 147 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR, | 141 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR, |
| 148 security_info.sha1_deprecation_status); | 142 security_info.sha1_deprecation_status); |
| 149 EXPECT_EQ(SecurityStateModel::SECURITY_ERROR, security_info.security_level); | 143 EXPECT_EQ(SecurityStateModel::SECURITY_ERROR, security_info.security_level); |
| 150 } | 144 } |
| 151 | 145 |
| 152 // Tests that |security_info.is_secure_protocol_and_ciphersuite| is | 146 // Tests that |security_info.is_secure_protocol_and_ciphersuite| is |
| 153 // computed correctly. | 147 // computed correctly. |
| 154 TEST_F(SecurityStateModelTest, SecureProtocolAndCiphersuite) { | 148 TEST(SecurityStateModelTest, SecureProtocolAndCiphersuite) { |
| 155 TestSecurityStateModelClient client; | 149 TestSecurityStateModelClient client; |
| 156 SecurityStateModel model; | 150 SecurityStateModel model; |
| 157 model.SetClient(&client); | 151 model.SetClient(&client); |
| 158 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from | 152 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from |
| 159 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param
eters-4 | 153 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param
eters-4 |
| 160 const uint16_t ciphersuite = 0xc02f; | 154 const uint16_t ciphersuite = 0xc02f; |
| 161 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2 | 155 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2 |
| 162 << net::SSL_CONNECTION_VERSION_SHIFT); | 156 << net::SSL_CONNECTION_VERSION_SHIFT); |
| 163 client.SetCipherSuite(ciphersuite); | 157 client.SetCipherSuite(ciphersuite); |
| 164 const SecurityStateModel::SecurityInfo& security_info = | 158 const SecurityStateModel::SecurityInfo& security_info = |
| 165 model.GetSecurityInfo(); | 159 model.GetSecurityInfo(); |
| 166 EXPECT_TRUE(security_info.is_secure_protocol_and_ciphersuite); | 160 EXPECT_TRUE(security_info.is_secure_protocol_and_ciphersuite); |
| 167 } | 161 } |
| 168 | 162 |
| 169 TEST_F(SecurityStateModelTest, NonsecureProtocol) { | 163 TEST(SecurityStateModelTest, NonsecureProtocol) { |
| 170 TestSecurityStateModelClient client; | 164 TestSecurityStateModelClient client; |
| 171 SecurityStateModel model; | 165 SecurityStateModel model; |
| 172 model.SetClient(&client); | 166 model.SetClient(&client); |
| 173 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from | 167 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from |
| 174 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param
eters-4 | 168 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param
eters-4 |
| 175 const uint16_t ciphersuite = 0xc02f; | 169 const uint16_t ciphersuite = 0xc02f; |
| 176 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_1 | 170 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_1 |
| 177 << net::SSL_CONNECTION_VERSION_SHIFT); | 171 << net::SSL_CONNECTION_VERSION_SHIFT); |
| 178 client.SetCipherSuite(ciphersuite); | 172 client.SetCipherSuite(ciphersuite); |
| 179 const SecurityStateModel::SecurityInfo& security_info = | 173 const SecurityStateModel::SecurityInfo& security_info = |
| 180 model.GetSecurityInfo(); | 174 model.GetSecurityInfo(); |
| 181 EXPECT_FALSE(security_info.is_secure_protocol_and_ciphersuite); | 175 EXPECT_FALSE(security_info.is_secure_protocol_and_ciphersuite); |
| 182 } | 176 } |
| 183 | 177 |
| 184 TEST_F(SecurityStateModelTest, NonsecureCiphersuite) { | 178 TEST(SecurityStateModelTest, NonsecureCiphersuite) { |
| 185 TestSecurityStateModelClient client; | 179 TestSecurityStateModelClient client; |
| 186 SecurityStateModel model; | 180 SecurityStateModel model; |
| 187 model.SetClient(&client); | 181 model.SetClient(&client); |
| 188 // TLS_RSA_WITH_AES_128_CCM_8 from | 182 // TLS_RSA_WITH_AES_128_CCM_8 from |
| 189 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param
eters-4 | 183 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param
eters-4 |
| 190 const uint16_t ciphersuite = 0xc0a0; | 184 const uint16_t ciphersuite = 0xc0a0; |
| 191 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2 | 185 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2 |
| 192 << net::SSL_CONNECTION_VERSION_SHIFT); | 186 << net::SSL_CONNECTION_VERSION_SHIFT); |
| 193 client.SetCipherSuite(ciphersuite); | 187 client.SetCipherSuite(ciphersuite); |
| 194 const SecurityStateModel::SecurityInfo& security_info = | 188 const SecurityStateModel::SecurityInfo& security_info = |
| 195 model.GetSecurityInfo(); | 189 model.GetSecurityInfo(); |
| 196 EXPECT_FALSE(security_info.is_secure_protocol_and_ciphersuite); | 190 EXPECT_FALSE(security_info.is_secure_protocol_and_ciphersuite); |
| 197 } | 191 } |
| 198 | 192 |
| 199 } // namespace | 193 } // namespace |
| 194 |
| 195 } // namespace security_state |
| OLD | NEW |