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

Side by Side Diff: ios/web/net/crw_cert_verification_controller_unittest.mm

Issue 1357773002: WKWebView: Implemented recoverable SSL interstitials. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lock_coloring
Patch Set: Addressed unit tests review comments Created 5 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
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 "ios/web/net/crw_cert_verification_controller.h" 5 #include "ios/web/net/crw_cert_verification_controller.h"
6 6
7 #include "base/mac/bind_objc_block.h" 7 #include "base/mac/bind_objc_block.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/test/ios/wait_util.h" 9 #include "base/test/ios/wait_util.h"
10 #include "ios/web/public/web_thread.h" 10 #include "ios/web/public/web_thread.h"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 // Returns NSArray of SecCertificateRef objects for the given |cert|. 58 // Returns NSArray of SecCertificateRef objects for the given |cert|.
59 NSArray* GetChain(const scoped_refptr<net::X509Certificate>& cert) const { 59 NSArray* GetChain(const scoped_refptr<net::X509Certificate>& cert) const {
60 NSMutableArray* result = [NSMutableArray 60 NSMutableArray* result = [NSMutableArray
61 arrayWithObject:static_cast<id>(cert->os_cert_handle())]; 61 arrayWithObject:static_cast<id>(cert->os_cert_handle())];
62 for (SecCertificateRef intermediate : cert->GetIntermediateCertificates()) { 62 for (SecCertificateRef intermediate : cert->GetIntermediateCertificates()) {
63 [result addObject:static_cast<id>(intermediate)]; 63 [result addObject:static_cast<id>(intermediate)];
64 } 64 }
65 return result; 65 return result;
66 } 66 }
67 67
68 // Synchronously returns result of decidePolicyForCert:host:completionHandler: 68 // Synchronously returns result of
69 // call. 69 // decideLoadPolicyForTrust:host:completionHandler: call.
70 void DecidePolicy(const scoped_refptr<net::X509Certificate>& cert, 70 void DecidePolicy(const base::ScopedCFTypeRef<SecTrustRef>& trust,
71 NSString* host, 71 NSString* host,
72 web::CertAcceptPolicy* policy, 72 web::CertAcceptPolicy* policy,
73 net::CertStatus* status) { 73 net::CertStatus* status) {
74 __block bool completion_handler_called = false; 74 __block bool completion_handler_called = false;
75 [controller_ decidePolicyForCert:cert 75 [controller_
76 host:host 76 decideLoadPolicyForTrust:trust
77 completionHandler:^(web::CertAcceptPolicy callback_policy, 77 host:host
78 net::CertStatus callback_status) { 78 completionHandler:^(web::CertAcceptPolicy callback_policy,
79 *policy = callback_policy; 79 net::CertStatus callback_status) {
80 *status = callback_status; 80 *policy = callback_policy;
81 completion_handler_called = true; 81 *status = callback_status;
82 }]; 82 completion_handler_called = true;
83 }];
83 base::test::ios::WaitUntilCondition(^{ 84 base::test::ios::WaitUntilCondition(^{
84 return completion_handler_called; 85 return completion_handler_called;
85 }, base::MessageLoop::current(), base::TimeDelta()); 86 }, base::MessageLoop::current(), base::TimeDelta());
86 } 87 }
87 88
88 // Synchronously returns result of 89 // Synchronously returns result of
89 // querySSLStatusForTrust:host:completionHandler: call. 90 // querySSLStatusForTrust:host:completionHandler: call.
90 void QueryStatus(const base::ScopedCFTypeRef<SecTrustRef>& trust, 91 void QueryStatus(const base::ScopedCFTypeRef<SecTrustRef>& trust,
91 NSString* host, 92 NSString* host,
92 SecurityStyle* style, 93 SecurityStyle* style,
(...skipping 12 matching lines...) Expand all
105 }, base::MessageLoop::current(), base::TimeDelta()); 106 }, base::MessageLoop::current(), base::TimeDelta());
106 } 107 }
107 108
108 scoped_refptr<net::X509Certificate> cert_; 109 scoped_refptr<net::X509Certificate> cert_;
109 base::ScopedCFTypeRef<SecTrustRef> valid_trust_; 110 base::ScopedCFTypeRef<SecTrustRef> valid_trust_;
110 base::ScopedCFTypeRef<SecTrustRef> invalid_trust_; 111 base::ScopedCFTypeRef<SecTrustRef> invalid_trust_;
111 net::MockCertVerifier cert_verifier_; 112 net::MockCertVerifier cert_verifier_;
112 base::scoped_nsobject<CRWCertVerificationController> controller_; 113 base::scoped_nsobject<CRWCertVerificationController> controller_;
113 }; 114 };
114 115
115 // Tests cert policy with a valid cert. 116 // Tests cert policy with a valid trust.
116 TEST_F(CRWCertVerificationControllerTest, PolicyForValidCert) { 117 TEST_F(CRWCertVerificationControllerTest, PolicyForValidTrust) {
117 net::CertVerifyResult verify_result; 118 net::CertVerifyResult verify_result;
118 verify_result.cert_status = net::CERT_STATUS_NO_REVOCATION_MECHANISM; 119 verify_result.cert_status = net::CERT_STATUS_NO_REVOCATION_MECHANISM;
119 verify_result.verified_cert = cert_; 120 verify_result.verified_cert = cert_;
120 cert_verifier_.AddResultForCertAndHost(cert_.get(), kHostName.UTF8String, 121 cert_verifier_.AddResultForCertAndHost(cert_.get(), kHostName.UTF8String,
121 verify_result, net::OK); 122 verify_result, net::OK);
122 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR; 123 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR;
123 net::CertStatus status; 124 net::CertStatus status;
124 DecidePolicy(cert_, kHostName, &policy, &status); 125 DecidePolicy(valid_trust_, kHostName, &policy, &status);
125 EXPECT_EQ(CERT_ACCEPT_POLICY_ALLOW, policy); 126 EXPECT_EQ(CERT_ACCEPT_POLICY_ALLOW, policy);
126 EXPECT_EQ(verify_result.cert_status, status); 127 EXPECT_FALSE(status);
127 } 128 }
128 129
129 // Tests cert policy with an invalid cert. 130 // Tests cert policy with an invalid trust not accepted by user.
130 TEST_F(CRWCertVerificationControllerTest, PolicyForInvalidCert) { 131 TEST_F(CRWCertVerificationControllerTest, PolicyForInvalidTrust) {
132 net::CertVerifyResult result;
133 result.cert_status = net::CERT_STATUS_COMMON_NAME_INVALID;
134 result.verified_cert = cert_;
135 cert_verifier_.AddResultForCertAndHost(cert_.get(), kHostName.UTF8String,
136 result,
137 net::ERR_CERT_COMMON_NAME_INVALID);
138
131 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR; 139 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR;
132 net::CertStatus status; 140 net::CertStatus status;
133 DecidePolicy(cert_, kHostName, &policy, &status); 141 DecidePolicy(invalid_trust_, kHostName, &policy, &status);
134 EXPECT_EQ(CERT_ACCEPT_POLICY_RECOVERABLE_ERROR, policy); 142 EXPECT_EQ(CERT_ACCEPT_POLICY_RECOVERABLE_ERROR_UNDECIDED_BY_USER, policy);
143 EXPECT_EQ(net::CERT_STATUS_COMMON_NAME_INVALID, status);
135 } 144 }
136 145
137 // Tests cert policy with null cert. 146 // Tests cert policy with an invalid trust accepted by user.
138 TEST_F(CRWCertVerificationControllerTest, PolicyForNullCert) { 147 TEST_F(CRWCertVerificationControllerTest, PolicyForInvalidTrustAcceptedByUser) {
148 net::CertVerifyResult result;
149 result.cert_status = net::CERT_STATUS_DATE_INVALID;
150 result.verified_cert = cert_;
151 cert_verifier_.AddResultForCertAndHost(cert_.get(), kHostName.UTF8String,
152 result, net::ERR_CERT_DATE_INVALID);
153
154 [controller_ allowCert:cert_.get()
155 forHost:kHostName
156 status:net::CERT_STATUS_ALL_ERRORS];
139 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR; 157 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR;
140 net::CertStatus status; 158 net::CertStatus status;
141 DecidePolicy(nullptr, kHostName, &policy, &status); 159 DecidePolicy(invalid_trust_, kHostName, &policy, &status);
142 EXPECT_EQ(CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR, policy); 160 EXPECT_EQ(CERT_ACCEPT_POLICY_RECOVERABLE_ERROR_ACCEPTED_BY_USER, policy);
161 EXPECT_EQ(net::CERT_STATUS_DATE_INVALID, status);
143 } 162 }
144 163
145 // Tests cert policy with null cert and null host. 164 // Tests that allowCert:forHost:status: strips all intermidiate certs.
165 TEST_F(CRWCertVerificationControllerTest, AllowCertIgnoresIntermidiateCerts) {
166 scoped_refptr<net::X509Certificate> cert(
167 net::X509Certificate::CreateFromHandle(cert_->os_cert_handle(),
168 {cert_->os_cert_handle()}));
169 net::CertVerifyResult result;
170 result.cert_status = net::CERT_STATUS_DATE_INVALID;
171 result.verified_cert = cert_;
172 cert_verifier_.AddResultForCertAndHost(cert_.get(), kHostName.UTF8String,
173 result, net::ERR_CERT_DATE_INVALID);
174
175 [controller_ allowCert:cert.get()
176 forHost:kHostName
177 status:net::CERT_STATUS_ALL_ERRORS];
178 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR;
179 net::CertStatus status;
180 DecidePolicy(invalid_trust_, kHostName, &policy, &status);
181 EXPECT_EQ(CERT_ACCEPT_POLICY_RECOVERABLE_ERROR_ACCEPTED_BY_USER, policy);
182 EXPECT_EQ(net::CERT_STATUS_DATE_INVALID, status);
183 }
184
185 // Tests cert policy with null trust.
186 TEST_F(CRWCertVerificationControllerTest, PolicyForNullTrust) {
187 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_ALLOW;
188 net::CertStatus status;
189 base::ScopedCFTypeRef<SecTrustRef> null_trust;
190 DecidePolicy(null_trust, kHostName, &policy, &status);
191 EXPECT_EQ(CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR, policy);
192 EXPECT_FALSE(status);
193 }
194
195 // Tests cert policy with invalid trust and null host.
146 TEST_F(CRWCertVerificationControllerTest, PolicyForNullHost) { 196 TEST_F(CRWCertVerificationControllerTest, PolicyForNullHost) {
147 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR; 197 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR;
148 net::CertStatus status; 198 net::CertStatus status;
149 DecidePolicy(cert_, nil, &policy, &status); 199 DecidePolicy(invalid_trust_, nil, &policy, &status);
150 EXPECT_EQ(CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR, policy); 200 EXPECT_EQ(CERT_ACCEPT_POLICY_RECOVERABLE_ERROR_UNDECIDED_BY_USER, policy);
201 EXPECT_FALSE(status);
151 } 202 }
152 203
153 // Tests SSL status with valid trust. 204 // Tests SSL status with valid trust.
154 TEST_F(CRWCertVerificationControllerTest, SSLStatusForValidTrust) { 205 TEST_F(CRWCertVerificationControllerTest, SSLStatusForValidTrust) {
155 SecurityStyle style = SECURITY_STYLE_UNKNOWN; 206 SecurityStyle style = SECURITY_STYLE_UNKNOWN;
156 net::CertStatus status = net::CERT_STATUS_ALL_ERRORS; 207 net::CertStatus status = net::CERT_STATUS_ALL_ERRORS;
157 208
158 QueryStatus(valid_trust_, kHostName, &style, &status); 209 QueryStatus(valid_trust_, kHostName, &style, &status);
159 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATED, style); 210 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATED, style);
160 EXPECT_FALSE(status); 211 EXPECT_FALSE(status);
(...skipping 26 matching lines...) Expand all
187 238
188 SecurityStyle style = SECURITY_STYLE_UNKNOWN; 239 SecurityStyle style = SECURITY_STYLE_UNKNOWN;
189 net::CertStatus status = net::CERT_STATUS_ALL_ERRORS; 240 net::CertStatus status = net::CERT_STATUS_ALL_ERRORS;
190 241
191 QueryStatus(invalid_trust_, kHostName, &style, &status); 242 QueryStatus(invalid_trust_, kHostName, &style, &status);
192 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATION_BROKEN, style); 243 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATION_BROKEN, style);
193 EXPECT_EQ(net::CERT_STATUS_DATE_INVALID, status); 244 EXPECT_EQ(net::CERT_STATUS_DATE_INVALID, status);
194 } 245 }
195 246
196 } // namespace web 247 } // namespace web
OLDNEW
« no previous file with comments | « ios/web/net/crw_cert_verification_controller.mm ('k') | ios/web/web_state/ui/crw_web_controller_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698