Chromium Code Reviews| 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_nonsecure_password_field_(false), | |
| 39 displayed_nonsecure_credit_card_field_(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_nonsecure_password_field( | |
| 68 bool displayed_nonsecure_password_field) { | |
| 69 displayed_nonsecure_password_field_ = displayed_nonsecure_password_field; | |
| 70 } | |
| 71 void set_displayed_nonsecure_credit_card_field( | |
| 72 bool displayed_nonsecure_credit_card_field) { | |
| 73 displayed_nonsecure_credit_card_field_ = | |
| 74 displayed_nonsecure_credit_card_field; | |
| 75 } | |
| 76 | |
| 77 void UseHttpUrl() { url_ = GURL(kHttpUrl); } | |
| 61 | 78 |
| 62 // SecurityStateModelClient: | 79 // SecurityStateModelClient: |
| 63 void GetVisibleSecurityState( | 80 void GetVisibleSecurityState( |
| 64 SecurityStateModel::VisibleSecurityState* state) override { | 81 SecurityStateModel::VisibleSecurityState* state) override { |
| 65 state->connection_info_initialized = true; | 82 state->connection_info_initialized = true; |
| 66 state->url = GURL(kUrl); | 83 state->url = url_; |
| 67 state->initial_security_level = initial_security_level_; | 84 state->initial_security_level = initial_security_level_; |
| 68 state->cert_status = cert_status_; | 85 state->cert_status = cert_status_; |
| 69 state->connection_status = connection_status_; | 86 state->connection_status = connection_status_; |
| 70 state->security_bits = 256; | 87 state->security_bits = 256; |
| 71 state->displayed_mixed_content = displayed_mixed_content_; | 88 state->displayed_mixed_content = displayed_mixed_content_; |
| 72 state->ran_mixed_content = ran_mixed_content_; | 89 state->ran_mixed_content = ran_mixed_content_; |
| 73 state->fails_malware_check = fails_malware_check_; | 90 state->fails_malware_check = fails_malware_check_; |
| 91 state->displayed_nonsecure_password_field = | |
| 92 displayed_nonsecure_password_field_; | |
| 93 state->displayed_nonsecure_credit_card_field = | |
| 94 displayed_nonsecure_credit_card_field_; | |
| 74 } | 95 } |
| 75 | 96 |
| 76 bool RetrieveCert(scoped_refptr<net::X509Certificate>* cert) override { | 97 bool RetrieveCert(scoped_refptr<net::X509Certificate>* cert) override { |
| 77 *cert = cert_; | 98 *cert = cert_; |
| 78 return true; | 99 return true; |
| 79 } | 100 } |
| 80 | 101 |
| 81 bool UsedPolicyInstalledCertificate() override { return false; } | 102 bool UsedPolicyInstalledCertificate() override { return false; } |
| 82 | 103 |
| 83 // Always returns true because all unit tests in this file test | 104 bool IsOriginSecure(const GURL& url) override { |
| 84 // scenarios in which the origin is secure. | 105 return url_ == GURL(kHttpsUrl); |
| 85 bool IsOriginSecure(const GURL& url) override { return true; } | 106 } |
|
felt
2016/09/20 06:09:41
this seems brittle -- why is the override needed?
estark
2016/09/20 17:53:33
The natural thing would be to use content::IsOrigi
felt
2016/09/20 19:36:13
Ahh right, content dependency. Umm-- yes, I think
| |
| 86 | 107 |
| 87 private: | 108 private: |
| 109 GURL url_; | |
| 88 SecurityStateModel::SecurityLevel initial_security_level_; | 110 SecurityStateModel::SecurityLevel initial_security_level_; |
| 89 scoped_refptr<net::X509Certificate> cert_; | 111 scoped_refptr<net::X509Certificate> cert_; |
| 90 int connection_status_; | 112 int connection_status_; |
| 91 net::CertStatus cert_status_; | 113 net::CertStatus cert_status_; |
| 92 bool displayed_mixed_content_; | 114 bool displayed_mixed_content_; |
| 93 bool ran_mixed_content_; | 115 bool ran_mixed_content_; |
| 94 bool fails_malware_check_; | 116 bool fails_malware_check_; |
| 117 bool displayed_nonsecure_password_field_; | |
| 118 bool displayed_nonsecure_credit_card_field_; | |
| 95 }; | 119 }; |
| 96 | 120 |
| 97 // Tests that SHA1-signed certificates expiring in 2016 downgrade the | 121 // Tests that SHA1-signed certificates expiring in 2016 downgrade the |
| 98 // security state of the page. | 122 // security state of the page. |
| 99 TEST(SecurityStateModelTest, SHA1Warning) { | 123 TEST(SecurityStateModelTest, SHA1Warning) { |
| 100 TestSecurityStateModelClient client; | 124 TestSecurityStateModelClient client; |
| 101 SecurityStateModel model; | 125 SecurityStateModel model; |
| 102 model.SetClient(&client); | 126 model.SetClient(&client); |
| 103 const SecurityStateModel::SecurityInfo& security_info = | 127 const SecurityStateModel::SecurityInfo& security_info = |
| 104 model.GetSecurityInfo(); | 128 model.GetSecurityInfo(); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 221 TestSecurityStateModelClient client; | 245 TestSecurityStateModelClient client; |
| 222 SecurityStateModel model; | 246 SecurityStateModel model; |
| 223 model.SetClient(&client); | 247 model.SetClient(&client); |
| 224 client.set_fails_malware_check(true); | 248 client.set_fails_malware_check(true); |
| 225 const SecurityStateModel::SecurityInfo& security_info = | 249 const SecurityStateModel::SecurityInfo& security_info = |
| 226 model.GetSecurityInfo(); | 250 model.GetSecurityInfo(); |
| 227 EXPECT_TRUE(security_info.fails_malware_check); | 251 EXPECT_TRUE(security_info.fails_malware_check); |
| 228 EXPECT_EQ(SecurityStateModel::SECURITY_ERROR, security_info.security_level); | 252 EXPECT_EQ(SecurityStateModel::SECURITY_ERROR, security_info.security_level); |
| 229 } | 253 } |
| 230 | 254 |
| 255 // Tests that password fields cause the security level to be downgraded | |
| 256 // to HTTP_WARNING when the command-line switch is set. | |
| 257 TEST(SecurityStateModelTest, PasswordFieldWarning) { | |
| 258 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 259 switches::kMarkNonSecureAs, | |
| 260 switches::kMarkNonSecureWithPasswordsOrCcAsNonSecure); | |
| 261 TestSecurityStateModelClient client; | |
| 262 client.UseHttpUrl(); | |
| 263 client.set_initial_security_level(SecurityStateModel::NONE); | |
| 264 SecurityStateModel model; | |
| 265 model.SetClient(&client); | |
| 266 client.set_displayed_nonsecure_password_field(true); | |
| 267 const SecurityStateModel::SecurityInfo& security_info = | |
| 268 model.GetSecurityInfo(); | |
| 269 EXPECT_EQ(SecurityStateModel::HTTP_WARNING, security_info.security_level); | |
| 270 } | |
| 271 | |
| 272 // Tests that credit card fields cause the security level to be downgraded | |
| 273 // to HTTP_WARNING when the command-line switch is set. | |
| 274 TEST(SecurityStateModelTest, CreditCardFieldWarning) { | |
| 275 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 276 switches::kMarkNonSecureAs, | |
| 277 switches::kMarkNonSecureWithPasswordsOrCcAsNonSecure); | |
| 278 TestSecurityStateModelClient client; | |
| 279 client.UseHttpUrl(); | |
| 280 client.set_initial_security_level(SecurityStateModel::NONE); | |
| 281 SecurityStateModel model; | |
| 282 model.SetClient(&client); | |
| 283 client.set_displayed_nonsecure_credit_card_field(true); | |
| 284 const SecurityStateModel::SecurityInfo& security_info = | |
| 285 model.GetSecurityInfo(); | |
| 286 EXPECT_EQ(SecurityStateModel::HTTP_WARNING, security_info.security_level); | |
| 287 } | |
| 288 | |
| 289 // Tests that neither password nor credit fields cause the security | |
| 290 // level to be downgraded to HTTP_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_nonsecure_password_field(true); | |
| 299 client.set_displayed_nonsecure_credit_card_field(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 |