Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(586)

Side by Side Diff: chrome/browser/captive_portal/captive_portal_browsertest.cc

Issue 318213002: Add custom interstitial for captive portals. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: mmenke comments, add login scenario to browser tests and fix race. Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <map> 5 #include <map>
6 #include <set> 6 #include <set>
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/path_service.h" 14 #include "base/path_service.h"
15 #include "base/prefs/pref_service.h" 15 #include "base/prefs/pref_service.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "chrome/browser/captive_portal/captive_portal_service.h" 17 #include "chrome/browser/captive_portal/captive_portal_service.h"
18 #include "chrome/browser/captive_portal/captive_portal_service_factory.h" 18 #include "chrome/browser/captive_portal/captive_portal_service_factory.h"
19 #include "chrome/browser/captive_portal/captive_portal_tab_helper.h" 19 #include "chrome/browser/captive_portal/captive_portal_tab_helper.h"
20 #include "chrome/browser/captive_portal/captive_portal_tab_reloader.h" 20 #include "chrome/browser/captive_portal/captive_portal_tab_reloader.h"
21 #include "chrome/browser/chrome_notification_types.h" 21 #include "chrome/browser/chrome_notification_types.h"
22 #include "chrome/browser/interstitials/security_interstitial_page.h"
22 #include "chrome/browser/net/url_request_mock_util.h" 23 #include "chrome/browser/net/url_request_mock_util.h"
23 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/ssl/captive_portal_blocking_page.h"
26 #include "chrome/browser/ssl/ssl_blocking_page.h"
24 #include "chrome/browser/ui/browser.h" 27 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/browser_commands.h" 28 #include "chrome/browser/ui/browser_commands.h"
26 #include "chrome/browser/ui/browser_finder.h" 29 #include "chrome/browser/ui/browser_finder.h"
27 #include "chrome/browser/ui/browser_navigator.h" 30 #include "chrome/browser/ui/browser_navigator.h"
28 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" 31 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
29 #include "chrome/browser/ui/tabs/tab_strip_model.h" 32 #include "chrome/browser/ui/tabs/tab_strip_model.h"
30 #include "chrome/common/chrome_paths.h" 33 #include "chrome/common/chrome_paths.h"
31 #include "chrome/common/chrome_switches.h" 34 #include "chrome/common/chrome_switches.h"
32 #include "chrome/common/pref_names.h" 35 #include "chrome/common/pref_names.h"
33 #include "chrome/test/base/in_process_browser_test.h" 36 #include "chrome/test/base/in_process_browser_test.h"
34 #include "chrome/test/base/ui_test_utils.h" 37 #include "chrome/test/base/ui_test_utils.h"
35 #include "content/public/browser/browser_thread.h" 38 #include "content/public/browser/browser_thread.h"
39 #include "content/public/browser/interstitial_page.h"
40 #include "content/public/browser/interstitial_page_delegate.h"
36 #include "content/public/browser/navigation_controller.h" 41 #include "content/public/browser/navigation_controller.h"
42 #include "content/public/browser/navigation_entry.h"
37 #include "content/public/browser/notification_observer.h" 43 #include "content/public/browser/notification_observer.h"
38 #include "content/public/browser/notification_registrar.h" 44 #include "content/public/browser/notification_registrar.h"
39 #include "content/public/browser/notification_service.h" 45 #include "content/public/browser/notification_service.h"
40 #include "content/public/browser/notification_types.h" 46 #include "content/public/browser/notification_types.h"
41 #include "content/public/browser/render_frame_host.h" 47 #include "content/public/browser/render_frame_host.h"
48 #include "content/public/browser/render_view_host.h"
42 #include "content/public/browser/web_contents.h" 49 #include "content/public/browser/web_contents.h"
43 #include "content/public/common/url_constants.h" 50 #include "content/public/common/url_constants.h"
51 #include "content/public/test/browser_test_utils.h"
44 #include "net/base/net_errors.h" 52 #include "net/base/net_errors.h"
45 #include "net/http/transport_security_state.h" 53 #include "net/http/transport_security_state.h"
46 #include "net/test/url_request/url_request_failed_job.h" 54 #include "net/test/url_request/url_request_failed_job.h"
47 #include "net/test/url_request/url_request_mock_http_job.h" 55 #include "net/test/url_request/url_request_mock_http_job.h"
48 #include "net/url_request/url_request.h" 56 #include "net/url_request/url_request.h"
49 #include "net/url_request/url_request_context.h" 57 #include "net/url_request/url_request_context.h"
50 #include "net/url_request/url_request_context_getter.h" 58 #include "net/url_request/url_request_context_getter.h"
51 #include "net/url_request/url_request_filter.h" 59 #include "net/url_request/url_request_filter.h"
52 #include "net/url_request/url_request_job.h" 60 #include "net/url_request/url_request_job.h"
53 #include "net/url_request/url_request_status.h" 61 #include "net/url_request/url_request_status.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 "https://mock.captive.portal.long.timeout2/title2.html"; 110 "https://mock.captive.portal.long.timeout2/title2.html";
103 111
104 // Same as kMockHttpsUrl, except the timeout happens instantly. 112 // Same as kMockHttpsUrl, except the timeout happens instantly.
105 const char* const kMockHttpsQuickTimeoutUrl = 113 const char* const kMockHttpsQuickTimeoutUrl =
106 "https://mock.captive.portal.quick.timeout/title2.html"; 114 "https://mock.captive.portal.quick.timeout/title2.html";
107 115
108 // Expected title of a tab once an HTTPS load completes, when not behind a 116 // Expected title of a tab once an HTTPS load completes, when not behind a
109 // captive portal. 117 // captive portal.
110 const char* const kInternetConnectedTitle = "Title Of Awesomeness"; 118 const char* const kInternetConnectedTitle = "Title Of Awesomeness";
111 119
120 // Wait until all <script> tags have executed, including jstemplate.
121 // This isn't ideal, but the same trick is used in
122 // SafeBrowsingBlockingPageBrowserTest to wait for the interstitials.
123 bool WaitForPageReady(content::RenderViewHost* rvh) {
mmenke 2014/11/26 18:57:48 Hrm...Do you know what this is needed? We embed t
meacer 2014/12/08 22:29:50 C++ side only waits for the navigation to complete
124 if (!rvh)
125 return false;
126 std::string ready_state;
127 do {
128 scoped_ptr<base::Value> value = content::ExecuteScriptAndGetValue(
129 rvh->GetMainFrame(), "document.readyState");
130 if (!value.get() || !value->GetAsString(&ready_state))
131 return false;
132 } while (ready_state != "complete");
133 return true;
134 }
135
112 // A URL request job that hangs until FailJobs() is called. Started jobs 136 // A URL request job that hangs until FailJobs() is called. Started jobs
113 // are stored in a static class variable containing a linked list so that 137 // are stored in a static class variable containing a linked list so that
114 // FailJobs() can locate them. 138 // FailJobs() can locate them.
115 class URLRequestTimeoutOnDemandJob : public net::URLRequestJob, 139 class URLRequestTimeoutOnDemandJob : public net::URLRequestJob,
116 public base::NonThreadSafe { 140 public base::NonThreadSafe {
117 public: 141 public:
118 // net::URLRequestJob: 142 // net::URLRequestJob:
119 void Start() override; 143 void Start() override;
120 144
121 // All the public static methods below can be called on any thread. 145 // All the public static methods below can be called on any thread.
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 base::MessageLoopForUI::current()->Quit(); 706 base::MessageLoopForUI::current()->Quit();
683 } 707 }
684 } 708 }
685 709
686 // An observer for watching the CaptivePortalService. It tracks the last 710 // An observer for watching the CaptivePortalService. It tracks the last
687 // received result and the total number of received results. 711 // received result and the total number of received results.
688 class CaptivePortalObserver : public content::NotificationObserver { 712 class CaptivePortalObserver : public content::NotificationObserver {
689 public: 713 public:
690 explicit CaptivePortalObserver(Profile* profile); 714 explicit CaptivePortalObserver(Profile* profile);
691 715
692 // Runs the message loop until until at exactly |update_count| capitive portal 716 // Runs the message loop until exactly |update_count| captive portal
693 // results have been received, since this creation of |this|. Expects no 717 // results have been received, since the creation of |this|. Expects no
694 // additional captive portal results. 718 // additional captive portal results.
695 void WaitForResults(int num_results_to_wait_for); 719 void WaitForResults(int num_results_to_wait_for);
696 720
697 int num_results_received() const { return num_results_received_; } 721 int num_results_received() const { return num_results_received_; }
698 722
699 CaptivePortalResult captive_portal_result() const { 723 CaptivePortalResult captive_portal_result() const {
700 return captive_portal_result_; 724 return captive_portal_result_;
701 } 725 }
702 726
703 private: 727 private:
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 void EnableCaptivePortalDetection(Profile* profile, bool enabled); 833 void EnableCaptivePortalDetection(Profile* profile, bool enabled);
810 834
811 // Sets up the captive portal service for the given profile so that 835 // Sets up the captive portal service for the given profile so that
812 // all checks go to |test_url|. Also disables all timers. 836 // all checks go to |test_url|. Also disables all timers.
813 void SetUpCaptivePortalService(Profile* profile, const GURL& test_url); 837 void SetUpCaptivePortalService(Profile* profile, const GURL& test_url);
814 838
815 // Returns true if |browser|'s profile is currently running a captive portal 839 // Returns true if |browser|'s profile is currently running a captive portal
816 // check. 840 // check.
817 bool CheckPending(Browser* browser); 841 bool CheckPending(Browser* browser);
818 842
843 // Returns the type of the interstitial being shown.
844 const void* GetInterstitialType(WebContents* contents) const;
845
819 // Returns the CaptivePortalTabReloader::State of |web_contents|. 846 // Returns the CaptivePortalTabReloader::State of |web_contents|.
820 CaptivePortalTabReloader::State GetStateOfTabReloader( 847 CaptivePortalTabReloader::State GetStateOfTabReloader(
821 WebContents* web_contents) const; 848 WebContents* web_contents) const;
822 849
823 // Returns the CaptivePortalTabReloader::State of the indicated tab. 850 // Returns the CaptivePortalTabReloader::State of the indicated tab.
824 CaptivePortalTabReloader::State GetStateOfTabReloaderAt(Browser* browser, 851 CaptivePortalTabReloader::State GetStateOfTabReloaderAt(Browser* browser,
825 int index) const; 852 int index) const;
826 853
827 // Returns the number of tabs with the given state, across all profiles. 854 // Returns the number of tabs with the given state, across all profiles.
828 int NumTabsWithState(CaptivePortalTabReloader::State state) const; 855 int NumTabsWithState(CaptivePortalTabReloader::State state) const;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
890 917
891 // Just like SlowLoadBehindCaptivePortal, except the navigated tab has 918 // Just like SlowLoadBehindCaptivePortal, except the navigated tab has
892 // a connection timeout rather having its time trigger, and the function 919 // a connection timeout rather having its time trigger, and the function
893 // waits until that timeout occurs. 920 // waits until that timeout occurs.
894 void FastTimeoutBehindCaptivePortal(Browser* browser, 921 void FastTimeoutBehindCaptivePortal(Browser* browser,
895 bool expect_open_login_tab); 922 bool expect_open_login_tab);
896 923
897 // Much as above, but accepts a URL parameter and can be used for errors that 924 // Much as above, but accepts a URL parameter and can be used for errors that
898 // trigger captive portal checks other than timeouts. |error_url| should 925 // trigger captive portal checks other than timeouts. |error_url| should
899 // result in an error rather than hanging. 926 // result in an error rather than hanging.
900 void FastErrorBehindCaptivePortal(Browser* browser, 927 void FastErrorBehindCaptivePortal(
901 bool expect_open_login_tab, 928 Browser* browser,
902 const GURL& error_url); 929 bool expect_open_login_tab,
930 const GURL& error_url,
931 bool disable_portal_check_until_interstitial);
903 932
904 // Navigates the login tab without logging in. The login tab must be the 933 // Navigates the login tab without logging in. The login tab must be the
905 // specified browser's active tab. Expects no other tab to change state. 934 // specified browser's active tab. Expects no other tab to change state.
906 // |num_loading_tabs| and |num_timed_out_tabs| are used as extra checks 935 // |num_loading_tabs| and |num_timed_out_tabs| are used as extra checks
907 // that nothing has gone wrong prior to the function call. 936 // that nothing has gone wrong prior to the function call.
908 void NavigateLoginTab(Browser* browser, 937 void NavigateLoginTab(Browser* browser,
909 int num_loading_tabs, 938 int num_loading_tabs,
910 int num_timed_out_tabs); 939 int num_timed_out_tabs);
911 940
912 // Simulates a login by updating the URLRequestMockCaptivePortalJob's 941 // Simulates a login by updating the URLRequestMockCaptivePortalJob's
913 // behind captive portal state, and navigating the login tab. Waits for 942 // behind captive portal state, and navigating the login tab. Waits for
914 // all broken but not loading tabs to be reloaded. 943 // all broken but not loading tabs to be reloaded.
915 // |num_loading_tabs| and |num_timed_out_tabs| are used as extra checks 944 // |num_loading_tabs| and |num_timed_out_tabs| are used as extra checks
916 // that nothing has gone wrong prior to the function call. 945 // that nothing has gone wrong prior to the function call.
917 void Login(Browser* browser, int num_loading_tabs, int num_timed_out_tabs); 946 void Login(Browser* browser, int num_loading_tabs, int num_timed_out_tabs);
918 947
948 // Simulates a login when the broken tab shows an SSL or captive portal
949 // interstitial. Can't use Login() in those cases because the interstitial
950 // tab looks like a cross between a hung tab (Load was never committed) and a
951 // tab at an error page (The load was stopped).
952 void LoginCertError(Browser* browser);
953
919 // Makes the slow SSL loads of all active tabs time out at once, and waits for 954 // Makes the slow SSL loads of all active tabs time out at once, and waits for
920 // them to finish both that load and the automatic reload it should trigger. 955 // them to finish both that load and the automatic reload it should trigger.
921 // There should be no timed out tabs when this is called. 956 // There should be no timed out tabs when this is called.
922 void FailLoadsAfterLogin(Browser* browser, int num_loading_tabs); 957 void FailLoadsAfterLogin(Browser* browser, int num_loading_tabs);
923 958
924 // Makes the slow SSL loads of all active tabs time out at once, and waits for 959 // Makes the slow SSL loads of all active tabs time out at once, and waits for
925 // them to finish displaying their error pages. The login tab should be the 960 // them to finish displaying their error pages. The login tab should be the
926 // active tab. There should be no timed out tabs when this is called. 961 // active tab. There should be no timed out tabs when this is called.
927 void FailLoadsWithoutLogin(Browser* browser, int num_loading_tabs); 962 void FailLoadsWithoutLogin(Browser* browser, int num_loading_tabs);
928 963
(...skipping 13 matching lines...) Expand all
942 void RunNavigateLoadingTabToTimeoutTest(Browser* browser, 977 void RunNavigateLoadingTabToTimeoutTest(Browser* browser,
943 const GURL& starting_url, 978 const GURL& starting_url,
944 const GURL& interrupted_url, 979 const GURL& interrupted_url,
945 const GURL& timeout_url); 980 const GURL& timeout_url);
946 981
947 // Sets the timeout used by a CaptivePortalTabReloader on slow SSL loads 982 // Sets the timeout used by a CaptivePortalTabReloader on slow SSL loads
948 // before a captive portal check. 983 // before a captive portal check.
949 void SetSlowSSLLoadTime(CaptivePortalTabReloader* tab_reloader, 984 void SetSlowSSLLoadTime(CaptivePortalTabReloader* tab_reloader,
950 base::TimeDelta slow_ssl_load_time); 985 base::TimeDelta slow_ssl_load_time);
951 986
987 void SetSSLErrorDisplayDelay(CaptivePortalTabHelper* tab_helper,
988 base::TimeDelta ssl_error_delay);
989
952 CaptivePortalTabReloader* GetTabReloader(WebContents* web_contents) const; 990 CaptivePortalTabReloader* GetTabReloader(WebContents* web_contents) const;
953 991
954 private: 992 private:
955 DISALLOW_COPY_AND_ASSIGN(CaptivePortalBrowserTest); 993 DISALLOW_COPY_AND_ASSIGN(CaptivePortalBrowserTest);
956 }; 994 };
957 995
958 CaptivePortalBrowserTest::CaptivePortalBrowserTest() { 996 CaptivePortalBrowserTest::CaptivePortalBrowserTest() {
959 } 997 }
960 998
961 void CaptivePortalBrowserTest::SetUpOnMainThread() { 999 void CaptivePortalBrowserTest::SetUpOnMainThread() {
(...skipping 26 matching lines...) Expand all
988 void CaptivePortalBrowserTest::EnableCaptivePortalDetection( 1026 void CaptivePortalBrowserTest::EnableCaptivePortalDetection(
989 Profile* profile, bool enabled) { 1027 Profile* profile, bool enabled) {
990 profile->GetPrefs()->SetBoolean(prefs::kAlternateErrorPagesEnabled, enabled); 1028 profile->GetPrefs()->SetBoolean(prefs::kAlternateErrorPagesEnabled, enabled);
991 } 1029 }
992 1030
993 void CaptivePortalBrowserTest::SetUpCaptivePortalService(Profile* profile, 1031 void CaptivePortalBrowserTest::SetUpCaptivePortalService(Profile* profile,
994 const GURL& test_url) { 1032 const GURL& test_url) {
995 CaptivePortalService* captive_portal_service = 1033 CaptivePortalService* captive_portal_service =
996 CaptivePortalServiceFactory::GetForProfile(profile); 1034 CaptivePortalServiceFactory::GetForProfile(profile);
997 captive_portal_service->set_test_url(test_url); 1035 captive_portal_service->set_test_url(test_url);
1036 captive_portal_service->SetPortalDetectionEnabledForTest(true);
998 1037
999 // Don't use any non-zero timers. Timers are checked in unit tests. 1038 // Don't use any non-zero timers. Timers are checked in unit tests.
1000 CaptivePortalService::RecheckPolicy* recheck_policy = 1039 CaptivePortalService::RecheckPolicy* recheck_policy =
1001 &captive_portal_service->recheck_policy(); 1040 &captive_portal_service->recheck_policy();
1002 recheck_policy->initial_backoff_no_portal_ms = 0; 1041 recheck_policy->initial_backoff_no_portal_ms = 0;
1003 recheck_policy->initial_backoff_portal_ms = 0; 1042 recheck_policy->initial_backoff_portal_ms = 0;
1004 recheck_policy->backoff_policy.maximum_backoff_ms = 0; 1043 recheck_policy->backoff_policy.maximum_backoff_ms = 0;
1005 } 1044 }
1006 1045
1007 bool CaptivePortalBrowserTest::CheckPending(Browser* browser) { 1046 bool CaptivePortalBrowserTest::CheckPending(Browser* browser) {
1008 CaptivePortalService* captive_portal_service = 1047 CaptivePortalService* captive_portal_service =
1009 CaptivePortalServiceFactory::GetForProfile(browser->profile()); 1048 CaptivePortalServiceFactory::GetForProfile(browser->profile());
1010 1049
1011 return captive_portal_service->DetectionInProgress() || 1050 return captive_portal_service->DetectionInProgress() ||
1012 captive_portal_service->TimerRunning(); 1051 captive_portal_service->TimerRunning();
1013 } 1052 }
1014 1053
1054 const void* CaptivePortalBrowserTest::GetInterstitialType(
1055 WebContents* contents) const {
1056 DCHECK(contents->ShowingInterstitialPage());
mmenke 2014/11/26 18:57:48 Rather than a DCHECK, seems like we should just as
meacer 2014/12/08 22:29:50 Done. Using NULL instead of nullptr to be consiste
1057 SecurityInterstitialPage* blocking_page =
1058 static_cast<SecurityInterstitialPage*>(
1059 contents->GetInterstitialPage()->GetDelegateForTesting());
1060 DCHECK(blocking_page);
1061 return blocking_page->GetTypeForTesting();
1062 }
1063
1015 CaptivePortalTabReloader::State CaptivePortalBrowserTest::GetStateOfTabReloader( 1064 CaptivePortalTabReloader::State CaptivePortalBrowserTest::GetStateOfTabReloader(
1016 WebContents* web_contents) const { 1065 WebContents* web_contents) const {
1017 return GetTabReloader(web_contents)->state(); 1066 return GetTabReloader(web_contents)->state();
1018 } 1067 }
1019 1068
1020 CaptivePortalTabReloader::State 1069 CaptivePortalTabReloader::State
1021 CaptivePortalBrowserTest::GetStateOfTabReloaderAt(Browser* browser, 1070 CaptivePortalBrowserTest::GetStateOfTabReloaderAt(Browser* browser,
1022 int index) const { 1071 int index) const {
1023 return GetStateOfTabReloader( 1072 return GetStateOfTabReloader(
1024 browser->tab_strip_model()->GetWebContentsAt(index)); 1073 browser->tab_strip_model()->GetWebContentsAt(index));
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
1237 1286
1238 // Reset the load time to be large, so the timer won't trigger on a reload. 1287 // Reset the load time to be large, so the timer won't trigger on a reload.
1239 SetSlowSSLLoadTime(tab_reloader, base::TimeDelta::FromHours(1)); 1288 SetSlowSSLLoadTime(tab_reloader, base::TimeDelta::FromHours(1));
1240 } 1289 }
1241 1290
1242 void CaptivePortalBrowserTest::FastTimeoutBehindCaptivePortal( 1291 void CaptivePortalBrowserTest::FastTimeoutBehindCaptivePortal(
1243 Browser* browser, 1292 Browser* browser,
1244 bool expect_open_login_tab) { 1293 bool expect_open_login_tab) {
1245 FastErrorBehindCaptivePortal(browser, 1294 FastErrorBehindCaptivePortal(browser,
1246 expect_open_login_tab, 1295 expect_open_login_tab,
1247 GURL(kMockHttpsQuickTimeoutUrl)); 1296 GURL(kMockHttpsQuickTimeoutUrl),
1297 false);
1248 } 1298 }
1249 1299
1250 void CaptivePortalBrowserTest::FastErrorBehindCaptivePortal( 1300 void CaptivePortalBrowserTest::FastErrorBehindCaptivePortal(
1251 Browser* browser, 1301 Browser* browser,
1252 bool expect_open_login_tab, 1302 bool expect_open_login_tab,
1253 const GURL& error_url) { 1303 const GURL& error_url,
1304 bool disable_portal_check_until_interstitial) {
1254 TabStripModel* tab_strip_model = browser->tab_strip_model(); 1305 TabStripModel* tab_strip_model = browser->tab_strip_model();
1255 // Calling this on a tab that's waiting for a load to manually be timed out 1306 // Calling this on a tab that's waiting for a load to manually be timed out
1256 // will result in a hang. 1307 // will result in a hang.
1257 ASSERT_FALSE(tab_strip_model->GetActiveWebContents()->IsLoading()); 1308 ASSERT_FALSE(tab_strip_model->GetActiveWebContents()->IsLoading());
1258 1309
1259 // Set the load time to be large, so the timer won't trigger. The value is 1310 // Set the load time to be large, so the timer won't trigger. The value is
1260 // not restored at the end of the function. 1311 // not restored at the end of the function.
1261 CaptivePortalTabReloader* tab_reloader = 1312 CaptivePortalTabReloader* tab_reloader =
1262 GetTabReloader(tab_strip_model->GetActiveWebContents()); 1313 GetTabReloader(tab_strip_model->GetActiveWebContents());
1263 ASSERT_TRUE(tab_reloader); 1314 ASSERT_TRUE(tab_reloader);
1264 SetSlowSSLLoadTime(tab_reloader, base::TimeDelta::FromHours(1)); 1315 SetSlowSSLLoadTime(tab_reloader, base::TimeDelta::FromHours(1));
1265 1316
1266 // Number of tabs expected to be open after the captive portal checks 1317 // Number of tabs expected to be open after the captive portal checks
1267 // have completed. 1318 // have completed.
1268 int initial_tab_count = tab_strip_model->count(); 1319 int initial_tab_count = tab_strip_model->count();
1269 int initial_active_index = tab_strip_model->active_index(); 1320 int initial_active_index = tab_strip_model->active_index();
1270 int initial_loading_tabs = NumLoadingTabs(); 1321 int initial_loading_tabs = NumLoadingTabs();
1271 int expected_broken_tabs = NumBrokenTabs(); 1322 int expected_broken_tabs = NumBrokenTabs();
1272 if (CaptivePortalTabReloader::STATE_BROKEN_BY_PORTAL != 1323 if (CaptivePortalTabReloader::STATE_BROKEN_BY_PORTAL !=
1273 GetStateOfTabReloader(tab_strip_model->GetActiveWebContents())) { 1324 GetStateOfTabReloader(tab_strip_model->GetActiveWebContents())) {
1274 ++expected_broken_tabs; 1325 ++expected_broken_tabs;
1275 } 1326 }
1276 1327
1328 CaptivePortalService* captive_portal_service =
1329 CaptivePortalServiceFactory::GetForProfile(browser->profile());
1330 if (disable_portal_check_until_interstitial)
1331 captive_portal_service->SetPortalDetectionEnabledForTest(false);
1332
1277 MultiNavigationObserver navigation_observer; 1333 MultiNavigationObserver navigation_observer;
1278 CaptivePortalObserver portal_observer(browser->profile()); 1334 CaptivePortalObserver portal_observer(browser->profile());
1279 ui_test_utils::NavigateToURLWithDisposition(browser, 1335 ui_test_utils::NavigateToURLWithDisposition(browser,
1280 error_url, 1336 error_url,
1281 CURRENT_TAB, 1337 CURRENT_TAB,
1282 ui_test_utils::BROWSER_TEST_NONE); 1338 ui_test_utils::BROWSER_TEST_NONE);
1339
1340 if (disable_portal_check_until_interstitial) {
1341 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1342 GetStateOfTabReloaderAt(browser, initial_active_index));
1343 // Once the interstitial is attached, probe for captive portal.
1344 WaitForInterstitialAttach(tab_strip_model->GetActiveWebContents());
1345 captive_portal_service->SetPortalDetectionEnabledForTest(true);
1346 captive_portal_service->DetectCaptivePortal();
1347 }
1348
1283 portal_observer.WaitForResults(1); 1349 portal_observer.WaitForResults(1);
1284 1350
1285 if (expect_open_login_tab) { 1351 if (expect_open_login_tab) {
1286 navigation_observer.WaitForNavigations(2); 1352 navigation_observer.WaitForNavigations(2);
1287 ASSERT_EQ(initial_tab_count + 1, tab_strip_model->count()); 1353 ASSERT_EQ(initial_tab_count + 1, tab_strip_model->count());
1288 EXPECT_EQ(initial_tab_count, tab_strip_model->active_index()); 1354 EXPECT_EQ(initial_tab_count, tab_strip_model->active_index());
1289 // Make sure that the originally active tab and the captive portal tab have 1355 // Make sure that the originally active tab and the captive portal tab have
1290 // each loaded once. 1356 // each loaded once.
1291 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab( 1357 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
1292 tab_strip_model->GetWebContentsAt(initial_active_index))); 1358 tab_strip_model->GetWebContentsAt(initial_active_index)));
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
1400 EXPECT_EQ(initial_tab_count, tab_strip_model->count()); 1466 EXPECT_EQ(initial_tab_count, tab_strip_model->count());
1401 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE, 1467 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1402 GetStateOfTabReloaderAt(browser, login_tab_index)); 1468 GetStateOfTabReloaderAt(browser, login_tab_index));
1403 EXPECT_FALSE(IsLoginTab(tab_strip_model->GetWebContentsAt(login_tab_index))); 1469 EXPECT_FALSE(IsLoginTab(tab_strip_model->GetWebContentsAt(login_tab_index)));
1404 1470
1405 // Make sure there were no unexpected navigations of the login tab. 1471 // Make sure there were no unexpected navigations of the login tab.
1406 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab( 1472 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
1407 tab_strip_model->GetWebContentsAt(login_tab_index))); 1473 tab_strip_model->GetWebContentsAt(login_tab_index)));
1408 } 1474 }
1409 1475
1476 void CaptivePortalBrowserTest::LoginCertError(Browser* browser) {
1477 TabStripModel* tab_strip_model = browser->tab_strip_model();
1478 URLRequestMockCaptivePortalJobFactory::SetBehindCaptivePortal(false);
1479 MultiNavigationObserver navigation_observer;
1480 CaptivePortalObserver portal_observer(browser->profile());
1481
1482 content::RenderFrameHost* render_frame_host =
1483 tab_strip_model->GetActiveWebContents()->GetMainFrame();
1484 render_frame_host->ExecuteJavaScript(base::ASCIIToUTF16("submitForm()"));
1485
1486 // The captive portal tab navigation will trigger a captive portal check,
1487 // and reloading the original tab will bring up the interstitial page again,
1488 // triggering a second captive portal check.
1489 portal_observer.WaitForResults(2);
1490
1491 // Wait for both tabs to finish loading.
1492 navigation_observer.WaitForNavigations(2);
1493 EXPECT_EQ(2, portal_observer.num_results_received());
1494 EXPECT_FALSE(CheckPending(browser));
1495 EXPECT_EQ(captive_portal::RESULT_INTERNET_CONNECTED,
1496 portal_observer.captive_portal_result());
1497
1498 // Check state of tabs. While the first tab is still displaying an
1499 // interstitial page, since no portal was found, it should be in STATE_NONE,
1500 // as should the login tab.
1501 ASSERT_EQ(2, tab_strip_model->count());
1502 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1503 GetStateOfTabReloaderAt(browser, 0));
1504 EXPECT_FALSE(IsLoginTab(tab_strip_model->GetWebContentsAt(1)));
1505 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1506 GetStateOfTabReloaderAt(browser, 1));
1507
1508 // Make sure only one navigation was for the login tab.
1509 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
1510 tab_strip_model->GetWebContentsAt(1)));
1511 }
1512
1410 void CaptivePortalBrowserTest::FailLoadsAfterLogin(Browser* browser, 1513 void CaptivePortalBrowserTest::FailLoadsAfterLogin(Browser* browser,
1411 int num_loading_tabs) { 1514 int num_loading_tabs) {
1412 ASSERT_EQ(num_loading_tabs, NumLoadingTabs()); 1515 ASSERT_EQ(num_loading_tabs, NumLoadingTabs());
1413 ASSERT_EQ(num_loading_tabs, NumNeedReloadTabs()); 1516 ASSERT_EQ(num_loading_tabs, NumNeedReloadTabs());
1414 EXPECT_EQ(0, NumBrokenTabs()); 1517 EXPECT_EQ(0, NumBrokenTabs());
1415 1518
1416 TabStripModel* tab_strip_model = browser->tab_strip_model(); 1519 TabStripModel* tab_strip_model = browser->tab_strip_model();
1417 int initial_num_tabs = tab_strip_model->count(); 1520 int initial_num_tabs = tab_strip_model->count();
1418 int initial_active_tab = tab_strip_model->active_index(); 1521 int initial_active_tab = tab_strip_model->active_index();
1419 1522
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
1539 // Timeout occurs, and page is automatically reloaded. 1642 // Timeout occurs, and page is automatically reloaded.
1540 FailLoadsAfterLogin(browser, 1); 1643 FailLoadsAfterLogin(browser, 1);
1541 } 1644 }
1542 1645
1543 void CaptivePortalBrowserTest::SetSlowSSLLoadTime( 1646 void CaptivePortalBrowserTest::SetSlowSSLLoadTime(
1544 CaptivePortalTabReloader* tab_reloader, 1647 CaptivePortalTabReloader* tab_reloader,
1545 base::TimeDelta slow_ssl_load_time) { 1648 base::TimeDelta slow_ssl_load_time) {
1546 tab_reloader->set_slow_ssl_load_time(slow_ssl_load_time); 1649 tab_reloader->set_slow_ssl_load_time(slow_ssl_load_time);
1547 } 1650 }
1548 1651
1652 void CaptivePortalBrowserTest::SetSSLErrorDisplayDelay(
1653 CaptivePortalTabHelper* tab_helper,
1654 base::TimeDelta ssl_error_delay) {
1655 tab_helper->SetSSLErrorDelayForTest(ssl_error_delay);
1656 }
1657
1549 CaptivePortalTabReloader* CaptivePortalBrowserTest::GetTabReloader( 1658 CaptivePortalTabReloader* CaptivePortalBrowserTest::GetTabReloader(
1550 WebContents* web_contents) const { 1659 WebContents* web_contents) const {
1551 return CaptivePortalTabHelper::FromWebContents(web_contents)-> 1660 return CaptivePortalTabHelper::FromWebContents(web_contents)->
1552 GetTabReloaderForTest(); 1661 GetTabReloaderForTest();
1553 } 1662 }
1554 1663
1555 // Make sure there's no test for a captive portal on HTTP timeouts. This will 1664 // Make sure there's no test for a captive portal on HTTP timeouts. This will
1556 // also trigger the link doctor page, which results in the load of a second 1665 // also trigger the link doctor page, which results in the load of a second
1557 // error page. 1666 // error page.
1558 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, HttpTimeout) { 1667 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, HttpTimeout) {
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
1698 } 1807 }
1699 1808
1700 // Checks the unlikely case that the tab times out before the timer triggers. 1809 // Checks the unlikely case that the tab times out before the timer triggers.
1701 // This most likely won't happen, but should still work: 1810 // This most likely won't happen, but should still work:
1702 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, LoginFastTimeout) { 1811 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, LoginFastTimeout) {
1703 FastTimeoutBehindCaptivePortal(browser(), true); 1812 FastTimeoutBehindCaptivePortal(browser(), true);
1704 Login(browser(), 0, 1); 1813 Login(browser(), 0, 1);
1705 } 1814 }
1706 1815
1707 // A cert error triggers a captive portal check and results in opening a login 1816 // A cert error triggers a captive portal check and results in opening a login
1708 // tab. The user then logs in and the page with the error is reloaded. 1817 // tab.
1709 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, SSLCertErrorLogin) { 1818 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest,
1710 // Need an HTTP TestServer to handle a dynamically created server redirect. 1819 ShowCaptivePortalInterstitialOnCertError) {
1711 ASSERT_TRUE(test_server()->Start());
1712
1713 net::SpawnedTestServer::SSLOptions https_options; 1820 net::SpawnedTestServer::SSLOptions https_options;
1714 https_options.server_certificate = 1821 https_options.server_certificate =
1715 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME; 1822 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
1716 net::SpawnedTestServer https_server( 1823 net::SpawnedTestServer https_server(
1717 net::SpawnedTestServer::TYPE_HTTPS, https_options, 1824 net::SpawnedTestServer::TYPE_HTTPS, https_options,
1718 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); 1825 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1719 ASSERT_TRUE(https_server.Start()); 1826 ASSERT_TRUE(https_server.Start());
1720 1827
1828 // Set SSL interstitial delay long enough so that a captive portal result
1829 // is guaranteed to arrive during this window, and a captive portal
1830 // error page is displayed instead of an SSL interstitial.
1831 TabStripModel* tab_strip_model = browser()->tab_strip_model();
1832 WebContents* broken_tab_contents = tab_strip_model->GetActiveWebContents();
1833 SetSSLErrorDisplayDelay(
1834 CaptivePortalTabHelper::FromWebContents(broken_tab_contents),
1835 base::TimeDelta::FromHours(1));
1836
1837 // The path does not matter.
1838 GURL cert_error_url = https_server.GetURL(kTestServerLoginPath);
1839 int cert_error_tab_index = tab_strip_model->active_index();
1840 // The interstitial should trigger a captive portal check when it opens, just
1841 // like navigating to kMockHttpsQuickTimeoutUrl.
1842 FastErrorBehindCaptivePortal(browser(), true, cert_error_url, false);
1843
1844 EXPECT_EQ(CaptivePortalBlockingPage::kTypeForTesting,
1845 GetInterstitialType(broken_tab_contents));
1846
1847 int login_tab_index = tab_strip_model->active_index();
1848 // Switch to the interstitial and click the "Connect" button. Should switch
1849 // active tab to the captive portal landing page.
1850 tab_strip_model->ActivateTabAt(cert_error_tab_index, false);
1851
1852 content::RenderViewHost* rvh =
1853 broken_tab_contents->GetInterstitialPage()->GetRenderViewHostForTesting();
1854 EXPECT_TRUE(WaitForPageReady(rvh));
1855 EXPECT_TRUE(
1856 content::ExecuteScript(
1857 rvh->GetMainFrame(),
1858 "document.getElementById('primary-button').click();"));
1859 EXPECT_EQ(login_tab_index, tab_strip_model->active_index());
1860
1861 LoginCertError(browser());
1862
1863 // Once logged in, broken tab should reload and display the SSL interstitial.
1864 WaitForInterstitialAttach(broken_tab_contents);
1865 tab_strip_model->ActivateTabAt(cert_error_tab_index, false);
1866
1867 EXPECT_EQ(SSLBlockingPage::kTypeForTesting,
1868 GetInterstitialType(tab_strip_model->GetActiveWebContents()));
1869
1870 // Trigger a final captive portal check. The captive portal interstitial
1871 // shouldn't get recreated.
1872 CaptivePortalObserver portal_observer(browser()->profile());
1873 CaptivePortalService* captive_portal_service =
1874 CaptivePortalServiceFactory::GetForProfile(browser()->profile());
1875 captive_portal_service->DetectCaptivePortal();
1876 portal_observer.WaitForResults(1);
1877 EXPECT_EQ(SSLBlockingPage::kTypeForTesting,
1878 GetInterstitialType(broken_tab_contents));
1879 }
1880
1881 // A cert error triggers a captive portal check and results in opening a login
1882 // tab. The user then logs in and the page with the error is reloaded.
1883 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, SSLCertErrorLogin) {
1884 // Need an HTTP TestServer to handle a dynamically created server redirect.
1885 ASSERT_TRUE(test_server()->Start());
1886
1887 net::SpawnedTestServer::SSLOptions https_options;
1888 https_options.server_certificate =
1889 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
1890 net::SpawnedTestServer https_server(
1891 net::SpawnedTestServer::TYPE_HTTPS, https_options,
1892 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1893 ASSERT_TRUE(https_server.Start());
1894
1895 // Set SSL interstitial delay to zero so that a captive portal result can not
1896 // arrive during this window, so an SSL interstitial is displayed instead
1897 // of a captive portal error page.
1898 TabStripModel* tab_strip_model = browser()->tab_strip_model();
1899 WebContents* broken_tab_contents = tab_strip_model->GetActiveWebContents();
1900 SetSSLErrorDisplayDelay(
1901 CaptivePortalTabHelper::FromWebContents(broken_tab_contents),
1902 base::TimeDelta());
1903
1904 // Setting the delay to zero above has a race condition: A captive portal
1905 // result triggered by a cert error can arrive before the SSL interstitial
1906 // display timer is fired, even though it's set to zero.
1907 // To avoid this, disable captive portal checks until the SSL interstitial is
1908 // displayed. Once it's displayed, enable portal checks and fire one.
1909 bool disable_portal_check_until_interstitial = true;
1910
1721 // The path does not matter. 1911 // The path does not matter.
1722 GURL cert_error_url = https_server.GetURL(kTestServerLoginPath); 1912 GURL cert_error_url = https_server.GetURL(kTestServerLoginPath);
1723 // The interstitial should trigger a captive portal check when it opens, just 1913 // The interstitial should trigger a captive portal check when it opens, just
1724 // like navigating to kMockHttpsQuickTimeoutUrl. 1914 // like navigating to kMockHttpsQuickTimeoutUrl.
1725 FastErrorBehindCaptivePortal(browser(), true, cert_error_url); 1915 FastErrorBehindCaptivePortal(
1916 browser(),
1917 true,
1918 cert_error_url,
1919 disable_portal_check_until_interstitial);
1726 1920
1727 // Simulate logging in. Can't use Login() because the interstitial tab looks 1921 EXPECT_EQ(SSLBlockingPage::kTypeForTesting,
1728 // like a cross between a hung tab (Load was never committed) and a tab at an 1922 GetInterstitialType(broken_tab_contents));
1729 // error page (The load was stopped).
1730 URLRequestMockCaptivePortalJobFactory::SetBehindCaptivePortal(false);
1731 MultiNavigationObserver navigation_observer;
1732 CaptivePortalObserver portal_observer(browser()->profile());
1733 1923
1734 TabStripModel* tab_strip_model = browser()->tab_strip_model(); 1924 LoginCertError(browser());
1735 content::RenderFrameHost* render_frame_host =
1736 tab_strip_model->GetActiveWebContents()->GetMainFrame();
1737 render_frame_host->ExecuteJavaScript(base::ASCIIToUTF16("submitForm()"));
1738
1739 // The captive portal tab navigation will trigger a captive portal check,
1740 // and reloading the original tab will bring up the interstitial page again,
1741 // triggering a second captive portal check.
1742 portal_observer.WaitForResults(2);
1743
1744 // Wait for both tabs to finish loading.
1745 navigation_observer.WaitForNavigations(2);
1746 EXPECT_EQ(2, portal_observer.num_results_received());
1747 EXPECT_FALSE(CheckPending(browser()));
1748 EXPECT_EQ(captive_portal::RESULT_INTERNET_CONNECTED,
1749 portal_observer.captive_portal_result());
1750
1751 // Check state of tabs. While the first tab is still displaying an
1752 // interstitial page, since no portal was found, it should be in STATE_NONE,
1753 // as should the login tab.
1754 ASSERT_EQ(2, tab_strip_model->count());
1755 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1756 GetStateOfTabReloaderAt(browser(), 0));
1757 EXPECT_FALSE(IsLoginTab(tab_strip_model->GetWebContentsAt(1)));
1758 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1759 GetStateOfTabReloaderAt(browser(), 1));
1760
1761 // Make sure only one navigation was for the login tab.
1762 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
1763 tab_strip_model->GetWebContentsAt(1)));
1764 } 1925 }
1765 1926
1766 // Tries navigating both the tab that encounters an SSL timeout and the 1927 // Tries navigating both the tab that encounters an SSL timeout and the
1767 // login tab twice, only logging in the second time. 1928 // login tab twice, only logging in the second time.
1768 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, LoginExtraNavigations) { 1929 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, LoginExtraNavigations) {
1769 FastTimeoutBehindCaptivePortal(browser(), true); 1930 FastTimeoutBehindCaptivePortal(browser(), true);
1770 1931
1771 // Activate the timed out tab and navigate it to a timeout again. 1932 // Activate the timed out tab and navigate it to a timeout again.
1772 TabStripModel* tab_strip_model = browser()->tab_strip_model(); 1933 TabStripModel* tab_strip_model = browser()->tab_strip_model();
1773 tab_strip_model->ActivateTabAt(0, true); 1934 tab_strip_model->ActivateTabAt(0, true);
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
2205 URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_TIMED_OUT); 2366 URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_TIMED_OUT);
2206 content::BrowserThread::PostTask( 2367 content::BrowserThread::PostTask(
2207 content::BrowserThread::IO, FROM_HERE, 2368 content::BrowserThread::IO, FROM_HERE,
2208 base::Bind(&AddHstsHost, 2369 base::Bind(&AddHstsHost,
2209 make_scoped_refptr(browser()->profile()->GetRequestContext()), 2370 make_scoped_refptr(browser()->profile()->GetRequestContext()),
2210 http_timeout_url.host())); 2371 http_timeout_url.host()));
2211 2372
2212 SlowLoadBehindCaptivePortal(browser(), true, http_timeout_url, 1, 1); 2373 SlowLoadBehindCaptivePortal(browser(), true, http_timeout_url, 1, 1);
2213 Login(browser(), 1, 0); 2374 Login(browser(), 1, 0);
2214 FailLoadsAfterLogin(browser(), 1); 2375 FailLoadsAfterLogin(browser(), 1);
2215 } 2376 }
mmenke 2014/11/26 18:57:48 One test I thought I asked about, but can't seem t
meacer 2014/12/08 22:29:50 Added InterstitialTimerCertErrorAfterSlowLoad. Thi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698