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 // Test cert filename. | |
estark
2016/06/29 23:00:15
nit: I would just name these |kTestCertFileName| a
Eugene But (OOO till 7-30)
2016/06/30 00:05:26
Done.
| |
18 const char kCertFileName[] = "ok_cert.pem"; | |
19 // Test hostname. | |
20 const char kHostName[] = "https://chromium.test/"; | |
21 } // namespace | |
22 | |
23 // Test fixture for IOSSSLErrorHandler class. | |
24 class IOSSSLErrorHandlerTest : public web::WebTestWithWebState { | |
25 protected: | |
26 IOSSSLErrorHandlerTest() | |
27 : browser_state_(builder_.Build()), | |
28 cert_(net::ImportCertFromFile(net::GetTestCertsDirectory(), | |
29 kCertFileName)) {} | |
30 | |
31 // web::WebTestWithWebState overrides: | |
32 void SetUp() override { | |
33 web::WebTestWithWebState::SetUp(); | |
34 ASSERT_TRUE(cert_); | |
35 ASSERT_FALSE(web_state()->IsShowingWebInterstitial()); | |
36 } | |
37 web::BrowserState* GetBrowserState() override { return browser_state_.get(); } | |
38 | |
39 // Returns certificate for testing. | |
40 scoped_refptr<net::X509Certificate> cert() { return cert_; } | |
41 | |
42 private: | |
43 TestChromeBrowserState::Builder builder_; | |
44 std::unique_ptr<TestChromeBrowserState> browser_state_; | |
45 scoped_refptr<net::X509Certificate> cert_; | |
46 }; | |
47 | |
48 // Tests non-overridable error handling. | |
49 TEST_F(IOSSSLErrorHandlerTest, NonOverridable) { | |
50 net::SSLInfo ssl_info; | |
51 ssl_info.cert = cert(); | |
52 GURL url(kHostName); | |
53 __block bool do_not_proceed_callback_called = false; | |
54 IOSSSLErrorHandler::HandleSSLError( | |
55 web_state(), net::ERR_CERT_AUTHORITY_INVALID, ssl_info, url, false, | |
56 base::BindBlock(^(bool proceed) { | |
57 EXPECT_FALSE(proceed); | |
58 do_not_proceed_callback_called = true; | |
59 })); | |
60 | |
61 // Make sure that interstitial is displayed. | |
62 EXPECT_TRUE(web_state()->IsShowingWebInterstitial()); | |
63 web::WebInterstitial* interstitial = web_state()->GetWebInterstitial(); | |
64 EXPECT_TRUE(interstitial); | |
65 | |
66 // Make sure callback is called on dismissal. | |
67 interstitial->DontProceed(); | |
68 EXPECT_TRUE(do_not_proceed_callback_called); | |
69 } | |
70 | |
71 // Tests overridable error handling. | |
72 TEST_F(IOSSSLErrorHandlerTest, Overridable) { | |
73 net::SSLInfo ssl_info; | |
74 ssl_info.cert = cert(); | |
75 GURL url(kHostName); | |
76 __block bool proceed_callback_called = false; | |
77 IOSSSLErrorHandler::HandleSSLError( | |
78 web_state(), net::ERR_CERT_AUTHORITY_INVALID, ssl_info, url, true, | |
79 base::BindBlock(^(bool proceed) { | |
80 EXPECT_TRUE(proceed); | |
81 proceed_callback_called = true; | |
82 })); | |
83 | |
84 // Make sure that interstitial is displayed. | |
85 EXPECT_TRUE(web_state()->IsShowingWebInterstitial()); | |
86 web::WebInterstitial* interstitial = web_state()->GetWebInterstitial(); | |
87 EXPECT_TRUE(interstitial); | |
88 | |
89 // Make sure callback is called on dismissal. | |
90 interstitial->Proceed(); | |
91 EXPECT_TRUE(proceed_callback_called); | |
estark
2016/06/29 23:00:15
optional additional test: you could test that Dont
Eugene But (OOO till 7-30)
2016/06/30 00:05:26
Done. Let me know if you think that I should also
| |
92 } | |
OLD | NEW |