1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ssl/ssl_error_handler.h"
#include "base/callback.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/field_trial.h"
#include "base/run_loop.h"
#include "base/time/time.h"
#include "chrome/browser/captive_portal/captive_portal_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ssl/common_name_mismatch_handler.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "components/captive_portal/captive_portal_testing_utils.h"
#include "content/public/browser/notification_service.h"
#include "net/base/net_errors.h"
#include "net/base/test_data_directory.h"
#include "net/cert/x509_certificate.h"
#include "net/ssl/ssl_info.h"
#include "net/test/cert_test_util.h"
#include "net/test/test_certificate_data.h"
#include "testing/gtest/include/gtest/gtest.h"
class TestSSLErrorHandler : public SSLErrorHandler {
public:
TestSSLErrorHandler(Profile* profile,
content::WebContents* web_contents,
const net::SSLInfo& ssl_info)
: SSLErrorHandler(web_contents,
net::ERR_CERT_COMMON_NAME_INVALID,
ssl_info,
GURL(),
0,
nullptr,
base::Callback<void(bool)>()),
profile_(profile),
captive_portal_checked_(false),
suggested_url_exists_(false),
suggested_url_checked_(false),
ssl_interstitial_shown_(false),
captive_portal_interstitial_shown_(false),
common_name_mismatch_redirect_(false) {}
~TestSSLErrorHandler() override {
}
using SSLErrorHandler::StartHandlingError;
void SendCaptivePortalNotification(
captive_portal::CaptivePortalResult result) {
CaptivePortalService::Results results;
results.previous_result = captive_portal::RESULT_INTERNET_CONNECTED;
results.result = result;
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT,
content::Source<Profile>(profile_),
content::Details<CaptivePortalService::Results>(&results));
}
void SendSuggestedUrlCheckResult(
const CommonNameMismatchHandler::SuggestedUrlCheckResult& result,
const GURL& suggested_url) {
CommonNameMismatchHandlerCallback(result, suggested_url);
}
bool IsTimerRunning() const {
return get_timer().IsRunning();
}
int captive_portal_checked() const {
return captive_portal_checked_;
}
int ssl_interstitial_shown() const {
return ssl_interstitial_shown_;
}
int captive_portal_interstitial_shown() const {
return captive_portal_interstitial_shown_;
}
void SetSuggestedUrlExists(bool suggested_url_exists) {
suggested_url_exists_ = suggested_url_exists;
}
bool GetSuggestedUrl(const std::vector<std::string>& dns_names,
GURL* suggested_url) const override {
if (!suggested_url_exists_)
return false;
*suggested_url = GURL("www.example.com");
return true;
}
bool suggested_url_checked() const { return suggested_url_checked_; }
bool common_name_mismatch_redirect() const {
return common_name_mismatch_redirect_;
}
void Reset() {
captive_portal_checked_ = false;
suggested_url_exists_ = false;
suggested_url_checked_ = false;
ssl_interstitial_shown_ = false;
captive_portal_interstitial_shown_ = false;
common_name_mismatch_redirect_ = false;
}
private:
void CheckForCaptivePortal() override {
captive_portal_checked_ = true;
}
void ShowSSLInterstitial() override { ssl_interstitial_shown_ = true; }
void ShowCaptivePortalInterstitial(const GURL& landing_url) override {
captive_portal_interstitial_shown_ = true;
}
void CheckSuggestedUrl(const GURL& suggested_url) override {
suggested_url_checked_ = true;
}
void NavigateToSuggestedURL(const GURL& suggested_url) override {
common_name_mismatch_redirect_ = true;
}
Profile* profile_;
bool captive_portal_checked_;
bool suggested_url_exists_;
bool suggested_url_checked_;
bool ssl_interstitial_shown_;
bool captive_portal_interstitial_shown_;
bool common_name_mismatch_redirect_;
DISALLOW_COPY_AND_ASSIGN(TestSSLErrorHandler);
};
class SSLErrorHandlerTest : public ChromeRenderViewHostTestHarness {
public:
SSLErrorHandlerTest()
: field_trial_list_(NULL) {
}
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
SSLErrorHandler::SetInterstitialDelayTypeForTest(SSLErrorHandler::NONE);
ssl_info_.cert =
net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem");
ssl_info_.cert_status = net::CERT_STATUS_COMMON_NAME_INVALID;
error_handler_.reset(new TestSSLErrorHandler(profile(),
web_contents(),
ssl_info_));
// Enable finch experiment for captive portal interstitials.
ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
"CaptivePortalInterstitial", "Enabled"));
}
void TearDown() override {
EXPECT_FALSE(error_handler()->IsTimerRunning());
error_handler_.reset(NULL);
ChromeRenderViewHostTestHarness::TearDown();
}
TestSSLErrorHandler* error_handler() { return error_handler_.get(); }
private:
net::SSLInfo ssl_info_;
scoped_ptr<TestSSLErrorHandler> error_handler_;
base::FieldTrialList field_trial_list_;
};
#if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
TEST_F(SSLErrorHandlerTest,
ShouldShowSSLInterstitialOnTimerExpired) {
EXPECT_FALSE(error_handler()->IsTimerRunning());
error_handler()->StartHandlingError();
EXPECT_TRUE(error_handler()->IsTimerRunning());
EXPECT_TRUE(error_handler()->captive_portal_checked());
EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown());
error_handler()->Reset();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_FALSE(error_handler()->IsTimerRunning());
EXPECT_FALSE(error_handler()->captive_portal_checked());
EXPECT_TRUE(error_handler()->ssl_interstitial_shown());
EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown());
}
TEST_F(SSLErrorHandlerTest,
ShouldShowCustomInterstitialOnCaptivePortalResult) {
EXPECT_FALSE(error_handler()->IsTimerRunning());
error_handler()->StartHandlingError();
EXPECT_TRUE(error_handler()->IsTimerRunning());
EXPECT_TRUE(error_handler()->captive_portal_checked());
EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown());
// Fake a captive portal result.
error_handler()->Reset();
error_handler()->SendCaptivePortalNotification(
captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_FALSE(error_handler()->IsTimerRunning());
EXPECT_FALSE(error_handler()->captive_portal_checked());
EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
EXPECT_TRUE(error_handler()->captive_portal_interstitial_shown());
}
TEST_F(SSLErrorHandlerTest,
ShouldShowSSLInterstitialOnNoCaptivePortalResult) {
EXPECT_FALSE(error_handler()->IsTimerRunning());
error_handler()->StartHandlingError();
EXPECT_TRUE(error_handler()->IsTimerRunning());
EXPECT_TRUE(error_handler()->captive_portal_checked());
EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown());
// Fake a "connected to internet" result for the captive portal check.
// This should immediately trigger an SSL interstitial without waiting for
// the timer to expire.
error_handler()->Reset();
error_handler()->SendCaptivePortalNotification(
captive_portal::RESULT_INTERNET_CONNECTED);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_FALSE(error_handler()->IsTimerRunning());
EXPECT_FALSE(error_handler()->captive_portal_checked());
EXPECT_TRUE(error_handler()->ssl_interstitial_shown());
EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown());
}
TEST_F(SSLErrorHandlerTest, ShouldNotCheckSuggestedUrlIfNoSuggestedUrl) {
error_handler()->SetSuggestedUrlExists(false);
error_handler()->StartHandlingError();
EXPECT_TRUE(error_handler()->captive_portal_checked());
EXPECT_TRUE(error_handler()->IsTimerRunning());
EXPECT_FALSE(error_handler()->suggested_url_checked());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(error_handler()->IsTimerRunning());
EXPECT_TRUE(error_handler()->ssl_interstitial_shown());
}
TEST_F(SSLErrorHandlerTest, ShouldNotCheckCaptivePortalIfSuggestedUrlExists) {
EXPECT_FALSE(error_handler()->IsTimerRunning());
error_handler()->SetSuggestedUrlExists(true);
error_handler()->StartHandlingError();
EXPECT_TRUE(error_handler()->IsTimerRunning());
EXPECT_TRUE(error_handler()->suggested_url_checked());
EXPECT_FALSE(error_handler()->captive_portal_checked());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(error_handler()->IsTimerRunning());
EXPECT_TRUE(error_handler()->ssl_interstitial_shown());
}
#else // #if !defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
TEST_F(SSLErrorHandlerTest,
ShouldShowSSLInterstitialOnCaptivePortalDetectionDisabled) {
EXPECT_FALSE(error_handler()->IsTimerRunning());
error_handler()->SetSuggestedUrlExists(false);
error_handler()->StartHandlingError();
EXPECT_FALSE(error_handler()->IsTimerRunning());
EXPECT_FALSE(error_handler()->captive_portal_checked());
EXPECT_TRUE(error_handler()->ssl_interstitial_shown());
EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown());
}
#endif // defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
TEST_F(SSLErrorHandlerTest,
ShouldShowSSLInterstitialOnTimerExpiredWhenSuggestedUrlExists) {
error_handler()->SetSuggestedUrlExists(true);
error_handler()->StartHandlingError();
EXPECT_TRUE(error_handler()->IsTimerRunning());
EXPECT_TRUE(error_handler()->suggested_url_checked());
EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
EXPECT_FALSE(error_handler()->common_name_mismatch_redirect());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(error_handler()->IsTimerRunning());
EXPECT_TRUE(error_handler()->ssl_interstitial_shown());
EXPECT_FALSE(error_handler()->common_name_mismatch_redirect());
}
TEST_F(SSLErrorHandlerTest, ShouldRedirectOnSuggestedUrlCheckResult) {
error_handler()->SetSuggestedUrlExists(true);
error_handler()->StartHandlingError();
EXPECT_TRUE(error_handler()->IsTimerRunning());
EXPECT_TRUE(error_handler()->suggested_url_checked());
EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
EXPECT_FALSE(error_handler()->common_name_mismatch_redirect());
// Fake a valid suggested URL check result.
// The URL returned by |SuggestedUrlCheckResult| can be different from
// |suggested_url|, if there is a redirect.
error_handler()->SendSuggestedUrlCheckResult(
CommonNameMismatchHandler::SuggestedUrlCheckResult::
SUGGESTED_URL_AVAILABLE,
GURL("https://random.example.com"));
EXPECT_FALSE(error_handler()->IsTimerRunning());
EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
EXPECT_TRUE(error_handler()->common_name_mismatch_redirect());
}
TEST_F(SSLErrorHandlerTest, ShouldShowSSLInterstitialOnInvalidUrlCheckResult) {
error_handler()->SetSuggestedUrlExists(true);
error_handler()->StartHandlingError();
EXPECT_TRUE(error_handler()->IsTimerRunning());
EXPECT_TRUE(error_handler()->suggested_url_checked());
EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
EXPECT_FALSE(error_handler()->common_name_mismatch_redirect());
// Fake an Invalid Suggested URL Check result.
error_handler()->SendSuggestedUrlCheckResult(
CommonNameMismatchHandler::SuggestedUrlCheckResult::
SUGGESTED_URL_NOT_AVAILABLE,
GURL());
EXPECT_FALSE(error_handler()->IsTimerRunning());
EXPECT_TRUE(error_handler()->ssl_interstitial_shown());
EXPECT_FALSE(error_handler()->common_name_mismatch_redirect());
}
|