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/utf_string_conversions.h" | |
8 #include "chrome/test/base/testing_profile.h" | |
9 #include "content/browser/cert_store.cc" | |
10 #include "content/public/common/ssl_status.h" | |
11 #include "net/base/cert_status_flags.h" | |
12 #include "net/base/ssl_connection_status_flags.h" | |
13 #include "net/base/test_certificate_data.h" | |
14 #include "net/base/x509_certificate.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 using content::SSLStatus; | |
18 | |
19 namespace { | |
20 | |
21 // SSL cipher suite like specified in RFC5246 Appendix A.5. "The Cipher Suite". | |
22 static int TLS_RSA_WITH_AES_256_CBC_SHA256 = 0x3D; | |
23 | |
24 int SetSSLVersion(int connection_status, int version) { | |
25 // Clear SSL version bits (Bits 20, 21 and 22). | |
26 connection_status &= | |
27 ~(net::SSL_CONNECTION_VERSION_MASK << net::SSL_CONNECTION_VERSION_MASK); | |
wtc
2012/02/17 00:48:56
BUG: the second one (to the right of <<) should be
markusheintz_
2012/02/20 18:14:12
Done.
| |
28 int bitmask = version << net::SSL_CONNECTION_VERSION_SHIFT; | |
29 return bitmask | connection_status; | |
30 } | |
31 | |
32 int SetSSLCipherSuite(int connection_status, int cipher_suite) { | |
33 // Clear cipher suite bits (the 16 lowest bits). | |
34 connection_status &= ~net::SSL_CONNECTION_CIPHERSUITE_MASK; | |
35 return cipher_suite | connection_status; | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 class WebsiteSettingsModelTest : public testing::Test { | |
41 public: | |
42 WebsiteSettingsModelTest() : profile_(new TestingProfile()), | |
43 cert_id_(0), | |
44 ev_cert_id_(0), | |
45 bad_cert_id_(0) { | |
46 InitCertStore(); | |
47 } | |
48 | |
49 void InitCertStore() { | |
50 // TODO(markusheintz): I wish there was an easy way to create a mock | |
51 // CertStore. | |
52 int render_process_host_id = 1; | |
53 base::Time start_date = base::Time::Now(); | |
54 base::Time expiration_date = base::Time::FromInternalValue( | |
55 start_date.ToInternalValue() + base::Time::kMicrosecondsPerWeek); | |
56 | |
57 net::X509Certificate* cert = | |
58 new net::X509Certificate("subject", | |
59 "issuer", | |
60 start_date, | |
61 expiration_date); | |
62 cert_id_ = CertStore::GetInstance()->StoreCert(cert, | |
63 render_process_host_id); | |
64 cert = net::X509Certificate::CreateFromBytes( | |
65 reinterpret_cast<const char*>(google_der), sizeof(google_der)); | |
66 ev_cert_id_ = CertStore::GetInstance()->StoreCert(cert, | |
67 render_process_host_id); | |
68 cert = new net::X509Certificate("subject", | |
69 "issuer", | |
70 base::Time(), | |
71 base::Time()); | |
72 bad_cert_id_ = CertStore::GetInstance()->StoreCert(cert, | |
73 render_process_host_id); | |
74 } | |
75 | |
76 int cert_id() const { return cert_id_; } | |
77 | |
78 int ev_cert_id() const { return ev_cert_id_; } | |
79 | |
80 int bad_cert_id() const { return bad_cert_id_; } | |
81 | |
82 Profile* profile() const { return profile_.get(); } | |
83 | |
84 private: | |
85 scoped_ptr<Profile> profile_; | |
86 int cert_id_; | |
87 int ev_cert_id_; | |
88 int bad_cert_id_; | |
89 }; | |
90 | |
91 TEST_F(WebsiteSettingsModelTest, HTTPConnection) { | |
92 GURL url = GURL("http://www.example.com"); | |
93 | |
94 SSLStatus ssl; | |
95 ssl.security_style = content::SECURITY_STYLE_UNAUTHENTICATED; | |
96 | |
97 scoped_ptr<WebsiteSettingsModel> model( | |
98 new WebsiteSettingsModel(profile(), url, ssl)); | |
99 EXPECT_EQ(WebsiteSettingsModel::SITE_CONNECTION_STATUS_UNENCRYPTED, | |
100 model->site_connection_status()); | |
101 EXPECT_EQ(WebsiteSettingsModel::SITE_IDENTITY_STATUS_NO_CERT, | |
102 model->site_identity_status()); | |
103 EXPECT_EQ(string16(), model->organization_name()); | |
104 } | |
105 | |
106 TEST_F(WebsiteSettingsModelTest, HTTPSConnection) { | |
107 GURL url = GURL("https://www.example.com"); | |
108 | |
109 SSLStatus ssl; | |
110 ssl.security_style = content::SECURITY_STYLE_AUTHENTICATED; | |
111 ssl.cert_id = cert_id(); | |
112 ssl.cert_status = 0; | |
113 ssl.security_bits = 81; // No error if > 80. | |
114 int status = 0; | |
115 status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1); | |
116 status = SetSSLCipherSuite(status, TLS_RSA_WITH_AES_256_CBC_SHA256); | |
117 ssl.connection_status = status; | |
118 | |
119 scoped_ptr<WebsiteSettingsModel> model( | |
120 new WebsiteSettingsModel(profile(), url, ssl)); | |
121 EXPECT_EQ(WebsiteSettingsModel::SITE_CONNECTION_STATUS_ENCRYPTED, | |
122 model->site_connection_status()); | |
123 EXPECT_EQ(WebsiteSettingsModel::SITE_IDENTITY_STATUS_CERT, | |
124 model->site_identity_status()); | |
125 EXPECT_EQ(string16(), model->organization_name()); | |
126 } | |
127 | |
128 TEST_F(WebsiteSettingsModelTest, HTTPSMixedContent) { | |
129 GURL url = GURL("https://www.example.com"); | |
130 | |
131 SSLStatus ssl; | |
132 ssl.security_style = content::SECURITY_STYLE_AUTHENTICATED; | |
133 ssl.cert_id = cert_id(); | |
134 ssl.cert_status = 0; | |
135 ssl.security_bits = 81; // No error if > 80. | |
136 ssl.content_status = SSLStatus::DISPLAYED_INSECURE_CONTENT; | |
137 int status = 0; | |
138 status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1); | |
139 status = SetSSLCipherSuite(status, TLS_RSA_WITH_AES_256_CBC_SHA256); | |
140 ssl.connection_status = status; | |
141 | |
142 scoped_ptr<WebsiteSettingsModel> model( | |
143 new WebsiteSettingsModel(profile(), url, ssl)); | |
144 EXPECT_EQ(WebsiteSettingsModel::SITE_CONNECTION_STATUS_MIXED_CONTENT, | |
145 model->site_connection_status()); | |
146 EXPECT_EQ(WebsiteSettingsModel::SITE_IDENTITY_STATUS_CERT, | |
147 model->site_identity_status()); | |
148 EXPECT_EQ(string16(), model->organization_name()); | |
149 } | |
150 | |
151 TEST_F(WebsiteSettingsModelTest, HTTPSEVCert) { | |
152 GURL url = GURL("https://www.example.com"); | |
153 | |
154 SSLStatus ssl; | |
155 ssl.security_style = content::SECURITY_STYLE_AUTHENTICATED; | |
156 ssl.cert_id = ev_cert_id(); | |
157 ssl.cert_status = net::CERT_STATUS_IS_EV; | |
158 ssl.security_bits = 81; // No error if > 80. | |
159 ssl.content_status = SSLStatus::DISPLAYED_INSECURE_CONTENT; | |
160 int status = 0; | |
161 status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1); | |
162 status = SetSSLCipherSuite(status, TLS_RSA_WITH_AES_256_CBC_SHA256); | |
163 ssl.connection_status = status; | |
164 | |
165 scoped_ptr<WebsiteSettingsModel> model( | |
166 new WebsiteSettingsModel(profile(), url, ssl)); | |
167 EXPECT_EQ(WebsiteSettingsModel::SITE_CONNECTION_STATUS_MIXED_CONTENT, | |
168 model->site_connection_status()); | |
169 EXPECT_EQ(WebsiteSettingsModel::SITE_IDENTITY_STATUS_EV_CERT, | |
170 model->site_identity_status()); | |
171 EXPECT_EQ(UTF8ToUTF16("Google Inc"), model->organization_name()); | |
172 } | |
173 | |
174 TEST_F(WebsiteSettingsModelTest, HTTPSBadCertificate) { | |
175 GURL url = GURL("https://www.example.com"); | |
176 | |
177 SSLStatus ssl; | |
178 ssl.security_style = content::SECURITY_STYLE_AUTHENTICATED; | |
179 ssl.cert_id = bad_cert_id(); | |
180 ssl.cert_status = net::CERT_STATUS_DATE_INVALID; | |
181 ssl.security_bits = 81; // No error if > 80. | |
182 int status = 0; | |
183 status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1); | |
184 status = SetSSLCipherSuite(status, TLS_RSA_WITH_AES_256_CBC_SHA256); | |
185 ssl.connection_status = status; | |
186 | |
187 scoped_ptr<WebsiteSettingsModel> model( | |
188 new WebsiteSettingsModel(profile(), url, ssl)); | |
189 EXPECT_EQ(WebsiteSettingsModel::SITE_CONNECTION_STATUS_ENCRYPTED, | |
190 model->site_connection_status()); | |
191 EXPECT_EQ(WebsiteSettingsModel::SITE_IDENTITY_STATUS_ERROR, | |
192 model->site_identity_status()); | |
193 EXPECT_EQ(string16(), model->organization_name()); | |
194 } | |
195 | |
196 TEST_F(WebsiteSettingsModelTest, HTTPSRevocationError) { | |
197 GURL url = GURL("https://www.example.com"); | |
198 | |
199 SSLStatus ssl; | |
200 ssl.security_style = content::SECURITY_STYLE_AUTHENTICATED; | |
201 ssl.cert_id = cert_id(); | |
202 ssl.cert_status = net::CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; | |
203 ssl.security_bits = 81; // No error if > 80. | |
204 int status = 0; | |
205 status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1); | |
206 status = SetSSLCipherSuite(status, TLS_RSA_WITH_AES_256_CBC_SHA256); | |
207 ssl.connection_status = status; | |
208 | |
209 scoped_ptr<WebsiteSettingsModel> model( | |
210 new WebsiteSettingsModel(profile(), url, ssl)); | |
211 EXPECT_EQ(WebsiteSettingsModel::SITE_CONNECTION_STATUS_ENCRYPTED, | |
212 model->site_connection_status()); | |
213 EXPECT_EQ(WebsiteSettingsModel::SITE_IDENTITY_STATUS_CERT_REVOCATION_UNKNOWN, | |
214 model->site_identity_status()); | |
215 EXPECT_EQ(string16(), model->organization_name()); | |
216 } | |
217 | |
218 TEST_F(WebsiteSettingsModelTest, HTTPSConnectionError) { | |
219 GURL url = GURL("https://www.example.com"); | |
220 | |
221 SSLStatus ssl; | |
222 ssl.security_style = content::SECURITY_STYLE_AUTHENTICATED; | |
223 ssl.cert_id = cert_id(); | |
224 ssl.cert_status = 0; | |
225 ssl.security_bits = 1; | |
226 int status = 0; | |
227 status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1); | |
228 status = SetSSLCipherSuite(status, TLS_RSA_WITH_AES_256_CBC_SHA256); | |
229 ssl.connection_status = status; | |
230 | |
231 scoped_ptr<WebsiteSettingsModel> model( | |
232 new WebsiteSettingsModel(profile(), url, ssl)); | |
233 EXPECT_EQ(WebsiteSettingsModel::SITE_CONNECTION_STATUS_ENCRYPTED_ERROR, | |
234 model->site_connection_status()); | |
235 EXPECT_EQ(WebsiteSettingsModel::SITE_IDENTITY_STATUS_CERT, | |
236 model->site_identity_status()); | |
237 EXPECT_EQ(string16(), model->organization_name()); | |
238 } | |
OLD | NEW |