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