OLD | NEW |
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 #import "ios/web/web_state/wk_web_view_security_util.h" | 5 #import "ios/web/web_state/wk_web_view_security_util.h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 #include <Security/Security.h> | 8 #include <Security/Security.h> |
9 | 9 |
10 #include "base/mac/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "crypto/rsa_private_key.h" | 12 #include "crypto/rsa_private_key.h" |
13 #include "ios/web/public/test/web_test_util.h" | 13 #include "ios/web/public/test/web_test_util.h" |
14 #include "net/cert/x509_cert_types.h" | 14 #include "net/cert/x509_cert_types.h" |
15 #include "net/cert/x509_certificate.h" | 15 #include "net/cert/x509_certificate.h" |
16 #include "net/cert/x509_util.h" | 16 #include "net/cert/x509_util.h" |
17 #include "net/ssl/ssl_info.h" | 17 #include "net/ssl/ssl_info.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "testing/gtest_mac.h" |
19 #include "testing/platform_test.h" | 20 #include "testing/platform_test.h" |
20 | 21 |
21 namespace web { | 22 namespace web { |
22 namespace { | 23 namespace { |
23 // Subject for testing self-signed certificate. | 24 // Subject for testing self-signed certificate. |
24 const char kTestSubject[] = "self-signed"; | 25 const char kTestSubject[] = "self-signed"; |
| 26 // Hostname for testing SecTrustRef objects. |
| 27 NSString* const kTestHost = @"www.example.com"; |
25 | 28 |
26 // Returns an autoreleased certificate chain for testing. Chain will contain a | 29 // Returns an autoreleased certificate chain for testing. Chain will contain a |
27 // single self-signed cert with |subject| as a subject. | 30 // single self-signed cert with |subject| as a subject. |
28 NSArray* MakeTestCertChain(const std::string& subject) { | 31 NSArray* MakeTestCertChain(const std::string& subject) { |
29 scoped_ptr<crypto::RSAPrivateKey> private_key; | 32 scoped_ptr<crypto::RSAPrivateKey> private_key; |
30 std::string der_cert; | 33 std::string der_cert; |
31 net::x509_util::CreateKeyAndSelfSignedCert( | 34 net::x509_util::CreateKeyAndSelfSignedCert( |
32 "CN=" + subject, 1, base::Time::Now(), | 35 "CN=" + subject, 1, base::Time::Now(), |
33 base::Time::Now() + base::TimeDelta::FromDays(1), &private_key, | 36 base::Time::Now() + base::TimeDelta::FromDays(1), &private_key, |
34 &der_cert); | 37 &der_cert); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 CreateTestTrust(MakeTestCertChain(kTestSubject)); | 96 CreateTestTrust(MakeTestCertChain(kTestSubject)); |
94 scoped_refptr<net::X509Certificate> cert = CreateCertFromTrust(trust); | 97 scoped_refptr<net::X509Certificate> cert = CreateCertFromTrust(trust); |
95 EXPECT_TRUE(cert->subject().GetDisplayName() == kTestSubject); | 98 EXPECT_TRUE(cert->subject().GetDisplayName() == kTestSubject); |
96 } | 99 } |
97 | 100 |
98 // Tests CreateCertFromTrust with nil trust. | 101 // Tests CreateCertFromTrust with nil trust. |
99 TEST_F(WKWebViewSecurityUtilTest, CreationCertFromNilTrust) { | 102 TEST_F(WKWebViewSecurityUtilTest, CreationCertFromNilTrust) { |
100 EXPECT_FALSE(CreateCertFromTrust(nil)); | 103 EXPECT_FALSE(CreateCertFromTrust(nil)); |
101 } | 104 } |
102 | 105 |
| 106 // Tests CreateServerTrustFromChain with valid input. |
| 107 TEST_F(WKWebViewSecurityUtilTest, CreationServerTrust) { |
| 108 // Create server trust. |
| 109 NSArray* chain = MakeTestCertChain(kTestSubject); |
| 110 base::ScopedCFTypeRef<SecTrustRef> server_trust( |
| 111 CreateServerTrustFromChain(chain, kTestHost)); |
| 112 EXPECT_TRUE(server_trust); |
| 113 |
| 114 // Verify chain. |
| 115 EXPECT_EQ(static_cast<CFIndex>(chain.count), |
| 116 SecTrustGetCertificateCount(server_trust)); |
| 117 [chain enumerateObjectsUsingBlock:^(id expected_cert, NSUInteger i, BOOL*) { |
| 118 id actual_cert = static_cast<id>(SecTrustGetCertificateAtIndex( |
| 119 server_trust.get(), static_cast<CFIndex>(i))); |
| 120 EXPECT_EQ(expected_cert, actual_cert); |
| 121 }]; |
| 122 |
| 123 // Verify policies. |
| 124 CFArrayRef policies = nullptr; |
| 125 EXPECT_EQ(errSecSuccess, SecTrustCopyPolicies(server_trust.get(), &policies)); |
| 126 EXPECT_EQ(1, CFArrayGetCount(policies)); |
| 127 SecPolicyRef policy = (SecPolicyRef)CFArrayGetValueAtIndex(policies, 0); |
| 128 base::ScopedCFTypeRef<CFDictionaryRef> properties( |
| 129 SecPolicyCopyProperties(policy)); |
| 130 NSString* name = static_cast<NSString*>( |
| 131 CFDictionaryGetValue(properties.get(), kSecPolicyName)); |
| 132 EXPECT_NSEQ(kTestHost, name); |
| 133 CFRelease(policies); |
| 134 } |
| 135 |
| 136 // Tests CreateServerTrustFromChain with nil chain. |
| 137 TEST_F(WKWebViewSecurityUtilTest, CreationServerTrustFromNilChain) { |
| 138 EXPECT_FALSE(CreateServerTrustFromChain(nil, kTestHost)); |
| 139 } |
| 140 |
| 141 // Tests CreateServerTrustFromChain with empty chain. |
| 142 TEST_F(WKWebViewSecurityUtilTest, CreationServerTrustFromEmptyChain) { |
| 143 EXPECT_FALSE(CreateServerTrustFromChain(@[], kTestHost)); |
| 144 } |
| 145 |
103 // Tests that IsWKWebViewSSLError returns true for NSError with NSURLErrorDomain | 146 // Tests that IsWKWebViewSSLError returns true for NSError with NSURLErrorDomain |
104 // domain and NSURLErrorSecureConnectionFailed error code. | 147 // domain and NSURLErrorSecureConnectionFailed error code. |
105 TEST_F(WKWebViewSecurityUtilTest, CheckSecureConnectionFailedError) { | 148 TEST_F(WKWebViewSecurityUtilTest, CheckSecureConnectionFailedError) { |
106 CR_TEST_REQUIRES_WK_WEB_VIEW(); | 149 CR_TEST_REQUIRES_WK_WEB_VIEW(); |
107 | 150 |
108 EXPECT_TRUE(IsWKWebViewSSLError( | 151 EXPECT_TRUE(IsWKWebViewSSLError( |
109 [NSError errorWithDomain:NSURLErrorDomain | 152 [NSError errorWithDomain:NSURLErrorDomain |
110 code:NSURLErrorSecureConnectionFailed | 153 code:NSURLErrorSecureConnectionFailed |
111 userInfo:nil])); | 154 userInfo:nil])); |
112 } | 155 } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 MakeTestCertChain(kTestSubject), | 243 MakeTestCertChain(kTestSubject), |
201 }]; | 244 }]; |
202 | 245 |
203 net::SSLInfo info; | 246 net::SSLInfo info; |
204 GetSSLInfoFromWKWebViewSSLError(unknownCertError, &info); | 247 GetSSLInfoFromWKWebViewSSLError(unknownCertError, &info); |
205 EXPECT_TRUE(info.is_valid()); | 248 EXPECT_TRUE(info.is_valid()); |
206 EXPECT_EQ(net::CERT_STATUS_INVALID, info.cert_status); | 249 EXPECT_EQ(net::CERT_STATUS_INVALID, info.cert_status); |
207 EXPECT_TRUE(info.cert->subject().GetDisplayName() == kTestSubject); | 250 EXPECT_TRUE(info.cert->subject().GetDisplayName() == kTestSubject); |
208 } | 251 } |
209 | 252 |
| 253 // Tests GetSecurityStyleFromTrustResult with bad SecTrustResultType result. |
| 254 TEST_F(WKWebViewSecurityUtilTest, GetSecurityStyleFromBadResult) { |
| 255 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATION_BROKEN, |
| 256 GetSecurityStyleFromTrustResult(kSecTrustResultDeny)); |
| 257 EXPECT_EQ( |
| 258 SECURITY_STYLE_AUTHENTICATION_BROKEN, |
| 259 GetSecurityStyleFromTrustResult(kSecTrustResultRecoverableTrustFailure)); |
| 260 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATION_BROKEN, |
| 261 GetSecurityStyleFromTrustResult(kSecTrustResultFatalTrustFailure)); |
| 262 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATION_BROKEN, |
| 263 GetSecurityStyleFromTrustResult(kSecTrustResultOtherError)); |
| 264 } |
| 265 |
| 266 // Tests GetSecurityStyleFromTrustResult with good SecTrustResultType result. |
| 267 TEST_F(WKWebViewSecurityUtilTest, GetSecurityStyleFromGoodResult) { |
| 268 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATED, |
| 269 GetSecurityStyleFromTrustResult(kSecTrustResultProceed)); |
| 270 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATED, |
| 271 GetSecurityStyleFromTrustResult(kSecTrustResultUnspecified)); |
| 272 } |
| 273 |
| 274 // Tests GetSecurityStyleFromTrustResult with invalid SecTrustResultType result. |
| 275 TEST_F(WKWebViewSecurityUtilTest, GetSecurityStyleFromInvalidResult) { |
| 276 EXPECT_EQ(SECURITY_STYLE_UNKNOWN, |
| 277 GetSecurityStyleFromTrustResult(kSecTrustResultInvalid)); |
| 278 } |
| 279 |
210 } // namespace web | 280 } // namespace web |
OLD | NEW |