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

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

Issue 1322193003: WKWebView(iOS9): correctly update SSL status for current navigation item (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@reland_cert_verification
Patch Set: Predeclare -certificateChain. Created 5 years, 2 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
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"
11 #include "ios/web/test/web_test.h" 11 #include "ios/web/test/web_test.h"
12 #include "net/base/test_data_directory.h" 12 #include "net/base/test_data_directory.h"
13 #include "net/cert/mock_cert_verifier.h" 13 #include "net/cert/mock_cert_verifier.h"
14 #include "net/cert/x509_certificate.h" 14 #include "net/cert/x509_certificate.h"
15 #include "net/test/cert_test_util.h" 15 #include "net/test/cert_test_util.h"
16 #include "net/url_request/url_request_context.h" 16 #include "net/url_request/url_request_context.h"
17 #include "net/url_request/url_request_context_getter.h" 17 #include "net/url_request/url_request_context_getter.h"
18 18
19 namespace web { 19 namespace web {
20 20
21 namespace { 21 namespace {
22 // Generated cert filename. 22 // Test certs filenames.
23 const char kCertFileName[] = "ok_cert.pem"; 23 const char kCertFileName[] = "ok_cert.pem";
24 // Test hostname for cert verification. 24 const char kValidCertFileName[] = "twitter-chain.pem";
Ryan Sleevi 2015/09/24 22:25:32 Don't use this chain. You should avoid all certif
Eugene But (OOO till 7-30) 2015/09/25 00:28:10 Then I don't have a way to test querySSLStatusForC
Ryan Sleevi 2015/09/25 01:05:06 If you can modify the SecTrustRef prior to evaluat
Eugene But (OOO till 7-30) 2015/09/25 17:03:10 I changed API to take SecTrustRef, so I can make i
25 // Test hostnames for cert verification.
25 NSString* const kHostName = @"www.example.com"; 26 NSString* const kHostName = @"www.example.com";
27 NSString* const kValidCertHostName = @"twitter.com";
26 } // namespace 28 } // namespace
27 29
28 // Test fixture to test CRWCertVerificationController class. 30 // Test fixture to test CRWCertVerificationController class.
29 class CRWCertVerificationControllerTest : public web::WebTest { 31 class CRWCertVerificationControllerTest : public web::WebTest {
30 protected: 32 protected:
31 void SetUp() override { 33 void SetUp() override {
32 web::WebTest::SetUp(); 34 web::WebTest::SetUp();
33 35
34 web::BrowserState* browser_state = GetBrowserState(); 36 web::BrowserState* browser_state = GetBrowserState();
35 net::URLRequestContextGetter* getter = browser_state->GetRequestContext(); 37 net::URLRequestContextGetter* getter = browser_state->GetRequestContext();
36 web::WebThread::PostTask(web::WebThread::IO, FROM_HERE, base::BindBlock(^{ 38 web::WebThread::PostTask(web::WebThread::IO, FROM_HERE, base::BindBlock(^{
37 getter->GetURLRequestContext()->set_cert_verifier(&cert_verifier_); 39 getter->GetURLRequestContext()->set_cert_verifier(&cert_verifier_);
38 })); 40 }));
39 41
40 controller_.reset([[CRWCertVerificationController alloc] 42 controller_.reset([[CRWCertVerificationController alloc]
41 initWithBrowserState:browser_state]); 43 initWithBrowserState:browser_state]);
42 cert_ = 44 valid_cert_ = net::ImportCertFromFile(net::GetTestCertsDirectory(),
45 kValidCertFileName);
46 ASSERT_TRUE(valid_cert_);
47 invalid_cert_ =
43 net::ImportCertFromFile(net::GetTestCertsDirectory(), kCertFileName); 48 net::ImportCertFromFile(net::GetTestCertsDirectory(), kCertFileName);
49 ASSERT_TRUE(invalid_cert_);
44 } 50 }
45 51
46 void TearDown() override { 52 void TearDown() override {
47 [controller_ shutDown]; 53 [controller_ shutDown];
48 web::WebTest::TearDown(); 54 web::WebTest::TearDown();
49 } 55 }
50 56
57 // Returns NSArray of SecCertificateRef objects for the given |cert|.
58 NSArray* GetChain(const scoped_refptr<net::X509Certificate>& cert) const {
59 NSMutableArray* result = [NSMutableArray
60 arrayWithObject:static_cast<id>(cert->os_cert_handle())];
61 for (SecCertificateRef intermediate : cert->GetIntermediateCertificates()) {
62 [result addObject:static_cast<id>(intermediate)];
63 }
64 return result;
65 }
66
51 // Synchronously returns result of decidePolicyForCert:host:completionHandler: 67 // Synchronously returns result of decidePolicyForCert:host:completionHandler:
52 // call. 68 // call.
53 void DecidePolicy(const scoped_refptr<net::X509Certificate>& cert, 69 void DecidePolicy(const scoped_refptr<net::X509Certificate>& cert,
54 NSString* host, 70 NSString* host,
55 web::CertAcceptPolicy* policy, 71 web::CertAcceptPolicy* policy,
56 net::CertStatus* status) { 72 net::CertStatus* status) {
57 __block bool completion_handler_called = false; 73 __block bool completion_handler_called = false;
58 [controller_ decidePolicyForCert:cert 74 [controller_ decidePolicyForCert:cert
59 host:host 75 host:host
60 completionHandler:^(web::CertAcceptPolicy callback_policy, 76 completionHandler:^(web::CertAcceptPolicy callback_policy,
61 net::CertStatus callback_status) { 77 net::CertStatus callback_status) {
62 *policy = callback_policy; 78 *policy = callback_policy;
63 *status = callback_status; 79 *status = callback_status;
64 completion_handler_called = true; 80 completion_handler_called = true;
65 }]; 81 }];
66 base::test::ios::WaitUntilCondition(^{ 82 base::test::ios::WaitUntilCondition(^{
67 return completion_handler_called; 83 return completion_handler_called;
68 }, base::MessageLoop::current(), base::TimeDelta()); 84 }, base::MessageLoop::current(), base::TimeDelta());
69 } 85 }
70 86
71 scoped_refptr<net::X509Certificate> cert_; 87 // Synchronously returns result of
88 // querySSLStatusForCertChain:host:completionHandler: call.
89 void QueryStatus(NSArray* chain,
90 NSString* host,
91 SecurityStyle* style,
92 net::CertStatus* status) {
93 __block bool completion_handler_called = false;
94 [controller_ querySSLStatusForCertChain:chain
95 host:host
96 completionHandler:^(SecurityStyle callback_style,
97 net::CertStatus callback_status) {
98 *style = callback_style;
99 *status = callback_status;
100 completion_handler_called = true;
101 }];
102 base::test::ios::WaitUntilCondition(^{
103 return completion_handler_called;
104 }, base::MessageLoop::current(), base::TimeDelta());
105 }
106
107 scoped_refptr<net::X509Certificate> valid_cert_;
108 scoped_refptr<net::X509Certificate> invalid_cert_;
72 net::MockCertVerifier cert_verifier_; 109 net::MockCertVerifier cert_verifier_;
73 base::scoped_nsobject<CRWCertVerificationController> controller_; 110 base::scoped_nsobject<CRWCertVerificationController> controller_;
74 }; 111 };
75 112
76 // Tests cert policy with a valid cert. 113 // Tests cert policy with a valid cert.
77 TEST_F(CRWCertVerificationControllerTest, ValidCert) { 114 TEST_F(CRWCertVerificationControllerTest, PolicyForValidCert) {
78 net::CertVerifyResult verify_result; 115 net::CertVerifyResult verify_result;
79 verify_result.cert_status = net::CERT_STATUS_NO_REVOCATION_MECHANISM; 116 verify_result.cert_status = net::CERT_STATUS_NO_REVOCATION_MECHANISM;
80 verify_result.verified_cert = cert_; 117 verify_result.verified_cert = invalid_cert_;
81 cert_verifier_.AddResultForCertAndHost(cert_.get(), [kHostName UTF8String], 118 cert_verifier_.AddResultForCertAndHost(
82 verify_result, net::OK); 119 invalid_cert_.get(), kHostName.UTF8String, verify_result, net::OK);
83 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR; 120 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR;
84 net::CertStatus status; 121 net::CertStatus status;
85 DecidePolicy(cert_, kHostName, &policy, &status); 122 DecidePolicy(invalid_cert_, kHostName, &policy, &status);
86 EXPECT_EQ(CERT_ACCEPT_POLICY_ALLOW, policy); 123 EXPECT_EQ(CERT_ACCEPT_POLICY_ALLOW, policy);
87 EXPECT_EQ(verify_result.cert_status, status); 124 EXPECT_EQ(verify_result.cert_status, status);
88 } 125 }
89 126
90 // Tests cert policy with an invalid cert. 127 // Tests cert policy with an invalid cert.
91 TEST_F(CRWCertVerificationControllerTest, InvalidCert) { 128 TEST_F(CRWCertVerificationControllerTest, PolicyForInvalidCert) {
92 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR; 129 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR;
93 net::CertStatus status; 130 net::CertStatus status;
94 DecidePolicy(cert_, kHostName, &policy, &status); 131 DecidePolicy(invalid_cert_, kHostName, &policy, &status);
95 EXPECT_EQ(CERT_ACCEPT_POLICY_RECOVERABLE_ERROR, policy); 132 EXPECT_EQ(CERT_ACCEPT_POLICY_RECOVERABLE_ERROR, policy);
96 } 133 }
97 134
98 // Tests cert policy with null cert. 135 // Tests cert policy with null cert.
99 TEST_F(CRWCertVerificationControllerTest, NullCert) { 136 TEST_F(CRWCertVerificationControllerTest, PolicyForNullCert) {
100 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR; 137 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR;
101 net::CertStatus status; 138 net::CertStatus status;
102 DecidePolicy(nullptr, kHostName, &policy, &status); 139 DecidePolicy(nullptr, kHostName, &policy, &status);
103 EXPECT_EQ(CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR, policy); 140 EXPECT_EQ(CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR, policy);
104 } 141 }
105 142
106 // Tests cert policy with null cert and null host. 143 // Tests cert policy with null cert and null host.
107 TEST_F(CRWCertVerificationControllerTest, NullHost) { 144 TEST_F(CRWCertVerificationControllerTest, PolicyForNullHost) {
108 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR; 145 web::CertAcceptPolicy policy = CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR;
109 net::CertStatus status; 146 net::CertStatus status;
110 DecidePolicy(cert_, nil, &policy, &status); 147 DecidePolicy(invalid_cert_, nil, &policy, &status);
111 EXPECT_EQ(CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR, policy); 148 EXPECT_EQ(CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR, policy);
112 } 149 }
113 150
151 // Tests SSL status with valid chain.
152 TEST_F(CRWCertVerificationControllerTest, SSLStatusForValidChain) {
153 SecurityStyle style = SECURITY_STYLE_UNKNOWN;
154 net::CertStatus status = net::CERT_STATUS_ALL_ERRORS;
155
156 QueryStatus(GetChain(valid_cert_), kValidCertHostName, &style, &status);
157 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATED, style);
158 EXPECT_FALSE(status);
159 }
160
161 // Tests SSL status with valid chain and SHA-1 signature.
162 TEST_F(CRWCertVerificationControllerTest, SSLStatusForValidChainWithSHA1) {
163 net::CertVerifyResult result;
164 result.cert_status =
165 net::CERT_STATUS_ALL_ERRORS | net::CERT_STATUS_SHA1_SIGNATURE_PRESENT;
166 result.verified_cert = valid_cert_;
167 cert_verifier_.AddResultForCertAndHost(valid_cert_.get(),
168 kValidCertHostName.UTF8String, result,
169 net::ERR_CERT_INVALID);
170
171 SecurityStyle style = SECURITY_STYLE_UNKNOWN;
172 net::CertStatus status = net::CERT_STATUS_ALL_ERRORS;
173
174 QueryStatus(GetChain(valid_cert_), kValidCertHostName, &style, &status);
175 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATED, style);
176 EXPECT_EQ(status, net::CERT_STATUS_SHA1_SIGNATURE_PRESENT);
177 }
178
179 // Tests SSL status with invalid host.
180 TEST_F(CRWCertVerificationControllerTest, SSLStatusForInvalidHost) {
181 net::CertVerifyResult result;
182 result.cert_status = net::CERT_STATUS_COMMON_NAME_INVALID;
183 result.verified_cert = invalid_cert_;
184 cert_verifier_.AddResultForCertAndHost(invalid_cert_.get(),
185 kHostName.UTF8String, result,
186 net::ERR_CERT_COMMON_NAME_INVALID);
187
188 SecurityStyle style = SECURITY_STYLE_UNKNOWN;
189 net::CertStatus status = net::CERT_STATUS_ALL_ERRORS;
190
191 QueryStatus(GetChain(invalid_cert_), kHostName, &style, &status);
192 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATION_BROKEN, style);
193 EXPECT_EQ(status, net::CERT_STATUS_COMMON_NAME_INVALID);
194 }
195
196 // Tests SSL status with expired cert chain.
197 TEST_F(CRWCertVerificationControllerTest, SSLStatusForExpiredCertChain) {
198 net::CertVerifyResult result;
199 result.cert_status = net::CERT_STATUS_DATE_INVALID;
200 result.verified_cert = invalid_cert_;
201 cert_verifier_.AddResultForCertAndHost(invalid_cert_.get(),
202 kHostName.UTF8String, result,
203 net::ERR_CERT_DATE_INVALID);
204
205 SecurityStyle style = SECURITY_STYLE_UNKNOWN;
206 net::CertStatus status = net::CERT_STATUS_ALL_ERRORS;
207
208 QueryStatus(GetChain(invalid_cert_), kHostName, &style, &status);
209 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATION_BROKEN, style);
210 EXPECT_EQ(net::CERT_STATUS_DATE_INVALID, status);
211 }
212
114 } // namespace web 213 } // namespace web
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698