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

Side by Side Diff: chrome/browser/ssl/security_state_model_unittest.cc

Issue 1539043002: Pull SecurityStateModel out into a component (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 11 months 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 "chrome/browser/ssl/security_state_model.h"
6
7 #include <stdint.h>
8
9 #include "chrome/browser/ssl/security_state_model_client.h"
10 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "content/public/browser/cert_store.h"
13 #include "content/public/common/origin_util.h"
14 #include "content/public/test/mock_render_process_host.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "net/base/test_data_directory.h"
17 #include "net/cert/x509_certificate.h"
18 #include "net/ssl/ssl_connection_status_flags.h"
19 #include "net/test/cert_test_util.h"
20 #include "net/test/test_certificate_data.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 namespace {
24
25 const char kUrl[] = "https://foo.test";
26
27 class TestSecurityStateModelClient : public SecurityStateModelClient {
28 public:
29 TestSecurityStateModelClient()
30 : initial_security_level_(SecurityStateModel::SECURE),
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 cert_ =
37 net::ImportCertFromFile(net::GetTestCertsDirectory(), "sha1_2016.pem");
38 }
39 ~TestSecurityStateModelClient() override {}
40
41 void set_connection_status(int connection_status) {
42 connection_status_ = connection_status;
43 }
44 void SetCipherSuite(uint16_t ciphersuite) {
45 net::SSLConnectionStatusSetCipherSuite(ciphersuite, &connection_status_);
46 }
47 void AddCertStatus(net::CertStatus cert_status) {
48 cert_status_ |= cert_status;
49 }
50 void SetDisplayedMixedContent(bool displayed_mixed_content) {
51 displayed_mixed_content_ = displayed_mixed_content;
52 }
53 void SetRanMixedContent(bool ran_mixed_content) {
54 ran_mixed_content_ = ran_mixed_content;
55 }
56 void set_initial_security_level(
57 SecurityStateModel::SecurityLevel security_level) {
58 initial_security_level_ = security_level;
59 }
60
61 // SecurityStateModelClient:
62 void GetVisibleSecurityState(
63 SecurityStateModel::VisibleSecurityState* state) override {
64 state->initialized = true;
65 state->url = GURL(kUrl);
66 state->initial_security_level = initial_security_level_;
67 state->cert_id = 1;
68 state->cert_status = cert_status_;
69 state->connection_status = connection_status_;
70 state->security_bits = 256;
71 state->displayed_mixed_content = displayed_mixed_content_;
72 state->ran_mixed_content = ran_mixed_content_;
73 }
74
75 bool RetrieveCert(scoped_refptr<net::X509Certificate>* cert) override {
76 *cert = cert_;
77 return true;
78 }
79
80 bool UsedPolicyInstalledCertificate() override { return false; }
81
82 bool IsOriginSecure(const GURL& url) override {
83 return content::IsOriginSecure(url);
84 }
85
86 private:
87 SecurityStateModel::SecurityLevel initial_security_level_;
88 scoped_refptr<net::X509Certificate> cert_;
89 int connection_status_;
90 net::CertStatus cert_status_;
91 bool displayed_mixed_content_;
92 bool ran_mixed_content_;
93 };
94
95 class SecurityStateModelTest : public ChromeRenderViewHostTestHarness {};
96
97 // Tests that SHA1-signed certificates expiring in 2016 downgrade the
98 // security state of the page.
99 TEST_F(SecurityStateModelTest, SHA1Warning) {
100 TestSecurityStateModelClient client;
101 SecurityStateModel model;
102 model.SetClient(&client);
103 const SecurityStateModel::SecurityInfo& security_info =
104 model.GetSecurityInfo();
105 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
106 security_info.sha1_deprecation_status);
107 EXPECT_EQ(SecurityStateModel::NONE, security_info.security_level);
108 }
109
110 // Tests that SHA1 warnings don't interfere with the handling of mixed
111 // content.
112 TEST_F(SecurityStateModelTest, SHA1WarningMixedContent) {
113 TestSecurityStateModelClient client;
114 SecurityStateModel model;
115 model.SetClient(&client);
116 client.SetDisplayedMixedContent(true);
117 const SecurityStateModel::SecurityInfo& security_info1 =
118 model.GetSecurityInfo();
119 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
120 security_info1.sha1_deprecation_status);
121 EXPECT_EQ(SecurityStateModel::DISPLAYED_MIXED_CONTENT,
122 security_info1.mixed_content_status);
123 EXPECT_EQ(SecurityStateModel::NONE, security_info1.security_level);
124
125 client.set_initial_security_level(SecurityStateModel::SECURITY_ERROR);
126 client.SetDisplayedMixedContent(false);
127 client.SetRanMixedContent(true);
128 const SecurityStateModel::SecurityInfo& security_info2 =
129 model.GetSecurityInfo();
130 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
131 security_info2.sha1_deprecation_status);
132 EXPECT_EQ(SecurityStateModel::RAN_MIXED_CONTENT,
133 security_info2.mixed_content_status);
134 EXPECT_EQ(SecurityStateModel::SECURITY_ERROR, security_info2.security_level);
135 }
136
137 // Tests that SHA1 warnings don't interfere with the handling of major
138 // cert errors.
139 TEST_F(SecurityStateModelTest, SHA1WarningBrokenHTTPS) {
140 TestSecurityStateModelClient client;
141 SecurityStateModel model;
142 model.SetClient(&client);
143 client.set_initial_security_level(SecurityStateModel::SECURITY_ERROR);
144 client.AddCertStatus(net::CERT_STATUS_DATE_INVALID);
145 const SecurityStateModel::SecurityInfo& security_info =
146 model.GetSecurityInfo();
147 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
148 security_info.sha1_deprecation_status);
149 EXPECT_EQ(SecurityStateModel::SECURITY_ERROR, security_info.security_level);
150 }
151
152 // Tests that |security_info.is_secure_protocol_and_ciphersuite| is
153 // computed correctly.
154 TEST_F(SecurityStateModelTest, SecureProtocolAndCiphersuite) {
155 TestSecurityStateModelClient client;
156 SecurityStateModel model;
157 model.SetClient(&client);
158 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from
159 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param eters-4
160 const uint16_t ciphersuite = 0xc02f;
161 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2
162 << net::SSL_CONNECTION_VERSION_SHIFT);
163 client.SetCipherSuite(ciphersuite);
164 const SecurityStateModel::SecurityInfo& security_info =
165 model.GetSecurityInfo();
166 EXPECT_TRUE(security_info.is_secure_protocol_and_ciphersuite);
167 }
168
169 TEST_F(SecurityStateModelTest, NonsecureProtocol) {
170 TestSecurityStateModelClient client;
171 SecurityStateModel model;
172 model.SetClient(&client);
173 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from
174 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param eters-4
175 const uint16_t ciphersuite = 0xc02f;
176 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_1
177 << net::SSL_CONNECTION_VERSION_SHIFT);
178 client.SetCipherSuite(ciphersuite);
179 const SecurityStateModel::SecurityInfo& security_info =
180 model.GetSecurityInfo();
181 EXPECT_FALSE(security_info.is_secure_protocol_and_ciphersuite);
182 }
183
184 TEST_F(SecurityStateModelTest, NonsecureCiphersuite) {
185 TestSecurityStateModelClient client;
186 SecurityStateModel model;
187 model.SetClient(&client);
188 // TLS_RSA_WITH_AES_128_CCM_8 from
189 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param eters-4
190 const uint16_t ciphersuite = 0xc0a0;
191 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2
192 << net::SSL_CONNECTION_VERSION_SHIFT);
193 client.SetCipherSuite(ciphersuite);
194 const SecurityStateModel::SecurityInfo& security_info =
195 model.GetSecurityInfo();
196 EXPECT_FALSE(security_info.is_secure_protocol_and_ciphersuite);
197 }
198
199 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/ssl/security_state_model_client.h ('k') | chrome/browser/ssl/ssl_browser_tests.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698