Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ios/chrome/browser/ssl/ios_ssl_error_handler.h" | |
| 6 | |
| 7 #include "base/mac/bind_objc_block.h" | |
| 8 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" | |
| 9 #include "ios/web/public/interstitials/web_interstitial.h" | |
| 10 #import "ios/web/public/test/web_test_with_web_state.h" | |
| 11 #import "ios/web/public/web_state/web_state.h" | |
| 12 #include "net/ssl/ssl_info.h" | |
| 13 #include "net/test/cert_test_util.h" | |
| 14 #include "net/test/test_data_directory.h" | |
| 15 | |
| 16 namespace { | |
| 17 const char kTestCertFileName[] = "ok_cert.pem"; | |
| 18 const char kTestHostName[] = "https://chromium.test/"; | |
| 19 } // namespace | |
| 20 | |
| 21 // Test fixture for IOSSSLErrorHandler class. | |
| 22 class IOSSSLErrorHandlerTest : public web::WebTestWithWebState { | |
| 23 protected: | |
| 24 IOSSSLErrorHandlerTest() | |
| 25 : browser_state_(builder_.Build()), | |
| 26 cert_(net::ImportCertFromFile(net::GetTestCertsDirectory(), | |
| 27 kTestCertFileName)) {} | |
| 28 | |
| 29 // web::WebTestWithWebState overrides: | |
|
sdefresne
2016/06/30 11:58:16
nit: prefer to put code in constructor instead of
Eugene But (OOO till 7-30)
2016/06/30 18:35:58
ASSERT_TRUE returns the value and can not be used
| |
| 30 void SetUp() override { | |
| 31 web::WebTestWithWebState::SetUp(); | |
| 32 ASSERT_TRUE(cert_); | |
| 33 ASSERT_FALSE(web_state()->IsShowingWebInterstitial()); | |
| 34 } | |
| 35 web::BrowserState* GetBrowserState() override { return browser_state_.get(); } | |
| 36 | |
| 37 // Returns certificate for testing. | |
| 38 scoped_refptr<net::X509Certificate> cert() { return cert_; } | |
| 39 | |
| 40 private: | |
| 41 TestChromeBrowserState::Builder builder_; | |
| 42 std::unique_ptr<TestChromeBrowserState> browser_state_; | |
| 43 scoped_refptr<net::X509Certificate> cert_; | |
| 44 }; | |
| 45 | |
| 46 // Tests non-overridable error handling. | |
| 47 TEST_F(IOSSSLErrorHandlerTest, NonOverridable) { | |
| 48 net::SSLInfo ssl_info; | |
| 49 ssl_info.cert = cert(); | |
| 50 GURL url(kTestHostName); | |
| 51 __block bool do_not_proceed_callback_called = false; | |
| 52 IOSSSLErrorHandler::HandleSSLError( | |
| 53 web_state(), net::ERR_CERT_AUTHORITY_INVALID, ssl_info, url, false, | |
| 54 base::BindBlock(^(bool proceed) { | |
| 55 EXPECT_FALSE(proceed); | |
| 56 do_not_proceed_callback_called = true; | |
| 57 })); | |
| 58 | |
| 59 // Make sure that interstitial is displayed. | |
| 60 EXPECT_TRUE(web_state()->IsShowingWebInterstitial()); | |
| 61 web::WebInterstitial* interstitial = web_state()->GetWebInterstitial(); | |
| 62 EXPECT_TRUE(interstitial); | |
| 63 | |
| 64 // Make sure callback is called on dismissal. | |
| 65 interstitial->DontProceed(); | |
| 66 EXPECT_TRUE(do_not_proceed_callback_called); | |
| 67 } | |
| 68 | |
| 69 // Tests proceed with overridable error. | |
| 70 TEST_F(IOSSSLErrorHandlerTest, OverridableProceed) { | |
| 71 net::SSLInfo ssl_info; | |
| 72 ssl_info.cert = cert(); | |
| 73 GURL url(kTestHostName); | |
| 74 __block bool proceed_callback_called = false; | |
| 75 IOSSSLErrorHandler::HandleSSLError( | |
| 76 web_state(), net::ERR_CERT_AUTHORITY_INVALID, ssl_info, url, true, | |
| 77 base::BindBlock(^(bool proceed) { | |
| 78 EXPECT_TRUE(proceed); | |
| 79 proceed_callback_called = true; | |
| 80 })); | |
| 81 | |
| 82 // Make sure that interstitial is displayed. | |
| 83 EXPECT_TRUE(web_state()->IsShowingWebInterstitial()); | |
| 84 web::WebInterstitial* interstitial = web_state()->GetWebInterstitial(); | |
| 85 EXPECT_TRUE(interstitial); | |
| 86 | |
| 87 // Make sure callback is called on dismissal. | |
| 88 interstitial->Proceed(); | |
| 89 EXPECT_TRUE(proceed_callback_called); | |
| 90 } | |
| 91 | |
| 92 // Tests do not proceed with overridable error. | |
| 93 TEST_F(IOSSSLErrorHandlerTest, OverridableDontProceed) { | |
| 94 net::SSLInfo ssl_info; | |
| 95 ssl_info.cert = cert(); | |
| 96 GURL url(kTestHostName); | |
| 97 __block bool do_not_proceed_callback_called = false; | |
| 98 IOSSSLErrorHandler::HandleSSLError( | |
| 99 web_state(), net::ERR_CERT_AUTHORITY_INVALID, ssl_info, url, true, | |
| 100 base::BindBlock(^(bool proceed) { | |
| 101 EXPECT_FALSE(proceed); | |
| 102 do_not_proceed_callback_called = true; | |
| 103 })); | |
| 104 | |
| 105 // Make sure that interstitial is displayed. | |
| 106 EXPECT_TRUE(web_state()->IsShowingWebInterstitial()); | |
| 107 web::WebInterstitial* interstitial = web_state()->GetWebInterstitial(); | |
| 108 EXPECT_TRUE(interstitial); | |
| 109 | |
| 110 // Make sure callback is called on dismissal. | |
| 111 interstitial->DontProceed(); | |
| 112 EXPECT_TRUE(do_not_proceed_callback_called); | |
| 113 } | |
| OLD | NEW |