forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptive_portal_browsertest.cc
2699 lines (2291 loc) · 112 KB
/
captive_portal_browsertest.cc
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 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 <algorithm>
#include <atomic>
#include <iterator>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/base_switches.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/scoped_observer.h"
#include "base/sequence_checker.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/post_task.h"
#include "base/test/bind_test_util.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/captive_portal/captive_portal_service.h"
#include "chrome/browser/captive_portal/captive_portal_service_factory.h"
#include "chrome/browser/captive_portal/captive_portal_tab_helper.h"
#include "chrome/browser/captive_portal/captive_portal_tab_reloader.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ssl/captive_portal_blocking_page.h"
#include "chrome/browser/ssl/ssl_blocking_page.h"
#include "chrome/browser/ssl/ssl_error_handler.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/prefs/pref_service.h"
#include "components/security_interstitials/content/security_interstitial_page.h"
#include "components/security_interstitials/content/security_interstitial_tab_helper.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/interstitial_page.h"
#include "content/public/browser/interstitial_page_delegate.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "net/base/net_errors.h"
#include "net/cert/x509_certificate.h"
#include "net/http/transport_security_state.h"
#include "net/test/cert_test_util.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/test_data_directory.h"
#include "net/test/url_request/url_request_failed_job.h"
#include "net/test/url_request/url_request_mock_http_job.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_status.h"
#include "testing/gtest/include/gtest/gtest.h"
using captive_portal::CaptivePortalResult;
using content::BrowserThread;
using content::WebContents;
namespace {
// Path of the fake login page, when using the TestServer.
const char* const kTestServerLoginPath = "/captive_portal/login.html";
// Path of a page with an iframe that has a mock SSL timeout, when using the
// TestServer.
const char* const kTestServerIframeTimeoutPath =
"/captive_portal/iframe_timeout.html";
// Path of a page that redirects to kMockHttpsUrl.
const char* const kRedirectToMockHttpsPath =
"/captive_portal/redirect_to_mock_https.html";
// Path of a page that serves a bad SSL certificate.
// The path doesn't matter because all we need is that it's served from a
// server that's configured to serve a bad cert.
const char* const kMockHttpsBadCertPath = "/bad_cert.html";
// The following URLs each have two different behaviors, depending on whether
// URLRequestMockCaptivePortalJobFactory is currently simulating the presence
// of a captive portal or not. They use different domains so that HSTS can be
// applied to them independently.
// A mock URL for the CaptivePortalService's |test_url|. When behind a captive
// portal, this URL returns a mock login page. When connected to the Internet,
// it returns a 204 response. Uses the name of the login file so that reloading
// it will not request a different URL.
const char* const kMockCaptivePortalTestUrl =
"http://mock.captive.portal.test/login.html";
// Another mock URL for the CaptivePortalService's |test_url|. When behind a
// captive portal, this URL returns a 511 status code and an HTML page that
// redirect to the above URL. When connected to the Internet, it returns a 204
// response.
const char* const kMockCaptivePortal511Url =
"http://mock.captive.portal.511/page511.html";
// When behind a captive portal, this URL hangs without committing until a call
// to FailJobs. When that function is called, the request will time out.
//
// When connected to the Internet, this URL returns a non-error page.
const char* const kMockHttpsUrl =
"https://mock.captive.portal.long.timeout/title2.html";
// Same as above, but different domain, so can be used to trigger cross-site
// navigations.
const char* const kMockHttpsUrl2 =
"https://mock.captive.portal.long.timeout2/title2.html";
// Same as kMockHttpsUrl, except the timeout happens instantly.
const char* const kMockHttpsQuickTimeoutUrl =
"https://mock.captive.portal.quick.timeout/title2.html";
// The intercepted URLs used to mock errors.
const char* const kMockHttpConnectionTimeoutErr =
"http://mock.captive.portal.quick.error/timeout";
const char* const kMockHttpsConnectionTimeoutErr =
"https://mock.captive.portal.quick.error/timeout";
const char* const kMockHttpsConnectionUnexpectedErr =
"https://mock.captive.portal.quick.error/unexpected";
const char* const kMockHttpConnectionConnectionClosedErr =
"http://mock.captive.portal.quick.error/connection_closed";
// Expected title of a tab once an HTTPS load completes, when not behind a
// captive portal.
const char* const kInternetConnectedTitle = "Title Of Awesomeness";
// Creates a server-side redirect for use with the TestServer.
std::string CreateServerRedirect(const std::string& dest_url) {
const char* const kServerRedirectBase = "/server-redirect?";
return kServerRedirectBase + dest_url;
}
// Returns the total number of loading tabs across all Browsers, for all
// Profiles.
int NumLoadingTabs() {
return std::count_if(AllTabContentses().begin(), AllTabContentses().end(),
[](content::WebContents* web_contents) {
return web_contents->IsLoading();
});
}
bool IsLoginTab(WebContents* web_contents) {
return CaptivePortalTabHelper::FromWebContents(web_contents)->IsLoginTab();
}
// Tracks how many times each tab has been navigated since the Observer was
// created. The standard TestNavigationObserver can only watch specific
// pre-existing tabs or loads in serial for all tabs.
class MultiNavigationObserver : public content::NotificationObserver {
public:
MultiNavigationObserver();
~MultiNavigationObserver() override;
// Waits for exactly |num_navigations_to_wait_for| LOAD_STOP
// notifications to have occurred since the construction of |this|. More
// navigations than expected occuring will trigger a expect failure.
void WaitForNavigations(int num_navigations_to_wait_for);
// Returns the number of LOAD_STOP events that have occurred for
// |web_contents| since this was constructed.
int NumNavigationsForTab(WebContents* web_contents) const;
// The number of LOAD_STOP events since |this| was created.
int num_navigations() const { return num_navigations_; }
private:
typedef std::map<const WebContents*, int> TabNavigationMap;
// content::NotificationObserver:
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
int num_navigations_;
// Map of how many times each tab has navigated since |this| was created.
TabNavigationMap tab_navigation_map_;
// Total number of navigations to wait for. Value only matters when
// |waiting_for_navigation_| is true.
int num_navigations_to_wait_for_;
// True if WaitForNavigations has been called, until
// |num_navigations_to_wait_for_| have been observed.
bool waiting_for_navigation_;
std::unique_ptr<base::RunLoop> run_loop_;
content::NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(MultiNavigationObserver);
};
MultiNavigationObserver::MultiNavigationObserver()
: num_navigations_(0),
num_navigations_to_wait_for_(0),
waiting_for_navigation_(false) {
registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
content::NotificationService::AllSources());
}
MultiNavigationObserver::~MultiNavigationObserver() {
}
void MultiNavigationObserver::WaitForNavigations(
int num_navigations_to_wait_for) {
// Shouldn't already be waiting for navigations.
EXPECT_FALSE(waiting_for_navigation_);
EXPECT_LT(0, num_navigations_to_wait_for);
if (num_navigations_ < num_navigations_to_wait_for) {
num_navigations_to_wait_for_ = num_navigations_to_wait_for;
waiting_for_navigation_ = true;
run_loop_ = std::make_unique<base::RunLoop>();
run_loop_->Run();
EXPECT_FALSE(waiting_for_navigation_);
}
EXPECT_EQ(num_navigations_, num_navigations_to_wait_for);
}
int MultiNavigationObserver::NumNavigationsForTab(
WebContents* web_contents) const {
auto tab_navigations = tab_navigation_map_.find(web_contents);
if (tab_navigations == tab_navigation_map_.end())
return 0;
return tab_navigations->second;
}
void MultiNavigationObserver::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
ASSERT_EQ(type, content::NOTIFICATION_LOAD_STOP);
content::NavigationController* controller =
content::Source<content::NavigationController>(source).ptr();
++num_navigations_;
++tab_navigation_map_[controller->GetWebContents()];
if (waiting_for_navigation_ &&
num_navigations_to_wait_for_ == num_navigations_) {
waiting_for_navigation_ = false;
if (run_loop_)
run_loop_->Quit();
}
}
// This observer creates a list of loading tabs, and then waits for them all
// to stop loading and have the kInternetConnectedTitle.
//
// This is for the specific purpose of observing tabs time out after logging in
// to a captive portal, which will then cause them to reload.
// MultiNavigationObserver is insufficient for this because there may or may not
// be a LOAD_STOP event between the timeout and the reload.
// See bug http://crbug.com/133227
class FailLoadsAfterLoginObserver : public content::NotificationObserver {
public:
FailLoadsAfterLoginObserver();
~FailLoadsAfterLoginObserver() override;
void WaitForNavigations();
private:
typedef std::set<const WebContents*> TabSet;
// content::NotificationObserver:
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
// The set of tabs that need to be navigated. This is the set of loading
// tabs when the observer is created.
TabSet tabs_needing_navigation_;
// Number of tabs that have stopped navigating with the expected title. These
// are expected not to be navigated again.
TabSet tabs_navigated_to_final_destination_;
// True if WaitForNavigations has been called, until
// |tabs_navigated_to_final_destination_| equals |tabs_needing_navigation_|.
bool waiting_for_navigation_;
std::unique_ptr<base::RunLoop> run_loop_;
content::NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(FailLoadsAfterLoginObserver);
};
FailLoadsAfterLoginObserver::FailLoadsAfterLoginObserver()
: waiting_for_navigation_(false) {
registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
content::NotificationService::AllSources());
std::copy_if(
AllTabContentses().begin(), AllTabContentses().end(),
std::inserter(tabs_needing_navigation_, tabs_needing_navigation_.end()),
[](content::WebContents* web_contents) {
return web_contents->IsLoading();
});
}
FailLoadsAfterLoginObserver::~FailLoadsAfterLoginObserver() {
}
void FailLoadsAfterLoginObserver::WaitForNavigations() {
// Shouldn't already be waiting for navigations.
EXPECT_FALSE(waiting_for_navigation_);
if (tabs_needing_navigation_.size() !=
tabs_navigated_to_final_destination_.size()) {
waiting_for_navigation_ = true;
run_loop_ = std::make_unique<base::RunLoop>();
run_loop_->Run();
EXPECT_FALSE(waiting_for_navigation_);
}
EXPECT_EQ(tabs_needing_navigation_.size(),
tabs_navigated_to_final_destination_.size());
}
void FailLoadsAfterLoginObserver::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
ASSERT_EQ(type, content::NOTIFICATION_LOAD_STOP);
content::NavigationController* controller =
content::Source<content::NavigationController>(source).ptr();
WebContents* contents = controller->GetWebContents();
ASSERT_EQ(1u, tabs_needing_navigation_.count(contents));
ASSERT_EQ(0u, tabs_navigated_to_final_destination_.count(contents));
if (contents->GetTitle() != base::ASCIIToUTF16(kInternetConnectedTitle))
return;
tabs_navigated_to_final_destination_.insert(contents);
if (waiting_for_navigation_ &&
tabs_needing_navigation_.size() ==
tabs_navigated_to_final_destination_.size()) {
waiting_for_navigation_ = false;
if (run_loop_)
run_loop_->Quit();
}
}
// An observer for watching the CaptivePortalService. It tracks the last
// received result and the total number of received results.
class CaptivePortalObserver : public content::NotificationObserver {
public:
explicit CaptivePortalObserver(Profile* profile);
// Runs the message loop until exactly |update_count| captive portal
// results have been received, since the creation of |this|. Expects no
// additional captive portal results.
void WaitForResults(int num_results_to_wait_for);
int num_results_received() const { return num_results_received_; }
CaptivePortalResult captive_portal_result() const {
return captive_portal_result_;
}
private:
// Records results and exits the message loop, if needed.
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
// Number of times OnPortalResult has been called since construction.
int num_results_received_;
// If WaitForResults was called, the total number of updates for which to
// wait. Value doesn't matter when |waiting_for_result_| is false.
int num_results_to_wait_for_;
bool waiting_for_result_;
std::unique_ptr<base::RunLoop> run_loop_;
Profile* profile_;
CaptivePortalService* captive_portal_service_;
// Last result received.
CaptivePortalResult captive_portal_result_;
content::NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(CaptivePortalObserver);
};
CaptivePortalObserver::CaptivePortalObserver(Profile* profile)
: num_results_received_(0),
num_results_to_wait_for_(0),
waiting_for_result_(false),
profile_(profile),
captive_portal_service_(
CaptivePortalServiceFactory::GetForProfile(profile)),
captive_portal_result_(
captive_portal_service_->last_detection_result()) {
registrar_.Add(this,
chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT,
content::Source<Profile>(profile_));
}
void CaptivePortalObserver::WaitForResults(int num_results_to_wait_for) {
EXPECT_LT(0, num_results_to_wait_for);
EXPECT_FALSE(waiting_for_result_);
if (num_results_received_ < num_results_to_wait_for) {
num_results_to_wait_for_ = num_results_to_wait_for;
waiting_for_result_ = true;
run_loop_ = std::make_unique<base::RunLoop>();
run_loop_->Run();
EXPECT_FALSE(waiting_for_result_);
}
EXPECT_EQ(num_results_to_wait_for, num_results_received_);
}
void CaptivePortalObserver::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
ASSERT_EQ(type, chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT);
ASSERT_EQ(profile_, content::Source<Profile>(source).ptr());
CaptivePortalService::Results* results =
content::Details<CaptivePortalService::Results>(details).ptr();
EXPECT_EQ(captive_portal_result_, results->previous_result);
EXPECT_EQ(captive_portal_service_->last_detection_result(),
results->result);
captive_portal_result_ = results->result;
++num_results_received_;
if (waiting_for_result_ &&
num_results_to_wait_for_ == num_results_received_) {
waiting_for_result_ = false;
if (run_loop_)
run_loop_->Quit();
}
}
// This observer waits for the SSLErrorHandler to start an interstitial timer
// for the given web contents.
class SSLInterstitialTimerObserver {
public:
explicit SSLInterstitialTimerObserver(content::WebContents* web_contents);
~SSLInterstitialTimerObserver();
// Waits until the interstitial delay timer in SSLErrorHandler is started.
void WaitForTimerStarted();
private:
void OnTimerStarted(content::WebContents* web_contents);
const content::WebContents* web_contents_;
SSLErrorHandler::TimerStartedCallback callback_;
scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
DISALLOW_COPY_AND_ASSIGN(SSLInterstitialTimerObserver);
};
SSLInterstitialTimerObserver::SSLInterstitialTimerObserver(
content::WebContents* web_contents)
: web_contents_(web_contents),
message_loop_runner_(new content::MessageLoopRunner) {
callback_ = base::Bind(&SSLInterstitialTimerObserver::OnTimerStarted,
base::Unretained(this));
SSLErrorHandler::SetInterstitialTimerStartedCallbackForTesting(&callback_);
}
SSLInterstitialTimerObserver::~SSLInterstitialTimerObserver() {
SSLErrorHandler::SetInterstitialTimerStartedCallbackForTesting(nullptr);
}
void SSLInterstitialTimerObserver::WaitForTimerStarted() {
message_loop_runner_->Run();
}
void SSLInterstitialTimerObserver::OnTimerStarted(
content::WebContents* web_contents) {
if (web_contents_ == web_contents && message_loop_runner_.get())
message_loop_runner_->Quit();
}
// Helper for waiting for a change of the active tab.
// Users can wait for the change via WaitForActiveTabChange method.
// DCHECKs ensure that only one change happens during the lifetime of a
// TabActivationWaiter instance.
class TabActivationWaiter : public TabStripModelObserver {
public:
explicit TabActivationWaiter(TabStripModel* tab_strip_model)
: number_of_unconsumed_active_tab_changes_(0) {
tab_strip_model->AddObserver(this);
}
void WaitForActiveTabChange() {
if (number_of_unconsumed_active_tab_changes_ == 0) {
// Wait until TabStripModelObserver::ActiveTabChanged will get called.
message_loop_runner_ = new content::MessageLoopRunner;
message_loop_runner_->Run();
}
// "consume" one tab activation event.
DCHECK_EQ(1, number_of_unconsumed_active_tab_changes_);
number_of_unconsumed_active_tab_changes_--;
}
// TabStripModelObserver overrides.
void OnTabStripModelChanged(
TabStripModel* tab_strip_model,
const TabStripModelChange& change,
const TabStripSelectionChange& selection) override {
if (tab_strip_model->empty() || !selection.active_tab_changed())
return;
number_of_unconsumed_active_tab_changes_++;
DCHECK_EQ(1, number_of_unconsumed_active_tab_changes_);
if (message_loop_runner_)
message_loop_runner_->Quit();
}
private:
scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
int number_of_unconsumed_active_tab_changes_;
DISALLOW_COPY_AND_ASSIGN(TabActivationWaiter);
};
} // namespace
class CaptivePortalBrowserTest : public InProcessBrowserTest {
public:
CaptivePortalBrowserTest();
// InProcessBrowserTest:
void SetUpOnMainThread() override;
void TearDownOnMainThread() override;
void SetUpCommandLine(base::CommandLine* command_line) override;
// Called by |url_loader_interceptor_|.
// It emulates captive portal behavior.
// Initially, it emulates being behind a captive portal. When
// SetBehindCaptivePortal(false) is called, it emulates behavior when not
// behind a captive portal.
bool OnIntercept(content::URLLoaderInterceptor::RequestParams* params);
// Sets the captive portal checking preference. Does not affect the command
// line flag, which is set in SetUpCommandLine.
void EnableCaptivePortalDetection(Profile* profile, bool enabled);
// Enables or disables actual captive portal probes. Should only be called
// after captive portal service setup is done. When disabled, probe requests
// are silently ignored, never receiving a response.
void RespondToProbeRequests(bool enabled);
// Sets up the captive portal service for the given profile so that
// all checks go to |test_url|. Also disables all timers.
void SetUpCaptivePortalService(Profile* profile, const GURL& test_url);
// Returns true if |browser|'s profile is currently running a captive portal
// check.
bool CheckPending(Browser* browser);
// Returns the type of the interstitial being shown.
content::InterstitialPageDelegate::TypeID GetInterstitialType(
WebContents* contents) const;
bool IsShowingInterstitial(WebContents* contents);
// Asserts an interstitial is showing and waits for the render frame to be
// ready.
void WaitForInterstitial(content::WebContents* contents);
// Returns the CaptivePortalTabReloader::State of |web_contents|.
CaptivePortalTabReloader::State GetStateOfTabReloader(
WebContents* web_contents) const;
// Returns the CaptivePortalTabReloader::State of the indicated tab.
CaptivePortalTabReloader::State GetStateOfTabReloaderAt(Browser* browser,
int index) const;
// Returns the number of tabs with the given state, across all profiles.
int NumTabsWithState(CaptivePortalTabReloader::State state) const;
// Returns the number of tabs broken by captive portals, across all profiles.
int NumBrokenTabs() const;
// Returns the number of tabs that need to be reloaded due to having logged
// in to a captive portal, across all profiles.
int NumNeedReloadTabs() const;
// Navigates |browser|'s active tab to |url| and expects no captive portal
// test to be triggered. |expected_navigations| is the number of times the
// active tab will end up being navigated. It should be 1, except for the
// Link Doctor page, which acts like two navigations.
void NavigateToPageExpectNoTest(Browser* browser,
const GURL& url,
int expected_navigations);
// Navigates |browser|'s active tab to an SSL tab that takes a while to load,
// triggering a captive portal check, which is expected to give the result
// |expected_result|. The page finishes loading, with a timeout, after the
// captive portal check.
void SlowLoadNoCaptivePortal(Browser* browser,
CaptivePortalResult expected_result);
// Navigates |browser|'s active tab to an SSL timeout, expecting a captive
// portal check to be triggered and return a result which will indicates
// there's no detected captive portal.
void FastTimeoutNoCaptivePortal(Browser* browser,
CaptivePortalResult expected_result);
// Navigates the active tab to a slow loading SSL page, which will then
// trigger a captive portal test. The test is expected to find a captive
// portal. The slow loading page will continue to load after the function
// returns, until FailJobs() is called, at which point it will timeout.
//
// When |expect_open_login_tab| is false, no login tab is expected to be
// opened, because one already exists, and the function returns once the
// captive portal test is complete.
//
// If |expect_open_login_tab| is true, a login tab is then expected to be
// opened. It waits until both the login tab has finished loading, and two
// captive portal tests complete. The second test is triggered by the load of
// the captive portal tab completing.
//
// This function must not be called when the active tab is currently loading.
// Waits for the hanging request to be issued, so other functions can rely
// on WaitForJobs having been called.
void SlowLoadBehindCaptivePortal(Browser* browser,
bool expect_open_login_tab);
// Same as above, but takes extra parameters.
//
// |hanging_url| should either be kMockHttpsUrl or redirect to kMockHttpsUrl.
//
// |expected_portal_checks| and |expected_login_tab_navigations| allow
// client-side redirects to be tested. |expected_login_tab_navigations| is
// ignored when |expect_open_login_tab| is false.
void SlowLoadBehindCaptivePortal(Browser* browser,
bool expect_open_login_tab,
const GURL& hanging_url,
int expected_portal_checks,
int expected_login_tab_navigations);
// Just like SlowLoadBehindCaptivePortal, except the navigated tab has
// a connection timeout rather having its time trigger, and the function
// waits until that timeout occurs.
void FastTimeoutBehindCaptivePortal(Browser* browser,
bool expect_open_login_tab);
// Much as above, but accepts a URL parameter and can be used for errors that
// trigger captive portal checks other than timeouts. |error_url| should
// result in an error rather than hanging.
void FastErrorBehindCaptivePortal(Browser* browser,
bool expect_open_login_tab,
const GURL& error_url);
// Navigates the active tab to an SSL error page which triggers an
// interstitial timer. Also disables captive portal checks indefinitely, so
// the page appears to be hanging.
void FastErrorWithInterstitialTimer(Browser* browser,
const GURL& cert_error_url);
// Navigates the login tab without logging in. The login tab must be the
// specified browser's active tab. Expects no other tab to change state.
// |num_loading_tabs| and |num_timed_out_tabs| are used as extra checks
// that nothing has gone wrong prior to the function call.
void NavigateLoginTab(Browser* browser,
int num_loading_tabs,
int num_timed_out_tabs);
// Simulates a login by updating the URLRequestMockCaptivePortalJob's
// behind captive portal state, and navigating the login tab. Waits for
// all broken but not loading tabs to be reloaded.
// |num_loading_tabs| and |num_timed_out_tabs| are used as extra checks
// that nothing has gone wrong prior to the function call.
void Login(Browser* browser, int num_loading_tabs, int num_timed_out_tabs);
// Simulates a login when the broken tab shows an SSL or captive portal
// interstitial. Can't use Login() in those cases because the interstitial
// tab looks like a cross between a hung tab (Load was never committed) and a
// tab at an error page (The load was stopped).
void LoginCertError(Browser* browser);
// Makes the slow SSL loads of all active tabs time out at once, and waits for
// them to finish both that load and the automatic reload it should trigger.
// There should be no timed out tabs when this is called.
void FailLoadsAfterLogin(Browser* browser, int num_loading_tabs);
// Makes the slow SSL loads of all active tabs time out at once, and waits for
// them to finish displaying their error pages. The login tab should be the
// active tab. There should be no timed out tabs when this is called.
void FailLoadsWithoutLogin(Browser* browser, int num_loading_tabs);
// Navigates |browser|'s active tab to |starting_url| while not behind a
// captive portal. Then navigates to |interrupted_url|, which should create
// a URLRequestTimeoutOnDemandJob, which is then abandoned. The load should
// trigger a captive portal check, which finds a captive portal and opens a
// tab.
//
// Then the navigation is interrupted by a navigation to |timeout_url|, which
// should trigger a captive portal check, and finally the test simulates
// logging in.
//
// The purpose of this test is to make sure the TabHelper triggers a captive
// portal check when a load is interrupted by another load, particularly in
// the case of cross-process navigations.
void RunNavigateLoadingTabToTimeoutTest(Browser* browser,
const GURL& starting_url,
const GURL& interrupted_url,
const GURL& timeout_url);
// Sets the timeout used by a CaptivePortalTabReloader on slow SSL loads
// before a captive portal check.
void SetSlowSSLLoadTime(CaptivePortalTabReloader* tab_reloader,
base::TimeDelta slow_ssl_load_time);
CaptivePortalTabReloader* GetTabReloader(WebContents* web_contents) const;
// Sets whether or not there is a captive portal. Outstanding requests are
// not affected.
void SetBehindCaptivePortal(bool behind_captive_portal) {
behind_captive_portal_ = behind_captive_portal;
}
// Waits for exactly |num_jobs| kMockHttps* requests.
void WaitForJobs(int num_jobs) {
if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
SetNumJobsToWaitForOnInterceptorThread(num_jobs);
} else {
base::PostTask(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(
&CaptivePortalBrowserTest::SetNumJobsToWaitForOnInterceptorThread,
base::Unretained(this), num_jobs));
}
run_loop_ = std::make_unique<base::RunLoop>();
// Will be exited via QuitRunLoop() when the interceptor has received
// |num_jobs|.
run_loop_->Run();
}
void SetNumJobsToWaitForOnInterceptorThread(int num_jobs) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(!num_jobs_to_wait_for_);
int num_ongoing_jobs = static_cast<int>(ongoing_mock_requests_.size());
if (num_ongoing_jobs == num_jobs) {
base::PostTask(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&CaptivePortalBrowserTest::QuitRunLoop,
base::Unretained(this)));
return;
}
EXPECT_LT(num_ongoing_jobs, num_jobs);
num_jobs_to_wait_for_ = num_jobs;
}
// Fails all active kMockHttps* requests with connection timeouts.
// There are expected to be exactly |expected_num_jobs| waiting for
// failure. The only way to guarantee this is with an earlier call to
// WaitForJobs, so makes sure there has been a matching WaitForJobs call.
void FailJobs(int expected_num_jobs) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
base::PostTask(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&CaptivePortalBrowserTest::FailJobs,
base::Unretained(this), expected_num_jobs));
return;
}
EXPECT_EQ(expected_num_jobs,
static_cast<int>(ongoing_mock_requests_.size()));
network::URLLoaderCompletionStatus status;
status.error_code = net::ERR_CONNECTION_TIMED_OUT;
for (auto& job : ongoing_mock_requests_)
job.client->OnComplete(status);
ongoing_mock_requests_.clear();
}
// Fails all active kMockHttps* requests with SSL cert errors.
// |expected_num_jobs| behaves just as in FailJobs.
void FailJobsWithCertError(int expected_num_jobs,
const net::SSLInfo& ssl_info) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
base::PostTask(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(&CaptivePortalBrowserTest::FailJobsWithCertError,
base::Unretained(this), expected_num_jobs, ssl_info));
return;
}
DCHECK(intercept_bad_cert_);
// With the network service enabled, these will be requests to
// kMockHttpsBadCertPath that is served by a misconfigured
// EmbeddedTestServer. Once the request reaches the network service, it'll
// notice the bad SSL cert.
// Set |intercept_bad_cert_| so that when we use the network service'
// URLLoaderFactory again it doesn't get intercepted and goes to the
// nework process. This has to be done on the UI thread as that's where we
// currently have a public URLLoaderFactory for the profile.
intercept_bad_cert_ = false;
EXPECT_EQ(expected_num_jobs,
static_cast<int>(ongoing_mock_requests_.size()));
for (auto& job : ongoing_mock_requests_) {
base::PostTask(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&CaptivePortalBrowserTest::CreateLoader,
base::Unretained(this), std::move(job)));
}
ongoing_mock_requests_.clear();
}
void CreateLoader(content::URLLoaderInterceptor::RequestParams job) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
content::BrowserContext::GetDefaultStoragePartition(browser()->profile())
->GetURLLoaderFactoryForBrowserProcess()
->CreateLoaderAndStart(std::move(job.receiver), job.routing_id,
job.request_id, job.options,
std::move(job.url_request),
std::move(job.client), job.traffic_annotation);
}
// Abandon all active kMockHttps* requests. |expected_num_jobs|
// behaves just as in FailJobs.
void AbandonJobs(int expected_num_jobs) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
base::PostTask(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&CaptivePortalBrowserTest::AbandonJobs,
base::Unretained(this), expected_num_jobs));
return;
}
EXPECT_EQ(expected_num_jobs,
static_cast<int>(ongoing_mock_requests_.size()));
for (auto& job : ongoing_mock_requests_)
ignore_result(job.client.PassInterface().PassHandle().release());
ongoing_mock_requests_.clear();
}
// Returns the contents of the given filename under chrome/test/data.
static std::string GetContents(const std::string& path) {
base::FilePath root_http;
base::PathService::Get(chrome::DIR_TEST_DATA, &root_http);
base::ScopedAllowBlockingForTesting allow_io;
base::FilePath file_path = root_http.AppendASCII(path);
std::string contents;
CHECK(base::ReadFileToString(file_path, &contents));
return contents;
}
void QuitRunLoop() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (run_loop_)
run_loop_->Quit();
}
protected:
std::unique_ptr<content::URLLoaderInterceptor> url_loader_interceptor_;
std::unique_ptr<base::RunLoop> run_loop_;
// Only accessed on the UI thread.
int num_jobs_to_wait_for_ = 0;
std::vector<content::URLLoaderInterceptor::RequestParams>
ongoing_mock_requests_;
std::atomic<bool> behind_captive_portal_;
bool intercept_bad_cert_ = true;
private:
DISALLOW_COPY_AND_ASSIGN(CaptivePortalBrowserTest);
};
CaptivePortalBrowserTest::CaptivePortalBrowserTest()
: behind_captive_portal_(true) {}
void CaptivePortalBrowserTest::SetUpOnMainThread() {
url_loader_interceptor_ =
std::make_unique<content::URLLoaderInterceptor>(base::Bind(
&CaptivePortalBrowserTest::OnIntercept, base::Unretained(this)));
// Double-check that the captive portal service isn't enabled by default for
// browser tests.
EXPECT_EQ(CaptivePortalService::DISABLED_FOR_TESTING,
CaptivePortalService::get_state_for_testing());
CaptivePortalService::set_state_for_testing(
CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
EnableCaptivePortalDetection(browser()->profile(), true);
// Set the captive portal service to use URLRequestMockCaptivePortalJob's
// mock URL, by default.
SetUpCaptivePortalService(browser()->profile(),
GURL(kMockCaptivePortalTestUrl));
// Set SSL interstitial delay long enough so that a captive portal result
// is guaranteed to arrive during this window, and a captive portal
// error page is displayed instead of an SSL interstitial.
SSLErrorHandler::SetInterstitialDelayForTesting(
base::TimeDelta::FromHours(1));
}
bool CaptivePortalBrowserTest::OnIntercept(
content::URLLoaderInterceptor::RequestParams* params) {
if (params->url_request.url.path() == kMockHttpsBadCertPath &&
intercept_bad_cert_) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
ongoing_mock_requests_.emplace_back(std::move(*params));
return true;
}
auto url_string = params->url_request.url.spec();
net::Error error = net::OK;
if (url_string == kMockHttpConnectionTimeoutErr ||
url_string == kMockHttpsConnectionTimeoutErr) {
error = net::ERR_CONNECTION_TIMED_OUT;
} else if (url_string == kMockHttpsConnectionUnexpectedErr) {
error = net::ERR_UNEXPECTED;
} else if (url_string == kMockHttpConnectionConnectionClosedErr) {
error = net::ERR_CONNECTION_CLOSED;
}
if (error != net::OK) {
params->client->OnComplete(network::URLLoaderCompletionStatus(error));
return true;
}
if (url_string == kMockHttpsUrl || url_string == kMockHttpsUrl2 ||
url_string == kMockHttpsQuickTimeoutUrl ||
params->url_request.url.path() == kRedirectToMockHttpsPath) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (params->url_request.url.path() == kRedirectToMockHttpsPath) {
net::RedirectInfo redirect_info;
redirect_info.new_url = GURL(kMockHttpsUrl);
redirect_info.new_method = "GET";
std::string headers;
headers = base::StringPrintf(
"HTTP/1.0 301 Moved permanently\n"
"Location: %s\n"
"Content-Type: text/html\n\n",
kMockHttpsUrl);
net::HttpResponseInfo info;
info.headers = base::MakeRefCounted<net::HttpResponseHeaders>(
net::HttpUtil::AssembleRawHeaders(headers));
auto response = network::mojom::URLResponseHead::New();
response->headers = info.headers;
response->headers->GetMimeType(&response->mime_type);
response->encoded_data_length = 0;
params->client->OnReceiveRedirect(redirect_info, std::move(response));
}
if (behind_captive_portal_) {
if (url_string == kMockHttpsQuickTimeoutUrl) {
network::URLLoaderCompletionStatus status;
status.error_code = net::ERR_CONNECTION_TIMED_OUT;
params->client->OnComplete(status);
} else {
ongoing_mock_requests_.emplace_back(std::move(*params));
if (num_jobs_to_wait_for_ ==
static_cast<int>(ongoing_mock_requests_.size())) {
num_jobs_to_wait_for_ = 0;
base::PostTask(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&CaptivePortalBrowserTest::QuitRunLoop,
base::Unretained(this)));
}
}
} else {
// Once logged in to the portal, HTTPS requests return the page that was
// actually requested.
content::URLLoaderInterceptor::WriteResponse(
"HTTP/1.1 200 OK\nContent-type: text/html\n\n",
GetContents("title2.html"), params->client.get());
}
return true;
}
std::string headers;