Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1231)

Side by Side Diff: components/security_state/security_state_model_unittest.cc

Issue 2448943002: Refactor SecurityStateModel/Clients for simplicity and reusability. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/security_state/security_state_model.h"
6
7 #include <stdint.h>
8
9 #include "base/command_line.h"
10 #include "components/security_state/security_state_model_client.h"
11 #include "components/security_state/switches.h"
12 #include "net/cert/x509_certificate.h"
13 #include "net/ssl/ssl_cipher_suite_names.h"
14 #include "net/ssl/ssl_connection_status_flags.h"
15 #include "net/test/cert_test_util.h"
16 #include "net/test/test_certificate_data.h"
17 #include "net/test/test_data_directory.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace security_state {
21
22 namespace {
23
24 const char kHttpsUrl[] = "https://foo.test";
25 const char kHttpUrl[] = "http://foo.test";
26
27 class TestSecurityStateModelClient : public SecurityStateModelClient {
28 public:
29 TestSecurityStateModelClient()
30 : url_(kHttpsUrl),
31 connection_status_(net::SSL_CONNECTION_VERSION_TLS1_2
32 << net::SSL_CONNECTION_VERSION_SHIFT),
33 cert_status_(net::CERT_STATUS_SHA1_SIGNATURE_PRESENT),
34 displayed_mixed_content_(false),
35 ran_mixed_content_(false),
36 fails_malware_check_(false),
37 displayed_password_field_on_http_(false),
38 displayed_credit_card_field_on_http_(false) {
39 cert_ =
40 net::ImportCertFromFile(net::GetTestCertsDirectory(), "sha1_2016.pem");
41 }
42 ~TestSecurityStateModelClient() override {}
43
44 void set_connection_status(int connection_status) {
45 connection_status_ = connection_status;
46 }
47 void SetCipherSuite(uint16_t ciphersuite) {
48 net::SSLConnectionStatusSetCipherSuite(ciphersuite, &connection_status_);
49 }
50 void AddCertStatus(net::CertStatus cert_status) {
51 cert_status_ |= cert_status;
52 }
53 void SetDisplayedMixedContent(bool displayed_mixed_content) {
54 displayed_mixed_content_ = displayed_mixed_content;
55 }
56 void SetRanMixedContent(bool ran_mixed_content) {
57 ran_mixed_content_ = ran_mixed_content;
58 }
59 void set_fails_malware_check(bool fails_malware_check) {
60 fails_malware_check_ = fails_malware_check;
61 }
62 void set_displayed_password_field_on_http(
63 bool displayed_password_field_on_http) {
64 displayed_password_field_on_http_ = displayed_password_field_on_http;
65 }
66 void set_displayed_credit_card_field_on_http(
67 bool displayed_credit_card_field_on_http) {
68 displayed_credit_card_field_on_http_ = displayed_credit_card_field_on_http;
69 }
70
71 void UseHttpUrl() { url_ = GURL(kHttpUrl); }
72
73 // SecurityStateModelClient:
74 void GetVisibleSecurityState(
75 SecurityStateModel::VisibleSecurityState* state) override {
76 state->connection_info_initialized = true;
77 state->url = url_;
78 state->certificate = cert_;
79 state->cert_status = cert_status_;
80 state->connection_status = connection_status_;
81 state->security_bits = 256;
82 state->displayed_mixed_content = displayed_mixed_content_;
83 state->ran_mixed_content = ran_mixed_content_;
84 state->fails_malware_check = fails_malware_check_;
85 state->displayed_password_field_on_http = displayed_password_field_on_http_;
86 state->displayed_credit_card_field_on_http =
87 displayed_credit_card_field_on_http_;
88 }
89
90 bool UsedPolicyInstalledCertificate() override { return false; }
91
92 bool IsOriginSecure(const GURL& url) override {
93 return url_ == GURL(kHttpsUrl);
94 }
95
96 private:
97 GURL url_;
98 scoped_refptr<net::X509Certificate> cert_;
99 int connection_status_;
100 net::CertStatus cert_status_;
101 bool displayed_mixed_content_;
102 bool ran_mixed_content_;
103 bool fails_malware_check_;
104 bool displayed_password_field_on_http_;
105 bool displayed_credit_card_field_on_http_;
106 };
107
108 // Tests that SHA1-signed certificates expiring in 2016 downgrade the
109 // security state of the page.
110 TEST(SecurityStateModelTest, SHA1Warning) {
111 TestSecurityStateModelClient client;
112 SecurityStateModel model;
113 model.SetClient(&client);
114 SecurityStateModel::SecurityInfo security_info;
115 model.GetSecurityInfo(&security_info);
116 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
117 security_info.sha1_deprecation_status);
118 EXPECT_EQ(SecurityStateModel::NONE, security_info.security_level);
119 }
120
121 // Tests that SHA1 warnings don't interfere with the handling of mixed
122 // content.
123 TEST(SecurityStateModelTest, SHA1WarningMixedContent) {
124 TestSecurityStateModelClient client;
125 SecurityStateModel model;
126 model.SetClient(&client);
127 client.SetDisplayedMixedContent(true);
128 SecurityStateModel::SecurityInfo security_info1;
129 model.GetSecurityInfo(&security_info1);
130 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
131 security_info1.sha1_deprecation_status);
132 EXPECT_EQ(SecurityStateModel::CONTENT_STATUS_DISPLAYED,
133 security_info1.mixed_content_status);
134 EXPECT_EQ(SecurityStateModel::NONE, security_info1.security_level);
135
136 client.SetDisplayedMixedContent(false);
137 client.SetRanMixedContent(true);
138 SecurityStateModel::SecurityInfo security_info2;
139 model.GetSecurityInfo(&security_info2);
140 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
141 security_info2.sha1_deprecation_status);
142 EXPECT_EQ(SecurityStateModel::CONTENT_STATUS_RAN,
143 security_info2.mixed_content_status);
144 EXPECT_EQ(SecurityStateModel::DANGEROUS, security_info2.security_level);
145 }
146
147 // Tests that SHA1 warnings don't interfere with the handling of major
148 // cert errors.
149 TEST(SecurityStateModelTest, SHA1WarningBrokenHTTPS) {
150 TestSecurityStateModelClient client;
151 SecurityStateModel model;
152 model.SetClient(&client);
153 client.AddCertStatus(net::CERT_STATUS_DATE_INVALID);
154 SecurityStateModel::SecurityInfo security_info;
155 model.GetSecurityInfo(&security_info);
156 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
157 security_info.sha1_deprecation_status);
158 EXPECT_EQ(SecurityStateModel::DANGEROUS, security_info.security_level);
159 }
160
161 // Tests that |security_info.is_secure_protocol_and_ciphersuite| is
162 // computed correctly.
163 TEST(SecurityStateModelTest, SecureProtocolAndCiphersuite) {
164 TestSecurityStateModelClient client;
165 SecurityStateModel model;
166 model.SetClient(&client);
167 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from
168 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param eters-4
169 const uint16_t ciphersuite = 0xc02f;
170 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2
171 << net::SSL_CONNECTION_VERSION_SHIFT);
172 client.SetCipherSuite(ciphersuite);
173 SecurityStateModel::SecurityInfo security_info;
174 model.GetSecurityInfo(&security_info);
175 EXPECT_EQ(net::OBSOLETE_SSL_NONE, security_info.obsolete_ssl_status);
176 }
177
178 TEST(SecurityStateModelTest, NonsecureProtocol) {
179 TestSecurityStateModelClient client;
180 SecurityStateModel model;
181 model.SetClient(&client);
182 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from
183 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param eters-4
184 const uint16_t ciphersuite = 0xc02f;
185 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_1
186 << net::SSL_CONNECTION_VERSION_SHIFT);
187 client.SetCipherSuite(ciphersuite);
188 SecurityStateModel::SecurityInfo security_info;
189 model.GetSecurityInfo(&security_info);
190 EXPECT_EQ(net::OBSOLETE_SSL_MASK_PROTOCOL, security_info.obsolete_ssl_status);
191 }
192
193 TEST(SecurityStateModelTest, NonsecureCiphersuite) {
194 TestSecurityStateModelClient client;
195 SecurityStateModel model;
196 model.SetClient(&client);
197 // TLS_RSA_WITH_AES_128_CCM_8 from
198 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param eters-4
199 const uint16_t ciphersuite = 0xc0a0;
200 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2
201 << net::SSL_CONNECTION_VERSION_SHIFT);
202 client.SetCipherSuite(ciphersuite);
203 SecurityStateModel::SecurityInfo security_info;
204 model.GetSecurityInfo(&security_info);
205 EXPECT_EQ(net::OBSOLETE_SSL_MASK_KEY_EXCHANGE | net::OBSOLETE_SSL_MASK_CIPHER,
206 security_info.obsolete_ssl_status);
207 }
208
209 // Tests that the malware/phishing status is set, and it overrides valid HTTPS.
210 TEST(SecurityStateModelTest, MalwareOverride) {
211 TestSecurityStateModelClient client;
212 SecurityStateModel model;
213 model.SetClient(&client);
214 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from
215 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param eters-4
216 const uint16_t ciphersuite = 0xc02f;
217 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2
218 << net::SSL_CONNECTION_VERSION_SHIFT);
219 client.SetCipherSuite(ciphersuite);
220 client.set_fails_malware_check(true);
221 SecurityStateModel::SecurityInfo security_info;
222 model.GetSecurityInfo(&security_info);
223 EXPECT_TRUE(security_info.fails_malware_check);
224 EXPECT_EQ(SecurityStateModel::DANGEROUS, security_info.security_level);
225 }
226
227 // Tests that the malware/phishing status is set, even if other connection info
228 // is not available.
229 TEST(SecurityStateModelTest, MalwareWithoutCOnnectionState) {
230 TestSecurityStateModelClient client;
231 SecurityStateModel model;
232 model.SetClient(&client);
233 client.set_fails_malware_check(true);
234 SecurityStateModel::SecurityInfo security_info;
235 model.GetSecurityInfo(&security_info);
236 EXPECT_TRUE(security_info.fails_malware_check);
237 EXPECT_EQ(SecurityStateModel::DANGEROUS, security_info.security_level);
238 }
239
240 // Tests that password fields cause the security level to be downgraded
241 // to HTTP_SHOW_WARNING when the command-line switch is set.
242 TEST(SecurityStateModelTest, PasswordFieldWarning) {
243 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
244 switches::kMarkHttpAs,
245 switches::kMarkHttpWithPasswordsOrCcWithChip);
246 TestSecurityStateModelClient client;
247 client.UseHttpUrl();
248 SecurityStateModel model;
249 model.SetClient(&client);
250 client.set_displayed_password_field_on_http(true);
251 SecurityStateModel::SecurityInfo security_info;
252 model.GetSecurityInfo(&security_info);
253 EXPECT_TRUE(security_info.displayed_private_user_data_input_on_http);
254 EXPECT_EQ(SecurityStateModel::HTTP_SHOW_WARNING,
255 security_info.security_level);
256 }
257
258 // Tests that credit card fields cause the security level to be downgraded
259 // to HTTP_SHOW_WARNING when the command-line switch is set.
260 TEST(SecurityStateModelTest, CreditCardFieldWarning) {
261 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
262 switches::kMarkHttpAs,
263 switches::kMarkHttpWithPasswordsOrCcWithChip);
264 TestSecurityStateModelClient client;
265 client.UseHttpUrl();
266 SecurityStateModel model;
267 model.SetClient(&client);
268 client.set_displayed_credit_card_field_on_http(true);
269 SecurityStateModel::SecurityInfo security_info;
270 model.GetSecurityInfo(&security_info);
271 EXPECT_TRUE(security_info.displayed_private_user_data_input_on_http);
272 EXPECT_EQ(SecurityStateModel::HTTP_SHOW_WARNING,
273 security_info.security_level);
274 }
275
276 // Tests that neither password nor credit fields cause the security
277 // level to be downgraded to HTTP_SHOW_WARNING when the command-line switch
278 // is NOT set.
279 TEST(SecurityStateModelTest, HttpWarningNotSetWithoutSwitch) {
280 TestSecurityStateModelClient client;
281 client.UseHttpUrl();
282 SecurityStateModel model;
283 model.SetClient(&client);
284 client.set_displayed_password_field_on_http(true);
285 client.set_displayed_credit_card_field_on_http(true);
286 SecurityStateModel::SecurityInfo security_info;
287 model.GetSecurityInfo(&security_info);
288 EXPECT_TRUE(security_info.displayed_private_user_data_input_on_http);
289 EXPECT_EQ(SecurityStateModel::NONE, security_info.security_level);
290 }
291
292 // Tests that |displayed_private_user_data_input_on_http| is not set
293 // when the corresponding VisibleSecurityState flags are not set.
294 TEST(SecurityStateModelTest, PrivateUserDataNotSet) {
295 TestSecurityStateModelClient client;
296 client.UseHttpUrl();
297 SecurityStateModel model;
298 model.SetClient(&client);
299 SecurityStateModel::SecurityInfo security_info;
300 model.GetSecurityInfo(&security_info);
301 EXPECT_FALSE(security_info.displayed_private_user_data_input_on_http);
302 EXPECT_EQ(SecurityStateModel::NONE, security_info.security_level);
303 }
304
305 } // namespace
306
307 } // namespace security_state
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698