| 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 "components/security_state/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 "base/command_line.h" |
| 9 #include "components/security_state/security_state_model_client.h" | 10 #include "components/security_state/security_state_model_client.h" |
| 11 #include "components/security_state/switches.h" |
| 10 #include "net/cert/x509_certificate.h" | 12 #include "net/cert/x509_certificate.h" |
| 11 #include "net/ssl/ssl_cipher_suite_names.h" | 13 #include "net/ssl/ssl_cipher_suite_names.h" |
| 12 #include "net/ssl/ssl_connection_status_flags.h" | 14 #include "net/ssl/ssl_connection_status_flags.h" |
| 13 #include "net/test/cert_test_util.h" | 15 #include "net/test/cert_test_util.h" |
| 14 #include "net/test/test_certificate_data.h" | 16 #include "net/test/test_certificate_data.h" |
| 15 #include "net/test/test_data_directory.h" | 17 #include "net/test/test_data_directory.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 19 |
| 18 namespace security_state { | 20 namespace security_state { |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 const char kUrl[] = "https://foo.test"; | 24 const char kHttpsUrl[] = "https://foo.test"; |
| 25 const char kHttpUrl[] = "http://foo.test"; |
| 23 | 26 |
| 24 class TestSecurityStateModelClient : public SecurityStateModelClient { | 27 class TestSecurityStateModelClient : public SecurityStateModelClient { |
| 25 public: | 28 public: |
| 26 TestSecurityStateModelClient() | 29 TestSecurityStateModelClient() |
| 27 : initial_security_level_(SecurityStateModel::SECURE), | 30 : url_(kHttpsUrl), |
| 31 initial_security_level_(SecurityStateModel::SECURE), |
| 28 connection_status_(net::SSL_CONNECTION_VERSION_TLS1_2 | 32 connection_status_(net::SSL_CONNECTION_VERSION_TLS1_2 |
| 29 << net::SSL_CONNECTION_VERSION_SHIFT), | 33 << net::SSL_CONNECTION_VERSION_SHIFT), |
| 30 cert_status_(net::CERT_STATUS_SHA1_SIGNATURE_PRESENT), | 34 cert_status_(net::CERT_STATUS_SHA1_SIGNATURE_PRESENT), |
| 31 displayed_mixed_content_(false), | 35 displayed_mixed_content_(false), |
| 32 ran_mixed_content_(false), | 36 ran_mixed_content_(false), |
| 33 fails_malware_check_(false) { | 37 fails_malware_check_(false), |
| 38 displayed_password_field_on_http_(false), |
| 39 displayed_credit_card_field_on_http_(false) { |
| 34 cert_ = | 40 cert_ = |
| 35 net::ImportCertFromFile(net::GetTestCertsDirectory(), "sha1_2016.pem"); | 41 net::ImportCertFromFile(net::GetTestCertsDirectory(), "sha1_2016.pem"); |
| 36 } | 42 } |
| 37 ~TestSecurityStateModelClient() override {} | 43 ~TestSecurityStateModelClient() override {} |
| 38 | 44 |
| 39 void set_connection_status(int connection_status) { | 45 void set_connection_status(int connection_status) { |
| 40 connection_status_ = connection_status; | 46 connection_status_ = connection_status; |
| 41 } | 47 } |
| 42 void SetCipherSuite(uint16_t ciphersuite) { | 48 void SetCipherSuite(uint16_t ciphersuite) { |
| 43 net::SSLConnectionStatusSetCipherSuite(ciphersuite, &connection_status_); | 49 net::SSLConnectionStatusSetCipherSuite(ciphersuite, &connection_status_); |
| 44 } | 50 } |
| 45 void AddCertStatus(net::CertStatus cert_status) { | 51 void AddCertStatus(net::CertStatus cert_status) { |
| 46 cert_status_ |= cert_status; | 52 cert_status_ |= cert_status; |
| 47 } | 53 } |
| 48 void SetDisplayedMixedContent(bool displayed_mixed_content) { | 54 void SetDisplayedMixedContent(bool displayed_mixed_content) { |
| 49 displayed_mixed_content_ = displayed_mixed_content; | 55 displayed_mixed_content_ = displayed_mixed_content; |
| 50 } | 56 } |
| 51 void SetRanMixedContent(bool ran_mixed_content) { | 57 void SetRanMixedContent(bool ran_mixed_content) { |
| 52 ran_mixed_content_ = ran_mixed_content; | 58 ran_mixed_content_ = ran_mixed_content; |
| 53 } | 59 } |
| 54 void set_fails_malware_check(bool fails_malware_check) { | 60 void set_fails_malware_check(bool fails_malware_check) { |
| 55 fails_malware_check_ = fails_malware_check; | 61 fails_malware_check_ = fails_malware_check; |
| 56 } | 62 } |
| 57 void set_initial_security_level( | 63 void set_initial_security_level( |
| 58 SecurityStateModel::SecurityLevel security_level) { | 64 SecurityStateModel::SecurityLevel security_level) { |
| 59 initial_security_level_ = security_level; | 65 initial_security_level_ = security_level; |
| 60 } | 66 } |
| 67 void set_displayed_password_field_on_http( |
| 68 bool displayed_password_field_on_http) { |
| 69 displayed_password_field_on_http_ = displayed_password_field_on_http; |
| 70 } |
| 71 void set_displayed_credit_card_field_on_http( |
| 72 bool displayed_credit_card_field_on_http) { |
| 73 displayed_credit_card_field_on_http_ = displayed_credit_card_field_on_http; |
| 74 } |
| 75 |
| 76 void UseHttpUrl() { url_ = GURL(kHttpUrl); } |
| 61 | 77 |
| 62 // SecurityStateModelClient: | 78 // SecurityStateModelClient: |
| 63 void GetVisibleSecurityState( | 79 void GetVisibleSecurityState( |
| 64 SecurityStateModel::VisibleSecurityState* state) override { | 80 SecurityStateModel::VisibleSecurityState* state) override { |
| 65 state->connection_info_initialized = true; | 81 state->connection_info_initialized = true; |
| 66 state->url = GURL(kUrl); | 82 state->url = url_; |
| 67 state->initial_security_level = initial_security_level_; | 83 state->initial_security_level = initial_security_level_; |
| 68 state->cert_status = cert_status_; | 84 state->cert_status = cert_status_; |
| 69 state->connection_status = connection_status_; | 85 state->connection_status = connection_status_; |
| 70 state->security_bits = 256; | 86 state->security_bits = 256; |
| 71 state->displayed_mixed_content = displayed_mixed_content_; | 87 state->displayed_mixed_content = displayed_mixed_content_; |
| 72 state->ran_mixed_content = ran_mixed_content_; | 88 state->ran_mixed_content = ran_mixed_content_; |
| 73 state->fails_malware_check = fails_malware_check_; | 89 state->fails_malware_check = fails_malware_check_; |
| 90 state->displayed_password_field_on_http = displayed_password_field_on_http_; |
| 91 state->displayed_credit_card_field_on_http = |
| 92 displayed_credit_card_field_on_http_; |
| 74 } | 93 } |
| 75 | 94 |
| 76 bool RetrieveCert(scoped_refptr<net::X509Certificate>* cert) override { | 95 bool RetrieveCert(scoped_refptr<net::X509Certificate>* cert) override { |
| 77 *cert = cert_; | 96 *cert = cert_; |
| 78 return true; | 97 return true; |
| 79 } | 98 } |
| 80 | 99 |
| 81 bool UsedPolicyInstalledCertificate() override { return false; } | 100 bool UsedPolicyInstalledCertificate() override { return false; } |
| 82 | 101 |
| 83 // Always returns true because all unit tests in this file test | 102 bool IsOriginSecure(const GURL& url) override { |
| 84 // scenarios in which the origin is secure. | 103 return url_ == GURL(kHttpsUrl); |
| 85 bool IsOriginSecure(const GURL& url) override { return true; } | 104 } |
| 86 | 105 |
| 87 private: | 106 private: |
| 107 GURL url_; |
| 88 SecurityStateModel::SecurityLevel initial_security_level_; | 108 SecurityStateModel::SecurityLevel initial_security_level_; |
| 89 scoped_refptr<net::X509Certificate> cert_; | 109 scoped_refptr<net::X509Certificate> cert_; |
| 90 int connection_status_; | 110 int connection_status_; |
| 91 net::CertStatus cert_status_; | 111 net::CertStatus cert_status_; |
| 92 bool displayed_mixed_content_; | 112 bool displayed_mixed_content_; |
| 93 bool ran_mixed_content_; | 113 bool ran_mixed_content_; |
| 94 bool fails_malware_check_; | 114 bool fails_malware_check_; |
| 115 bool displayed_password_field_on_http_; |
| 116 bool displayed_credit_card_field_on_http_; |
| 95 }; | 117 }; |
| 96 | 118 |
| 97 // Tests that SHA1-signed certificates expiring in 2016 downgrade the | 119 // Tests that SHA1-signed certificates expiring in 2016 downgrade the |
| 98 // security state of the page. | 120 // security state of the page. |
| 99 TEST(SecurityStateModelTest, SHA1Warning) { | 121 TEST(SecurityStateModelTest, SHA1Warning) { |
| 100 TestSecurityStateModelClient client; | 122 TestSecurityStateModelClient client; |
| 101 SecurityStateModel model; | 123 SecurityStateModel model; |
| 102 model.SetClient(&client); | 124 model.SetClient(&client); |
| 103 const SecurityStateModel::SecurityInfo& security_info = | 125 const SecurityStateModel::SecurityInfo& security_info = |
| 104 model.GetSecurityInfo(); | 126 model.GetSecurityInfo(); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 TestSecurityStateModelClient client; | 243 TestSecurityStateModelClient client; |
| 222 SecurityStateModel model; | 244 SecurityStateModel model; |
| 223 model.SetClient(&client); | 245 model.SetClient(&client); |
| 224 client.set_fails_malware_check(true); | 246 client.set_fails_malware_check(true); |
| 225 const SecurityStateModel::SecurityInfo& security_info = | 247 const SecurityStateModel::SecurityInfo& security_info = |
| 226 model.GetSecurityInfo(); | 248 model.GetSecurityInfo(); |
| 227 EXPECT_TRUE(security_info.fails_malware_check); | 249 EXPECT_TRUE(security_info.fails_malware_check); |
| 228 EXPECT_EQ(SecurityStateModel::SECURITY_ERROR, security_info.security_level); | 250 EXPECT_EQ(SecurityStateModel::SECURITY_ERROR, security_info.security_level); |
| 229 } | 251 } |
| 230 | 252 |
| 253 // Tests that password fields cause the security level to be downgraded |
| 254 // to HTTP_SHOW_WARNING when the command-line switch is set. |
| 255 TEST(SecurityStateModelTest, PasswordFieldWarning) { |
| 256 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 257 switches::kMarkNonSecureAs, |
| 258 switches::kMarkNonSecureWithPasswordsOrCcAsNonSecure); |
| 259 TestSecurityStateModelClient client; |
| 260 client.UseHttpUrl(); |
| 261 client.set_initial_security_level(SecurityStateModel::NONE); |
| 262 SecurityStateModel model; |
| 263 model.SetClient(&client); |
| 264 client.set_displayed_password_field_on_http(true); |
| 265 const SecurityStateModel::SecurityInfo& security_info = |
| 266 model.GetSecurityInfo(); |
| 267 EXPECT_EQ(SecurityStateModel::HTTP_SHOW_WARNING, |
| 268 security_info.security_level); |
| 269 } |
| 270 |
| 271 // Tests that credit card fields cause the security level to be downgraded |
| 272 // to HTTP_SHOW_WARNING when the command-line switch is set. |
| 273 TEST(SecurityStateModelTest, CreditCardFieldWarning) { |
| 274 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 275 switches::kMarkNonSecureAs, |
| 276 switches::kMarkNonSecureWithPasswordsOrCcAsNonSecure); |
| 277 TestSecurityStateModelClient client; |
| 278 client.UseHttpUrl(); |
| 279 client.set_initial_security_level(SecurityStateModel::NONE); |
| 280 SecurityStateModel model; |
| 281 model.SetClient(&client); |
| 282 client.set_displayed_credit_card_field_on_http(true); |
| 283 const SecurityStateModel::SecurityInfo& security_info = |
| 284 model.GetSecurityInfo(); |
| 285 EXPECT_EQ(SecurityStateModel::HTTP_SHOW_WARNING, |
| 286 security_info.security_level); |
| 287 } |
| 288 |
| 289 // Tests that neither password nor credit fields cause the security |
| 290 // level to be downgraded to HTTP_SHOW_WARNING when the command-line switch |
| 291 // is NOT set. |
| 292 TEST(SecurityStateModelTest, HttpWarningNotSetWithoutSwitch) { |
| 293 TestSecurityStateModelClient client; |
| 294 client.UseHttpUrl(); |
| 295 client.set_initial_security_level(SecurityStateModel::NONE); |
| 296 SecurityStateModel model; |
| 297 model.SetClient(&client); |
| 298 client.set_displayed_password_field_on_http(true); |
| 299 client.set_displayed_credit_card_field_on_http(true); |
| 300 const SecurityStateModel::SecurityInfo& security_info = |
| 301 model.GetSecurityInfo(); |
| 302 EXPECT_EQ(SecurityStateModel::NONE, security_info.security_level); |
| 303 } |
| 304 |
| 231 } // namespace | 305 } // namespace |
| 232 | 306 |
| 233 } // namespace security_state | 307 } // namespace security_state |
| OLD | NEW |