OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/website_settings_model.h" |
| 6 |
| 7 #include "base/at_exit.h" |
| 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/test/base/testing_profile.h" |
| 10 #include "content/browser/cert_store.h" |
| 11 #include "content/public/common/ssl_status.h" |
| 12 #include "net/base/cert_status_flags.h" |
| 13 #include "net/base/ssl_connection_status_flags.h" |
| 14 #include "net/base/test_certificate_data.h" |
| 15 #include "net/base/x509_certificate.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 using content::SSLStatus; |
| 20 using namespace testing; |
| 21 |
| 22 namespace { |
| 23 |
| 24 // SSL cipher suite like specified in RFC5246 Appendix A.5. "The Cipher Suite". |
| 25 static int TLS_RSA_WITH_AES_256_CBC_SHA256 = 0x3D; |
| 26 |
| 27 int SetSSLVersion(int connection_status, int version) { |
| 28 // Clear SSL version bits (Bits 20, 21 and 22). |
| 29 connection_status &= |
| 30 ~(net::SSL_CONNECTION_VERSION_MASK << net::SSL_CONNECTION_VERSION_SHIFT); |
| 31 int bitmask = version << net::SSL_CONNECTION_VERSION_SHIFT; |
| 32 return bitmask | connection_status; |
| 33 } |
| 34 |
| 35 int SetSSLCipherSuite(int connection_status, int cipher_suite) { |
| 36 // Clear cipher suite bits (the 16 lowest bits). |
| 37 connection_status &= ~net::SSL_CONNECTION_CIPHERSUITE_MASK; |
| 38 return cipher_suite | connection_status; |
| 39 } |
| 40 |
| 41 class MockCertStore : public CertStore { |
| 42 public: |
| 43 virtual ~MockCertStore() {} |
| 44 MOCK_METHOD2(StoreCert, int(net::X509Certificate*, int)); |
| 45 MOCK_METHOD2(RetrieveCert, bool(int, scoped_refptr<net::X509Certificate>*)); |
| 46 MOCK_METHOD1(RemoveCertsForRenderProcesHost, void(int)); |
| 47 }; |
| 48 |
| 49 class WebsiteSettingsModelTest : public testing::Test { |
| 50 public: |
| 51 WebsiteSettingsModelTest() : testing::Test(), |
| 52 profile_(new TestingProfile()), |
| 53 cert_id_(0) { |
| 54 } |
| 55 |
| 56 virtual ~WebsiteSettingsModelTest() { |
| 57 } |
| 58 |
| 59 virtual void SetUp() { |
| 60 cert_id_ = 1; |
| 61 |
| 62 base::Time start_date = base::Time::Now(); |
| 63 base::Time expiration_date = base::Time::FromInternalValue( |
| 64 start_date.ToInternalValue() + base::Time::kMicrosecondsPerWeek); |
| 65 cert_ = new net::X509Certificate("subject", |
| 66 "issuer", |
| 67 start_date, |
| 68 expiration_date); |
| 69 |
| 70 EXPECT_CALL(cert_store_, RetrieveCert(cert_id_, _) ) |
| 71 .Times(AnyNumber()) |
| 72 .WillRepeatedly(DoAll(SetArgPointee<1>(cert_), Return(true))); |
| 73 } |
| 74 |
| 75 virtual void TearDown() OVERRIDE { |
| 76 EXPECT_TRUE(Mock::VerifyAndClear(&cert_store_)); |
| 77 } |
| 78 |
| 79 Profile* profile() const { return profile_.get(); } |
| 80 |
| 81 scoped_ptr<Profile> profile_; |
| 82 int cert_id_; |
| 83 scoped_refptr<net::X509Certificate> cert_; |
| 84 MockCertStore cert_store_; |
| 85 }; |
| 86 |
| 87 } // namespace |
| 88 |
| 89 TEST_F(WebsiteSettingsModelTest, HTTPConnection) { |
| 90 GURL url = GURL("http://www.example.com"); |
| 91 SSLStatus ssl; |
| 92 ssl.security_style = content::SECURITY_STYLE_UNAUTHENTICATED; |
| 93 |
| 94 scoped_ptr<WebsiteSettingsModel> model( |
| 95 new WebsiteSettingsModel(profile(), url, ssl, &cert_store_)); |
| 96 EXPECT_EQ(WebsiteSettingsModel::SITE_CONNECTION_STATUS_UNENCRYPTED, |
| 97 model->site_connection_status()); |
| 98 EXPECT_EQ(WebsiteSettingsModel::SITE_IDENTITY_STATUS_NO_CERT, |
| 99 model->site_identity_status()); |
| 100 EXPECT_EQ(string16(), model->organization_name()); |
| 101 } |
| 102 |
| 103 TEST_F(WebsiteSettingsModelTest, HTTPSConnection) { |
| 104 GURL url = GURL("https://www.example.com"); |
| 105 |
| 106 SSLStatus ssl; |
| 107 ssl.security_style = content::SECURITY_STYLE_AUTHENTICATED; |
| 108 ssl.cert_id = cert_id_; |
| 109 ssl.cert_status = 0; |
| 110 ssl.security_bits = 81; // No error if > 80. |
| 111 int status = 0; |
| 112 status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1); |
| 113 status = SetSSLCipherSuite(status, TLS_RSA_WITH_AES_256_CBC_SHA256); |
| 114 ssl.connection_status = status; |
| 115 |
| 116 scoped_ptr<WebsiteSettingsModel> model( |
| 117 new WebsiteSettingsModel(profile(), url, ssl, &cert_store_)); |
| 118 EXPECT_EQ(WebsiteSettingsModel::SITE_CONNECTION_STATUS_ENCRYPTED, |
| 119 model->site_connection_status()); |
| 120 EXPECT_EQ(WebsiteSettingsModel::SITE_IDENTITY_STATUS_CERT, |
| 121 model->site_identity_status()); |
| 122 EXPECT_EQ(string16(), model->organization_name()); |
| 123 } |
| 124 |
| 125 TEST_F(WebsiteSettingsModelTest, HTTPSMixedContent) { |
| 126 GURL url = GURL("https://www.example.com"); |
| 127 |
| 128 SSLStatus ssl; |
| 129 ssl.security_style = content::SECURITY_STYLE_AUTHENTICATED; |
| 130 ssl.cert_id = cert_id_; |
| 131 ssl.cert_status = 0; |
| 132 ssl.security_bits = 81; // No error if > 80. |
| 133 ssl.content_status = SSLStatus::DISPLAYED_INSECURE_CONTENT; |
| 134 int status = 0; |
| 135 status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1); |
| 136 status = SetSSLCipherSuite(status, TLS_RSA_WITH_AES_256_CBC_SHA256); |
| 137 ssl.connection_status = status; |
| 138 |
| 139 scoped_ptr<WebsiteSettingsModel> model( |
| 140 new WebsiteSettingsModel(profile(), url, ssl, &cert_store_)); |
| 141 EXPECT_EQ(WebsiteSettingsModel::SITE_CONNECTION_STATUS_MIXED_CONTENT, |
| 142 model->site_connection_status()); |
| 143 EXPECT_EQ(WebsiteSettingsModel::SITE_IDENTITY_STATUS_CERT, |
| 144 model->site_identity_status()); |
| 145 EXPECT_EQ(string16(), model->organization_name()); |
| 146 } |
| 147 |
| 148 TEST_F(WebsiteSettingsModelTest, HTTPSEVCert) { |
| 149 GURL url = GURL("https://www.example.com"); |
| 150 |
| 151 scoped_refptr<net::X509Certificate> ev_cert = |
| 152 net::X509Certificate::CreateFromBytes( |
| 153 reinterpret_cast<const char*>(google_der), |
| 154 sizeof(google_der)); |
| 155 int ev_cert_id = 1; |
| 156 MockCertStore tmp_cert_store; |
| 157 EXPECT_CALL(tmp_cert_store, RetrieveCert(ev_cert_id, _) ).WillRepeatedly( |
| 158 DoAll(SetArgPointee<1>(ev_cert), |
| 159 Return(true))); |
| 160 |
| 161 SSLStatus ssl; |
| 162 ssl.security_style = content::SECURITY_STYLE_AUTHENTICATED; |
| 163 ssl.cert_id = ev_cert_id; |
| 164 ssl.cert_status = net::CERT_STATUS_IS_EV; |
| 165 ssl.security_bits = 81; // No error if > 80. |
| 166 ssl.content_status = SSLStatus::DISPLAYED_INSECURE_CONTENT; |
| 167 int status = 0; |
| 168 status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1); |
| 169 status = SetSSLCipherSuite(status, TLS_RSA_WITH_AES_256_CBC_SHA256); |
| 170 ssl.connection_status = status; |
| 171 |
| 172 scoped_ptr<WebsiteSettingsModel> model( |
| 173 new WebsiteSettingsModel(profile(), url, ssl, &tmp_cert_store)); |
| 174 EXPECT_EQ(WebsiteSettingsModel::SITE_CONNECTION_STATUS_MIXED_CONTENT, |
| 175 model->site_connection_status()); |
| 176 EXPECT_EQ(WebsiteSettingsModel::SITE_IDENTITY_STATUS_EV_CERT, |
| 177 model->site_identity_status()); |
| 178 EXPECT_EQ(UTF8ToUTF16("Google Inc"), model->organization_name()); |
| 179 EXPECT_TRUE(Mock::VerifyAndClear(&tmp_cert_store)); |
| 180 } |
| 181 |
| 182 TEST_F(WebsiteSettingsModelTest, HTTPSBadCertificate) { |
| 183 GURL url = GURL("https://www.example.com"); |
| 184 |
| 185 scoped_refptr<net::X509Certificate> bad_cert = |
| 186 new net::X509Certificate("subject 2", "issuer 2", base::Time(), |
| 187 base::Time()); |
| 188 // This test triggers a code path that uses the singleton |CertStore|. |
| 189 // Therefore it is not possible to use the |MockCertStore| in this test. Some |
| 190 // code in |SSLErrorInfo| tries to retrieve the cert used in this test from |
| 191 // the singleton |CertStore|. If the |CertStore| doesn't have the certificate, |
| 192 // than a DCHECK will be fired. |
| 193 CertStore* cert_store = CertStore::GetInstance(); |
| 194 int render_process_host_id = 1; |
| 195 int bad_cert_id = cert_store->StoreCert(bad_cert, render_process_host_id); |
| 196 |
| 197 SSLStatus ssl; |
| 198 ssl.security_style = content::SECURITY_STYLE_AUTHENTICATED; |
| 199 ssl.cert_id = bad_cert_id; |
| 200 ssl.cert_status = net::CERT_STATUS_DATE_INVALID; |
| 201 ssl.security_bits = 81; // No error if > 80. |
| 202 int status = 0; |
| 203 status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1); |
| 204 status = SetSSLCipherSuite(status, TLS_RSA_WITH_AES_256_CBC_SHA256); |
| 205 ssl.connection_status = status; |
| 206 |
| 207 scoped_ptr<WebsiteSettingsModel> model( |
| 208 new WebsiteSettingsModel(profile(), url, ssl, cert_store)); |
| 209 EXPECT_EQ(WebsiteSettingsModel::SITE_CONNECTION_STATUS_ENCRYPTED, |
| 210 model->site_connection_status()); |
| 211 EXPECT_EQ(WebsiteSettingsModel::SITE_IDENTITY_STATUS_ERROR, |
| 212 model->site_identity_status()); |
| 213 EXPECT_EQ(string16(), model->organization_name()); |
| 214 cert_store->RemoveCertsForRenderProcesHost(render_process_host_id); |
| 215 } |
| 216 |
| 217 TEST_F(WebsiteSettingsModelTest, HTTPSRevocationError) { |
| 218 GURL url = GURL("https://www.example.com"); |
| 219 |
| 220 SSLStatus ssl; |
| 221 ssl.security_style = content::SECURITY_STYLE_AUTHENTICATED; |
| 222 ssl.cert_id = cert_id_; |
| 223 ssl.cert_status = net::CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 224 ssl.security_bits = 81; // No error if > 80. |
| 225 int status = 0; |
| 226 status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1); |
| 227 status = SetSSLCipherSuite(status, TLS_RSA_WITH_AES_256_CBC_SHA256); |
| 228 ssl.connection_status = status; |
| 229 |
| 230 scoped_ptr<WebsiteSettingsModel> model( |
| 231 new WebsiteSettingsModel(profile(), url, ssl, &cert_store_)); |
| 232 EXPECT_EQ(WebsiteSettingsModel::SITE_CONNECTION_STATUS_ENCRYPTED, |
| 233 model->site_connection_status()); |
| 234 EXPECT_EQ(WebsiteSettingsModel::SITE_IDENTITY_STATUS_CERT_REVOCATION_UNKNOWN, |
| 235 model->site_identity_status()); |
| 236 EXPECT_EQ(string16(), model->organization_name()); |
| 237 } |
| 238 |
| 239 TEST_F(WebsiteSettingsModelTest, HTTPSConnectionError) { |
| 240 GURL url = GURL("https://www.example.com"); |
| 241 |
| 242 SSLStatus ssl; |
| 243 ssl.security_style = content::SECURITY_STYLE_AUTHENTICATED; |
| 244 ssl.cert_id = cert_id_; |
| 245 ssl.cert_status = 0; |
| 246 ssl.security_bits = 1; |
| 247 int status = 0; |
| 248 status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1); |
| 249 status = SetSSLCipherSuite(status, TLS_RSA_WITH_AES_256_CBC_SHA256); |
| 250 ssl.connection_status = status; |
| 251 |
| 252 scoped_ptr<WebsiteSettingsModel> model( |
| 253 new WebsiteSettingsModel(profile(), url, ssl, &cert_store_)); |
| 254 EXPECT_EQ(WebsiteSettingsModel::SITE_CONNECTION_STATUS_ENCRYPTED_ERROR, |
| 255 model->site_connection_status()); |
| 256 EXPECT_EQ(WebsiteSettingsModel::SITE_IDENTITY_STATUS_CERT, |
| 257 model->site_identity_status()); |
| 258 EXPECT_EQ(string16(), model->organization_name()); |
| 259 } |
OLD | NEW |