OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 "chrome/browser/ssl/ssl_error_handler.h" | |
6 | |
7 #include "base/callback.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/run_loop.h" | |
10 #include "chrome/browser/captive_portal/captive_portal_service.h" | |
11 #include "chrome/browser/profiles/profile.h" | |
12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
13 #include "chrome/test/base/testing_profile.h" | |
14 #include "components/captive_portal/captive_portal_testing_utils.h" | |
15 #include "content/public/browser/notification_service.h" | |
16 #include "net/base/net_errors.h" | |
17 #include "net/ssl/ssl_info.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 class TestSSLErrorHandler : public SSLErrorHandler { | |
21 public: | |
22 TestSSLErrorHandler(Profile* profile, | |
23 content::WebContents* web_contents, | |
24 const net::SSLInfo& ssl_info) | |
25 : SSLErrorHandler(web_contents, | |
26 net::ERR_CERT_COMMON_NAME_INVALID, | |
27 ssl_info, | |
28 GURL(), | |
29 0, | |
30 base::TimeDelta(), | |
31 base::Callback<void(bool)>()), | |
32 profile_(profile), | |
33 captive_portal_checked_(false), | |
34 ssl_interstitial_shown_(false), | |
35 captive_portal_interstitial_shown_(false) { | |
36 } | |
37 | |
38 ~TestSSLErrorHandler() override { | |
39 } | |
40 | |
41 void StartHandlingError() { | |
42 SSLErrorHandler::StartHandlingError(); | |
43 } | |
mmenke
2014/12/09 22:42:26
I believe this can just be:
using SSLErrorHandler
meacer
2014/12/10 22:48:03
Done.
| |
44 | |
45 void SendCaptivePortalNotification( | |
46 captive_portal::CaptivePortalResult result) { | |
47 CaptivePortalService::Results results; | |
48 results.previous_result = captive_portal::RESULT_INTERNET_CONNECTED; | |
49 results.result = result; | |
50 content::NotificationService::current()->Notify( | |
51 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, | |
52 content::Source<Profile>(profile_), | |
53 content::Details<CaptivePortalService::Results>(&results)); | |
54 } | |
55 | |
56 bool IsTimerRunning() const { | |
57 return timer_.IsRunning(); | |
58 } | |
59 | |
60 int captive_portal_checked() const { | |
61 return captive_portal_checked_; | |
62 } | |
63 | |
64 int ssl_interstitial_shown() const { | |
65 return ssl_interstitial_shown_; | |
66 } | |
67 | |
68 int captive_portal_interstitial_shown() const { | |
69 return captive_portal_interstitial_shown_; | |
70 } | |
71 | |
72 void Reset() { | |
73 captive_portal_checked_ = false; | |
74 ssl_interstitial_shown_ = false; | |
75 captive_portal_interstitial_shown_ = false; | |
76 } | |
77 | |
78 private: | |
79 void CheckForCaptivePortal() override { | |
80 captive_portal_checked_ = true; | |
81 } | |
82 | |
83 void ShowSSLInterstitial() override { | |
84 ssl_interstitial_shown_ = true; | |
85 } | |
86 | |
87 void ShowCaptivePortalInterstitial() override { | |
88 captive_portal_interstitial_shown_ = true; | |
89 } | |
90 | |
91 Profile* profile_; | |
92 bool captive_portal_checked_; | |
93 bool ssl_interstitial_shown_; | |
94 bool captive_portal_interstitial_shown_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(TestSSLErrorHandler); | |
97 }; | |
98 | |
99 class SSLErrorHandlerTest : public ChromeRenderViewHostTestHarness { | |
100 public: | |
101 void SetUp() override { | |
102 ChromeRenderViewHostTestHarness::SetUp(); | |
103 error_handler_.reset(new TestSSLErrorHandler(profile(), | |
104 web_contents(), | |
105 ssl_info_)); | |
106 } | |
107 | |
108 void TearDown() override { | |
109 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
110 error_handler_.reset(NULL); | |
111 ChromeRenderViewHostTestHarness::TearDown(); | |
112 } | |
113 | |
114 TestSSLErrorHandler* error_handler() { return error_handler_.get(); } | |
115 | |
116 private: | |
117 net::SSLInfo ssl_info_; | |
118 scoped_ptr<TestSSLErrorHandler> error_handler_; | |
119 }; | |
120 | |
121 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | |
122 | |
123 TEST_F(SSLErrorHandlerTest, | |
124 ShouldShowSSLInterstitialOnTimerExpired) { | |
125 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
126 error_handler()->StartHandlingError(); | |
127 | |
128 EXPECT_TRUE(error_handler()->IsTimerRunning()); | |
129 EXPECT_TRUE(error_handler()->captive_portal_checked()); | |
130 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); | |
131 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); | |
132 | |
133 error_handler()->Reset(); | |
134 base::MessageLoop::current()->RunUntilIdle(); | |
135 | |
136 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
137 EXPECT_FALSE(error_handler()->captive_portal_checked()); | |
138 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | |
139 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); | |
140 } | |
141 | |
142 TEST_F(SSLErrorHandlerTest, | |
143 ShouldShowCustomInterstitialOnCaptivePortalResult) { | |
144 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
145 error_handler()->StartHandlingError(); | |
146 | |
147 EXPECT_TRUE(error_handler()->IsTimerRunning()); | |
148 EXPECT_TRUE(error_handler()->captive_portal_checked()); | |
149 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); | |
150 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); | |
151 // Fake a captive portal result. | |
152 error_handler()->Reset(); | |
153 error_handler()->SendCaptivePortalNotification( | |
154 captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL); | |
155 base::MessageLoop::current()->RunUntilIdle(); | |
156 | |
157 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
158 EXPECT_FALSE(error_handler()->captive_portal_checked()); | |
159 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); | |
160 EXPECT_TRUE(error_handler()->captive_portal_interstitial_shown()); | |
161 } | |
162 | |
163 TEST_F(SSLErrorHandlerTest, | |
164 ShouldShowSSLInterstitialOnNoCaptivePortalResult) { | |
165 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
166 error_handler()->StartHandlingError(); | |
167 | |
168 EXPECT_TRUE(error_handler()->IsTimerRunning()); | |
169 EXPECT_TRUE(error_handler()->captive_portal_checked()); | |
170 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); | |
171 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); | |
172 // Fake a "connected to internet" result for the captive portal check. | |
173 // This should immediately trigger an SSL interstitial without waiting for | |
174 // the timer to expire. | |
175 error_handler()->Reset(); | |
176 error_handler()->SendCaptivePortalNotification( | |
177 captive_portal::RESULT_INTERNET_CONNECTED); | |
178 base::MessageLoop::current()->RunUntilIdle(); | |
179 | |
180 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
181 EXPECT_FALSE(error_handler()->captive_portal_checked()); | |
182 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | |
183 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); | |
184 } | |
185 | |
186 #else // #if !defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | |
187 | |
188 TEST_F(SSLErrorHandlerTest, | |
189 ShouldShowSSLInterstitialOnCaptivePortalDetectionDisabled) { | |
190 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
191 error_handler()->StartHandlingError(); | |
192 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
193 EXPECT_FALSE(error_handler()->captive_portal_checked()); | |
194 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | |
195 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); | |
196 } | |
197 | |
198 #endif // defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | |
OLD | NEW |