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

Side by Side Diff: components/security_state/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: unit test debugging Created 5 years 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
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 "chrome/browser/ssl/security_state_model.h" 5 #include "components/security_state/security_state_model.h"
6 6
7 #include "chrome/browser/ssl/security_state_model_client.h" 7 #include "components/security_state/security_state_model_client.h"
8 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "content/public/browser/cert_store.h"
11 #include "content/public/common/origin_util.h"
12 #include "content/public/test/mock_render_process_host.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "net/base/test_data_directory.h" 8 #include "net/base/test_data_directory.h"
15 #include "net/cert/x509_certificate.h" 9 #include "net/cert/x509_certificate.h"
16 #include "net/ssl/ssl_connection_status_flags.h" 10 #include "net/ssl/ssl_connection_status_flags.h"
17 #include "net/test/cert_test_util.h" 11 #include "net/test/cert_test_util.h"
18 #include "net/test/test_certificate_data.h" 12 #include "net/test/test_certificate_data.h"
19 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
20 14
15 namespace security_state {
16
21 namespace { 17 namespace {
22 18
23 const char kUrl[] = "https://foo.test"; 19 const char kUrl[] = "https://foo.test";
24 20
25 class TestSecurityStateModelClient : public SecurityStateModelClient { 21 class TestSecurityStateModelClient : public SecurityStateModelClient {
26 public: 22 public:
27 TestSecurityStateModelClient() 23 TestSecurityStateModelClient()
28 : initial_security_level_(SecurityStateModel::SECURE), 24 : initial_security_level_(SecurityStateModel::SECURE),
29 connection_status_(net::SSL_CONNECTION_VERSION_TLS1_2 25 connection_status_(net::SSL_CONNECTION_VERSION_TLS1_2
30 << net::SSL_CONNECTION_VERSION_SHIFT), 26 << net::SSL_CONNECTION_VERSION_SHIFT),
31 cert_status_(net::CERT_STATUS_SHA1_SIGNATURE_PRESENT), 27 cert_status_(net::CERT_STATUS_SHA1_SIGNATURE_PRESENT),
32 displayed_mixed_content_(false), 28 displayed_mixed_content_(false),
33 ran_mixed_content_(false) { 29 ran_mixed_content_(false) {
30 LOG(ERROR) << "Test certs directory: " << net::GetTestCertsDirectory().value ();
blundell 2016/01/05 16:02:16 this should go before landing, of course.
estark 2016/01/05 22:59:30 Done.
34 cert_ = 31 cert_ =
35 net::ImportCertFromFile(net::GetTestCertsDirectory(), "sha1_2016.pem"); 32 net::ImportCertFromFile(net::GetTestCertsDirectory(), "sha1_2016.pem");
36 } 33 }
37 ~TestSecurityStateModelClient() override {} 34 ~TestSecurityStateModelClient() override {}
38 35
39 void set_connection_status(int connection_status) { 36 void set_connection_status(int connection_status) {
40 connection_status_ = connection_status; 37 connection_status_ = connection_status;
41 } 38 }
42 void SetCipherSuite(uint16 ciphersuite) { 39 void SetCipherSuite(uint16 ciphersuite) {
43 net::SSLConnectionStatusSetCipherSuite(ciphersuite, &connection_status_); 40 net::SSLConnectionStatusSetCipherSuite(ciphersuite, &connection_status_);
(...skipping 26 matching lines...) Expand all
70 state->ran_mixed_content = ran_mixed_content_; 67 state->ran_mixed_content = ran_mixed_content_;
71 } 68 }
72 69
73 bool RetrieveCert(scoped_refptr<net::X509Certificate>* cert) override { 70 bool RetrieveCert(scoped_refptr<net::X509Certificate>* cert) override {
74 *cert = cert_; 71 *cert = cert_;
75 return true; 72 return true;
76 } 73 }
77 74
78 bool UsedPolicyInstalledCertificate() override { return false; } 75 bool UsedPolicyInstalledCertificate() override { return false; }
79 76
80 bool IsOriginSecure(const GURL& url) override { 77 bool IsOriginSecure(const GURL& url) override { return true; }
81 return content::IsOriginSecure(url);
82 }
83 78
84 private: 79 private:
85 SecurityStateModel::SecurityLevel initial_security_level_; 80 SecurityStateModel::SecurityLevel initial_security_level_;
86 scoped_refptr<net::X509Certificate> cert_; 81 scoped_refptr<net::X509Certificate> cert_;
87 int connection_status_; 82 int connection_status_;
88 net::CertStatus cert_status_; 83 net::CertStatus cert_status_;
89 bool displayed_mixed_content_; 84 bool displayed_mixed_content_;
90 bool ran_mixed_content_; 85 bool ran_mixed_content_;
91 }; 86 };
92 87
93 class SecurityStateModelTest : public ChromeRenderViewHostTestHarness {};
94
95 // Tests that SHA1-signed certificates expiring in 2016 downgrade the 88 // Tests that SHA1-signed certificates expiring in 2016 downgrade the
96 // security state of the page. 89 // security state of the page.
97 TEST_F(SecurityStateModelTest, SHA1Warning) { 90 TEST(SecurityStateModelTest, SHA1Warning) {
98 TestSecurityStateModelClient client; 91 TestSecurityStateModelClient client;
99 SecurityStateModel model; 92 SecurityStateModel model;
100 model.SetClient(&client); 93 model.SetClient(&client);
101 const SecurityStateModel::SecurityInfo& security_info = 94 const SecurityStateModel::SecurityInfo& security_info =
102 model.GetSecurityInfo(); 95 model.GetSecurityInfo();
103 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR, 96 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
104 security_info.sha1_deprecation_status); 97 security_info.sha1_deprecation_status);
105 EXPECT_EQ(SecurityStateModel::NONE, security_info.security_level); 98 EXPECT_EQ(SecurityStateModel::NONE, security_info.security_level);
106 } 99 }
107 100
108 // Tests that SHA1 warnings don't interfere with the handling of mixed 101 // Tests that SHA1 warnings don't interfere with the handling of mixed
109 // content. 102 // content.
110 TEST_F(SecurityStateModelTest, SHA1WarningMixedContent) { 103 TEST(SecurityStateModelTest, SHA1WarningMixedContent) {
111 TestSecurityStateModelClient client; 104 TestSecurityStateModelClient client;
112 SecurityStateModel model; 105 SecurityStateModel model;
113 model.SetClient(&client); 106 model.SetClient(&client);
114 client.SetDisplayedMixedContent(true); 107 client.SetDisplayedMixedContent(true);
115 const SecurityStateModel::SecurityInfo& security_info1 = 108 const SecurityStateModel::SecurityInfo& security_info1 =
116 model.GetSecurityInfo(); 109 model.GetSecurityInfo();
117 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR, 110 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
118 security_info1.sha1_deprecation_status); 111 security_info1.sha1_deprecation_status);
119 EXPECT_EQ(SecurityStateModel::DISPLAYED_MIXED_CONTENT, 112 EXPECT_EQ(SecurityStateModel::DISPLAYED_MIXED_CONTENT,
120 security_info1.mixed_content_status); 113 security_info1.mixed_content_status);
121 EXPECT_EQ(SecurityStateModel::NONE, security_info1.security_level); 114 EXPECT_EQ(SecurityStateModel::NONE, security_info1.security_level);
122 115
123 client.set_initial_security_level(SecurityStateModel::SECURITY_ERROR); 116 client.set_initial_security_level(SecurityStateModel::SECURITY_ERROR);
124 client.SetDisplayedMixedContent(false); 117 client.SetDisplayedMixedContent(false);
125 client.SetRanMixedContent(true); 118 client.SetRanMixedContent(true);
126 const SecurityStateModel::SecurityInfo& security_info2 = 119 const SecurityStateModel::SecurityInfo& security_info2 =
127 model.GetSecurityInfo(); 120 model.GetSecurityInfo();
128 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR, 121 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
129 security_info2.sha1_deprecation_status); 122 security_info2.sha1_deprecation_status);
130 EXPECT_EQ(SecurityStateModel::RAN_MIXED_CONTENT, 123 EXPECT_EQ(SecurityStateModel::RAN_MIXED_CONTENT,
131 security_info2.mixed_content_status); 124 security_info2.mixed_content_status);
132 EXPECT_EQ(SecurityStateModel::SECURITY_ERROR, security_info2.security_level); 125 EXPECT_EQ(SecurityStateModel::SECURITY_ERROR, security_info2.security_level);
133 } 126 }
134 127
135 // Tests that SHA1 warnings don't interfere with the handling of major 128 // Tests that SHA1 warnings don't interfere with the handling of major
136 // cert errors. 129 // cert errors.
137 TEST_F(SecurityStateModelTest, SHA1WarningBrokenHTTPS) { 130 TEST(SecurityStateModelTest, SHA1WarningBrokenHTTPS) {
138 TestSecurityStateModelClient client; 131 TestSecurityStateModelClient client;
139 SecurityStateModel model; 132 SecurityStateModel model;
140 model.SetClient(&client); 133 model.SetClient(&client);
141 client.set_initial_security_level(SecurityStateModel::SECURITY_ERROR); 134 client.set_initial_security_level(SecurityStateModel::SECURITY_ERROR);
142 client.AddCertStatus(net::CERT_STATUS_DATE_INVALID); 135 client.AddCertStatus(net::CERT_STATUS_DATE_INVALID);
143 const SecurityStateModel::SecurityInfo& security_info = 136 const SecurityStateModel::SecurityInfo& security_info =
144 model.GetSecurityInfo(); 137 model.GetSecurityInfo();
145 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR, 138 EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
146 security_info.sha1_deprecation_status); 139 security_info.sha1_deprecation_status);
147 EXPECT_EQ(SecurityStateModel::SECURITY_ERROR, security_info.security_level); 140 EXPECT_EQ(SecurityStateModel::SECURITY_ERROR, security_info.security_level);
148 } 141 }
149 142
150 // Tests that |security_info.is_secure_protocol_and_ciphersuite| is 143 // Tests that |security_info.is_secure_protocol_and_ciphersuite| is
151 // computed correctly. 144 // computed correctly.
152 TEST_F(SecurityStateModelTest, SecureProtocolAndCiphersuite) { 145 TEST(SecurityStateModelTest, SecureProtocolAndCiphersuite) {
153 TestSecurityStateModelClient client; 146 TestSecurityStateModelClient client;
154 SecurityStateModel model; 147 SecurityStateModel model;
155 model.SetClient(&client); 148 model.SetClient(&client);
156 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from 149 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from
157 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param eters-4 150 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param eters-4
158 const uint16 ciphersuite = 0xc02f; 151 const uint16 ciphersuite = 0xc02f;
159 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2 152 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2
160 << net::SSL_CONNECTION_VERSION_SHIFT); 153 << net::SSL_CONNECTION_VERSION_SHIFT);
161 client.SetCipherSuite(ciphersuite); 154 client.SetCipherSuite(ciphersuite);
162 const SecurityStateModel::SecurityInfo& security_info = 155 const SecurityStateModel::SecurityInfo& security_info =
163 model.GetSecurityInfo(); 156 model.GetSecurityInfo();
164 EXPECT_TRUE(security_info.is_secure_protocol_and_ciphersuite); 157 EXPECT_TRUE(security_info.is_secure_protocol_and_ciphersuite);
165 } 158 }
166 159
167 TEST_F(SecurityStateModelTest, NonsecureProtocol) { 160 TEST(SecurityStateModelTest, NonsecureProtocol) {
168 TestSecurityStateModelClient client; 161 TestSecurityStateModelClient client;
169 SecurityStateModel model; 162 SecurityStateModel model;
170 model.SetClient(&client); 163 model.SetClient(&client);
171 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from 164 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from
172 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param eters-4 165 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param eters-4
173 const uint16 ciphersuite = 0xc02f; 166 const uint16 ciphersuite = 0xc02f;
174 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_1 167 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_1
175 << net::SSL_CONNECTION_VERSION_SHIFT); 168 << net::SSL_CONNECTION_VERSION_SHIFT);
176 client.SetCipherSuite(ciphersuite); 169 client.SetCipherSuite(ciphersuite);
177 const SecurityStateModel::SecurityInfo& security_info = 170 const SecurityStateModel::SecurityInfo& security_info =
178 model.GetSecurityInfo(); 171 model.GetSecurityInfo();
179 EXPECT_FALSE(security_info.is_secure_protocol_and_ciphersuite); 172 EXPECT_FALSE(security_info.is_secure_protocol_and_ciphersuite);
180 } 173 }
181 174
182 TEST_F(SecurityStateModelTest, NonsecureCiphersuite) { 175 TEST(SecurityStateModelTest, NonsecureCiphersuite) {
183 TestSecurityStateModelClient client; 176 TestSecurityStateModelClient client;
184 SecurityStateModel model; 177 SecurityStateModel model;
185 model.SetClient(&client); 178 model.SetClient(&client);
186 // TLS_RSA_WITH_AES_128_CCM_8 from 179 // TLS_RSA_WITH_AES_128_CCM_8 from
187 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param eters-4 180 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-param eters-4
188 const uint16 ciphersuite = 0xc0a0; 181 const uint16 ciphersuite = 0xc0a0;
189 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2 182 client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2
190 << net::SSL_CONNECTION_VERSION_SHIFT); 183 << net::SSL_CONNECTION_VERSION_SHIFT);
191 client.SetCipherSuite(ciphersuite); 184 client.SetCipherSuite(ciphersuite);
192 const SecurityStateModel::SecurityInfo& security_info = 185 const SecurityStateModel::SecurityInfo& security_info =
193 model.GetSecurityInfo(); 186 model.GetSecurityInfo();
194 EXPECT_FALSE(security_info.is_secure_protocol_and_ciphersuite); 187 EXPECT_FALSE(security_info.is_secure_protocol_and_ciphersuite);
195 } 188 }
196 189
197 } // namespace 190 } // namespace
191
192 } // namespace security_state
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698