| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/net/crw_ssl_status_updater.h" | 5 #import "ios/web/net/crw_ssl_status_updater.h" |
| 6 | 6 |
| 7 #include "base/mac/foundation_util.h" |
| 7 #include "base/mac/scoped_block.h" | 8 #include "base/mac/scoped_block.h" |
| 8 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 9 #import "ios/web/navigation/crw_session_controller+private_constructors.h" | 10 #import "ios/web/navigation/crw_session_controller+private_constructors.h" |
| 10 #import "ios/web/navigation/crw_session_controller.h" | 11 #import "ios/web/navigation/crw_session_controller.h" |
| 11 #import "ios/web/navigation/navigation_manager_impl.h" | 12 #import "ios/web/navigation/navigation_manager_impl.h" |
| 12 #import "ios/web/public/navigation_item.h" | 13 #import "ios/web/public/navigation_item.h" |
| 13 #include "ios/web/public/ssl_status.h" | 14 #include "ios/web/public/ssl_status.h" |
| 14 #include "ios/web/public/test/web_test.h" | 15 #include "ios/web/public/test/web_test.h" |
| 15 #import "ios/web/web_state/wk_web_view_security_util.h" | 16 #import "ios/web/web_state/wk_web_view_security_util.h" |
| 16 #include "net/cert/x509_util_ios_and_mac.h" | 17 #include "net/cert/x509_util_ios_and_mac.h" |
| 17 #include "net/test/cert_test_util.h" | 18 #include "net/test/cert_test_util.h" |
| 18 #include "net/test/test_data_directory.h" | 19 #include "net/test/test_data_directory.h" |
| 19 #include "third_party/ocmock/OCMock/OCMock.h" | 20 #include "third_party/ocmock/OCMock/OCMock.h" |
| 20 #include "third_party/ocmock/gtest_support.h" | 21 #include "third_party/ocmock/gtest_support.h" |
| 21 | 22 |
| 23 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 24 #error "This file requires ARC support." |
| 25 #endif |
| 26 |
| 22 // Mocks CRWSSLStatusUpdaterTestDataSource. | 27 // Mocks CRWSSLStatusUpdaterTestDataSource. |
| 23 @interface CRWSSLStatusUpdaterTestDataSource | 28 @interface CRWSSLStatusUpdaterTestDataSource |
| 24 : NSObject<CRWSSLStatusUpdaterDataSource> { | 29 : NSObject<CRWSSLStatusUpdaterDataSource> { |
| 25 base::mac::ScopedBlock<StatusQueryHandler> _verificationCompletionHandler; | 30 StatusQueryHandler _verificationCompletionHandler; |
| 26 } | 31 } |
| 27 | 32 |
| 28 // Yes if |SSLStatusUpdater:querySSLStatusForTrust:host:completionHandler| was | 33 // Yes if |SSLStatusUpdater:querySSLStatusForTrust:host:completionHandler| was |
| 29 // called. | 34 // called. |
| 30 @property(nonatomic, readonly) BOOL certVerificationRequested; | 35 @property(nonatomic, readonly) BOOL certVerificationRequested; |
| 31 | 36 |
| 32 // Calls completion handler passed in | 37 // Calls completion handler passed in |
| 33 // |SSLStatusUpdater:querySSLStatusForTrust:host:completionHandler|. | 38 // |SSLStatusUpdater:querySSLStatusForTrust:host:completionHandler|. |
| 34 - (void)finishVerificationWithCertStatus:(net::CertStatus)certStatus | 39 - (void)finishVerificationWithCertStatus:(net::CertStatus)certStatus |
| 35 securityStyle:(web::SecurityStyle)securityStyle; | 40 securityStyle:(web::SecurityStyle)securityStyle; |
| 36 | 41 |
| 37 @end | 42 @end |
| 38 | 43 |
| 39 @implementation CRWSSLStatusUpdaterTestDataSource | 44 @implementation CRWSSLStatusUpdaterTestDataSource |
| 40 | 45 |
| 41 - (BOOL)certVerificationRequested { | 46 - (BOOL)certVerificationRequested { |
| 42 return _verificationCompletionHandler ? YES : NO; | 47 return _verificationCompletionHandler ? YES : NO; |
| 43 } | 48 } |
| 44 | 49 |
| 45 - (void)finishVerificationWithCertStatus:(net::CertStatus)certStatus | 50 - (void)finishVerificationWithCertStatus:(net::CertStatus)certStatus |
| 46 securityStyle:(web::SecurityStyle)securityStyle { | 51 securityStyle:(web::SecurityStyle)securityStyle { |
| 47 _verificationCompletionHandler.get()(securityStyle, certStatus); | 52 _verificationCompletionHandler(securityStyle, certStatus); |
| 48 } | 53 } |
| 49 | 54 |
| 50 #pragma mark CRWSSLStatusUpdaterDataSource | 55 #pragma mark CRWSSLStatusUpdaterDataSource |
| 51 | 56 |
| 52 - (void)SSLStatusUpdater:(CRWSSLStatusUpdater*)SSLStatusUpdater | 57 - (void)SSLStatusUpdater:(CRWSSLStatusUpdater*)SSLStatusUpdater |
| 53 querySSLStatusForTrust:(base::ScopedCFTypeRef<SecTrustRef>)trust | 58 querySSLStatusForTrust:(base::ScopedCFTypeRef<SecTrustRef>)trust |
| 54 host:(NSString*)host | 59 host:(NSString*)host |
| 55 completionHandler:(StatusQueryHandler)completionHandler { | 60 completionHandler:(StatusQueryHandler)completionHandler { |
| 56 _verificationCompletionHandler.reset([completionHandler copy]); | 61 _verificationCompletionHandler = [completionHandler copy]; |
| 57 } | 62 } |
| 58 | 63 |
| 59 @end | 64 @end |
| 60 | 65 |
| 61 namespace web { | 66 namespace web { |
| 62 | 67 |
| 63 namespace { | 68 namespace { |
| 64 // Generated cert filename. | 69 // Generated cert filename. |
| 65 const char kCertFileName[] = "ok_cert.pem"; | 70 const char kCertFileName[] = "ok_cert.pem"; |
| 66 // Test hostname for cert verification. | 71 // Test hostname for cert verification. |
| 67 NSString* const kHostName = @"www.example.com"; | 72 NSString* const kHostName = @"www.example.com"; |
| 68 // Test https url for cert verification. | 73 // Test https url for cert verification. |
| 69 const char kHttpsUrl[] = "https://www.example.com"; | 74 const char kHttpsUrl[] = "https://www.example.com"; |
| 70 // Test http url for cert verification. | 75 // Test http url for cert verification. |
| 71 const char kHttpUrl[] = "http://www.example.com"; | 76 const char kHttpUrl[] = "http://www.example.com"; |
| 72 } // namespace | 77 } // namespace |
| 73 | 78 |
| 74 // Test fixture to test CRWSSLStatusUpdater class. | 79 // Test fixture to test CRWSSLStatusUpdater class. |
| 75 class CRWSSLStatusUpdaterTest : public web::WebTest { | 80 class CRWSSLStatusUpdaterTest : public web::WebTest { |
| 76 protected: | 81 protected: |
| 77 void SetUp() override { | 82 void SetUp() override { |
| 78 web::WebTest::SetUp(); | 83 web::WebTest::SetUp(); |
| 79 | 84 |
| 80 data_source_.reset([[CRWSSLStatusUpdaterTestDataSource alloc] init]); | 85 data_source_ = [[CRWSSLStatusUpdaterTestDataSource alloc] init]; |
| 81 delegate_.reset([[OCMockObject | 86 delegate_ = |
| 82 mockForProtocol:@protocol(CRWSSLStatusUpdaterDelegate)] retain]); | 87 [OCMockObject mockForProtocol:@protocol(CRWSSLStatusUpdaterDelegate)]; |
| 83 | 88 |
| 84 nav_manager_.reset(new NavigationManagerImpl()); | 89 nav_manager_.reset(new NavigationManagerImpl()); |
| 85 nav_manager_->SetBrowserState(GetBrowserState()); | 90 nav_manager_->SetBrowserState(GetBrowserState()); |
| 86 | 91 |
| 87 ssl_status_updater_.reset([[CRWSSLStatusUpdater alloc] | 92 ssl_status_updater_ = |
| 88 initWithDataSource:data_source_ | 93 [[CRWSSLStatusUpdater alloc] initWithDataSource:data_source_ |
| 89 navigationManager:nav_manager_.get()]); | 94 navigationManager:nav_manager_.get()]; |
| 90 [ssl_status_updater_ setDelegate:delegate_]; | 95 [ssl_status_updater_ setDelegate:delegate_]; |
| 91 | 96 |
| 92 // Create test cert chain. | 97 // Create test cert chain. |
| 93 scoped_refptr<net::X509Certificate> cert = | 98 scoped_refptr<net::X509Certificate> cert = |
| 94 net::ImportCertFromFile(net::GetTestCertsDirectory(), kCertFileName); | 99 net::ImportCertFromFile(net::GetTestCertsDirectory(), kCertFileName); |
| 95 ASSERT_TRUE(cert); | 100 ASSERT_TRUE(cert); |
| 96 base::ScopedCFTypeRef<CFMutableArrayRef> chain( | 101 base::ScopedCFTypeRef<CFMutableArrayRef> chain( |
| 97 net::x509_util::CreateSecCertificateArrayForX509Certificate( | 102 net::x509_util::CreateSecCertificateArrayForX509Certificate( |
| 98 cert.get())); | 103 cert.get())); |
| 99 ASSERT_TRUE(chain); | 104 ASSERT_TRUE(chain); |
| 100 trust_ = CreateServerTrustFromChain(static_cast<NSArray*>(chain.get()), | 105 trust_ = CreateServerTrustFromChain(base::mac::CFToNSCast(chain.get()), |
| 101 kHostName); | 106 kHostName); |
| 102 } | 107 } |
| 103 | 108 |
| 104 void TearDown() override { | 109 void TearDown() override { |
| 105 EXPECT_OCMOCK_VERIFY(delegate_); | 110 EXPECT_OCMOCK_VERIFY(delegate_); |
| 106 web::WebTest::TearDown(); | 111 web::WebTest::TearDown(); |
| 107 } | 112 } |
| 108 | 113 |
| 109 // Returns autoreleased session controller with a single committed entry. | 114 // Returns autoreleased session controller with a single committed entry. |
| 110 CRWSessionController* SessionControllerWithEntry(std::string item_url_spec) { | 115 CRWSessionController* SessionControllerWithEntry(std::string item_url_spec) { |
| 111 std::vector<std::unique_ptr<web::NavigationItem>> nav_items; | 116 std::vector<std::unique_ptr<web::NavigationItem>> nav_items; |
| 112 base::scoped_nsobject<CRWSessionController> session_controller( | 117 CRWSessionController* session_controller = |
| 113 [[CRWSessionController alloc] initWithBrowserState:GetBrowserState() | 118 [[CRWSessionController alloc] initWithBrowserState:GetBrowserState() |
| 114 navigationItems:std::move(nav_items) | 119 navigationItems:std::move(nav_items) |
| 115 lastCommittedItemIndex:0]); | 120 lastCommittedItemIndex:0]; |
| 116 [session_controller | 121 [session_controller |
| 117 addPendingItem:GURL(item_url_spec) | 122 addPendingItem:GURL(item_url_spec) |
| 118 referrer:Referrer() | 123 referrer:Referrer() |
| 119 transition:ui::PAGE_TRANSITION_LINK | 124 transition:ui::PAGE_TRANSITION_LINK |
| 120 initiationType:web::NavigationInitiationType::USER_INITIATED | 125 initiationType:web::NavigationInitiationType::USER_INITIATED |
| 121 userAgentOverrideOption:NavigationManager::UserAgentOverrideOption:: | 126 userAgentOverrideOption:NavigationManager::UserAgentOverrideOption:: |
| 122 INHERIT]; | 127 INHERIT]; |
| 123 [session_controller commitPendingItem]; | 128 [session_controller commitPendingItem]; |
| 124 | 129 |
| 125 return session_controller.autorelease(); | 130 return session_controller; |
| 126 } | 131 } |
| 127 | 132 |
| 128 base::scoped_nsobject<CRWSSLStatusUpdaterTestDataSource> data_source_; | 133 CRWSSLStatusUpdaterTestDataSource* data_source_; |
| 129 base::scoped_nsprotocol<id> delegate_; | 134 id delegate_; |
| 130 std::unique_ptr<web::NavigationManagerImpl> nav_manager_; | 135 std::unique_ptr<web::NavigationManagerImpl> nav_manager_; |
| 131 base::scoped_nsobject<CRWSSLStatusUpdater> ssl_status_updater_; | 136 CRWSSLStatusUpdater* ssl_status_updater_; |
| 132 base::ScopedCFTypeRef<SecTrustRef> trust_; | 137 base::ScopedCFTypeRef<SecTrustRef> trust_; |
| 133 }; | 138 }; |
| 134 | 139 |
| 135 // Tests that CRWSSLStatusUpdater init returns non nil object. | 140 // Tests that CRWSSLStatusUpdater init returns non nil object. |
| 136 TEST_F(CRWSSLStatusUpdaterTest, Initialization) { | 141 TEST_F(CRWSSLStatusUpdaterTest, Initialization) { |
| 137 EXPECT_TRUE(ssl_status_updater_); | 142 EXPECT_TRUE(ssl_status_updater_); |
| 138 } | 143 } |
| 139 | 144 |
| 140 // Tests updating http navigation item. | 145 // Tests updating http navigation item. |
| 141 TEST_F(CRWSSLStatusUpdaterTest, HttpItem) { | 146 TEST_F(CRWSSLStatusUpdaterTest, HttpItem) { |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 [data_source_ | 369 [data_source_ |
| 365 finishVerificationWithCertStatus:0 | 370 finishVerificationWithCertStatus:0 |
| 366 securityStyle:web::SECURITY_STYLE_AUTHENTICATED]; | 371 securityStyle:web::SECURITY_STYLE_AUTHENTICATED]; |
| 367 | 372 |
| 368 // Make sure that security style and content status did change. | 373 // Make sure that security style and content status did change. |
| 369 EXPECT_EQ(web::SECURITY_STYLE_UNKNOWN, item->GetSSL().security_style); | 374 EXPECT_EQ(web::SECURITY_STYLE_UNKNOWN, item->GetSSL().security_style); |
| 370 EXPECT_EQ(web::SSLStatus::NORMAL_CONTENT, item->GetSSL().content_status); | 375 EXPECT_EQ(web::SSLStatus::NORMAL_CONTENT, item->GetSSL().content_status); |
| 371 } | 376 } |
| 372 | 377 |
| 373 } // namespace web | 378 } // namespace web |
| OLD | NEW |